tustvold commented on code in PR #2278: URL: https://github.com/apache/arrow-rs/pull/2278#discussion_r935854974
########## parquet/src/util/bit_pack.rs: ########## @@ -0,0 +1,231 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! 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) => { + pub fn unpack<const NUM_BITS: usize>(input: &[u8], output: &mut [$t; $bits]) { + if NUM_BITS == 0 { + for out in output { + *out = 0; + } + return; + } + + assert!(NUM_BITS <= $bytes * 8); + + let mask = match NUM_BITS { + $bits => <$t>::MAX, + _ => ((1 << NUM_BITS) - 1), + }; + + assert!(input.len() >= NUM_BITS * $bytes); + + let r = |output_idx: usize| { + <$t>::from_le_bytes( + input[output_idx * $bytes..output_idx * $bytes + $bytes] + .try_into() + .unwrap(), + ) + }; + + unroll!(for i in 0..$bits { Review Comment: LLVM was very reticent to unroll this loop, this is not all that surprising given it isn't immediately obvious how much it collapses down. We therefore give it a helping hand :smile: -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
