1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
use std::fmt; use std::marker::PhantomData; use tract_data::internal::*; pub trait Lut: fmt::Debug + dyn_clone::DynClone + Send + Sync { fn table(&self) -> &[u8]; fn run(&self, buf: &mut [u8]); } dyn_clone::clone_trait_object!(Lut); #[derive(Debug, Clone)] pub struct LutImpl<K> where K: LutKer, { table: Tensor, _boo: PhantomData<K>, } impl<K> LutImpl<K> where K: LutKer, { pub fn new(table: &[u8]) -> LutImpl<K> { unsafe { LutImpl { table: Tensor::from_raw_aligned::<u8>( &[table.len()], table, K::table_alignment_bytes(), ) .unwrap(), _boo: PhantomData, } } } } impl<K> Lut for LutImpl<K> where K: LutKer, { fn table(&self) -> &[u8] { self.table.as_slice().unwrap() } fn run(&self, buf: &mut [u8]) { unsafe { let table: *const u8 = self.table.as_ptr_unchecked(); let align = K::input_alignment_bytes(); let aligned_start = (buf.as_ptr() as usize + align - 1) / align * align; let prefix = (aligned_start - buf.as_ptr() as usize).min(buf.len()); for i in 0..(prefix as isize) { let ptr = buf.as_mut_ptr().offset(i); *ptr = *table.offset(*ptr as isize); } let remaining = buf.len() - prefix; if remaining == 0 { return; } let n = K::n(); let aligned_len = remaining / n * n; if aligned_len > 0 { K::run(buf.as_mut_ptr().offset(prefix as isize), aligned_len, table); } let remaining = buf.len() - aligned_len - prefix; for i in 0..remaining { let ptr = buf.as_mut_ptr().offset((i + prefix + aligned_len) as isize); *ptr = *table.offset(*ptr as isize); } } } } pub trait LutKer: Clone + fmt::Debug + Send + Sync { fn name() -> &'static str; fn n() -> usize; fn input_alignment_bytes() -> usize; fn table_alignment_bytes() -> usize; fn run(buf: *mut u8, len: usize, table: *const u8); } #[cfg(test)] #[macro_use] pub mod test { use super::*; use proptest::prelude::*; #[derive(Debug)] pub struct LutProblem { pub table: Vec<u8>, pub data: Vec<u8>, } impl Arbitrary for LutProblem { type Parameters = (); type Strategy = BoxedStrategy<Self>; fn arbitrary_with(_p: ()) -> Self::Strategy { proptest::collection::vec(any::<u8>(), 1..256) .prop_flat_map(|table| { let data = proptest::collection::vec(0..table.len() as u8, 0..100); (Just(table), data) }) .prop_map(|(table, data)| LutProblem { table, data }) .boxed() } } impl LutProblem { pub fn reference(&self) -> Vec<u8> { self.data.iter().map(|x| self.table[*x as usize]).collect() } pub fn test<K: LutKer>(&self) -> Vec<u8> { let lut = LutImpl::<K>::new(&self.table); let mut data = self.data.clone(); lut.run(&mut data); data } } #[macro_export] macro_rules! lut_frame_tests { ($cond:expr, $ker:ty) => { mod lut { use proptest::prelude::*; #[allow(unused_imports)] use $crate::frame::lut::test::*; proptest::proptest! { #[test] fn lut_prop(pb in any::<LutProblem>()) { if $cond { prop_assert_eq!(pb.test::<$ker>(), pb.reference()) } } } #[test] fn test_empty() { let pb = LutProblem { table: vec![0], data: vec![] }; assert_eq!(pb.test::<$ker>(), pb.reference()) } } }; } }