|
1 | 1 | //! This module hacks in "implicit deref" for Simd's operators.
|
2 | 2 | //! Ideally, Rust would take care of this itself,
|
3 | 3 | //! and method calls usually handle the LHS implicitly.
|
4 |
| -//! So, we'll manually deref the RHS. |
| 4 | +//! But this is not the case with arithmetic ops. |
5 | 5 | use super::*;
|
6 | 6 |
|
7 |
| -macro_rules! deref_ops { |
8 |
| - ($(impl<T, const LANES: usize> $trait:ident<&Self> for Simd<T, LANES> { |
9 |
| - fn $call:ident(rhs: &Self) |
10 |
| - })*) => { |
11 |
| - $(impl<T, const LANES: usize> $trait<&Self> for Simd<T, LANES> |
| 7 | +macro_rules! deref_lhs { |
| 8 | + (impl<T, const LANES: usize> $trait:ident for $simd:ty { |
| 9 | + fn $call:ident |
| 10 | + }) => { |
| 11 | + impl<T, const LANES: usize> $trait<$simd> for &$simd |
| 12 | + where |
| 13 | + T: SimdElement, |
| 14 | + $simd: $trait<$simd, Output = $simd>, |
| 15 | + LaneCount<LANES>: SupportedLaneCount, |
| 16 | + { |
| 17 | + type Output = Simd<T, LANES>; |
| 18 | + |
| 19 | + #[inline] |
| 20 | + #[must_use = "operator returns a new vector without mutating the inputs"] |
| 21 | + fn $call(self, rhs: $simd) -> Self::Output { |
| 22 | + (*self).$call(rhs) |
| 23 | + } |
| 24 | + } |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +macro_rules! deref_rhs { |
| 29 | + (impl<T, const LANES: usize> $trait:ident for $simd:ty { |
| 30 | + fn $call:ident |
| 31 | + }) => { |
| 32 | + impl<T, const LANES: usize> $trait<&$simd> for $simd |
12 | 33 | where
|
13 |
| - Self: $trait<Self, Output = Self>, |
14 | 34 | T: SimdElement,
|
| 35 | + $simd: $trait<$simd, Output = $simd>, |
15 | 36 | LaneCount<LANES>: SupportedLaneCount,
|
16 | 37 | {
|
17 |
| - type Output = Self; |
| 38 | + type Output = Simd<T, LANES>; |
18 | 39 |
|
19 | 40 | #[inline]
|
20 | 41 | #[must_use = "operator returns a new vector without mutating the inputs"]
|
21 |
| - fn $call(self, rhs: &Self) -> Self::Output { |
| 42 | + fn $call(self, rhs: &$simd) -> Self::Output { |
22 | 43 | self.$call(*rhs)
|
23 | 44 | }
|
24 |
| - })* |
| 45 | + } |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +macro_rules! deref_ops { |
| 50 | + ($(impl<T, const LANES: usize> $trait:ident for $simd:ty { |
| 51 | + fn $call:ident |
| 52 | + })*) => { |
| 53 | + $( |
| 54 | + deref_rhs! { |
| 55 | + impl<T, const LANES: usize> $trait for $simd { |
| 56 | + fn $call |
| 57 | + } |
| 58 | + } |
| 59 | + deref_lhs! { |
| 60 | + impl<T, const LANES: usize> $trait for $simd { |
| 61 | + fn $call |
| 62 | + } |
| 63 | + } |
| 64 | + impl<'lhs, 'rhs, T, const LANES: usize> $trait<&'rhs $simd> for &'lhs $simd |
| 65 | + where |
| 66 | + T: SimdElement, |
| 67 | + $simd: $trait<$simd, Output = $simd>, |
| 68 | + LaneCount<LANES>: SupportedLaneCount, |
| 69 | + { |
| 70 | + type Output = $simd; |
| 71 | + |
| 72 | + #[inline] |
| 73 | + #[must_use = "operator returns a new vector without mutating the inputs"] |
| 74 | + fn $call(self, rhs: &$simd) -> Self::Output { |
| 75 | + (*self).$call(*rhs) |
| 76 | + } |
| 77 | + } |
| 78 | + )* |
25 | 79 | }
|
26 | 80 | }
|
27 | 81 |
|
28 | 82 | deref_ops! {
|
29 | 83 | // Arithmetic
|
30 |
| - impl<T, const LANES: usize> Add<&Self> for Simd<T, LANES> { |
31 |
| - fn add(rhs: &Self) |
| 84 | + impl<T, const LANES: usize> Add for Simd<T, LANES> { |
| 85 | + fn add |
32 | 86 | }
|
33 | 87 |
|
34 |
| - impl<T, const LANES: usize> Mul<&Self> for Simd<T, LANES> { |
35 |
| - fn mul(rhs: &Self) |
| 88 | + impl<T, const LANES: usize> Mul for Simd<T, LANES> { |
| 89 | + fn mul |
36 | 90 | }
|
37 | 91 |
|
38 |
| - impl<T, const LANES: usize> Sub<&Self> for Simd<T, LANES> { |
39 |
| - fn sub(rhs: &Self) |
| 92 | + impl<T, const LANES: usize> Sub for Simd<T, LANES> { |
| 93 | + fn sub |
40 | 94 | }
|
41 | 95 |
|
42 |
| - impl<T, const LANES: usize> Div<&Self> for Simd<T, LANES> { |
43 |
| - fn div(rhs: &Self) |
| 96 | + impl<T, const LANES: usize> Div for Simd<T, LANES> { |
| 97 | + fn div |
44 | 98 | }
|
45 | 99 |
|
46 |
| - impl<T, const LANES: usize> Rem<&Self> for Simd<T, LANES> { |
47 |
| - fn rem(rhs: &Self) |
| 100 | + impl<T, const LANES: usize> Rem for Simd<T, LANES> { |
| 101 | + fn rem |
48 | 102 | }
|
49 | 103 |
|
50 | 104 | // Bitops
|
51 |
| - impl<T, const LANES: usize> BitAnd<&Self> for Simd<T, LANES> { |
52 |
| - fn bitand(rhs: &Self) |
| 105 | + impl<T, const LANES: usize> BitAnd for Simd<T, LANES> { |
| 106 | + fn bitand |
53 | 107 | }
|
54 | 108 |
|
55 |
| - impl<T, const LANES: usize> BitOr<&Self> for Simd<T, LANES> { |
56 |
| - fn bitor(rhs: &Self) |
| 109 | + impl<T, const LANES: usize> BitOr for Simd<T, LANES> { |
| 110 | + fn bitor |
57 | 111 | }
|
58 | 112 |
|
59 |
| - impl<T, const LANES: usize> BitXor<&Self> for Simd<T, LANES> { |
60 |
| - fn bitxor(rhs: &Self) |
| 113 | + impl<T, const LANES: usize> BitXor for Simd<T, LANES> { |
| 114 | + fn bitxor |
61 | 115 | }
|
62 | 116 |
|
63 |
| - impl<T, const LANES: usize> Shl<&Self> for Simd<T, LANES> { |
64 |
| - fn shl(rhs: &Self) |
| 117 | + impl<T, const LANES: usize> Shl for Simd<T, LANES> { |
| 118 | + fn shl |
65 | 119 | }
|
66 | 120 |
|
67 |
| - impl<T, const LANES: usize> Shr<&Self> for Simd<T, LANES> { |
68 |
| - fn shr(rhs: &Self) |
| 121 | + impl<T, const LANES: usize> Shr for Simd<T, LANES> { |
| 122 | + fn shr |
69 | 123 | }
|
70 | 124 | }
|
0 commit comments