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
393
394
macro_rules! pack_unpack_with_bits {

    ($name:ident, $n:expr) => {

        mod $name {
            use super::BLOCK_LEN;
            use super::{DataType,
                set1,
                right_shift_32,
                left_shift_32,
                op_or,
                op_and,
                load_unaligned,
                store_unaligned};

            const NUM_BITS: usize = $n;
            const NUM_BYTES_PER_BLOCK: usize = NUM_BITS * super::BLOCK_LEN / 8;

            #[inline(always)]
            pub fn pack<Delta: FnMut(DataType) -> DataType>(input_arr: &[u32], output_arr: &mut [u8], mut delta_computer: Delta) -> usize {
                assert_eq!(input_arr.len(), BLOCK_LEN, "Input block too small {}, (expected {})", input_arr.len(), BLOCK_LEN);
                assert!(output_arr.len() >= NUM_BYTES_PER_BLOCK, "Output array too small (numbits {}). {} <= {}", NUM_BITS, output_arr.len(), NUM_BYTES_PER_BLOCK);

                let input_ptr = input_arr.as_ptr() as *const DataType;
                let mut output_ptr = output_arr.as_mut_ptr() as *mut DataType;

                unsafe {
                    let mut out_register: DataType = delta_computer(load_unaligned(input_ptr));

                    unroll! {
                        for iter in 0..30 {
                            const i: usize = 1 + iter;

                            const bits_filled: usize = i * NUM_BITS;
                            const inner_cursor: usize = bits_filled % 32;
                            const remaining: usize = 32 - inner_cursor;

                            let offset_ptr = input_ptr.offset(i as isize);
                            let in_register: DataType = delta_computer(load_unaligned(offset_ptr));

                            out_register =
                                if inner_cursor > 0 {
                                    let shifted = left_shift_32(in_register, inner_cursor as i32);
                                    op_or(out_register, shifted)
                                } else {
                                    in_register
                                };

                            if remaining <= NUM_BITS {
                                store_unaligned(output_ptr, out_register);
                                output_ptr = output_ptr.offset(1);
                                if remaining < NUM_BITS {
                                    out_register = right_shift_32(in_register, remaining as i32);
                                }
                            }
                        }
                    }
                    let in_register: DataType = delta_computer(load_unaligned(input_ptr.offset(31 as isize)));
                    out_register =
                        if NUM_BITS != 32 {
                            let shifted = left_shift_32(in_register, 32 - NUM_BITS as i32);
                            op_or(out_register, shifted)
                        } else {
                            in_register
                        };
                    store_unaligned(output_ptr, out_register);
                }

                NUM_BYTES_PER_BLOCK
            }


            #[inline(always)]
            pub fn unpack<Output: FnMut(DataType)>(compressed: &[u8], mut output: Output) -> usize {

                assert!(compressed.len() >= NUM_BYTES_PER_BLOCK, "Compressed array seems too small. ({} < {}) ", compressed.len(), NUM_BYTES_PER_BLOCK);

                let mut input_ptr = compressed.as_ptr() as *const DataType;

                let mask_scalar: u32 = ((1u64 << NUM_BITS) - 1u64) as u32;
                unsafe {
                    let mask = set1(mask_scalar as i32);

                    let mut in_register: DataType = load_unaligned(input_ptr);

                    let out_register = op_and(in_register, mask);
                    output(out_register);

                    unroll! {
                        for iter in 0..31 {
                            const i: usize = iter + 1;

                            const inner_cursor: usize = (i * NUM_BITS) % 32;
                            const inner_capacity: usize = 32 - inner_cursor;

                            // LLVM will not emit the shift operand if
                            // `inner_cursor` is 0.
                            let shifted_in_register = right_shift_32(in_register, inner_cursor as i32);
                            let mut out_register: DataType = op_and(shifted_in_register, mask);

                            // We consumed our current quadruplets entirely.
                            // We therefore read another one.
                            if inner_capacity <= NUM_BITS && i != 31 {
                                input_ptr = input_ptr.offset(1);
                                in_register = load_unaligned(input_ptr);

                                // This quadruplets is actually cutting one of
                                // our `DataType`. We need to read the next one.
                                if inner_capacity < NUM_BITS {
                                    let shifted = left_shift_32(in_register, inner_capacity as i32);
                                    let masked = op_and(shifted, mask);
                                    out_register = op_or(out_register, masked);
                                }
                            }

                            output(out_register);
                        }
                    }
                }

                NUM_BYTES_PER_BLOCK
            }
        }
    }
}

