Skip to content

Commit d418b8a

Browse files
u64 -> u32
1 parent e6696fc commit d418b8a

File tree

10 files changed

+205
-201
lines changed

10 files changed

+205
-201
lines changed

testable-simd-models/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ tests work by testing the models against the intrinsics in the Rust
2323
core, trying out random inputs (generally 1000), and comparing their
2424
outputs.
2525

26+
The tests can run by executing `cargo test`.
27+
2628
## Modeling Process
2729
The process of adding a specific intrinsic's model goes as follows.
2830
For this example, let us say the intrinsic we are adding is

testable-simd-models/src/abstractions/bitvec.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::fmt::Formatter;
1515
/// making the bit pattern more human-readable. The type also implements indexing,
1616
/// allowing for easy access to individual bits.
1717
#[derive(Copy, Clone, Eq, PartialEq)]
18-
pub struct BitVec<const N: u64>(FunArray<N, Bit>);
18+
pub struct BitVec<const N: u32>(FunArray<N, Bit>);
1919

2020
/// Pretty prints a bit slice by group of 8
2121
fn bit_slice_to_string(bits: &[Bit]) -> String {
@@ -33,15 +33,15 @@ fn bit_slice_to_string(bits: &[Bit]) -> String {
3333
.into()
3434
}
3535

36-
impl<const N: u64> core::fmt::Debug for BitVec<N> {
36+
impl<const N: u32> core::fmt::Debug for BitVec<N> {
3737
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
3838
write!(f, "{}", bit_slice_to_string(&self.0.as_vec()))
3939
}
4040
}
4141

42-
impl<const N: u64> core::ops::Index<u64> for BitVec<N> {
42+
impl<const N: u32> core::ops::Index<u32> for BitVec<N> {
4343
type Output = Bit;
44-
fn index(&self, index: u64) -> &Self::Output {
44+
fn index(&self, index: u32) -> &Self::Output {
4545
self.0.get(index)
4646
}
4747
}
@@ -75,19 +75,19 @@ fn int_from_bit_slice<T: TryFrom<i128> + MachineInteger + Copy>(bits: &[Bit]) ->
7575
};
7676
n
7777
}
78-
impl<const N: u64> BitVec<N> {
78+
impl<const N: u32> BitVec<N> {
7979
/// Constructor for BitVec. `BitVec::<N>::from_fn` constructs a bitvector out of a function that takes usizes smaller than `N` and produces bits.
80-
pub fn from_fn<F: Fn(u64) -> Bit>(f: F) -> Self {
80+
pub fn from_fn<F: Fn(u32) -> Bit>(f: F) -> Self {
8181
Self(FunArray::from_fn(f))
8282
}
8383
/// Convert a slice of machine integers where only the `d` least significant bits are relevant.
84-
pub fn from_slice<T: Into<i128> + MachineInteger + Copy>(x: &[T], d: u64) -> Self {
84+
pub fn from_slice<T: Into<i128> + MachineInteger + Copy>(x: &[T], d: u32) -> Self {
8585
Self::from_fn(|i| Bit::of_int::<T>(x[(i / d) as usize], (i % d) as u32))
8686
}
8787

8888
/// Construct a BitVec out of a machine integer.
8989
pub fn from_int<T: Into<i128> + MachineInteger + Copy>(n: T) -> Self {
90-
Self::from_slice::<T>(&[n], T::bits() as u64)
90+
Self::from_slice::<T>(&[n], T::bits() as u32)
9191
}
9292

9393
/// Convert a BitVec into a machine integer of type `T`.
@@ -115,12 +115,12 @@ impl<const N: u64> BitVec<N> {
115115
}
116116
}
117117

118-
impl<const N: u64> BitVec<N> {
119-
pub fn chunked_shift<const CHUNK: u64, const SHIFTS: u64>(
118+
impl<const N: u32> BitVec<N> {
119+
pub fn chunked_shift<const CHUNK: u32, const SHIFTS: u32>(
120120
self,
121121
shl: FunArray<SHIFTS, i128>,
122122
) -> BitVec<N> {
123-
fn chunked_shift<const N: u64, const CHUNK: u64, const SHIFTS: u64>(
123+
fn chunked_shift<const N: u32, const CHUNK: u32, const SHIFTS: u32>(
124124
bitvec: BitVec<N>,
125125
shl: FunArray<SHIFTS, i128>,
126126
) -> BitVec<N> {
@@ -134,7 +134,7 @@ impl<const N: u64> BitVec<N> {
134134
};
135135
let local_index = (nth_bit as i128).wrapping_sub(shift);
136136
if local_index < CHUNK as i128 && local_index >= 0 {
137-
let local_index = local_index as u64;
137+
let local_index = local_index as u32;
138138
bitvec[nth_chunk * CHUNK + local_index]
139139
} else {
140140
Bit::Zero

testable-simd-models/src/abstractions/funarr.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
/// Internally, it uses a fixed-length array of `Option<T>` with a maximum capacity of 512 elements.
66
/// Unused elements beyond `N` are filled with `None`.
77
#[derive(Copy, Clone, Eq, PartialEq)]
8-
pub struct FunArray<const N: u64, T>([Option<T>; 512]);
8+
pub struct FunArray<const N: u32, T>([Option<T>; 512]);
99

10-
impl<const N: u64, T> FunArray<N, T> {
10+
impl<const N: u32, T> FunArray<N, T> {
1111
/// Gets a reference to the element at index `i`.
12-
pub fn get(&self, i: u64) -> &T {
12+
pub fn get(&self, i: u32) -> &T {
1313
self.0[i as usize].as_ref().unwrap()
1414
}
1515
/// Constructor for FunArray. `FunArray<N,T>::from_fn` constructs a funarray out of a function that takes usizes smaller than `N` and produces an element of type T.
16-
pub fn from_fn<F: Fn(u64) -> T>(f: F) -> Self {
16+
pub fn from_fn<F: Fn(u32) -> T>(f: F) -> Self {
1717
// let vec = (0..N).map(f).collect();
1818
let arr = core::array::from_fn(|i| {
19-
if (i as u64) < N {
20-
Some(f(i as u64))
19+
if (i as u32) < N {
20+
Some(f(i as u32))
2121
} else {
2222
None
2323
}
@@ -53,27 +53,27 @@ impl<const N: u64, T> FunArray<N, T> {
5353
}
5454
}
5555

56-
impl<const N: u64, T: Clone> TryFrom<Vec<T>> for FunArray<N, T> {
56+
impl<const N: u32, T: Clone> TryFrom<Vec<T>> for FunArray<N, T> {
5757
type Error = ();
5858
fn try_from(v: Vec<T>) -> Result<Self, ()> {
59-
if (v.len() as u64) < N {
59+
if (v.len() as u32) < N {
6060
Err(())
6161
} else {
6262
Ok(Self::from_fn(|i| v[i as usize].clone()))
6363
}
6464
}
6565
}
6666

67-
impl<const N: u64, T: core::fmt::Debug + Clone> core::fmt::Debug for FunArray<N, T> {
67+
impl<const N: u32, T: core::fmt::Debug + Clone> core::fmt::Debug for FunArray<N, T> {
6868
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
6969
write!(f, "{:?}", self.as_vec())
7070
}
7171
}
7272

73-
impl<const N: u64, T> core::ops::Index<u64> for FunArray<N, T> {
73+
impl<const N: u32, T> core::ops::Index<u32> for FunArray<N, T> {
7474
type Output = T;
7575

76-
fn index(&self, index: u64) -> &Self::Output {
76+
fn index(&self, index: u32) -> &Self::Output {
7777
self.get(index)
7878
}
7979
}

testable-simd-models/src/abstractions/simd.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ macro_rules! interpretations {
2020
#[doc = concat!("Conversion from ", stringify!($ty), " vectors of size ", stringify!($m), "to bit vectors of size ", stringify!($n))]
2121
pub fn [< from_ $name >](iv: $name) -> BitVec<$n> {
2222
let vec: Vec<$ty> = iv.as_vec();
23-
Self::from_slice(&vec[..], <$ty>::bits() as u64)
23+
Self::from_slice(&vec[..], <$ty>::bits() as u32)
2424
}
2525
#[doc = concat!("Conversion from bit vectors of size ", stringify!($n), " to ", stringify!($ty), " vectors of size ", stringify!($m))]
2626
pub fn [< to_ $name >](bv: BitVec<$n>) -> $name {
@@ -72,7 +72,7 @@ interpretations!(32; i8x4 [i8; 4], u8x4 [u8; 4]);
7272
///
7373
/// `idx` must be in-bounds of the vector, ie. idx < N
7474
75-
pub fn simd_insert<const N: u64, T: Copy>(x: FunArray<N, T>, idx: u64, val: T) -> FunArray<N, T> {
75+
pub fn simd_insert<const N: u32, T: Copy>(x: FunArray<N, T>, idx: u32, val: T) -> FunArray<N, T> {
7676
FunArray::from_fn(|i| if i == idx { val } else { x[i] })
7777
}
7878

@@ -81,61 +81,61 @@ pub fn simd_insert<const N: u64, T: Copy>(x: FunArray<N, T>, idx: u64, val: T) -
8181
/// # Safety
8282
///
8383
/// `idx` must be in-bounds of the vector, ie. idx < N
84-
pub fn simd_extract<const N: u64, T: Clone>(x: FunArray<N, T>, idx: u64) -> T {
84+
pub fn simd_extract<const N: u32, T: Clone>(x: FunArray<N, T>, idx: u32) -> T {
8585
x.get(idx).clone()
8686
}
8787

8888
/// Adds two vectors elementwise with wrapping on overflow/underflow.
89-
pub fn simd_add<const N: u64, T: MachineInteger + Copy>(
89+
pub fn simd_add<const N: u32, T: MachineInteger + Copy>(
9090
x: FunArray<N, T>,
9191
y: FunArray<N, T>,
9292
) -> FunArray<N, T> {
93-
FunArray::from_fn(|i| (x[i].wrapping_add(y[i])))
93+
FunArray::from_fn(|i| x[i].wrapping_add(y[i]))
9494
}
9595

9696
/// Subtracts `rhs` from `lhs` elementwise with wrapping on overflow/underflow.
97-
pub fn simd_sub<const N: u64, T: MachineInteger + Copy>(
97+
pub fn simd_sub<const N: u32, T: MachineInteger + Copy>(
9898
x: FunArray<N, T>,
9999
y: FunArray<N, T>,
100100
) -> FunArray<N, T> {
101-
FunArray::from_fn(|i| (x[i].wrapping_sub(y[i])))
101+
FunArray::from_fn(|i| x[i].wrapping_sub(y[i]))
102102
}
103103

104104
/// Multiplies two vectors elementwise with wrapping on overflow/underflow.
105-
pub fn simd_mul<const N: u64, T: MachineInteger + Copy>(
105+
pub fn simd_mul<const N: u32, T: MachineInteger + Copy>(
106106
x: FunArray<N, T>,
107107
y: FunArray<N, T>,
108108
) -> FunArray<N, T> {
109-
FunArray::from_fn(|i| (x[i].overflowing_mul(y[i])))
109+
FunArray::from_fn(|i| x[i].overflowing_mul(y[i]))
110110
}
111111

112112
/// Produces the elementwise absolute values.
113113
/// For vectors of unsigned integers it returns the vector untouched.
114114
/// If the element is the minimum value of a signed integer, it returns the element as is.
115-
pub fn simd_abs<const N: u64, T: MachineInteger + Copy>(x: FunArray<N, T>) -> FunArray<N, T> {
115+
pub fn simd_abs<const N: u32, T: MachineInteger + Copy>(x: FunArray<N, T>) -> FunArray<N, T> {
116116
FunArray::from_fn(|i| x[i].absolute_val())
117117
}
118118

119119
/// Produces the elementwise absolute difference of two vectors.
120120
/// Note: Absolute difference in this case is simply the element with the smaller value subtracted from the element with the larger value, with overflow/underflow.
121121
/// For example, if the elements are i8, the absolute difference of 255 and -2 is -255.
122-
pub fn simd_abs_diff<const N: u64, T: MachineInteger + Copy>(
122+
pub fn simd_abs_diff<const N: u32, T: MachineInteger + Copy>(
123123
x: FunArray<N, T>,
124124
y: FunArray<N, T>,
125125
) -> FunArray<N, T> {
126-
FunArray::from_fn(|i| (x[i].absolute_diff(y[i])))
126+
FunArray::from_fn(|i| x[i].absolute_diff(y[i]))
127127
}
128128

129129
/// Shifts vector left elementwise, with UB on overflow.
130130
///
131131
/// # Safety
132132
///
133133
/// Each element of `rhs` must be less than `<int>::BITS`.
134-
pub fn simd_shl<const N: u64, T: Shl + Copy>(
134+
pub fn simd_shl<const N: u32, T: Shl + Copy>(
135135
x: FunArray<N, T>,
136136
y: FunArray<N, T>,
137137
) -> FunArray<N, <T as Shl>::Output> {
138-
FunArray::from_fn(|i| (x[i] << y[i]))
138+
FunArray::from_fn(|i| x[i] << y[i])
139139
}
140140

141141
/// Shifts vector right elementwise, with UB on overflow.
@@ -146,38 +146,38 @@ pub fn simd_shl<const N: u64, T: Shl + Copy>(
146146
///
147147
/// Each element of `rhs` must be less than `<int>::BITS`.
148148
149-
pub fn simd_shr<const N: u64, T: Shr + Copy>(
149+
pub fn simd_shr<const N: u32, T: Shr + Copy>(
150150
x: FunArray<N, T>,
151151
y: FunArray<N, T>,
152152
) -> FunArray<N, <T as Shr>::Output> {
153-
FunArray::from_fn(|i| (x[i] >> y[i]))
153+
FunArray::from_fn(|i| x[i] >> y[i])
154154
}
155155

156156
/// "Ands" vectors elementwise.
157157
158-
pub fn simd_and<const N: u64, T: BitAnd + Copy>(
158+
pub fn simd_and<const N: u32, T: BitAnd + Copy>(
159159
x: FunArray<N, T>,
160160
y: FunArray<N, T>,
161161
) -> FunArray<N, <T as BitAnd>::Output> {
162-
FunArray::from_fn(|i| (x[i] & y[i]))
162+
FunArray::from_fn(|i| x[i] & y[i])
163163
}
164164

165165
/// "Ors" vectors elementwise.
166166
167-
pub fn simd_or<const N: u64, T: BitOr + Copy>(
167+
pub fn simd_or<const N: u32, T: BitOr + Copy>(
168168
x: FunArray<N, T>,
169169
y: FunArray<N, T>,
170170
) -> FunArray<N, <T as BitOr>::Output> {
171-
FunArray::from_fn(|i| (x[i] | y[i]))
171+
FunArray::from_fn(|i| x[i] | y[i])
172172
}
173173

174174
/// "Exclusive ors" vectors elementwise.
175175
176-
pub fn simd_xor<const N: u64, T: BitXor + Copy>(
176+
pub fn simd_xor<const N: u32, T: BitXor + Copy>(
177177
x: FunArray<N, T>,
178178
y: FunArray<N, T>,
179179
) -> FunArray<N, <T as BitXor>::Output> {
180-
FunArray::from_fn(|i| (x[i] ^ y[i]))
180+
FunArray::from_fn(|i| x[i] ^ y[i])
181181
}
182182

183183
pub trait CastsFrom<T> {
@@ -327,15 +327,15 @@ self_impls!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128);
327327
///
328328
/// When casting from a wider number to a smaller number, the higher bits are removed.
329329
/// Otherwise, it extends the number, following signedness.
330-
pub fn simd_cast<const N: u64, T1: Copy, T2: CastsFrom<T1>>(x: FunArray<N, T1>) -> FunArray<N, T2> {
330+
pub fn simd_cast<const N: u32, T1: Copy, T2: CastsFrom<T1>>(x: FunArray<N, T1>) -> FunArray<N, T2> {
331331
FunArray::from_fn(|i| T2::cast(x[i]))
332332
}
333333

334334
/// Negates a vector elementwise.
335335
///
336336
/// Rust panics for `-<int>::Min` due to overflow, but here, it just returns the element as is.
337337
338-
pub fn simd_neg<const N: u64, T: From<<T as Neg>::Output> + MachineInteger + Eq + Neg + Copy>(
338+
pub fn simd_neg<const N: u32, T: From<<T as Neg>::Output> + MachineInteger + Eq + Neg + Copy>(
339339
x: FunArray<N, T>,
340340
) -> FunArray<N, T> {
341341
FunArray::from_fn(|i| {
@@ -350,7 +350,7 @@ pub fn simd_neg<const N: u64, T: From<<T as Neg>::Output> + MachineInteger + Eq
350350
///
351351
/// Returns `0` (all zeros) for false and `!0` (all ones) for true.
352352
353-
pub fn simd_eq<const N: u64, T: Eq + MachineInteger + Copy>(
353+
pub fn simd_eq<const N: u32, T: Eq + MachineInteger + Copy>(
354354
x: FunArray<N, T>,
355355
y: FunArray<N, T>,
356356
) -> FunArray<N, T> {
@@ -361,7 +361,7 @@ pub fn simd_eq<const N: u64, T: Eq + MachineInteger + Copy>(
361361
///
362362
/// Returns `0` (all zeros) for false and `!0` (all ones) for true.
363363
364-
pub fn simd_ne<const N: u64, T: Eq + MachineInteger + Copy>(
364+
pub fn simd_ne<const N: u32, T: Eq + MachineInteger + Copy>(
365365
x: FunArray<N, T>,
366366
y: FunArray<N, T>,
367367
) -> FunArray<N, T> {
@@ -372,7 +372,7 @@ pub fn simd_ne<const N: u64, T: Eq + MachineInteger + Copy>(
372372
///
373373
/// Returns `0` (all zeros) for false and `!0` (all ones) for true.
374374
375-
pub fn simd_lt<const N: u64, T: Ord + MachineInteger + Copy>(
375+
pub fn simd_lt<const N: u32, T: Ord + MachineInteger + Copy>(
376376
x: FunArray<N, T>,
377377
y: FunArray<N, T>,
378378
) -> FunArray<N, T> {
@@ -383,7 +383,7 @@ pub fn simd_lt<const N: u64, T: Ord + MachineInteger + Copy>(
383383
///
384384
/// Returns `0` (all zeros) for false and `!0` (all ones) for true.
385385
386-
pub fn simd_le<const N: u64, T: Ord + MachineInteger + Copy>(
386+
pub fn simd_le<const N: u32, T: Ord + MachineInteger + Copy>(
387387
x: FunArray<N, T>,
388388
y: FunArray<N, T>,
389389
) -> FunArray<N, T> {
@@ -394,7 +394,7 @@ pub fn simd_le<const N: u64, T: Ord + MachineInteger + Copy>(
394394
///
395395
/// Returns `0` (all zeros) for false and `!0` (all ones) for true.
396396
397-
pub fn simd_gt<const N: u64, T: Ord + MachineInteger + Copy>(
397+
pub fn simd_gt<const N: u32, T: Ord + MachineInteger + Copy>(
398398
x: FunArray<N, T>,
399399
y: FunArray<N, T>,
400400
) -> FunArray<N, T> {
@@ -405,7 +405,7 @@ pub fn simd_gt<const N: u64, T: Ord + MachineInteger + Copy>(
405405
///
406406
/// Returns `0` (all zeros) for false and `!0` (all ones) for true.
407407
408-
pub fn simd_ge<const N: u64, T: Ord + MachineInteger + Copy>(
408+
pub fn simd_ge<const N: u32, T: Ord + MachineInteger + Copy>(
409409
x: FunArray<N, T>,
410410
y: FunArray<N, T>,
411411
) -> FunArray<N, T> {
@@ -415,10 +415,10 @@ pub fn simd_ge<const N: u64, T: Ord + MachineInteger + Copy>(
415415
/// Shuffles two vectors by the indices in idx.
416416
///
417417
/// For safety, `N2 <= N1 + N3` must hold.
418-
pub fn simd_shuffle<T: Copy, const N1: u64, const N2: usize, const N3: u64>(
418+
pub fn simd_shuffle<T: Copy, const N1: u32, const N2: usize, const N3: u32>(
419419
x: FunArray<N1, T>,
420420
y: FunArray<N1, T>,
421-
idx: [u64; N2],
421+
idx: [u32; N2],
422422
) -> FunArray<N3, T> {
423423
FunArray::from_fn(|i| {
424424
let i = idx[i as usize];
@@ -432,7 +432,7 @@ pub fn simd_shuffle<T: Copy, const N1: u64, const N2: usize, const N3: u64>(
432432

433433
/// Adds two vectors elementwise, with saturation.
434434
435-
pub fn simd_saturating_add<T: MachineInteger + Copy, const N: u64>(
435+
pub fn simd_saturating_add<T: MachineInteger + Copy, const N: u32>(
436436
x: FunArray<N, T>,
437437
y: FunArray<N, T>,
438438
) -> FunArray<N, T> {
@@ -441,7 +441,7 @@ pub fn simd_saturating_add<T: MachineInteger + Copy, const N: u64>(
441441

442442
/// Subtracts `y` from `x` elementwise, with saturation.
443443
444-
pub fn simd_saturating_sub<T: MachineInteger + Copy, const N: u64>(
444+
pub fn simd_saturating_sub<T: MachineInteger + Copy, const N: u32>(
445445
x: FunArray<N, T>,
446446
y: FunArray<N, T>,
447447
) -> FunArray<N, T> {
@@ -923,7 +923,7 @@ pub(crate) use simd_bitmask_big;
923923
/// # Safety
924924
/// `mask` must only contain `0` and `!0`.
925925
926-
pub fn simd_select<const N: u64, T1: Eq + MachineInteger, T2: Copy + MachineInteger>(
926+
pub fn simd_select<const N: u32, T1: Eq + MachineInteger, T2: Copy + MachineInteger>(
927927
mask: FunArray<N, T1>,
928928
if_true: FunArray<N, T2>,
929929
if_false: FunArray<N, T2>,

0 commit comments

Comments
 (0)