Skip to content

Commit

Permalink
Switch to using seq_macro
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Aug 3, 2022
1 parent f5edaad commit 5ee4a48
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 89 deletions.
1 change: 1 addition & 0 deletions parquet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ arrow = { path = "../arrow", version = "19.0.0", optional = true, default-featur
base64 = { version = "0.13", default-features = false, features = ["std"], optional = true }
clap = { version = "3", default-features = false, features = ["std", "derive", "env"], optional = true }
serde_json = { version = "1.0", default-features = false, features = ["std"], optional = true }
seq-macro = { version = "0.3", default-features = false }
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"] }
futures = { version = "0.3", default-features = false, features = ["std"], optional = true }
tokio = { version = "1.0", optional = true, default-features = false, features = ["macros", "fs", "rt", "io-util"] }
Expand Down
91 changes: 2 additions & 89 deletions parquet/src/util/bit_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,6 @@

//! Vectorised bit-packing utilities

macro_rules! unroll_impl {
(1, $offset:expr, $v:ident, $c:block) => {{
const $v: usize = $offset;
$c
}};
(2, $offset:expr, $v:ident, $c:block) => {{
unroll_impl!(1, $offset, $v, $c);
unroll_impl!(1, $offset + 1, $v, $c);
}};
(4, $offset:expr, $v:ident, $c:block) => {{
unroll_impl!(2, 0, __v4, { unroll_impl!(2, __v4 * 2 + $offset, $v, $c) });
}};
(8, $offset:expr, $v:ident, $c:block) => {{
unroll_impl!(2, 0, __v8, { unroll_impl!(4, __v8 * 4 + $offset, $v, $c) });
}};
(16, $offset:expr, $v:ident, $c:block) => {{
unroll_impl!(4, 0, __v16, {
unroll_impl!(4, __v16 * 4 + $offset, $v, $c)
});
}};
(32, $offset:expr, $v:ident, $c:block) => {{
unroll_impl!(2, 0, __v32, {
unroll_impl!(16, __v32 * 16 + $offset, $v, $c)
});
}};
(64, $offset:expr, $v:ident, $c:block) => {{
unroll_impl!(2, 0, __v64, {
unroll_impl!(32, __v64 * 32 + $offset, $v, $c)
});
}};
}

/// Manually unrolls a loop body, this is useful for two reasons
///
/// - force unrolling of a loop so that LLVM optimises it properly
/// - call a const generic function with the loop value
macro_rules! unroll {
(for $v:ident in 0..$end:tt $c:block) => {
#[allow(non_upper_case_globals)]
{
unroll_impl!($end, 0, $v, $c)
}
};
}

/// Macro that generates an unpack function taking the number of bits as a const generic
macro_rules! unpack_impl {
($t:ty, $bytes:literal, $bits:tt) => {
Expand Down Expand Up @@ -90,7 +45,7 @@ macro_rules! unpack_impl {
)
};

unroll!(for i in 0..$bits {
seq_macro::seq!(i in 0..$bits {
let start_bit = i * NUM_BITS;
let end_bit = start_bit + NUM_BITS;

Expand Down Expand Up @@ -124,14 +79,11 @@ macro_rules! unpack {
/// Unpack packed `input` into `output` with a bit width of `num_bits`
pub fn $name(input: &[u8], output: &mut [$t; $bits], num_bits: usize) {
// This will get optimised into a jump table
unroll!(for i in 0..$bits {
seq_macro::seq!(i in 0..=$bits {
if i == num_bits {
return $name::unpack::<i>(input, output);
}
});
if num_bits == $bits {
return $name::unpack::<$bits>(input, output);
}
unreachable!("invalid num_bits {}", num_bits);
}
};
Expand All @@ -153,45 +105,6 @@ mod tests {
(0..8).map(|_| rand.gen()).collect()
}

#[test]
fn test_unroll() {
let mut vec = vec![];
unroll!(for i in 0..1 {
vec.push(i);
});
assert_eq!(vec, (0..1).collect::<Vec<_>>());

let mut vec = vec![];
unroll!(for i in 0..4 {
vec.push(i);
});
assert_eq!(vec, (0..4).collect::<Vec<_>>());

let mut vec = vec![];
unroll!(for i in 0..8 {
vec.push(i);
});
assert_eq!(vec, (0..8).collect::<Vec<_>>());

let mut vec = vec![];
unroll!(for i in 0..16 {
vec.push(i);
});
assert_eq!(vec, (0..16).collect::<Vec<_>>());

let mut vec = vec![];
unroll!(for i in 0..32 {
vec.push(i);
});
assert_eq!(vec, (0..32).collect::<Vec<_>>());

let mut vec = vec![];
unroll!(for i in 0..64 {
vec.push(i);
});
assert_eq!(vec, (0..64).collect::<Vec<_>>());
}

#[test]
fn test_basic() {
let input = [0xFF; 4096];
Expand Down

0 comments on commit 5ee4a48

Please sign in to comment.