byteshuffle 0.1.2

SIMD-accelerated byte shuffle/unshuffle functions
Documentation
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! SIMD-accelerated byte shuffle/unshuffle routines
//!
//! The byte-shuffle is a very efficient way to improve the compressibility of data that consists
//! of an array of fixed-size objects.  It rearranges the array in order to group all elements'
//! least significant bytes together, most-significant bytes together, and everything in between.
//! Since real applications' arrays often contain consecutive elements that are closely correlated
//! with each other, this filter frequently results in lengthy continuous runs of identical bytes.
//! Such runs are highly compressible by general-purpose compression libraries like gzip, lz4, etc.
//!
//! The [blosc](https://www.blosc.org) project was the original inspiration for this library.
//! Blosc is a C library intended primarily for HPC users, and it implements a shuffle filter,
//! among many other things.  This crate is a clean reimplementation of Blosc's shuffle filter.
//!
//! # Examples
//!
//! Typical use: a byte array consists of an arithmetic sequence.  Shuffle it, compress it,
//! decompress it, and then unshuffle it.
//! ```
//! # use byteshuffle::*;
//! const IN: [u8; 8] = [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04];
//! let shuffled = shuffle(2, &IN);
//! assert_ne!(IN, &shuffled[..]);
//! // In normal use, you would now serialize `shuffled`.  Then compress it, and later decompress
//! // and deserialize it.  Then unshuffle like below.
//! let unshuffled = unshuffle(2, &shuffled);
//! assert_eq!(IN, &unshuffled[..]);
//! ```
//!
//! # Crate Features
//!
//! This crate has a "nightly" feature.  It enables methods that require the use of types from the
//! standard library that aren't yet stabilized.
#![cfg_attr(feature = "nightly", feature(core_io_borrowed_buf))]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(feature = "nightly")]
use std::io::BorrowedCursor;
use std::{mem, str::FromStr};

use cfg_if::cfg_if;
use ctor::ctor;

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod avx2;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod avx512f;
mod generic;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod sse2;

type LlFunc = unsafe fn(usize, usize, *const u8, *mut u8);
static mut IMPL: (LlFunc, LlFunc) = (generic::shuffle, generic::unshuffle);

/// Explicitly specify an instruction set to use.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SimdImpl {
    /// Automatically determine the implementation to use
    Auto,
    /// Don't use any SIMD accelerations
    Generic,
    /// Use the SSE2 instruction set
    Sse2,
    /// Use the AVX2 instruction set
    Avx2,
    /// Use the AVX512F + AVX512BW instruction sets
    Avx512F,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ParseSimdImplErr;

impl FromStr for SimdImpl {
    type Err = ParseSimdImplErr;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "auto" => Ok(SimdImpl::Auto),
            "generic" => Ok(SimdImpl::Generic),
            "sse2" => Ok(SimdImpl::Sse2),
            "avx2" => Ok(SimdImpl::Avx2),
            "avx512f" => Ok(SimdImpl::Avx512F),
            _ => Err(ParseSimdImplErr),
        }
    }
}

#[ctor]
fn select_implementation_ctor() {
    // Safe because we're single-threaded before main
    unsafe { select_implementation(SimdImpl::Auto) }
}

/// Force the use of a particular CPU instruction set, globally.
///
/// The default behavior is to automatically select, which should normally work best.  But this
/// function may be used to force a lower instruction set for test and benchmarking purposes, or if
/// one of the higher level optimizations does not work well.
///
/// # Safety
///
/// May not be called concurrently with any other function in this library.
pub unsafe fn select_implementation(impl_: SimdImpl) {
    // Safe because ctor guarantees only one writer at a time
    cfg_if! {
        if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
            match impl_ {
                SimdImpl::Auto => {
                    if is_x86_feature_detected!("avx512f") && is_x86_feature_detected!("avx512bw"){
                        unsafe { IMPL = (avx512f::shuffle, avx2::unshuffle); }
                    } else if is_x86_feature_detected!("avx2") {
                        unsafe { IMPL = (avx2::shuffle, avx2::unshuffle); }
                    } else if is_x86_feature_detected!("sse2") {
                        unsafe { IMPL = (sse2::shuffle, sse2::unshuffle); }
                    } else {
                        unsafe { IMPL = (generic::shuffle, generic::unshuffle); }
                    }
                },
                SimdImpl::Generic => unsafe { IMPL = (generic::shuffle, generic::unshuffle); },
                SimdImpl::Sse2 => unsafe { IMPL = (sse2::shuffle, sse2::unshuffle); },
                SimdImpl::Avx2 => unsafe { IMPL = (avx2::shuffle, sse2::unshuffle); },
                SimdImpl::Avx512F => unsafe { IMPL = (avx512f::shuffle, sse2::unshuffle); },
            }
        } else {
            let _ = impl_;
            unsafe { IMPL = (generic::shuffle, generic::unshuffle); }
        }
    }
}

