Debian Codegraph @

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