macro_rules! declare_bitpacker {

    ($bitpacker_name:ident, $block_len:expr) => {

        use super::BitPacker;
        use super::most_significant_bit;

        const BLOCK_LEN: usize = NUM_INTS_PER_REGISTER * 32;

        pack_unpack_with_bits!(pack_unpack_with_bits_1, 1);
        pack_unpack_with_bits!(pack_unpack_with_bits_2, 2);
        pack_unpack_with_bits!(pack_unpack_with_bits_3, 3);
        pack_unpack_with_bits!(pack_unpack_with_bits_4, 4);
        pack_unpack_with_bits!(pack_unpack_with_bits_5, 5);
        pack_unpack_with_bits!(pack_unpack_with_bits_6, 6);
        pack_unpack_with_bits!(pack_unpack_with_bits_7, 7);
        pack_unpack_with_bits!(pack_unpack_with_bits_8, 8);
        pack_unpack_with_bits!(pack_unpack_with_bits_9, 9);
        pack_unpack_with_bits!(pack_unpack_with_bits_10, 10);
        pack_unpack_with_bits!(pack_unpack_with_bits_11, 11);
        pack_unpack_with_bits!(pack_unpack_with_bits_12, 12);
        pack_unpack_with_bits!(pack_unpack_with_bits_13, 13);
        pack_unpack_with_bits!(pack_unpack_with_bits_14, 14);
        pack_unpack_with_bits!(pack_unpack_with_bits_15, 15);
        pack_unpack_with_bits!(pack_unpack_with_bits_16, 16);
        pack_unpack_with_bits!(pack_unpack_with_bits_17, 17);
        pack_unpack_with_bits!(pack_unpack_with_bits_18, 18);
        pack_unpack_with_bits!(pack_unpack_with_bits_19, 19);
        pack_unpack_with_bits!(pack_unpack_with_bits_20, 20);
        pack_unpack_with_bits!(pack_unpack_with_bits_21, 21);
        pack_unpack_with_bits!(pack_unpack_with_bits_22, 22);
        pack_unpack_with_bits!(pack_unpack_with_bits_23, 23);
        pack_unpack_with_bits!(pack_unpack_with_bits_24, 24);
        pack_unpack_with_bits!(pack_unpack_with_bits_25, 25);
        pack_unpack_with_bits!(pack_unpack_with_bits_26, 26);
        pack_unpack_with_bits!(pack_unpack_with_bits_27, 27);
        pack_unpack_with_bits!(pack_unpack_with_bits_28, 28);
        pack_unpack_with_bits!(pack_unpack_with_bits_29, 29);
        pack_unpack_with_bits!(pack_unpack_with_bits_30, 30);
        pack_unpack_with_bits!(pack_unpack_with_bits_31, 31);
        pack_unpack_with_bits!(pack_unpack_with_bits_32, 32);

        pub struct $bitpacker_name;

        impl $bitpacker_name {

            fn compress_generic<Delta: FnMut(DataType) -> DataType>(decompressed: &[u32], compressed: &mut [u8], num_bits: u8, delta_computer: Delta) -> usize {
                match num_bits {
                    0 => { 0 }
                    1 => pack_unpack_with_bits_1::pack(decompressed, compressed, delta_computer),
                    2 => pack_unpack_with_bits_2::pack(decompressed, compressed, delta_computer),
                    3 => pack_unpack_with_bits_3::pack(decompressed, compressed, delta_computer),
                    4 => pack_unpack_with_bits_4::pack(decompressed, compressed, delta_computer),
                    5 => pack_unpack_with_bits_5::pack(decompressed, compressed, delta_computer),
                    6 => pack_unpack_with_bits_6::pack(decompressed, compressed, delta_computer),
                    7 => pack_unpack_with_bits_7::pack(decompressed, compressed, delta_computer),
                    8 => pack_unpack_with_bits_8::pack(decompressed, compressed, delta_computer),
                    9 => pack_unpack_with_bits_9::pack(decompressed, compressed, delta_computer),
                    10 => pack_unpack_with_bits_10::pack(decompressed, compressed, delta_computer),
                    11 => pack_unpack_with_bits_11::pack(decompressed, compressed, delta_computer),
                    12 => pack_unpack_with_bits_12::pack(decompressed, compressed, delta_computer),
                    13 => pack_unpack_with_bits_13::pack(decompressed, compressed, delta_computer),
                    14 => pack_unpack_with_bits_14::pack(decompressed, compressed, delta_computer),
                    15 => pack_unpack_with_bits_15::pack(decompressed, compressed, delta_computer),
                    16 => pack_unpack_with_bits_16::pack(decompressed, compressed, delta_computer),
                    17 => pack_unpack_with_bits_17::pack(decompressed, compressed, delta_computer),
                    18 => pack_unpack_with_bits_18::pack(decompressed, compressed, delta_computer),
                    19 => pack_unpack_with_bits_19::pack(decompressed, compressed, delta_computer),
                    20 => pack_unpack_with_bits_20::pack(decompressed, compressed, delta_computer),
                    21 => pack_unpack_with_bits_21::pack(decompressed, compressed, delta_computer),
                    22 => pack_unpack_with_bits_22::pack(decompressed, compressed, delta_computer),
                    23 => pack_unpack_with_bits_23::pack(decompressed, compressed, delta_computer),
                    24 => pack_unpack_with_bits_24::pack(decompressed, compressed, delta_computer),
                    25 => pack_unpack_with_bits_25::pack(decompressed, compressed, delta_computer),
                    26 => pack_unpack_with_bits_26::pack(decompressed, compressed, delta_computer),
                    27 => pack_unpack_with_bits_27::pack(decompressed, compressed, delta_computer),
                    28 => pack_unpack_with_bits_28::pack(decompressed, compressed, delta_computer),
                    29 => pack_unpack_with_bits_29::pack(decompressed, compressed, delta_computer),
                    30 => pack_unpack_with_bits_30::pack(decompressed, compressed, delta_computer),
                    31 => pack_unpack_with_bits_31::pack(decompressed, compressed, delta_computer),
                    32 => pack_unpack_with_bits_32::pack(decompressed, compressed, delta_computer),
                    _ => {
                        panic!("Num bits must be <= 32. Was {}.", num_bits);
                    }
                }
            }
        }

        impl BitPacker for $bitpacker_name {

            const BLOCK_LEN: usize = $block_len;

            type DataType = DataType;

            fn compress(decompressed: &[u32], compressed: &mut [u8], num_bits: u8) -> usize {
                let no_delta = |curr| { curr };
                Self::compress_generic(
                    decompressed,
                    compressed,
                    num_bits,
                    no_delta)
            }

            fn compress_sorted(initial: u32,
                      decompressed: &[u32],
                      compressed: &mut [u8],
                      num_bits: u8) -> usize {

                let mut previous = unsafe { set1(initial as i32) };
                let delta_computer = |current: Self::DataType| {
                    let result = compute_delta(current, previous);
                    previous = current;
                    result
                };
                Self::compress_generic(
                    decompressed,
                    compressed,
                    num_bits,
                    delta_computer)
            }

            #[inline(always)]
            fn decompress_to<Output: FnMut(Self::DataType)>(compressed: &[u8], mut sink: Output, num_bits: u8) -> usize {
                match num_bits {
                    0 => {
                        let zero = unsafe { set1(0i32) };
                        for _ in 0..32 {
                            sink(zero);
                        }
                        0
                    },
                    1 => pack_unpack_with_bits_1::unpack(compressed, sink),
                    2 => pack_unpack_with_bits_2::unpack(compressed, sink),
                    3 => pack_unpack_with_bits_3::unpack(compressed, sink),
                    4 => pack_unpack_with_bits_4::unpack(compressed, sink),
                    5 => pack_unpack_with_bits_5::unpack(compressed, sink),
                    6 => pack_unpack_with_bits_6::unpack(compressed, sink),
                    7 => pack_unpack_with_bits_7::unpack(compressed, sink),
                    8 => pack_unpack_with_bits_8::unpack(compressed, sink),
                    9 => pack_unpack_with_bits_9::unpack(compressed, sink),
                    10 => pack_unpack_with_bits_10::unpack(compressed, sink),
                    11 => pack_unpack_with_bits_11::unpack(compressed, sink),
                    12 => pack_unpack_with_bits_12::unpack(compressed, sink),
                    13 => pack_unpack_with_bits_13::unpack(compressed, sink),
                    14 => pack_unpack_with_bits_14::unpack(compressed, sink),
                    15 => pack_unpack_with_bits_15::unpack(compressed, sink),
                    16 => pack_unpack_with_bits_16::unpack(compressed, sink),
                    17 => pack_unpack_with_bits_17::unpack(compressed, sink),
                    18 => pack_unpack_with_bits_18::unpack(compressed, sink),
                    19 => pack_unpack_with_bits_19::unpack(compressed, sink),
                    20 => pack_unpack_with_bits_20::unpack(compressed, sink),
                    21 => pack_unpack_with_bits_21::unpack(compressed, sink),
                    22 => pack_unpack_with_bits_22::unpack(compressed, sink),
                    23 => pack_unpack_with_bits_23::unpack(compressed, sink),
                    24 => pack_unpack_with_bits_24::unpack(compressed, sink),
                    25 => pack_unpack_with_bits_25::unpack(compressed, sink),
                    26 => pack_unpack_with_bits_26::unpack(compressed, sink),
                    27 => pack_unpack_with_bits_27::unpack(compressed, sink),
                    28 => pack_unpack_with_bits_28::unpack(compressed, sink),
                    29 => pack_unpack_with_bits_29::unpack(compressed, sink),
                    30 => pack_unpack_with_bits_30::unpack(compressed, sink),
                    31 => pack_unpack_with_bits_31::unpack(compressed, sink),
                    32 => pack_unpack_with_bits_32::unpack(compressed, sink),
                    _ => {
                        panic!("Num bits must be <= 32. Was {}.", num_bits);
                    }
                }
            }

            fn decompress(compressed: &[u8], decompressed: &mut [u32], num_bits: u8) -> usize {
                assert!(
                    decompressed.len() >= Self::BLOCK_LEN,
                    "The output array is not large enough : ({} >= {})",
                    decompressed.len(), Self::BLOCK_LEN);
                let mut output_ptr = decompressed.as_mut_ptr()  as *mut DataType;
                let output = |out_register| {
                    unsafe {
                        store_unaligned(output_ptr, out_register);
                        output_ptr = output_ptr.offset(1);
                    }
                };
                Self::decompress_to(compressed, output, num_bits)
            }

            fn decompress_sorted(
                initial: u32,
                compressed: &[u8],
                decompressed: &mut [u32],
                num_bits: u8) -> usize
            {
                unsafe {
                    let mut current = set1(initial as i32);
                    assert!(
                        decompressed.len() >= Self::BLOCK_LEN,
                        "The output array is not large enough : ({} >= {})",
                        decompressed.len(), Self::BLOCK_LEN);
                    let mut output_ptr = decompressed.as_mut_ptr()  as *mut DataType;
                    let output = |delta| {
                        current = integrate_delta(current, delta);
                        store_unaligned(output_ptr, current);
                        output_ptr = output_ptr.offset(1);
                    };
                    Self::decompress_to(compressed, output, num_bits)
                }
            }

            fn num_bits(decompressed: &[u32]) -> u8 {
                assert_eq!(decompressed.len(), BLOCK_LEN, "`decompressed`'s len is not `BLOCK_LEN={}`", BLOCK_LEN);
                let data: *const DataType = decompressed.as_ptr() as *const DataType;
                let mut accumulator = unsafe { load_unaligned(data) };
                unroll! {
                    for iter in 0..31 {
                        let i = iter + 1;
                        let newvec = unsafe { load_unaligned(data.offset(i as isize)) };
                        accumulator = unsafe { op_or(accumulator, newvec) };
                    }
                }
                most_significant_bit(or_collapse_to_u32(accumulator))
            }

            fn num_bits_sorted(initial: u32, decompressed: &[u32]) -> u8 {
                let initial_vec = unsafe { set1(initial as i32) };
                let data: *const DataType = decompressed.as_ptr() as *const DataType;

                let first = unsafe { load_unaligned(data) };
                let mut accumulator = unsafe { compute_delta(load_unaligned(data), initial_vec) };
                let mut previous = first;

                unroll! {
                    for iter in 0..30 {
                        let i = iter + 1;
                        let current = unsafe { load_unaligned(data.offset(i as isize)) };
                        let delta = unsafe { compute_delta(current, previous) };
                        accumulator =  unsafe { op_or(accumulator, delta) };
                        previous = current;
                    }
                }
                let current = unsafe { load_unaligned(data.offset(31 as isize)) };
                let delta = unsafe { compute_delta(current, previous) };
                accumulator =  unsafe { op_or(accumulator, delta) };
                most_significant_bit(or_collapse_to_u32(accumulator))
            }

        }


        #[cfg(test)]
        mod test {
            use tests::test_suite_compress_decompress;
            use super::$bitpacker_name;

            #[test]
            fn test_bitpacker() {
                test_suite_compress_decompress::<$bitpacker_name>(false);
            }

            #[test]
            fn test_bitpacker_delta() {
                test_suite_compress_decompress::<$bitpacker_name>(true);
            }


            bench_suite!($bitpacker_name);
        }

    }

}