Skip to content

Commit 4035b7e

Browse files
author
dustletter
committed
Add comments explaining safety of unsafe sections
1 parent 4e44079 commit 4035b7e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/arch/wasm.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ pub fn dequantize_and_idct_block_8x8(
172172

173173
let data_with_offset = i16x8_add_sat(data[i], i16x8_splat(OFFSET + ROUNDING_BIAS));
174174

175+
// SAFETY: the assert at the start of this function ensures
176+
// `output_linestride * i + 7` < output.len(), so all accesses are in-bounds.
175177
unsafe {
176178
v128_store64_lane::<0>(
177179
u8x16_narrow_i16x8(
@@ -187,6 +189,7 @@ pub fn dequantize_and_idct_block_8x8(
187189
#[cfg(target_arch = "wasm32")]
188190
#[target_feature(enable = "simd128")]
189191
pub fn color_convert_line_ycbcr(y_slice: &[u8], cb_slice: &[u8], cr_slice: &[u8], output: &mut [u8]) -> usize {
192+
190193
assert!(output.len() % 3 == 0);
191194
let num = output.len() / 3;
192195
assert!(num <= y_slice.len());
@@ -201,6 +204,8 @@ pub fn color_convert_line_ycbcr(y_slice: &[u8], cb_slice: &[u8], cr_slice: &[u8]
201204
let y: v128;
202205
let cb: v128;
203206
let cr: v128;
207+
// SAFETY: i is at most `num / 8 - 8`, so the highest v128_load64_zero reads from
208+
// [num - 8, num). The above asserts ensure this is in-bounds.
204209
unsafe {
205210
y = v128_load64_zero(y_slice.as_ptr().wrapping_add(i * 8) as *const _);
206211
cb = v128_load64_zero(cb_slice.as_ptr().wrapping_add(i * 8) as *const _);
@@ -260,6 +265,8 @@ pub fn color_convert_line_ycbcr(y_slice: &[u8], cb_slice: &[u8], cr_slice: &[u8]
260265
0, 0, 0, // --, --, --
261266
0>(rg_lanes, b); // --
262267

268+
// SAFETY: i is at most `output.len() / 24 - 1` so the highest possible write is to
269+
// `output.len() - 1`.
263270
unsafe {
264271
v128_store(output.as_mut_ptr().wrapping_add(24 * i) as *mut _, rgb_low);
265272
v128_store64_lane::<0>(rgb_hi, output.as_mut_ptr().wrapping_add(24 * i + 16) as *mut _);

0 commit comments

Comments
 (0)