-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimd_galloping.rs
More file actions
288 lines (250 loc) · 8.61 KB
/
simd_galloping.rs
File metadata and controls
288 lines (250 loc) · 8.61 KB
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
#![cfg(feature = "simd")]
/// SIMD Galloping algorithm by D. Lemire et al.
///
/// Extends the classical galloping algorithm by performing comparisons of
/// blocks of 8 4xi32 registers, placing results in bitmasks Q1, Q2, Q3, Q4
/// where each Q is the result of a pairwise comparison between two SIMD
/// vectors. The galloping stage bounds in leaps of 4x8 SIMD registers = 32x4
/// integers, then performs a mini binary search to narrow it down to a block of
/// 8 registers.
use std::simd::*;
use std::simd::cmp::*;
use crate::{visitor::{Visitor, BsrVisitor}, intersect, instructions::load_unsafe, bsr::BsrRef};
const NUM_LANES_IN_BOUND: usize = 32;
/// 4 lane version used to intersect with 128-bit vectors, e.g., i32x4.
pub fn galloping_sse<T, V>(small: &[T], large: &[T], visitor: &mut V)
where
T: SimdElement + MaskElement + Ord + Default,
Simd<T, 4>: SimdPartialEq<Mask=Mask<T, 4>>,
V: Visitor<T>,
{
simd_galloping_impl::<T, V, 4>(small, large, visitor)
}
/// 8 lane version used to intersect with 256-bit vectors, e.g., i32x8.
pub fn galloping_avx2<T, V>(small: &[T], large: &[T], visitor: &mut V)
where
T: SimdElement + MaskElement + Ord + Default,
Simd<T, 8>: SimdPartialEq<Mask=Mask<T, 8>>,
V: Visitor<T>,
{
simd_galloping_impl::<T, V, 8>(small, large, visitor)
}
/// 16 lane version used to intersect with 512-bit vectors, e.g., i32x16.
/// Only faster if native 512-bit vectors are supported.
pub fn galloping_avx512<T, V>(small: &[T], large: &[T], visitor: &mut V)
where
T: SimdElement + MaskElement + Ord + Default,
Simd<T, 16>: SimdPartialEq<Mask=Mask<T, 16>>,
V: Visitor<T>,
{
simd_galloping_impl::<T, V, 16>(small, large, visitor)
}
fn simd_galloping_impl<'a, T, V, const LANES: usize>(
mut small: &'a[T],
mut large: &'a[T],
visitor: &mut V)
where
T: SimdElement + MaskElement + Ord + Default,
LaneCount<LANES>: SupportedLaneCount,
Simd<T, LANES>: SimdPartialEq<Mask=Mask<T, LANES>>,
V: Visitor<T>,
{
if small.len() > large.len() {
(small, large) = (large, small);
}
let bound = Simd::<T, LANES>::from_array([T::default(); LANES]).len() * NUM_LANES_IN_BOUND;
while !small.is_empty() && large.len() >= bound {
let target = small[0];
let target_block = gallop_wide(target, large, bound);
// Check if block actually contains target.
if large[(target_block + 1) * bound - 1] < target {
// If not, shrink large.
large = &large[(target_block + 1) * bound..];
debug_assert!(large.len() < bound);
// Swap small and large if small is big enough.
if small.len() >= bound {
(small, large) = (large, small);
continue;
}
else {
break;
}
}
debug_assert!(target_block == 0 || large[target_block * bound - 1] < target);
debug_assert!(large[(target_block+1) * bound - 1] >= target);
large = &large[target_block * bound..];
debug_assert!(large.len() >= bound);
let inner_offset: usize = reduce_search_bound(target, large, bound);
let result = block_compare::<T, LANES>(target, inner_offset, large);
if result.any() {
visitor.visit(target);
}
small = &small[1..];
}
debug_assert!(small.is_empty() || large.len() < bound);
intersect::branchless_merge(small, large, visitor)
}
pub fn galloping_sse_bsr<'a, V>(
small: BsrRef<'a>,
large: BsrRef<'a>,
visitor: &mut V)
where
V: BsrVisitor,
{
simd_galloping_bsr_impl::<V, 4, u8>(small, large, visitor)
}
pub fn galloping_avx2_bsr<'a, V>(
small: BsrRef<'a>,
large: BsrRef<'a>,
visitor: &mut V)
where
V: BsrVisitor,
{
simd_galloping_bsr_impl::<V, 8, u8>(small, large, visitor)
}
pub fn galloping_avx512_bsr<'a, V>(
small: BsrRef<'a>,
large: BsrRef<'a>,
visitor: &mut V)
where
V: BsrVisitor,
{
simd_galloping_bsr_impl::<V, 16, u16>(small, large, visitor)
}
pub fn simd_galloping_bsr_impl<'a, V, const LANES: usize, B>(
mut small: BsrRef<'a>,
mut large: BsrRef<'a>,
visitor: &mut V)
where
V: BsrVisitor,
LaneCount<LANES>: SupportedLaneCount,
Simd<u32, LANES>: SimdPartialEq<Mask=Mask<i32, LANES>>,
{
if small.len() > large.len() {
(small, large) = (large, small);
}
let bound = Simd::<i32, LANES>::from_array([0; LANES]).len();
while !small.is_empty() && large.len() >= bound {
let target_base = small.bases[0];
let target_state = small.states[0];
let found_block = gallop_wide(target_base, large.bases, bound);
// Check if block actually contains target.
if large.bases[(found_block + 1) * bound - 1] < target_base {
// If not, shrink large.
large = large.advanced_by((found_block + 1) * bound);
debug_assert!(large.len() < bound);
// Swap small and large if small is big enough.
if small.len() >= bound {
(small, large) = (large, small);
continue;
}
else {
break;
}
}
debug_assert!(found_block == 0 || large.bases[found_block * bound - 1] < target_base);
debug_assert!(large.bases[(found_block+1) * bound - 1] >= target_base);
large = large.advanced_by(found_block * bound);
debug_assert!(large.len() >= bound);
let target_vec = Simd::<u32, LANES>::splat(target_base);
let cmp_mask = target_vec.simd_eq(unsafe { load_unsafe(large.bases.as_ptr()) });
if cmp_mask.any() {
let p = cmp_mask.to_bitmask().trailing_zeros();
let result_state = target_state & large.states[p as usize];
if result_state != 0 {
visitor.visit_bsr(target_base, result_state);
}
}
small = small.advanced_by(1);
}
debug_assert!(small.is_empty() || large.len() < bound);
intersect::branchless_merge_bsr(small, large, visitor)
}
fn gallop_wide<T>(target: T, large: &[T], bound: usize) -> usize
where
T: Ord
{
let upper_bound = if large[bound - 1] >= target {
0
}
else {
let mut offset = 1;
while (offset + 1) * bound - 1 < large.len()
&& large[(offset + 1) * bound - 1] < target
{
offset *= 2;
}
offset
};
let lo = upper_bound / 2;
let hi = (large.len() / bound - 1).min(upper_bound);
binary_search_wide(target, large, lo, hi, bound)
}
fn binary_search_wide<T>(
target: T,
large: &[T],
low: usize,
high: usize,
bound: usize) -> usize
where
T: Ord
{
let mut lo = low as isize;
let mut hi = high as isize;
// Trying to find the block index such that the last element in the block
// is greater than or equal to the target value
while lo < hi {
let mid = lo + (hi - lo) / 2;
if large[(mid as usize + 1) * bound - 1] < target {
lo = mid + 1;
}
else {
hi = mid;
}
}
debug_assert!(lo == hi);
lo as usize
}
#[inline]
fn reduce_search_bound<T>(target: T, large: &[T], bound: usize) -> usize
where
T: Ord,
{
if large[bound / 2 - 1] >= target {
if large[bound / 4 - 1] < target {
NUM_LANES_IN_BOUND / 4
}
else {
0
}
}
else if large[bound * 3 / 4 - 1] < target {
NUM_LANES_IN_BOUND * 3 / 4
}
else {
NUM_LANES_IN_BOUND / 2
}
}
#[inline]
fn block_compare<T, const LANES: usize>(
target: T,
inner_offset: usize,
large: &[T]) -> Mask<T, LANES>
where
T: SimdElement + MaskElement + PartialOrd,
LaneCount<LANES>: SupportedLaneCount,
Simd<T, LANES>: SimdPartialEq<Mask=Mask<T, LANES>>,
{
let target_vec = Simd::<T, LANES>::splat(target);
let qs = [
target_vec.simd_eq(unsafe { load_unsafe(large.as_ptr().add(LANES * (inner_offset ))) }) |
target_vec.simd_eq(unsafe { load_unsafe(large.as_ptr().add(LANES * (inner_offset + 1))) }),
target_vec.simd_eq(unsafe { load_unsafe(large.as_ptr().add(LANES * (inner_offset + 2))) }) |
target_vec.simd_eq(unsafe { load_unsafe(large.as_ptr().add(LANES * (inner_offset + 3))) }),
target_vec.simd_eq(unsafe { load_unsafe(large.as_ptr().add(LANES * (inner_offset + 4))) }) |
target_vec.simd_eq(unsafe { load_unsafe(large.as_ptr().add(LANES * (inner_offset + 5))) }),
target_vec.simd_eq(unsafe { load_unsafe(large.as_ptr().add(LANES * (inner_offset + 6))) }) |
target_vec.simd_eq(unsafe { load_unsafe(large.as_ptr().add(LANES * (inner_offset + 7))) })
];
(qs[0] | qs[1]) | (qs[2] | qs[3])
}