Debian Codegraph @

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