Skip to content

Commit 2373a85

Browse files
committed
add vector/scalar ops
1 parent c5cf3bc commit 2373a85

File tree

3 files changed

+97
-106
lines changed

3 files changed

+97
-106
lines changed

coresimd/ppsv/api/bitwise_ops.rs

Lines changed: 8 additions & 8 deletions
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;

coresimd/ppsv/api/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ macro_rules! define_ty {
6969
#[macro_use]
7070
mod arithmetic_ops;
7171
#[macro_use]
72+
mod arithmetic_scalar_ops;
73+
#[macro_use]
7274
mod arithmetic_reductions;
7375
#[macro_use]
7476
mod bitwise_ops;
7577
#[macro_use]
78+
mod bitwise_scalar_ops;
79+
#[macro_use]
7680
mod bitwise_reductions;
7781
#[macro_use]
7882
mod boolean_reductions;
@@ -112,6 +116,8 @@ mod partial_eq;
112116
//#[macro_use]
113117
//mod gather_scatter;
114118
#[macro_use]
119+
mod scalar_shifts;
120+
#[macro_use]
115121
mod shifts;
116122

117123
/// Defines a portable packed SIMD floating-point vector type.
@@ -124,6 +130,7 @@ macro_rules! simd_f_ty {
124130
[impl_load_store, $id, $elem_ty, $elem_count],
125131
[impl_cmp, $id, $bool_ty],
126132
[impl_arithmetic_ops, $id],
133+
[impl_arithmetic_scalar_ops, $id, $elem_ty],
127134
[impl_arithmetic_reductions, $id, $elem_ty],
128135
[impl_minmax_reductions, $id, $elem_ty],
129136
[impl_neg_op, $id, $elem_ty],
@@ -138,6 +145,7 @@ macro_rules! simd_f_ty {
138145
test_load_store!($id, $elem_ty);
139146
test_cmp!($id, $elem_ty, $bool_ty, 1. as $elem_ty, 0. as $elem_ty);
140147
test_arithmetic_ops!($id, $elem_ty);
148+
test_arithmetic_scalar_ops!($id, $elem_ty);
141149
test_arithmetic_reductions!($id, $elem_ty);
142150
test_minmax_reductions!($id, $elem_ty);
143151
test_neg_op!($id, $elem_ty);
@@ -159,12 +167,15 @@ macro_rules! simd_i_ty {
159167
[impl_cmp, $id, $bool_ty],
160168
[impl_hash, $id, $elem_ty],
161169
[impl_arithmetic_ops, $id],
170+
[impl_arithmetic_scalar_ops, $id, $elem_ty],
162171
[impl_arithmetic_reductions, $id, $elem_ty],
163172
[impl_minmax_reductions, $id, $elem_ty],
164173
[impl_neg_op, $id, $elem_ty],
165174
[impl_bitwise_ops, $id, !(0 as $elem_ty)],
175+
[impl_bitwise_scalar_ops, $id, $elem_ty],
166176
[impl_bitwise_reductions, $id, $elem_ty],
167-
[impl_all_shifts, $id, $elem_ty],
177+
[impl_all_scalar_shifts, $id, $elem_ty],
178+
[impl_vector_shifts, $id, $elem_ty],
168179
[impl_hex_fmt, $id, $elem_ty],
169180
[impl_eq, $id],
170181
[impl_partial_eq, $id],
@@ -179,12 +190,15 @@ macro_rules! simd_i_ty {
179190
test_cmp!($id, $elem_ty, $bool_ty, 1 as $elem_ty, 0 as $elem_ty);
180191
test_hash!($id, $elem_ty);
181192
test_arithmetic_ops!($id, $elem_ty);
193+
test_arithmetic_scalar_ops!($id, $elem_ty);
182194
test_arithmetic_reductions!($id, $elem_ty);
183195
test_minmax_reductions!($id, $elem_ty);
184196
test_neg_op!($id, $elem_ty);
185197
test_int_bitwise_ops!($id, $elem_ty);
198+
test_int_bitwise_scalar_ops!($id, $elem_ty);
186199
test_bitwise_reductions!($id, !(0 as $elem_ty));
187-
test_all_shift_ops!($id, $elem_ty);
200+
test_all_scalar_shift_ops!($id, $elem_ty);
201+
test_vector_shift_ops!($id, $elem_ty);
188202
test_hex_fmt!($id, $elem_ty);
189203
test_partial_eq!($id, 1 as $elem_ty, 0 as $elem_ty);
190204
test_default!($id, $elem_ty);
@@ -204,11 +218,14 @@ macro_rules! simd_u_ty {
204218
[impl_cmp, $id, $bool_ty],
205219
[impl_hash, $id, $elem_ty],
206220
[impl_arithmetic_ops, $id],
221+
[impl_arithmetic_scalar_ops, $id, $elem_ty],
207222
[impl_arithmetic_reductions, $id, $elem_ty],
208223
[impl_minmax_reductions, $id, $elem_ty],
224+
[impl_bitwise_scalar_ops, $id, $elem_ty],
209225
[impl_bitwise_ops, $id, !(0 as $elem_ty)],
210226
[impl_bitwise_reductions, $id, $elem_ty],
211-
[impl_all_shifts, $id, $elem_ty],
227+
[impl_all_scalar_shifts, $id, $elem_ty],
228+
[impl_vector_shifts, $id, $elem_ty],
212229
[impl_hex_fmt, $id, $elem_ty],
213230
[impl_eq, $id],
214231
[impl_partial_eq, $id],
@@ -223,11 +240,14 @@ macro_rules! simd_u_ty {
223240
test_cmp!($id, $elem_ty, $bool_ty, 1 as $elem_ty, 0 as $elem_ty);
224241
test_hash!($id, $elem_ty);
225242
test_arithmetic_ops!($id, $elem_ty);
243+
test_arithmetic_scalar_ops!($id, $elem_ty);
226244
test_arithmetic_reductions!($id, $elem_ty);
227245
test_minmax_reductions!($id, $elem_ty);
228246
test_int_bitwise_ops!($id, $elem_ty);
247+
test_int_bitwise_scalar_ops!($id, $elem_ty);
229248
test_bitwise_reductions!($id, !(0 as $elem_ty));
230-
test_all_shift_ops!($id, $elem_ty);
249+
test_all_scalar_shift_ops!($id, $elem_ty);
250+
test_vector_shift_ops!($id, $elem_ty);
231251
test_hex_fmt!($id, $elem_ty);
232252
test_partial_eq!($id, 1 as $elem_ty, 0 as $elem_ty);
233253
test_default!($id, $elem_ty);
@@ -244,6 +264,7 @@ macro_rules! simd_b_ty {
244264
[define_ty, $id, $($elem_tys),+ | $(#[$doc])*],
245265
[impl_bool_minimal, $id, $elem_ty, $elem_count, $($elem_name),*],
246266
[impl_bitwise_ops, $id, true],
267+
[impl_bitwise_scalar_ops, $id, bool],
247268
[impl_bool_bitwise_reductions, $id, bool, $elem_ty],
248269
[impl_bool_reductions, $id],
249270
[impl_bool_cmp, $id, $id],
@@ -257,6 +278,7 @@ macro_rules! simd_b_ty {
257278
mod $test_mod {
258279
test_bool_minimal!($id, $elem_count);
259280
test_bool_bitwise_ops!($id);
281+
test_bool_bitwise_scalar_ops!($id);
260282
test_bool_reductions!($id);
261283
test_bitwise_reductions!($id, true);
262284
test_cmp!($id, $elem_ty, $id, true, false);

coresimd/ppsv/api/shifts.rs

Lines changed: 63 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,42 @@
11
//! Implements integer shifts.
22
#![allow(unused)]
33

4-
macro_rules! impl_shifts {
5-
($id:ident, $elem_ty:ident, $($by:ident),+) => {
6-
$(
7-
impl ::ops::Shl<$by> for $id {
8-
type Output = Self;
9-
#[inline]
10-
fn shl(self, other: $by) -> Self {
11-
use coresimd::simd_llvm::simd_shl;
12-
unsafe { simd_shl(self, $id::splat(other as $elem_ty)) }
13-
}
14-
}
15-
impl ::ops::Shr<$by> for $id {
16-
type Output = Self;
17-
#[inline]
18-
fn shr(self, other: $by) -> Self {
19-
use coresimd::simd_llvm::simd_shr;
20-
unsafe { simd_shr(self, $id::splat(other as $elem_ty)) }
4+
macro_rules! impl_vector_shifts {
5+
($id:ident, $elem_ty:ident) => {
6+
impl ::ops::Shl<$id> for $id {
7+
type Output = Self;
8+
#[inline]
9+
fn shl(self, other: Self) -> Self {
10+
use coresimd::simd_llvm::simd_shl;
11+
unsafe { simd_shl(self, other) }
2112
}
13+
}
14+
impl ::ops::Shr<$id> for $id {
15+
type Output = Self;
16+
#[inline]
17+
fn shr(self, other: Self) -> Self {
18+
use coresimd::simd_llvm::simd_shr;
19+
unsafe { simd_shr(self, other) }
2220
}
23-
24-
impl ::ops::ShlAssign<$by> for $id {
25-
#[inline]
26-
fn shl_assign(&mut self, other: $by) {
27-
*self = *self << other;
28-
}
21+
}
22+
impl ::ops::ShlAssign<$id> for $id {
23+
#[inline]
24+
fn shl_assign(&mut self, other: Self) {
25+
*self = *self << other;
2926
}
30-
impl ::ops::ShrAssign<$by> for $id {
31-
#[inline]
32-
fn shr_assign(&mut self, other: $by) {
33-
*self = *self >> other;
34-
}
27+
}
28+
impl ::ops::ShrAssign<$id> for $id {
29+
#[inline]
30+
fn shr_assign(&mut self, other: Self) {
31+
*self = *self >> other;
3532
}
36-
37-
)+
38-
}
39-
}
40-
41-
macro_rules! impl_all_shifts {
42-
($id:ident, $elem_ty:ident) => {
43-
impl_shifts!(
44-
$id, $elem_ty,
45-
u8, u16, u32, u64, usize,
46-
i8, i16, i32, i64, isize);
47-
33+
}
4834
}
4935
}
5036

5137
#[cfg(test)]
52-
macro_rules! test_shift_ops {
53-
($id:ident, $elem_ty:ident, $($index_ty:ident),+) => {
38+
macro_rules! test_vector_shift_ops {
39+
($id:ident, $elem_ty:ident) => {
5440
#[test]
5541
fn shift_ops() {
5642
use ::coresimd::simd::$id;
@@ -60,64 +46,47 @@ macro_rules! test_shift_ops {
6046
let t = $id::splat(2 as $elem_ty);
6147
let f = $id::splat(4 as $elem_ty);
6248

63-
$(
64-
{
65-
let zi = 0 as $index_ty;
66-
let oi = 1 as $index_ty;
67-
let ti = 2 as $index_ty;
68-
let maxi = (mem::size_of::<$elem_ty>() * 8 - 1) as $index_ty;
49+
let max = $id::splat((mem::size_of::<$elem_ty>() * 8 - 1) as $elem_ty);
6950

70-
// shr
71-
assert_eq!(z >> zi, z);
72-
assert_eq!(z >> oi, z);
73-
assert_eq!(z >> ti, z);
74-
assert_eq!(z >> ti, z);
51+
// shr
52+
assert_eq!(z >> z, z);
53+
assert_eq!(z >> o, z);
54+
assert_eq!(z >> t, z);
55+
assert_eq!(z >> t, z);
7556

76-
assert_eq!(o >> zi, o);
77-
assert_eq!(t >> zi, t);
78-
assert_eq!(f >> zi, f);
79-
assert_eq!(f >> maxi, z);
57+
assert_eq!(o >> z, o);
58+
assert_eq!(t >> z, t);
59+
assert_eq!(f >> z, f);
60+
assert_eq!(f >> max, z);
8061

81-
assert_eq!(o >> oi, z);
82-
assert_eq!(t >> oi, o);
83-
assert_eq!(t >> ti, z);
84-
assert_eq!(f >> oi, t);
85-
assert_eq!(f >> ti, o);
86-
assert_eq!(f >> maxi, z);
62+
assert_eq!(o >> o, z);
63+
assert_eq!(t >> o, o);
64+
assert_eq!(t >> t, z);
65+
assert_eq!(f >> o, t);
66+
assert_eq!(f >> t, o);
67+
assert_eq!(f >> max, z);
8768

88-
// shl
89-
assert_eq!(z << zi, z);
90-
assert_eq!(o << zi, o);
91-
assert_eq!(t << zi, t);
92-
assert_eq!(f << zi, f);
93-
assert_eq!(f << maxi, z);
69+
// shl
70+
assert_eq!(z << z, z);
71+
assert_eq!(o << z, o);
72+
assert_eq!(t << z, t);
73+
assert_eq!(f << z, f);
74+
assert_eq!(f << max, z);
9475

95-
assert_eq!(o << oi, t);
96-
assert_eq!(o << ti, f);
97-
assert_eq!(t << oi, f);
76+
assert_eq!(o << o, t);
77+
assert_eq!(o << t, f);
78+
assert_eq!(t << o, f);
9879

99-
{ // shr_assign
100-
let mut v = o;
101-
v >>= oi;
102-
assert_eq!(v, z);
103-
}
104-
{ // shl_assign
105-
let mut v = o;
106-
v <<= oi;
107-
assert_eq!(v, t);
108-
}
109-
}
110-
)+
80+
{ // shr_assign
81+
let mut v = o;
82+
v >>= o;
83+
assert_eq!(v, z);
84+
}
85+
{ // shl_assign
86+
let mut v = o;
87+
v <<= o;
88+
assert_eq!(v, t);
89+
}
11190
}
11291
};
11392
}
114-
115-
#[cfg(test)]
116-
macro_rules! test_all_shift_ops {
117-
($id:ident, $elem_ty:ident) => {
118-
test_shift_ops!(
119-
$id, $elem_ty,
120-
u8, u16, u32, u64, usize,
121-
i8, i16, i32, i64, isize);
122-
}
123-
}

0 commit comments

Comments
 (0)