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 diagonal of `self`.
287    #[inline]
288    #[must_use]
289    pub fn diagonal(&self) -> Vec2 {
290        Vec2::new(self.x_axis.x, self.y_axis.y)
291    }
292
293    /// Returns the determinant of `self`.
294    #[inline]
295    #[must_use]
296    pub fn determinant(&self) -> f32 {
297        unsafe {
298            let abcd = self.0;
299            let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11);
300            let prod = _mm_mul_ps(abcd, dcba);
301            let det = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01));
302            _mm_cvtss_f32(det)
303        }
304    }
305
306    /// If `CHECKED` is true then if the determinant is zero this function will return a tuple
307    /// containing a zero matrix and false. If the determinant is non zero a tuple containing the
308    /// inverted matrix and true is returned.
309    ///
310    /// If `CHECKED` is false then the determinant is not checked and if it is zero the resulting
311    /// inverted matrix will be invalid. Will panic if the determinant of `self` is zero when
312    /// `glam_assert` is enabled.
313    ///
314    /// A tuple containing the inverted matrix and a bool is used instead of an option here as
315    /// regular Rust enums put the discriminant first which can result in a lot of padding if the
316    /// matrix is aligned.
317    #[inline(always)]
318    #[must_use]
319    fn inverse_checked<const CHECKED: bool>(&self) -> (Self, bool) {
320        unsafe {
321            use crate::Vec4;
322            const SIGN: __m128 = crate::sse2::m128_from_f32x4([1.0, -1.0, -1.0, 1.0]);
323            let abcd = self.0;
324            let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11);
325            let prod = _mm_mul_ps(abcd, dcba);
326            let sub = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01));
327            let det = _mm_shuffle_ps(sub, sub, 0b00_00_00_00);
328            if CHECKED {
329                if Vec4(det) == Vec4::ZERO {
330                    return (Self::ZERO, false);
331                }
332            } else {
333                glam_assert!(Vec4(det).cmpne(Vec4::ZERO).all());
334            }
335            let tmp = _mm_div_ps(SIGN, det);
336            let dbca = _mm_shuffle_ps(abcd, abcd, 0b00_10_01_11);
337            (Self(_mm_mul_ps(dbca, tmp)), true)
338        }
339    }
340
341    /// Returns the inverse of `self`.
342    ///
343    /// If the matrix is not invertible the returned matrix will be invalid.
344    ///
345    /// # Panics
346    ///
347    /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
348    #[inline]
349    #[must_use]
350    pub fn inverse(&self) -> Self {
351        self.inverse_checked::<false>().0
352    }
353
354    /// Returns the inverse of `self` or `None` if the matrix is not invertible.
355    #[inline]
356    #[must_use]
357    pub fn try_inverse(&self) -> Option<Self> {
358        let (m, is_valid) = self.inverse_checked::<true>();
359        if is_valid {
360            Some(m)
361        } else {
362            None
363        }
364    }
365
366    /// Returns the inverse of `self` or `Mat2::ZERO` if the matrix is not invertible.
367    #[inline]
368    #[must_use]
369    pub fn inverse_or_zero(&self) -> Self {
370        self.inverse_checked::<true>().0
371    }
372
373    /// Transforms a 2D vector.
374    #[inline]
375    #[must_use]
376    pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
377        unsafe {
378            use crate::Align16;
379            use core::mem::MaybeUninit;
380            let abcd = self.0;
381            let xxyy = _mm_set_ps(rhs.y, rhs.y, rhs.x, rhs.x);
382            let axbxcydy = _mm_mul_ps(abcd, xxyy);
383            let cydyaxbx = _mm_shuffle_ps(axbxcydy, axbxcydy, 0b01_00_11_10);
384            let result = _mm_add_ps(axbxcydy, cydyaxbx);
385            let mut out: MaybeUninit<Align16<Vec2>> = MaybeUninit::uninit();
386            _mm_store_ps(out.as_mut_ptr().cast(), result);
387            out.assume_init().0
388        }
389    }
390
391    /// Transforms a 2D vector by the transpose of `self`.
392    #[inline]
393    #[must_use]
394    pub fn mul_transpose_vec2(&self, rhs: Vec2) -> Vec2 {
395        Vec2::new(self.x_axis.dot(rhs), self.y_axis.dot(rhs))
396    }
397
398    /// Multiplies two 2x2 matrices.
399    #[inline]
400    #[must_use]
401    pub fn mul_mat2(&self, rhs: &Self) -> Self {
402        self.mul(rhs)
403    }
404
405    /// Adds two 2x2 matrices.
406    #[inline]
407    #[must_use]
408    pub fn add_mat2(&self, rhs: &Self) -> Self {
409        self.add(rhs)
410    }
411
412    /// Subtracts two 2x2 matrices.
413    #[inline]
414    #[must_use]
415    pub fn sub_mat2(&self, rhs: &Self) -> Self {
416        self.sub(rhs)
417    }
418
419    /// Multiplies a 2x2 matrix by a scalar.
420    #[inline]
421    #[must_use]
422    pub fn mul_scalar(&self, rhs: f32) -> Self {
423        Self(unsafe { _mm_mul_ps(self.0, _mm_set_ps1(rhs)) })
424    }
425
426    /// Multiply `self` by a scaling vector `scale`.
427    /// This is faster than creating a whole diagonal scaling matrix and then multiplying that.
428    /// This operation is commutative.
429    #[inline]
430    #[must_use]
431    pub fn mul_diagonal_scale(&self, scale: Vec2) -> Self {
432        Self::from_cols(self.x_axis * scale.x, self.y_axis * scale.y)
433    }
434
435    /// Divides a 2x2 matrix by a scalar.
436    #[inline]
437    #[must_use]
438    pub fn div_scalar(&self, rhs: f32) -> Self {
439        Self(unsafe { _mm_div_ps(self.0, _mm_set_ps1(rhs)) })
440    }
441
442    /// Returns true if the absolute difference of all elements between `self` and `rhs`
443    /// is less than or equal to `max_abs_diff`.
444    ///
445    /// This can be used to compare if two matrices contain similar elements. It works best
446    /// when comparing with a known value. The `max_abs_diff` that should be used used
447    /// depends on the values being compared against.
448    ///
449    /// For more see
450    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
451    #[inline]
452    #[must_use]
453    pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
454        self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
455            && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
456    }
457
458    /// Takes the absolute value of each element in `self`
459    #[inline]
460    #[must_use]
461    pub fn abs(&self) -> Self {
462        Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
463    }
464
465    #[inline]
466    pub fn as_dmat2(&self) -> DMat2 {
467        DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
468    }
469}
470
471impl Default for Mat2 {
472    #[inline]
473    fn default() -> Self {
474        Self::IDENTITY
475    }
476}
477
478impl Add for Mat2 {
479    type Output = Self;
480    #[inline]
481    fn add(self, rhs: Self) -> Self {
482        Self(unsafe { _mm_add_ps(self.0, rhs.0) })
483    }
484}
485
486impl Add<&Self> for Mat2 {
487    type Output = Self;
488    #[inline]
489    fn add(self, rhs: &Self) -> Self {
490        self.add(*rhs)
491    }
492}
493
494impl Add<&Mat2> for &Mat2 {
495    type Output = Mat2;
496    #[inline]
497    fn add(self, rhs: &Mat2) -> Mat2 {
498        (*self).add(*rhs)
499    }
500}
501
502impl Add<Mat2> for &Mat2 {
503    type Output = Mat2;
504    #[inline]
505    fn add(self, rhs: Mat2) -> Mat2 {
506        (*self).add(rhs)
507    }
508}
509
510impl AddAssign for Mat2 {
511    #[inline]
512    fn add_assign(&mut self, rhs: Self) {
513        *self = self.add(rhs);
514    }
515}
516
517impl AddAssign<&Self> for Mat2 {
518    #[inline]
519    fn add_assign(&mut self, rhs: &Self) {
520        self.add_assign(*rhs);
521    }
522}
523
524impl Sub for Mat2 {
525    type Output = Self;
526    #[inline]
527    fn sub(self, rhs: Self) -> Self {
528        Self(unsafe { _mm_sub_ps(self.0, rhs.0) })
529    }
530}
531
532impl Sub<&Self> for Mat2 {
533    type Output = Self;
534    #[inline]
535    fn sub(self, rhs: &Self) -> Self {
536        self.sub(*rhs)
537    }
538}
539
540impl Sub<&Mat2> for &Mat2 {
541    type Output = Mat2;
542    #[inline]
543    fn sub(self, rhs: &Mat2) -> Mat2 {
544        (*self).sub(*rhs)
545    }
546}
547
548impl Sub<Mat2> for &Mat2 {
549    type Output = Mat2;
550    #[inline]
551    fn sub(self, rhs: Mat2) -> Mat2 {
552        (*self).sub(rhs)
553    }
554}
555
556impl SubAssign for Mat2 {
557    #[inline]
558    fn sub_assign(&mut self, rhs: Self) {
559        *self = self.sub(rhs);
560    }
561}
562
563impl SubAssign<&Self> for Mat2 {
564    #[inline]
565    fn sub_assign(&mut self, rhs: &Self) {
566        self.sub_assign(*rhs);
567    }
568}
569
570impl Neg for Mat2 {
571    type Output = Self;
572    #[inline]
573    fn neg(self) -> Self::Output {
574        Self(unsafe { _mm_xor_ps(self.0, _mm_set1_ps(-0.0)) })
575    }
576}
577
578impl Neg for &Mat2 {
579    type Output = Mat2;
580    #[inline]
581    fn neg(self) -> Mat2 {
582        (*self).neg()
583    }
584}
585
586impl Mul for Mat2 {
587    type Output = Self;
588    #[inline]
589    fn mul(self, rhs: Self) -> Self {
590        unsafe {
591            let abcd = self.0;
592            let rhs = rhs.0;
593            let xxyy0 = _mm_shuffle_ps(rhs, rhs, 0b01_01_00_00);
594            let xxyy1 = _mm_shuffle_ps(rhs, rhs, 0b11_11_10_10);
595            let axbxcydy0 = _mm_mul_ps(abcd, xxyy0);
596            let axbxcydy1 = _mm_mul_ps(abcd, xxyy1);
597            let cydyaxbx0 = _mm_shuffle_ps(axbxcydy0, axbxcydy0, 0b01_00_11_10);
598            let cydyaxbx1 = _mm_shuffle_ps(axbxcydy1, axbxcydy1, 0b01_00_11_10);
599            let result0 = _mm_add_ps(axbxcydy0, cydyaxbx0);
600            let result1 = _mm_add_ps(axbxcydy1, cydyaxbx1);
601            Self(_mm_shuffle_ps(result0, result1, 0b01_00_01_00))
602        }
603    }
604}
605
606impl Mul<&Self> for Mat2 {
607    type Output = Self;
608    #[inline]
609    fn mul(self, rhs: &Self) -> Self {
610        self.mul(*rhs)
611    }
612}
613
614impl Mul<&Mat2> for &Mat2 {
615    type Output = Mat2;
616    #[inline]
617    fn mul(self, rhs: &Mat2) -> Mat2 {
618        (*self).mul(*rhs)
619    }
620}
621
622impl Mul<Mat2> for &Mat2 {
623    type Output = Mat2;
624    #[inline]
625    fn mul(self, rhs: Mat2) -> Mat2 {
626        (*self).mul(rhs)
627    }
628}
629
630impl MulAssign for Mat2 {
631    #[inline]
632    fn mul_assign(&mut self, rhs: Self) {
633        *self = self.mul(rhs);
634    }
635}
636
637impl MulAssign<&Self> for Mat2 {
638    #[inline]
639    fn mul_assign(&mut self, rhs: &Self) {
640        self.mul_assign(*rhs);
641    }
642}
643
644impl Mul<Vec2> for Mat2 {
645    type Output = Vec2;
646    #[inline]
647    fn mul(self, rhs: Vec2) -> Self::Output {
648        self.mul_vec2(rhs)
649    }
650}
651
652impl Mul<&Vec2> for Mat2 {
653    type Output = Vec2;
654    #[inline]
655    fn mul(self, rhs: &Vec2) -> Vec2 {
656        self.mul(*rhs)
657    }
658}
659
660impl Mul<&Vec2> for &Mat2 {
661    type Output = Vec2;
662    #[inline]
663    fn mul(self, rhs: &Vec2) -> Vec2 {
664        (*self).mul(*rhs)
665    }
666}
667
668impl Mul<Vec2> for &Mat2 {
669    type Output = Vec2;
670    #[inline]
671    fn mul(self, rhs: Vec2) -> Vec2 {
672        (*self).mul(rhs)
673    }
674}
675
676impl Mul<Mat2> for f32 {
677    type Output = Mat2;
678    #[inline]
679    fn mul(self, rhs: Mat2) -> Self::Output {
680        rhs.mul_scalar(self)
681    }
682}
683
684impl Mul<&Mat2> for f32 {
685    type Output = Mat2;
686    #[inline]
687    fn mul(self, rhs: &Mat2) -> Mat2 {
688        self.mul(*rhs)
689    }
690}
691
692impl Mul<&Mat2> for &f32 {
693    type Output = Mat2;
694    #[inline]
695    fn mul(self, rhs: &Mat2) -> Mat2 {
696        (*self).mul(*rhs)
697    }
698}
699
700impl Mul<Mat2> for &f32 {
701    type Output = Mat2;
702    #[inline]
703    fn mul(self, rhs: Mat2) -> Mat2 {
704        (*self).mul(rhs)
705    }
706}
707
708impl Mul<f32> for Mat2 {
709    type Output = Self;
710    #[inline]
711    fn mul(self, rhs: f32) -> Self {
712        self.mul_scalar(rhs)
713    }
714}
715
716impl Mul<&f32> for Mat2 {
717    type Output = Self;
718    #[inline]
719    fn mul(self, rhs: &f32) -> Self {
720        self.mul(*rhs)
721    }
722}
723
724impl Mul<&f32> for &Mat2 {
725    type Output = Mat2;
726    #[inline]
727    fn mul(self, rhs: &f32) -> Mat2 {
728        (*self).mul(*rhs)
729    }
730}
731
732impl Mul<f32> for &Mat2 {
733    type Output = Mat2;
734    #[inline]
735    fn mul(self, rhs: f32) -> Mat2 {
736        (*self).mul(rhs)
737    }
738}
739
740impl MulAssign<f32> for Mat2 {
741    #[inline]
742    fn mul_assign(&mut self, rhs: f32) {
743        *self = self.mul(rhs);
744    }
745}
746
747impl MulAssign<&f32> for Mat2 {
748    #[inline]
749    fn mul_assign(&mut self, rhs: &f32) {
750        self.mul_assign(*rhs);
751    }
752}
753
754impl Div<Mat2> for f32 {
755    type Output = Mat2;
756    #[inline]
757    fn div(self, rhs: Mat2) -> Self::Output {
758        rhs.div_scalar(self)
759    }
760}
761
762impl Div<&Mat2> for f32 {
763    type Output = Mat2;
764    #[inline]
765    fn div(self, rhs: &Mat2) -> Mat2 {
766        self.div(*rhs)
767    }
768}
769
770impl Div<&Mat2> for &f32 {
771    type Output = Mat2;
772    #[inline]
773    fn div(self, rhs: &Mat2) -> Mat2 {
774        (*self).div(*rhs)
775    }
776}
777
778impl Div<Mat2> for &f32 {
779    type Output = Mat2;
780    #[inline]
781    fn div(self, rhs: Mat2) -> Mat2 {
782        (*self).div(rhs)
783    }
784}
785
786impl Div<f32> for Mat2 {
787    type Output = Self;
788    #[inline]
789    fn div(self, rhs: f32) -> Self {
790        self.div_scalar(rhs)
791    }
792}
793
794impl Div<&f32> for Mat2 {
795    type Output = Self;
796    #[inline]
797    fn div(self, rhs: &f32) -> Self {
798        self.div(*rhs)
799    }
800}
801
802impl Div<&f32> for &Mat2 {
803    type Output = Mat2;
804    #[inline]
805    fn div(self, rhs: &f32) -> Mat2 {
806        (*self).div(*rhs)
807    }
808}
809
810impl Div<f32> for &Mat2 {
811    type Output = Mat2;
812    #[inline]
813    fn div(self, rhs: f32) -> Mat2 {
814        (*self).div(rhs)
815    }
816}
817
818impl DivAssign<f32> for Mat2 {
819    #[inline]
820    fn div_assign(&mut self, rhs: f32) {
821        *self = self.div(rhs);
822    }
823}
824
825impl DivAssign<&f32> for Mat2 {
826    #[inline]
827    fn div_assign(&mut self, rhs: &f32) {
828        self.div_assign(*rhs);
829    }
830}
831
832impl Sum<Self> for Mat2 {
833    fn sum<I>(iter: I) -> Self
834    where
835        I: Iterator<Item = Self>,
836    {
837        iter.fold(Self::ZERO, Self::add)
838    }
839}
840
841impl<'a> Sum<&'a Self> for Mat2 {
842    fn sum<I>(iter: I) -> Self
843    where
844        I: Iterator<Item = &'a Self>,
845    {
846        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
847    }
848}
849
850impl Product for Mat2 {
851    fn product<I>(iter: I) -> Self
852    where
853        I: Iterator<Item = Self>,
854    {
855        iter.fold(Self::IDENTITY, Self::mul)
856    }
857}
858
859impl<'a> Product<&'a Self> for Mat2 {
860    fn product<I>(iter: I) -> Self
861    where
862        I: Iterator<Item = &'a Self>,
863    {
864        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
865    }
866}
867
868impl PartialEq for Mat2 {
869    #[inline]
870    fn eq(&self, rhs: &Self) -> bool {
871        self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
872    }
873}
874
875impl AsRef<[f32; 4]> for Mat2 {
876    #[inline]
877    fn as_ref(&self) -> &[f32; 4] {
878        unsafe { &*(self as *const Self as *const [f32; 4]) }
879    }
880}
881
882impl AsMut<[f32; 4]> for Mat2 {
883    #[inline]
884    fn as_mut(&mut self) -> &mut [f32; 4] {
885        unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
886    }
887}
888
889impl core::ops::Deref for Mat2 {
890    type Target = crate::deref::Cols2<Vec2>;
891    #[inline]
892    fn deref(&self) -> &Self::Target {
893        unsafe { &*(self as *const Self as *const Self::Target) }
894    }
895}
896
897impl core::ops::DerefMut for Mat2 {
898    #[inline]
899    fn deref_mut(&mut self) -> &mut Self::Target {
900        unsafe { &mut *(self as *mut Self as *mut Self::Target) }
901    }
902}
903
904impl fmt::Debug for Mat2 {
905    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
906        fmt.debug_struct(stringify!(Mat2))
907            .field("x_axis", &self.x_axis)
908            .field("y_axis", &self.y_axis)
909            .finish()
910    }
911}
912
913impl fmt::Display for Mat2 {
914    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
915        if let Some(p) = f.precision() {
916            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
917        } else {
918            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
919        }
920    }
921}