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