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