| 1 | // Generated from vec.rs.tera template. Edit the template, not the generated file. |
| 2 | |
| 3 | use crate::{f32::math, sse2::*, BVec4, BVec4A, Vec2, Vec3, Vec3A}; |
| 4 | |
| 5 | #[cfg(not(target_arch = "spirv"))] |
| 6 | use core::fmt; |
| 7 | use core::iter::{Product, Sum}; |
| 8 | use core::{f32, ops::*}; |
| 9 | |
| 10 | #[cfg(target_arch = "x86")] |
| 11 | use core::arch::x86::*; |
| 12 | #[cfg(target_arch = "x86_64")] |
| 13 | use core::arch::x86_64::*; |
| 14 | |
| 15 | #[repr(C)] |
| 16 | union UnionCast { |
| 17 | a: [f32; 4], |
| 18 | v: Vec4, |
| 19 | } |
| 20 | |
| 21 | /// Creates a 4-dimensional vector. |
| 22 | #[inline(always)] |
| 23 | #[must_use] |
| 24 | pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 { |
| 25 | Vec4::new(x, y, z, w) |
| 26 | } |
| 27 | |
| 28 | /// A 4-dimensional vector. |
| 29 | /// |
| 30 | /// SIMD vector types are used for storage on supported platforms. |
| 31 | /// |
| 32 | /// This type is 16 byte aligned. |
| 33 | #[derive(Clone, Copy)] |
| 34 | #[repr(transparent)] |
| 35 | pub struct Vec4(pub(crate) __m128); |
| 36 | |
| 37 | impl Vec4 { |
| 38 | /// All zeroes. |
| 39 | pub const ZERO: Self = Self::splat(0.0); |
| 40 | |
| 41 | /// All ones. |
| 42 | pub const ONE: Self = Self::splat(1.0); |
| 43 | |
| 44 | /// All negative ones. |
| 45 | pub const NEG_ONE: Self = Self::splat(-1.0); |
| 46 | |
| 47 | /// All `f32::MIN`. |
| 48 | pub const MIN: Self = Self::splat(f32::MIN); |
| 49 | |
| 50 | /// All `f32::MAX`. |
| 51 | pub const MAX: Self = Self::splat(f32::MAX); |
| 52 | |
| 53 | /// All `f32::NAN`. |
| 54 | pub const NAN: Self = Self::splat(f32::NAN); |
| 55 | |
| 56 | /// All `f32::INFINITY`. |
| 57 | pub const INFINITY: Self = Self::splat(f32::INFINITY); |
| 58 | |
| 59 | /// All `f32::NEG_INFINITY`. |
| 60 | pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY); |
| 61 | |
| 62 | /// A unit vector pointing along the positive X axis. |
| 63 | pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0); |
| 64 | |
| 65 | /// A unit vector pointing along the positive Y axis. |
| 66 | pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0); |
| 67 | |
| 68 | /// A unit vector pointing along the positive Z axis. |
| 69 | pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0); |
| 70 | |
| 71 | /// A unit vector pointing along the positive W axis. |
| 72 | pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0); |
| 73 | |
| 74 | /// A unit vector pointing along the negative X axis. |
| 75 | pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0); |
| 76 | |
| 77 | /// A unit vector pointing along the negative Y axis. |
| 78 | pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0); |
| 79 | |
| 80 | /// A unit vector pointing along the negative Z axis. |
| 81 | pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0); |
| 82 | |
| 83 | /// A unit vector pointing along the negative W axis. |
| 84 | pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0); |
| 85 | |
| 86 | /// The unit axes. |
| 87 | pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W]; |
| 88 | |
| 89 | /// Creates a new vector. |
| 90 | #[inline(always)] |
| 91 | #[must_use] |
| 92 | pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self { |
| 93 | unsafe { UnionCast { a: [x, y, z, w] }.v } |
| 94 | } |
| 95 | |
| 96 | /// Creates a vector with all elements set to `v`. |
| 97 | #[inline] |
| 98 | #[must_use] |
| 99 | pub const fn splat(v: f32) -> Self { |
| 100 | unsafe { UnionCast { a: [v; 4] }.v } |
| 101 | } |
| 102 | |
| 103 | /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use |
| 104 | /// for each element of `self`. |
| 105 | /// |
| 106 | /// A true element in the mask uses the corresponding element from `if_true`, and false |
| 107 | /// uses the element from `if_false`. |
| 108 | #[inline] |
| 109 | #[must_use] |
| 110 | pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Self { |
| 111 | Self(unsafe { |
| 112 | _mm_or_ps( |
| 113 | _mm_andnot_ps(mask.0, if_false.0), |
| 114 | _mm_and_ps(if_true.0, mask.0), |
| 115 | ) |
| 116 | }) |
| 117 | } |
| 118 | |
| 119 | /// Creates a new vector from an array. |
| 120 | #[inline] |
| 121 | #[must_use] |
| 122 | pub const fn from_array(a: [f32; 4]) -> Self { |
| 123 | Self::new(a[0], a[1], a[2], a[3]) |
| 124 | } |
| 125 | |
| 126 | /// `[x, y, z, w]` |
| 127 | #[inline] |
| 128 | #[must_use] |
| 129 | pub const fn to_array(&self) -> [f32; 4] { |
| 130 | unsafe { *(self as *const Vec4 as *const [f32; 4]) } |
| 131 | } |
| 132 | |
| 133 | /// Creates a vector from the first 4 values in `slice`. |
| 134 | /// |
| 135 | /// # Panics |
| 136 | /// |
| 137 | /// Panics if `slice` is less than 4 elements long. |
| 138 | #[inline] |
| 139 | #[must_use] |
| 140 | pub const fn from_slice(slice: &[f32]) -> Self { |
| 141 | Self::new(slice[0], slice[1], slice[2], slice[3]) |
| 142 | } |
| 143 | |
| 144 | /// Writes the elements of `self` to the first 4 elements in `slice`. |
| 145 | /// |
| 146 | /// # Panics |
| 147 | /// |
| 148 | /// Panics if `slice` is less than 4 elements long. |
| 149 | #[inline] |
| 150 | pub fn write_to_slice(self, slice: &mut [f32]) { |
| 151 | assert!(slice.len() >= 4); |
| 152 | unsafe { |
| 153 | _mm_storeu_ps(slice.as_mut_ptr(), self.0); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`. |
| 158 | /// |
| 159 | /// Truncation to [`Vec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()]. |
| 160 | /// |
| 161 | /// To truncate to [`Vec3A`] use [`Vec3A::from()`]. |
| 162 | #[inline] |
| 163 | #[must_use] |
| 164 | pub fn truncate(self) -> Vec3 { |
| 165 | use crate::swizzles::Vec4Swizzles; |
| 166 | self.xyz() |
| 167 | } |
| 168 | |
| 169 | /// Creates a 4D vector from `self` with the given value of `x`. |
| 170 | #[inline] |
| 171 | #[must_use] |
| 172 | pub fn with_x(mut self, x: f32) -> Self { |
| 173 | self.x = x; |
| 174 | self |
| 175 | } |
| 176 | |
| 177 | /// Creates a 4D vector from `self` with the given value of `y`. |
| 178 | #[inline] |
| 179 | #[must_use] |
| 180 | pub fn with_y(mut self, y: f32) -> Self { |
| 181 | self.y = y; |
| 182 | self |
| 183 | } |
| 184 | |
| 185 | /// Creates a 4D vector from `self` with the given value of `z`. |
| 186 | #[inline] |
| 187 | #[must_use] |
| 188 | pub fn with_z(mut self, z: f32) -> Self { |
| 189 | self.z = z; |
| 190 | self |
| 191 | } |
| 192 | |
| 193 | /// Creates a 4D vector from `self` with the given value of `w`. |
| 194 | #[inline] |
| 195 | #[must_use] |
| 196 | pub fn with_w(mut self, w: f32) -> Self { |
| 197 | self.w = w; |
| 198 | self |
| 199 | } |
| 200 | |
| 201 | /// Computes the dot product of `self` and `rhs`. |
| 202 | #[inline] |
| 203 | #[must_use] |
| 204 | pub fn dot(self, rhs: Self) -> f32 { |
| 205 | unsafe { dot4(self.0, rhs.0) } |
| 206 | } |
| 207 | |
| 208 | /// Returns a vector where every component is the dot product of `self` and `rhs`. |
| 209 | #[inline] |
| 210 | #[must_use] |
| 211 | pub fn dot_into_vec(self, rhs: Self) -> Self { |
| 212 | Self(unsafe { dot4_into_m128(self.0, rhs.0) }) |
| 213 | } |
| 214 | |
| 215 | /// Returns a vector containing the minimum values for each element of `self` and `rhs`. |
| 216 | /// |
| 217 | /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`. |
| 218 | #[inline] |
| 219 | #[must_use] |
| 220 | pub fn min(self, rhs: Self) -> Self { |
| 221 | Self(unsafe { _mm_min_ps(self.0, rhs.0) }) |
| 222 | } |
| 223 | |
| 224 | /// Returns a vector containing the maximum values for each element of `self` and `rhs`. |
| 225 | /// |
| 226 | /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`. |
| 227 | #[inline] |
| 228 | #[must_use] |
| 229 | pub fn max(self, rhs: Self) -> Self { |
| 230 | Self(unsafe { _mm_max_ps(self.0, rhs.0) }) |
| 231 | } |
| 232 | |
| 233 | /// Component-wise clamping of values, similar to [`f32::clamp`]. |
| 234 | /// |
| 235 | /// Each element in `min` must be less-or-equal to the corresponding element in `max`. |
| 236 | /// |
| 237 | /// # Panics |
| 238 | /// |
| 239 | /// Will panic if `min` is greater than `max` when `glam_assert` is enabled. |
| 240 | #[inline] |
| 241 | #[must_use] |
| 242 | pub fn clamp(self, min: Self, max: Self) -> Self { |
| 243 | glam_assert!(min.cmple(max).all(), "clamp: expected min <= max"); |
| 244 | self.max(min).min(max) |
| 245 | } |
| 246 | |
| 247 | /// Returns the horizontal minimum of `self`. |
| 248 | /// |
| 249 | /// In other words this computes `min(x, y, ..)`. |
| 250 | #[inline] |
| 251 | #[must_use] |
| 252 | pub fn min_element(self) -> f32 { |
| 253 | unsafe { |
| 254 | let v = self.0; |
| 255 | let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_11_10)); |
| 256 | let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 257 | _mm_cvtss_f32(v) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /// Returns the horizontal maximum of `self`. |
| 262 | /// |
| 263 | /// In other words this computes `max(x, y, ..)`. |
| 264 | #[inline] |
| 265 | #[must_use] |
| 266 | pub fn max_element(self) -> f32 { |
| 267 | unsafe { |
| 268 | let v = self.0; |
| 269 | let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_11_10)); |
| 270 | let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01)); |
| 271 | _mm_cvtss_f32(v) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /// Returns the sum of all elements of `self`. |
| 276 | /// |
| 277 | /// In other words, this computes `self.x + self.y + ..`. |
| 278 | #[inline] |
| 279 | #[must_use] |
| 280 | pub fn element_sum(self) -> f32 { |
| 281 | unsafe { |
| 282 | let v = self.0; |
| 283 | let v = _mm_add_ps(v, _mm_shuffle_ps(v, v, 0b00_11_00_01)); |
| 284 | let v = _mm_add_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10)); |
| 285 | _mm_cvtss_f32(v) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /// Returns the product of all elements of `self`. |
| 290 | /// |
| 291 | /// In other words, this computes `self.x * self.y * ..`. |
| 292 | #[inline] |
| 293 | #[must_use] |
| 294 | pub fn element_product(self) -> f32 { |
| 295 | unsafe { |
| 296 | let v = self.0; |
| 297 | let v = _mm_mul_ps(v, _mm_shuffle_ps(v, v, 0b00_11_00_01)); |
| 298 | let v = _mm_mul_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10)); |
| 299 | _mm_cvtss_f32(v) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /// Returns a vector mask containing the result of a `==` comparison for each element of |
| 304 | /// `self` and `rhs`. |
| 305 | /// |
| 306 | /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all |
| 307 | /// elements. |
| 308 | #[inline] |
| 309 | #[must_use] |
| 310 | pub fn cmpeq(self, rhs: Self) -> BVec4A { |
| 311 | BVec4A(unsafe { _mm_cmpeq_ps(self.0, rhs.0) }) |
| 312 | } |
| 313 | |
| 314 | /// Returns a vector mask containing the result of a `!=` comparison for each element of |
| 315 | /// `self` and `rhs`. |
| 316 | /// |
| 317 | /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all |
| 318 | /// elements. |
| 319 | #[inline] |
| 320 | #[must_use] |
| 321 | pub fn cmpne(self, rhs: Self) -> BVec4A { |
| 322 | BVec4A(unsafe { _mm_cmpneq_ps(self.0, rhs.0) }) |
| 323 | } |
| 324 | |
| 325 | /// Returns a vector mask containing the result of a `>=` comparison for each element of |
| 326 | /// `self` and `rhs`. |
| 327 | /// |
| 328 | /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all |
| 329 | /// elements. |
| 330 | #[inline] |
| 331 | #[must_use] |
| 332 | pub fn cmpge(self, rhs: Self) -> BVec4A { |
| 333 | BVec4A(unsafe { _mm_cmpge_ps(self.0, rhs.0) }) |
| 334 | } |
| 335 | |
| 336 | /// Returns a vector mask containing the result of a `>` comparison for each element of |
| 337 | /// `self` and `rhs`. |
| 338 | /// |
| 339 | /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all |
| 340 | /// elements. |
| 341 | #[inline] |
| 342 | #[must_use] |
| 343 | pub fn cmpgt(self, rhs: Self) -> BVec4A { |
| 344 | BVec4A(unsafe { _mm_cmpgt_ps(self.0, rhs.0) }) |
| 345 | } |
| 346 | |
| 347 | /// Returns a vector mask containing the result of a `<=` comparison for each element of |
| 348 | /// `self` and `rhs`. |
| 349 | /// |
| 350 | /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all |
| 351 | /// elements. |
| 352 | #[inline] |
| 353 | #[must_use] |
| 354 | pub fn cmple(self, rhs: Self) -> BVec4A { |
| 355 | BVec4A(unsafe { _mm_cmple_ps(self.0, rhs.0) }) |
| 356 | } |
| 357 | |
| 358 | /// Returns a vector mask containing the result of a `<` comparison for each element of |
| 359 | /// `self` and `rhs`. |
| 360 | /// |
| 361 | /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all |
| 362 | /// elements. |
| 363 | #[inline] |
| 364 | #[must_use] |
| 365 | pub fn cmplt(self, rhs: Self) -> BVec4A { |
| 366 | BVec4A(unsafe { _mm_cmplt_ps(self.0, rhs.0) }) |
| 367 | } |
| 368 | |
| 369 | /// Returns a vector containing the absolute value of each element of `self`. |
| 370 | #[inline] |
| 371 | #[must_use] |
| 372 | pub fn abs(self) -> Self { |
| 373 | Self(unsafe { crate::sse2::m128_abs(self.0) }) |
| 374 | } |
| 375 | |
| 376 | /// Returns a vector with elements representing the sign of `self`. |
| 377 | /// |
| 378 | /// - `1.0` if the number is positive, `+0.0` or `INFINITY` |
| 379 | /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY` |
| 380 | /// - `NAN` if the number is `NAN` |
| 381 | #[inline] |
| 382 | #[must_use] |
| 383 | pub fn signum(self) -> Self { |
| 384 | let result = Self(unsafe { _mm_or_ps(_mm_and_ps(self.0, Self::NEG_ONE.0), Self::ONE.0) }); |
| 385 | let mask = self.is_nan_mask(); |
| 386 | Self::select(mask, self, result) |
| 387 | } |
| 388 | |
| 389 | /// Returns a vector with signs of `rhs` and the magnitudes of `self`. |
| 390 | #[inline] |
| 391 | #[must_use] |
| 392 | pub fn copysign(self, rhs: Self) -> Self { |
| 393 | let mask = Self::splat(-0.0); |
| 394 | Self(unsafe { _mm_or_ps(_mm_and_ps(rhs.0, mask.0), _mm_andnot_ps(mask.0, self.0)) }) |
| 395 | } |
| 396 | |
| 397 | /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`. |
| 398 | /// |
| 399 | /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes |
| 400 | /// into the first lowest bit, element `y` into the second, etc. |
| 401 | #[inline] |
| 402 | #[must_use] |
| 403 | pub fn is_negative_bitmask(self) -> u32 { |
| 404 | unsafe { _mm_movemask_ps(self.0) as u32 } |
| 405 | } |
| 406 | |
| 407 | /// Returns `true` if, and only if, all elements are finite. If any element is either |
| 408 | /// `NaN`, positive or negative infinity, this will return `false`. |
| 409 | #[inline] |
| 410 | #[must_use] |
| 411 | pub fn is_finite(self) -> bool { |
| 412 | self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite() |
| 413 | } |
| 414 | |
| 415 | /// Returns `true` if any elements are `NaN`. |
| 416 | #[inline] |
| 417 | #[must_use] |
| 418 | pub fn is_nan(self) -> bool { |
| 419 | self.is_nan_mask().any() |
| 420 | } |
| 421 | |
| 422 | /// Performs `is_nan` on each element of self, returning a vector mask of the results. |
| 423 | /// |
| 424 | /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`. |
| 425 | #[inline] |
| 426 | #[must_use] |
| 427 | pub fn is_nan_mask(self) -> BVec4A { |
| 428 | BVec4A(unsafe { _mm_cmpunord_ps(self.0, self.0) }) |
| 429 | } |
| 430 | |
| 431 | /// Computes the length of `self`. |
| 432 | #[doc(alias = "magnitude")] |
| 433 | #[inline] |
| 434 | #[must_use] |
| 435 | pub fn length(self) -> f32 { |
| 436 | unsafe { |
| 437 | let dot = dot4_in_x(self.0, self.0); |
| 438 | _mm_cvtss_f32(_mm_sqrt_ps(dot)) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /// Computes the squared length of `self`. |
| 443 | /// |
| 444 | /// This is faster than `length()` as it avoids a square root operation. |
| 445 | #[doc(alias = "magnitude2")] |
| 446 | #[inline] |
| 447 | #[must_use] |
| 448 | pub fn length_squared(self) -> f32 { |
| 449 | self.dot(self) |
| 450 | } |
| 451 | |
| 452 | /// Computes `1.0 / length()`. |
| 453 | /// |
| 454 | /// For valid results, `self` must _not_ be of length zero. |
| 455 | #[inline] |
| 456 | #[must_use] |
| 457 | pub fn length_recip(self) -> f32 { |
| 458 | unsafe { |
| 459 | let dot = dot4_in_x(self.0, self.0); |
| 460 | _mm_cvtss_f32(_mm_div_ps(Self::ONE.0, _mm_sqrt_ps(dot))) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /// Computes the Euclidean distance between two points in space. |
| 465 | #[inline] |
| 466 | #[must_use] |
| 467 | pub fn distance(self, rhs: Self) -> f32 { |
| 468 | (self - rhs).length() |
| 469 | } |
| 470 | |
| 471 | /// Compute the squared euclidean distance between two points in space. |
| 472 | #[inline] |
| 473 | #[must_use] |
| 474 | pub fn distance_squared(self, rhs: Self) -> f32 { |
| 475 | (self - rhs).length_squared() |
| 476 | } |
| 477 | |
| 478 | /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`. |
| 479 | #[inline] |
| 480 | #[must_use] |
| 481 | pub fn div_euclid(self, rhs: Self) -> Self { |
| 482 | Self::new( |
| 483 | math::div_euclid(self.x, rhs.x), |
| 484 | math::div_euclid(self.y, rhs.y), |
| 485 | math::div_euclid(self.z, rhs.z), |
| 486 | math::div_euclid(self.w, rhs.w), |
| 487 | ) |
| 488 | } |
| 489 | |
| 490 | /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`. |
| 491 | /// |
| 492 | /// [Euclidean division]: f32::rem_euclid |
| 493 | #[inline] |
| 494 | #[must_use] |
| 495 | pub fn rem_euclid(self, rhs: Self) -> Self { |
| 496 | Self::new( |
| 497 | math::rem_euclid(self.x, rhs.x), |
| 498 | math::rem_euclid(self.y, rhs.y), |
| 499 | math::rem_euclid(self.z, rhs.z), |
| 500 | math::rem_euclid(self.w, rhs.w), |
| 501 | ) |
| 502 | } |
| 503 | |
| 504 | /// Returns `self` normalized to length 1.0. |
| 505 | /// |
| 506 | /// For valid results, `self` must _not_ be of length zero, nor very close to zero. |
| 507 | /// |
| 508 | /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`]. |
| 509 | /// |
| 510 | /// Panics |
| 511 | /// |
| 512 | /// Will panic if `self` is zero length when `glam_assert` is enabled. |
| 513 | #[inline] |
| 514 | #[must_use] |
| 515 | pub fn normalize(self) -> Self { |
| 516 | unsafe { |
| 517 | let length = _mm_sqrt_ps(dot4_into_m128(self.0, self.0)); |
| 518 | #[allow(clippy::let_and_return)] |
| 519 | let normalized = Self(_mm_div_ps(self.0, length)); |
| 520 | glam_assert!(normalized.is_finite()); |
| 521 | normalized |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /// Returns `self` normalized to length 1.0 if possible, else returns `None`. |
| 526 | /// |
| 527 | /// In particular, if the input is zero (or very close to zero), or non-finite, |
| 528 | /// the result of this operation will be `None`. |
| 529 | /// |
| 530 | /// See also [`Self::normalize_or_zero()`]. |
| 531 | #[inline] |
| 532 | #[must_use] |
| 533 | pub fn try_normalize(self) -> Option<Self> { |
| 534 | let rcp = self.length_recip(); |
| 535 | if rcp.is_finite() && rcp > 0.0 { |
| 536 | Some(self * rcp) |
| 537 | } else { |
| 538 | None |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /// Returns `self` normalized to length 1.0 if possible, else returns a |
| 543 | /// fallback value. |
| 544 | /// |
| 545 | /// In particular, if the input is zero (or very close to zero), or non-finite, |
| 546 | /// the result of this operation will be the fallback value. |
| 547 | /// |
| 548 | /// See also [`Self::try_normalize()`]. |
| 549 | #[inline] |
| 550 | #[must_use] |
| 551 | pub fn normalize_or(self, fallback: Self) -> Self { |
| 552 | let rcp = self.length_recip(); |
| 553 | if rcp.is_finite() && rcp > 0.0 { |
| 554 | self * rcp |
| 555 | } else { |
| 556 | fallback |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /// Returns `self` normalized to length 1.0 if possible, else returns zero. |
| 561 | /// |
| 562 | /// In particular, if the input is zero (or very close to zero), or non-finite, |
| 563 | /// the result of this operation will be zero. |
| 564 | /// |
| 565 | /// See also [`Self::try_normalize()`]. |
| 566 | #[inline] |
| 567 | #[must_use] |
| 568 | pub fn normalize_or_zero(self) -> Self { |
| 569 | self.normalize_or(Self::ZERO) |
| 570 | } |
| 571 | |
| 572 | /// Returns whether `self` is length `1.0` or not. |
| 573 | /// |
| 574 | /// Uses a precision threshold of approximately `1e-4`. |
| 575 | #[inline] |
| 576 | #[must_use] |
| 577 | pub fn is_normalized(self) -> bool { |
| 578 | math::abs(self.length_squared() - 1.0) <= 2e-4 |
| 579 | } |
| 580 | |
| 581 | /// Returns the vector projection of `self` onto `rhs`. |
| 582 | /// |
| 583 | /// `rhs` must be of non-zero length. |
| 584 | /// |
| 585 | /// # Panics |
| 586 | /// |
| 587 | /// Will panic if `rhs` is zero length when `glam_assert` is enabled. |
| 588 | #[inline] |
| 589 | #[must_use] |
| 590 | pub fn project_onto(self, rhs: Self) -> Self { |
| 591 | let other_len_sq_rcp = rhs.dot(rhs).recip(); |
| 592 | glam_assert!(other_len_sq_rcp.is_finite()); |
| 593 | rhs * self.dot(rhs) * other_len_sq_rcp |
| 594 | } |
| 595 | |
| 596 | /// Returns the vector rejection of `self` from `rhs`. |
| 597 | /// |
| 598 | /// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 599 | /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`. |
| 600 | /// |
| 601 | /// `rhs` must be of non-zero length. |
| 602 | /// |
| 603 | /// # Panics |
| 604 | /// |
| 605 | /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled. |
| 606 | #[inline] |
| 607 | #[must_use] |
| 608 | pub fn reject_from(self, rhs: Self) -> Self { |
| 609 | self - self.project_onto(rhs) |
| 610 | } |
| 611 | |
| 612 | /// Returns the vector projection of `self` onto `rhs`. |
| 613 | /// |
| 614 | /// `rhs` must be normalized. |
| 615 | /// |
| 616 | /// # Panics |
| 617 | /// |
| 618 | /// Will panic if `rhs` is not normalized when `glam_assert` is enabled. |
| 619 | #[inline] |
| 620 | #[must_use] |
| 621 | pub fn project_onto_normalized(self, rhs: Self) -> Self { |
| 622 | glam_assert!(rhs.is_normalized()); |
| 623 | rhs * self.dot(rhs) |
| 624 | } |
| 625 | |
| 626 | /// Returns the vector rejection of `self` from `rhs`. |
| 627 | /// |
| 628 | /// The vector rejection is the vector perpendicular to the projection of `self` onto |
| 629 | /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`. |
| 630 | /// |
| 631 | /// `rhs` must be normalized. |
| 632 | /// |
| 633 | /// # Panics |
| 634 | /// |
| 635 | /// Will panic if `rhs` is not normalized when `glam_assert` is enabled. |
| 636 | #[inline] |
| 637 | #[must_use] |
| 638 | pub fn reject_from_normalized(self, rhs: Self) -> Self { |
| 639 | self - self.project_onto_normalized(rhs) |
| 640 | } |
| 641 | |
| 642 | /// Returns a vector containing the nearest integer to a number for each element of `self`. |
| 643 | /// Round half-way cases away from 0.0. |
| 644 | #[inline] |
| 645 | #[must_use] |
| 646 | pub fn round(self) -> Self { |
| 647 | Self(unsafe { m128_round(self.0) }) |
| 648 | } |
| 649 | |
| 650 | /// Returns a vector containing the largest integer less than or equal to a number for each |
| 651 | /// element of `self`. |
| 652 | #[inline] |
| 653 | #[must_use] |
| 654 | pub fn floor(self) -> Self { |
| 655 | Self(unsafe { m128_floor(self.0) }) |
| 656 | } |
| 657 | |
| 658 | /// Returns a vector containing the smallest integer greater than or equal to a number for |
| 659 | /// each element of `self`. |
| 660 | #[inline] |
| 661 | #[must_use] |
| 662 | pub fn ceil(self) -> Self { |
| 663 | Self(unsafe { m128_ceil(self.0) }) |
| 664 | } |
| 665 | |
| 666 | /// Returns a vector containing the integer part each element of `self`. This means numbers are |
| 667 | /// always truncated towards zero. |
| 668 | #[inline] |
| 669 | #[must_use] |
| 670 | pub fn trunc(self) -> Self { |
| 671 | Self(unsafe { m128_trunc(self.0) }) |
| 672 | } |
| 673 | |
| 674 | /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`. |
| 675 | /// |
| 676 | /// Note that this differs from the GLSL implementation of `fract` which returns |
| 677 | /// `self - self.floor()`. |
| 678 | /// |
| 679 | /// Note that this is fast but not precise for large numbers. |
| 680 | #[inline] |
| 681 | #[must_use] |
| 682 | pub fn fract(self) -> Self { |
| 683 | self - self.trunc() |
| 684 | } |
| 685 | |
| 686 | /// Returns a vector containing the fractional part of the vector as `self - self.floor()`. |
| 687 | /// |
| 688 | /// Note that this differs from the Rust implementation of `fract` which returns |
| 689 | /// `self - self.trunc()`. |
| 690 | /// |
| 691 | /// Note that this is fast but not precise for large numbers. |
| 692 | #[inline] |
| 693 | #[must_use] |
| 694 | pub fn fract_gl(self) -> Self { |
| 695 | self - self.floor() |
| 696 | } |
| 697 | |
| 698 | /// Returns a vector containing `e^self` (the exponential function) for each element of |
| 699 | /// `self`. |
| 700 | #[inline] |
| 701 | #[must_use] |
| 702 | pub fn exp(self) -> Self { |
| 703 | Self::new( |
| 704 | math::exp(self.x), |
| 705 | math::exp(self.y), |
| 706 | math::exp(self.z), |
| 707 | math::exp(self.w), |
| 708 | ) |
| 709 | } |
| 710 | |
| 711 | /// Returns a vector containing each element of `self` raised to the power of `n`. |
| 712 | #[inline] |
| 713 | #[must_use] |
| 714 | pub fn powf(self, n: f32) -> Self { |
| 715 | Self::new( |
| 716 | math::powf(self.x, n), |
| 717 | math::powf(self.y, n), |
| 718 | math::powf(self.z, n), |
| 719 | math::powf(self.w, n), |
| 720 | ) |
| 721 | } |
| 722 | |
| 723 | /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`. |
| 724 | #[inline] |
| 725 | #[must_use] |
| 726 | pub fn recip(self) -> Self { |
| 727 | Self(unsafe { _mm_div_ps(Self::ONE.0, self.0) }) |
| 728 | } |
| 729 | |
| 730 | /// Performs a linear interpolation between `self` and `rhs` based on the value `s`. |
| 731 | /// |
| 732 | /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result |
| 733 | /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly |
| 734 | /// extrapolated. |
| 735 | #[doc(alias = "mix")] |
| 736 | #[inline] |
| 737 | #[must_use] |
| 738 | pub fn lerp(self, rhs: Self, s: f32) -> Self { |
| 739 | self + ((rhs - self) * s) |
| 740 | } |
| 741 | |
| 742 | /// Moves towards `rhs` based on the value `d`. |
| 743 | /// |
| 744 | /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to |
| 745 | /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`. |
| 746 | #[inline] |
| 747 | #[must_use] |
| 748 | pub fn move_towards(&self, rhs: Self, d: f32) -> Self { |
| 749 | let a = rhs - *self; |
| 750 | let len = a.length(); |
| 751 | if len <= d || len <= 1e-4 { |
| 752 | return rhs; |
| 753 | } |
| 754 | *self + a / len * d |
| 755 | } |
| 756 | |
| 757 | /// Calculates the midpoint between `self` and `rhs`. |
| 758 | /// |
| 759 | /// The midpoint is the average of, or halfway point between, two vectors. |
| 760 | /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)` |
| 761 | /// while being slightly cheaper to compute. |
| 762 | #[inline] |
| 763 | pub fn midpoint(self, rhs: Self) -> Self { |
| 764 | (self + rhs) * 0.5 |
| 765 | } |
| 766 | |
| 767 | /// Returns true if the absolute difference of all elements between `self` and `rhs` is |
| 768 | /// less than or equal to `max_abs_diff`. |
| 769 | /// |
| 770 | /// This can be used to compare if two vectors contain similar elements. It works best when |
| 771 | /// comparing with a known value. The `max_abs_diff` that should be used used depends on |
| 772 | /// the values being compared against. |
| 773 | /// |
| 774 | /// For more see |
| 775 | /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 776 | #[inline] |
| 777 | #[must_use] |
| 778 | pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool { |
| 779 | self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all() |
| 780 | } |
| 781 | |
| 782 | /// Returns a vector with a length no less than `min` and no more than `max`. |
| 783 | /// |
| 784 | /// # Panics |
| 785 | /// |
| 786 | /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled. |
| 787 | #[inline] |
| 788 | #[must_use] |
| 789 | pub fn clamp_length(self, min: f32, max: f32) -> Self { |
| 790 | glam_assert!(0.0 <= min); |
| 791 | glam_assert!(min <= max); |
| 792 | let length_sq = self.length_squared(); |
| 793 | if length_sq < min * min { |
| 794 | min * (self / math::sqrt(length_sq)) |
| 795 | } else if length_sq > max * max { |
| 796 | max * (self / math::sqrt(length_sq)) |
| 797 | } else { |
| 798 | self |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | /// Returns a vector with a length no more than `max`. |
| 803 | /// |
| 804 | /// # Panics |
| 805 | /// |
| 806 | /// Will panic if `max` is negative when `glam_assert` is enabled. |
| 807 | #[inline] |
| 808 | #[must_use] |
| 809 | pub fn clamp_length_max(self, max: f32) -> Self { |
| 810 | glam_assert!(0.0 <= max); |
| 811 | let length_sq = self.length_squared(); |
| 812 | if length_sq > max * max { |
| 813 | max * (self / math::sqrt(length_sq)) |
| 814 | } else { |
| 815 | self |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | /// Returns a vector with a length no less than `min`. |
| 820 | /// |
| 821 | /// # Panics |
| 822 | /// |
| 823 | /// Will panic if `min` is negative when `glam_assert` is enabled. |
| 824 | #[inline] |
| 825 | #[must_use] |
| 826 | pub fn clamp_length_min(self, min: f32) -> Self { |
| 827 | glam_assert!(0.0 <= min); |
| 828 | let length_sq = self.length_squared(); |
| 829 | if length_sq < min * min { |
| 830 | min * (self / math::sqrt(length_sq)) |
| 831 | } else { |
| 832 | self |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding |
| 837 | /// error, yielding a more accurate result than an unfused multiply-add. |
| 838 | /// |
| 839 | /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target |
| 840 | /// architecture has a dedicated fma CPU instruction. However, this is not always true, |
| 841 | /// and will be heavily dependant on designing algorithms with specific target hardware in |
| 842 | /// mind. |
| 843 | #[inline] |
| 844 | #[must_use] |
| 845 | pub fn mul_add(self, a: Self, b: Self) -> Self { |
| 846 | #[cfg(target_feature = "fma")] |
| 847 | unsafe { |
| 848 | Self(_mm_fmadd_ps(self.0, a.0, b.0)) |
| 849 | } |
| 850 | #[cfg(not(target_feature = "fma"))] |
| 851 | Self::new( |
| 852 | math::mul_add(self.x, a.x, b.x), |
| 853 | math::mul_add(self.y, a.y, b.y), |
| 854 | math::mul_add(self.z, a.z, b.z), |
| 855 | math::mul_add(self.w, a.w, b.w), |
| 856 | ) |
| 857 | } |
| 858 | |
| 859 | /// Casts all elements of `self` to `f64`. |
| 860 | #[inline] |
| 861 | #[must_use] |
| 862 | pub fn as_dvec4(&self) -> crate::DVec4 { |
| 863 | crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64) |
| 864 | } |
| 865 | |
| 866 | /// Casts all elements of `self` to `i16`. |
| 867 | #[inline] |
| 868 | #[must_use] |
| 869 | pub fn as_i16vec4(&self) -> crate::I16Vec4 { |
| 870 | crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16) |
| 871 | } |
| 872 | |
| 873 | /// Casts all elements of `self` to `u16`. |
| 874 | #[inline] |
| 875 | #[must_use] |
| 876 | pub fn as_u16vec4(&self) -> crate::U16Vec4 { |
| 877 | crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16) |
| 878 | } |
| 879 | |
| 880 | /// Casts all elements of `self` to `i32`. |
| 881 | #[inline] |
| 882 | #[must_use] |
| 883 | pub fn as_ivec4(&self) -> crate::IVec4 { |
| 884 | crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32) |
| 885 | } |
| 886 | |
| 887 | /// Casts all elements of `self` to `u32`. |
| 888 | #[inline] |
| 889 | #[must_use] |
| 890 | pub fn as_uvec4(&self) -> crate::UVec4 { |
| 891 | crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32) |
| 892 | } |
| 893 | |
| 894 | /// Casts all elements of `self` to `i64`. |
| 895 | #[inline] |
| 896 | #[must_use] |
| 897 | pub fn as_i64vec4(&self) -> crate::I64Vec4 { |
| 898 | crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64) |
| 899 | } |
| 900 | |
| 901 | /// Casts all elements of `self` to `u64`. |
| 902 | #[inline] |
| 903 | #[must_use] |
| 904 | pub fn as_u64vec4(&self) -> crate::U64Vec4 { |
| 905 | crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64) |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | impl Default for Vec4 { |
| 910 | #[inline(always)] |
| 911 | fn default() -> Self { |
| 912 | Self::ZERO |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | impl PartialEq for Vec4 { |
| 917 | #[inline] |
| 918 | fn eq(&self, rhs: &Self) -> bool { |
| 919 | self.cmpeq(*rhs).all() |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | impl Div<Vec4> for Vec4 { |
| 924 | type Output = Self; |
| 925 | #[inline] |
| 926 | fn div(self, rhs: Self) -> Self { |
| 927 | Self(unsafe { _mm_div_ps(self.0, rhs.0) }) |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | impl DivAssign<Vec4> for Vec4 { |
| 932 | #[inline] |
| 933 | fn div_assign(&mut self, rhs: Self) { |
| 934 | self.0 = unsafe { _mm_div_ps(self.0, rhs.0) }; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | impl Div<f32> for Vec4 { |
| 939 | type Output = Self; |
| 940 | #[inline] |
| 941 | fn div(self, rhs: f32) -> Self { |
| 942 | Self(unsafe { _mm_div_ps(self.0, _mm_set1_ps(rhs)) }) |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | impl DivAssign<f32> for Vec4 { |
| 947 | #[inline] |
| 948 | fn div_assign(&mut self, rhs: f32) { |
| 949 | self.0 = unsafe { _mm_div_ps(self.0, _mm_set1_ps(rhs)) }; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | impl Div<Vec4> for f32 { |
| 954 | type Output = Vec4; |
| 955 | #[inline] |
| 956 | fn div(self, rhs: Vec4) -> Vec4 { |
| 957 | Vec4(unsafe { _mm_div_ps(_mm_set1_ps(self), rhs.0) }) |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | impl Mul<Vec4> for Vec4 { |
| 962 | type Output = Self; |
| 963 | #[inline] |
| 964 | fn mul(self, rhs: Self) -> Self { |
| 965 | Self(unsafe { _mm_mul_ps(self.0, rhs.0) }) |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | impl MulAssign<Vec4> for Vec4 { |
| 970 | #[inline] |
| 971 | fn mul_assign(&mut self, rhs: Self) { |
| 972 | self.0 = unsafe { _mm_mul_ps(self.0, rhs.0) }; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | impl Mul<f32> for Vec4 { |
| 977 | type Output = Self; |
| 978 | #[inline] |
| 979 | fn mul(self, rhs: f32) -> Self { |
| 980 | Self(unsafe { _mm_mul_ps(self.0, _mm_set1_ps(rhs)) }) |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | impl MulAssign<f32> for Vec4 { |
| 985 | #[inline] |
| 986 | fn mul_assign(&mut self, rhs: f32) { |
| 987 | self.0 = unsafe { _mm_mul_ps(self.0, _mm_set1_ps(rhs)) }; |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | impl Mul<Vec4> for f32 { |
| 992 | type Output = Vec4; |
| 993 | #[inline] |
| 994 | fn mul(self, rhs: Vec4) -> Vec4 { |
| 995 | Vec4(unsafe { _mm_mul_ps(_mm_set1_ps(self), rhs.0) }) |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | impl Add<Vec4> for Vec4 { |
| 1000 | type Output = Self; |
| 1001 | #[inline] |
| 1002 | fn add(self, rhs: Self) -> Self { |
| 1003 | Self(unsafe { _mm_add_ps(self.0, rhs.0) }) |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | impl AddAssign<Vec4> for Vec4 { |
| 1008 | #[inline] |
| 1009 | fn add_assign(&mut self, rhs: Self) { |
| 1010 | self.0 = unsafe { _mm_add_ps(self.0, rhs.0) }; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | impl Add<f32> for Vec4 { |
| 1015 | type Output = Self; |
| 1016 | #[inline] |
| 1017 | fn add(self, rhs: f32) -> Self { |
| 1018 | Self(unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) }) |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | impl AddAssign<f32> for Vec4 { |
| 1023 | #[inline] |
| 1024 | fn add_assign(&mut self, rhs: f32) { |
| 1025 | self.0 = unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) }; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | impl Add<Vec4> for f32 { |
| 1030 | type Output = Vec4; |
| 1031 | #[inline] |
| 1032 | fn add(self, rhs: Vec4) -> Vec4 { |
| 1033 | Vec4(unsafe { _mm_add_ps(_mm_set1_ps(self), rhs.0) }) |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | impl Sub<Vec4> for Vec4 { |
| 1038 | type Output = Self; |
| 1039 | #[inline] |
| 1040 | fn sub(self, rhs: Self) -> Self { |
| 1041 | Self(unsafe { _mm_sub_ps(self.0, rhs.0) }) |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | impl SubAssign<Vec4> for Vec4 { |
| 1046 | #[inline] |
| 1047 | fn sub_assign(&mut self, rhs: Vec4) { |
| 1048 | self.0 = unsafe { _mm_sub_ps(self.0, rhs.0) }; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | impl Sub<f32> for Vec4 { |
| 1053 | type Output = Self; |
| 1054 | #[inline] |
| 1055 | fn sub(self, rhs: f32) -> Self { |
| 1056 | Self(unsafe { _mm_sub_ps(self.0, _mm_set1_ps(rhs)) }) |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | impl SubAssign<f32> for Vec4 { |
| 1061 | #[inline] |
| 1062 | fn sub_assign(&mut self, rhs: f32) { |
| 1063 | self.0 = unsafe { _mm_sub_ps(self.0, _mm_set1_ps(rhs)) }; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | impl Sub<Vec4> for f32 { |
| 1068 | type Output = Vec4; |
| 1069 | #[inline] |
| 1070 | fn sub(self, rhs: Vec4) -> Vec4 { |
| 1071 | Vec4(unsafe { _mm_sub_ps(_mm_set1_ps(self), rhs.0) }) |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | impl Rem<Vec4> for Vec4 { |
| 1076 | type Output = Self; |
| 1077 | #[inline] |
| 1078 | fn rem(self, rhs: Self) -> Self { |
| 1079 | unsafe { |
| 1080 | let n = m128_floor(_mm_div_ps(self.0, rhs.0)); |
| 1081 | Self(_mm_sub_ps(self.0, _mm_mul_ps(n, rhs.0))) |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | impl RemAssign<Vec4> for Vec4 { |
| 1087 | #[inline] |
| 1088 | fn rem_assign(&mut self, rhs: Self) { |
| 1089 | *self = self.rem(rhs); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | impl Rem<f32> for Vec4 { |
| 1094 | type Output = Self; |
| 1095 | #[inline] |
| 1096 | fn rem(self, rhs: f32) -> Self { |
| 1097 | self.rem(Self::splat(rhs)) |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | impl RemAssign<f32> for Vec4 { |
| 1102 | #[inline] |
| 1103 | fn rem_assign(&mut self, rhs: f32) { |
| 1104 | *self = self.rem(Self::splat(rhs)); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | impl Rem<Vec4> for f32 { |
| 1109 | type Output = Vec4; |
| 1110 | #[inline] |
| 1111 | fn rem(self, rhs: Vec4) -> Vec4 { |
| 1112 | Vec4::splat(self).rem(rhs) |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | #[cfg(not(target_arch = "spirv"))] |
| 1117 | impl AsRef<[f32; 4]> for Vec4 { |
| 1118 | #[inline] |
| 1119 | fn as_ref(&self) -> &[f32; 4] { |
| 1120 | unsafe { &*(self as *const Vec4 as *const [f32; 4]) } |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | #[cfg(not(target_arch = "spirv"))] |
| 1125 | impl AsMut<[f32; 4]> for Vec4 { |
| 1126 | #[inline] |
| 1127 | fn as_mut(&mut self) -> &mut [f32; 4] { |
| 1128 | unsafe { &mut *(self as *mut Vec4 as *mut [f32; 4]) } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | impl Sum for Vec4 { |
| 1133 | #[inline] |
| 1134 | fn sum<I>(iter: I) -> Self |
| 1135 | where |
| 1136 | I: Iterator<Item = Self>, |
| 1137 | { |
| 1138 | iter.fold(Self::ZERO, Self::add) |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | impl<'a> Sum<&'a Self> for Vec4 { |
| 1143 | #[inline] |
| 1144 | fn sum<I>(iter: I) -> Self |
| 1145 | where |
| 1146 | I: Iterator<Item = &'a Self>, |
| 1147 | { |
| 1148 | iter.fold(Self::ZERO, |a, &b| Self::add(a, b)) |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | impl Product for Vec4 { |
| 1153 | #[inline] |
| 1154 | fn product<I>(iter: I) -> Self |
| 1155 | where |
| 1156 | I: Iterator<Item = Self>, |
| 1157 | { |
| 1158 | iter.fold(Self::ONE, Self::mul) |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | impl<'a> Product<&'a Self> for Vec4 { |
| 1163 | #[inline] |
| 1164 | fn product<I>(iter: I) -> Self |
| 1165 | where |
| 1166 | I: Iterator<Item = &'a Self>, |
| 1167 | { |
| 1168 | iter.fold(Self::ONE, |a, &b| Self::mul(a, b)) |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | impl Neg for Vec4 { |
| 1173 | type Output = Self; |
| 1174 | #[inline] |
| 1175 | fn neg(self) -> Self { |
| 1176 | Self(unsafe { _mm_xor_ps(_mm_set1_ps(-0.0), self.0) }) |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | impl Index<usize> for Vec4 { |
| 1181 | type Output = f32; |
| 1182 | #[inline] |
| 1183 | fn index(&self, index: usize) -> &Self::Output { |
| 1184 | match index { |
| 1185 | 0 => &self.x, |
| 1186 | 1 => &self.y, |
| 1187 | 2 => &self.z, |
| 1188 | 3 => &self.w, |
| 1189 | _ => panic!("index out of bounds"), |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | impl IndexMut<usize> for Vec4 { |
| 1195 | #[inline] |
| 1196 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 1197 | match index { |
| 1198 | 0 => &mut self.x, |
| 1199 | 1 => &mut self.y, |
| 1200 | 2 => &mut self.z, |
| 1201 | 3 => &mut self.w, |
| 1202 | _ => panic!("index out of bounds"), |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | #[cfg(not(target_arch = "spirv"))] |
| 1208 | impl fmt::Display for Vec4 { |
| 1209 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1210 | if let Some(p) = f.precision() { |
| 1211 | write!( |
| 1212 | f, |
| 1213 | "[{:.*}, {:.*}, {:.*}, {:.*}]", |
| 1214 | p, self.x, p, self.y, p, self.z, p, self.w |
| 1215 | ) |
| 1216 | } else { |
| 1217 | write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w) |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | #[cfg(not(target_arch = "spirv"))] |
| 1223 | impl fmt::Debug for Vec4 { |
| 1224 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1225 | fmt.debug_tuple(stringify!(Vec4)) |
| 1226 | .field(&self.x) |
| 1227 | .field(&self.y) |
| 1228 | .field(&self.z) |
| 1229 | .field(&self.w) |
| 1230 | .finish() |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | impl From<Vec4> for __m128 { |
| 1235 | #[inline(always)] |
| 1236 | fn from(t: Vec4) -> Self { |
| 1237 | t.0 |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | impl From<__m128> for Vec4 { |
| 1242 | #[inline(always)] |
| 1243 | fn from(t: __m128) -> Self { |
| 1244 | Self(t) |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | impl From<[f32; 4]> for Vec4 { |
| 1249 | #[inline] |
| 1250 | fn from(a: [f32; 4]) -> Self { |
| 1251 | Self(unsafe { _mm_loadu_ps(a.as_ptr()) }) |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | impl From<Vec4> for [f32; 4] { |
| 1256 | #[inline] |
| 1257 | fn from(v: Vec4) -> Self { |
| 1258 | use crate::Align16; |
| 1259 | use core::mem::MaybeUninit; |
| 1260 | let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit(); |
| 1261 | unsafe { |
| 1262 | _mm_store_ps(out.as_mut_ptr().cast(), v.0); |
| 1263 | out.assume_init().0 |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | impl From<(f32, f32, f32, f32)> for Vec4 { |
| 1269 | #[inline] |
| 1270 | fn from(t: (f32, f32, f32, f32)) -> Self { |
| 1271 | Self::new(t.0, t.1, t.2, t.3) |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | impl From<Vec4> for (f32, f32, f32, f32) { |
| 1276 | #[inline] |
| 1277 | fn from(v: Vec4) -> Self { |
| 1278 | use crate::Align16; |
| 1279 | use core::mem::MaybeUninit; |
| 1280 | let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit(); |
| 1281 | unsafe { |
| 1282 | _mm_store_ps(out.as_mut_ptr().cast(), v.0); |
| 1283 | out.assume_init().0 |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | impl From<(Vec3A, f32)> for Vec4 { |
| 1289 | #[inline] |
| 1290 | fn from((v, w): (Vec3A, f32)) -> Self { |
| 1291 | v.extend(w) |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | impl From<(f32, Vec3A)> for Vec4 { |
| 1296 | #[inline] |
| 1297 | fn from((x, v): (f32, Vec3A)) -> Self { |
| 1298 | Self::new(x, v.x, v.y, v.z) |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | impl From<(Vec3, f32)> for Vec4 { |
| 1303 | #[inline] |
| 1304 | fn from((v, w): (Vec3, f32)) -> Self { |
| 1305 | Self::new(v.x, v.y, v.z, w) |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | impl From<(f32, Vec3)> for Vec4 { |
| 1310 | #[inline] |
| 1311 | fn from((x, v): (f32, Vec3)) -> Self { |
| 1312 | Self::new(x, v.x, v.y, v.z) |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | impl From<(Vec2, f32, f32)> for Vec4 { |
| 1317 | #[inline] |
| 1318 | fn from((v, z, w): (Vec2, f32, f32)) -> Self { |
| 1319 | Self::new(v.x, v.y, z, w) |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | impl From<(Vec2, Vec2)> for Vec4 { |
| 1324 | #[inline] |
| 1325 | fn from((v, u): (Vec2, Vec2)) -> Self { |
| 1326 | Self::new(v.x, v.y, u.x, u.y) |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | impl Deref for Vec4 { |
| 1331 | type Target = crate::deref::Vec4<f32>; |
| 1332 | #[inline] |
| 1333 | fn deref(&self) -> &Self::Target { |
| 1334 | unsafe { &*(self as *const Self).cast() } |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | impl DerefMut for Vec4 { |
| 1339 | #[inline] |
| 1340 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 1341 | unsafe { &mut *(self as *mut Self).cast() } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | impl From<BVec4> for Vec4 { |
| 1346 | #[inline] |
| 1347 | fn from(v: BVec4) -> Self { |
| 1348 | Self::new( |
| 1349 | f32::from(v.x), |
| 1350 | f32::from(v.y), |
| 1351 | f32::from(v.z), |
| 1352 | f32::from(v.w), |
| 1353 | ) |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | #[cfg(not(feature = "scalar-math"))] |
| 1358 | |
| 1359 | impl From<BVec4A> for Vec4 { |
| 1360 | #[inline] |
| 1361 | fn from(v: BVec4A) -> Self { |
| 1362 | let bool_array: [bool; 4] = v.into(); |
| 1363 | Self::new( |
| 1364 | f32::from(bool_array[0]), |
| 1365 | f32::from(bool_array[1]), |
| 1366 | f32::from(bool_array[2]), |
| 1367 | f32::from(bool_array[3]), |
| 1368 | ) |
| 1369 | } |
| 1370 | } |