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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::seq_t;
use crate::unpack;
use crate::{FastLanes, FastLanesComparable, supported_bit_width};
use pastey::paste;
pub trait BitPackingCompare: FastLanes {
/// A fused unpack (see `BitPacking::unpack`) and compare, packing the boolean results into a
/// bitmask of `1024` bits (`16 x u64`).
///
/// This compares, using the comparison function, all of the packed values against 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
/// bit-packed as unsigned ones.
///
/// The output is a bitmask in **lane-major order**, not logical row order. The `1024` bits
/// are `Self::LANES` words of `Self::T` bits, one word per lane laid out contiguously
/// (little-endian) in the `[u64; 16]`. Within a lane's word the comparison results are packed
/// LSB-first: row `r` (for `r` in `0..Self::T`) lands at bit `r`, holding the comparison for
/// the value at logical index `index(row, lane)` (see the `unpack!` macro). This is the
/// cheapest order to produce: it needs no cross-lane shuffles, just a per-lane accumulator
/// that the compiler keeps in a (vectorized) register.
///
/// For `u64` this lane-major order is the bit-level [`crate::Transpose::untranspose`] of the
/// logical mask. To recover logical row order (e.g. an Arrow-style boolean buffer), pass the
/// result through [`crate::transpose_bits::<Self>`](crate::transpose_bits).
fn unpack_cmp<const W: usize, const B: usize, V, F>(
input: &[Self; B],
output: &mut [u64; 16],
comparison: F,
value: V,
) where
V: FastLanesComparable<Bitpacked = Self>,
F: Fn(V, V) -> bool;
/// A fused unpack (see `BitPacking::unpack`) and compare, packing the boolean results into a
/// bitmask of `1024` bits (`16 x u64`). See [`BitPackingCompare::unpack_cmp`] for the output
/// bit ordering.
///
/// # 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 is exactly `[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 [u64; 16],
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 [u64; 16],
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);
}
// The output is 1024 bits laid out as `Self::LANES` words of `Self::T` bits each
// (which is always 128 bytes == `[u64; 16]`). Each lane owns one contiguous word
// holding that lane's `Self::T` comparison results, LSB-first: row `r` lands at bit
// `r`. Per-lane ownership means the accumulator stays in a register and the store is
// a single contiguous (vectorizable) write per lane -- no `[bool; 1024]`
// (or `[Self; 1024]`) materialization, no cross-lane shuffles.
//
// For `u64` (`Self::LANES == 16`) this LSB-first ordering coincides with the
// canonical FastLanes transpose; for narrower widths it is the per-width packing
// that [`crate::bit_transpose::untranspose_bits::<Self>`] inverts. Either way that
// is what [`untranspose_cmp_mask`] uses to recover logical row order.
//
// SAFETY: `[u64; 16]` and `[Self; LANES]` are both exactly 128 bytes, and `u64`'s
// alignment (8) is >= `Self`'s alignment, so the reinterpret is sound.
let words: &mut [$T; <$T>::LANES] =
unsafe { &mut *output.as_mut_ptr().cast::<[$T; <$T>::LANES]>() };
for lane in 0..Self::LANES {
let mut word: $T = 0;
let mut bit: usize = 0;
unpack!($T, W, input, lane, |$idx, $elem| {
let _ = $idx;
word |= <$T>::from(f(V::as_unpacked($elem), other)) << bit;
#[allow(unused_assignments)]
{ bit += 1; }
});
words[lane] = word;
}
}
unsafe fn unchecked_unpack_cmp<V, F>(
width: usize,
input: &[Self],
output: &mut [u64; 16],
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>(
unsafe { crate::as_array_unchecked(input) },
output,
comparison,
value
)
},)*
// seq_t has exclusive upper bound
Self::T => {
const W: usize = <$T>::T;
Self::unpack_cmp::<W, 1024, V, F>(
unsafe { crate::as_array_unchecked(input) },
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, transpose_bits};
use alloc::{format, string::ToString, vec};
use core::array;
use core::fmt::Debug;
use hegel::TestCase;
use hegel::generators as gs;
use hegel::generators::Integer;
use pastey::paste;
/// Reference bitmask in the same `FastLanes` (LSB-first, per-lane) order produced by
/// `unpack_cmp`:
/// fully unpack, then for each lane set bit `row` from the comparison of the value at the
/// logical index `index(row, lane)`.
fn reference_mask<T, V, F>(packed_unpacked: &[T; 1024], f: F, other: V) -> [u64; 16]
where
T: FastLanes,
V: FastLanesComparable<Bitpacked = T>,
F: Fn(V, V) -> bool,
{
let mut out = [0u64; 16];
for lane in 0..T::LANES {
for row in 0..T::T {
// `index(row, lane)` from the unpack macro.
let o = row / 8;
let s = row % 8;
let idx = (crate::FL_ORDER[o] * 16) + (s * 128) + lane;
if f(V::as_unpacked(packed_unpacked[idx]), other) {
// LSB-first within each lane word: row `r` lands at bit `r`.
let bit = lane * T::T + row;
out[bit / 64] |= 1u64 << (bit % 64);
}
}
}
out
}
fn comparison<V: PartialOrd>(operation: u8) -> fn(V, V) -> bool {
match operation {
0 => |a, b| a == b,
1 => |a, b| a != b,
2 => |a, b| a < b,
3 => |a, b| a <= b,
4 => |a, b| a > b,
5 => |a, b| a >= b,
_ => unreachable!("unsupported comparison {operation}"),
}
}
#[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);
let mut unpacked = [0u32; 1024];
T::unpack::<W, B>(&packed, &mut unpacked);
// Check equality against every value of the vector.
for v in 0..1024 {
let cmp = {
let mut output = [0u64; 16];
T::unpack_cmp::<W, B, _, _>(&packed, &mut output, |a, b| a == b, v);
output
};
let expected = reference_mask(&unpacked, |a, b| a == b, v);
assert_eq!(cmp, expected, "Failed == {v}");
}
}
fn assert_unpack_cmp_matches_reference<T, V>(tc: &TestCase)
where
T: BitPacking + BitPackingCompare + Debug + Integer + Send + Sync + 'static,
V: Debug + FastLanesComparable<Bitpacked = T> + Integer + PartialOrd + 'static,
{
let values: [T; 1024] = tc.draw(gs::arrays(gs::integers::<T>()));
let other = tc.draw(gs::integers::<V>());
for width in 0..=T::T {
let packed_len = 1024 * width / T::T;
let mut packed = vec![T::max_value(); packed_len];
unsafe { T::unchecked_pack(width, &values, &mut packed) };
let mask = if width == 0 {
T::zero()
} else if width == T::T {
T::max_value()
} else {
(T::one() << width) - T::one()
};
let unpacked = values.map(|value| value & mask);
for operation in 0..=5 {
let f = comparison::<V>(operation);
let expected = reference_mask(&unpacked, f, other);
let mut actual = [u64::MAX; 16];
unsafe {
T::unchecked_unpack_cmp(width, &packed, &mut actual, f, other);
}
assert_eq!(
actual,
expected,
"bitpacked={} comparable={} width={width} operation={operation}",
core::any::type_name::<T>(),
core::any::type_name::<V>(),
);
}
}
}
fn assert_unpack_cmp_transposes_to_logical<T, V>(tc: &TestCase)
where
T: BitPacking + BitPackingCompare + Debug + Integer + Send + Sync + 'static,
V: Debug + FastLanesComparable<Bitpacked = T> + Integer + PartialOrd + 'static,
{
let values: [T; 1024] = tc.draw(gs::arrays(gs::integers::<T>()));
let other = tc.draw(gs::integers::<V>());
for width in 0..=T::T {
let packed_len = 1024 * width / T::T;
let mut packed = vec![T::max_value(); packed_len];
unsafe { T::unchecked_pack(width, &values, &mut packed) };
let mask = if width == 0 {
T::zero()
} else if width == T::T {
T::max_value()
} else {
(T::one() << width) - T::one()
};
let unpacked = values.map(|value| value & mask);
for operation in 0..=5 {
let f = comparison::<V>(operation);
let mut expected = [0u64; 16];
for (index, value) in unpacked.iter().copied().enumerate() {
if f(V::as_unpacked(value), other) {
expected[index / 64] |= 1u64 << (index % 64);
}
}
let mut lane_major = [u64::MAX; 16];
unsafe {
T::unchecked_unpack_cmp(width, &packed, &mut lane_major, f, other);
}
let mut actual = [u64::MAX; 16];
transpose_bits::<T>(&lane_major, &mut actual);
assert_eq!(
actual,
expected,
"bitpacked={} comparable={} width={width} operation={operation}",
core::any::type_name::<T>(),
core::any::type_name::<V>(),
);
}
}
}
macro_rules! comparison_property_tests {
($T:ident, $V:ident) => {
paste! {
#[hegel::test(test_cases = 10)]
fn [<test_unpack_cmp_matches_reference_ $T _ $V>](tc: TestCase) {
assert_unpack_cmp_matches_reference::<$T, $V>(&tc);
}
#[hegel::test(test_cases = 10)]
fn [<test_unpack_cmp_transposes_to_logical_ $T _ $V>](tc: TestCase) {
assert_unpack_cmp_transposes_to_logical::<$T, $V>(&tc);
}
}
};
}
comparison_property_tests!(u8, u8);
comparison_property_tests!(u8, i8);
comparison_property_tests!(u16, u16);
comparison_property_tests!(u16, i16);
comparison_property_tests!(u32, u32);
comparison_property_tests!(u32, i32);
comparison_property_tests!(u64, u64);
comparison_property_tests!(u64, i64);
}