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