Skip to content

Commit cbeaf16

Browse files
gnzlbgalexcrichton
authored andcommitted
add vector/scalar ops (#381)
1 parent 8a0889c commit cbeaf16

File tree

6 files changed

+639
-106
lines changed

6 files changed

+639
-106
lines changed
+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
//! Lane-wise arithmetic operations.
2+
#![allow(unused)]
3+
4+
macro_rules! impl_arithmetic_scalar_ops {
5+
($id:ident, $elem_ty:ident) => {
6+
impl ::ops::Add<$elem_ty> for $id {
7+
type Output = Self;
8+
#[inline]
9+
fn add(self, other: $elem_ty) -> Self {
10+
self + $id::splat(other)
11+
}
12+
}
13+
impl ::ops::Add<$id> for $elem_ty {
14+
type Output = $id;
15+
#[inline]
16+
fn add(self, other: $id) -> $id {
17+
$id::splat(self) + other
18+
}
19+
}
20+
21+
impl ::ops::Sub<$elem_ty> for $id {
22+
type Output = Self;
23+
#[inline]
24+
fn sub(self, other: $elem_ty) -> Self {
25+
self - $id::splat(other)
26+
}
27+
}
28+
impl ::ops::Sub<$id> for $elem_ty {
29+
type Output = $id;
30+
#[inline]
31+
fn sub(self, other: $id) -> $id {
32+
$id::splat(self) - other
33+
}
34+
}
35+
36+
impl ::ops::Mul<$elem_ty> for $id {
37+
type Output = Self;
38+
#[inline]
39+
fn mul(self, other: $elem_ty) -> Self {
40+
self * $id::splat(other)
41+
}
42+
}
43+
impl ::ops::Mul<$id> for $elem_ty {
44+
type Output = $id;
45+
#[inline]
46+
fn mul(self, other: $id) -> $id {
47+
$id::splat(self) * other
48+
}
49+
}
50+
51+
impl ::ops::Div<$elem_ty> for $id {
52+
type Output = Self;
53+
#[inline]
54+
fn div(self, other: $elem_ty) -> Self {
55+
self / $id::splat(other)
56+
}
57+
}
58+
impl ::ops::Div<$id> for $elem_ty {
59+
type Output = $id;
60+
#[inline]
61+
fn div(self, other: $id) -> $id {
62+
$id::splat(self) / other
63+
}
64+
}
65+
66+
impl ::ops::Rem<$elem_ty> for $id {
67+
type Output = Self;
68+
#[inline]
69+
fn rem(self, other: $elem_ty) -> Self {
70+
self % $id::splat(other)
71+
}
72+
}
73+
impl ::ops::Rem<$id> for $elem_ty {
74+
type Output = $id;
75+
#[inline]
76+
fn rem(self, other: $id) -> $id {
77+
$id::splat(self) % other
78+
}
79+
}
80+
81+
impl ::ops::AddAssign<$elem_ty> for $id {
82+
#[inline]
83+
fn add_assign(&mut self, other: $elem_ty) {
84+
*self = *self + other;
85+
}
86+
}
87+
88+
impl ::ops::SubAssign<$elem_ty> for $id {
89+
#[inline]
90+
fn sub_assign(&mut self, other: $elem_ty) {
91+
*self = *self - other;
92+
}
93+
}
94+
95+
impl ::ops::MulAssign<$elem_ty> for $id {
96+
#[inline]
97+
fn mul_assign(&mut self, other: $elem_ty) {
98+
*self = *self * other;
99+
}
100+
}
101+
102+
impl ::ops::DivAssign<$elem_ty> for $id {
103+
#[inline]
104+
fn div_assign(&mut self, other: $elem_ty) {
105+
*self = *self / other;
106+
}
107+
}
108+
109+
impl ::ops::RemAssign<$elem_ty> for $id {
110+
#[inline]
111+
fn rem_assign(&mut self, other: $elem_ty) {
112+
*self = *self % other;
113+
}
114+
}
115+
}
116+
}
117+
118+
#[cfg(test)]
119+
macro_rules! test_arithmetic_scalar_ops {
120+
($id:ident, $elem_ty:ident) => {
121+
#[test]
122+
fn arithmetic_scalar() {
123+
use ::coresimd::simd::$id;
124+
let zi = 0 as $elem_ty;
125+
let oi = 1 as $elem_ty;
126+
let ti = 2 as $elem_ty;
127+
let fi = 4 as $elem_ty;
128+
let z = $id::splat(zi);
129+
let o = $id::splat(oi);
130+
let t = $id::splat(ti);
131+
let f = $id::splat(fi);
132+
133+
// add
134+
assert_eq!(zi + z, z);
135+
assert_eq!(z + zi, z);
136+
assert_eq!(oi + z, o);
137+
assert_eq!(o + zi, o);
138+
assert_eq!(ti + z, t);
139+
assert_eq!(t + zi, t);
140+
assert_eq!(ti + t, f);
141+
assert_eq!(t + ti, f);
142+
// sub
143+
assert_eq!(zi - z, z);
144+
assert_eq!(z - zi, z);
145+
assert_eq!(oi - z, o);
146+
assert_eq!(o - zi, o);
147+
assert_eq!(ti - z, t);
148+
assert_eq!(t - zi, t);
149+
assert_eq!(fi - t, t);
150+
assert_eq!(f - ti, t);
151+
assert_eq!(f - o - o, t);
152+
assert_eq!(f - oi - oi, t);
153+
// mul
154+
assert_eq!(zi * z, z);
155+
assert_eq!(z * zi, z);
156+
assert_eq!(zi * o, z);
157+
assert_eq!(z * oi, z);
158+
assert_eq!(zi * t, z);
159+
assert_eq!(z * ti, z);
160+
assert_eq!(oi * t, t);
161+
assert_eq!(o * ti, t);
162+
assert_eq!(ti * t, f);
163+
assert_eq!(t * ti, f);
164+
// div
165+
assert_eq!(zi / o, z);
166+
assert_eq!(z / oi, z);
167+
assert_eq!(ti / o, t);
168+
assert_eq!(t / oi, t);
169+
assert_eq!(fi / o, f);
170+
assert_eq!(f / oi, f);
171+
assert_eq!(ti / t, o);
172+
assert_eq!(t / ti, o);
173+
assert_eq!(fi / t, t);
174+
assert_eq!(f / ti, t);
175+
// rem
176+
assert_eq!(oi % o, z);
177+
assert_eq!(o % oi, z);
178+
assert_eq!(fi % t, z);
179+
assert_eq!(f % ti, z);
180+
181+
{
182+
let mut v = z;
183+
assert_eq!(v, z);
184+
v += oi; // add_assign
185+
assert_eq!(v, o);
186+
v -= oi; // sub_assign
187+
assert_eq!(v, z);
188+
v = t;
189+
v *= oi; // mul_assign
190+
assert_eq!(v, t);
191+
v *= ti;
192+
assert_eq!(v, f);
193+
v /= oi; // div_assign
194+
assert_eq!(v, f);
195+
v /= ti;
196+
assert_eq!(v, t);
197+
v %= ti; // rem_assign
198+
assert_eq!(v, z);
199+
}
200+
}
201+
};
202+
}

coresimd/ppsv/api/bitwise_ops.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,51 @@
22
#![allow(unused)]
33

44
macro_rules! impl_bitwise_ops {
5-
($ty:ident, $true_val:expr) => {
6-
impl ::ops::Not for $ty {
5+
($id:ident, $true_val:expr) => {
6+
impl ::ops::Not for $id {
77
type Output = Self;
88
#[inline]
99
fn not(self) -> Self {
1010
Self::splat($true_val) ^ self
1111
}
1212
}
13-
impl ::ops::BitXor for $ty {
13+
impl ::ops::BitXor for $id {
1414
type Output = Self;
1515
#[inline]
1616
fn bitxor(self, other: Self) -> Self {
1717
use coresimd::simd_llvm::simd_xor;
1818
unsafe { simd_xor(self, other) }
1919
}
2020
}
21-
impl ::ops::BitAnd for $ty {
21+
impl ::ops::BitAnd for $id {
2222
type Output = Self;
2323
#[inline]
2424
fn bitand(self, other: Self) -> Self {
2525
use coresimd::simd_llvm::simd_and;
2626
unsafe { simd_and(self, other) }
2727
}
2828
}
29-
impl ::ops::BitOr for $ty {
29+
impl ::ops::BitOr for $id {
3030
type Output = Self;
3131
#[inline]
3232
fn bitor(self, other: Self) -> Self {
3333
use coresimd::simd_llvm::simd_or;
3434
unsafe { simd_or(self, other) }
3535
}
3636
}
37-
impl ::ops::BitAndAssign for $ty {
37+
impl ::ops::BitAndAssign for $id {
3838
#[inline]
3939
fn bitand_assign(&mut self, other: Self) {
4040
*self = *self & other;
4141
}
4242
}
43-
impl ::ops::BitOrAssign for $ty {
43+
impl ::ops::BitOrAssign for $id {
4444
#[inline]
4545
fn bitor_assign(&mut self, other: Self) {
4646
*self = *self | other;
4747
}
4848
}
49-
impl ::ops::BitXorAssign for $ty {
49+
impl ::ops::BitXorAssign for $id {
5050
#[inline]
5151
fn bitxor_assign(&mut self, other: Self) {
5252
*self = *self ^ other;

0 commit comments

Comments
 (0)