1use 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#[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#[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 pub const ZERO: Self = Self::from_cols(Vec2::ZERO, Vec2::ZERO);
46
47 pub const IDENTITY: Self = Self::from_cols(Vec2::X, Vec2::Y);
49
50 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[inline]
288 #[must_use]
289 pub fn diagonal(&self) -> Vec2 {
290 Vec2::new(self.x_axis.x, self.y_axis.y)
291 }
292
293 #[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 #[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 #[inline]
349 #[must_use]
350 pub fn inverse(&self) -> Self {
351 self.inverse_checked::<false>().0
352 }
353
354 #[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 #[inline]
368 #[must_use]
369 pub fn inverse_or_zero(&self) -> Self {
370 self.inverse_checked::<true>().0
371 }
372
373 #[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 #[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 #[inline]
400 #[must_use]
401 pub fn mul_mat2(&self, rhs: &Self) -> Self {
402 self.mul(rhs)
403 }
404
405 #[inline]
407 #[must_use]
408 pub fn add_mat2(&self, rhs: &Self) -> Self {
409 self.add(rhs)
410 }
411
412 #[inline]
414 #[must_use]
415 pub fn sub_mat2(&self, rhs: &Self) -> Self {
416 self.sub(rhs)
417 }
418
419 #[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 #[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 #[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 #[inline]
444 #[must_use]
445 pub fn recip(&self) -> Self {
446 Self::from_cols(self.x_axis.recip(), self.y_axis.recip())
447 }
448
449 #[inline]
459 #[must_use]
460 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
461 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
462 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
463 }
464
465 #[inline]
467 #[must_use]
468 pub fn abs(&self) -> Self {
469 Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
470 }
471
472 #[inline]
473 pub fn as_dmat2(&self) -> DMat2 {
474 DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
475 }
476}
477
478impl Default for Mat2 {
479 #[inline]
480 fn default() -> Self {
481 Self::IDENTITY
482 }
483}
484
485impl Add for Mat2 {
486 type Output = Self;
487 #[inline]
488 fn add(self, rhs: Self) -> Self {
489 Self(unsafe { _mm_add_ps(self.0, rhs.0) })
490 }
491}
492
493impl Add<&Self> for Mat2 {
494 type Output = Self;
495 #[inline]
496 fn add(self, rhs: &Self) -> Self {
497 self.add(*rhs)
498 }
499}
500
501impl Add<&Mat2> for &Mat2 {
502 type Output = Mat2;
503 #[inline]
504 fn add(self, rhs: &Mat2) -> Mat2 {
505 (*self).add(*rhs)
506 }
507}
508
509impl Add<Mat2> for &Mat2 {
510 type Output = Mat2;
511 #[inline]
512 fn add(self, rhs: Mat2) -> Mat2 {
513 (*self).add(rhs)
514 }
515}
516
517impl AddAssign for Mat2 {
518 #[inline]
519 fn add_assign(&mut self, rhs: Self) {
520 *self = self.add(rhs);
521 }
522}
523
524impl AddAssign<&Self> for Mat2 {
525 #[inline]
526 fn add_assign(&mut self, rhs: &Self) {
527 self.add_assign(*rhs);
528 }
529}
530
531impl Sub for Mat2 {
532 type Output = Self;
533 #[inline]
534 fn sub(self, rhs: Self) -> Self {
535 Self(unsafe { _mm_sub_ps(self.0, rhs.0) })
536 }
537}
538
539impl Sub<&Self> for Mat2 {
540 type Output = Self;
541 #[inline]
542 fn sub(self, rhs: &Self) -> Self {
543 self.sub(*rhs)
544 }
545}
546
547impl Sub<&Mat2> for &Mat2 {
548 type Output = Mat2;
549 #[inline]
550 fn sub(self, rhs: &Mat2) -> Mat2 {
551 (*self).sub(*rhs)
552 }
553}
554
555impl Sub<Mat2> for &Mat2 {
556 type Output = Mat2;
557 #[inline]
558 fn sub(self, rhs: Mat2) -> Mat2 {
559 (*self).sub(rhs)
560 }
561}
562
563impl SubAssign for Mat2 {
564 #[inline]
565 fn sub_assign(&mut self, rhs: Self) {
566 *self = self.sub(rhs);
567 }
568}
569
570impl SubAssign<&Self> for Mat2 {
571 #[inline]
572 fn sub_assign(&mut self, rhs: &Self) {
573 self.sub_assign(*rhs);
574 }
575}
576
577impl Neg for Mat2 {
578 type Output = Self;
579 #[inline]
580 fn neg(self) -> Self::Output {
581 Self(unsafe { _mm_xor_ps(self.0, _mm_set1_ps(-0.0)) })
582 }
583}
584
585impl Neg for &Mat2 {
586 type Output = Mat2;
587 #[inline]
588 fn neg(self) -> Mat2 {
589 (*self).neg()
590 }
591}
592
593impl Mul for Mat2 {
594 type Output = Self;
595 #[inline]
596 fn mul(self, rhs: Self) -> Self {
597 unsafe {
598 let abcd = self.0;
599 let rhs = rhs.0;
600 let xxyy0 = _mm_shuffle_ps(rhs, rhs, 0b01_01_00_00);
601 let xxyy1 = _mm_shuffle_ps(rhs, rhs, 0b11_11_10_10);
602 let axbxcydy0 = _mm_mul_ps(abcd, xxyy0);
603 let axbxcydy1 = _mm_mul_ps(abcd, xxyy1);
604 let cydyaxbx0 = _mm_shuffle_ps(axbxcydy0, axbxcydy0, 0b01_00_11_10);
605 let cydyaxbx1 = _mm_shuffle_ps(axbxcydy1, axbxcydy1, 0b01_00_11_10);
606 let result0 = _mm_add_ps(axbxcydy0, cydyaxbx0);
607 let result1 = _mm_add_ps(axbxcydy1, cydyaxbx1);
608 Self(_mm_shuffle_ps(result0, result1, 0b01_00_01_00))
609 }
610 }
611}
612
613impl Mul<&Self> for Mat2 {
614 type Output = Self;
615 #[inline]
616 fn mul(self, rhs: &Self) -> Self {
617 self.mul(*rhs)
618 }
619}
620
621impl Mul<&Mat2> for &Mat2 {
622 type Output = Mat2;
623 #[inline]
624 fn mul(self, rhs: &Mat2) -> Mat2 {
625 (*self).mul(*rhs)
626 }
627}
628
629impl Mul<Mat2> for &Mat2 {
630 type Output = Mat2;
631 #[inline]
632 fn mul(self, rhs: Mat2) -> Mat2 {
633 (*self).mul(rhs)
634 }
635}
636
637impl MulAssign for Mat2 {
638 #[inline]
639 fn mul_assign(&mut self, rhs: Self) {
640 *self = self.mul(rhs);
641 }
642}
643
644impl MulAssign<&Self> for Mat2 {
645 #[inline]
646 fn mul_assign(&mut self, rhs: &Self) {
647 self.mul_assign(*rhs);
648 }
649}
650
651impl Mul<Vec2> for Mat2 {
652 type Output = Vec2;
653 #[inline]
654 fn mul(self, rhs: Vec2) -> Self::Output {
655 self.mul_vec2(rhs)
656 }
657}
658
659impl Mul<&Vec2> for Mat2 {
660 type Output = Vec2;
661 #[inline]
662 fn mul(self, rhs: &Vec2) -> Vec2 {
663 self.mul(*rhs)
664 }
665}
666
667impl Mul<&Vec2> for &Mat2 {
668 type Output = Vec2;
669 #[inline]
670 fn mul(self, rhs: &Vec2) -> Vec2 {
671 (*self).mul(*rhs)
672 }
673}
674
675impl Mul<Vec2> for &Mat2 {
676 type Output = Vec2;
677 #[inline]
678 fn mul(self, rhs: Vec2) -> Vec2 {
679 (*self).mul(rhs)
680 }
681}
682
683impl Mul<Mat2> for f32 {
684 type Output = Mat2;
685 #[inline]
686 fn mul(self, rhs: Mat2) -> Self::Output {
687 rhs.mul_scalar(self)
688 }
689}
690
691impl Mul<&Mat2> for f32 {
692 type Output = Mat2;
693 #[inline]
694 fn mul(self, rhs: &Mat2) -> Mat2 {
695 self.mul(*rhs)
696 }
697}
698
699impl Mul<&Mat2> for &f32 {
700 type Output = Mat2;
701 #[inline]
702 fn mul(self, rhs: &Mat2) -> Mat2 {
703 (*self).mul(*rhs)
704 }
705}
706
707impl Mul<Mat2> for &f32 {
708 type Output = Mat2;
709 #[inline]
710 fn mul(self, rhs: Mat2) -> Mat2 {
711 (*self).mul(rhs)
712 }
713}
714
715impl Mul<f32> for Mat2 {
716 type Output = Self;
717 #[inline]
718 fn mul(self, rhs: f32) -> Self {
719 self.mul_scalar(rhs)
720 }
721}
722
723impl Mul<&f32> for Mat2 {
724 type Output = Self;
725 #[inline]
726 fn mul(self, rhs: &f32) -> Self {
727 self.mul(*rhs)
728 }
729}
730
731impl Mul<&f32> for &Mat2 {
732 type Output = Mat2;
733 #[inline]
734 fn mul(self, rhs: &f32) -> Mat2 {
735 (*self).mul(*rhs)
736 }
737}
738
739impl Mul<f32> for &Mat2 {
740 type Output = Mat2;
741 #[inline]
742 fn mul(self, rhs: f32) -> Mat2 {
743 (*self).mul(rhs)
744 }
745}
746
747impl MulAssign<f32> for Mat2 {
748 #[inline]
749 fn mul_assign(&mut self, rhs: f32) {
750 *self = self.mul(rhs);
751 }
752}
753
754impl MulAssign<&f32> for Mat2 {
755 #[inline]
756 fn mul_assign(&mut self, rhs: &f32) {
757 self.mul_assign(*rhs);
758 }
759}
760
761impl Div<Mat2> for f32 {
762 type Output = Mat2;
763 #[inline]
764 fn div(self, rhs: Mat2) -> Self::Output {
765 Mat2(unsafe { _mm_div_ps(_mm_set_ps1(self), rhs.0) })
766 }
767}
768
769impl Div<&Mat2> for f32 {
770 type Output = Mat2;
771 #[inline]
772 fn div(self, rhs: &Mat2) -> Mat2 {
773 self.div(*rhs)
774 }
775}
776
777impl Div<&Mat2> for &f32 {
778 type Output = Mat2;
779 #[inline]
780 fn div(self, rhs: &Mat2) -> Mat2 {
781 (*self).div(*rhs)
782 }
783}
784
785impl Div<Mat2> for &f32 {
786 type Output = Mat2;
787 #[inline]
788 fn div(self, rhs: Mat2) -> Mat2 {
789 (*self).div(rhs)
790 }
791}
792
793impl Div<f32> for Mat2 {
794 type Output = Self;
795 #[inline]
796 fn div(self, rhs: f32) -> Self {
797 self.div_scalar(rhs)
798 }
799}
800
801impl Div<&f32> for Mat2 {
802 type Output = Self;
803 #[inline]
804 fn div(self, rhs: &f32) -> Self {
805 self.div(*rhs)
806 }
807}
808
809impl Div<&f32> for &Mat2 {
810 type Output = Mat2;
811 #[inline]
812 fn div(self, rhs: &f32) -> Mat2 {
813 (*self).div(*rhs)
814 }
815}
816
817impl Div<f32> for &Mat2 {
818 type Output = Mat2;
819 #[inline]
820 fn div(self, rhs: f32) -> Mat2 {
821 (*self).div(rhs)
822 }
823}
824
825impl DivAssign<f32> for Mat2 {
826 #[inline]
827 fn div_assign(&mut self, rhs: f32) {
828 *self = self.div(rhs);
829 }
830}
831
832impl DivAssign<&f32> for Mat2 {
833 #[inline]
834 fn div_assign(&mut self, rhs: &f32) {
835 self.div_assign(*rhs);
836 }
837}
838
839impl Sum<Self> for Mat2 {
840 fn sum<I>(iter: I) -> Self
841 where
842 I: Iterator<Item = Self>,
843 {
844 iter.fold(Self::ZERO, Self::add)
845 }
846}
847
848impl<'a> Sum<&'a Self> for Mat2 {
849 fn sum<I>(iter: I) -> Self
850 where
851 I: Iterator<Item = &'a Self>,
852 {
853 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
854 }
855}
856
857impl Product for Mat2 {
858 fn product<I>(iter: I) -> Self
859 where
860 I: Iterator<Item = Self>,
861 {
862 iter.fold(Self::IDENTITY, Self::mul)
863 }
864}
865
866impl<'a> Product<&'a Self> for Mat2 {
867 fn product<I>(iter: I) -> Self
868 where
869 I: Iterator<Item = &'a Self>,
870 {
871 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
872 }
873}
874
875impl PartialEq for Mat2 {
876 #[inline]
877 fn eq(&self, rhs: &Self) -> bool {
878 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
879 }
880}
881
882impl AsRef<[f32; 4]> for Mat2 {
883 #[inline]
884 fn as_ref(&self) -> &[f32; 4] {
885 unsafe { &*(self as *const Self as *const [f32; 4]) }
886 }
887}
888
889impl AsMut<[f32; 4]> for Mat2 {
890 #[inline]
891 fn as_mut(&mut self) -> &mut [f32; 4] {
892 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
893 }
894}
895
896impl core::ops::Deref for Mat2 {
897 type Target = crate::deref::Cols2<Vec2>;
898 #[inline]
899 fn deref(&self) -> &Self::Target {
900 unsafe { &*(self as *const Self as *const Self::Target) }
901 }
902}
903
904impl core::ops::DerefMut for Mat2 {
905 #[inline]
906 fn deref_mut(&mut self) -> &mut Self::Target {
907 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
908 }
909}
910
911impl fmt::Debug for Mat2 {
912 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
913 fmt.debug_struct(stringify!(Mat2))
914 .field("x_axis", &self.x_axis)
915 .field("y_axis", &self.y_axis)
916 .finish()
917 }
918}
919
920impl fmt::Display for Mat2 {
921 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
922 if let Some(p) = f.precision() {
923 write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
924 } else {
925 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
926 }
927 }
928}