Debian Codegraph @

debian/missing-sources/glam-0.28.0/src/f32/sse2/mat2.rs

raw symbols SCIP
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 #[cfg(target_arch = "x86")]
10 use core::arch::x86::*;
11 #[cfg(target_arch = "x86_64")]
12 use core::arch::x86_64::*;
13
14 #[repr(C)]
15 union UnionCast {
16 a: [f32; 4],
17 v: Mat2,
18 }
19
20 /// Creates a 2x2 matrix from two column vectors.
21 #[inline(always)]
22 #[must_use]
23 pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
24 Mat2::from_cols(x_axis, y_axis)
25 }
26
27 /// A 2x2 column major matrix.
28 ///
29 /// SIMD vector types are used for storage on supported platforms.
30 ///
31 /// This type is 16 byte aligned.
32 #[derive(Clone, Copy)]
33 #[repr(transparent)]
34 pub struct Mat2(pub(crate) __m128);
35
36 impl Mat2 {
37 /// A 2x2 matrix with all elements set to `0.0`.
38 pub const ZERO: Self = Self::from_cols(Vec2::ZERO, Vec2::ZERO);
39
40 /// A 2x2 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
41 pub const IDENTITY: Self = Self::from_cols(Vec2::X, Vec2::Y);
42
43 /// All NAN:s.
44 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 /// Creates a 2x2 matrix from two column vectors.
59 #[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 /// Creates a 2x2 matrix from a `[f32; 4]` array stored in column major order.
71 /// If your data is stored in row major you will need to `transpose` the returned
72 /// matrix.
73 #[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 /// Creates a `[f32; 4]` array storing data in column major order.
80 /// If you require data in row major order `transpose` the matrix first.
81 #[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 /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
88 /// If your data is in row major order you will need to `transpose` the returned
89 /// matrix.
90 #[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 /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order.
97 /// If you require data in row major order `transpose` the matrix first.
98 #[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 /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.
105 #[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 /// Creates a 2x2 matrix containing the combining non-uniform `scale` and rotation of
113 /// `angle` (in radians).
114 #[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 /// Creates a 2x2 matrix containing a rotation of `angle` (in radians).
122 #[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 /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
130 #[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 /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
137 #[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 /// Creates a 2x2 matrix from the first 4 values in `slice`.
144 ///
145 /// # Panics
146 ///
147 /// Panics if `slice` is less than 4 elements long.
148 #[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 /// Writes the columns of `self` to the first 4 elements in `slice`.
155 ///
156 /// # Panics
157 ///
158 /// Panics if `slice` is less than 4 elements long.
159 #[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 /// Returns the matrix column for the given `index`.
168 ///
169 /// # Panics
170 ///
171 /// Panics if `index` is greater than 1.
172 #[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 /// Returns a mutable reference to the matrix column for the given `index`.
183 ///
184 /// # Panics
185 ///
186 /// Panics if `index` is greater than 1.
187 #[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 /// Returns the matrix row for the given `index`.
197 ///
198 /// # Panics
199 ///
200 /// Panics if `index` is greater than 1.
201 #[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 /// Returns `true` if, and only if, all elements are finite.
212 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
213 #[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 /// Returns `true` if any elements are `NaN`.
220 #[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 /// Returns the transpose of `self`.
227 #[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 /// Returns the determinant of `self`.
234 #[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 /// Returns the inverse of `self`.
247 ///
248 /// If the matrix is not invertible the returned matrix will be invalid.
249 ///
250 /// # Panics
251 ///
252 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
253 #[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 /// Transforms a 2D vector.
271 #[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 /// Multiplies two 2x2 matrices.
289 #[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 /// Adds two 2x2 matrices.
308 #[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 /// Subtracts two 2x2 matrices.
315 #[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 /// Multiplies a 2x2 matrix by a scalar.
322 #[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 /// Divides a 2x2 matrix by a scalar.
329 #[inline]
330 #[must_use]
331 pub fn div_scalar(&self, rhs: f32) -> Self {
332 Self(unsafe { _mm_div_ps(self.0, _mm_set_ps1(rhs)) })
333 }
334
335 /// Returns true if the absolute difference of all elements between `self` and `rhs`
336 /// is less than or equal to `max_abs_diff`.
337 ///
338 /// This can be used to compare if two matrices contain similar elements. It works best
339 /// when comparing with a known value. The `max_abs_diff` that should be used used
340 /// depends on the values being compared against.
341 ///
342 /// For more see
343 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
344 #[inline]
345 #[must_use]
346 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
347 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
348 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
349 }
350
351 /// Takes the absolute value of each element in `self`
352 #[inline]
353 #[must_use]
354 pub fn abs(&self) -> Self {
355 Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
356 }
357
358 #[inline]
359 pub fn as_dmat2(&self) -> DMat2 {
360 DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
361 }
362 }
363
364 impl Default for Mat2 {
365 #[inline]
366 fn default() -> Self {
367 Self::IDENTITY
368 }
369 }
370
371 impl Add<Mat2> for Mat2 {
372 type Output = Self;
373 #[inline]
374 fn add(self, rhs: Self) -> Self::Output {
375 self.add_mat2(&rhs)
376 }
377 }
378
379 impl AddAssign<Mat2> for Mat2 {
380 #[inline]
381 fn add_assign(&mut self, rhs: Self) {
382 *self = self.add_mat2(&rhs);
383 }
384 }
385
386 impl Sub<Mat2> for Mat2 {
387 type Output = Self;
388 #[inline]
389 fn sub(self, rhs: Self) -> Self::Output {
390 self.sub_mat2(&rhs)
391 }
392 }
393
394 impl SubAssign<Mat2> for Mat2 {
395 #[inline]
396 fn sub_assign(&mut self, rhs: Self) {
397 *self = self.sub_mat2(&rhs);
398 }
399 }
400
401 impl Neg for Mat2 {
402 type Output = Self;
403 #[inline]
404 fn neg(self) -> Self::Output {
405 Self(unsafe { _mm_xor_ps(self.0, _mm_set1_ps(-0.0)) })
406 }
407 }
408
409 impl Mul<Mat2> for Mat2 {
410 type Output = Self;
411 #[inline]
412 fn mul(self, rhs: Self) -> Self::Output {
413 self.mul_mat2(&rhs)
414 }
415 }
416
417 impl MulAssign<Mat2> for Mat2 {
418 #[inline]
419 fn mul_assign(&mut self, rhs: Self) {
420 *self = self.mul_mat2(&rhs);
421 }
422 }
423
424 impl Mul<Vec2> for Mat2 {
425 type Output = Vec2;
426 #[inline]
427 fn mul(self, rhs: Vec2) -> Self::Output {
428 self.mul_vec2(rhs)
429 }
430 }
431
432 impl Mul<Mat2> for f32 {
433 type Output = Mat2;
434 #[inline]
435 fn mul(self, rhs: Mat2) -> Self::Output {
436 rhs.mul_scalar(self)
437 }
438 }
439
440 impl Mul<f32> for Mat2 {
441 type Output = Self;
442 #[inline]
443 fn mul(self, rhs: f32) -> Self::Output {
444 self.mul_scalar(rhs)
445 }
446 }
447
448 impl MulAssign<f32> for Mat2 {
449 #[inline]
450 fn mul_assign(&mut self, rhs: f32) {
451 *self = self.mul_scalar(rhs);
452 }
453 }
454
455 impl Div<Mat2> for f32 {
456 type Output = Mat2;
457 #[inline]
458 fn div(self, rhs: Mat2) -> Self::Output {
459 rhs.div_scalar(self)
460 }
461 }
462
463 impl Div<f32> for Mat2 {
464 type Output = Self;
465 #[inline]
466 fn div(self, rhs: f32) -> Self::Output {
467 self.div_scalar(rhs)
468 }
469 }
470
471 impl DivAssign<f32> for Mat2 {
472 #[inline]
473 fn div_assign(&mut self, rhs: f32) {
474 *self = self.div_scalar(rhs);
475 }
476 }
477
478 impl Sum<Self> for Mat2 {
479 fn sum<I>(iter: I) -> Self
480 where
481 I: Iterator<Item = Self>,
482 {
483 iter.fold(Self::ZERO, Self::add)
484 }
485 }
486
487 impl<'a> Sum<&'a Self> for Mat2 {
488 fn sum<I>(iter: I) -> Self
489 where
490 I: Iterator<Item = &'a Self>,
491 {
492 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
493 }
494 }
495
496 impl Product for Mat2 {
497 fn product<I>(iter: I) -> Self
498 where
499 I: Iterator<Item = Self>,
500 {
501 iter.fold(Self::IDENTITY, Self::mul)
502 }
503 }
504
505 impl<'a> Product<&'a Self> for Mat2 {
506 fn product<I>(iter: I) -> Self
507 where
508 I: Iterator<Item = &'a Self>,
509 {
510 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
511 }
512 }
513
514 impl PartialEq for Mat2 {
515 #[inline]
516 fn eq(&self, rhs: &Self) -> bool {
517 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
518 }
519 }
520
521 #[cfg(not(target_arch = "spirv"))]
522 impl AsRef<[f32; 4]> for Mat2 {
523 #[inline]
524 fn as_ref(&self) -> &[f32; 4] {
525 unsafe { &*(self as *const Self as *const [f32; 4]) }
526 }
527 }
528
529 #[cfg(not(target_arch = "spirv"))]
530 impl AsMut<[f32; 4]> for Mat2 {
531 #[inline]
532 fn as_mut(&mut self) -> &mut [f32; 4] {
533 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
534 }
535 }
536
537 impl core::ops::Deref for Mat2 {
538 type Target = crate::deref::Cols2<Vec2>;
539 #[inline]
540 fn deref(&self) -> &Self::Target {
541 unsafe { &*(self as *const Self as *const Self::Target) }
542 }
543 }
544
545 impl core::ops::DerefMut for Mat2 {
546 #[inline]
547 fn deref_mut(&mut self) -> &mut Self::Target {
548 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
549 }
550 }
551
552 #[cfg(not(target_arch = "spirv"))]
553 impl fmt::Debug for Mat2 {
554 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
555 fmt.debug_struct(stringify!(Mat2))
556 .field("x_axis", &self.x_axis)
557 .field("y_axis", &self.y_axis)
558 .finish()
559 }
560 }
561
562 #[cfg(not(target_arch = "spirv"))]
563 impl fmt::Display for Mat2 {
564 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
565 if let Some(p) = f.precision() {
566 write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
567 } else {
568 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
569 }
570 }
571 }