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};
4#[cfg(not(target_arch = "spirv"))]
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
8
9#[cfg(target_arch = "x86")]
10use core::arch::x86::*;
11#[cfg(target_arch = "x86_64")]
12use core::arch::x86_64::*;
13
14#[repr(C)]
15union UnionCast {
16    a: [f32; 4],
17    v: Mat2,
18}
19
20/// Creates a 2x2 matrix from two column vectors.
21#[inline(always)]
22#[must_use]
23pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
24    Mat2::from_cols(x_axis, y_axis)
25}
26
27/// A 2x2 column major matrix.
28///
29/// SIMD vector types are used for storage on supported platforms.
30///
31/// This type is 16 byte aligned.
32#[derive(Clone, Copy)]
33#[repr(transparent)]
34pub struct Mat2(pub(crate) __m128);
35
36impl Mat2 {
37    /// A 2x2 matrix with all elements set to `0.0`.
38    pub const ZERO: Self = Self::from_cols(Vec2::ZERO, Vec2::ZERO);
39
40    /// A 2x2 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
41    pub const IDENTITY: Self = Self::from_cols(Vec2::X, Vec2::Y);
42
43    /// All NAN:s.
44    pub const NAN: Self = Self::from_cols(Vec2::NAN, Vec2::NAN);
45
46    #[allow(clippy::too_many_arguments)]
47    #[inline(always)]
48    #[must_use]
49    const fn new(m00: f32, m01: f32, m10: f32, m11: f32) -> Self {
50        unsafe {
51            UnionCast {
52                a: [m00, m01, m10, m11],
53            }
54            .v
55        }
56    }
57
58    /// Creates a 2x2 matrix from two column vectors.
59    #[inline(always)]
60    #[must_use]
61    pub const fn from_cols(x_axis: Vec2, y_axis: Vec2) -> Self {
62        unsafe {
63            UnionCast {
64                a: [x_axis.x, x_axis.y, y_axis.x, y_axis.y],
65            }
66            .v
67        }
68    }
69
70    /// Creates a 2x2 matrix from a `[f32; 4]` array stored in column major order.
71    /// If your data is stored in row major you will need to `transpose` the returned
72    /// matrix.
73    #[inline]
74    #[must_use]
75    pub const fn from_cols_array(m: &[f32; 4]) -> Self {
76        Self::new(m[0], m[1], m[2], m[3])
77    }
78
79    /// Creates a `[f32; 4]` array storing data in column major order.
80    /// If you require data in row major order `transpose` the matrix first.
81    #[inline]
82    #[must_use]
83    pub const fn to_cols_array(&self) -> [f32; 4] {
84        unsafe { *(self as *const Self as *const [f32; 4]) }
85    }
86
87    /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
88    /// If your data is in row major order you will need to `transpose` the returned
89    /// matrix.
90    #[inline]
91    #[must_use]
92    pub const fn from_cols_array_2d(m: &[[f32; 2]; 2]) -> Self {
93        Self::from_cols(Vec2::from_array(m[0]), Vec2::from_array(m[1]))
94    }
95
96    /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order.
97    /// If you require data in row major order `transpose` the matrix first.
98    #[inline]
99    #[must_use]
100    pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] {
101        unsafe { *(self as *const Self as *const [[f32; 2]; 2]) }
102    }
103
104    /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.
105    #[doc(alias = "scale")]
106    #[inline]
107    #[must_use]
108    pub const fn from_diagonal(diagonal: Vec2) -> Self {
109        Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
110    }
111
112    /// Creates a 2x2 matrix containing the combining non-uniform `scale` and rotation of
113    /// `angle` (in radians).
114    #[inline]
115    #[must_use]
116    pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
117        let (sin, cos) = math::sin_cos(angle);
118        Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
119    }
120
121    /// Creates a 2x2 matrix containing a rotation of `angle` (in radians).
122    #[inline]
123    #[must_use]
124    pub fn from_angle(angle: f32) -> Self {
125        let (sin, cos) = math::sin_cos(angle);
126        Self::new(cos, sin, -sin, cos)
127    }
128
129    /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
130    #[inline]
131    #[must_use]
132    pub fn from_mat3(m: Mat3) -> Self {
133        Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
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_mat3a(m: Mat3A) -> Self {
140        Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
141    }
142
143    /// Creates a 2x2 matrix from the first 4 values in `slice`.
144    ///
145    /// # Panics
146    ///
147    /// Panics if `slice` is less than 4 elements long.
148    #[inline]
149    #[must_use]
150    pub const fn from_cols_slice(slice: &[f32]) -> Self {
151        Self::new(slice[0], slice[1], slice[2], slice[3])
152    }
153
154    /// Writes the columns of `self` to the first 4 elements in `slice`.
155    ///
156    /// # Panics
157    ///
158    /// Panics if `slice` is less than 4 elements long.
159    #[inline]
160    pub fn write_cols_to_slice(self, slice: &mut [f32]) {
161        slice[0] = self.x_axis.x;
162        slice[1] = self.x_axis.y;
163        slice[2] = self.y_axis.x;
164        slice[3] = self.y_axis.y;
165    }
166
167    /// Returns the matrix column for the given `index`.
168    ///
169    /// # Panics
170    ///
171    /// Panics if `index` is greater than 1.
172    #[inline]
173    #[must_use]
174    pub fn col(&self, index: usize) -> Vec2 {
175        match index {
176            0 => self.x_axis,
177            1 => self.y_axis,
178            _ => panic!("index out of bounds"),
179        }
180    }
181
182    /// Returns a mutable reference to the matrix column for the given `index`.
183    ///
184    /// # Panics
185    ///
186    /// Panics if `index` is greater than 1.
187    #[inline]
188    pub fn col_mut(&mut self, index: usize) -> &mut Vec2 {
189        match index {
190            0 => &mut self.x_axis,
191            1 => &mut self.y_axis,
192            _ => panic!("index out of bounds"),
193        }
194    }
195
196    /// Returns the matrix row for the given `index`.
197    ///
198    /// # Panics
199    ///
200    /// Panics if `index` is greater than 1.
201    #[inline]
202    #[must_use]
203    pub fn row(&self, index: usize) -> Vec2 {
204        match index {
205            0 => Vec2::new(self.x_axis.x, self.y_axis.x),
206            1 => Vec2::new(self.x_axis.y, self.y_axis.y),
207            _ => panic!("index out of bounds"),
208        }
209    }
210
211    /// Returns `true` if, and only if, all elements are finite.
212    /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
213    #[inline]
214    #[must_use]
215    pub fn is_finite(&self) -> bool {
216        self.x_axis.is_finite() && self.y_axis.is_finite()
217    }
218
219    /// Returns `true` if any elements are `NaN`.
220    #[inline]
221    #[must_use]
222    pub fn is_nan(&self) -> bool {
223        self.x_axis.is_nan() || self.y_axis.is_nan()
224    }
225
226    /// Returns the transpose of `self`.
227    #[inline]
228    #[must_use]
229    pub fn transpose(&self) -> Self {
230        Self(unsafe { _mm_shuffle_ps(self.0, self.0, 0b11_01_10_00) })
231    }
232
233    /// Returns the determinant of `self`.
234    #[inline]
235    #[must_use]
236    pub fn determinant(&self) -> f32 {
237        unsafe {
238            let abcd = self.0;
239            let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11);
240            let prod = _mm_mul_ps(abcd, dcba);
241            let det = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01));
242            _mm_cvtss_f32(det)
243        }
244    }
245
246    /// Returns the inverse of `self`.
247    ///
248    /// If the matrix is not invertible the returned matrix will be invalid.
249    ///
250    /// # Panics
251    ///
252    /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
253    #[inline]
254    #[must_use]
255    pub fn inverse(&self) -> Self {
256        unsafe {
257            const SIGN: __m128 = crate::sse2::m128_from_f32x4([1.0, -1.0, -1.0, 1.0]);
258            let abcd = self.0;
259            let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11);
260            let prod = _mm_mul_ps(abcd, dcba);
261            let sub = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01));
262            let det = _mm_shuffle_ps(sub, sub, 0b00_00_00_00);
263            let tmp = _mm_div_ps(SIGN, det);
264            glam_assert!(Mat2(tmp).is_finite());
265            let dbca = _mm_shuffle_ps(abcd, abcd, 0b00_10_01_11);
266            Self(_mm_mul_ps(dbca, tmp))
267        }
268    }
269
270    /// Transforms a 2D vector.
271    #[inline]
272    #[must_use]
273    pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
274        unsafe {
275            use crate::Align16;
276            use core::mem::MaybeUninit;
277            let abcd = self.0;
278            let xxyy = _mm_set_ps(rhs.y, rhs.y, rhs.x, rhs.x);
279            let axbxcydy = _mm_mul_ps(abcd, xxyy);
280            let cydyaxbx = _mm_shuffle_ps(axbxcydy, axbxcydy, 0b01_00_11_10);
281            let result = _mm_add_ps(axbxcydy, cydyaxbx);
282            let mut out: MaybeUninit<Align16<Vec2>> = MaybeUninit::uninit();
283            _mm_store_ps(out.as_mut_ptr().cast(), result);
284            out.assume_init().0
285        }
286    }
287
288    /// Multiplies two 2x2 matrices.
289    #[inline]
290    #[must_use]
291    pub fn mul_mat2(&self, rhs: &Self) -> Self {
292        unsafe {
293            let abcd = self.0;
294            let rhs = rhs.0;
295            let xxyy0 = _mm_shuffle_ps(rhs, rhs, 0b01_01_00_00);
296            let xxyy1 = _mm_shuffle_ps(rhs, rhs, 0b11_11_10_10);
297            let axbxcydy0 = _mm_mul_ps(abcd, xxyy0);
298            let axbxcydy1 = _mm_mul_ps(abcd, xxyy1);
299            let cydyaxbx0 = _mm_shuffle_ps(axbxcydy0, axbxcydy0, 0b01_00_11_10);
300            let cydyaxbx1 = _mm_shuffle_ps(axbxcydy1, axbxcydy1, 0b01_00_11_10);
301            let result0 = _mm_add_ps(axbxcydy0, cydyaxbx0);
302            let result1 = _mm_add_ps(axbxcydy1, cydyaxbx1);
303            Self(_mm_shuffle_ps(result0, result1, 0b01_00_01_00))
304        }
305    }
306
307    /// Adds two 2x2 matrices.
308    #[inline]
309    #[must_use]
310    pub fn add_mat2(&self, rhs: &Self) -> Self {
311        Self(unsafe { _mm_add_ps(self.0, rhs.0) })
312    }
313
314    /// Subtracts two 2x2 matrices.
315    #[inline]
316    #[must_use]
317    pub fn sub_mat2(&self, rhs: &Self) -> Self {
318        Self(unsafe { _mm_sub_ps(self.0, rhs.0) })
319    }
320
321    /// Multiplies a 2x2 matrix by a scalar.
322    #[inline]
323    #[must_use]
324    pub fn mul_scalar(&self, rhs: f32) -> Self {
325        Self(unsafe { _mm_mul_ps(self.0, _mm_set_ps1(rhs)) })
326    }
327
328    /// Returns true if the absolute difference of all elements between `self` and `rhs`
329    /// is less than or equal to `max_abs_diff`.
330    ///
331    /// This can be used to compare if two matrices contain similar elements. It works best
332    /// when comparing with a known value. The `max_abs_diff` that should be used used
333    /// depends on the values being compared against.
334    ///
335    /// For more see
336    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
337    #[inline]
338    #[must_use]
339    pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
340        self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
341            && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
342    }
343
344    #[inline]
345    pub fn as_dmat2(&self) -> DMat2 {
346        DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
347    }
348}
349
350impl Default for Mat2 {
351    #[inline]
352    fn default() -> Self {
353        Self::IDENTITY
354    }
355}
356
357impl Add<Mat2> for Mat2 {
358    type Output = Self;
359    #[inline]
360    fn add(self, rhs: Self) -> Self::Output {
361        self.add_mat2(&rhs)
362    }
363}
364
365impl AddAssign<Mat2> for Mat2 {
366    #[inline]
367    fn add_assign(&mut self, rhs: Self) {
368        *self = self.add_mat2(&rhs);
369    }
370}
371
372impl Sub<Mat2> for Mat2 {
373    type Output = Self;
374    #[inline]
375    fn sub(self, rhs: Self) -> Self::Output {
376        self.sub_mat2(&rhs)
377    }
378}
379
380impl SubAssign<Mat2> for Mat2 {
381    #[inline]
382    fn sub_assign(&mut self, rhs: Self) {
383        *self = self.sub_mat2(&rhs);
384    }
385}
386
387impl Neg for Mat2 {
388    type Output = Self;
389    #[inline]
390    fn neg(self) -> Self::Output {
391        Self(unsafe { _mm_xor_ps(self.0, _mm_set1_ps(-0.0)) })
392    }
393}
394
395impl Mul<Mat2> for Mat2 {
396    type Output = Self;
397    #[inline]
398    fn mul(self, rhs: Self) -> Self::Output {
399        self.mul_mat2(&rhs)
400    }
401}
402
403impl MulAssign<Mat2> for Mat2 {
404    #[inline]
405    fn mul_assign(&mut self, rhs: Self) {
406        *self = self.mul_mat2(&rhs);
407    }
408}
409
410impl Mul<Vec2> for Mat2 {
411    type Output = Vec2;
412    #[inline]
413    fn mul(self, rhs: Vec2) -> Self::Output {
414        self.mul_vec2(rhs)
415    }
416}
417
418impl Mul<Mat2> for f32 {
419    type Output = Mat2;
420    #[inline]
421    fn mul(self, rhs: Mat2) -> Self::Output {
422        rhs.mul_scalar(self)
423    }
424}
425
426impl Mul<f32> for Mat2 {
427    type Output = Self;
428    #[inline]
429    fn mul(self, rhs: f32) -> Self::Output {
430        self.mul_scalar(rhs)
431    }
432}
433
434impl MulAssign<f32> for Mat2 {
435    #[inline]
436    fn mul_assign(&mut self, rhs: f32) {
437        *self = self.mul_scalar(rhs);
438    }
439}
440
441impl Sum<Self> for Mat2 {
442    fn sum<I>(iter: I) -> Self
443    where
444        I: Iterator<Item = Self>,
445    {
446        iter.fold(Self::ZERO, Self::add)
447    }
448}
449
450impl<'a> Sum<&'a Self> for Mat2 {
451    fn sum<I>(iter: I) -> Self
452    where
453        I: Iterator<Item = &'a Self>,
454    {
455        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
456    }
457}
458
459impl Product for Mat2 {
460    fn product<I>(iter: I) -> Self
461    where
462        I: Iterator<Item = Self>,
463    {
464        iter.fold(Self::IDENTITY, Self::mul)
465    }
466}
467
468impl<'a> Product<&'a Self> for Mat2 {
469    fn product<I>(iter: I) -> Self
470    where
471        I: Iterator<Item = &'a Self>,
472    {
473        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
474    }
475}
476
477impl PartialEq for Mat2 {
478    #[inline]
479    fn eq(&self, rhs: &Self) -> bool {
480        self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
481    }
482}
483
484#[cfg(not(target_arch = "spirv"))]
485impl AsRef<[f32; 4]> for Mat2 {
486    #[inline]
487    fn as_ref(&self) -> &[f32; 4] {
488        unsafe { &*(self as *const Self as *const [f32; 4]) }
489    }
490}
491
492#[cfg(not(target_arch = "spirv"))]
493impl AsMut<[f32; 4]> for Mat2 {
494    #[inline]
495    fn as_mut(&mut self) -> &mut [f32; 4] {
496        unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
497    }
498}
499
500impl core::ops::Deref for Mat2 {
501    type Target = crate::deref::Cols2<Vec2>;
502    #[inline]
503    fn deref(&self) -> &Self::Target {
504        unsafe { &*(self as *const Self as *const Self::Target) }
505    }
506}
507
508impl core::ops::DerefMut for Mat2 {
509    #[inline]
510    fn deref_mut(&mut self) -> &mut Self::Target {
511        unsafe { &mut *(self as *mut Self as *mut Self::Target) }
512    }
513}
514
515#[cfg(not(target_arch = "spirv"))]
516impl fmt::Debug for Mat2 {
517    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
518        fmt.debug_struct(stringify!(Mat2))
519            .field("x_axis", &self.x_axis)
520            .field("y_axis", &self.y_axis)
521            .finish()
522    }
523}
524
525#[cfg(not(target_arch = "spirv"))]
526impl fmt::Display for Mat2 {
527    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
528        write!(f, "[{}, {}]", self.x_axis, self.y_axis)
529    }
530}