Debian Codegraph @

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

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