Debian Codegraph @

debian/missing-sources/glam/src/f32/coresimd/mat2.rs

raw
1 // Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{f32::math, swizzles::*, DMat2, Mat3, Mat3A, Vec2};
4 use core::fmt;
5 use core::iter::{Product, Sum};
6 use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
7
8 use core::simd::*;
9
10 /// Creates a 2x2 matrix from two column vectors.
11 #[inline(always)]
12 #[must_use]
13 pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
14 Mat2::from_cols(x_axis, y_axis)
15 }
16
17 /// A 2x2 column major matrix.
18 ///
19 /// SIMD vector types are used for storage on supported platforms.
20 ///
21 /// This type is 16 byte aligned.
22 #[derive(Clone, Copy)]
23 #[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
24 #[repr(transparent)]
25 pub struct Mat2(pub(crate) f32x4);
26
27 impl Mat2 {
28 /// A 2x2 matrix with all elements set to `0.0`.
29 pub const ZERO: Self = Self::from_cols(Vec2::ZERO, Vec2::ZERO);
30
31 /// A 2x2 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
32 pub const IDENTITY: Self = Self::from_cols(Vec2::X, Vec2::Y);
33
34 /// All NAN:s.
35 pub const NAN: Self = Self::from_cols(Vec2::NAN, Vec2::NAN);
36
37 #[allow(clippy::too_many_arguments)]
38 #[inline(always)]
39 #[must_use]
40 const fn new(m00: f32, m01: f32, m10: f32, m11: f32) -> Self {
41 Self(f32x4::from_array([m00, m01, m10, m11]))
42 }
43
44 /// Creates a 2x2 matrix from two column vectors.
45 #[inline(always)]
46 #[must_use]
47 pub const fn from_cols(x_axis: Vec2, y_axis: Vec2) -> Self {
48 Self(f32x4::from_array([x_axis.x, x_axis.y, y_axis.x, y_axis.y]))
49 }
50
51 /// Creates a 2x2 matrix from a `[f32; 4]` array stored in column major order.
52 /// If your data is stored in row major you will need to `transpose` the returned
53 /// matrix.
54 #[inline]
55 #[must_use]
56 pub const fn from_cols_array(m: &[f32; 4]) -> Self {
57 Self(f32x4::from_array(*m))
58 }
59
60 /// Creates a `[f32; 4]` array storing data in column major order.
61 /// If you require data in row major order `transpose` the matrix first.
62 #[inline]
63 #[must_use]
64 pub const fn to_cols_array(&self) -> [f32; 4] {
65 unsafe { *(self as *const Self as *const [f32; 4]) }
66 }
67
68 /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
69 /// If your data is in row major order you will need to `transpose` the returned
70 /// matrix.
71 #[inline]
72 #[must_use]
73 pub const fn from_cols_array_2d(m: &[[f32; 2]; 2]) -> Self {
74 Self::from_cols(Vec2::from_array(m[0]), Vec2::from_array(m[1]))
75 }
76
77 /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order.
78 /// If you require data in row major order `transpose` the matrix first.
79 #[inline]
80 #[must_use]
81 pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] {
82 unsafe { *(self as *const Self as *const [[f32; 2]; 2]) }
83 }
84
85 /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.
86 #[doc(alias = "scale")]
87 #[inline]
88 #[must_use]
89 pub const fn from_diagonal(diagonal: Vec2) -> Self {
90 Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
91 }
92
93 /// Creates a 2x2 matrix containing the combining non-uniform `scale` and rotation of
94 /// `angle` (in radians).
95 #[inline]
96 #[must_use]
97 pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
98 let (sin, cos) = math::sin_cos(angle);
99 Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
100 }
101
102 /// Creates a 2x2 matrix containing a rotation of `angle` (in radians).
103 #[inline]
104 #[must_use]
105 pub fn from_angle(angle: f32) -> Self {
106 let (sin, cos) = math::sin_cos(angle);
107 Self::new(cos, sin, -sin, cos)
108 }
109
110 /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
111 #[inline]
112 #[must_use]
113 pub fn from_mat3(m: Mat3) -> Self {
114 Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
115 }
116
117 /// Creates a 2x2 matrix from the minor of the given 3x3 matrix, discarding the `i`th column
118 /// and `j`th row.
119 ///
120 /// # Panics
121 ///
122 /// Panics if `i` or `j` is greater than 2.
123 #[inline]
124 #[must_use]
125 pub fn from_mat3_minor(m: Mat3, i: usize, j: usize) -> Self {
126 match (i, j) {
127 (0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
128 (0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
129 (0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
130 (1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
131 (1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
132 (1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
133 (2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
134 (2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
135 (2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
136 _ => panic!("index out of bounds"),
137 }
138 }
139
140 /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
141 #[inline]
142 #[must_use]
143 pub fn from_mat3a(m: Mat3A) -> Self {
144 Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
145 }
146
147 /// Creates a 2x2 matrix from the minor of the given 3x3 matrix, discarding the `i`th column
148 /// and `j`th row.
149 ///
150 /// # Panics
151 ///
152 /// Panics if `i` or `j` is greater than 2.
153 #[inline]
154 #[must_use]
155 pub fn from_mat3a_minor(m: Mat3A, i: usize, j: usize) -> Self {
156 match (i, j) {
157 (0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
158 (0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
159 (0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
160 (1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
161 (1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
162 (1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
163 (2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
164 (2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
165 (2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
166 _ => panic!("index out of bounds"),
167 }
168 }
169
170 /// Creates a 2x2 matrix from the first 4 values in `slice`.
171 ///
172 /// # Panics
173 ///
174 /// Panics if `slice` is less than 4 elements long.
175 #[inline]
176 #[must_use]
177 pub const fn from_cols_slice(slice: &[f32]) -> Self {
178 Self::new(slice[0], slice[1], slice[2], slice[3])
179 }
180
181 /// Writes the columns of `self` to the first 4 elements in `slice`.
182 ///
183 /// # Panics
184 ///
185 /// Panics if `slice` is less than 4 elements long.
186 #[inline]
187 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
188 slice[0] = self.x_axis.x;
189 slice[1] = self.x_axis.y;
190 slice[2] = self.y_axis.x;
191 slice[3] = self.y_axis.y;
192 }
193
194 /// Returns the matrix column for the given `index`.
195 ///
196 /// # Panics
197 ///
198 /// Panics if `index` is greater than 1.
199 #[inline]
200 #[must_use]
201 pub fn col(&self, index: usize) -> Vec2 {
202 match index {
203 0 => self.x_axis,
204 1 => self.y_axis,
205 _ => panic!("index out of bounds"),
206 }
207 }
208
209 /// Returns a mutable reference to the matrix column for the given `index`.
210 ///
211 /// # Panics
212 ///
213 /// Panics if `index` is greater than 1.
214 #[inline]
215 pub fn col_mut(&mut self, index: usize) -> &mut Vec2 {
216 match index {
217 0 => &mut self.x_axis,
218 1 => &mut self.y_axis,
219 _ => panic!("index out of bounds"),
220 }
221 }
222
223 /// Returns the matrix row for the given `index`.
224 ///
225 /// # Panics
226 ///
227 /// Panics if `index` is greater than 1.
228 #[inline]
229 #[must_use]
230 pub fn row(&self, index: usize) -> Vec2 {
231 match index {
232 0 => Vec2::new(self.x_axis.x, self.y_axis.x),
233 1 => Vec2::new(self.x_axis.y, self.y_axis.y),
234 _ => panic!("index out of bounds"),
235 }
236 }
237
238 /// Returns `true` if, and only if, all elements are finite.
239 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
240 #[inline]
241 #[must_use]
242 pub fn is_finite(&self) -> bool {
243 self.x_axis.is_finite() && self.y_axis.is_finite()
244 }
245
246 /// Returns `true` if any elements are `NaN`.
247 #[inline]
248 #[must_use]
249 pub fn is_nan(&self) -> bool {
250 self.x_axis.is_nan() || self.y_axis.is_nan()
251 }
252
253 /// Returns the transpose of `self`.
254 #[inline]
255 #[must_use]
256 pub fn transpose(&self) -> Self {
257 Self(simd_swizzle!(self.0, [0, 2, 1, 3]))
258 }
259
260 /// Returns the determinant of `self`.
261 #[inline]
262 #[must_use]
263 pub fn determinant(&self) -> f32 {
264 let abcd = self.0;
265 let dcba = simd_swizzle!(abcd, [3, 2, 1, 0]);
266 let prod = abcd * dcba;
267 let det = prod - simd_swizzle!(prod, [1, 1, 1, 1]);
268 det[0]
269 }
270
271 /// Returns the inverse of `self`.
272 ///
273 /// If the matrix is not invertible the returned matrix will be invalid.
274 ///
275 /// # Panics
276 ///
277 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
278 #[inline]
279 #[must_use]
280 pub fn inverse(&self) -> Self {
281 const SIGN: f32x4 = f32x4::from_array([1.0, -1.0, -1.0, 1.0]);
282 let abcd = self.0;
283 let dcba = simd_swizzle!(abcd, [3, 2, 1, 0]);
284 let prod = abcd * dcba;
285 let sub = prod - simd_swizzle!(prod, [1, 1, 1, 1]);
286 let det = simd_swizzle!(sub, [0, 0, 0, 0]);
287 let tmp = SIGN / det;
288 glam_assert!(Mat2(tmp).is_finite());
289 let dbca = simd_swizzle!(abcd, [3, 1, 2, 0]);
290 Self(dbca.mul(tmp))
291 }
292
293 /// Transforms a 2D vector.
294 #[inline]
295 #[must_use]
296 pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
297 let abcd = self.0;
298 let xxyy = f32x4::from_array([rhs.x, rhs.x, rhs.y, rhs.y]);
299 let axbxcydy = abcd.mul(xxyy);
300 let cydyaxbx = simd_swizzle!(axbxcydy, [2, 3, 0, 1]);
301 let result = axbxcydy.add(cydyaxbx);
302 unsafe { *(&result as *const f32x4 as *const Vec2) }
303 }
304
305 /// Multiplies two 2x2 matrices.
306 #[inline]
307 #[must_use]
308 pub fn mul_mat2(&self, rhs: &Self) -> Self {
309 self.mul(rhs)
310 }
311
312 /// Adds two 2x2 matrices.
313 #[inline]
314 #[must_use]
315 pub fn add_mat2(&self, rhs: &Self) -> Self {
316 self.add(rhs)
317 }
318
319 /// Subtracts two 2x2 matrices.
320 #[inline]
321 #[must_use]
322 pub fn sub_mat2(&self, rhs: &Self) -> Self {
323 self.sub(rhs)
324 }
325
326 /// Multiplies a 2x2 matrix by a scalar.
327 #[inline]
328 #[must_use]
329 pub fn mul_scalar(&self, rhs: f32) -> Self {
330 Self(self.0 * f32x4::splat(rhs))
331 }
332
333 /// Divides a 2x2 matrix by a scalar.
334 #[inline]
335 #[must_use]
336 pub fn div_scalar(&self, rhs: f32) -> Self {
337 Self(self.0 / f32x4::splat(rhs))
338 }
339
340 /// Returns true if the absolute difference of all elements between `self` and `rhs`
341 /// is less than or equal to `max_abs_diff`.
342 ///
343 /// This can be used to compare if two matrices contain similar elements. It works best
344 /// when comparing with a known value. The `max_abs_diff` that should be used used
345 /// depends on the values being compared against.
346 ///
347 /// For more see
348 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
349 #[inline]
350 #[must_use]
351 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
352 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
353 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
354 }
355
356 /// Takes the absolute value of each element in `self`
357 #[inline]
358 #[must_use]
359 pub fn abs(&self) -> Self {
360 Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
361 }
362
363 #[inline]
364 pub fn as_dmat2(&self) -> DMat2 {
365 DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
366 }
367 }
368
369 impl Default for Mat2 {
370 #[inline]
371 fn default() -> Self {
372 Self::IDENTITY
373 }
374 }
375
376 impl Add for Mat2 {
377 type Output = Self;
378 #[inline]
379 fn add(self, rhs: Self) -> Self {
380 Self(self.0 + rhs.0)
381 }
382 }
383
384 impl Add<&Self> for Mat2 {
385 type Output = Self;
386 #[inline]
387 fn add(self, rhs: &Self) -> Self {
388 self.add(*rhs)
389 }
390 }
391
392 impl Add<&Mat2> for &Mat2 {
393 type Output = Mat2;
394 #[inline]
395 fn add(self, rhs: &Mat2) -> Mat2 {
396 (*self).add(*rhs)
397 }
398 }
399
400 impl Add<Mat2> for &Mat2 {
401 type Output = Mat2;
402 #[inline]
403 fn add(self, rhs: Mat2) -> Mat2 {
404 (*self).add(rhs)
405 }
406 }
407
408 impl AddAssign for Mat2 {
409 #[inline]
410 fn add_assign(&mut self, rhs: Self) {
411 *self = self.add(rhs);
412 }
413 }
414
415 impl AddAssign<&Self> for Mat2 {
416 #[inline]
417 fn add_assign(&mut self, rhs: &Self) {
418 self.add_assign(*rhs);
419 }
420 }
421
422 impl Sub for Mat2 {
423 type Output = Self;
424 #[inline]
425 fn sub(self, rhs: Self) -> Self {
426 Self(self.0 - rhs.0)
427 }
428 }
429
430 impl Sub<&Self> for Mat2 {
431 type Output = Self;
432 #[inline]
433 fn sub(self, rhs: &Self) -> Self {
434 self.sub(*rhs)
435 }
436 }
437
438 impl Sub<&Mat2> for &Mat2 {
439 type Output = Mat2;
440 #[inline]
441 fn sub(self, rhs: &Mat2) -> Mat2 {
442 (*self).sub(*rhs)
443 }
444 }
445
446 impl Sub<Mat2> for &Mat2 {
447 type Output = Mat2;
448 #[inline]
449 fn sub(self, rhs: Mat2) -> Mat2 {
450 (*self).sub(rhs)
451 }
452 }
453
454 impl SubAssign for Mat2 {
455 #[inline]
456 fn sub_assign(&mut self, rhs: Self) {
457 *self = self.sub(rhs);
458 }
459 }
460
461 impl SubAssign<&Self> for Mat2 {
462 #[inline]
463 fn sub_assign(&mut self, rhs: &Self) {
464 self.sub_assign(*rhs);
465 }
466 }
467
468 impl Neg for Mat2 {
469 type Output = Self;
470 #[inline]
471 fn neg(self) -> Self::Output {
472 Self(-self.0)
473 }
474 }
475
476 impl Neg for &Mat2 {
477 type Output = Mat2;
478 #[inline]
479 fn neg(self) -> Mat2 {
480 (*self).neg()
481 }
482 }
483
484 impl Mul for Mat2 {
485 type Output = Self;
486 #[inline]
487 fn mul(self, rhs: Self) -> Self {
488 let abcd = self.0;
489 let xxyy0 = simd_swizzle!(rhs.0, [0, 0, 1, 1]);
490 let xxyy1 = simd_swizzle!(rhs.0, [2, 2, 3, 3]);
491 let axbxcydy0 = abcd * xxyy0;
492 let axbxcydy1 = abcd * xxyy1;
493 let cydyaxbx0 = simd_swizzle!(axbxcydy0, [2, 3, 0, 1]);
494 let cydyaxbx1 = simd_swizzle!(axbxcydy1, [2, 3, 0, 1]);
495 let result0 = axbxcydy0 + cydyaxbx0;
496 let result1 = axbxcydy1 + cydyaxbx1;
497 Self(simd_swizzle!(result0, result1, [0, 1, 4, 5]))
498 }
499 }
500
501 impl Mul<&Self> for Mat2 {
502 type Output = Self;
503 #[inline]
504 fn mul(self, rhs: &Self) -> Self {
505 self.mul(*rhs)
506 }
507 }
508
509 impl Mul<&Mat2> for &Mat2 {
510 type Output = Mat2;
511 #[inline]
512 fn mul(self, rhs: &Mat2) -> Mat2 {
513 (*self).mul(*rhs)
514 }
515 }
516
517 impl Mul<Mat2> for &Mat2 {
518 type Output = Mat2;
519 #[inline]
520 fn mul(self, rhs: Mat2) -> Mat2 {
521 (*self).mul(rhs)
522 }
523 }
524
525 impl MulAssign for Mat2 {
526 #[inline]
527 fn mul_assign(&mut self, rhs: Self) {
528 *self = self.mul(rhs);
529 }
530 }
531
532 impl MulAssign<&Self> for Mat2 {
533 #[inline]
534 fn mul_assign(&mut self, rhs: &Self) {
535 self.mul_assign(*rhs);
536 }
537 }
538
539 impl Mul<Vec2> for Mat2 {
540 type Output = Vec2;
541 #[inline]
542 fn mul(self, rhs: Vec2) -> Self::Output {
543 self.mul_vec2(rhs)
544 }
545 }
546
547 impl Mul<&Vec2> for Mat2 {
548 type Output = Vec2;
549 #[inline]
550 fn mul(self, rhs: &Vec2) -> Vec2 {
551 self.mul(*rhs)
552 }
553 }
554
555 impl Mul<&Vec2> for &Mat2 {
556 type Output = Vec2;
557 #[inline]
558 fn mul(self, rhs: &Vec2) -> Vec2 {
559 (*self).mul(*rhs)
560 }
561 }
562
563 impl Mul<Vec2> for &Mat2 {
564 type Output = Vec2;
565 #[inline]
566 fn mul(self, rhs: Vec2) -> Vec2 {
567 (*self).mul(rhs)
568 }
569 }
570
571 impl Mul<Mat2> for f32 {
572 type Output = Mat2;
573 #[inline]
574 fn mul(self, rhs: Mat2) -> Self::Output {
575 rhs.mul_scalar(self)
576 }
577 }
578
579 impl Mul<&Mat2> for f32 {
580 type Output = Mat2;
581 #[inline]
582 fn mul(self, rhs: &Mat2) -> Mat2 {
583 self.mul(*rhs)
584 }
585 }
586
587 impl Mul<&Mat2> for &f32 {
588 type Output = Mat2;
589 #[inline]
590 fn mul(self, rhs: &Mat2) -> Mat2 {
591 (*self).mul(*rhs)
592 }
593 }
594
595 impl Mul<Mat2> for &f32 {
596 type Output = Mat2;
597 #[inline]
598 fn mul(self, rhs: Mat2) -> Mat2 {
599 (*self).mul(rhs)
600 }
601 }
602
603 impl Mul<f32> for Mat2 {
604 type Output = Self;
605 #[inline]
606 fn mul(self, rhs: f32) -> Self {
607 self.mul_scalar(rhs)
608 }
609 }
610
611 impl Mul<&f32> for Mat2 {
612 type Output = Self;
613 #[inline]
614 fn mul(self, rhs: &f32) -> Self {
615 self.mul(*rhs)
616 }
617 }
618
619 impl Mul<&f32> for &Mat2 {
620 type Output = Mat2;
621 #[inline]
622 fn mul(self, rhs: &f32) -> Mat2 {
623 (*self).mul(*rhs)
624 }
625 }
626
627 impl Mul<f32> for &Mat2 {
628 type Output = Mat2;
629 #[inline]
630 fn mul(self, rhs: f32) -> Mat2 {
631 (*self).mul(rhs)
632 }
633 }
634
635 impl MulAssign<f32> for Mat2 {
636 #[inline]
637 fn mul_assign(&mut self, rhs: f32) {
638 *self = self.mul(rhs);
639 }
640 }
641
642 impl MulAssign<&f32> for Mat2 {
643 #[inline]
644 fn mul_assign(&mut self, rhs: &f32) {
645 self.mul_assign(*rhs);
646 }
647 }
648
649 impl Div<Mat2> for f32 {
650 type Output = Mat2;
651 #[inline]
652 fn div(self, rhs: Mat2) -> Self::Output {
653 rhs.div_scalar(self)
654 }
655 }
656
657 impl Div<&Mat2> for f32 {
658 type Output = Mat2;
659 #[inline]
660 fn div(self, rhs: &Mat2) -> Mat2 {
661 self.div(*rhs)
662 }
663 }
664
665 impl Div<&Mat2> for &f32 {
666 type Output = Mat2;
667 #[inline]
668 fn div(self, rhs: &Mat2) -> Mat2 {
669 (*self).div(*rhs)
670 }
671 }
672
673 impl Div<Mat2> for &f32 {
674 type Output = Mat2;
675 #[inline]
676 fn div(self, rhs: Mat2) -> Mat2 {
677 (*self).div(rhs)
678 }
679 }
680
681 impl Div<f32> for Mat2 {
682 type Output = Self;
683 #[inline]
684 fn div(self, rhs: f32) -> Self {
685 self.div_scalar(rhs)
686 }
687 }
688
689 impl Div<&f32> for Mat2 {
690 type Output = Self;
691 #[inline]
692 fn div(self, rhs: &f32) -> Self {
693 self.div(*rhs)
694 }
695 }
696
697 impl Div<&f32> for &Mat2 {
698 type Output = Mat2;
699 #[inline]
700 fn div(self, rhs: &f32) -> Mat2 {
701 (*self).div(*rhs)
702 }
703 }
704
705 impl Div<f32> for &Mat2 {
706 type Output = Mat2;
707 #[inline]
708 fn div(self, rhs: f32) -> Mat2 {
709 (*self).div(rhs)
710 }
711 }
712
713 impl DivAssign<f32> for Mat2 {
714 #[inline]
715 fn div_assign(&mut self, rhs: f32) {
716 *self = self.div(rhs);
717 }
718 }
719
720 impl DivAssign<&f32> for Mat2 {
721 #[inline]
722 fn div_assign(&mut self, rhs: &f32) {
723 self.div_assign(*rhs);
724 }
725 }
726
727 impl Sum<Self> for Mat2 {
728 fn sum<I>(iter: I) -> Self
729 where
730 I: Iterator<Item = Self>,
731 {
732 iter.fold(Self::ZERO, Self::add)
733 }
734 }
735
736 impl<'a> Sum<&'a Self> for Mat2 {
737 fn sum<I>(iter: I) -> Self
738 where
739 I: Iterator<Item = &'a Self>,
740 {
741 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
742 }
743 }
744
745 impl Product for Mat2 {
746 fn product<I>(iter: I) -> Self
747 where
748 I: Iterator<Item = Self>,
749 {
750 iter.fold(Self::IDENTITY, Self::mul)
751 }
752 }
753
754 impl<'a> Product<&'a Self> for Mat2 {
755 fn product<I>(iter: I) -> Self
756 where
757 I: Iterator<Item = &'a Self>,
758 {
759 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
760 }
761 }
762
763 impl PartialEq for Mat2 {
764 #[inline]
765 fn eq(&self, rhs: &Self) -> bool {
766 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
767 }
768 }
769
770 impl AsRef<[f32; 4]> for Mat2 {
771 #[inline]
772 fn as_ref(&self) -> &[f32; 4] {
773 unsafe { &*(self as *const Self as *const [f32; 4]) }
774 }
775 }
776
777 impl AsMut<[f32; 4]> for Mat2 {
778 #[inline]
779 fn as_mut(&mut self) -> &mut [f32; 4] {
780 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
781 }
782 }
783
784 impl core::ops::Deref for Mat2 {
785 type Target = crate::deref::Cols2<Vec2>;
786 #[inline]
787 fn deref(&self) -> &Self::Target {
788 unsafe { &*(self as *const Self as *const Self::Target) }
789 }
790 }
791
792 impl core::ops::DerefMut for Mat2 {
793 #[inline]
794 fn deref_mut(&mut self) -> &mut Self::Target {
795 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
796 }
797 }
798
799 impl fmt::Debug for Mat2 {
800 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
801 fmt.debug_struct(stringify!(Mat2))
802 .field("x_axis", &self.x_axis)
803 .field("y_axis", &self.y_axis)
804 .finish()
805 }
806 }
807
808 impl fmt::Display for Mat2 {
809 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
810 if let Some(p) = f.precision() {
811 write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
812 } else {
813 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
814 }
815 }
816 }