/// Shuffle an array of fixed-size objects.
///
/// The result will be a shuffled byte array.  Be aware that this byte array will usually not be
/// portable.  It can only be unshuffled by the same computer architecture as the one that shuffled
/// it, and sometimes only by the same compiler version.  If portability is desired, then first
/// serialize the data and then use [`shuffle`].
///
/// # Examples
/// ```
/// # use byteshuffle::*;
/// const IN: [u16; 4] = [0x01, 0x02, 0x03, 0x04];
/// let out = shuffle_objects(&IN);
#[cfg_attr(
    target_endian = "little",
    doc = "assert_eq!(out, [0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00]);"
)]
#[cfg_attr(
    target_endian = "big",
    doc = "assert_eq!(out, [0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04]);"
)]
/// ```
pub fn shuffle_objects<T: Copy>(src: &[T]) -> Vec<u8> {
    let ts = mem::size_of::<T>();
    assert!(ts > 1, "No point shuffling plain [u8]");
    let mut dst = Vec::with_capacity(mem::size_of_val(src));
    // Safe because we src and dst have same length in bytes
    unsafe {
        IMPL.0(
            ts,
            mem::size_of_val(src),
            src.as_ptr() as *const u8,
            dst.as_mut_ptr(),
        );
        dst.set_len(mem::size_of_val(src));
    };
    dst
}

/// Shuffle a byte array whose contents are known to approximately repeat with
/// period `typesize`.
///
/// # Examples
/// ```
/// # use byteshuffle::*;
/// const IN: [u8; 8] = [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04];
/// let out = shuffle(2, &IN);
/// assert_eq!(out, [0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04]);
/// ```
pub fn shuffle(typesize: usize, src: &[u8]) -> Vec<u8> {
    let mut dst = Vec::with_capacity(src.len());
    // Safe because src and dst have the same capacity
    unsafe {
        IMPL.0(typesize, src.len(), src.as_ptr(), dst.as_mut_ptr());
        dst.set_len(src.len());
    }
    assert_eq!(src.len(), dst.len());
    dst
}

/// Like [`shuffle`], but allows the caller to control allocation for the output.
///
/// # Examples
/// ```
/// # use byteshuffle::*;
/// const IN: [u8; 8] = [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04];
/// let mut out = [0u8; 8];
/// shuffle_into(2, &IN, &mut out);
/// assert_eq!(out, [0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04]);
/// ```
pub fn shuffle_into(typesize: usize, src: &[u8], dst: &mut [u8]) {
    assert_eq!(src.len(), dst.len());
    // Safe because src and dst have the same length
    unsafe {
        IMPL.0(typesize, src.len(), src.as_ptr(), dst.as_mut_ptr());
    }
}

/// Like [`shuffle_into`], but works with uninitialized output buffers.
///
/// # Example
/// ```
/// #![cfg_attr(feature = "nightly", feature(core_io_borrowed_buf))]
/// use byteshuffle::*;
/// use std::io::BorrowedBuf;
/// use std::mem::MaybeUninit;
///
/// const IN: [u8; 8] = [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04];
/// let mut outvec = [MaybeUninit::uninit(); 8];
/// let mut buf = BorrowedBuf::from(&mut outvec[..]);
/// shuffle_buf(2, &IN, buf.unfilled());
/// assert_eq!(buf.filled(), [0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04]);
/// ```
#[cfg(feature = "nightly")]
#[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
pub fn shuffle_buf(typesize: usize, src: &[u8], mut buf: BorrowedCursor<'_>) {
    assert!(buf.capacity() >= src.len());
    unsafe {
        let dst: *mut u8 = buf.as_mut().as_mut_ptr().cast();
        IMPL.0(typesize, src.len(), src.as_ptr(), dst);
        buf.advance(src.len());
    }
}

/// Unshuffle an array of fixed-size objects.
///
/// # Safety
///
/// The `src` must've originally been produced by [`shuffle`], by a program that uses the exact
/// same byte layout as this one.  That means the same wordsize, same endianness, and may even
/// require the same compiler version.  If you can't guarantee those conditions, then serialize and
/// use [`shuffle`]/[`unshuffle`] instead.
///
/// # Examples
/// ```
/// # use byteshuffle::*;
/// const IN: [u16; 4] = [0x01, 0x02, 0x03, 0x04];
/// let mut shuffled = shuffle_objects(&IN);
/// let out = unsafe { unshuffle_objects(&shuffled) };
/// assert_eq!(IN, &out[..]);
/// ```
pub unsafe fn unshuffle_objects<T: Copy>(src: &[u8]) -> Vec<T> {
    let ts = mem::size_of::<T>();
    assert!(ts > 1, "No point shuffling plain [u8]");
    let mut dst = Vec::with_capacity(src.len() / ts);
    // Safe because we src and dst have same length in bytes
    unsafe {
        IMPL.1(
            ts,
            mem::size_of_val(src),
            src.as_ptr(),
            dst.as_mut_ptr() as *mut u8,
        );
        dst.set_len(src.len() / ts);
    }
    assert_eq!(mem::size_of_val(src), mem::size_of_val(&dst[..]));
    dst
}

