Debian Codegraph @

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