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