Debian Codegraph @

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

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