Debian Codegraph @

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