/// Unshuffle a byte array whose contents are known to approximately repeat with
/// period `typesize`.
///
/// # Examples
/// ```
/// # use byteshuffle::*;
/// const IN: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04];
/// let out = unshuffle(2, &IN);
/// assert_eq!(out, [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]);
/// ```
pub fn unshuffle(typesize: usize, src: &[u8]) -> Vec<u8> {
    let mut dst = Vec::with_capacity(src.len());
    unsafe {
        IMPL.1(typesize, src.len(), src.as_ptr(), dst.as_mut_ptr());
        dst.set_len(src.len());
    }
    assert_eq!(src.len(), dst.len());
    dst
}

/// Like [`unshuffle`], but allows the caller to control allocation for the destination.
///
/// # Example
/// ```
/// use byteshuffle::*;
///
/// const IN: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04];
/// let mut out = [0u8; 8];
/// unshuffle_into(2, &IN, &mut out);
/// assert_eq!(out, [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]);
/// ```
pub fn unshuffle_into(typesize: usize, src: &[u8], dst: &mut [u8]) {
    assert_eq!(src.len(), dst.len());
    unsafe {
        IMPL.1(typesize, src.len(), src.as_ptr(), dst.as_mut_ptr());
    }
}

/// Like [`unshuffle_into`], but works with uninitialized output buffers.
///
/// # Example
/// ```
/// #![cfg_attr(feature = "nightly", feature(core_io_borrowed_buf))]
/// use byteshuffle::*;
/// use std::io::BorrowedBuf;
/// use std::mem::MaybeUninit;
///
/// const IN: [u8; 8] = [0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04];
/// let mut outvec = [MaybeUninit::uninit(); 8];
/// let mut buf = BorrowedBuf::from(&mut outvec[..]);
/// unshuffle_buf(2, &IN, buf.unfilled());
/// assert_eq!(buf.filled(), [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]);
/// ```
#[cfg(feature = "nightly")]
#[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
pub fn unshuffle_buf(typesize: usize, src: &[u8], mut buf: BorrowedCursor<'_>) {
    assert!(buf.capacity() >= src.len());
    unsafe {
        let dst: *mut u8 = buf.as_mut().as_mut_ptr().cast();
        IMPL.1(typesize, src.len(), src.as_ptr(), dst);
        buf.advance(src.len());
    }
}

#[cfg(test)]
mod t {
    use super::*;

    /// Test the type-based shuffle_objects API
    mod shuffle_objects {
        use super::*;

        // Two elements of two bytes each
        #[test]
        fn twobytwo() {
            let src = [0x1234u16, 0x5678u16];
            let dst = shuffle_objects(&src[..]);
            cfg_if! {
                if #[cfg(target_endian = "big")] {
                    let expected = [0x78u8, 0x34, 0x56, 0x12];
                } else {
                    let expected = [0x34, 0x78u8, 0x12, 0x56];
                }
            }
            assert_eq!(dst, &expected[..]);
        }

