Skip to main content

glam/f32/neon/
mat2.rs

1// Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3#[cfg(feature = "f64")]
4use crate::DMat2;
5
6use crate::{Mat3, Mat3A, Vec2, f32::math, swizzles::*};
7use core::fmt;
8use core::iter::{Product, Sum};
9use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
10
11use core::arch::aarch64::*;
12
13#[cfg(feature = "zerocopy-08")]
14use zerocopy_derive_08::*;
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-08",
38    derive(FromBytes, Immutable, IntoBytes, KnownLayout)
39)]
40#[repr(transparent)]
41pub struct Mat2(pub(crate) float32x4_t);
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    ///
67    /// See also [`Self::from_rows`] when the data is in row major order.
68    #[inline(always)]
69    #[must_use]
70    pub const fn from_cols(x_axis: Vec2, y_axis: Vec2) -> Self {
71        unsafe {
72            UnionCast {
73                a: [x_axis.x, x_axis.y, y_axis.x, y_axis.y],
74            }
75            .v
76        }
77    }
78
79    /// Creates a 2x2 matrix from two row vectors.
80    ///
81    /// Matrices are stored in column major order, so the given rows are permuted into
82    /// the matrix layout. Use [`Self::from_cols`] instead when the data is already in
83    /// column major order.
84    #[inline(always)]
85    #[must_use]
86    pub const fn from_rows(row0: Vec2, row1: Vec2) -> Self {
87        let [m00, m01] = row0.to_array();
88        let [m10, m11] = row1.to_array();
89        Self::new(m00, m10, m01, m11)
90    }
91
92    /// Creates a 2x2 matrix from a `[f32; 4]` array stored in column major order.
93    ///
94    /// If the data is in row major order use [`Self::from_rows_array`] instead.
95    #[inline]
96    #[must_use]
97    pub const fn from_cols_array(m: &[f32; 4]) -> Self {
98        Self::new(m[0], m[1], m[2], m[3])
99    }
100
101    /// Creates a `[f32; 4]` array storing data in column major order.
102    ///
103    /// If you require the data in row major order use [`Self::to_rows_array`] instead.
104    #[inline]
105    #[must_use]
106    pub const fn to_cols_array(&self) -> [f32; 4] {
107        unsafe { *(self as *const Self as *const [f32; 4]) }
108    }
109
110    /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
111    ///
112    /// If the data is in row major order `transpose` the returned matrix.
113    #[inline]
114    #[must_use]
115    pub const fn from_cols_array_2d(m: &[[f32; 2]; 2]) -> Self {
116        Self::from_cols(Vec2::from_array(m[0]), Vec2::from_array(m[1]))
117    }
118
119    /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order.
120    ///
121    /// If you require row major order `transpose` the matrix first.
122    #[inline]
123    #[must_use]
124    pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] {
125        unsafe { *(self as *const Self as *const [[f32; 2]; 2]) }
126    }
127
128    /// Creates a 2x2 matrix from a `[f32; 4]` array stored in row major order.
129    ///
130    /// Matrices are stored in column major order, so the array is permuted into the
131    /// matrix layout. Use [`Self::from_cols_array`] instead when the data is already in
132    /// column major order.
133    #[inline]
134    #[must_use]
135    pub const fn from_rows_array(m: &[f32; 4]) -> Self {
136        Self::new(m[0], m[2], m[1], m[3])
137    }
138
139    /// Creates a `[f32; 4]` array storing data in row major order.
140    ///
141    /// Matrices are stored in column major order, so the array is permuted out of the
142    /// column major storage. Use [`Self::to_cols_array`] instead when you want data in
143    /// column major order.
144    #[inline]
145    #[must_use]
146    pub const fn to_rows_array(&self) -> [f32; 4] {
147        let m = self.to_cols_array();
148        [m[0], m[2], m[1], m[3]]
149    }
150
151    /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.
152    #[doc(alias = "scale")]
153    #[inline]
154    #[must_use]
155    pub const fn from_diagonal(diagonal: Vec2) -> Self {
156        Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
157    }
158
159    /// Creates a 2x2 matrix containing the combining non-uniform `scale` and rotation of
160    /// `angle` (in radians).
161    #[inline]
162    #[must_use]
163    pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
164        let (sin, cos) = math::sin_cos(angle);
165        Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
166    }
167
168    /// Creates a 2x2 matrix containing a rotation of `angle` (in radians).
169    #[inline]
170    #[must_use]
171    pub fn from_angle(angle: f32) -> Self {
172        let (sin, cos) = math::sin_cos(angle);
173        Self::new(cos, sin, -sin, cos)
174    }
175
176    /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
177    #[inline]
178    #[must_use]
179    pub fn from_mat3(m: Mat3) -> Self {
180        Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
181    }
182
183    /// Creates a 2x2 matrix from the minor of the given 3x3 matrix, discarding the `i`th column
184    /// and `j`th row.
185    ///
186    /// # Panics
187    ///
188    /// Panics if `i` or `j` is greater than 2.
189    #[inline]
190    #[must_use]
191    #[track_caller]
192    pub fn from_mat3_minor(m: Mat3, i: usize, j: usize) -> Self {
193        match (i, j) {
194            (0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
195            (0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
196            (0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
197            (1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
198            (1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
199            (1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
200            (2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
201            (2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
202            (2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
203            _ => panic!("index out of bounds"),
204        }
205    }
206
207    /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
208    #[inline]
209    #[must_use]
210    pub fn from_mat3a(m: Mat3A) -> Self {
211        Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
212    }
213
214    /// Creates a 2x2 matrix from the minor of the given 3x3 matrix, discarding the `i`th column
215    /// and `j`th row.
216    ///
217    /// # Panics
218    ///
219    /// Panics if `i` or `j` is greater than 2.
220    #[inline]
221    #[must_use]
222    #[track_caller]
223    pub fn from_mat3a_minor(m: Mat3A, i: usize, j: usize) -> Self {
224        match (i, j) {
225            (0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
226            (0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
227            (0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
228            (1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
229            (1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
230            (1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
231            (2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
232            (2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
233            (2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
234            _ => panic!("index out of bounds"),
235        }
236    }
237
238    /// Creates a 2x2 matrix from the first 4 values in `slice`.
239    ///
240    /// See also [`Self::from_rows_slice`] when the slice is in row major order.
241    ///
242    /// # Panics
243    ///
244    /// Panics if `slice` is less than 4 elements long.
245    #[inline]
246    #[must_use]
247    #[track_caller]
248    pub const fn from_cols_slice(slice: &[f32]) -> Self {
249        Self::new(slice[0], slice[1], slice[2], slice[3])
250    }
251
252    /// Writes the columns of `self` to the first 4 elements in `slice`.
253    ///
254    /// # Panics
255    ///
256    /// Panics if `slice` is less than 4 elements long.
257    #[inline]
258    #[track_caller]
259    pub fn write_cols_to_slice(&self, slice: &mut [f32]) {
260        slice[0] = self.x_axis.x;
261        slice[1] = self.x_axis.y;
262        slice[2] = self.y_axis.x;
263        slice[3] = self.y_axis.y;
264    }
265
266    /// Creates a 2x2 matrix from the first 4 values in `slice`, stored in row
267    /// major order.
268    ///
269    /// Matrices are stored in column major order, so the slice is permuted into the
270    /// matrix layout. Use [`Self::from_cols_slice`] instead when the slice is already in
271    /// column major order.
272    ///
273    /// # Panics
274    ///
275    /// Panics if `slice` is less than 4 elements long.
276    #[inline]
277    #[must_use]
278    #[track_caller]
279    pub const fn from_rows_slice(slice: &[f32]) -> Self {
280        Self::new(slice[0], slice[2], slice[1], slice[3])
281    }
282
283    /// Returns the matrix column for the given `index`.
284    ///
285    /// # Panics
286    ///
287    /// Panics if `index` is greater than 1.
288    #[inline]
289    #[must_use]
290    #[track_caller]
291    pub fn col(&self, index: usize) -> Vec2 {
292        match index {
293            0 => self.x_axis,
294            1 => self.y_axis,
295            _ => panic!("index out of bounds"),
296        }
297    }
298
299    /// Returns a mutable reference to the matrix column for the given `index`.
300    ///
301    /// # Panics
302    ///
303    /// Panics if `index` is greater than 1.
304    #[inline]
305    #[track_caller]
306    pub fn col_mut(&mut self, index: usize) -> &mut Vec2 {
307        match index {
308            0 => &mut self.x_axis,
309            1 => &mut self.y_axis,
310            _ => panic!("index out of bounds"),
311        }
312    }
313
314    /// Returns the matrix row for the given `index`.
315    ///
316    /// See also [`Self::set_row`] when you need to change the row.
317    ///
318    /// # Panics
319    ///
320    /// Panics if `index` is greater than 1.
321    #[inline]
322    #[must_use]
323    #[track_caller]
324    pub fn row(&self, index: usize) -> Vec2 {
325        match index {
326            0 => Vec2::new(self.x_axis.x, self.y_axis.x),
327            1 => Vec2::new(self.x_axis.y, self.y_axis.y),
328            _ => panic!("index out of bounds"),
329        }
330    }
331
332    /// Sets the matrix row for the given `index`.
333    ///
334    /// Matrices are stored in column major order, so the row is spread across all
335    /// 2 columns and writing it touches every column. Use [`Self::col_mut`]
336    /// instead when you can work with columns. See also [`Self::row`].
337    ///
338    /// # Panics
339    ///
340    /// Panics if `index` is greater than 1.
341    #[inline]
342    #[track_caller]
343    pub fn set_row(&mut self, index: usize, row: Vec2) {
344        match index {
345            0 => {
346                self.x_axis.x = row.x;
347                self.y_axis.x = row.y;
348            }
349            1 => {
350                self.x_axis.y = row.x;
351                self.y_axis.y = row.y;
352            }
353            _ => panic!("index out of bounds"),
354        }
355    }
356
357    /// Returns `true` if, and only if, all elements are finite.
358    /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
359    #[inline]
360    #[must_use]
361    pub fn is_finite(&self) -> bool {
362        self.x_axis.is_finite() && self.y_axis.is_finite()
363    }
364
365    /// Returns `true` if any elements are `NaN`.
366    #[inline]
367    #[must_use]
368    pub fn is_nan(&self) -> bool {
369        self.x_axis.is_nan() || self.y_axis.is_nan()
370    }
371
372    /// Returns the transpose of `self`.
373    #[inline]
374    #[must_use]
375    pub fn transpose(&self) -> Self {
376        Self(unsafe {
377            vsetq_lane_f32(
378                vgetq_lane_f32(self.0, 2),
379                vsetq_lane_f32(vgetq_lane_f32(self.0, 1), self.0, 2),
380                1,
381            )
382        })
383    }
384
385    /// Returns the diagonal of `self`.
386    #[inline]
387    #[must_use]
388    pub fn diagonal(&self) -> Vec2 {
389        Vec2::new(self.x_axis.x, self.y_axis.y)
390    }
391
392    /// Returns the determinant of `self`.
393    #[inline]
394    #[must_use]
395    pub fn determinant(&self) -> f32 {
396        self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x
397    }
398
399    /// If `CHECKED` is true then if the determinant is zero this function will return a tuple
400    /// containing a zero matrix and false. If the determinant is non zero a tuple containing the
401    /// inverted matrix and true is returned.
402    ///
403    /// If `CHECKED` is false then the determinant is not checked and if it is zero the resulting
404    /// inverted matrix will be invalid. Will panic if the resulting inverted matrix is not finite
405    /// when `glam_assert` is enabled.
406    ///
407    /// A tuple containing the inverted matrix and a bool is used instead of an option here as
408    /// regular Rust enums put the discriminant first which can result in a lot of padding if the
409    /// matrix is aligned.
410    #[inline(always)]
411    #[must_use]
412    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
413    fn inverse_checked<const CHECKED: bool>(&self) -> (Self, bool) {
414        unsafe {
415            const SIGN: float32x4_t = crate::neon::f32x4_from_array([1.0, -1.0, -1.0, 1.0]);
416            let abcd = self.0;
417            let ab = vget_low_f32(abcd);
418            let cd = vget_high_f32(abcd);
419            let ad = vmul_lane_f32(ab, cd, 1);
420            let bc = vmul_lane_f32(cd, ab, 1);
421            let det = vget_lane_f32(vsub_f32(ad, bc), 0);
422            let tmp = vmulq_n_f32(SIGN, 1.0f32 / det);
423            let dbca = vsetq_lane_f32(
424                vgetq_lane_f32(abcd, 0),
425                vsetq_lane_f32(vgetq_lane_f32(abcd, 3), abcd, 0),
426                3,
427            );
428            let m = Self(vmulq_f32(dbca, tmp));
429            if CHECKED {
430                if !m.is_finite() {
431                    return (Self::ZERO, false);
432                }
433            } else {
434                glam_assert!(m.is_finite());
435            }
436            (m, true)
437        }
438    }
439
440    /// Returns the inverse of `self`.
441    ///
442    /// If the matrix is not invertible the returned matrix will be invalid. The
443    /// returned matrix will also be invalid if the inverse is not finite, which can
444    /// happen when `self` contains very large or very small values. Use
445    /// [`Self::try_inverse`] or [`Self::inverse_or_zero`] to detect these cases.
446    ///
447    /// # Panics
448    ///
449    /// Will panic if the resulting inverted matrix is not finite when `glam_assert`
450    /// is enabled.
451    #[inline]
452    #[must_use]
453    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
454    pub fn inverse(&self) -> Self {
455        self.inverse_checked::<false>().0
456    }
457
458    /// Returns the inverse of `self` or `None` if the matrix is not invertible, or if
459    /// the inverse is not finite.
460    #[inline]
461    #[must_use]
462    pub fn try_inverse(&self) -> Option<Self> {
463        let (m, is_valid) = self.inverse_checked::<true>();
464        if is_valid { Some(m) } else { None }
465    }
466
467    /// Returns the inverse of `self` or `Mat2::ZERO` if the matrix is not
468    /// invertible, or if the inverse is not finite.
469    #[inline]
470    #[must_use]
471    pub fn inverse_or_zero(&self) -> Self {
472        self.inverse_checked::<true>().0
473    }
474
475    /// Transforms a 2D vector.
476    #[inline]
477    #[must_use]
478    pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
479        unsafe {
480            let abcd = self.0;
481            let xxyy = vld1q_f32([rhs.x, rhs.x, rhs.y, rhs.y].as_ptr());
482            let axbxcydy = vmulq_f32(abcd, xxyy);
483            // let cydyaxbx = simd_swizzle!(axbxcydy, [2, 3, 0, 1]);
484            let cydyaxbx = vextq_f32(axbxcydy, axbxcydy, 2);
485            let result = vaddq_f32(axbxcydy, cydyaxbx);
486            *(&result as *const float32x4_t as *const Vec2)
487        }
488    }
489
490    /// Transforms a 2D vector by the transpose of `self`.
491    #[inline]
492    #[must_use]
493    pub fn transpose_mul_vec2(&self, rhs: Vec2) -> Vec2 {
494        Vec2::new(self.x_axis.dot(rhs), self.y_axis.dot(rhs))
495    }
496
497    /// Transforms a 2D vector by the transpose of `self`.
498    #[inline]
499    #[must_use]
500    #[deprecated(
501        since = "0.34.2",
502        note = "Use `transpose_mul_vec2` instead, whose word order matches its order of operations and is consistent with `transpose().mul_vec2`."
503    )]
504    pub fn mul_transpose_vec2(&self, rhs: Vec2) -> Vec2 {
505        self.transpose_mul_vec2(rhs)
506    }
507
508    /// Multiplies two 2x2 matrices.
509    #[inline]
510    #[must_use]
511    pub fn mul_mat2(&self, rhs: &Self) -> Self {
512        self.mul(rhs)
513    }
514
515    /// Adds two 2x2 matrices.
516    #[inline]
517    #[must_use]
518    pub fn add_mat2(&self, rhs: &Self) -> Self {
519        self.add(rhs)
520    }
521
522    /// Subtracts two 2x2 matrices.
523    #[inline]
524    #[must_use]
525    pub fn sub_mat2(&self, rhs: &Self) -> Self {
526        self.sub(rhs)
527    }
528
529    /// Multiplies a 2x2 matrix by a scalar.
530    #[inline]
531    #[must_use]
532    pub fn mul_scalar(&self, rhs: f32) -> Self {
533        Self(unsafe { vmulq_f32(self.0, vld1q_dup_f32(&rhs)) })
534    }
535
536    /// Multiply `self` by a scaling vector `scale`.
537    /// This is faster than creating a whole diagonal scaling matrix and then multiplying that.
538    /// This operation is commutative.
539    #[inline]
540    #[must_use]
541    pub fn mul_diagonal_scale(&self, scale: Vec2) -> Self {
542        Self::from_cols(self.x_axis * scale.x, self.y_axis * scale.y)
543    }
544
545    /// Divides a 2x2 matrix by a scalar.
546    #[inline]
547    #[must_use]
548    pub fn div_scalar(&self, rhs: f32) -> Self {
549        Self(unsafe { vdivq_f32(self.0, vld1q_dup_f32(&rhs)) })
550    }
551
552    /// Returns a matrix containing the reciprocal `1.0/n` of each element of `self`.
553    #[inline]
554    #[must_use]
555    pub fn recip(&self) -> Self {
556        Self::from_cols(self.x_axis.recip(), self.y_axis.recip())
557    }
558
559    /// Returns true if the absolute difference of all elements between `self` and `rhs`
560    /// is less than or equal to `max_abs_diff`.
561    ///
562    /// This can be used to compare if two matrices contain similar elements. It works best
563    /// when comparing with a known value. The `max_abs_diff` that should be used used
564    /// depends on the values being compared against.
565    ///
566    /// For more see
567    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
568    #[inline]
569    #[must_use]
570    pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
571        self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
572            && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
573    }
574
575    /// Takes the absolute value of each element in `self`
576    #[inline]
577    #[must_use]
578    pub fn abs(&self) -> Self {
579        Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
580    }
581
582    #[cfg(feature = "f64")]
583    #[inline]
584    #[must_use]
585    pub fn as_dmat2(&self) -> DMat2 {
586        DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
587    }
588}
589
590impl Default for Mat2 {
591    #[inline]
592    fn default() -> Self {
593        Self::IDENTITY
594    }
595}
596
597impl Add for Mat2 {
598    type Output = Self;
599    #[inline]
600    fn add(self, rhs: Self) -> Self {
601        Self(unsafe { vaddq_f32(self.0, rhs.0) })
602    }
603}
604
605impl Add<&Self> for Mat2 {
606    type Output = Self;
607    #[inline]
608    fn add(self, rhs: &Self) -> Self {
609        self.add(*rhs)
610    }
611}
612
613impl Add<&Mat2> for &Mat2 {
614    type Output = Mat2;
615    #[inline]
616    fn add(self, rhs: &Mat2) -> Mat2 {
617        (*self).add(*rhs)
618    }
619}
620
621impl Add<Mat2> for &Mat2 {
622    type Output = Mat2;
623    #[inline]
624    fn add(self, rhs: Mat2) -> Mat2 {
625        (*self).add(rhs)
626    }
627}
628
629impl AddAssign for Mat2 {
630    #[inline]
631    fn add_assign(&mut self, rhs: Self) {
632        *self = self.add(rhs);
633    }
634}
635
636impl AddAssign<&Self> for Mat2 {
637    #[inline]
638    fn add_assign(&mut self, rhs: &Self) {
639        self.add_assign(*rhs);
640    }
641}
642
643impl Sub for Mat2 {
644    type Output = Self;
645    #[inline]
646    fn sub(self, rhs: Self) -> Self {
647        Self(unsafe { vsubq_f32(self.0, rhs.0) })
648    }
649}
650
651impl Sub<&Self> for Mat2 {
652    type Output = Self;
653    #[inline]
654    fn sub(self, rhs: &Self) -> Self {
655        self.sub(*rhs)
656    }
657}
658
659impl Sub<&Mat2> for &Mat2 {
660    type Output = Mat2;
661    #[inline]
662    fn sub(self, rhs: &Mat2) -> Mat2 {
663        (*self).sub(*rhs)
664    }
665}
666
667impl Sub<Mat2> for &Mat2 {
668    type Output = Mat2;
669    #[inline]
670    fn sub(self, rhs: Mat2) -> Mat2 {
671        (*self).sub(rhs)
672    }
673}
674
675impl SubAssign for Mat2 {
676    #[inline]
677    fn sub_assign(&mut self, rhs: Self) {
678        *self = self.sub(rhs);
679    }
680}
681
682impl SubAssign<&Self> for Mat2 {
683    #[inline]
684    fn sub_assign(&mut self, rhs: &Self) {
685        self.sub_assign(*rhs);
686    }
687}
688
689impl Neg for Mat2 {
690    type Output = Self;
691    #[inline]
692    fn neg(self) -> Self::Output {
693        Self(unsafe { vnegq_f32(self.0) })
694    }
695}
696
697impl Neg for &Mat2 {
698    type Output = Mat2;
699    #[inline]
700    fn neg(self) -> Mat2 {
701        (*self).neg()
702    }
703}
704
705impl Mul for Mat2 {
706    type Output = Self;
707    #[inline]
708    fn mul(self, rhs: Self) -> Self {
709        unsafe {
710            let abcd = self.0;
711            let xxyy0 = vzip1q_f32(rhs.0, rhs.0);
712            let xxyy1 = vzip2q_f32(rhs.0, rhs.0);
713            let axbxcydy0 = vmulq_f32(abcd, xxyy0);
714            let axbxcydy1 = vmulq_f32(abcd, xxyy1);
715            let cydyaxbx0 = vextq_f32(axbxcydy0, axbxcydy0, 2);
716            let cydyaxbx1 = vextq_f32(axbxcydy1, axbxcydy1, 2);
717            let result0 = vaddq_f32(axbxcydy0, cydyaxbx0);
718            let result1 = vaddq_f32(axbxcydy1, cydyaxbx1);
719            Self(vreinterpretq_f32_u64(vsetq_lane_u64(
720                vgetq_lane_u64(vreinterpretq_u64_f32(result1), 0),
721                vreinterpretq_u64_f32(result0),
722                1,
723            )))
724        }
725    }
726}
727
728impl Mul<&Self> for Mat2 {
729    type Output = Self;
730    #[inline]
731    fn mul(self, rhs: &Self) -> Self {
732        self.mul(*rhs)
733    }
734}
735
736impl Mul<&Mat2> for &Mat2 {
737    type Output = Mat2;
738    #[inline]
739    fn mul(self, rhs: &Mat2) -> Mat2 {
740        (*self).mul(*rhs)
741    }
742}
743
744impl Mul<Mat2> for &Mat2 {
745    type Output = Mat2;
746    #[inline]
747    fn mul(self, rhs: Mat2) -> Mat2 {
748        (*self).mul(rhs)
749    }
750}
751
752impl MulAssign for Mat2 {
753    #[inline]
754    fn mul_assign(&mut self, rhs: Self) {
755        *self = self.mul(rhs);
756    }
757}
758
759impl MulAssign<&Self> for Mat2 {
760    #[inline]
761    fn mul_assign(&mut self, rhs: &Self) {
762        self.mul_assign(*rhs);
763    }
764}
765
766impl Mul<Vec2> for Mat2 {
767    type Output = Vec2;
768    #[inline]
769    fn mul(self, rhs: Vec2) -> Self::Output {
770        self.mul_vec2(rhs)
771    }
772}
773
774impl Mul<&Vec2> for Mat2 {
775    type Output = Vec2;
776    #[inline]
777    fn mul(self, rhs: &Vec2) -> Vec2 {
778        self.mul(*rhs)
779    }
780}
781
782impl Mul<&Vec2> for &Mat2 {
783    type Output = Vec2;
784    #[inline]
785    fn mul(self, rhs: &Vec2) -> Vec2 {
786        (*self).mul(*rhs)
787    }
788}
789
790impl Mul<Vec2> for &Mat2 {
791    type Output = Vec2;
792    #[inline]
793    fn mul(self, rhs: Vec2) -> Vec2 {
794        (*self).mul(rhs)
795    }
796}
797
798impl Mul<Mat2> for f32 {
799    type Output = Mat2;
800    #[inline]
801    fn mul(self, rhs: Mat2) -> Self::Output {
802        rhs.mul_scalar(self)
803    }
804}
805
806impl Mul<&Mat2> for f32 {
807    type Output = Mat2;
808    #[inline]
809    fn mul(self, rhs: &Mat2) -> Mat2 {
810        self.mul(*rhs)
811    }
812}
813
814impl Mul<&Mat2> for &f32 {
815    type Output = Mat2;
816    #[inline]
817    fn mul(self, rhs: &Mat2) -> Mat2 {
818        (*self).mul(*rhs)
819    }
820}
821
822impl Mul<Mat2> for &f32 {
823    type Output = Mat2;
824    #[inline]
825    fn mul(self, rhs: Mat2) -> Mat2 {
826        (*self).mul(rhs)
827    }
828}
829
830impl Mul<f32> for Mat2 {
831    type Output = Self;
832    #[inline]
833    fn mul(self, rhs: f32) -> Self {
834        self.mul_scalar(rhs)
835    }
836}
837
838impl Mul<&f32> for Mat2 {
839    type Output = Self;
840    #[inline]
841    fn mul(self, rhs: &f32) -> Self {
842        self.mul(*rhs)
843    }
844}
845
846impl Mul<&f32> for &Mat2 {
847    type Output = Mat2;
848    #[inline]
849    fn mul(self, rhs: &f32) -> Mat2 {
850        (*self).mul(*rhs)
851    }
852}
853
854impl Mul<f32> for &Mat2 {
855    type Output = Mat2;
856    #[inline]
857    fn mul(self, rhs: f32) -> Mat2 {
858        (*self).mul(rhs)
859    }
860}
861
862impl MulAssign<f32> for Mat2 {
863    #[inline]
864    fn mul_assign(&mut self, rhs: f32) {
865        *self = self.mul(rhs);
866    }
867}
868
869impl MulAssign<&f32> for Mat2 {
870    #[inline]
871    fn mul_assign(&mut self, rhs: &f32) {
872        self.mul_assign(*rhs);
873    }
874}
875
876impl Div<Mat2> for f32 {
877    type Output = Mat2;
878    #[inline]
879    fn div(self, rhs: Mat2) -> Self::Output {
880        Mat2(unsafe { vdivq_f32(vld1q_dup_f32(&self), rhs.0) })
881    }
882}
883
884impl Div<&Mat2> for f32 {
885    type Output = Mat2;
886    #[inline]
887    fn div(self, rhs: &Mat2) -> Mat2 {
888        self.div(*rhs)
889    }
890}
891
892impl Div<&Mat2> for &f32 {
893    type Output = Mat2;
894    #[inline]
895    fn div(self, rhs: &Mat2) -> Mat2 {
896        (*self).div(*rhs)
897    }
898}
899
900impl Div<Mat2> for &f32 {
901    type Output = Mat2;
902    #[inline]
903    fn div(self, rhs: Mat2) -> Mat2 {
904        (*self).div(rhs)
905    }
906}
907
908impl Div<f32> for Mat2 {
909    type Output = Self;
910    #[inline]
911    fn div(self, rhs: f32) -> Self {
912        self.div_scalar(rhs)
913    }
914}
915
916impl Div<&f32> for Mat2 {
917    type Output = Self;
918    #[inline]
919    fn div(self, rhs: &f32) -> Self {
920        self.div(*rhs)
921    }
922}
923
924impl Div<&f32> for &Mat2 {
925    type Output = Mat2;
926    #[inline]
927    fn div(self, rhs: &f32) -> Mat2 {
928        (*self).div(*rhs)
929    }
930}
931
932impl Div<f32> for &Mat2 {
933    type Output = Mat2;
934    #[inline]
935    fn div(self, rhs: f32) -> Mat2 {
936        (*self).div(rhs)
937    }
938}
939
940impl DivAssign<f32> for Mat2 {
941    #[inline]
942    fn div_assign(&mut self, rhs: f32) {
943        *self = self.div(rhs);
944    }
945}
946
947impl DivAssign<&f32> for Mat2 {
948    #[inline]
949    fn div_assign(&mut self, rhs: &f32) {
950        self.div_assign(*rhs);
951    }
952}
953
954impl Sum<Self> for Mat2 {
955    fn sum<I>(iter: I) -> Self
956    where
957        I: Iterator<Item = Self>,
958    {
959        iter.fold(Self::ZERO, Self::add)
960    }
961}
962
963impl<'a> Sum<&'a Self> for Mat2 {
964    fn sum<I>(iter: I) -> Self
965    where
966        I: Iterator<Item = &'a Self>,
967    {
968        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
969    }
970}
971
972impl Product for Mat2 {
973    fn product<I>(iter: I) -> Self
974    where
975        I: Iterator<Item = Self>,
976    {
977        iter.fold(Self::IDENTITY, Self::mul)
978    }
979}
980
981impl<'a> Product<&'a Self> for Mat2 {
982    fn product<I>(iter: I) -> Self
983    where
984        I: Iterator<Item = &'a Self>,
985    {
986        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
987    }
988}
989
990impl PartialEq for Mat2 {
991    #[inline]
992    fn eq(&self, rhs: &Self) -> bool {
993        self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
994    }
995}
996
997impl AsRef<[f32; 4]> for Mat2 {
998    #[inline]
999    fn as_ref(&self) -> &[f32; 4] {
1000        unsafe { &*(self as *const Self as *const [f32; 4]) }
1001    }
1002}
1003
1004impl AsMut<[f32; 4]> for Mat2 {
1005    #[inline]
1006    fn as_mut(&mut self) -> &mut [f32; 4] {
1007        unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
1008    }
1009}
1010
1011impl core::ops::Deref for Mat2 {
1012    type Target = crate::deref::Cols2<Vec2>;
1013    #[inline]
1014    fn deref(&self) -> &Self::Target {
1015        unsafe { &*(self as *const Self as *const Self::Target) }
1016    }
1017}
1018
1019impl core::ops::DerefMut for Mat2 {
1020    #[inline]
1021    fn deref_mut(&mut self) -> &mut Self::Target {
1022        unsafe { &mut *(self as *mut Self as *mut Self::Target) }
1023    }
1024}
1025
1026impl fmt::Debug for Mat2 {
1027    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1028        fmt.debug_struct(stringify!(Mat2))
1029            .field("x_axis", &self.x_axis)
1030            .field("y_axis", &self.y_axis)
1031            .finish()
1032    }
1033}
1034
1035impl fmt::Display for Mat2 {
1036    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1037        if let Some(p) = f.precision() {
1038            write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
1039        } else {
1040            write!(f, "[{}, {}]", self.x_axis, self.y_axis)
1041        }
1042    }
1043}