-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
392 lines (311 loc) · 10.1 KB
/
lib.rs
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#![allow(dead_code)]
#![allow(unused_parens)]
use std::borrow::BorrowMut;
use std::fmt::{Debug, Display};
use std::marker::PhantomData;
pub trait BitChunkAccessor: Debug + Display + Copy {
type PrimitiveType: std::ops::BitAnd<Self::PrimitiveType, Output = Self::PrimitiveType>
+ std::ops::BitOr<Self::PrimitiveType, Output = Self::PrimitiveType>
+ std::ops::Shr<usize, Output = Self::PrimitiveType>
+ std::ops::Shl<usize, Output = Self::PrimitiveType>
+ std::ops::Sub<Self::PrimitiveType, Output = Self::PrimitiveType>;
fn bytes() -> usize {
return std::mem::size_of::<Self>();
}
fn bits() -> usize {
Self::bytes() * 8
}
fn one() -> Self::PrimitiveType;
unsafe fn read(ptr: *const u8, offset: isize) -> Self::PrimitiveType;
}
impl BitChunkAccessor for u8 {
type PrimitiveType = u8;
fn one() -> u8 {
1_u8
}
unsafe fn read(ptr: *const u8, offset: isize) -> u8 {
// no need for unaligned read for bytes
std::ptr::read(ptr.offset(offset))
}
}
impl BitChunkAccessor for u16 {
type PrimitiveType = u16;
fn one() -> u16 {
1_u16
}
unsafe fn read(ptr: *const u8, offset: isize) -> u16 {
std::ptr::read_unaligned((ptr as *const u16).offset(offset))
}
}
impl BitChunkAccessor for u32 {
type PrimitiveType = u32;
fn one() -> u32 {
1_u32
}
unsafe fn read(ptr: *const u8, offset: isize) -> u32 {
std::ptr::read_unaligned((ptr as *const u32).offset(offset))
}
}
impl BitChunkAccessor for u64 {
type PrimitiveType = u64;
fn one() -> u64 {
1_u64
}
unsafe fn read(ptr: *const u8, offset: isize) -> u64 {
std::ptr::read_unaligned((ptr as *const u64).offset(offset))
}
}
impl BitChunkAccessor for u128 {
type PrimitiveType = u128;
fn one() -> u128 {
1_u128
}
unsafe fn read(ptr: *const u8, offset: isize) -> u128 {
std::ptr::read_unaligned((ptr as *const u128).offset(offset))
}
}
#[inline(always)]
fn ceil_div_power_of_2(n: u64, p: u64) -> u64 {
debug_assert!(p.is_power_of_two());
((n + 7) & !(p - 1)) / p
}
pub struct BitChunks<'a, T: BitChunkAccessor> {
buffer: &'a [u8],
accessor: PhantomData<T>,
bit_offset: usize,
raw_data: *const u8,
chunk_len: usize,
remainder_len: usize,
}
pub struct BitChunkIterator<'a, T: BitChunkAccessor> {
buffer: &'a [u8],
accessor: PhantomData<T>,
bit_offset: usize,
raw_data: *const u8,
chunk_len: usize,
index: usize,
}
pub fn bit_chunk_iterator<T: BitChunkAccessor>(
buffer: &[u8],
bit_offset: usize,
) -> BitChunks<'_, T> {
let bytes = T::bytes();
let bits = T::bits();
debug_assert!(bits.is_power_of_two() && bits >= 8 && bits <= 64);
let byte_offset = (bit_offset / 8);
let bit_offset = bit_offset % 8;
let raw_data = unsafe { buffer.as_ptr().offset(byte_offset as isize) };
let len_bits = (buffer.len() - byte_offset) * 8 - bit_offset;
let chunk_len = (len_bits) / bits;
let remainder_len = (len_bits & (bits - 1));
BitChunks::<T> {
buffer,
accessor: PhantomData::<T>::default(),
bit_offset,
raw_data,
chunk_len,
remainder_len,
}
}
impl<'a, T: BitChunkAccessor> BitChunks<'a, T> {
pub fn remainder_len(&self) -> usize {
self.remainder_len
}
pub fn remainder_bits(&self) -> u64 {
let bit_len = self.remainder_len;
if bit_len == 0 {
0
} else {
let byte_len = ceil_div_power_of_2(bit_len as u64, 8) as usize;
let mut res = 0_u64;
for i in 0..byte_len {
res |= (self.buffer[self.chunk_len * T::bytes() + i] as u64) << (i * 8);
}
let offset = self.bit_offset as u64;
if offset != 0 {
(res >> offset) & !(1 << (64 - offset) - 1)
} else {
res
}
}
}
pub fn remainder_bytes(&self) -> Vec<u8> {
let bit_len = self.remainder_len;
if bit_len == 0 {
vec![]
} else {
let bits = self.remainder_bits();
let byte_len = ceil_div_power_of_2(bit_len as u64, 8) as usize;
let mut res: Vec<u8> = Vec::with_capacity(byte_len);
for i in 0..byte_len {
res.push((bits >> (i * 8) & 0xFF) as u8);
}
res
}
}
pub fn iter(&self) -> BitChunkIterator<'a, T> {
BitChunkIterator::<'a, T> {
buffer: self.buffer,
accessor: PhantomData::default(),
bit_offset: self.bit_offset,
raw_data: self.raw_data,
chunk_len: self.chunk_len,
index: 0,
}
}
}
impl<'a, T: BitChunkAccessor> IntoIterator for BitChunks<'a, T> {
type Item = T::PrimitiveType;
type IntoIter = BitChunkIterator<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T: BitChunkAccessor> Iterator for BitChunkIterator<'_, T> {
type Item = T::PrimitiveType;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.chunk_len {
return None;
}
let current = unsafe { T::read(self.raw_data, self.index as isize) };
let combined = if self.bit_offset == 0 {
current
} else {
let next = unsafe { T::read(self.raw_data, self.index as isize + 1) };
current >> self.bit_offset
| (next & ((T::one() << self.bit_offset) - T::one()))
<< (T::bits() - self.bit_offset)
};
self.index += 1;
Some(combined)
}
fn size_hint(&self) -> (usize, Option<usize>) {
(
self.chunk_len - self.index,
Some(self.chunk_len - self.index),
)
}
}
pub fn aggregate_sum_kernel(input: &[f32], valid: &[u8], offset: usize) -> f32 {
let chunks = input[offset..].chunks_exact(64);
let remainder = chunks.remainder();
let sum = &mut [0_f32; 64];
let bitchunks = bit_chunk_iterator::<u64>(valid, offset);
bitchunks
.iter()
.zip(chunks.into_iter())
.for_each(|(mask, slice)| {
for i in 0..64 {
let blend = if (mask & (1 << i)) != 0 { 1.0 } else { 0.0 };
sum[i] += blend * (slice[i]);
}
});
let mut sum: f32 = sum.iter().sum();
let remainder_len = bitchunks.remainder_len();
let remainder_bits = bitchunks.remainder_bits();
for i in 0..remainder_len {
if remainder_bits & (1 << i) != 0 {
sum += remainder[i];
}
}
sum
}
pub fn combine_bitmap(
left: &[u8],
left_offset: usize,
right: &[u8],
right_offset: usize,
output: &mut [u8],
) {
let chunk_size = <u128 as BitChunkAccessor>::bytes();
let left_chunks = bit_chunk_iterator::<u128>(left, left_offset);
let right_chunks = bit_chunk_iterator::<u128>(right, right_offset);
let mut output_chunks = output.chunks_exact_mut(chunk_size);
output_chunks
.borrow_mut()
.zip(left_chunks.iter().zip(right_chunks.iter()))
.for_each(|(out, (l, r))| {
//let out: &mut [u128] = unsafe {std::mem::transmute(out) };
//out[0] = l&r;
unsafe { (out.as_mut_ptr() as *mut u128).write(l & r) };
});
output_chunks
.into_remainder()
.iter_mut()
.zip(
left_chunks
.remainder_bytes()
.iter()
.zip(right_chunks.remainder_bytes().iter()),
)
.for_each(|(out, (l, r))| {
*out = l & r;
});
}
#[cfg(test)]
mod tests {
use crate::{aggregate_sum_kernel, bit_chunk_iterator, ceil_div_power_of_2};
#[test]
fn test_ceil() {
assert_eq!(0, ceil_div_power_of_2(0, 8));
assert_eq!(1, ceil_div_power_of_2(1, 8));
assert_eq!(1, ceil_div_power_of_2(7, 8));
assert_eq!(1, ceil_div_power_of_2(8, 8));
assert_eq!(2, ceil_div_power_of_2(9, 8));
}
#[test]
fn test_iter_aligned_8() {
let input: &[u8] = &[0, 1, 2, 4];
let bitchunks = bit_chunk_iterator::<u8>(input, 0);
let result = bitchunks.into_iter().collect::<Vec<u8>>();
assert_eq!(vec![0, 1, 2, 4], result);
}
#[test]
fn test_iter_unaligned_8() {
let input: &[u8] = &[0b0000000, 0b00010001, 0b00100010, 0b01000100];
let bitchunks = bit_chunk_iterator::<u8>(input, 1);
assert_eq!(7, bitchunks.remainder_len());
assert_eq!(0b00100010, bitchunks.remainder_bits());
let result = bitchunks.into_iter().collect::<Vec<u8>>();
assert_eq!(vec![0b10000000, 0b00001000, 0b00010001], result);
}
#[test]
fn test_iter_unaligned_16() {
let input: &[u8] = &[0b01010101, 0b11111111, 0b01010101, 0b11111111];
let bitchunks = bit_chunk_iterator::<u16>(input, 1);
let result = bitchunks.iter().collect::<Vec<u16>>();
assert_eq!(vec![0b1111111110101010], result);
assert_eq!(15, bitchunks.remainder_len());
assert_eq!(0b0111111110101010, bitchunks.remainder_bits());
}
#[test]
fn test_iter_aligned_16() {
let input: &[u8] = &[0, 1, 2, 4];
let result = bit_chunk_iterator::<u16>(input, 0)
.into_iter()
.collect::<Vec<u16>>();
assert_eq!(vec![0x0100, 0x0402], result);
}
#[test]
fn test_aggregate_sum_kernel() {
let len = 1000;
let input: Vec<f32> = (0..len)
.map(|i| if i % 2 == 0 { 2.0 } else { 1.0 })
.collect();
let valid: Vec<u8> = (0..ceil_div_power_of_2(len, 8))
.map(|i| 0b01010101)
.collect();
let expected: f32 = (0..len).map(|i| if i % 2 == 0 { 2.0 } else { 0.0 }).sum();
let result = aggregate_sum_kernel(&input, &valid, 0);
assert_eq!(expected, result);
}
#[test]
fn test_combine_bitmap() {
let left: Vec<f32> = (0..1024)
.map(|i| if i % 2 == 0 { 2.0 } else { 1.0 })
.collect();
let right: Vec<f32> = (0..1024)
.map(|i| if i % 2 == 0 { 2.0 } else { 1.0 })
.collect();
}
}