Skip to content

Commit

Permalink
reads better
Browse files Browse the repository at this point in the history
  • Loading branch information
lwwmanning committed Jul 1, 2024
1 parent 2b11f02 commit 20c2214
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/bitpacking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,16 @@ macro_rules! impl_packing {
// From the row, we can get the correct start bit within the lane.
let start_bit = row * W;

// we read one or two T-bit words from the lane, depending on how our target
// W-bit value overlaps with the T-bit words
let start_word = start_bit / Self::T;
let end_word_inclusive = (start_bit + W - 1) / Self::T;

// shift and mask the correct bits from the T-bit words
// We need to read one or two T-bit words from the lane, depending on how our
// target W-bit value overlaps with the T-bit words. To avoid a branch, we
// always read two T-bit words, and then shift/mask as needed.
let lo_word = start_bit / Self::T;
let lo_shift = start_bit % Self::T;
let lo = packed[Self::LANES * start_word + lane] >> lo_shift;
let lo = packed[Self::LANES * lo_word + lane] >> lo_shift;

let hi_word = (start_bit + W - 1) / Self::T;
let hi_shift = (Self::T - lo_shift) % Self::T;
let hi = packed[Self::LANES * end_word_inclusive + lane] << hi_shift;
let hi = packed[Self::LANES * hi_word + lane] << hi_shift;

let mask: Self = if W == Self::T { Self::MAX } else { ((1 as Self) << (W % Self::T)) - 1 };
(lo | hi) & mask
Expand Down

0 comments on commit 20c2214

Please sign in to comment.