Debian Codegraph @

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