Debian Codegraph @

debian/missing-sources/glam-0.24.2/src/f32/wasm32/vec4.rs

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