pub use crate::fixed::{ MAX_16, MAX_32, add, div_s, l_add, l_mult, l_sub, mult, negate, sub, }; pub const MIN_16: i16 = -0x8000; pub const MIN_32: i32 = i32::MIN; use super::tables::{INV_SQRT_TBL, LOG2_TBL, POW2_TBL, SQRT_L_TBL}; #[inline] pub fn abs_s(a: i16) -> i16 { if a == MIN_16 { MAX_16 } else { a.abs() } } #[inline] pub fn shl_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 { if var2 < 0 { let n = -(var2 as i32); if n < 15 { var1 >> n } else { 0 } } else { let n = var2 as u32; let out: i16 = if n >= 32 { 0 } else { ((var1 as i32) << n) as i16 }; let back = if n >= 32 { 0 } else { (out as i32) >> n }; let chk = if n >= 16 { if out < 0 { -1 } else { 0 } } else { back }; if chk != var1 as i32 { *ovf = true; (var1 >> 15) ^ MAX_16 } else { out } } } #[inline] pub fn shl(var1: i16, var2: i16) -> i16 { shl_o(var1, var2, &mut false) } #[inline] pub fn shr_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 { if var2 == 0 { var1 } else if var2 > 0 { var1 >> (var2.min(15) as u32) } else { let n = ((-(var2 as i32)).min(15)) as u32; let result = ((var1 as i32) << n) as i16; if ((result as i32) >> n) != var1 as i32 { *ovf = true; if var1 > 0 { MAX_16 } else { MIN_16 } } else { result } } } #[inline] pub fn shr(var1: i16, var2: i16) -> i16 { shr_o(var1, var2, &mut false) } #[inline] pub fn shr_r(var1: i16, var2: i16) -> i16 { if var2 > 15 { 0 } else { let mut out = shr(var1, var2); if var2 > 0 && (var1 as i32 & (1 << (var2 - 1))) != 0 { out = out.wrapping_add(1); } out } } #[inline] pub fn l_add_o(a: i32, b: i32, ovf: &mut bool) -> i32 { match a.checked_add(b) { Some(v) => v, None => { *ovf = true; if a < 0 { MIN_32 } else { MAX_32 } } } } #[inline] pub fn l_mac_o(acc: i32, a: i16, b: i16, ovf: &mut bool) -> i32 { let p = a as i32 * b as i32; if p != 0x4000_0000 { l_add_o(acc, p << 1, ovf) } else { *ovf = true; MAX_32 } } #[inline] pub fn l_mac(acc: i32, a: i16, b: i16) -> i32 { l_mac_o(acc, a, b, &mut false) } #[inline] pub fn l_msu(acc: i32, a: i16, b: i16) -> i32 { l_sub(acc, l_mult(a, b)) } #[inline] pub fn l_shl_o(x: i32, n: i16, ovf: &mut bool) -> i32 { if n > 0 { let n = n as u32; let ok = if n >= 32 { x == 0 } else { ((x << n) >> n) == x }; if ok { if n >= 32 { 0 } else { x << n } } else { *ovf = true; (x >> 31) ^ MAX_32 } } else { let m = -(n as i32); if m < 31 { x >> m } else { 0 } } } #[inline] pub fn l_shl(x: i32, n: i16) -> i32 { l_shl_o(x, n, &mut false) } #[inline] pub fn l_shr_o(x: i32, n: i16, ovf: &mut bool) -> i32 { if n > 0 { if n < 31 { x >> n } else { 0 } } else { let m = -(n as i32); let ok = if m >= 32 { x == 0 } else { ((x << m) >> m) == x }; if ok { if m >= 32 { 0 } else { x << m } } else { *ovf = true; (x >> 31) ^ MAX_32 } } } #[inline] pub fn l_shr(x: i32, n: i16) -> i32 { l_shr_o(x, n, &mut false) } #[inline] pub fn l_shr_r(x: i32, n: i16) -> i32 { if n > 31 { 0 } else { let mut r = l_shr(x, n); if n > 0 && (x & (1i32 << (n - 1))) != 0 { r = r.wrapping_add(1); } r } } #[inline] pub fn round_(x: i32) -> i16 { (l_add(x, 0x8000) >> 16) as i16 } #[inline] pub fn norm_s(x: i16) -> i16 { if x == 0 { 0 } else { let y = x.wrapping_sub((x < 0) as i16); let v = y ^ (y >> 15); (v.leading_zeros() as i16) - 1 } } #[inline] pub fn norm_l(x: i32) -> i16 { if x == 0 { 0 } else { let y = x.wrapping_sub((x < 0) as i32); let v = y ^ (y >> 31); (v.leading_zeros() as i16) - 1 } } #[inline] pub fn mpy_32_16(hi: i16, lo: i16, v: i16) -> i32 { let mut p = hi as i32 * v as i32; p = if p != 0x4000_0000 { p << 1 } else { MAX_32 }; let r = (lo as i32 * v as i32) >> 15; let s = p.wrapping_add(r << 1); if (p ^ r) > 0 && ((s ^ p) >> 31) != 0 { return if (p >> 31) != 0 { MIN_32 } else { MAX_32 }; } s } #[inline] pub fn pow2(exponent: i16, fraction: i16) -> i32 { let lx = l_mult(fraction, 32); let i = (((lx >> 16) as i16) & 31) as usize; let a = ((lx >> 1) & 0x7fff) as i16; let mut x = (POW2_TBL[i] as i32) << 16; let tmp = POW2_TBL[i].wrapping_sub(POW2_TBL[i + 1]); x = l_msu(x, tmp, a); let exp = 30i16.wrapping_sub(exponent); l_shr_r(x, exp) } #[inline] pub fn log2_norm(x: i32, exp: i16, exponent: &mut i16, fraction: &mut i16) { if x <= 0 { *exponent = 0; *fraction = 0; } else { *exponent = 30i16.wrapping_sub(exp); let x = x >> 10; let i = ((x >> 15) as i16).wrapping_sub(32) as usize; let a = x & 0x7fff; let mut y = (LOG2_TBL[i] as i32) << 16; let tmp = LOG2_TBL[i].wrapping_sub(LOG2_TBL[i + 1]) as i32; y = y.wrapping_sub((tmp * a) << 1); *fraction = (y >> 16) as i16; } } #[inline] pub fn log2(l_x: i32, exponent: &mut i16, fraction: &mut i16) { let exp = norm_l(l_x); let r = l_x.wrapping_shl(exp as u32); log2_norm(r, exp, exponent, fraction); } #[inline] pub fn inv_sqrt(l_x: i32) -> i32 { if l_x <= 0 { return 0x3fff_ffff; } let mut exp = norm_l(l_x); let mut x = l_x << exp; exp = 30 - exp; if (exp & 1) == 0 { x >>= 1; } exp >>= 1; exp += 1; x >>= 9; let i = ((x >> 16) as i16 - 16) as usize; let a = ((x >> 1) as i16 & 0x7fff) as i32; let mut y = (INV_SQRT_TBL[i] as i32) << 16; let tmp = (INV_SQRT_TBL[i] - INV_SQRT_TBL[i + 1]) as i32; y = y.wrapping_sub((tmp * a) << 1); y >> exp } #[inline] pub fn sqrt_l_exp(l_x: i32, exp: &mut i16) -> i32 { if l_x <= 0 { *exp = 0; return 0; } let e = norm_l(l_x) & !1; let x = l_shl(l_x, e); *exp = e; let x = x >> 10; let mut i = ((x >> 15) & 63) as usize; let a = x as i16 & 0x7fff; if i > 15 { i -= 16; } let y = (SQRT_L_TBL[i] as i32) << 16; let tmp = SQRT_L_TBL[i].wrapping_sub(SQRT_L_TBL[i + 1]); l_msu(y, tmp, a) } #[inline] pub fn gmed_n(ind: &[i16], n: usize) -> i16 { let mut tmp2 = [0i16; 9]; let mut tmp = [0usize; 9]; tmp2[..n].copy_from_slice(&ind[..n]); let mut ix = 0usize; for i in 0..n { let mut max = -32767i16; for j in 0..n { if tmp2[j] >= max { max = tmp2[j]; ix = j; } } tmp2[ix] = -32768; tmp[i] = ix; } ind[tmp[n >> 1]] } #[cfg(test)] mod tests { use super::*; #[test] fn saturation() { assert_eq!(add(32767, 1), 32767); assert_eq!(sub(-32768, 1), -32768); assert_eq!(abs_s(-32768), 32767); assert_eq!(negate(-32768), 32767); assert_eq!(l_add(i32::MAX, 1), i32::MAX); assert_eq!(l_sub(i32::MIN, 1), i32::MIN); assert_eq!(l_mult(-32768, -32768), i32::MAX); assert_eq!(l_mac(5, -32768, -32768), i32::MAX); assert_eq!(mult(-32768, -32768), 32767); assert_eq!(shl(0x4000, 1), 32767); assert_eq!(shl(-0x4001, 1), -32768); assert_eq!(shl(3, 2), 12); assert_eq!(shl(-8, -2), -2); assert_eq!(shr(1, -15), 32767); assert_eq!(shr(-5, 1), -3); assert_eq!(shr_r(5, 1), 3); assert_eq!(l_shl(0x4000_0000, 1), i32::MAX); assert_eq!(l_shl(-0x4000_0001, 1), i32::MIN); assert_eq!(l_shr_r(5, 1), 3); assert_eq!(l_shr(1, -31), i32::MAX); } #[test] fn norms_and_math() { assert_eq!(norm_s(0), 0); assert_eq!(norm_s(1), 14); assert_eq!(norm_s(-1), 14); assert_eq!(norm_s(-32768), 0); assert_eq!(norm_l(1), 30); assert_eq!(norm_l(-1), 30); assert_eq!(norm_l(i32::MIN), 0); assert_eq!(div_s(1, 2), 16384); assert_eq!(div_s(5, 5), 32767); assert_eq!(round_(0x7fff_ffff), 32767); assert_eq!(pow2(14, 0), 16384); assert_eq!(inv_sqrt(0), 0x3fff_ffff); let (mut e, mut f) = (0, 0); log2(1 << 20, &mut e, &mut f); assert_eq!((e, f), (20, 0)); assert_eq!(gmed_n(&[5, 1, 9, 3, 7], 5), 5); } }