Skip to content

Commit d7a07ea

Browse files
committed
wip
Signed-off-by: Alexander Droste <[email protected]>
1 parent 18ba05c commit d7a07ea

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

vortex-buffer/src/bit/buf.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -427,44 +427,43 @@ impl BitBuffer {
427427
}
428428

429429
let is_bit_set = |byte: u8, bit_idx: usize| (byte & (1 << bit_idx)) != 0;
430-
430+
let bit_offset = self.offset % 8;
431431
let mut buffer_ptr = unsafe { self.buffer.as_ptr().add(self.offset / 8) };
432-
let start_bit_in_byte = self.offset % 8;
433-
let mut bit_pos = 0;
432+
let mut callback_idx = 0;
434433

435434
// Handle incomplete first byte.
436-
if start_bit_in_byte > 0 {
437-
let bits_in_first_byte = (8 - start_bit_in_byte).min(total_bits);
435+
if bit_offset > 0 {
436+
let bits_in_first_byte = (8 - bit_offset).min(total_bits);
438437
let byte = unsafe { *buffer_ptr };
439438

440439
for bit_idx in 0..bits_in_first_byte {
441-
f(bit_pos, is_bit_set(byte, start_bit_in_byte + bit_idx));
440+
f(callback_idx, is_bit_set(byte, bit_offset + bit_idx));
441+
callback_idx += 1;
442442
}
443443

444-
bit_pos += bits_in_first_byte;
445444
buffer_ptr = unsafe { buffer_ptr.add(1) };
446445
}
447446

448-
let complete_bytes = (total_bits - bit_pos) / 8;
449-
450447
// Process complete bytes.
451-
for byte_idx in 0..complete_bytes {
448+
let complete_bytes = (total_bits - callback_idx) / 8;
449+
for _ in 0..complete_bytes {
452450
let byte = unsafe { *buffer_ptr };
453451

454452
for bit_idx in 0..8 {
455-
f(bit_pos + byte_idx * 8 + bit_idx, is_bit_set(byte, bit_idx));
453+
f(callback_idx, is_bit_set(byte, bit_idx));
454+
callback_idx += 1;
456455
}
457456
buffer_ptr = unsafe { buffer_ptr.add(1) };
458457
}
459-
bit_pos += complete_bytes * 8;
460458

461459
// Handle remaining bits at the end.
462-
let remaining_bits = (total_bits - bit_pos) % 8;
460+
let remaining_bits = total_bits - callback_idx;
463461
if remaining_bits > 0 {
464462
let byte = unsafe { *buffer_ptr };
465463

466464
for bit_idx in 0..remaining_bits {
467-
f(bit_pos + bit_idx, is_bit_set(byte, bit_idx));
465+
f(callback_idx, is_bit_set(byte, bit_idx));
466+
callback_idx += 1;
468467
}
469468
}
470469
}

0 commit comments

Comments
 (0)