Debian Codegraph @

debian/missing-sources/glam-0.20.5/src/vec.rs

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