Debian Codegraph @

debian/missing-sources/glam-0.22.0/src/f32/wasm32/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::arch::wasm32::*;
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) v128);
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(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(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::new(m[0], m[1], m[2], m[3])
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(i32x4_shuffle::<0, 2, 5, 7>(self.0, self.0))
195 }
196
197 /// Returns the determinant of `self`.
198 #[inline]
199 pub fn determinant(&self) -> f32 {
200 let abcd = self.0;
201 let dcba = i32x4_shuffle::<3, 2, 5, 4>(abcd, abcd);
202 let prod = f32x4_mul(abcd, dcba);
203 let det = f32x4_sub(prod, i32x4_shuffle::<1, 1, 5, 5>(prod, prod));
204 f32x4_extract_lane::<0>(det)
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: v128 = crate::wasm32::v128_from_f32x4([1.0, -1.0, -1.0, 1.0]);
218 let abcd = self.0;
219 let dcba = i32x4_shuffle::<3, 2, 5, 4>(abcd, abcd);
220 let prod = f32x4_mul(abcd, dcba);
221 let sub = f32x4_sub(prod, i32x4_shuffle::<1, 1, 5, 5>(prod, prod));
222 let det = i32x4_shuffle::<0, 0, 4, 4>(sub, sub);
223 let tmp = f32x4_div(SIGN, det);
224 glam_assert!(Mat2(tmp).is_finite());
225 let dbca = i32x4_shuffle::<3, 1, 6, 4>(abcd, abcd);
226 Self(f32x4_mul(dbca, tmp))
227 }
228
229 /// Transforms a 2D vector.
230 #[inline]
231 pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
232 use core::mem::MaybeUninit;
233 let abcd = self.0;
234 let xxyy = f32x4(rhs.x, rhs.x, rhs.y, rhs.y);
235 let axbxcydy = f32x4_mul(abcd, xxyy);
236 let cydyaxbx = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy, axbxcydy);
237 let result = f32x4_add(axbxcydy, cydyaxbx);
238 let mut out: MaybeUninit<v128> = MaybeUninit::uninit();
239 unsafe {
240 v128_store(out.as_mut_ptr(), result);
241 *(&out.assume_init() as *const v128 as *const Vec2)
242 }
243 }
244
245 /// Multiplies two 2x2 matrices.
246 #[inline]
247 pub fn mul_mat2(&self, rhs: &Self) -> Self {
248 let abcd = self.0;
249 let rhs = rhs.0;
250 let xxyy0 = i32x4_shuffle::<0, 0, 5, 5>(rhs, rhs);
251 let xxyy1 = i32x4_shuffle::<2, 2, 7, 7>(rhs, rhs);
252 let axbxcydy0 = f32x4_mul(abcd, xxyy0);
253 let axbxcydy1 = f32x4_mul(abcd, xxyy1);
254 let cydyaxbx0 = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy0, axbxcydy0);
255 let cydyaxbx1 = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy1, axbxcydy1);
256 let result0 = f32x4_add(axbxcydy0, cydyaxbx0);
257 let result1 = f32x4_add(axbxcydy1, cydyaxbx1);
258 Self(i32x4_shuffle::<0, 1, 4, 5>(result0, result1))
259 }
260
261 /// Adds two 2x2 matrices.
262 #[inline]
263 pub fn add_mat2(&self, rhs: &Self) -> Self {
264 Self(f32x4_add(self.0, rhs.0))
265 }
266
267 /// Subtracts two 2x2 matrices.
268 #[inline]
269 pub fn sub_mat2(&self, rhs: &Self) -> Self {
270 Self(f32x4_sub(self.0, rhs.0))
271 }
272
273 /// Multiplies a 2x2 matrix by a scalar.
274 #[inline]
275 pub fn mul_scalar(&self, rhs: f32) -> Self {
276 Self(f32x4_mul(self.0, f32x4_splat(rhs)))
277 }
278
279 /// Returns true if the absolute difference of all elements between `self` and `rhs`
280 /// is less than or equal to `max_abs_diff`.
281 ///
282 /// This can be used to compare if two matrices contain similar elements. It works best
283 /// when comparing with a known value. The `max_abs_diff` that should be used used
284 /// depends on the values being compared against.
285 ///
286 /// For more see
287 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
288 #[inline]
289 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
290 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
291 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
292 }
293
294 #[inline]
295 pub fn as_dmat2(&self) -> DMat2 {
296 DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
297 }
298 }
299
300 impl Default for Mat2 {
301 #[inline]
302 fn default() -> Self {
303 Self::IDENTITY
304 }
305 }
306
307 impl Add<Mat2> for Mat2 {
308 type Output = Self;
309 #[inline]
310 fn add(self, rhs: Self) -> Self::Output {
311 self.add_mat2(&rhs)
312 }
313 }
314
315 impl AddAssign<Mat2> for Mat2 {
316 #[inline]
317 fn add_assign(&mut self, rhs: Self) {
318 *self = self.add_mat2(&rhs);
319 }
320 }
321
322 impl Sub<Mat2> for Mat2 {
323 type Output = Self;
324 #[inline]
325 fn sub(self, rhs: Self) -> Self::Output {
326 self.sub_mat2(&rhs)
327 }
328 }
329
330 impl SubAssign<Mat2> for Mat2 {
331 #[inline]
332 fn sub_assign(&mut self, rhs: Self) {
333 *self = self.sub_mat2(&rhs);
334 }
335 }
336
337 impl Neg for Mat2 {
338 type Output = Self;
339 #[inline]
340 fn neg(self) -> Self::Output {
341 Self(f32x4_neg(self.0))
342 }
343 }
344
345 impl Mul<Mat2> for Mat2 {
346 type Output = Self;
347 #[inline]
348 fn mul(self, rhs: Self) -> Self::Output {
349 self.mul_mat2(&rhs)
350 }
351 }
352
353 impl MulAssign<Mat2> for Mat2 {
354 #[inline]
355 fn mul_assign(&mut self, rhs: Self) {
356 *self = self.mul_mat2(&rhs);
357 }
358 }
359
360 impl Mul<Vec2> for Mat2 {
361 type Output = Vec2;
362 #[inline]
363 fn mul(self, rhs: Vec2) -> Self::Output {
364 self.mul_vec2(rhs)
365 }
366 }
367
368 impl Mul<Mat2> for f32 {
369 type Output = Mat2;
370 #[inline]
371 fn mul(self, rhs: Mat2) -> Self::Output {
372 rhs.mul_scalar(self)
373 }
374 }
375
376 impl Mul<f32> for Mat2 {
377 type Output = Self;
378 #[inline]
379 fn mul(self, rhs: f32) -> Self::Output {
380 self.mul_scalar(rhs)
381 }
382 }
383
384 impl MulAssign<f32> for Mat2 {
385 #[inline]
386 fn mul_assign(&mut self, rhs: f32) {
387 *self = self.mul_scalar(rhs);
388 }
389 }
390
391 impl Sum<Self> for Mat2 {
392 fn sum<I>(iter: I) -> Self
393 where
394 I: Iterator<Item = Self>,
395 {
396 iter.fold(Self::ZERO, Self::add)
397 }
398 }
399
400 impl<'a> Sum<&'a Self> for Mat2 {
401 fn sum<I>(iter: I) -> Self
402 where
403 I: Iterator<Item = &'a Self>,
404 {
405 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
406 }
407 }
408
409 impl Product for Mat2 {
410 fn product<I>(iter: I) -> Self
411 where
412 I: Iterator<Item = Self>,
413 {
414 iter.fold(Self::IDENTITY, Self::mul)
415 }
416 }
417
418 impl<'a> Product<&'a Self> for Mat2 {
419 fn product<I>(iter: I) -> Self
420 where
421 I: Iterator<Item = &'a Self>,
422 {
423 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
424 }
425 }
426
427 impl PartialEq for Mat2 {
428 #[inline]
429 fn eq(&self, rhs: &Self) -> bool {
430 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
431 }
432 }
433
434 #[cfg(not(target_arch = "spirv"))]
435 impl AsRef<[f32; 4]> for Mat2 {
436 #[inline]
437 fn as_ref(&self) -> &[f32; 4] {
438 unsafe { &*(self as *const Self as *const [f32; 4]) }
439 }
440 }
441
442 #[cfg(not(target_arch = "spirv"))]
443 impl AsMut<[f32; 4]> for Mat2 {
444 #[inline]
445 fn as_mut(&mut self) -> &mut [f32; 4] {
446 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
447 }
448 }
449
450 impl core::ops::Deref for Mat2 {
451 type Target = crate::deref::Cols2<Vec2>;
452 #[inline]
453 fn deref(&self) -> &Self::Target {
454 unsafe { &*(self as *const Self as *const Self::Target) }
455 }
456 }
457
458 impl core::ops::DerefMut for Mat2 {
459 #[inline]
460 fn deref_mut(&mut self) -> &mut Self::Target {
461 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
462 }
463 }
464
465 #[cfg(not(target_arch = "spirv"))]
466 impl fmt::Debug for Mat2 {
467 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
468 fmt.debug_struct(stringify!(Mat2))
469 .field("x_axis", &self.x_axis)
470 .field("y_axis", &self.y_axis)
471 .finish()
472 }
473 }
474
475 #[cfg(not(target_arch = "spirv"))]
476 impl fmt::Display for Mat2 {
477 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
478 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
479 }
480 }