Skip to content

Commit

Permalink
Do not zero txa before initializing it
Browse files Browse the repository at this point in the history
  • Loading branch information
rinon committed Jun 28, 2024
1 parent 9d2da2f commit 293dfe8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
26 changes: 26 additions & 0 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ impl<const UP_TO: usize, const WITH_DEFAULT: bool> CaseSetter<UP_TO, WITH_DEFAUL
let mut buf = buf.index_mut(self.offset..self.offset + self.len);
small_memset::<V, UP_TO, WITH_DEFAULT>(&mut *buf, val);
}

/// # Safety
///
/// `buf` must be correctly aligned and dereferencable (but need not be
/// initialized).
#[inline]
pub unsafe fn set_raw<T: Clone + Copy>(&self, buf: *mut [T], val: T) {
assert!(buf.len() >= self.offset + self.len);
let buf = (buf as *mut T).add(self.offset);
match self.len {
01 if UP_TO >= 01 => unsafe { *(buf as *mut [T; 01]) = [val; 01] },
02 if UP_TO >= 02 => unsafe { *(buf as *mut [T; 02]) = [val; 02] },
04 if UP_TO >= 04 => unsafe { *(buf as *mut [T; 04]) = [val; 04] },
08 if UP_TO >= 08 => unsafe { *(buf as *mut [T; 08]) = [val; 08] },
16 if UP_TO >= 16 => unsafe { *(buf as *mut [T; 16]) = [val; 16] },
32 if UP_TO >= 32 => unsafe { *(buf as *mut [T; 32]) = [val; 32] },
64 if UP_TO >= 64 => unsafe { *(buf as *mut [T; 64]) = [val; 64] },
_ => {
if WITH_DEFAULT {
for i in 0..self.len {
unsafe { buf.add(i).write(val) };
}
}
}
}
}
}

/// The entrypoint to the [`CaseSet`] API.
Expand Down
28 changes: 21 additions & 7 deletions src/lf_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use libc::ptrdiff_t;
use parking_lot::RwLock;
use std::cmp;
use std::ffi::c_int;
use std::mem;
use std::mem::MaybeUninit;
use std::ptr;

#[repr(C)]
pub struct Av1FilterLUT {
Expand Down Expand Up @@ -93,7 +96,7 @@ pub struct Av1Restoration {
/// Instead of offsetting `txa`, the offsets are calculated from
/// the existing `y_off` and `x_off` args and applied at each use site of `txa.
fn decomp_tx(
txa: &mut [[[[u8; 32]; 32]; 2]; 2],
txa: &mut [[[[MaybeUninit<u8>; 32]; 32]; 2]; 2],
from: TxfmSize,
depth: usize,
y_off: u8,
Expand Down Expand Up @@ -130,13 +133,15 @@ fn decomp_tx(

CaseSet::<16, false>::one((), t_dim.w as usize, x0, |case, ()| {
for y in 0..t_dim.h as usize {
case.set(&mut txa[0][0][y0 + y], lw);
case.set(&mut txa[1][0][y0 + y], lh);
txa[0][1][y0 + y][x0] = t_dim.w;
unsafe {
case.set_raw(ptr::from_mut(&mut txa[0][0][y0 + y]) as *mut [u8; 32], lw);
case.set_raw(ptr::from_mut(&mut txa[1][0][y0 + y]) as *mut [u8; 32], lh);
txa[0][1][y0 + y][x0].write(t_dim.w);
}
}
});
CaseSet::<16, false>::one((), t_dim.w as usize, x0, |case, ()| {
case.set(&mut txa[1][1][y0], t_dim.h);
CaseSet::<16, false>::one((), t_dim.w as usize, x0, |case, ()| unsafe {
case.set_raw(ptr::from_mut(&mut txa[1][1][y0]) as *mut [u8; 32], t_dim.h);
});
};
}
Expand All @@ -157,14 +162,23 @@ fn mask_edges_inter(
let t_dim = &dav1d_txfm_dimensions[max_tx as usize];

// See [`decomp_tx`]'s docs for the `txa` arg.
let mut txa = Align16([[[[0; 32]; 32]; 2]; 2]);

// SAFETY: We are building an array of uninitialized MaybeUninit values,
// which do not require initialization, so this `assume_init` is safe
let mut txa: Align16<[[[[MaybeUninit<u8>; 32]; 32]; 2]; 2]> =
unsafe { MaybeUninit::uninit().assume_init() };

for (y_off, _) in (0..h4).step_by(t_dim.h as usize).enumerate() {
for (x_off, _) in (0..w4).step_by(t_dim.w as usize).enumerate() {
decomp_tx(&mut txa.0, max_tx, 0, y_off as u8, x_off as u8, tx_masks);
}
}

// SAFETY: Calls to `decomp_tx` above fully initialize the `txa` array. We
// can't call `array_assume_init` because it is unstable, but transmute from
// fully initialized `MaybeUninit` to the same inner type is safe.
let txa: [[[[u8; 32]; 32]; 2]; 2] = unsafe { mem::transmute(txa) };

// left block edge
for y in 0..h4 {
let mask = 1u32 << (by4 + y);
Expand Down

0 comments on commit 293dfe8

Please sign in to comment.