forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust381.4 MiBcdccbc2Tahinli10 hours ago4263 commits

basic.rs446 lines, 11.9 KiBHistoryRawDownload
1//! AMR-NB basic operations (saturating 16/32-bit fixed point), bit-exact with the
2//! opencore-amr C-equivalent basic_op. Word16 = i16, Word32 = i32.
3//! Plain functions have no overflow flag; `*_o` variants additionally set `*ovf = true`
4//! when the result saturated. `round_` is C `round`/`pv_round`.
5
6pub const MAX_16: i16 = 0x7fff;
7pub const MIN_16: i16 = -0x8000;
8pub const MAX_32: i32 = 0x7fff_ffff;
9pub const MIN_32: i32 = i32::MIN;
10
11use super::tables::{INV_SQRT_TBL, LOG2_TBL, POW2_TBL, SQRT_L_TBL};
12
13// ---------------------------------------------------------------- 16-bit
14
15#[inline]
16pub fn add(a: i16, b: i16) -> i16 {
17 (a as i32 + b as i32).clamp(-32768, 32767) as i16
18}
19#[inline]
20pub fn sub(a: i16, b: i16) -> i16 {
21 (a as i32 - b as i32).clamp(-32768, 32767) as i16
22}
23/// abs_s(-32768) = 32767.
24#[inline]
25pub fn abs_s(a: i16) -> i16 {
26 if a == MIN_16 {
27 MAX_16
28 } else {
29 a.abs()
30 }
31}
32/// negate(-32768) = 32767.
33#[inline]
34pub fn negate(a: i16) -> i16 {
35 if a == MIN_16 {
36 MAX_16
37 } else {
38 -a
39 }
40}
41/// (a*b)>>15, clipped to 0x7fff (only upper clip, as in C `mult`).
42#[inline]
43pub fn mult(a: i16, b: i16) -> i16 {
44 let p = (a as i32 * b as i32) >> 15;
45 if p > 0x7fff {
46 MAX_16
47 } else {
48 p as i16
49 }
50}
51
52/// C shl (left shift with saturation; negative count shifts right, count <= -15 gives 0).
53#[inline]
54pub fn shl_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 {
55 if var2 < 0 {
56 let n = -(var2 as i32);
57 if n < 15 {
58 var1 >> n
59 } else {
60 0
61 }
62 } else {
63 let n = var2 as u32;
64 let out: i16 = if n >= 32 { 0 } else { ((var1 as i32) << n) as i16 };
65 let back = if n >= 32 { 0 } else { (out as i32) >> n };
66 // C: (var_out >> var2) != var1 (var2 >= 16 can only match for var1 == 0)
67 let chk = if n >= 16 { if out < 0 { -1 } else { 0 } } else { back };
68 if chk != var1 as i32 {
69 *ovf = true;
70 (var1 >> 15) ^ MAX_16
71 } else {
72 out
73 }
74 }
75}
76#[inline]
77pub fn shl(var1: i16, var2: i16) -> i16 {
78 shl_o(var1, var2, &mut false)
79}
80/// C shr (arithmetic right shift; negative count shifts left with saturation).
81#[inline]
82pub fn shr_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 {
83 if var2 == 0 {
84 var1
85 } else if var2 > 0 {
86 var1 >> (var2.min(15) as u32)
87 } else {
88 let n = ((-(var2 as i32)).min(15)) as u32;
89 let result = ((var1 as i32) << n) as i16;
90 if ((result as i32) >> n) != var1 as i32 {
91 *ovf = true;
92 if var1 > 0 { MAX_16 } else { MIN_16 }
93 } else {
94 result
95 }
96 }
97}
98#[inline]
99pub fn shr(var1: i16, var2: i16) -> i16 {
100 shr_o(var1, var2, &mut false)
101}
102/// shr with rounding.
103#[inline]
104pub fn shr_r(var1: i16, var2: i16) -> i16 {
105 if var2 > 15 {
106 0
107 } else {
108 let mut out = shr(var1, var2);
109 if var2 > 0 && (var1 as i32 & (1 << (var2 - 1))) != 0 {
110 out = out.wrapping_add(1);
111 }
112 out
113 }
114}
115
116// ---------------------------------------------------------------- 32-bit
117
118#[inline]
119pub fn l_add_o(a: i32, b: i32, ovf: &mut bool) -> i32 {
120 match a.checked_add(b) {
121 Some(v) => v,
122 None => {
123 *ovf = true;
124 if a < 0 { MIN_32 } else { MAX_32 }
125 }
126 }
127}
128#[inline]
129pub fn l_add(a: i32, b: i32) -> i32 {
130 a.saturating_add(b)
131}
132#[inline]
133pub fn l_sub(a: i32, b: i32) -> i32 {
134 a.saturating_sub(b)
135}
136/// (a*b)<<1, saturating (only 0x8000*0x8000 overflows).
137#[inline]
138pub fn l_mult(a: i16, b: i16) -> i32 {
139 let p = a as i32 * b as i32;
140 if p != 0x4000_0000 {
141 p << 1
142 } else {
143 MAX_32
144 }
145}
146/// C L_mac: opencore semantics (0x4000_0000 product gives MAX_32).
147#[inline]
148pub fn l_mac_o(acc: i32, a: i16, b: i16, ovf: &mut bool) -> i32 {
149 let p = a as i32 * b as i32;
150 if p != 0x4000_0000 {
151 l_add_o(acc, p << 1, ovf)
152 } else {
153 *ovf = true;
154 MAX_32
155 }
156}
157#[inline]
158pub fn l_mac(acc: i32, a: i16, b: i16) -> i32 {
159 l_mac_o(acc, a, b, &mut false)
160}
161#[inline]
162pub fn l_msu(acc: i32, a: i16, b: i16) -> i32 {
163 l_sub(acc, l_mult(a, b))
164}
165/// C L_shl: left shift with saturation (negative count: arithmetic right, count <= -31 gives 0).
166#[inline]
167pub fn l_shl_o(x: i32, n: i16, ovf: &mut bool) -> i32 {
168 if n > 0 {
169 let n = n as u32;
170 let ok = if n >= 32 { x == 0 } else { ((x << n) >> n) == x };
171 if ok {
172 if n >= 32 { 0 } else { x << n }
173 } else {
174 *ovf = true;
175 (x >> 31) ^ MAX_32
176 }
177 } else {
178 let m = -(n as i32);
179 if m < 31 { x >> m } else { 0 }
180 }
181}
182#[inline]
183pub fn l_shl(x: i32, n: i16) -> i32 {
184 l_shl_o(x, n, &mut false)
185}
186/// C L_shr: arithmetic right shift (count >= 31 gives 0, negative count shifts left saturating).
187#[inline]
188pub fn l_shr_o(x: i32, n: i16, ovf: &mut bool) -> i32 {
189 if n > 0 {
190 if n < 31 { x >> n } else { 0 }
191 } else {
192 let m = -(n as i32);
193 let ok = if m >= 32 { x == 0 } else { ((x << m) >> m) == x };
194 if ok {
195 if m >= 32 { 0 } else { x << m }
196 } else {
197 *ovf = true;
198 (x >> 31) ^ MAX_32
199 }
200 }
201}
202#[inline]
203pub fn l_shr(x: i32, n: i16) -> i32 {
204 l_shr_o(x, n, &mut false)
205}
206/// L_shr with rounding.
207#[inline]
208pub fn l_shr_r(x: i32, n: i16) -> i32 {
209 if n > 31 {
210 0
211 } else {
212 let mut r = l_shr(x, n);
213 if n > 0 && (x & (1i32 << (n - 1))) != 0 {
214 r = r.wrapping_add(1);
215 }
216 r
217 }
218}
219/// C round / pv_round: round 32 to high 16 bits.
220#[inline]
221pub fn round_(x: i32) -> i16 {
222 (l_add(x, 0x8000) >> 16) as i16
223}
224
225/// Normalisation shift count for 16-bit (0 for 0 input).
226#[inline]
227pub fn norm_s(x: i16) -> i16 {
228 if x == 0 {
229 0
230 } else {
231 // opencore: y = x - (x<0); v = y ^ (y>>15) (this equals |x|, not ~x)
232 let y = x.wrapping_sub((x < 0) as i16);
233 let v = y ^ (y >> 15);
234 (v.leading_zeros() as i16) - 1
235 }
236}
237/// Normalisation shift count for 32-bit (0 for 0 input).
238#[inline]
239pub fn norm_l(x: i32) -> i16 {
240 if x == 0 {
241 0
242 } else {
243 let y = x.wrapping_sub((x < 0) as i32);
244 let v = y ^ (y >> 31);
245 (v.leading_zeros() as i16) - 1
246 }
247}
248
249/// C div_s: Q15 fraction var1/var2; requires 0 <= var1 <= var2, else 0.
250#[inline]
251pub fn div_s(var1: i16, var2: i16) -> i16 {
252 if var1 > var2 || var1 < 0 {
253 return 0;
254 }
255 if var1 == 0 {
256 return 0;
257 }
258 if var1 == var2 {
259 return MAX_16;
260 }
261 let mut out: i16 = 0;
262 let mut num = var1 as i32;
263 let den = var2 as i32;
264 for _ in 0..5 {
265 out <<= 3;
266 num <<= 3;
267 if num >= den << 2 {
268 num -= den << 2;
269 out |= 4;
270 }
271 if num >= den << 1 {
272 num -= den << 1;
273 out |= 2;
274 }
275 if num >= den {
276 num -= den;
277 out |= 1;
278 }
279 }
280 out
281}
282
283// ---------------------------------------------------------------- 32-bit via hi/lo
284
285/// C Mpy_32_16: (hi/lo) * var2.
286#[inline]
287pub fn mpy_32_16(hi: i16, lo: i16, v: i16) -> i32 {
288 let mut p = hi as i32 * v as i32;
289 p = if p != 0x4000_0000 { p << 1 } else { MAX_32 };
290 let r = (lo as i32 * v as i32) >> 15;
291 let s = p.wrapping_add(r << 1);
292 if (p ^ r) > 0 && ((s ^ p) >> 31) != 0 {
293 return if (p >> 31) != 0 { MIN_32 } else { MAX_32 };
294 }
295 s
296}
297
298// ---------------------------------------------------------------- math functions
299
300/// C Pow2: 2^(exponent + fraction/32768) as Q0 32-bit (exponent 0..30, fraction Q15).
301#[inline]
302pub fn pow2(exponent: i16, fraction: i16) -> i32 {
303 let lx = l_mult(fraction, 32);
304 let i = (((lx >> 16) as i16) & 31) as usize;
305 let a = ((lx >> 1) & 0x7fff) as i16;
306 let mut x = (POW2_TBL[i] as i32) << 16;
307 let tmp = POW2_TBL[i].wrapping_sub(POW2_TBL[i + 1]);
308 x = l_msu(x, tmp, a);
309 let exp = 30i16.wrapping_sub(exponent);
310 l_shr_r(x, exp)
311}
312/// C Log2_norm: x already normalised by `exp` shifts.
313#[inline]
314pub fn log2_norm(x: i32, exp: i16, exponent: &mut i16, fraction: &mut i16) {
315 if x <= 0 {
316 *exponent = 0;
317 *fraction = 0;
318 } else {
319 *exponent = 30i16.wrapping_sub(exp);
320 let x = x >> 10;
321 let i = ((x >> 15) as i16).wrapping_sub(32) as usize;
322 let a = x & 0x7fff;
323 let mut y = (LOG2_TBL[i] as i32) << 16;
324 let tmp = LOG2_TBL[i].wrapping_sub(LOG2_TBL[i + 1]) as i32;
325 y = y.wrapping_sub((tmp * a) << 1);
326 *fraction = (y >> 16) as i16;
327 }
328}
329/// C Log2: log2(l_x) = exponent + fraction (Q15).
330#[inline]
331pub fn log2(l_x: i32, exponent: &mut i16, fraction: &mut i16) {
332 let exp = norm_l(l_x);
333 let r = l_x.wrapping_shl(exp as u32);
334 log2_norm(r, exp, exponent, fraction);
335}
336/// C Inv_sqrt: 1/sqrt(x), Q30 result (x Q0).
337#[inline]
338pub fn inv_sqrt(l_x: i32) -> i32 {
339 if l_x <= 0 {
340 return 0x3fff_ffff;
341 }
342 let mut exp = norm_l(l_x);
343 let mut x = l_x << exp;
344 exp = 30 - exp;
345 if (exp & 1) == 0 {
346 x >>= 1;
347 }
348 exp >>= 1;
349 exp += 1;
350 x >>= 9;
351 let i = ((x >> 16) as i16 - 16) as usize;
352 let a = ((x >> 1) as i16 & 0x7fff) as i32;
353 let mut y = (INV_SQRT_TBL[i] as i32) << 16;
354 let tmp = (INV_SQRT_TBL[i] - INV_SQRT_TBL[i + 1]) as i32;
355 y = y.wrapping_sub((tmp * a) << 1);
356 y >> exp
357}
358/// C sqrt_l_exp: returns sqrt mantissa, exponent in *exp.
359#[inline]
360pub fn sqrt_l_exp(l_x: i32, exp: &mut i16) -> i32 {
361 if l_x <= 0 {
362 *exp = 0;
363 return 0;
364 }
365 let e = norm_l(l_x) & !1;
366 let x = l_shl(l_x, e);
367 *exp = e;
368 let x = x >> 10;
369 let mut i = ((x >> 15) & 63) as usize;
370 let a = x as i16 & 0x7fff;
371 if i > 15 {
372 i -= 16;
373 }
374 let y = (SQRT_L_TBL[i] as i32) << 16;
375 let tmp = SQRT_L_TBL[i].wrapping_sub(SQRT_L_TBL[i + 1]);
376 l_msu(y, tmp, a)
377}
378/// C gmed_n: median of the first n (<= 9) values of ind (n odd as used by the codec).
379#[inline]
380pub fn gmed_n(ind: &[i16], n: usize) -> i16 {
381 let mut tmp2 = [0i16; 9];
382 let mut tmp = [0usize; 9];
383 tmp2[..n].copy_from_slice(&ind[..n]);
384 let mut ix = 0usize;
385 for i in 0..n {
386 let mut max = -32767i16;
387 for j in 0..n {
388 if tmp2[j] >= max {
389 max = tmp2[j];
390 ix = j;
391 }
392 }
393 tmp2[ix] = -32768;
394 tmp[i] = ix;
395 }
396 ind[tmp[n >> 1]]
397}
398
399#[cfg(test)]
400mod tests {
401 use super::*;
402
403 #[test]
404 fn saturation() {
405 assert_eq!(add(32767, 1), 32767);
406 assert_eq!(sub(-32768, 1), -32768);
407 assert_eq!(abs_s(-32768), 32767);
408 assert_eq!(negate(-32768), 32767);
409 assert_eq!(l_add(i32::MAX, 1), i32::MAX);
410 assert_eq!(l_sub(i32::MIN, 1), i32::MIN);
411 assert_eq!(l_mult(-32768, -32768), i32::MAX);
412 assert_eq!(l_mac(5, -32768, -32768), i32::MAX);
413 assert_eq!(mult(-32768, -32768), 32767);
414 assert_eq!(shl(0x4000, 1), 32767);
415 assert_eq!(shl(-0x4001, 1), -32768);
416 assert_eq!(shl(3, 2), 12);
417 assert_eq!(shl(-8, -2), -2);
418 assert_eq!(shr(1, -15), 32767);
419 assert_eq!(shr(-5, 1), -3);
420 assert_eq!(shr_r(5, 1), 3);
421 assert_eq!(l_shl(0x4000_0000, 1), i32::MAX);
422 assert_eq!(l_shl(-0x4000_0001, 1), i32::MIN);
423 assert_eq!(l_shr_r(5, 1), 3);
424 assert_eq!(l_shr(1, -31), i32::MAX);
425 }
426
427 #[test]
428 fn norms_and_math() {
429 assert_eq!(norm_s(0), 0);
430 assert_eq!(norm_s(1), 14);
431 assert_eq!(norm_s(-1), 14);
432 assert_eq!(norm_s(-32768), 0);
433 assert_eq!(norm_l(1), 30);
434 assert_eq!(norm_l(-1), 30);
435 assert_eq!(norm_l(i32::MIN), 0);
436 assert_eq!(div_s(1, 2), 16384);
437 assert_eq!(div_s(5, 5), 32767);
438 assert_eq!(round_(0x7fff_ffff), 32767);
439 assert_eq!(pow2(14, 0), 16384);
440 assert_eq!(inv_sqrt(0), 0x3fff_ffff);
441 let (mut e, mut f) = (0, 0);
442 log2(1 << 20, &mut e, &mut f);
443 assert_eq!((e, f), (20, 0));
444 assert_eq!(gmed_n(&[5, 1, 9, 3, 7], 5), 5);
445 }
446}