1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::seq_t;
use crate::unpack;
use crate::{supported_bit_width, FastLanes, FastLanesComparable};
use paste::paste;
pub trait BitPackingCompare: FastLanes {
/// A fused unpack (see `BitPacking::unpack`) and compare and pack into bit bools.
/// This will compare using the comparison function all the packed values with a constant value,
/// the values are of type `Self`, whereas the comparison is on the type `V` (where `V::Bitpacked` = `Self`).
/// This allows for comparison between signed values which are bitpacked as unsigned ones.
fn unpack_cmp<const W: usize, const B: usize, V, F>(
input: &[Self; B],
output: &mut [bool; 1024],
comparison: F,
value: V,
) where
V: FastLanesComparable<Bitpacked = Self>,
F: Fn(V, V) -> bool;
/// A fused unpack (see `BitPacking::unpack`) and compare and pack into bit bools.
///
/// # Safety
/// The input slice must be of length `1024 * W / T`, where `T` is the bit-width of Self and `W`
/// is the packed width. The output slice must be of exactly length `[u64; 16]` (`1024` bits).
/// These lengths are checked only with `debug_assert` (i.e., not checked on release builds).
unsafe fn unchecked_unpack_cmp<V, F>(
width: usize,
input: &[Self],
output: &mut [bool; 1024],
comparison: F,
value: V,
) where
V: FastLanesComparable<Bitpacked = Self>,
F: Fn(V, V) -> bool;
}
macro_rules! impl_packing_compare {
($T:ty) => {
impl BitPackingCompare for $T {
#[inline(never)]
fn unpack_cmp<const W: usize, const B: usize, V, F>(
input: &[Self; B],
output: &mut [bool; 1024],
f: F,
other: V,
)
where
V: FastLanesComparable<Bitpacked = Self>,
F: Fn(V, V) -> bool
{
const {
assert!(supported_bit_width(W, 8 * core::mem::size_of::<$T>()));
assert!(B == 1024 * W / Self::T);
}
for lane in (0..Self::LANES) {
unpack!($T, W, input, lane, |$idx, $elem| {
output[$idx] = f(V::as_unpacked($elem), other);
});
}
}
unsafe fn unchecked_unpack_cmp<V, F>(
width: usize,
input: &[Self],
output: &mut [bool; 1024],
comparison: F,
value: V,
)
where
V: FastLanesComparable<Bitpacked = Self>,
F: Fn(V, V) -> bool
{
let packed_len = 128 * width / size_of::<Self>();
debug_assert_eq!(input.len(), packed_len, "Input buffer must be of size 1024 * W / T");
debug_assert!(width <= Self::T, "Width must be less than or equal to {}", Self::T);
paste!(seq_t!(W in $T {
match width {
#(W => {
const B: usize = 1024 * W / <$T>::T;
Self::unpack_cmp::<W, B, V, F>(
arrayref::array_ref![input, 0, 1024 * W / <$T>::T],
output,
comparison,
value
)
},)*
_ => unreachable!("Unsupported width: {}", width)
}
}))
}
}
};
}
impl_packing_compare!(u8);
impl_packing_compare!(u16);
impl_packing_compare!(u32);
impl_packing_compare!(u64);
#[cfg(test)]
mod tests {
use super::*;
use crate::BitPacking;
use alloc::vec::Vec;
use core::array;
#[test]
fn test_unpack_eq() {
type T = u32;
const W: usize = 10;
const B: usize = 1024 * W / T::T;
let values = array::from_fn(|i| i as T % (1 << W));
let mut packed = [0; (128 * W) / size_of::<T>()];
T::pack::<W, B>(&values, &mut packed);
// Check equality against every value of the vector
for v in 0..1024 {
let cmp = {
let mut output = [false; 1024];
T::unpack_cmp::<W, B, _, _>(&packed, &mut output, |a, b| a == b, v);
output
};
let expected = values.iter().map(|&x| x == v).collect::<Vec<_>>();
assert_eq!(cmp.as_slice(), expected.as_slice(), "Failed == {v}");
}
}
}