Debian Codegraph @

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