Debian Codegraph @

debian/missing-sources/glam-0.21.3/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, 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 fn to_cols_array(&self) -> [f32; 4] {
60 [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y]
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 fn to_cols_array_2d(&self) -> [[f32; 2]; 2] {
75 [self.x_axis.to_array(), self.y_axis.to_array()]
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 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 the first 4 values in `slice`.
107 ///
108 /// # Panics
109 ///
110 /// Panics if `slice` is less than 4 elements long.
111 #[inline]
112 pub const fn from_cols_slice(slice: &[f32]) -> Self {
113 Self::new(slice[0], slice[1], slice[2], slice[3])
114 }
115
116 /// Writes the columns of `self` to the first 4 elements in `slice`.
117 ///
118 /// # Panics
119 ///
120 /// Panics if `slice` is less than 4 elements long.
121 #[inline]
122 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
123 slice[0] = self.x_axis.x;
124 slice[1] = self.x_axis.y;
125 slice[2] = self.y_axis.x;
126 slice[3] = self.y_axis.y;
127 }
128
129 /// Returns the matrix column for the given `index`.
130 ///
131 /// # Panics
132 ///
133 /// Panics if `index` is greater than 1.
134 #[inline]
135 pub fn col(&self, index: usize) -> Vec2 {
136 match index {
137 0 => self.x_axis,
138 1 => self.y_axis,
139 _ => panic!("index out of bounds"),
140 }
141 }
142
143 /// Returns a mutable reference to the matrix column for the given `index`.
144 ///
145 /// # Panics
146 ///
147 /// Panics if `index` is greater than 1.
148 #[inline]
149 pub fn col_mut(&mut self, index: usize) -> &mut Vec2 {
150 match index {
151 0 => &mut self.x_axis,
152 1 => &mut self.y_axis,
153 _ => panic!("index out of bounds"),
154 }
155 }
156
157 /// Returns the matrix row for the given `index`.
158 ///
159 /// # Panics
160 ///
161 /// Panics if `index` is greater than 1.
162 #[inline]
163 pub fn row(&self, index: usize) -> Vec2 {
164 match index {
165 0 => Vec2::new(self.x_axis.x, self.y_axis.x),
166 1 => Vec2::new(self.x_axis.y, self.y_axis.y),
167 _ => panic!("index out of bounds"),
168 }
169 }
170
171 /// Returns `true` if, and only if, all elements are finite.
172 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
173 #[inline]
174 pub fn is_finite(&self) -> bool {
175 self.x_axis.is_finite() && self.y_axis.is_finite()
176 }
177
178 /// Returns `true` if any elements are `NaN`.
179 #[inline]
180 pub fn is_nan(&self) -> bool {
181 self.x_axis.is_nan() || self.y_axis.is_nan()
182 }
183
184 /// Returns the transpose of `self`.
185 #[must_use]
186 #[inline]
187 pub fn transpose(&self) -> Self {
188 Self(simd_swizzle!(self.0, [0, 2, 1, 3]))
189 }
190
191 /// Returns the determinant of `self`.
192 #[inline]
193 pub fn determinant(&self) -> f32 {
194 let abcd = self.0;
195 let dcba = simd_swizzle!(abcd, [3, 2, 1, 0]);
196 let prod = abcd * dcba;
197 let det = prod - simd_swizzle!(prod, [1, 1, 1, 1]);
198 det[0]
199 }
200
201 /// Returns the inverse of `self`.
202 ///
203 /// If the matrix is not invertible the returned matrix will be invalid.
204 ///
205 /// # Panics
206 ///
207 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
208 #[must_use]
209 #[inline]
210 pub fn inverse(&self) -> Self {
211 const SIGN: f32x4 = f32x4::from_array([1.0, -1.0, -1.0, 1.0]);
212 let abcd = self.0;
213 let dcba = simd_swizzle!(abcd, [3, 2, 1, 0]);
214 let prod = abcd * dcba;
215 let sub = prod - simd_swizzle!(prod, [1, 1, 1, 1]);
216 let det = simd_swizzle!(sub, [0, 0, 0, 0]);
217 let tmp = SIGN / det;
218 glam_assert!(tmp.is_finite());
219 let dbca = simd_swizzle!(abcd, [3, 1, 2, 0]);
220 Self(dbca.mul(tmp))
221 }
222
223 /// Transforms a 2D vector.
224 #[inline]
225 pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
226 let abcd = self.0;
227 let xxyy = f32x4::from_array([rhs.x, rhs.x, rhs.y, rhs.y]);
228 let axbxcydy = abcd.mul(xxyy);
229 let cydyaxbx = simd_swizzle!(axbxcydy, [2, 3, 0, 1]);
230 let result = axbxcydy.add(cydyaxbx);
231 unsafe { *(&result as *const f32x4 as *const Vec2) }
232 }
233
234 /// Multiplies two 2x2 matrices.
235 #[inline]
236 pub fn mul_mat2(&self, rhs: &Self) -> Self {
237 let abcd = self.0;
238 let xxyy0 = simd_swizzle!(rhs.0, [0, 0, 1, 1]);
239 let xxyy1 = simd_swizzle!(rhs.0, [2, 2, 3, 3]);
240 let axbxcydy0 = abcd * xxyy0;
241 let axbxcydy1 = abcd * xxyy1;
242 let cydyaxbx0 = simd_swizzle!(axbxcydy0, [2, 3, 0, 1]);
243 let cydyaxbx1 = simd_swizzle!(axbxcydy1, [2, 3, 0, 1]);
244 let result0 = axbxcydy0 + cydyaxbx0;
245 let result1 = axbxcydy1 + cydyaxbx1;
246 Self(simd_swizzle!(
247 result0,
248 result1,
249 [First(0), First(1), Second(0), Second(1)]
250 ))
251 }
252
253 /// Adds two 2x2 matrices.
254 #[inline]
255 pub fn add_mat2(&self, rhs: &Self) -> Self {
256 Self(self.0 + rhs.0)
257 }
258
259 /// Subtracts two 2x2 matrices.
260 #[inline]
261 pub fn sub_mat2(&self, rhs: &Self) -> Self {
262 Self(self.0 - rhs.0)
263 }
264
265 /// Multiplies a 2x2 matrix by a scalar.
266 #[inline]
267 pub fn mul_scalar(&self, rhs: f32) -> Self {
268 Self(self.0 * f32x4::splat(rhs))
269 }
270
271 /// Returns true if the absolute difference of all elements between `self` and `rhs`
272 /// is less than or equal to `max_abs_diff`.
273 ///
274 /// This can be used to compare if two matrices contain similar elements. It works best
275 /// when comparing with a known value. The `max_abs_diff` that should be used used
276 /// depends on the values being compared against.
277 ///
278 /// For more see
279 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
280 #[inline]
281 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
282 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
283 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
284 }
285
286 #[inline]
287 pub fn as_dmat2(&self) -> DMat2 {
288 DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
289 }
290 }
291
292 impl Default for Mat2 {
293 #[inline]
294 fn default() -> Self {
295 Self::IDENTITY
296 }
297 }
298
299 impl Add<Mat2> for Mat2 {
300 type Output = Self;
301 #[inline]
302 fn add(self, rhs: Self) -> Self::Output {
303 self.add_mat2(&rhs)
304 }
305 }
306
307 impl AddAssign<Mat2> for Mat2 {
308 #[inline]
309 fn add_assign(&mut self, rhs: Self) {
310 *self = self.add_mat2(&rhs);
311 }
312 }
313
314 impl Sub<Mat2> for Mat2 {
315 type Output = Self;
316 #[inline]
317 fn sub(self, rhs: Self) -> Self::Output {
318 self.sub_mat2(&rhs)
319 }
320 }
321
322 impl SubAssign<Mat2> for Mat2 {
323 #[inline]
324 fn sub_assign(&mut self, rhs: Self) {
325 *self = self.sub_mat2(&rhs);
326 }
327 }
328
329 impl Neg for Mat2 {
330 type Output = Self;
331 #[inline]
332 fn neg(self) -> Self::Output {
333 Self(-self.0)
334 }
335 }
336
337 impl Mul<Mat2> for Mat2 {
338 type Output = Self;
339 #[inline]
340 fn mul(self, rhs: Self) -> Self::Output {
341 self.mul_mat2(&rhs)
342 }
343 }
344
345 impl MulAssign<Mat2> for Mat2 {
346 #[inline]
347 fn mul_assign(&mut self, rhs: Self) {
348 *self = self.mul_mat2(&rhs);
349 }
350 }
351
352 impl Mul<Vec2> for Mat2 {
353 type Output = Vec2;
354 #[inline]
355 fn mul(self, rhs: Vec2) -> Self::Output {
356 self.mul_vec2(rhs)
357 }
358 }
359
360 impl Mul<Mat2> for f32 {
361 type Output = Mat2;
362 #[inline]
363 fn mul(self, rhs: Mat2) -> Self::Output {
364 rhs.mul_scalar(self)
365 }
366 }
367
368 impl Mul<f32> for Mat2 {
369 type Output = Self;
370 #[inline]
371 fn mul(self, rhs: f32) -> Self::Output {
372 self.mul_scalar(rhs)
373 }
374 }
375
376 impl MulAssign<f32> for Mat2 {
377 #[inline]
378 fn mul_assign(&mut self, rhs: f32) {
379 *self = self.mul_scalar(rhs);
380 }
381 }
382
383 impl<'a> Sum<&'a Self> for Mat2 {
384 fn sum<I>(iter: I) -> Self
385 where
386 I: Iterator<Item = &'a Self>,
387 {
388 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
389 }
390 }
391
392 impl<'a> Product<&'a Self> for Mat2 {
393 fn product<I>(iter: I) -> Self
394 where
395 I: Iterator<Item = &'a Self>,
396 {
397 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
398 }
399 }
400
401 impl PartialEq for Mat2 {
402 #[inline]
403 fn eq(&self, rhs: &Self) -> bool {
404 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
405 }
406 }
407
408 #[cfg(not(target_arch = "spirv"))]
409 impl AsRef<[f32; 4]> for Mat2 {
410 #[inline]
411 fn as_ref(&self) -> &[f32; 4] {
412 unsafe { &*(self as *const Self as *const [f32; 4]) }
413 }
414 }
415
416 #[cfg(not(target_arch = "spirv"))]
417 impl AsMut<[f32; 4]> for Mat2 {
418 #[inline]
419 fn as_mut(&mut self) -> &mut [f32; 4] {
420 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
421 }
422 }
423
424 impl core::ops::Deref for Mat2 {
425 type Target = crate::deref::Cols2<Vec2>;
426 #[inline]
427 fn deref(&self) -> &Self::Target {
428 unsafe { &*(self as *const Self as *const Self::Target) }
429 }
430 }
431
432 impl core::ops::DerefMut for Mat2 {
433 #[inline]
434 fn deref_mut(&mut self) -> &mut Self::Target {
435 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
436 }
437 }
438
439 #[cfg(not(target_arch = "spirv"))]
440 impl fmt::Debug for Mat2 {
441 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
442 fmt.debug_struct(stringify!(Mat2))
443 .field("x_axis", &self.x_axis)
444 .field("y_axis", &self.y_axis)
445 .finish()
446 }
447 }
448
449 #[cfg(not(target_arch = "spirv"))]
450 impl fmt::Display for Mat2 {
451 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
452 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
453 }
454 }