1#[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#[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-08",
38 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
39)]
40#[repr(transparent)]
41pub struct Mat2(pub(crate) float32x4_t);
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)]
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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[inline]
387 #[must_use]
388 pub fn diagonal(&self) -> Vec2 {
389 Vec2::new(self.x_axis.x, self.y_axis.y)
390 }
391
392 #[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 #[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 #[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 #[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 #[inline]
470 #[must_use]
471 pub fn inverse_or_zero(&self) -> Self {
472 self.inverse_checked::<true>().0
473 }
474
475 #[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 = 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 #[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 #[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 #[inline]
510 #[must_use]
511 pub fn mul_mat2(&self, rhs: &Self) -> Self {
512 self.mul(rhs)
513 }
514
515 #[inline]
517 #[must_use]
518 pub fn add_mat2(&self, rhs: &Self) -> Self {
519 self.add(rhs)
520 }
521
522 #[inline]
524 #[must_use]
525 pub fn sub_mat2(&self, rhs: &Self) -> Self {
526 self.sub(rhs)
527 }
528
529 #[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 #[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 #[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 #[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 #[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 #[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}