Skip to content

Commit

Permalink
ByteAddressableBuffer: unify bounds check in separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
Firestar99 committed Sep 27, 2024
1 parent fa01a79 commit 6545b87
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions crates/spirv-std/src/byte_addressable_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ pub struct ByteAddressableBuffer<T> {
pub data: T,
}

fn bounds_check<T>(data: &[u32], byte_index: u32) {
let sizeof = mem::size_of::<T>() as u32;
if byte_index % 4 != 0 {
panic!("`byte_index` should be a multiple of 4");
}
if byte_index + sizeof > data.len() as u32 {
let last_byte = byte_index + sizeof;
panic!(
"index out of bounds: the len is {} but loading {} bytes at `byte_index` {} reads until {} (exclusive)",
data.len(),
sizeof,
byte_index,
last_byte,
);
}
}

impl<'a> ByteAddressableBuffer<&'a [u32]> {
/// Creates a `ByteAddressableBuffer` from the untyped blob of data.
#[inline]
Expand All @@ -80,16 +97,7 @@ impl<'a> ByteAddressableBuffer<&'a [u32]> {
/// # Safety
/// See [`Self`].
pub unsafe fn load<T>(&self, byte_index: u32) -> T {
if byte_index % 4 != 0 {
panic!("`byte_index` should be a multiple of 4");
}
if byte_index + mem::size_of::<T>() as u32 > self.data.len() as u32 {
panic!(
"index out of bounds: the len is {} but the `byte_index` is {}",
self.data.len(),
byte_index
);
}
bounds_check::<T>(self.data, byte_index);
buffer_load_intrinsic(self.data, byte_index)
}

Expand All @@ -116,16 +124,7 @@ impl<'a> ByteAddressableBuffer<&'a mut [u32]> {
/// # Safety
/// See [`Self`].
pub unsafe fn load<T>(&self, byte_index: u32) -> T {
if byte_index % 4 != 0 {
panic!("`byte_index` should be a multiple of 4");
}
if byte_index + mem::size_of::<T>() as u32 > self.data.len() as u32 {
panic!(
"index out of bounds: the len is {} but the `byte_index` is {}",
self.data.len(),
byte_index
);
}
bounds_check::<T>(self.data, byte_index);
buffer_load_intrinsic(self.data, byte_index)
}

Expand All @@ -144,9 +143,7 @@ impl<'a> ByteAddressableBuffer<&'a mut [u32]> {
/// # Safety
/// See [`Self`].
pub unsafe fn store<T>(&mut self, byte_index: u32, value: T) {
if byte_index + mem::size_of::<T>() as u32 > self.data.len() as u32 {
panic!("Index out of range");
}
bounds_check::<T>(self.data, byte_index);
buffer_store_intrinsic(self.data, byte_index, value);
}

Expand Down

0 comments on commit 6545b87

Please sign in to comment.