| 1 | #[cfg(target_arch = "x86")] |
| 2 | use core::arch::x86::*; |
| 3 | #[cfg(target_arch = "x86_64")] |
| 4 | use core::arch::x86_64::*; |
| 5 | |
| 6 | use core::mem::MaybeUninit; |
| 7 | |
| 8 | use crate::{ |
| 9 | const_m128, |
| 10 | core::{ |
| 11 | storage::{Align16, Columns2, Columns3, Columns4, XY, XYZ}, |
| 12 | traits::{ |
| 13 | matrix::{ |
| 14 | FloatMatrix2x2, FloatMatrix3x3, FloatMatrix4x4, Matrix, Matrix2x2, Matrix3x3, |
| 15 | Matrix4x4, MatrixConst, |
| 16 | }, |
| 17 | projection::ProjectionMatrix, |
| 18 | vector::{FloatVector4, Vector, Vector4, Vector4Const}, |
| 19 | }, |
| 20 | }, |
| 21 | }; |
| 22 | |
| 23 | // __m128 as a Matrix2x2 |
| 24 | impl MatrixConst for __m128 { |
| 25 | const ZERO: __m128 = const_m128!([0.0, 0.0, 0.0, 0.0]); |
| 26 | const IDENTITY: __m128 = const_m128!([1.0, 0.0, 0.0, 1.0]); |
| 27 | } |
| 28 | |
| 29 | impl Matrix<f32> for __m128 {} |
| 30 | |
| 31 | impl Matrix2x2<f32, XY<f32>> for __m128 { |
| 32 | #[inline(always)] |
| 33 | fn new(m00: f32, m01: f32, m10: f32, m11: f32) -> Self { |
| 34 | unsafe { _mm_set_ps(m11, m10, m01, m00) } |
| 35 | } |
| 36 | |
| 37 | #[inline(always)] |
| 38 | fn from_cols(x_axis: XY<f32>, y_axis: XY<f32>) -> Self { |
| 39 | Matrix2x2::new(x_axis.x, x_axis.y, y_axis.x, y_axis.y) |
| 40 | } |
| 41 | |
| 42 | #[inline(always)] |
| 43 | fn x_axis(&self) -> &XY<f32> { |
| 44 | unsafe { &(*(self as *const Self as *const Columns2<XY<f32>>)).x_axis } |
| 45 | } |
| 46 | |
| 47 | #[inline(always)] |
| 48 | fn y_axis(&self) -> &XY<f32> { |
| 49 | unsafe { &(*(self as *const Self as *const Columns2<XY<f32>>)).y_axis } |
| 50 | } |
| 51 | |
| 52 | #[inline] |
| 53 | fn determinant(&self) -> f32 { |
| 54 | // self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x |
| 55 | unsafe { |
| 56 | let abcd = *self; |
| 57 | let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11); |
| 58 | let prod = _mm_mul_ps(abcd, dcba); |
| 59 | let det = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01)); |
| 60 | _mm_cvtss_f32(det) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[inline(always)] |
| 65 | fn transpose(&self) -> Self { |
| 66 | unsafe { _mm_shuffle_ps(*self, *self, 0b11_01_10_00) } |
| 67 | } |
| 68 | |
| 69 | #[inline] |
| 70 | fn mul_vector(&self, other: XY<f32>) -> XY<f32> { |
| 71 | unsafe { |
| 72 | let abcd = *self; |
| 73 | let xxyy = _mm_set_ps(other.y, other.y, other.x, other.x); |
| 74 | let axbxcydy = _mm_mul_ps(abcd, xxyy); |
| 75 | let cydyaxbx = _mm_shuffle_ps(axbxcydy, axbxcydy, 0b01_00_11_10); |
| 76 | let result = _mm_add_ps(axbxcydy, cydyaxbx); |
| 77 | let mut out: MaybeUninit<Align16<XY<f32>>> = MaybeUninit::uninit(); |
| 78 | _mm_store_ps(out.as_mut_ptr() as *mut f32, result); |
| 79 | out.assume_init().0 |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #[inline] |
| 84 | fn mul_matrix(&self, other: &Self) -> Self { |
| 85 | unsafe { |
| 86 | let abcd = *self; |
| 87 | let other = *other; |
| 88 | let xxyy0 = _mm_shuffle_ps(other, other, 0b01_01_00_00); |
| 89 | let xxyy1 = _mm_shuffle_ps(other, other, 0b11_11_10_10); |
| 90 | let axbxcydy0 = _mm_mul_ps(abcd, xxyy0); |
| 91 | let axbxcydy1 = _mm_mul_ps(abcd, xxyy1); |
| 92 | let cydyaxbx0 = _mm_shuffle_ps(axbxcydy0, axbxcydy0, 0b01_00_11_10); |
| 93 | let cydyaxbx1 = _mm_shuffle_ps(axbxcydy1, axbxcydy1, 0b01_00_11_10); |
| 94 | let result0 = _mm_add_ps(axbxcydy0, cydyaxbx0); |
| 95 | let result1 = _mm_add_ps(axbxcydy1, cydyaxbx1); |
| 96 | _mm_shuffle_ps(result0, result1, 0b01_00_01_00) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[inline] |
| 101 | fn mul_scalar(&self, other: f32) -> Self { |
| 102 | unsafe { _mm_mul_ps(*self, _mm_set_ps1(other)) } |
| 103 | } |
| 104 | |
| 105 | #[inline] |
| 106 | fn add_matrix(&self, other: &Self) -> Self { |
| 107 | unsafe { _mm_add_ps(*self, *other) } |
| 108 | } |
| 109 | |
| 110 | #[inline] |
| 111 | fn sub_matrix(&self, other: &Self) -> Self { |
| 112 | unsafe { _mm_sub_ps(*self, *other) } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl FloatMatrix2x2<f32, XY<f32>> for __m128 { |
| 117 | #[inline] |
| 118 | fn abs_diff_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 119 | FloatVector4::abs_diff_eq(*self, *other, max_abs_diff) |
| 120 | } |
| 121 | |
| 122 | #[inline] |
| 123 | fn inverse(&self) -> Self { |
| 124 | unsafe { |
| 125 | const SIGN: __m128 = const_m128!([1.0, -1.0, -1.0, 1.0]); |
| 126 | let abcd = *self; |
| 127 | let dcba = _mm_shuffle_ps(abcd, abcd, 0b00_01_10_11); |
| 128 | let prod = _mm_mul_ps(abcd, dcba); |
| 129 | let sub = _mm_sub_ps(prod, _mm_shuffle_ps(prod, prod, 0b01_01_01_01)); |
| 130 | let det = _mm_shuffle_ps(sub, sub, 0b00_00_00_00); |
| 131 | let tmp = _mm_div_ps(SIGN, det); |
| 132 | glam_assert!(tmp.is_finite()); |
| 133 | let dbca = _mm_shuffle_ps(abcd, abcd, 0b00_10_01_11); |
| 134 | _mm_mul_ps(dbca, tmp) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | impl MatrixConst for Columns3<__m128> { |
| 140 | const ZERO: Columns3<__m128> = Columns3 { |
| 141 | x_axis: __m128::ZERO, |
| 142 | y_axis: __m128::ZERO, |
| 143 | z_axis: __m128::ZERO, |
| 144 | }; |
| 145 | const IDENTITY: Columns3<__m128> = Columns3 { |
| 146 | x_axis: __m128::X, |
| 147 | y_axis: __m128::Y, |
| 148 | z_axis: __m128::Z, |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | impl Matrix<f32> for Columns3<__m128> {} |
| 153 | |
| 154 | impl Matrix3x3<f32, __m128> for Columns3<__m128> { |
| 155 | #[inline(always)] |
| 156 | fn from_cols(x_axis: __m128, y_axis: __m128, z_axis: __m128) -> Self { |
| 157 | Self { |
| 158 | x_axis, |
| 159 | y_axis, |
| 160 | z_axis, |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #[inline(always)] |
| 165 | fn x_axis(&self) -> &__m128 { |
| 166 | &self.x_axis |
| 167 | } |
| 168 | |
| 169 | #[inline(always)] |
| 170 | fn y_axis(&self) -> &__m128 { |
| 171 | &self.y_axis |
| 172 | } |
| 173 | |
| 174 | #[inline(always)] |
| 175 | fn z_axis(&self) -> &__m128 { |
| 176 | &self.z_axis |
| 177 | } |
| 178 | |
| 179 | #[inline] |
| 180 | fn transpose(&self) -> Self { |
| 181 | unsafe { |
| 182 | let tmp0 = _mm_shuffle_ps(self.x_axis, self.y_axis, 0b01_00_01_00); |
| 183 | let tmp1 = _mm_shuffle_ps(self.x_axis, self.y_axis, 0b11_10_11_10); |
| 184 | |
| 185 | Self { |
| 186 | x_axis: _mm_shuffle_ps(tmp0, self.z_axis, 0b00_00_10_00), |
| 187 | y_axis: _mm_shuffle_ps(tmp0, self.z_axis, 0b01_01_11_01), |
| 188 | z_axis: _mm_shuffle_ps(tmp1, self.z_axis, 0b10_10_10_00), |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | impl FloatMatrix3x3<f32, __m128> for Columns3<__m128> { |
| 195 | #[inline] |
| 196 | fn transform_point2(&self, other: XY<f32>) -> XY<f32> { |
| 197 | let mut res = self.x_axis.mul_scalar(other.x); |
| 198 | res = self.y_axis.mul_scalar(other.y).add(res); |
| 199 | res = self.z_axis.add(res); |
| 200 | res.into() |
| 201 | } |
| 202 | |
| 203 | #[inline] |
| 204 | fn transform_vector2(&self, other: XY<f32>) -> XY<f32> { |
| 205 | let mut res = self.x_axis.mul_scalar(other.x); |
| 206 | res = self.y_axis.mul_scalar(other.y).add(res); |
| 207 | res.into() |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | impl MatrixConst for Columns4<__m128> { |
| 212 | const ZERO: Columns4<__m128> = Columns4 { |
| 213 | x_axis: __m128::ZERO, |
| 214 | y_axis: __m128::ZERO, |
| 215 | z_axis: __m128::ZERO, |
| 216 | w_axis: __m128::ZERO, |
| 217 | }; |
| 218 | const IDENTITY: Columns4<__m128> = Columns4 { |
| 219 | x_axis: __m128::X, |
| 220 | y_axis: __m128::Y, |
| 221 | z_axis: __m128::Z, |
| 222 | w_axis: __m128::W, |
| 223 | }; |
| 224 | } |
| 225 | |
| 226 | impl Matrix<f32> for Columns4<__m128> {} |
| 227 | |
| 228 | impl Matrix4x4<f32, __m128> for Columns4<__m128> { |
| 229 | #[inline(always)] |
| 230 | fn from_cols(x_axis: __m128, y_axis: __m128, z_axis: __m128, w_axis: __m128) -> Self { |
| 231 | Self { |
| 232 | x_axis, |
| 233 | y_axis, |
| 234 | z_axis, |
| 235 | w_axis, |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | #[inline(always)] |
| 240 | fn x_axis(&self) -> &__m128 { |
| 241 | &self.x_axis |
| 242 | } |
| 243 | |
| 244 | #[inline(always)] |
| 245 | fn y_axis(&self) -> &__m128 { |
| 246 | &self.y_axis |
| 247 | } |
| 248 | |
| 249 | #[inline(always)] |
| 250 | fn z_axis(&self) -> &__m128 { |
| 251 | &self.z_axis |
| 252 | } |
| 253 | |
| 254 | #[inline(always)] |
| 255 | fn w_axis(&self) -> &__m128 { |
| 256 | &self.w_axis |
| 257 | } |
| 258 | |
| 259 | #[inline] |
| 260 | fn determinant(&self) -> f32 { |
| 261 | unsafe { |
| 262 | // Based on https://github.com/g-truc/glm `glm_mat4_determinant` |
| 263 | let swp2a = _mm_shuffle_ps(self.z_axis, self.z_axis, 0b00_01_01_10); |
| 264 | let swp3a = _mm_shuffle_ps(self.w_axis, self.w_axis, 0b11_10_11_11); |
| 265 | let swp2b = _mm_shuffle_ps(self.z_axis, self.z_axis, 0b11_10_11_11); |
| 266 | let swp3b = _mm_shuffle_ps(self.w_axis, self.w_axis, 0b00_10_01_10); |
| 267 | let swp2c = _mm_shuffle_ps(self.z_axis, self.z_axis, 0b00_00_01_10); |
| 268 | let swp3c = _mm_shuffle_ps(self.w_axis, self.w_axis, 0b01_10_00_00); |
| 269 | |
| 270 | let mula = _mm_mul_ps(swp2a, swp3a); |
| 271 | let mulb = _mm_mul_ps(swp2b, swp3b); |
| 272 | let mulc = _mm_mul_ps(swp2c, swp3c); |
| 273 | let sube = _mm_sub_ps(mula, mulb); |
| 274 | let subf = _mm_sub_ps(_mm_movehl_ps(mulc, mulc), mulc); |
| 275 | |
| 276 | let subfaca = _mm_shuffle_ps(sube, sube, 0b10_01_00_00); |
| 277 | let swpfaca = _mm_shuffle_ps(self.y_axis, self.y_axis, 0b00_00_00_01); |
| 278 | let mulfaca = _mm_mul_ps(swpfaca, subfaca); |
| 279 | |
| 280 | let subtmpb = _mm_shuffle_ps(sube, subf, 0b00_00_11_01); |
| 281 | let subfacb = _mm_shuffle_ps(subtmpb, subtmpb, 0b11_01_01_00); |
| 282 | let swpfacb = _mm_shuffle_ps(self.y_axis, self.y_axis, 0b01_01_10_10); |
| 283 | let mulfacb = _mm_mul_ps(swpfacb, subfacb); |
| 284 | |
| 285 | let subres = _mm_sub_ps(mulfaca, mulfacb); |
| 286 | let subtmpc = _mm_shuffle_ps(sube, subf, 0b01_00_10_10); |
| 287 | let subfacc = _mm_shuffle_ps(subtmpc, subtmpc, 0b11_11_10_00); |
| 288 | let swpfacc = _mm_shuffle_ps(self.y_axis, self.y_axis, 0b10_11_11_11); |
| 289 | let mulfacc = _mm_mul_ps(swpfacc, subfacc); |
| 290 | |
| 291 | let addres = _mm_add_ps(subres, mulfacc); |
| 292 | let detcof = _mm_mul_ps(addres, _mm_setr_ps(1.0, -1.0, 1.0, -1.0)); |
| 293 | |
| 294 | Vector4::dot(self.x_axis, detcof) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | #[inline] |
| 299 | fn transpose(&self) -> Self { |
| 300 | unsafe { |
| 301 | // Based on https://github.com/microsoft/DirectXMath `XMMatrixTranspose` |
| 302 | let tmp0 = _mm_shuffle_ps(self.x_axis, self.y_axis, 0b01_00_01_00); |
| 303 | let tmp1 = _mm_shuffle_ps(self.x_axis, self.y_axis, 0b11_10_11_10); |
| 304 | let tmp2 = _mm_shuffle_ps(self.z_axis, self.w_axis, 0b01_00_01_00); |
| 305 | let tmp3 = _mm_shuffle_ps(self.z_axis, self.w_axis, 0b11_10_11_10); |
| 306 | |
| 307 | Self { |
| 308 | x_axis: _mm_shuffle_ps(tmp0, tmp2, 0b10_00_10_00), |
| 309 | y_axis: _mm_shuffle_ps(tmp0, tmp2, 0b11_01_11_01), |
| 310 | z_axis: _mm_shuffle_ps(tmp1, tmp3, 0b10_00_10_00), |
| 311 | w_axis: _mm_shuffle_ps(tmp1, tmp3, 0b11_01_11_01), |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | impl FloatMatrix4x4<f32, __m128> for Columns4<__m128> { |
| 318 | type SIMDVector3 = __m128; |
| 319 | |
| 320 | fn inverse(&self) -> Self { |
| 321 | unsafe { |
| 322 | // Based on https://github.com/g-truc/glm `glm_mat4_inverse` |
| 323 | let fac0 = { |
| 324 | let swp0a = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b11_11_11_11); |
| 325 | let swp0b = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b10_10_10_10); |
| 326 | |
| 327 | let swp00 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b10_10_10_10); |
| 328 | let swp01 = _mm_shuffle_ps(swp0a, swp0a, 0b10_00_00_00); |
| 329 | let swp02 = _mm_shuffle_ps(swp0b, swp0b, 0b10_00_00_00); |
| 330 | let swp03 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b11_11_11_11); |
| 331 | |
| 332 | let mul00 = _mm_mul_ps(swp00, swp01); |
| 333 | let mul01 = _mm_mul_ps(swp02, swp03); |
| 334 | _mm_sub_ps(mul00, mul01) |
| 335 | }; |
| 336 | let fac1 = { |
| 337 | let swp0a = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b11_11_11_11); |
| 338 | let swp0b = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b01_01_01_01); |
| 339 | |
| 340 | let swp00 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b01_01_01_01); |
| 341 | let swp01 = _mm_shuffle_ps(swp0a, swp0a, 0b10_00_00_00); |
| 342 | let swp02 = _mm_shuffle_ps(swp0b, swp0b, 0b10_00_00_00); |
| 343 | let swp03 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b11_11_11_11); |
| 344 | |
| 345 | let mul00 = _mm_mul_ps(swp00, swp01); |
| 346 | let mul01 = _mm_mul_ps(swp02, swp03); |
| 347 | _mm_sub_ps(mul00, mul01) |
| 348 | }; |
| 349 | let fac2 = { |
| 350 | let swp0a = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b10_10_10_10); |
| 351 | let swp0b = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b01_01_01_01); |
| 352 | |
| 353 | let swp00 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b01_01_01_01); |
| 354 | let swp01 = _mm_shuffle_ps(swp0a, swp0a, 0b10_00_00_00); |
| 355 | let swp02 = _mm_shuffle_ps(swp0b, swp0b, 0b10_00_00_00); |
| 356 | let swp03 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b10_10_10_10); |
| 357 | |
| 358 | let mul00 = _mm_mul_ps(swp00, swp01); |
| 359 | let mul01 = _mm_mul_ps(swp02, swp03); |
| 360 | _mm_sub_ps(mul00, mul01) |
| 361 | }; |
| 362 | let fac3 = { |
| 363 | let swp0a = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b11_11_11_11); |
| 364 | let swp0b = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b00_00_00_00); |
| 365 | |
| 366 | let swp00 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b00_00_00_00); |
| 367 | let swp01 = _mm_shuffle_ps(swp0a, swp0a, 0b10_00_00_00); |
| 368 | let swp02 = _mm_shuffle_ps(swp0b, swp0b, 0b10_00_00_00); |
| 369 | let swp03 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b11_11_11_11); |
| 370 | |
| 371 | let mul00 = _mm_mul_ps(swp00, swp01); |
| 372 | let mul01 = _mm_mul_ps(swp02, swp03); |
| 373 | _mm_sub_ps(mul00, mul01) |
| 374 | }; |
| 375 | let fac4 = { |
| 376 | let swp0a = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b10_10_10_10); |
| 377 | let swp0b = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b00_00_00_00); |
| 378 | |
| 379 | let swp00 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b00_00_00_00); |
| 380 | let swp01 = _mm_shuffle_ps(swp0a, swp0a, 0b10_00_00_00); |
| 381 | let swp02 = _mm_shuffle_ps(swp0b, swp0b, 0b10_00_00_00); |
| 382 | let swp03 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b10_10_10_10); |
| 383 | |
| 384 | let mul00 = _mm_mul_ps(swp00, swp01); |
| 385 | let mul01 = _mm_mul_ps(swp02, swp03); |
| 386 | _mm_sub_ps(mul00, mul01) |
| 387 | }; |
| 388 | let fac5 = { |
| 389 | let swp0a = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b01_01_01_01); |
| 390 | let swp0b = _mm_shuffle_ps(self.w_axis, self.z_axis, 0b00_00_00_00); |
| 391 | |
| 392 | let swp00 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b00_00_00_00); |
| 393 | let swp01 = _mm_shuffle_ps(swp0a, swp0a, 0b10_00_00_00); |
| 394 | let swp02 = _mm_shuffle_ps(swp0b, swp0b, 0b10_00_00_00); |
| 395 | let swp03 = _mm_shuffle_ps(self.z_axis, self.y_axis, 0b01_01_01_01); |
| 396 | |
| 397 | let mul00 = _mm_mul_ps(swp00, swp01); |
| 398 | let mul01 = _mm_mul_ps(swp02, swp03); |
| 399 | _mm_sub_ps(mul00, mul01) |
| 400 | }; |
| 401 | let sign_a = _mm_set_ps(1.0, -1.0, 1.0, -1.0); |
| 402 | let sign_b = _mm_set_ps(-1.0, 1.0, -1.0, 1.0); |
| 403 | |
| 404 | let temp0 = _mm_shuffle_ps(self.y_axis, self.x_axis, 0b00_00_00_00); |
| 405 | let vec0 = _mm_shuffle_ps(temp0, temp0, 0b10_10_10_00); |
| 406 | |
| 407 | let temp1 = _mm_shuffle_ps(self.y_axis, self.x_axis, 0b01_01_01_01); |
| 408 | let vec1 = _mm_shuffle_ps(temp1, temp1, 0b10_10_10_00); |
| 409 | |
| 410 | let temp2 = _mm_shuffle_ps(self.y_axis, self.x_axis, 0b10_10_10_10); |
| 411 | let vec2 = _mm_shuffle_ps(temp2, temp2, 0b10_10_10_00); |
| 412 | |
| 413 | let temp3 = _mm_shuffle_ps(self.y_axis, self.x_axis, 0b11_11_11_11); |
| 414 | let vec3 = _mm_shuffle_ps(temp3, temp3, 0b10_10_10_00); |
| 415 | |
| 416 | let mul00 = _mm_mul_ps(vec1, fac0); |
| 417 | let mul01 = _mm_mul_ps(vec2, fac1); |
| 418 | let mul02 = _mm_mul_ps(vec3, fac2); |
| 419 | let sub00 = _mm_sub_ps(mul00, mul01); |
| 420 | let add00 = _mm_add_ps(sub00, mul02); |
| 421 | let inv0 = _mm_mul_ps(sign_b, add00); |
| 422 | |
| 423 | let mul03 = _mm_mul_ps(vec0, fac0); |
| 424 | let mul04 = _mm_mul_ps(vec2, fac3); |
| 425 | let mul05 = _mm_mul_ps(vec3, fac4); |
| 426 | let sub01 = _mm_sub_ps(mul03, mul04); |
| 427 | let add01 = _mm_add_ps(sub01, mul05); |
| 428 | let inv1 = _mm_mul_ps(sign_a, add01); |
| 429 | |
| 430 | let mul06 = _mm_mul_ps(vec0, fac1); |
| 431 | let mul07 = _mm_mul_ps(vec1, fac3); |
| 432 | let mul08 = _mm_mul_ps(vec3, fac5); |
| 433 | let sub02 = _mm_sub_ps(mul06, mul07); |
| 434 | let add02 = _mm_add_ps(sub02, mul08); |
| 435 | let inv2 = _mm_mul_ps(sign_b, add02); |
| 436 | |
| 437 | let mul09 = _mm_mul_ps(vec0, fac2); |
| 438 | let mul10 = _mm_mul_ps(vec1, fac4); |
| 439 | let mul11 = _mm_mul_ps(vec2, fac5); |
| 440 | let sub03 = _mm_sub_ps(mul09, mul10); |
| 441 | let add03 = _mm_add_ps(sub03, mul11); |
| 442 | let inv3 = _mm_mul_ps(sign_a, add03); |
| 443 | |
| 444 | let row0 = _mm_shuffle_ps(inv0, inv1, 0b00_00_00_00); |
| 445 | let row1 = _mm_shuffle_ps(inv2, inv3, 0b00_00_00_00); |
| 446 | let row2 = _mm_shuffle_ps(row0, row1, 0b10_00_10_00); |
| 447 | |
| 448 | let dot0 = Vector4::dot(self.x_axis, row2); |
| 449 | glam_assert!(dot0 != 0.0); |
| 450 | |
| 451 | let rcp0 = _mm_set1_ps(dot0.recip()); |
| 452 | |
| 453 | Self { |
| 454 | x_axis: _mm_mul_ps(inv0, rcp0), |
| 455 | y_axis: _mm_mul_ps(inv1, rcp0), |
| 456 | z_axis: _mm_mul_ps(inv2, rcp0), |
| 457 | w_axis: _mm_mul_ps(inv3, rcp0), |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | #[inline(always)] |
| 463 | fn transform_point3(&self, other: XYZ<f32>) -> XYZ<f32> { |
| 464 | self.x_axis |
| 465 | .mul_scalar(other.x) |
| 466 | .add(self.y_axis.mul_scalar(other.y)) |
| 467 | .add(self.z_axis.mul_scalar(other.z)) |
| 468 | .add(self.w_axis) |
| 469 | .into() |
| 470 | } |
| 471 | |
| 472 | #[inline(always)] |
| 473 | fn transform_vector3(&self, other: XYZ<f32>) -> XYZ<f32> { |
| 474 | self.x_axis |
| 475 | .mul_scalar(other.x) |
| 476 | .add(self.y_axis.mul_scalar(other.y)) |
| 477 | .add(self.z_axis.mul_scalar(other.z)) |
| 478 | .into() |
| 479 | } |
| 480 | |
| 481 | #[inline] |
| 482 | fn transform_float4_as_point3(&self, other: __m128) -> __m128 { |
| 483 | let mut res = self.x_axis.mul(Vector4::splat_x(other)); |
| 484 | res = self.y_axis.mul_add(Vector4::splat_y(other), res); |
| 485 | res = self.z_axis.mul_add(Vector4::splat_z(other), res); |
| 486 | res = self.w_axis.add(res); |
| 487 | res |
| 488 | } |
| 489 | |
| 490 | #[inline] |
| 491 | fn transform_float4_as_vector3(&self, other: __m128) -> __m128 { |
| 492 | let mut res = self.x_axis.mul(Vector4::splat_x(other)); |
| 493 | res = self.y_axis.mul_add(Vector4::splat_y(other), res); |
| 494 | res = self.z_axis.mul_add(Vector4::splat_z(other), res); |
| 495 | res |
| 496 | } |
| 497 | |
| 498 | #[inline] |
| 499 | fn project_float4_as_point3(&self, other: __m128) -> __m128 { |
| 500 | let mut res = self.x_axis.mul(Vector4::splat_x(other)); |
| 501 | res = self.y_axis.mul_add(Vector4::splat_y(other), res); |
| 502 | res = self.z_axis.mul_add(Vector4::splat_z(other), res); |
| 503 | res = self.w_axis.add(res); |
| 504 | res = res.mul(res.splat_w().recip()); |
| 505 | res |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | impl ProjectionMatrix<f32, __m128> for Columns4<__m128> {} |
| 510 | |
| 511 | impl From<Columns3<XYZ<f32>>> for Columns3<__m128> { |
| 512 | #[inline(always)] |
| 513 | fn from(v: Columns3<XYZ<f32>>) -> Columns3<__m128> { |
| 514 | Self { |
| 515 | x_axis: v.x_axis.into(), |
| 516 | y_axis: v.y_axis.into(), |
| 517 | z_axis: v.z_axis.into(), |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | impl From<Columns3<__m128>> for Columns3<XYZ<f32>> { |
| 523 | #[inline(always)] |
| 524 | fn from(v: Columns3<__m128>) -> Columns3<XYZ<f32>> { |
| 525 | Self { |
| 526 | x_axis: v.x_axis.into(), |
| 527 | y_axis: v.y_axis.into(), |
| 528 | z_axis: v.z_axis.into(), |
| 529 | } |
| 530 | } |
| 531 | } |