Debian Codegraph @

debian/missing-sources/glam-0.21.3/src/f32/coresimd/vec4.rs

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