        // Four elements of four bytes each
        #[test]
        fn fourbyfour() {
            let src = [0x11223344u32, 0x55667788, 0x99aabbcc, 0xddeeff00];
            let dst = shuffle_objects(&src[..]);
            cfg_if! {
                if #[cfg(target_endian = "big")] {
                    let expected = [0x00u8, 0xcc, 0x88, 0x44, 0xff, 0xbb, 0x77, 0x33,
                       0xee, 0xaa, 0x66, 0x22, 0xdd, 0x99, 0x55, 0x11];
                } else {
                    let expected = [0x44, 0x88, 0xcc, 0x00, 0x33, 0x77, 0xbb, 0xff,
                       0x22, 0x66, 0xaa, 0xee, 0x11, 0x55, 0x99, 0xdd];
                }
            }
            assert_eq!(dst, &expected[..]);
        }
    }

    mod shuffle {
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        use rand::Rng;
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        use rstest::rstest;

        /// Compare optimized results against generic results
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        #[rstest]
        #[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"),
            case::sse2(crate::sse2::shuffle, is_x86_feature_detected!("sse2")))]
        #[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"),
            case::avx2(crate::avx2::shuffle, is_x86_feature_detected!("avx2")))]
        #[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"),
            case::avx512f(crate::avx512f::shuffle,
                        is_x86_feature_detected!("avx512f") && is_x86_feature_detected!("avx512bw")
            )
        )]
        fn compare(
            #[values(2, 4, 8, 13, 16, 18, 32, 36, 43, 47)] typesize: usize,
            #[values(64, 65, 256, 258, 1024, 1028, 4096, 4112)] len: usize,
            #[case] f: unsafe fn(usize, usize, *const u8, *mut u8),
            #[case] has_feature: bool,
        ) {
            if !has_feature {
                eprintln!("Skipping: CPU feature unavailable.");
                return;
            }

            let mut rng = rand::rng();

            let src = (0..len).map(|_| rng.random()).collect::<Vec<u8>>();
            let mut generic_dst = vec![0u8; len];
            let mut opt_dst = vec![0u8; len];
            unsafe {
                crate::generic::shuffle(typesize, len, src.as_ptr(), generic_dst.as_mut_ptr());
                f(typesize, len, src.as_ptr(), opt_dst.as_mut_ptr());
            }
            assert_eq!(generic_dst, opt_dst);
        }
    }

    /// Test the type-based shuffle API
    mod unshuffle_objects {
        use super::*;

        // Two elements of two bytes each
        #[test]
        fn twobytwo() {
            cfg_if! {
                if #[cfg(target_endian = "big")] {
                    let src = [0x78u8, 0x34, 0x56, 0x12];
                } else {
                    let src = [0x34, 0x78u8, 0x12, 0x56];
                }
            }
            let dst = unsafe { unshuffle_objects::<u16>(&src[..]) };
            assert_eq!(dst, &[0x1234u16, 0x5678u16][..]);
        }

        // Four elements of four bytes each
        #[test]
        fn fourbyfour() {
            cfg_if! {
                if #[cfg(target_endian = "big")] {
                    let src = [0x00u8, 0xcc, 0x88, 0x44, 0xff, 0xbb, 0x77, 0x33,
                       0xee, 0xaa, 0x66, 0x22, 0xdd, 0x99, 0x55, 0x11];
                } else {
                    let src = [0x44, 0x88, 0xcc, 0x00, 0x33, 0x77, 0xbb, 0xff,
                       0x22, 0x66, 0xaa, 0xee, 0x11, 0x55, 0x99, 0xdd];
                }
            }
            let dst = unsafe { unshuffle_objects::<u32>(&src[..]) };
            assert_eq!(
                dst,
                &[0x11223344u32, 0x55667788, 0x99aabbcc, 0xddeeff00][..]
            );
        }
    }

    mod unshuffle {
        use rand::Rng;
        use rstest::rstest;

        /// Compare optimized results against generic results
        #[rstest]
        #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
        #[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"),
            case::sse2(crate::sse2::unshuffle, is_x86_feature_detected!("sse2")))]
        #[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"),
            case::avx2(crate::avx2::unshuffle, is_x86_feature_detected!("avx2")))]
        fn compare(
            #[values(2, 4, 8, 16, 18, 32, 36, 43, 47)] typesize: usize,
            #[values(64, 65, 256, 258, 1024, 1028, 4096, 4112)] len: usize,
            #[case] f: unsafe fn(usize, usize, *const u8, *mut u8),
            #[case] has_feature: bool,
        ) {
            if !has_feature {
                eprintln!("Skipping: CPU feature unavailable.");
                return;
            }

            let mut rng = rand::rng();

            let src = (0..len).map(|_| rng.random()).collect::<Vec<u8>>();
            let mut generic_dst = vec![0u8; len];
            let mut opt_dst = vec![0u8; len];
            unsafe {
                crate::generic::unshuffle(typesize, len, src.as_ptr(), generic_dst.as_mut_ptr());
                f(typesize, len, src.as_ptr(), opt_dst.as_mut_ptr());
            }
            assert_eq!(generic_dst, opt_dst);
        }

        /// unshuffle should be the inverse of shuffle
        #[rstest]
        fn inverse(
            #[values(2, 4, 8, 16, 18, 32, 36, 43, 47)] typesize: usize,
            #[values(64, 65, 256, 258, 1024, 1028, 4096, 4112)] len: usize,
        ) {
            let mut rng = rand::rng();

            let src = (0..len).map(|_| rng.random()).collect::<Vec<u8>>();
            let mut shuffled = vec![0u8; len];
            let mut dst = vec![0u8; len];
            unsafe {
                crate::generic::shuffle(typesize, len, src.as_ptr(), shuffled.as_mut_ptr());
                crate::generic::unshuffle(typesize, len, shuffled.as_ptr(), dst.as_mut_ptr());
            }
            assert_eq!(src, dst);
        }
    }
}