| 1 | |
| 2 | |
| 3 | |
| 4 | #[inline] |
| 5 | pub(crate) fn smulwb(a: i32, b: i32) -> i32 { |
| 6 | (((a >> 16) * (b as i16 as i32)) as i64 + ((((a & 0xFFFF) * (b as i16 as i32)) >> 16) as i64)) |
| 7 | as i32 |
| 8 | } |
| 9 | |
| 10 | #[inline] |
| 11 | pub(crate) fn smlawb(a: i32, b: i32, c: i32) -> i32 { |
| 12 | a.wrapping_add(smulwb(b, c)) |
| 13 | } |
| 14 | |
| 15 | #[inline] |
| 16 | pub(crate) fn smulbb(a: i32, b: i32) -> i32 { |
| 17 | (a as i16 as i32).wrapping_mul(b as i16 as i32) |
| 18 | } |
| 19 | |
| 20 | #[inline] |
| 21 | pub(crate) fn smultt(a: i32, b: i32) -> i32 { |
| 22 | (a >> 16) * (b >> 16) |
| 23 | } |
| 24 | |
| 25 | #[inline] |
| 26 | pub(crate) fn smlabb(a: i32, b: i32, c: i32) -> i32 { |
| 27 | a.wrapping_add(smulbb(b, c)) |
| 28 | } |
| 29 | |
| 30 | #[inline] |
| 31 | pub(crate) fn rshift_round(a: i32, shift: u32) -> i32 { |
| 32 | if shift == 1 { |
| 33 | (a >> 1) + (a & 1) |
| 34 | } else { |
| 35 | ((a >> (shift - 1)) + 1) >> 1 |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[inline] |
| 40 | pub(crate) fn rshift_round64(a: i64, shift: u32) -> i64 { |
| 41 | if shift == 1 { |
| 42 | (a >> 1) + (a & 1) |
| 43 | } else { |
| 44 | ((a >> (shift - 1)) + 1) >> 1 |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | #[inline] |
| 49 | pub(crate) fn smulww(a: i32, b: i32) -> i32 { |
| 50 | ((a as i64 * b as i64) >> 16) as i32 |
| 51 | } |
| 52 | |
| 53 | #[inline] |
| 54 | pub(crate) fn smlaww(a: i32, b: i32, c: i32) -> i32 { |
| 55 | a.wrapping_add(((b as i64 * c as i64) >> 16) as i32) |
| 56 | } |
| 57 | |
| 58 | |
| 59 | #[inline] |
| 60 | pub(crate) fn smmul(a: i32, b: i32) -> i32 { |
| 61 | ((a as i64 * b as i64) >> 32) as i32 |
| 62 | } |
| 63 | |
| 64 | #[inline] |
| 65 | pub(crate) fn sat16(a: i32) -> i16 { |
| 66 | a.clamp(-32768, 32767) as i16 |
| 67 | } |
| 68 | |
| 69 | #[inline] |
| 70 | pub(crate) fn clz32(x: i32) -> i32 { |
| 71 | if x == 0 { |
| 72 | 32 |
| 73 | } else { |
| 74 | (x as u32).leading_zeros() as i32 |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | #[inline] |
| 80 | pub(crate) fn clz_frac(x: i32) -> (i32, i32) { |
| 81 | let lz = clz32(x); |
| 82 | let frac = (x as u32).rotate_right((24 - lz) as u32) as i32 & 0x7F; |
| 83 | (lz, frac) |
| 84 | } |
| 85 | |
| 86 | pub(crate) fn sqrt_approx(x: i32) -> i32 { |
| 87 | if x <= 0 { |
| 88 | return 0; |
| 89 | } |
| 90 | let (lz, frac) = clz_frac(x); |
| 91 | let mut y = if lz & 1 != 0 { 32768 } else { 46214 }; |
| 92 | y >>= lz >> 1; |
| 93 | smlawb(y, y, smulbb(213, frac)) |
| 94 | } |
| 95 | |
| 96 | pub(crate) fn log2lin(log_q7: i32) -> i32 { |
| 97 | if log_q7 < 0 { |
| 98 | return 0; |
| 99 | } |
| 100 | if log_q7 >= 3967 { |
| 101 | return i32::MAX; |
| 102 | } |
| 103 | let mut out = 1i32 << (log_q7 >> 7); |
| 104 | let frac = log_q7 & 0x7F; |
| 105 | let adj = smlawb(frac, smulbb(frac, 128 - frac), -174); |
| 106 | if log_q7 < 2048 { |
| 107 | out += ((out as i64 * adj as i64) >> 7) as i32; |
| 108 | } else { |
| 109 | out = out.wrapping_add((out >> 7).wrapping_mul(adj)); |
| 110 | } |
| 111 | out |
| 112 | } |
| 113 | |
| 114 | |
| 115 | pub(crate) fn inverse32_var_q(b32: i32, qres: u32) -> i32 { |
| 116 | debug_assert!(b32 != 0); |
| 117 | let b_headrm = clz32(b32.abs()) - 1; |
| 118 | let b32_nrm = b32 << b_headrm; |
| 119 | let b32_inv = (i32::MAX >> 2) / (b32_nrm >> 16); |
| 120 | let mut result = b32_inv << 16; |
| 121 | let err_q32 = ((1 << 29) - smulwb(b32_nrm, b32_inv)) << 3; |
| 122 | result = smlaww(result, err_q32, b32_inv); |
| 123 | let lshift = 61 - b_headrm - qres as i32; |
| 124 | if lshift <= 0 { |
| 125 | lshift_sat32(result, -lshift) |
| 126 | } else if lshift < 32 { |
| 127 | result >> lshift |
| 128 | } else { |
| 129 | 0 |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | pub(crate) fn div32_var_q(a32: i32, b32: i32, qres: u32) -> i32 { |
| 135 | debug_assert!(b32 != 0); |
| 136 | let a_headrm = clz32(a32.abs()) - 1; |
| 137 | let mut a32_nrm = a32 << a_headrm; |
| 138 | let b_headrm = clz32(b32.abs()) - 1; |
| 139 | let b32_nrm = b32 << b_headrm; |
| 140 | let b32_inv = (i32::MAX >> 2) / (b32_nrm >> 16); |
| 141 | let mut result = smulwb(a32_nrm, b32_inv); |
| 142 | a32_nrm = a32_nrm.wrapping_sub(smmul(b32_nrm, result).wrapping_shl(3)); |
| 143 | result = smlawb(result, a32_nrm, b32_inv); |
| 144 | let lshift = 29 + a_headrm - b_headrm - qres as i32; |
| 145 | if lshift < 0 { |
| 146 | lshift_sat32(result, -lshift) |
| 147 | } else if lshift < 32 { |
| 148 | result >> lshift |
| 149 | } else { |
| 150 | 0 |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | pub(crate) fn lshift_sat32(a: i32, shift: i32) -> i32 { |
| 155 | let lim = i32::MAX >> shift; |
| 156 | a.clamp(-lim - 1, lim) << shift |
| 157 | } |
| 158 | |
| 159 | |
| 160 | #[inline] |
| 161 | pub(crate) fn silk_rand(seed: i32) -> i32 { |
| 162 | (907633515i32).wrapping_add(seed.wrapping_mul(196314165)) |
| 163 | } |
| 164 | |
| 165 | #[inline] |
| 166 | pub(crate) fn smulwt(a: i32, b: i32) -> i32 { |
| 167 | (a >> 16) |
| 168 | .wrapping_mul(b >> 16) |
| 169 | .wrapping_add(((a & 0xFFFF) * (b >> 16)) >> 16) |
| 170 | } |
| 171 | |
| 172 | #[inline] |
| 173 | pub(crate) fn smlawt(a: i32, b: i32, c: i32) -> i32 { |
| 174 | a.wrapping_add(smulwt(b, c)) |
| 175 | } |
| 176 | |
| 177 | #[inline] |
| 178 | pub(crate) fn add_sat32(a: i32, b: i32) -> i32 { |
| 179 | a.saturating_add(b) |
| 180 | } |
| 181 | |
| 182 | #[inline] |
| 183 | pub(crate) fn sub_sat32(a: i32, b: i32) -> i32 { |
| 184 | a.saturating_sub(b) |
| 185 | } |
| 186 | |
| 187 | |
| 188 | pub(crate) fn lin2log(in_lin: i32) -> i32 { |
| 189 | let (lz, frac_q7) = clz_frac(in_lin); |
| 190 | smlawb(frac_q7, frac_q7 * (128 - frac_q7), 179).wrapping_add((31 - lz) << 7) |
| 191 | } |
| 192 | |
| 193 | pub(crate) fn add_pos_sat32(a: i32, b: i32) -> i32 { |
| 194 | let s = a.wrapping_add(b); |
| 195 | if s < 0 { i32::MAX } else { s } |
| 196 | } |