glam/f32/sse2/
mat2.rs

1// Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3use crate::{f32::math, swizzles::*, DMat2, Mat3, Mat3A, Vec2};
4use core::fmt;
5use core::iter::{Product, Sum};
6use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
7
8#[cfg(target_arch = "x86")]
9use core::arch::x86::*;
10#[cfg(target_arch = "x86_64")]
11use core::arch::x86_64::*;
12
13#[cfg(feature = "zerocopy")]
14use zerocopy_derive::*;
15
16#[repr(C)]
17union UnionCast {
18    a: [f32; 4],
19    v: Mat2,
20}
21
22/// Creates a 2x2 matrix from two column vectors.
23#[inline(always)]
24#[must_use]
25pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
26    Mat2::from_cols(x_axis, y_axis)
27}
28
29/// A 2x2 column major matrix.
30///
31/// SIMD vector types are used for storage on supported platforms.
32///
33/// This type is 16 byte aligned.
34#[derive(Clone, Copy)]
35#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
36#[cfg_attr(
37    feature = "zerocopy",
38    derive(FromBytes, Immutable, IntoBytes, KnownLayout)
39)]
40#[repr(transparent)]
41pub struct Mat2(pub(crate) __m128);
42
43impl Mat2 {
44    /// A 2x2 matrix with all elements set to `0.0`.
45    pub const ZERO: Self = Self::from_cols(Vec2::ZERO, Vec2::ZERO);
46
47    /// A 2x2 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
48    pub const IDENTITY: Self = Self::from_cols(Vec2::X, Vec2::Y);
49
50    /// All NAN:s.
51    pub const NAN: Self = Self::from_cols(Vec2::NAN, Vec2::NAN);
52
53    #[allow(clippy::too_many_arguments)]
54    #[inline(always)]
55    #[must_use]
56    const fn new(m00: f32, m01: f32, m10: f32, m11: f32) -> Self {
57        unsafe {
58            UnionCast {
59                a: [m00, m01, m10, m11],
60            }
61            .v
62        }
63    }
64
65    /// Creates a 2x2 matrix from two column vectors.
66    #[inline(always)]
67    #[must_use]
68    pub const fn from_cols(x_axis: Vec2, y_axis: Vec2) -> Self {
69        unsafe {
70            UnionCast {
71                a: [x_axis.x, x_axis.y, y_axis.x, y_axis.y],
72            }
73            .v
74        }
75    }
76
77    /// Creates a 2x2 matrix from a `[f32; 4]` array stored in column major order.
78    /// If your data is stored in row major you will need to `transpose` the returned
79    /// matrix.
80    #[inline]
81    #[must_use]
82    pub const fn from_cols_array(m: &[f32; 4]) -> Self {
83        Self::new(m[0], m[1], m[2], m[3])
84    }
85
86    /// Creates a `[f32; 4]` array storing data in column major order.
87    /// If you require data in row major order `transpose` the matrix first.
88    #[inline]
89    #[must_use]
90    pub const fn to_cols_array(&self) -> [f32; 4] {
91        unsafe { *(self as *const Self as *const [f32; 4]) }
92    }
93
94    /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
95    /// If your data is in row major order you will need to `transpose` the returned
96    /// matrix.
97    #[inline]
98    #[must_use]
99    pub const fn from_cols_array_2d(m: &[[f32; 2]; 2]) -> Self {
100        Self::from_cols(Vec2::from_array(m[0]), Vec2::from_array(m[1]))
101    }
102
103    /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order.
104    /// If you require data in row major order `transpose` the matrix first.
105    #[inline]
106    #[must_use]
107    pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] {
108        unsafe { *(self as *const Self as *const [[f32; 2]; 2]) }
109    }
110
111    /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.
112    #[doc(alias = "scale")]
113    #[inline]
114    #[must_use]
115    pub const fn from_diagonal(diagonal: Vec2) -> Self {
116        Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
117    }
118
119    /// Creates a 2x2 matrix containing the combining non-uniform `scale` and rotation of
120    /// `angle` (in radians).
121    #[inline]
122    #[must_use]
123    pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
124        let (sin, cos) = math::sin_cos(angle);
125        Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
126    }
127
128    /// Creates a 2x2 matrix containing a rotation of `angle` (in radians).
129    #[inline]
130    #[must_use]
131    pub fn from_angle(angle: f32) -> Self {
132        let (sin, cos) = math::sin_cos(angle);
133        Self::new(cos, sin, -sin, cos)
134    }
135
136    /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
137    #[inline]
138    #[must_use]
139    pub fn from_mat3(m: Mat3) -> Self {
140        Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
141    }
142
143    /// Creates a 2x2 matrix from the minor of the given 3x3 matrix, discarding the `i`th column
144    /// and `j`th row.
145    ///
146    /// # Panics
147    ///
148    /// Panics if `i` or `j` is greater than 2.
149    #[inline]
150    #[must_use]
151    pub fn from_mat3_minor(m: Mat3, i: usize, j: usize) -> Self {
152        match (i, j) {
153            (0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
154            (0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
155            (0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
156            (1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
157            (1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
158            (1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
159            (2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
160            (2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
161            (2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
162            _ => panic!("index out of bounds"),
163        }
164    }
165
166    /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
167    #[inline]
168    #[must_use]
169    pub fn from_mat3a(m: Mat3A) -> Self {
170        Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
171    }
172
173    /// Creates a 2x2 matrix from the minor of the given 3x3 matrix, discarding the `i`th column
174    /// and `j`th row.
175    ///
176    /// # Panics
177    ///
178    /// Panics if `i` or `j` is greater than 2.
179    #[inline]
180    #[must_use]
181    pub fn from_mat3a_minor(m: Mat3A, i: usize, j: usize) -> Self {
182        match (i, j) {
183            (0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
184            (0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
185            (0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
186            (1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
187            (1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
188            (1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
189            (2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
190            (2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
191            (2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
192            _ => panic!("index out of bounds"),
193        }
194    }
195
196    /// Creates a 2x2 matrix from the first 4 values in `slice`.
197    ///
198    /// # Panics
199    ///
200    /// Panics if `slice` is less than 4 elements long.
201    #[inline]
202    #[must_use]
203    pub const fn from_cols_slice(slice: &[f32]) -> Self {
204        Self::new(slice[0], slice[1], slice[2], slice[3])
205    }
206
207    /// Writes the columns of `self` to the first 4 elements in `slice`.
208    ///
209    /// # Panics
210    ///
211    /// Panics if `slice` is less than 4 elements long.
212    #[inline]
213    pub fn write_cols_to_slice(self, slice: &mut [f32]) {
214        slice[0] = self.x_axis.x;
215        slice[1] = self.x_axis.y;
216        slice[2] = self.y_axis.x;
217        slice[3] = self.y_axis.y;
218    }
219
220    /// Returns the matrix column for the given `index`.
221    ///
222    /// # Panics
223    ///
224    /// Panics if `index` is greater than 1.
225    #[inline]
226    #[must_use]
227    pub fn col(&self, index: usize) -> Vec2 {
228        match index {
229            0 => self.x_axis,
230            1 => self.y_axis,
231            _ => panic!("index out of bounds"),
232        }
233    }
234
235    /// Returns a mutable reference to the matrix column for the given `index`.
236    ///
237    /// # Panics
238    ///
239    /// Panics if `index` is greater than 1.
240    #[inline]
241    pub fn col_mut(&mut self, index: usize) -> &mut Vec2 {
242        match index {
243            0 => &mut self.x_axis,
244            1 => &mut self.y_axis,
245            _ => panic!("index out of bounds"),
246        }
247    }
248
249    /// Returns the matrix row for the given `index`.
250    ///
251    /// # Panics
252    ///
253    /// Panics if `index` is greater than 1.
254    #[inline]
255    #[must_use]
256    pub fn row(&self, index: usize) -> Vec2 {
257        match index {
258            0 => Vec2::new(self.x_axis.x, self.y_axis.x),
259            1 => Vec2::new(self.x_axis.y, self.y_axis.y),
260            _ => panic!("index out of bounds"),
261        }
262    }
263
264    /// Returns `true` if, and only if, all elements are finite.
265    /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
266    #[inline]
267    #[must_use]
268    pub fn is_finite(&self) -> bool {
269        self.x_axis.is_finite() && self.y_axis.is_finite()
270    }
271
272    /// Returns `true` if any elements are `NaN`.
273    #[inline]
274    #[must_use]
275    pub fn is_nan(&self) -> bool {
276        self.x_axis.is_nan() || self.y_axis.is_nan()
277    }
278
279    /// Returns the transpose of `self`.
280    #[inline]
281    #[must_use]
282    pub fn transpose(&self) -> Self {
283        Self(unsafe { _mm_shuffle_ps(self.0, self.0, 0b11_01_10_00) })
284    }
285
286    /// Returns the determinant of `self`.
287    #[inline]
288    #[must_use]
289    pub fn determinant(&self) -> f32 {
290        unsafe {
291            let abcd = self.0;
292            let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11);
293            let prod = _mm_mul_ps(abcd, dcba);
294            let det = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01));
295            _mm_cvtss_f32(det)
296        }
297    }
298
299    /// Returns the inverse of `self`.
300    ///
301    /// If the matrix is not invertible the returned matrix will be invalid.
302    ///
303    /// # Panics
304    ///
305    /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
306    #[inline]
307    #[must_use]
308    pub fn inverse(&self) -> Self {
309        unsafe {
310            const SIGN: __m128 = crate::sse2::m128_from_f32x4([1.0, -1.0, -1.0, 1.0]);
311            let abcd = self.0;
312            let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11);
313            let prod = _mm_mul_ps(abcd, dcba);
314            let sub = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01));
315            let det = _mm_shuffle_ps(sub, sub, 0b00_00_00_00);
316            let tmp = _mm_div_ps(SIGN, det);
317            glam_assert!(Mat2(tmp).is_finite());
318            let dbca = _mm_shuffle_ps(abcd, abcd, 0b00_10_01_11);
319            Self(_mm_mul_ps(dbca, tmp))
320        }
321    }
322
323    /// Transforms a 2D vector.
324    #[inline]
325    #[must_use]
326    pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
327        unsafe {
328            use crate::Align16;
329            use core::mem::MaybeUninit;
330            let abcd = self.0;
331            let xxyy = _mm_set_ps(rhs.y, rhs.y, rhs.x, rhs.x);
332            let axbxcydy = _mm_mul_ps(abcd, xxyy);
333            let cydyaxbx = _mm_shuffle_ps(axbxcydy, axbxcydy, 0b01_00_11_10);
334            let result = _mm_add_ps(axbxcydy, cydyaxbx);
335            let mut out: MaybeUninit<Align16<Vec2>> = MaybeUninit::uninit();
336            _mm_store_ps(out.as_mut_ptr().cast(), result);
337            out.assume_init().0
338        }
339    }
340
341    /// Multiplies two 2x2 matrices.
342    #[inline]
343    #[must_use]
344    pub fn mul_mat2(&self, rhs: &Self) -> Self {
345        self.mul(rhs)
346    }
347
348    /// Adds two 2x2 matrices.
349    #[inline]
350    #[must_use]
351    pub fn add_mat2(&self, rhs: &Self) -> Self {
352        self.add(rhs)
353    }
354
355    /// Subtracts two 2x2 matrices.
356    #[inline]
357    #[must_use]
358    pub fn sub_mat2(&self, rhs: &Self) -> Self {
359        self.sub(rhs)
360    }
361
362    /// Multiplies a 2x2 matrix by a scalar.
363    #[inline]
364    #[must_use]
365    pub fn mul_scalar(&self, rhs: f32) -> Self {
366        Self(unsafe { _mm_mul_ps(self.0, _mm_set_ps1(rhs)) })
367    }
368
369    /// Divides a 2x2 matrix by a scalar.
370    #[inline]
371    #[must_use]
372    pub fn div_scalar(&self, rhs: f32) -> Self {
373        Self(unsafe { _mm_div_ps(self.0, _mm_set_ps1(rhs)) })
374    }
375
376    /// Returns true if the absolute difference of all elements between `self` and `rhs`
377    /// is less than or equal to `max_abs_diff`.
378    ///
379    /// This can be used to compare if two matrices contain similar elements. It works best
380    /// when comparing with a known value. The `max_abs_diff` that should be used used
381    /// depends on the values being compared against.
382    ///
383    /// For more see
384    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
385    #[inline]
386    #[must_use]
387    pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
388        self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
389            && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
390    }
391
392    /// Takes the absolute value of each element in `self`
393    #[inline]
394    #[must_use]
395    pub fn abs(&self) -> Self {
396        Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
397    }
398
399    #[inline]
400    pub fn as_dmat2(&self) -> DMat2 {
401        DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
402    }
403}
404
405impl Default for Mat2 {
406    #[inline]
407    fn default() -> Self {
408        Self::IDENTITY
409    }
410}
411
412impl Add for Mat2 {
413    type Output = Self;
414    #[inline]
415    fn add(self, rhs: Self) -> Self {
416        Self(unsafe { _mm_add_ps(self.0, rhs.0) })
417    }
418}
419
420impl Add<&Self> for Mat2 {
421    type Output = Self;
422    #[inline]
423    fn add(self, rhs: &Self) -> Self {
424        self.add(*rhs)
425    }
426}
427
428impl Add<&Mat2> for &Mat2 {
429    type Output = Mat2;
430    #[inline]
431    fn add(self, rhs: &Mat2) -> Mat2 {
432        (*self).add(*rhs)
433    }
434}
435
436impl Add<Mat2> for &Mat2 {
437    type Output = Mat2;
438    #[inline]
439    fn add(self, rhs: Mat2) -> Mat2 {
440        (*self).add(rhs)
441    }
442}
443
444impl AddAssign for Mat2 {
445    #[inline]
446    fn add_assign(&mut self, rhs: Self) {
447        *self = self.add(rhs);
448    }
449}
450
451impl AddAssign<&Self> for Mat2 {
452    #[inline]
453    fn add_assign(&mut self, rhs: &Self) {
454        self.add_assign(*rhs);
455    }
456}
457
458impl Sub for Mat2 {
459    type Output = Self;
460    #[inline]
461    fn sub(self, rhs: Self) -> Self {
462        Self(unsafe { _mm_sub_ps(self.0, rhs.0) })
463    }
464}
465
466impl Sub<&Self> for Mat2 {
467    type Output = Self;
468    #[inline]
469    fn sub(self, rhs: &Self) -> Self {
470        self.sub(*rhs)
471    }
472}
473
474impl Sub<&Mat2> for &Mat2 {
475    type Output = Mat2;
476    #[inline]
477    fn sub(self, rhs: &Mat2) -> Mat2 {
478        (*self).sub(*rhs)
479    }
480}
481
482impl Sub<Mat2> for &Mat2 {
483    type Output = Mat2;
484    #[inline]
485    fn sub(self, rhs: Mat2) -> Mat2 {
486        (*self).sub(rhs)
487    }
488}
489
490impl SubAssign for Mat2 {
491    #[inline]
492    fn sub_assign(&mut self, rhs: Self) {
493        *self = self.sub(rhs);
494    }
495}
496
497impl SubAssign<&Self> for Mat2 {
498    #[inline]
499    fn sub_assign(&mut self, rhs: &Self) {
500        self.sub_assign(*rhs);
501    }
502}
503
504impl Neg for Mat2 {
505    type Output = Self;
506    #[inline]
507    fn neg(self) -> Self::Output {
508        Self(unsafe { _mm_xor_ps(self.0, _mm_set1_ps(-0.0)) })
509    }
510}
511
512impl Neg for &Mat2 {
513    type Output = Mat2;
514    #[inline]
515    fn neg(self) -> Mat2 {
516        (*self).neg()
517    }
518}
519
520impl Mul for Mat2 {
521    type Output = Self;
522    #[inline]
523    fn mul(self, rhs: Self) -> Self {
524        unsafe {
525            let abcd = self.0;
526            let rhs = rhs.0;
527            let xxyy0 = _mm_shuffle_ps(rhs, rhs, 0b01_01_00_00);
528            let xxyy1 = _mm_shuffle_ps(rhs, rhs, 0b11_11_10_10);
529            let axbxcydy0 = _mm_mul_ps(abcd, xxyy0);
530            let axbxcydy1 = _mm_mul_ps(abcd, xxyy1);
531            let cydyaxbx0 = _mm_shuffle_ps(axbxcydy0, axbxcydy0, 0b01_00_11_10);
532            let cydyaxbx1 = _mm_shuffle_ps(axbxcydy1, axbxcydy1, 0b01_00_11_10);
533            let result0 = _mm_add_ps(axbxcydy0, cydyaxbx0);
534            let result1 = _mm_add_ps(axbxcydy1, cydyaxbx1);
535            Self(_mm_shuffle_ps(result0, result1, 0b01_00_01_00))
536        }
537    }
538}
539
540impl Mul<&Self> for Mat2 {
541    type Output = Self;
542    #[inline]
543    fn mul(self, rhs: &Self) -> Self {
544        self.mul(*rhs)
545    }
546}
547
548impl Mul<&Mat2> for &Mat2 {
549    type Output = Mat2;
550    #[inline]
551    fn mul(self, rhs: &Mat2) -> Mat2 {
552        (*self).mul(*rhs)
553    }
554}
555
556impl Mul<Mat2> for &Mat2 {
557    type Output = Mat2;
558    #[inline]
559    fn mul(self, rhs: Mat2) -> Mat2 {
560        (*self).mul(rhs)
561    }
562}
563
564impl MulAssign for Mat2 {
565    #[inline]
566    fn mul_assign(&mut self, rhs: Self) {
567        *self = self.mul(rhs);
568    }
569}
570
571impl MulAssign<&Self> for Mat2 {
572    #[inline]
573    fn mul_assign(&mut self, rhs: &Self) {
574        self.mul_assign(*rhs);
575    }
576}
577
578impl Mul<Vec2> for Mat2 {
579    type Output = Vec2;
580    #[inline]
581    fn mul(self, rhs: Vec2) -> Self::Output {
582        self.mul_vec2(rhs)
583    }
584}
585
586impl Mul<&Vec2> for Mat2 {
587    type Output = Vec2;
588    #[inline]
589    fn mul(self, rhs: &Vec2) -> Vec2 {
590        self.mul(*rhs)
591    }
592}
593
594impl Mul<&Vec2> for &Mat2 {
595    type Output = Vec2;
596    #[inline]
597    fn mul(self, rhs: &Vec2) -> Vec2 {
598        (*self).mul(*rhs)
599    }
600}
601
602impl Mul<Vec2> for &Mat2 {
603    type Output = Vec2;
604    #[inline]
605    fn mul(self, rhs: Vec2) -> Vec2 {
606        (*self).mul(rhs)
607    }
608}
609
610impl Mul<Mat2> for f32 {
611    type Output = Mat2;
612    #[inline]
613    fn mul(self, rhs: Mat2) -> Self::Output {
614        rhs.mul_scalar(self)
615    }
616}
617
618impl Mul<&Mat2> for f32 {
619    type Output = Mat2;
620    #[inline]
621    fn mul(self, rhs: &Mat2) -> Mat2 {
622        self.mul(*rhs)
623    }
624}
625
626impl Mul<&Mat2> for &f32 {
627    type Output = Mat2;
628    #[inline]
629    fn mul(self, rhs: &Mat2) -> Mat2 {
630        (*self).mul(*rhs)
631    }
632}
633
634impl Mul<Mat2> for &f32 {
635    type Output = Mat2;
636    #[inline]
637    fn mul(self, rhs: Mat2) -> Mat2 {
638        (*self).mul(rhs)
639    }
640}
641
642impl Mul<f32> for Mat2 {
643    type Output = Self;
644    #[inline]
645    fn mul(self, rhs: f32) -> Self {
646        self.mul_scalar(rhs)
647    }
648}
649
650impl Mul<&f32> for Mat2 {
651    type Output = Self;
652    #[inline]
653    fn mul(self, rhs: &f32) -> Self {
654        self.mul(*rhs)
655    }
656}
657
658impl Mul<&f32> for &Mat2 {
659    type Output = Mat2;
660    #[inline]
661    fn mul(self, rhs: &f32) -> Mat2 {
662        (*self).mul(*rhs)
663    }
664}
665
666impl Mul<f32> for &Mat2 {
667    type Output = Mat2;
668    #[inline]
669    fn mul(self, rhs: f32) -> Mat2 {
670        (*self).mul(rhs)
671    }
672}
673
674impl MulAssign<f32> for Mat2 {
675    #[inline]
676    fn mul_assign(&mut self, rhs: f32) {
677        *self = self.mul(rhs);
678    }
679}
680
681impl MulAssign<&f32> for Mat2 {
682    #[inline]
683    fn mul_assign(&mut self, rhs: &f32) {
684        self.mul_assign(*rhs);
685    }
686}
687
688impl Div<Mat2> for f32 {
689    type Output = Mat2;
690    #[inline]
691    fn div(self, rhs: Mat2) -> Self::Output {
692        rhs.div_scalar(self)
693    }
694}
695
696impl Div<&Mat2> for f32 {
697    type Output = Mat2;
698    #[inline]
699    fn div(self, rhs: &Mat2) -> Mat2 {
700        self.div(*rhs)
701    }
702}
703
704impl Div<&Mat2> for &f32 {
705    type Output = Mat2;
706    #[inline]
707    fn div(self, rhs: &Mat2) -> Mat2 {
708        (*self).div(*rhs)
709    }
710}
711
712impl Div<Mat2> for &f32 {
713    type Output = Mat2;
714    #[inline]
715    fn div(self, rhs: Mat2) -> Mat2 {
716        (*self).div(rhs)
717    }
718}
719
720impl Div<f32> for Mat2 {
721    type Output = Self;
722    #[inline]
723    fn div(self, rhs: f32) -> Self {
724        self.div_scalar(rhs)
725    }
726}
727
728impl Div<&f32> for Mat2 {
729    type Output = Self;
730    #[inline]
731    fn div(self, rhs: &f32) -> Self {
732        self.div(*rhs)
733    }
734}
735
736impl Div<&f32> for &Mat2 {
737    type Output = Mat2;
738    #[inline]
739    fn div(self, rhs: &f32) -> Mat2 {
740        (*self).div(*rhs)
741    }
742}
743
744impl Div<f32> for &Mat2 {
745    type Output = Mat2;
746    #[inline]
747    fn div(self, rhs: f32) -> Mat2 {
748        (*self).div(rhs)
749    }
750}
751
752impl DivAssign<f32> for Mat2 {
753    #[inline]
754    fn div_assign(&mut self, rhs: f32) {
755        *self = self.div(rhs);
756    }
757}
758
759impl DivAssign<&f32> for Mat2 {
760    #[inline]
761    fn div_assign(&mut self, rhs: &f32) {
762        self.div_assign(*rhs);
763    }
764}
765
766impl Sum<Self> for Mat2 {
767    fn sum<I>(iter: I) -> Self
768    where
769        I: Iterator<Item = Self>,
770    {
771        iter.fold(Self::ZERO, Self::add)
772    }
773}
774
775impl<'a> Sum<&'a Self> for Mat2 {
776    fn sum<I>(iter: I) -> Self
777    where
778        I: Iterator<Item = &'a Self>,
779    {
780        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
781    }
782}
783
784impl Product for Mat2 {
785    fn product<I>(iter: I) -> Self
786    where
787        I: Iterator<Item = Self>,
788    {
789        iter.fold(Self::IDENTITY, Self::mul)
790    }
791}
792
793impl<'a> Product<&'a Self> for Mat2 {
794    fn product<I>(iter: I) -> Self
795    where
796        I: Iterator<Item = &'a Self>,
797    {
798        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
799    }
800}
801
802impl PartialEq for Mat2 {
803    #[inline]
804    fn eq(&self, rhs: &Self) -> bool {
805        self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
806    }
807}
808
809impl AsRef<[f32; 4]> for Mat2 {
810    #[inline]
811    fn as_ref(&self) -> &[f32; 4] {
812        unsafe { &*(self as *const Self as *const [f32; 4]) }
813    }
814}
815
816impl AsMut<[f32; 4]> for Mat2 {
817    #[inline]
818    fn as_mut(&mut self) -> &mut [f32; 4] {
819        unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
820    }
821}
822
823impl core::ops::Deref for Mat2 {
824    type Target = crate::deref::Cols2<Vec2>;
825    #[inline]
826    fn deref(&self) -> &Self::Target {
827        unsafe { &*(self as *const Self as *const Self::Target) }
828    }
829}
830
831impl core::ops::DerefMut for Mat2 {
832    #[inline]
833    fn deref_mut(&mut self) -> &mut Self::Target {
834        unsafe { &mut *(self as *mut Self as *mut Self::Target) }
835    }
836}
837
838impl fmt::Debug for Mat2 {
839    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
840        fmt.debug_struct(stringify!(Mat2))
841            .field("x_axis", &self.x_axis)
842            .field("y_axis", &self.y_axis)
843            .finish()
844    }
845}
846
847impl fmt::Display for Mat2 {
848    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
849        if let Some(p) = f.precision() {
850            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
851        } else {
852            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
853        }
854    }
855}