1use 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#[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#[derive(Clone, Copy)]
33#[repr(transparent)]
34pub struct Mat2(pub(crate) __m128);
35
36impl Mat2 {
37 pub const ZERO: Self = Self::from_cols(Vec2::ZERO, Vec2::ZERO);
39
40 pub const IDENTITY: Self = Self::from_cols(Vec2::X, Vec2::Y);
42
43 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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}