|
1 |
| -include!(concat!(env!("OUT_DIR"), "/lut.rs")); |
| 1 | +mod codegen; |
| 2 | + |
| 3 | +const MAX_FLOAT_BITS: u32 = 0x3f7fffff; // 1.0 - f32::EPSILON |
| 4 | + |
| 5 | +// SAFETY: Only use this macro if `input` is clamped between `min_float` and `max_float`. |
| 6 | +macro_rules! linear_float_to_encoded_uint { |
| 7 | + ($enc:ty, $lut:ty, $input:ident, $min_float_bits:ident, $table:ident, $bit_width:expr, $man_index_width:expr) => {{ |
| 8 | + let input_bits = $input.to_bits(); |
| 9 | + #[cfg(test)] |
| 10 | + { |
| 11 | + debug_assert!(($min_float_bits..=MAX_FLOAT_BITS).contains(&$input.to_bits())); |
| 12 | + } |
| 13 | + let entry = { |
| 14 | + let i = ((input_bits - $min_float_bits) >> (23 - $man_index_width)) as usize; |
| 15 | + #[cfg(test)] |
| 16 | + { |
| 17 | + debug_assert!($table.get(i).is_some()); |
| 18 | + } |
| 19 | + unsafe { *$table.get_unchecked(i) } |
| 20 | + }; |
| 21 | + |
| 22 | + let bias = (entry >> (2 * $bit_width)) << ($bit_width + 1); |
| 23 | + let scale = entry & ((1 << (2 * $bit_width)) - 1); |
| 24 | + let t = |
| 25 | + (input_bits as $lut >> (23 - $man_index_width - $bit_width)) & ((1 << $bit_width) - 1); |
| 26 | + let res = (bias + scale * t) >> (2 * $bit_width); |
| 27 | + #[cfg(test)] |
| 28 | + { |
| 29 | + debug_assert!(res < ((<$enc>::MAX as $lut) + 1), "{}", res); |
| 30 | + } |
| 31 | + res as $enc |
| 32 | + }}; |
| 33 | +} |
| 34 | + |
| 35 | +#[inline] |
| 36 | +fn linear_f32_to_encoded_u8(linear: f32, min_float_bits: u32, table: &[u32]) -> u8 { |
| 37 | + let min_float = f32::from_bits(min_float_bits); |
| 38 | + let max_float = f32::from_bits(MAX_FLOAT_BITS); |
| 39 | + |
| 40 | + let mut input = linear; |
| 41 | + if input.partial_cmp(&min_float) != Some(core::cmp::Ordering::Greater) { |
| 42 | + input = min_float; |
| 43 | + } else if input > max_float { |
| 44 | + input = max_float; |
| 45 | + } |
| 46 | + |
| 47 | + linear_float_to_encoded_uint!(u8, u32, input, min_float_bits, table, 8, 3) |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(feature = "gamma_lut_u16")] |
| 51 | +#[inline] |
| 52 | +fn linear_f32_to_encoded_u16_with_linear_scale( |
| 53 | + linear: f32, |
| 54 | + linear_scale: f32, |
| 55 | + min_float_bits: u32, |
| 56 | + table: &[u64], |
| 57 | +) -> u16 { |
| 58 | + let min_float = f32::from_bits(min_float_bits); |
| 59 | + let max_float = f32::from_bits(MAX_FLOAT_BITS); |
| 60 | + |
| 61 | + let mut input = linear; |
| 62 | + if input.partial_cmp(&0.0) != Some(core::cmp::Ordering::Greater) { |
| 63 | + input = 0.0; |
| 64 | + } else if input > max_float { |
| 65 | + input = max_float; |
| 66 | + } |
| 67 | + |
| 68 | + if input < min_float { |
| 69 | + return ((linear_scale * input + 8388608.0).to_bits() & 65535) as u16; |
| 70 | + } |
| 71 | + |
| 72 | + linear_float_to_encoded_uint!(u16, u64, input, min_float_bits, table, 16, 7) |
| 73 | +} |
0 commit comments