Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuyukitanimura committed Sep 4, 2024
1 parent d751a7f commit ef2864f
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions arrow-buffer/src/util/bit_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ fn set_upto_64bits(
} else {
let len = std::cmp::min(len, 64 - std::cmp::max(read_shift, write_shift));
let bytes = ceil(len + read_shift, 8);
// SAFETY: chunk gets masked, so it is safe
let chunk = unsafe { read_bytes_to_u64(data, read_byte, bytes) };
let mask = u64::MAX >> (64 - len);
let chunk = (chunk >> read_shift) & mask;
// SAFETY: chunk gets masked, so it is safe
let chunk = unsafe {
let chunk = read_bytes_to_u64(data, read_byte, bytes);
(chunk >> read_shift) & mask
};
let chunk = chunk << write_shift;
let null_count = len - chunk.count_ones() as usize;
let bytes = ceil(len + write_shift, 8);
Expand All @@ -119,7 +121,6 @@ fn set_upto_64bits(
/// The caller must ensure all arguments are within the valid range.
/// The caller must be aware `8 - count` bytes in the returned value are uninitialized.
#[inline]
#[cfg(not(miri))]
unsafe fn read_bytes_to_u64(data: &[u8], offset: usize, count: usize) -> u64 {
debug_assert!(count <= 8);
let mut tmp = std::mem::MaybeUninit::<u64>::uninit();
Expand All @@ -131,13 +132,6 @@ unsafe fn read_bytes_to_u64(data: &[u8], offset: usize, count: usize) -> u64 {
}
}

#[cfg(miri)]
unsafe fn read_bytes_to_u64(data: &[u8], offset: usize, count: usize) -> u64 {
let mut arr = [0u8; 8];
arr[0..count].copy_from_slice(&data[offset..(offset + count)]);
u64::from_le_bytes(arr)
}

#[inline]
fn write_u64_bytes(data: &mut [u8], offset: usize, chunk: u64) {
// SAFETY: the caller must ensure `data` has `offset..(offset + 8)` range
Expand Down

0 comments on commit ef2864f

Please sign in to comment.