//! SILK fixed-point macros (`silk/macros.h`, `SigProc_FIX.h`, `Inlines.h`), //! shared by the decoder and the encoder. #[inline] pub(crate) fn smulwb(a: i32, b: i32) -> i32 { (((a >> 16) * (b as i16 as i32)) as i64 + ((((a & 0xFFFF) * (b as i16 as i32)) >> 16) as i64)) as i32 } #[inline] pub(crate) fn smlawb(a: i32, b: i32, c: i32) -> i32 { a.wrapping_add(smulwb(b, c)) } #[inline] pub(crate) fn smulbb(a: i32, b: i32) -> i32 { (a as i16 as i32).wrapping_mul(b as i16 as i32) } #[inline] pub(crate) fn smultt(a: i32, b: i32) -> i32 { (a >> 16) * (b >> 16) } #[inline] pub(crate) fn smlabb(a: i32, b: i32, c: i32) -> i32 { a.wrapping_add(smulbb(b, c)) } #[inline] pub(crate) fn rshift_round(a: i32, shift: u32) -> i32 { if shift == 1 { (a >> 1) + (a & 1) } else { ((a >> (shift - 1)) + 1) >> 1 } } #[inline] pub(crate) fn rshift_round64(a: i64, shift: u32) -> i64 { if shift == 1 { (a >> 1) + (a & 1) } else { ((a >> (shift - 1)) + 1) >> 1 } } #[inline] pub(crate) fn smulww(a: i32, b: i32) -> i32 { ((a as i64 * b as i64) >> 16) as i32 } #[inline] pub(crate) fn smlaww(a: i32, b: i32, c: i32) -> i32 { a.wrapping_add(((b as i64 * c as i64) >> 16) as i32) } /// `silk_SMMUL`: the top 32 bits of a 32x32 product. #[inline] pub(crate) fn smmul(a: i32, b: i32) -> i32 { ((a as i64 * b as i64) >> 32) as i32 } #[inline] pub(crate) fn sat16(a: i32) -> i16 { a.clamp(-32768, 32767) as i16 } #[inline] pub(crate) fn clz32(x: i32) -> i32 { if x == 0 { 32 } else { (x as u32).leading_zeros() as i32 } } /// `silk_CLZ_FRAC`: leading zeros plus the seven bits below the leading one. #[inline] pub(crate) fn clz_frac(x: i32) -> (i32, i32) { let lz = clz32(x); let frac = (x as u32).rotate_right((24 - lz) as u32) as i32 & 0x7F; (lz, frac) } pub(crate) fn sqrt_approx(x: i32) -> i32 { if x <= 0 { return 0; } let (lz, frac) = clz_frac(x); let mut y = if lz & 1 != 0 { 32768 } else { 46214 }; y >>= lz >> 1; smlawb(y, y, smulbb(213, frac)) } pub(crate) fn log2lin(log_q7: i32) -> i32 { if log_q7 < 0 { return 0; } if log_q7 >= 3967 { return i32::MAX; } let mut out = 1i32 << (log_q7 >> 7); let frac = log_q7 & 0x7F; let adj = smlawb(frac, smulbb(frac, 128 - frac), -174); if log_q7 < 2048 { out += ((out as i64 * adj as i64) >> 7) as i32; } else { out = out.wrapping_add((out >> 7).wrapping_mul(adj)); } out } /// `silk_INVERSE32_varQ`: `(1 << q) / b`, to about 30 bits. pub(crate) fn inverse32_var_q(b32: i32, qres: u32) -> i32 { debug_assert!(b32 != 0); let b_headrm = clz32(b32.abs()) - 1; let b32_nrm = b32 << b_headrm; let b32_inv = (i32::MAX >> 2) / (b32_nrm >> 16); let mut result = b32_inv << 16; let err_q32 = ((1 << 29) - smulwb(b32_nrm, b32_inv)) << 3; result = smlaww(result, err_q32, b32_inv); let lshift = 61 - b_headrm - qres as i32; if lshift <= 0 { lshift_sat32(result, -lshift) } else if lshift < 32 { result >> lshift } else { 0 } } /// `silk_DIV32_varQ`: `(a << q) / b`. pub(crate) fn div32_var_q(a32: i32, b32: i32, qres: u32) -> i32 { debug_assert!(b32 != 0); let a_headrm = clz32(a32.abs()) - 1; let mut a32_nrm = a32 << a_headrm; let b_headrm = clz32(b32.abs()) - 1; let b32_nrm = b32 << b_headrm; let b32_inv = (i32::MAX >> 2) / (b32_nrm >> 16); let mut result = smulwb(a32_nrm, b32_inv); a32_nrm = a32_nrm.wrapping_sub(smmul(b32_nrm, result).wrapping_shl(3)); result = smlawb(result, a32_nrm, b32_inv); let lshift = 29 + a_headrm - b_headrm - qres as i32; if lshift < 0 { lshift_sat32(result, -lshift) } else if lshift < 32 { result >> lshift } else { 0 } } pub(crate) fn lshift_sat32(a: i32, shift: i32) -> i32 { let lim = i32::MAX >> shift; a.clamp(-lim - 1, lim) << shift } /// `silk_RAND`: the excitation sign PRNG. #[inline] pub(crate) fn silk_rand(seed: i32) -> i32 { (907633515i32).wrapping_add(seed.wrapping_mul(196314165)) } #[inline] pub(crate) fn smulwt(a: i32, b: i32) -> i32 { (a >> 16) .wrapping_mul(b >> 16) .wrapping_add(((a & 0xFFFF) * (b >> 16)) >> 16) } #[inline] pub(crate) fn smlawt(a: i32, b: i32, c: i32) -> i32 { a.wrapping_add(smulwt(b, c)) } #[inline] pub(crate) fn add_sat32(a: i32, b: i32) -> i32 { a.saturating_add(b) } #[inline] pub(crate) fn sub_sat32(a: i32, b: i32) -> i32 { a.saturating_sub(b) } /// `silk_lin2log`: approximation of `128 * log2(x)`. pub(crate) fn lin2log(in_lin: i32) -> i32 { let (lz, frac_q7) = clz_frac(in_lin); smlawb(frac_q7, frac_q7 * (128 - frac_q7), 179).wrapping_add((31 - lz) << 7) } pub(crate) fn add_pos_sat32(a: i32, b: i32) -> i32 { let s = a.wrapping_add(b); if s < 0 { i32::MAX } else { s } }