| 1 | #![allow(clippy::many_single_char_names)] |
| 2 | |
| 3 | #[cfg(target_arch = "x86")] |
| 4 | use core::arch::x86::*; |
| 5 | #[cfg(target_arch = "x86_64")] |
| 6 | use core::arch::x86_64::*; |
| 7 | |
| 8 | use super::float::*; |
| 9 | use crate::core::{ |
| 10 | storage::{Align16, XY, XYZ, XYZW}, |
| 11 | traits::{scalar::*, vector::*}, |
| 12 | }; |
| 13 | use core::mem::MaybeUninit; |
| 14 | |
| 15 | impl MaskVectorConst for __m128 { |
| 16 | const FALSE: __m128 = const_f32x4!([0.0; 4]); |
| 17 | } |
| 18 | |
| 19 | impl MaskVector for __m128 { |
| 20 | #[inline(always)] |
| 21 | fn bitand(self, other: Self) -> Self { |
| 22 | unsafe { _mm_and_ps(self, other) } |
| 23 | } |
| 24 | |
| 25 | #[inline(always)] |
| 26 | fn bitor(self, other: Self) -> Self { |
| 27 | unsafe { _mm_or_ps(self, other) } |
| 28 | } |
| 29 | |
| 30 | #[inline] |
| 31 | fn not(self) -> Self { |
| 32 | unsafe { _mm_andnot_ps(self, _mm_set_ps1(f32::from_bits(0xff_ff_ff_ff))) } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl MaskVector3 for __m128 { |
| 37 | #[inline(always)] |
| 38 | fn new(x: bool, y: bool, z: bool) -> Self { |
| 39 | // A SSE2 mask can be any bit pattern but for the `MaskVector3` implementation of select we |
| 40 | // expect either 0 or 0xff_ff_ff_ff. This should be a safe assumption as this type can only |
| 41 | // be created via this function or by `Vector3` methods. |
| 42 | |
| 43 | unsafe { |
| 44 | _mm_set_ps( |
| 45 | 0.0, |
| 46 | f32::from_bits(MaskConst::MASK[z as usize]), |
| 47 | f32::from_bits(MaskConst::MASK[y as usize]), |
| 48 | f32::from_bits(MaskConst::MASK[x as usize]), |
| 49 | ) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | #[inline(always)] |
| 54 | fn bitmask(self) -> u32 { |
| 55 | unsafe { (_mm_movemask_ps(self) as u32) & 0x7 } |
| 56 | } |
| 57 | |
| 58 | #[inline(always)] |
| 59 | fn any(self) -> bool { |
| 60 | unsafe { (_mm_movemask_ps(self) & 0x7) != 0 } |
| 61 | } |
| 62 | |
| 63 | #[inline(always)] |
| 64 | fn all(self) -> bool { |
| 65 | unsafe { (_mm_movemask_ps(self) & 0x7) == 0x7 } |
| 66 | } |
| 67 | |
| 68 | #[inline] |
| 69 | fn into_bool_array(self) -> [bool; 3] { |
| 70 | let bitmask = MaskVector3::bitmask(self); |
| 71 | [(bitmask & 1) != 0, (bitmask & 2) != 0, (bitmask & 4) != 0] |
| 72 | } |
| 73 | |
| 74 | #[inline] |
| 75 | fn into_u32_array(self) -> [u32; 3] { |
| 76 | let bitmask = MaskVector3::bitmask(self); |
| 77 | [ |
| 78 | MaskConst::MASK[(bitmask & 1) as usize], |
| 79 | MaskConst::MASK[((bitmask >> 1) & 1) as usize], |
| 80 | MaskConst::MASK[((bitmask >> 2) & 1) as usize], |
| 81 | ] |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl MaskVector4 for __m128 { |
| 86 | #[inline(always)] |
| 87 | fn new(x: bool, y: bool, z: bool, w: bool) -> Self { |
| 88 | // A SSE2 mask can be any bit pattern but for the `Vec4Mask` implementation of select we |
| 89 | // expect either 0 or 0xff_ff_ff_ff. This should be a safe assumption as this type can only |
| 90 | // be created via this function or by `Vec4` methods. |
| 91 | |
| 92 | const MASK: [u32; 2] = [0, 0xff_ff_ff_ff]; |
| 93 | unsafe { |
| 94 | _mm_set_ps( |
| 95 | f32::from_bits(MASK[w as usize]), |
| 96 | f32::from_bits(MASK[z as usize]), |
| 97 | f32::from_bits(MASK[y as usize]), |
| 98 | f32::from_bits(MASK[x as usize]), |
| 99 | ) |
| 100 | } |
| 101 | } |
| 102 | #[inline(always)] |
| 103 | fn bitmask(self) -> u32 { |
| 104 | unsafe { _mm_movemask_ps(self) as u32 } |
| 105 | } |
| 106 | |
| 107 | #[inline(always)] |
| 108 | fn any(self) -> bool { |
| 109 | unsafe { _mm_movemask_ps(self) != 0 } |
| 110 | } |
| 111 | |
| 112 | #[inline(always)] |
| 113 | fn all(self) -> bool { |
| 114 | unsafe { _mm_movemask_ps(self) == 0xf } |
| 115 | } |
| 116 | |
| 117 | #[inline] |
| 118 | fn into_bool_array(self) -> [bool; 4] { |
| 119 | let bitmask = MaskVector4::bitmask(self); |
| 120 | [ |
| 121 | (bitmask & 1) != 0, |
| 122 | (bitmask & 2) != 0, |
| 123 | (bitmask & 4) != 0, |
| 124 | (bitmask & 8) != 0, |
| 125 | ] |
| 126 | } |
| 127 | |
| 128 | #[inline] |
| 129 | fn into_u32_array(self) -> [u32; 4] { |
| 130 | let bitmask = MaskVector4::bitmask(self); |
| 131 | [ |
| 132 | MaskConst::MASK[(bitmask & 1) as usize], |
| 133 | MaskConst::MASK[((bitmask >> 1) & 1) as usize], |
| 134 | MaskConst::MASK[((bitmask >> 2) & 1) as usize], |
| 135 | MaskConst::MASK[((bitmask >> 3) & 1) as usize], |
| 136 | ] |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /// Calculates the vector 3 dot product and returns answer in x lane of __m128. |
| 141 | #[inline(always)] |
| 142 | unsafe fn dot3_in_x(lhs: __m128, rhs: __m128) -> __m128 { |
| 143 | let x2_y2_z2_w2 = _mm_mul_ps(lhs, rhs); |
| 144 | let y2_0_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_00_01); |
| 145 | let z2_0_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_00_10); |
| 146 | let x2y2_0_0_0 = _mm_add_ss(x2_y2_z2_w2, y2_0_0_0); |
| 147 | _mm_add_ss(x2y2_0_0_0, z2_0_0_0) |
| 148 | } |
| 149 | |
| 150 | /// Calculates the vector 4 dot product and returns answer in x lane of __m128. |
| 151 | #[inline(always)] |
| 152 | unsafe fn dot4_in_x(lhs: __m128, rhs: __m128) -> __m128 { |
| 153 | let x2_y2_z2_w2 = _mm_mul_ps(lhs, rhs); |
| 154 | let z2_w2_0_0 = _mm_shuffle_ps(x2_y2_z2_w2, x2_y2_z2_w2, 0b00_00_11_10); |
| 155 | let x2z2_y2w2_0_0 = _mm_add_ps(x2_y2_z2_w2, z2_w2_0_0); |
| 156 | let y2w2_0_0_0 = _mm_shuffle_ps(x2z2_y2w2_0_0, x2z2_y2w2_0_0, 0b00_00_00_01); |
| 157 | _mm_add_ps(x2z2_y2w2_0_0, y2w2_0_0_0) |
| 158 | } |
| 159 | |
| 160 | impl VectorConst for __m128 { |
| 161 | const ZERO: __m128 = const_f32x4!([0.0; 4]); |
| 162 | const ONE: __m128 = const_f32x4!([1.0; 4]); |
| 163 | } |
| 164 | |
| 165 | impl Vector3Const for __m128 { |
| 166 | const X: __m128 = const_f32x4!([1.0, 0.0, 0.0, 0.0]); |
| 167 | const Y: __m128 = const_f32x4!([0.0, 1.0, 0.0, 0.0]); |
| 168 | const Z: __m128 = const_f32x4!([0.0, 0.0, 1.0, 0.0]); |
| 169 | } |
| 170 | |
| 171 | impl Vector4Const for __m128 { |
| 172 | const X: __m128 = const_f32x4!([1.0, 0.0, 0.0, 0.0]); |
| 173 | const Y: __m128 = const_f32x4!([0.0, 1.0, 0.0, 0.0]); |
| 174 | const Z: __m128 = const_f32x4!([0.0, 0.0, 1.0, 0.0]); |
| 175 | const W: __m128 = const_f32x4!([0.0, 0.0, 0.0, 1.0]); |
| 176 | } |
| 177 | |
| 178 | impl NanConstEx for __m128 { |
| 179 | const NAN: __m128 = const_f32x4!([f32::NAN; 4]); |
| 180 | } |
| 181 | |
| 182 | impl Vector<f32> for __m128 { |
| 183 | type Mask = __m128; |
| 184 | |
| 185 | #[inline(always)] |
| 186 | fn splat(s: f32) -> Self { |
| 187 | unsafe { _mm_set_ps1(s) } |
| 188 | } |
| 189 | |
| 190 | #[inline(always)] |
| 191 | fn select(mask: Self::Mask, if_true: Self, if_false: Self) -> Self { |
| 192 | unsafe { _mm_or_ps(_mm_andnot_ps(mask, if_false), _mm_and_ps(if_true, mask)) } |
| 193 | } |
| 194 | |
| 195 | #[inline(always)] |
| 196 | fn cmpeq(self, other: Self) -> Self::Mask { |
| 197 | unsafe { _mm_cmpeq_ps(self, other) } |
| 198 | } |
| 199 | |
| 200 | #[inline(always)] |
| 201 | fn cmpne(self, other: Self) -> Self::Mask { |
| 202 | unsafe { _mm_cmpneq_ps(self, other) } |
| 203 | } |
| 204 | |
| 205 | #[inline(always)] |
| 206 | fn cmpge(self, other: Self) -> Self::Mask { |
| 207 | unsafe { _mm_cmpge_ps(self, other) } |
| 208 | } |
| 209 | |
| 210 | #[inline(always)] |
| 211 | fn cmpgt(self, other: Self) -> Self::Mask { |
| 212 | unsafe { _mm_cmpgt_ps(self, other) } |
| 213 | } |
| 214 | |
| 215 | #[inline(always)] |
| 216 | fn cmple(self, other: Self) -> Self::Mask { |
| 217 | unsafe { _mm_cmple_ps(self, other) } |
| 218 | } |
| 219 | |
| 220 | #[inline(always)] |
| 221 | fn cmplt(self, other: Self) -> Self::Mask { |
| 222 | unsafe { _mm_cmplt_ps(self, other) } |
| 223 | } |
| 224 | |
| 225 | #[inline(always)] |
| 226 | fn add(self, other: Self) -> Self { |
| 227 | unsafe { _mm_add_ps(self, other) } |
| 228 | } |
| 229 | |
| 230 | #[inline(always)] |
| 231 | fn div(self, other: Self) -> Self { |
| 232 | unsafe { _mm_div_ps(self, other) } |
| 233 | } |
| 234 | |
| 235 | #[inline(always)] |
| 236 | fn mul(self, other: Self) -> Self { |
| 237 | unsafe { _mm_mul_ps(self, other) } |
| 238 | } |
| 239 | |
| 240 | #[inline(always)] |
| 241 | fn sub(self, other: Self) -> Self { |
| 242 | unsafe { _mm_sub_ps(self, other) } |
| 243 | } |
| 244 | |
| 245 | #[inline(always)] |
| 246 | fn add_scalar(self, other: f32) -> Self { |
| 247 | unsafe { _mm_add_ps(self, _mm_set_ps1(other)) } |
| 248 | } |
| 249 | |
| 250 | #[inline(always)] |
| 251 | fn sub_scalar(self, other: f32) -> Self { |
| 252 | unsafe { _mm_sub_ps(self, _mm_set_ps1(other)) } |
| 253 | } |
| 254 | |
| 255 | #[inline(always)] |
| 256 | fn mul_scalar(self, other: f32) -> Self { |
| 257 | unsafe { _mm_mul_ps(self, _mm_set_ps1(other)) } |
| 258 | } |
| 259 | |
| 260 | #[inline(always)] |
| 261 | fn div_scalar(self, other: f32) -> Self { |
| 262 | unsafe { _mm_div_ps(self, _mm_set_ps1(other)) } |
| 263 | } |
| 264 | |
| 265 | #[inline(always)] |
| 266 | fn rem(self, other: Self) -> Self { |
| 267 | unsafe { |
| 268 | let n = m128_floor(_mm_div_ps(self, other)); |
| 269 | _mm_sub_ps(self, _mm_mul_ps(n, other)) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | #[inline(always)] |
| 274 | fn rem_scalar(self, other: f32) -> Self { |
| 275 | unsafe { self.rem(_mm_set1_ps(other)) } |
| 276 | } |
| 277 | |
| 278 | #[inline(always)] |
| 279 | fn min(self, other: Self) -> Self { |
| 280 | unsafe { _mm_min_ps(self, other) } |
| 281 | } |
| 282 | |
| 283 | #[inline(always)] |
| 284 | fn max(self, other: Self) -> Self { |
| 285 | unsafe { _mm_max_ps(self, other) } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | impl Vector3<f32> for __m128 { |
| 290 | #[inline(always)] |
| 291 | fn new(x: f32, y: f32, z: f32) -> Self { |
| 292 | unsafe { _mm_set_ps(z, z, y, x) } |
| 293 | } |
| 294 | |
| 295 | #[inline(always)] |
| 296 | fn x(self) -> f32 { |
| 297 | unsafe { _mm_cvtss_f32(self) } |
| 298 | } |
| 299 | |
| 300 | #[inline(always)] |
| 301 | fn y(self) -> f32 { |
| 302 | unsafe { _mm_cvtss_f32(_mm_shuffle_ps(self, self, 0b01_01_01_01)) } |
| 303 | } |
| 304 | |
| 305 | #[inline(always)] |
| 306 | fn z(self) -> f32 { |
| 307 | unsafe { _mm_cvtss_f32(_mm_shuffle_ps(self, self, 0b10_10_10_10)) } |
| 308 | } |
| 309 | |
| 310 | #[inline(always)] |
| 311 | fn splat_x(self) -> Self { |
| 312 | unsafe { _mm_shuffle_ps(self, self, 0b00_00_00_00) } |
| 313 | } |
| 314 | |
| 315 | #[inline(always)] |
| 316 | fn splat_y(self) -> Self { |
| 317 | unsafe { _mm_shuffle_ps(self, self, 0b01_01_01_01) } |
| 318 | } |
| 319 | |
| 320 | #[inline(always)] |
| 321 | fn splat_z(self) -> Self { |
| 322 | unsafe { _mm_shuffle_ps(self, self, 0b10_10_10_10) } |
| 323 | } |
| 324 | |
| 325 | #[inline(always)] |
| 326 | fn from_slice_unaligned(slice: &[f32]) -> Self { |
| 327 | Vector3::new(slice[0], slice[1], slice[2]) |
| 328 | } |
| 329 | |
| 330 | #[inline(always)] |
| 331 | fn write_to_slice_unaligned(self, slice: &mut [f32]) { |
| 332 | let xyz = self.as_ref_xyz(); |
| 333 | slice[0] = xyz.x; |
| 334 | slice[1] = xyz.y; |
| 335 | slice[2] = xyz.z; |
| 336 | } |
| 337 | |
| 338 | #[inline(always)] |
| 339 | fn as_ref_xyz(&self) -> &XYZ<f32> { |
| 340 | unsafe { &*(self as *const Self).cast() } |
| 341 | } |
| 342 | |
| 343 | #[inline(always)] |
| 344 | fn as_mut_xyz(&mut self) -> &mut XYZ<f32> { |
| 345 | unsafe { &mut *(self as *mut Self).cast() } |
| 346 | } |
| 347 | |
| 348 | #[inline(always)] |
| 349 | fn into_xy(self) -> XY<f32> { |
| 350 | let mut out: MaybeUninit<Align16<XY<f32>>> = MaybeUninit::uninit(); |
| 351 | unsafe { |
| 352 | _mm_store_ps(out.as_mut_ptr().cast(), self); |
| 353 | out.assume_init().0 |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | #[inline] |
| 358 | fn into_xyzw(self, w: f32) -> XYZW<f32> { |
| 359 | unsafe { |
| 360 | let mut t = _mm_move_ss(self, _mm_set_ss(w)); |
| 361 | t = _mm_shuffle_ps(t, t, 0b00_10_01_00); |
| 362 | // TODO: need a SIMD path |
| 363 | *_mm_move_ss(t, self).as_ref_xyzw() |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | #[inline(always)] |
| 368 | fn from_array(a: [f32; 3]) -> Self { |
| 369 | unsafe { _mm_set_ps(a[2], a[2], a[1], a[0]) } |
| 370 | } |
| 371 | |
| 372 | #[inline(always)] |
| 373 | fn into_array(self) -> [f32; 3] { |
| 374 | let mut out: MaybeUninit<Align16<[f32; 3]>> = MaybeUninit::uninit(); |
| 375 | unsafe { |
| 376 | _mm_store_ps(out.as_mut_ptr().cast(), self); |
| 377 | out.assume_init().0 |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | #[inline(always)] |
| 382 | fn from_tuple(t: (f32, f32, f32)) -> Self { |
| 383 | unsafe { _mm_set_ps(t.2, t.2, t.1, t.0) } |
| 384 | } |
| 385 | |
| 386 | #[inline(always)] |
| 387 | fn into_tuple(self) -> (f32, f32, f32) { |
| 388 | let mut out: MaybeUninit<Align16<(f32, f32, f32)>> = MaybeUninit::uninit(); |
| 389 | unsafe { |
| 390 | _mm_store_ps(out.as_mut_ptr().cast(), self); |
| 391 | out.assume_init().0 |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | #[inline] |
| 396 | fn min_element(self) -> f32 { |
| 397 | unsafe { |
| 398 | let v = self; |
| 399 | let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b01_01_10_10)); |
| 400 | let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 401 | _mm_cvtss_f32(v) |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | #[inline] |
| 406 | fn max_element(self) -> f32 { |
| 407 | unsafe { |
| 408 | let v = self; |
| 409 | let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_10_10)); |
| 410 | let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 411 | _mm_cvtss_f32(v) |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | #[inline] |
| 416 | fn dot(self, other: Self) -> f32 { |
| 417 | unsafe { _mm_cvtss_f32(dot3_in_x(self, other)) } |
| 418 | } |
| 419 | |
| 420 | #[inline] |
| 421 | fn dot_into_vec(self, other: Self) -> Self { |
| 422 | unsafe { |
| 423 | let dot_in_x = dot3_in_x(self, other); |
| 424 | _mm_shuffle_ps(dot_in_x, dot_in_x, 0b00_00_00_00) |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #[inline] |
| 429 | fn cross(self, other: Self) -> Self { |
| 430 | unsafe { |
| 431 | // x <- a.y*b.z - a.z*b.y |
| 432 | // y <- a.z*b.x - a.x*b.z |
| 433 | // z <- a.x*b.y - a.y*b.x |
| 434 | // We can save a shuffle by grouping it in this wacky order: |
| 435 | // (self.zxy() * other - self * other.zxy()).zxy() |
| 436 | let lhszxy = _mm_shuffle_ps(self, self, 0b01_01_00_10); |
| 437 | let rhszxy = _mm_shuffle_ps(other, other, 0b01_01_00_10); |
| 438 | let lhszxy_rhs = _mm_mul_ps(lhszxy, other); |
| 439 | let rhszxy_lhs = _mm_mul_ps(rhszxy, self); |
| 440 | let sub = _mm_sub_ps(lhszxy_rhs, rhszxy_lhs); |
| 441 | _mm_shuffle_ps(sub, sub, 0b01_01_00_10) |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | #[inline] |
| 446 | fn clamp(self, min: Self, max: Self) -> Self { |
| 447 | glam_assert!( |
| 448 | MaskVector3::all(min.cmple(max)), |
| 449 | "clamp: expected min <= max" |
| 450 | ); |
| 451 | self.max(min).min(max) |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | impl Vector4<f32> for __m128 { |
| 456 | #[inline(always)] |
| 457 | fn new(x: f32, y: f32, z: f32, w: f32) -> Self { |
| 458 | unsafe { _mm_set_ps(w, z, y, x) } |
| 459 | } |
| 460 | |
| 461 | #[inline(always)] |
| 462 | fn x(self) -> f32 { |
| 463 | unsafe { _mm_cvtss_f32(self) } |
| 464 | } |
| 465 | |
| 466 | #[inline(always)] |
| 467 | fn y(self) -> f32 { |
| 468 | unsafe { _mm_cvtss_f32(_mm_shuffle_ps(self, self, 0b01_01_01_01)) } |
| 469 | } |
| 470 | |
| 471 | #[inline(always)] |
| 472 | fn z(self) -> f32 { |
| 473 | unsafe { _mm_cvtss_f32(_mm_shuffle_ps(self, self, 0b10_10_10_10)) } |
| 474 | } |
| 475 | |
| 476 | #[inline(always)] |
| 477 | fn w(self) -> f32 { |
| 478 | unsafe { _mm_cvtss_f32(_mm_shuffle_ps(self, self, 0b11_11_11_11)) } |
| 479 | } |
| 480 | |
| 481 | #[inline(always)] |
| 482 | fn splat_x(self) -> Self { |
| 483 | unsafe { _mm_shuffle_ps(self, self, 0b00_00_00_00) } |
| 484 | } |
| 485 | |
| 486 | #[inline(always)] |
| 487 | fn splat_y(self) -> Self { |
| 488 | unsafe { _mm_shuffle_ps(self, self, 0b01_01_01_01) } |
| 489 | } |
| 490 | |
| 491 | #[inline(always)] |
| 492 | fn splat_z(self) -> Self { |
| 493 | unsafe { _mm_shuffle_ps(self, self, 0b10_10_10_10) } |
| 494 | } |
| 495 | |
| 496 | #[inline(always)] |
| 497 | fn splat_w(self) -> Self { |
| 498 | unsafe { _mm_shuffle_ps(self, self, 0b11_11_11_11) } |
| 499 | } |
| 500 | |
| 501 | #[inline(always)] |
| 502 | fn from_slice_unaligned(slice: &[f32]) -> Self { |
| 503 | assert!(slice.len() >= 4); |
| 504 | unsafe { _mm_loadu_ps(slice.as_ptr()) } |
| 505 | } |
| 506 | |
| 507 | #[inline(always)] |
| 508 | fn write_to_slice_unaligned(self, slice: &mut [f32]) { |
| 509 | unsafe { |
| 510 | assert!(slice.len() >= 4); |
| 511 | _mm_storeu_ps(slice.as_mut_ptr(), self); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | #[inline(always)] |
| 516 | fn as_ref_xyzw(&self) -> &XYZW<f32> { |
| 517 | unsafe { &*(self as *const Self).cast() } |
| 518 | } |
| 519 | |
| 520 | #[inline(always)] |
| 521 | fn as_mut_xyzw(&mut self) -> &mut XYZW<f32> { |
| 522 | unsafe { &mut *(self as *mut Self).cast() } |
| 523 | } |
| 524 | |
| 525 | #[inline(always)] |
| 526 | fn into_xy(self) -> XY<f32> { |
| 527 | let mut out: MaybeUninit<Align16<XY<f32>>> = MaybeUninit::uninit(); |
| 528 | unsafe { |
| 529 | _mm_store_ps(out.as_mut_ptr().cast(), self); |
| 530 | out.assume_init().0 |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | #[inline(always)] |
| 535 | fn into_xyz(self) -> XYZ<f32> { |
| 536 | let mut out: MaybeUninit<Align16<XYZ<f32>>> = MaybeUninit::uninit(); |
| 537 | unsafe { |
| 538 | _mm_store_ps(out.as_mut_ptr().cast(), self); |
| 539 | out.assume_init().0 |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | #[inline(always)] |
| 544 | fn from_array(a: [f32; 4]) -> Self { |
| 545 | unsafe { _mm_loadu_ps(a.as_ptr()) } |
| 546 | } |
| 547 | |
| 548 | #[inline(always)] |
| 549 | fn into_array(self) -> [f32; 4] { |
| 550 | let mut out: MaybeUninit<Align16<[f32; 4]>> = MaybeUninit::uninit(); |
| 551 | unsafe { |
| 552 | _mm_store_ps(out.as_mut_ptr().cast(), self); |
| 553 | out.assume_init().0 |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | #[inline(always)] |
| 558 | fn from_tuple(t: (f32, f32, f32, f32)) -> Self { |
| 559 | unsafe { _mm_set_ps(t.3, t.2, t.1, t.0) } |
| 560 | } |
| 561 | |
| 562 | #[inline(always)] |
| 563 | fn into_tuple(self) -> (f32, f32, f32, f32) { |
| 564 | let mut out: MaybeUninit<Align16<(f32, f32, f32, f32)>> = MaybeUninit::uninit(); |
| 565 | unsafe { |
| 566 | _mm_store_ps(out.as_mut_ptr().cast(), self); |
| 567 | out.assume_init().0 |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | #[inline] |
| 572 | fn min_element(self) -> f32 { |
| 573 | unsafe { |
| 574 | let v = self; |
| 575 | let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_11_10)); |
| 576 | let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 577 | _mm_cvtss_f32(v) |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | #[inline] |
| 582 | fn max_element(self) -> f32 { |
| 583 | unsafe { |
| 584 | let v = self; |
| 585 | let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_11_10)); |
| 586 | let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 587 | _mm_cvtss_f32(v) |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | #[inline] |
| 592 | fn dot(self, other: Self) -> f32 { |
| 593 | unsafe { _mm_cvtss_f32(dot4_in_x(self, other)) } |
| 594 | } |
| 595 | |
| 596 | #[inline] |
| 597 | fn dot_into_vec(self, other: Self) -> Self { |
| 598 | unsafe { |
| 599 | let dot_in_x = dot4_in_x(self, other); |
| 600 | _mm_shuffle_ps(dot_in_x, dot_in_x, 0b00_00_00_00) |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | #[inline] |
| 605 | fn clamp(self, min: Self, max: Self) -> Self { |
| 606 | glam_assert!( |
| 607 | MaskVector4::all(min.cmple(max)), |
| 608 | "clamp: expected min <= max" |
| 609 | ); |
| 610 | self.max(min).min(max) |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | impl SignedVector<f32> for __m128 { |
| 615 | #[inline(always)] |
| 616 | fn neg(self) -> Self { |
| 617 | unsafe { _mm_sub_ps(Self::ZERO, self) } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | impl SignedVector3<f32> for __m128 { |
| 622 | #[inline] |
| 623 | fn abs(self) -> Self { |
| 624 | unsafe { m128_abs(self) } |
| 625 | } |
| 626 | |
| 627 | #[inline] |
| 628 | fn signum(self) -> Self { |
| 629 | const NEG_ONE: __m128 = const_f32x4!([-1.0; 4]); |
| 630 | let mask = self.cmpge(Self::ZERO); |
| 631 | let result = Self::select(mask, Self::ONE, NEG_ONE); |
| 632 | let mask = unsafe { _mm_cmpunord_ps(self, self) }; |
| 633 | Self::select(mask, self, result) |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | impl FloatVector3<f32> for __m128 { |
| 638 | #[inline] |
| 639 | fn is_finite(self) -> bool { |
| 640 | let (x, y, z) = Vector3::into_tuple(self); |
| 641 | x.is_finite() && y.is_finite() && z.is_finite() |
| 642 | } |
| 643 | |
| 644 | #[inline] |
| 645 | fn is_nan(self) -> bool { |
| 646 | MaskVector3::any(FloatVector3::is_nan_mask(self)) |
| 647 | } |
| 648 | |
| 649 | #[inline(always)] |
| 650 | fn is_nan_mask(self) -> Self::Mask { |
| 651 | unsafe { _mm_cmpunord_ps(self, self) } |
| 652 | } |
| 653 | |
| 654 | #[inline(always)] |
| 655 | #[cfg(target_feature = "fma")] |
| 656 | fn mul_add(self, b: Self, c: Self) -> Self { |
| 657 | unsafe { _mm_fmadd_ps(self, b, c) } |
| 658 | } |
| 659 | |
| 660 | #[inline] |
| 661 | fn floor(self) -> Self { |
| 662 | unsafe { m128_floor(self) } |
| 663 | } |
| 664 | |
| 665 | #[inline] |
| 666 | fn ceil(self) -> Self { |
| 667 | unsafe { m128_ceil(self) } |
| 668 | } |
| 669 | |
| 670 | #[inline] |
| 671 | fn round(self) -> Self { |
| 672 | unsafe { m128_round(self) } |
| 673 | } |
| 674 | |
| 675 | #[inline(always)] |
| 676 | fn recip(self) -> Self { |
| 677 | unsafe { _mm_div_ps(Self::ONE, self) } |
| 678 | } |
| 679 | |
| 680 | #[inline] |
| 681 | fn exp(self) -> Self { |
| 682 | let (x, y, z) = Vector3::into_tuple(self); |
| 683 | unsafe { _mm_set_ps(0.0, z.exp(), y.exp(), x.exp()) } |
| 684 | } |
| 685 | |
| 686 | #[inline] |
| 687 | fn powf(self, n: f32) -> Self { |
| 688 | let (x, y, z) = Vector3::into_tuple(self); |
| 689 | unsafe { _mm_set_ps(0.0, z.powf(n), y.powf(n), x.powf(n)) } |
| 690 | } |
| 691 | |
| 692 | #[inline] |
| 693 | fn length(self) -> f32 { |
| 694 | unsafe { |
| 695 | let dot = dot3_in_x(self, self); |
| 696 | _mm_cvtss_f32(_mm_sqrt_ps(dot)) |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | #[inline] |
| 701 | fn length_recip(self) -> f32 { |
| 702 | unsafe { |
| 703 | let dot = dot3_in_x(self, self); |
| 704 | _mm_cvtss_f32(_mm_div_ps(Self::ONE, _mm_sqrt_ps(dot))) |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | #[inline] |
| 709 | fn normalize(self) -> Self { |
| 710 | unsafe { |
| 711 | let length = _mm_sqrt_ps(Vector3::dot_into_vec(self, self)); |
| 712 | #[allow(clippy::let_and_return)] |
| 713 | let normalized = _mm_div_ps(self, length); |
| 714 | glam_assert!(FloatVector3::is_finite(normalized)); |
| 715 | normalized |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | impl SignedVector4<f32> for __m128 { |
| 721 | #[inline] |
| 722 | fn abs(self) -> Self { |
| 723 | unsafe { m128_abs(self) } |
| 724 | } |
| 725 | |
| 726 | #[inline] |
| 727 | fn signum(self) -> Self { |
| 728 | const NEG_ONE: __m128 = const_f32x4!([-1.0; 4]); |
| 729 | let mask = self.cmpge(Self::ZERO); |
| 730 | let result = Self::select(mask, Self::ONE, NEG_ONE); |
| 731 | let mask = unsafe { _mm_cmpunord_ps(self, self) }; |
| 732 | Self::select(mask, self, result) |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | impl FloatVector4<f32> for __m128 { |
| 737 | #[inline] |
| 738 | fn is_finite(self) -> bool { |
| 739 | let (x, y, z, w) = Vector4::into_tuple(self); |
| 740 | x.is_finite() && y.is_finite() && z.is_finite() && w.is_finite() |
| 741 | } |
| 742 | |
| 743 | #[inline] |
| 744 | fn is_nan(self) -> bool { |
| 745 | MaskVector4::any(FloatVector4::is_nan_mask(self)) |
| 746 | } |
| 747 | |
| 748 | #[inline(always)] |
| 749 | fn is_nan_mask(self) -> Self::Mask { |
| 750 | unsafe { _mm_cmpunord_ps(self, self) } |
| 751 | } |
| 752 | |
| 753 | #[inline(always)] |
| 754 | #[cfg(target_feature = "fma")] |
| 755 | fn mul_add(self, b: Self, c: Self) -> Self { |
| 756 | unsafe { _mm_fmadd_ps(self, b, c) } |
| 757 | } |
| 758 | |
| 759 | #[inline] |
| 760 | fn floor(self) -> Self { |
| 761 | unsafe { m128_floor(self) } |
| 762 | } |
| 763 | |
| 764 | #[inline] |
| 765 | fn ceil(self) -> Self { |
| 766 | unsafe { m128_ceil(self) } |
| 767 | } |
| 768 | |
| 769 | #[inline] |
| 770 | fn round(self) -> Self { |
| 771 | unsafe { m128_round(self) } |
| 772 | } |
| 773 | |
| 774 | #[inline(always)] |
| 775 | fn recip(self) -> Self { |
| 776 | unsafe { _mm_div_ps(Self::ONE, self) } |
| 777 | } |
| 778 | |
| 779 | #[inline] |
| 780 | fn exp(self) -> Self { |
| 781 | let (x, y, z, w) = Vector4::into_tuple(self); |
| 782 | unsafe { _mm_set_ps(w.exp(), z.exp(), y.exp(), x.exp()) } |
| 783 | } |
| 784 | |
| 785 | #[inline] |
| 786 | fn powf(self, n: f32) -> Self { |
| 787 | let (x, y, z, w) = Vector4::into_tuple(self); |
| 788 | unsafe { _mm_set_ps(w.powf(n), z.powf(n), y.powf(n), x.powf(n)) } |
| 789 | } |
| 790 | |
| 791 | #[inline] |
| 792 | fn length(self) -> f32 { |
| 793 | unsafe { |
| 794 | let dot = dot4_in_x(self, self); |
| 795 | _mm_cvtss_f32(_mm_sqrt_ps(dot)) |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | #[inline] |
| 800 | fn length_recip(self) -> f32 { |
| 801 | unsafe { |
| 802 | let dot = dot4_in_x(self, self); |
| 803 | _mm_cvtss_f32(_mm_div_ps(Self::ONE, _mm_sqrt_ps(dot))) |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | #[inline] |
| 808 | fn normalize(self) -> Self { |
| 809 | unsafe { |
| 810 | let dot = Vector4::dot_into_vec(self, self); |
| 811 | #[allow(clippy::let_and_return)] |
| 812 | let normalized = _mm_div_ps(self, _mm_sqrt_ps(dot)); |
| 813 | glam_assert!(FloatVector4::is_finite(normalized)); |
| 814 | normalized |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | impl From<XYZW<f32>> for __m128 { |
| 820 | #[inline(always)] |
| 821 | fn from(v: XYZW<f32>) -> __m128 { |
| 822 | unsafe { _mm_set_ps(v.w, v.z, v.y, v.x) } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | impl From<XYZ<f32>> for __m128 { |
| 827 | #[inline(always)] |
| 828 | fn from(v: XYZ<f32>) -> __m128 { |
| 829 | unsafe { _mm_set_ps(v.z, v.z, v.y, v.x) } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | impl From<XY<f32>> for __m128 { |
| 834 | #[inline(always)] |
| 835 | fn from(v: XY<f32>) -> __m128 { |
| 836 | unsafe { _mm_set_ps(v.y, v.y, v.y, v.x) } |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | impl From<__m128> for XYZW<f32> { |
| 841 | #[inline(always)] |
| 842 | fn from(v: __m128) -> XYZW<f32> { |
| 843 | let mut out: MaybeUninit<Align16<XYZW<f32>>> = MaybeUninit::uninit(); |
| 844 | unsafe { |
| 845 | _mm_store_ps(out.as_mut_ptr().cast(), v); |
| 846 | out.assume_init().0 |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | impl From<__m128> for XYZ<f32> { |
| 852 | #[inline(always)] |
| 853 | fn from(v: __m128) -> XYZ<f32> { |
| 854 | let mut out: MaybeUninit<Align16<XYZ<f32>>> = MaybeUninit::uninit(); |
| 855 | unsafe { |
| 856 | _mm_store_ps(out.as_mut_ptr().cast(), v); |
| 857 | out.assume_init().0 |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | impl From<__m128> for XY<f32> { |
| 863 | #[inline(always)] |
| 864 | fn from(v: __m128) -> XY<f32> { |
| 865 | let mut out: MaybeUninit<Align16<XY<f32>>> = MaybeUninit::uninit(); |
| 866 | unsafe { |
| 867 | _mm_store_ps(out.as_mut_ptr().cast(), v); |
| 868 | out.assume_init().0 |
| 869 | } |
| 870 | } |
| 871 | } |