forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

basic.rs627 lines, 16.5 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#![allow(dead_code)]
6
7pub const MAX_16: i16 = 0x7fff;
8pub const MIN_16: i16 = -0x8000;
9pub const MAX_32: i32 = 0x7fff_ffff;
10pub const MIN_32: i32 = i32::MIN;
11
12use super::tables::{INV_SQRT_TBL, LOG2_TBL, POW2_TBL, SQRT_L_TBL};
13
14// ---------------------------------------------------------------- 16-bit
15
16/// C add (add_16). Saturating.
17#[inline]
18pub fn add_o(a: i16, b: i16, ovf: &mut bool) -> i16 {
19 let s = a as i32 + b as i32;
20 if s > 0x7fff {
21 *ovf = true;
22 MAX_16
23 } else if s < -32768 {
24 *ovf = true;
25 MIN_16
26 } else {
27 s as i16
28 }
29}
30#[inline]
31pub fn add(a: i16, b: i16) -> i16 {
32 (a as i32 + b as i32).clamp(-32768, 32767) as i16
33}
34/// C sub. Saturating.
35#[inline]
36pub fn sub_o(a: i16, b: i16, ovf: &mut bool) -> i16 {
37 let s = a as i32 - b as i32;
38 if s > 0x7fff {
39 *ovf = true;
40 MAX_16
41 } else if s < -32768 {
42 *ovf = true;
43 MIN_16
44 } else {
45 s as i16
46 }
47}
48#[inline]
49pub fn sub(a: i16, b: i16) -> i16 {
50 (a as i32 - b as i32).clamp(-32768, 32767) as i16
51}
52/// abs_s(-32768) = 32767.
53#[inline]
54pub fn abs_s(a: i16) -> i16 {
55 if a == MIN_16 {
56 MAX_16
57 } else {
58 a.abs()
59 }
60}
61/// negate(-32768) = 32767.
62#[inline]
63pub fn negate(a: i16) -> i16 {
64 if a == MIN_16 {
65 MAX_16
66 } else {
67 -a
68 }
69}
70/// (a*b)>>15, clipped to 0x7fff (only upper clip, as in C `mult`).
71#[inline]
72pub fn mult(a: i16, b: i16) -> i16 {
73 let p = (a as i32 * b as i32) >> 15;
74 if p > 0x7fff {
75 MAX_16
76 } else {
77 p as i16
78 }
79}
80/// (a*b + 0x4000)>>15 saturated.
81#[inline]
82pub fn mult_r_o(a: i16, b: i16, ovf: &mut bool) -> i16 {
83 let p = (a as i32 * b as i32 + 0x4000) >> 15;
84 if p > 0x7fff {
85 *ovf = true;
86 MAX_16
87 } else if p < -32768 {
88 *ovf = true;
89 MIN_16
90 } else {
91 p as i16
92 }
93}
94#[inline]
95pub fn mult_r(a: i16, b: i16) -> i16 {
96 mult_r_o(a, b, &mut false)
97}
98
99/// C shl (left shift with saturation; negative count shifts right, count <= -15 gives 0).
100#[inline]
101pub fn shl_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 {
102 if var2 < 0 {
103 let n = -(var2 as i32);
104 if n < 15 {
105 var1 >> n
106 } else {
107 0
108 }
109 } else {
110 let n = var2 as u32;
111 let out: i16 = if n >= 32 { 0 } else { ((var1 as i32) << n) as i16 };
112 let back = if n >= 32 { 0 } else { (out as i32) >> n };
113 // C: (var_out >> var2) != var1 (var2 >= 16 can only match for var1 == 0)
114 let chk = if n >= 16 { if out < 0 { -1 } else { 0 } } else { back };
115 if chk != var1 as i32 {
116 *ovf = true;
117 (var1 >> 15) ^ MAX_16
118 } else {
119 out
120 }
121 }
122}
123#[inline]
124pub fn shl(var1: i16, var2: i16) -> i16 {
125 shl_o(var1, var2, &mut false)
126}
127/// C shr (arithmetic right shift; negative count shifts left with saturation).
128#[inline]
129pub fn shr_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 {
130 if var2 == 0 {
131 var1
132 } else if var2 > 0 {
133 var1 >> (var2.min(15) as u32)
134 } else {
135 let n = ((-(var2 as i32)).min(15)) as u32;
136 let result = ((var1 as i32) << n) as i16;
137 if ((result as i32) >> n) != var1 as i32 {
138 *ovf = true;
139 if var1 > 0 { MAX_16 } else { MIN_16 }
140 } else {
141 result
142 }
143 }
144}
145#[inline]
146pub fn shr(var1: i16, var2: i16) -> i16 {
147 shr_o(var1, var2, &mut false)
148}
149/// shr with rounding.
150#[inline]
151pub fn shr_r(var1: i16, var2: i16) -> i16 {
152 if var2 > 15 {
153 0
154 } else {
155 let mut out = shr(var1, var2);
156 if var2 > 0 && (var1 as i32 & (1 << (var2 - 1))) != 0 {
157 out = out.wrapping_add(1);
158 }
159 out
160 }
161}
162#[inline]
163pub fn shr_r_o(var1: i16, var2: i16, ovf: &mut bool) -> i16 {
164 if var2 > 15 {
165 0
166 } else {
167 let mut out = shr_o(var1, var2, ovf);
168 if var2 > 0 && (var1 as i32 & (1 << (var2 - 1))) != 0 {
169 out = out.wrapping_add(1);
170 }
171 out
172 }
173}
174
175// ---------------------------------------------------------------- 32-bit
176
177#[inline]
178pub fn l_add_o(a: i32, b: i32, ovf: &mut bool) -> i32 {
179 match a.checked_add(b) {
180 Some(v) => v,
181 None => {
182 *ovf = true;
183 if a < 0 { MIN_32 } else { MAX_32 }
184 }
185 }
186}
187#[inline]
188pub fn l_add(a: i32, b: i32) -> i32 {
189 a.saturating_add(b)
190}
191#[inline]
192pub fn l_sub_o(a: i32, b: i32, ovf: &mut bool) -> i32 {
193 match a.checked_sub(b) {
194 Some(v) => v,
195 None => {
196 *ovf = true;
197 if a < 0 { MIN_32 } else { MAX_32 }
198 }
199 }
200}
201#[inline]
202pub fn l_sub(a: i32, b: i32) -> i32 {
203 a.saturating_sub(b)
204}
205/// (a*b)<<1, saturating (only 0x8000*0x8000 overflows).
206#[inline]
207pub fn l_mult(a: i16, b: i16) -> i32 {
208 let p = a as i32 * b as i32;
209 if p != 0x4000_0000 {
210 p << 1
211 } else {
212 MAX_32
213 }
214}
215/// Plain 16x16 product, no doubling or saturation.
216#[inline]
217pub fn l_mult0(a: i16, b: i16) -> i32 {
218 (a as i32).wrapping_mul(b as i32)
219}
220/// C L_mac: opencore semantics (0x4000_0000 product gives MAX_32).
221#[inline]
222pub fn l_mac_o(acc: i32, a: i16, b: i16, ovf: &mut bool) -> i32 {
223 let p = a as i32 * b as i32;
224 if p != 0x4000_0000 {
225 l_add_o(acc, p << 1, ovf)
226 } else {
227 *ovf = true;
228 MAX_32
229 }
230}
231#[inline]
232pub fn l_mac(acc: i32, a: i16, b: i16) -> i32 {
233 l_mac_o(acc, a, b, &mut false)
234}
235/// C L_msu = L_sub(acc, L_mult(a,b)).
236#[inline]
237pub fn l_msu_o(acc: i32, a: i16, b: i16, ovf: &mut bool) -> i32 {
238 l_sub_o(acc, l_mult(a, b), ovf)
239}
240#[inline]
241pub fn l_msu(acc: i32, a: i16, b: i16) -> i32 {
242 l_sub(acc, l_mult(a, b))
243}
244#[inline]
245pub fn l_negate(a: i32) -> i32 {
246 if a == MIN_32 { MAX_32 } else { -a }
247}
248#[inline]
249pub fn l_abs(a: i32) -> i32 {
250 if a == MIN_32 { MAX_32 } else { a.abs() }
251}
252#[inline]
253pub fn l_deposit_h(a: i16) -> i32 {
254 (a as i32) << 16
255}
256#[inline]
257pub fn l_deposit_l(a: i16) -> i32 {
258 a as i32
259}
260#[inline]
261pub fn extract_h(a: i32) -> i16 {
262 (a >> 16) as i16
263}
264#[inline]
265pub fn extract_l(a: i32) -> i16 {
266 a as i16
267}
268/// C L_shl: left shift with saturation (negative count: arithmetic right, count <= -31 gives 0).
269#[inline]
270pub fn l_shl_o(x: i32, n: i16, ovf: &mut bool) -> i32 {
271 if n > 0 {
272 let n = n as u32;
273 let ok = if n >= 32 { x == 0 } else { ((x << n) >> n) == x };
274 if ok {
275 if n >= 32 { 0 } else { x << n }
276 } else {
277 *ovf = true;
278 (x >> 31) ^ MAX_32
279 }
280 } else {
281 let m = -(n as i32);
282 if m < 31 { x >> m } else { 0 }
283 }
284}
285#[inline]
286pub fn l_shl(x: i32, n: i16) -> i32 {
287 l_shl_o(x, n, &mut false)
288}
289/// C L_shr: arithmetic right shift (count >= 31 gives 0, negative count shifts left saturating).
290#[inline]
291pub fn l_shr_o(x: i32, n: i16, ovf: &mut bool) -> i32 {
292 if n > 0 {
293 if n < 31 { x >> n } else { 0 }
294 } else {
295 let m = -(n as i32);
296 let ok = if m >= 32 { x == 0 } else { ((x << m) >> m) == x };
297 if ok {
298 if m >= 32 { 0 } else { x << m }
299 } else {
300 *ovf = true;
301 (x >> 31) ^ MAX_32
302 }
303 }
304}
305#[inline]
306pub fn l_shr(x: i32, n: i16) -> i32 {
307 l_shr_o(x, n, &mut false)
308}
309/// L_shr with rounding.
310#[inline]
311pub fn l_shr_r(x: i32, n: i16) -> i32 {
312 if n > 31 {
313 0
314 } else {
315 let mut r = l_shr(x, n);
316 if n > 0 && (x & (1i32 << (n - 1))) != 0 {
317 r = r.wrapping_add(1);
318 }
319 r
320 }
321}
322/// C round / pv_round: round 32 to high 16 bits.
323#[inline]
324pub fn round_(x: i32) -> i16 {
325 (l_add(x, 0x8000) >> 16) as i16
326}
327/// L_mac then round.
328#[inline]
329pub fn mac_r(acc: i32, a: i16, b: i16) -> i16 {
330 round_(l_mac(acc, a, b))
331}
332/// L_msu then round.
333#[inline]
334pub fn msu_r(acc: i32, a: i16, b: i16) -> i16 {
335 round_(l_msu(acc, a, b))
336}
337
338/// Normalisation shift count for 16-bit (0 for 0 input).
339#[inline]
340pub fn norm_s(x: i16) -> i16 {
341 if x == 0 {
342 0
343 } else {
344 // opencore: y = x - (x<0); v = y ^ (y>>15) (this equals |x|, not ~x)
345 let y = x.wrapping_sub((x < 0) as i16);
346 let v = y ^ (y >> 15);
347 (v.leading_zeros() as i16) - 1
348 }
349}
350/// Normalisation shift count for 32-bit (0 for 0 input).
351#[inline]
352pub fn norm_l(x: i32) -> i16 {
353 if x == 0 {
354 0
355 } else {
356 let y = x.wrapping_sub((x < 0) as i32);
357 let v = y ^ (y >> 31);
358 (v.leading_zeros() as i16) - 1
359 }
360}
361
362/// C div_s: Q15 fraction var1/var2; requires 0 <= var1 <= var2, else 0.
363#[inline]
364pub fn div_s(var1: i16, var2: i16) -> i16 {
365 if var1 > var2 || var1 < 0 {
366 return 0;
367 }
368 if var1 == 0 {
369 return 0;
370 }
371 if var1 == var2 {
372 return MAX_16;
373 }
374 let mut out: i16 = 0;
375 let mut num = var1 as i32;
376 let den = var2 as i32;
377 for _ in 0..5 {
378 out <<= 3;
379 num <<= 3;
380 if num >= den << 2 {
381 num -= den << 2;
382 out |= 4;
383 }
384 if num >= den << 1 {
385 num -= den << 1;
386 out |= 2;
387 }
388 if num >= den {
389 num -= den;
390 out |= 1;
391 }
392 }
393 out
394}
395
396// ---------------------------------------------------------------- 32-bit via hi/lo
397
398/// C L_Extract: split into (hi, lo) with lo in 0..0x7fff.
399#[inline]
400pub fn l_extract(x: i32, hi: &mut i16, lo: &mut i16) {
401 let h = (x >> 16) as i16;
402 *hi = h;
403 *lo = ((x >> 1) - ((h as i32) << 15)) as i16;
404}
405/// C L_Comp: inverse of l_extract.
406#[inline]
407pub fn l_comp(hi: i16, lo: i16) -> i32 {
408 l_mac(l_deposit_h(hi), lo, 1)
409}
410/// C Mpy_32: (hi1/lo1) * (hi2/lo2), result in Q31 (saturating as in C).
411#[inline]
412pub fn mpy_32(h1: i16, l1: i16, h2: i16, l2: i16) -> i32 {
413 let mut p = h1 as i32 * h2 as i32;
414 p = if p != 0x4000_0000 { p << 1 } else { MAX_32 };
415 let p32 = (h1 as i32 * l2 as i32) >> 15;
416 let s = p.wrapping_add(p32 << 1);
417 let mut sum = s;
418 if (p ^ p32) > 0 && ((s ^ p) >> 31) != 0 {
419 sum = if (p >> 31) != 0 { MIN_32 } else { MAX_32 };
420 }
421 let p = sum;
422 let p32 = (l1 as i32 * h2 as i32) >> 15;
423 let s = p.wrapping_add(p32 << 1);
424 if (p ^ p32) > 0 && ((s ^ p) >> 31) != 0 {
425 return if (p >> 31) != 0 { MIN_32 } else { MAX_32 };
426 }
427 s
428}
429/// C Mpy_32_16: (hi/lo) * var2.
430#[inline]
431pub fn mpy_32_16(hi: i16, lo: i16, v: i16) -> i32 {
432 let mut p = hi as i32 * v as i32;
433 p = if p != 0x4000_0000 { p << 1 } else { MAX_32 };
434 let r = (lo as i32 * v as i32) >> 15;
435 let s = p.wrapping_add(r << 1);
436 if (p ^ r) > 0 && ((s ^ p) >> 31) != 0 {
437 return if (p >> 31) != 0 { MIN_32 } else { MAX_32 };
438 }
439 s
440}
441/// C Mac_32: acc + (h1/l1)*(h2/l2).
442#[inline]
443pub fn mac_32(acc: i32, h1: i16, l1: i16, h2: i16, l2: i16) -> i32 {
444 let mut a = l_mac(acc, h1, h2);
445 a = l_mac(a, mult(h1, l2), 1);
446 l_mac(a, mult(l1, h2), 1)
447}
448/// C Mac_32_16: acc + (hi/lo)*var2.
449#[inline]
450pub fn mac_32_16(acc: i32, hi: i16, lo: i16, v: i16) -> i32 {
451 let a = l_mac(acc, hi, v);
452 l_mac(a, mult(lo, v), 1)
453}
454/// C Div_32: L_num / (denom_hi/denom_lo), result Q31.
455#[inline]
456pub fn div_32(l_num: i32, d_hi: i16, d_lo: i16) -> i32 {
457 let approx = div_s(0x3fff, d_hi);
458 let mut r = mpy_32_16(d_hi, d_lo, approx);
459 r = 0x7fff_ffff - r;
460 let hi = (r >> 16) as i16;
461 let lo = ((r >> 1) - ((hi as i32) << 15)) as i16;
462 r = mpy_32_16(hi, lo, approx);
463 let hi = (r >> 16) as i16;
464 let lo = ((r >> 1) - ((hi as i32) << 15)) as i16;
465 let n_hi = (l_num >> 16) as i16;
466 let n_lo = ((l_num >> 1) - ((n_hi as i32) << 15)) as i16;
467 r = mpy_32(n_hi, n_lo, hi, lo);
468 l_shl(r, 2)
469}
470
471// ---------------------------------------------------------------- math functions
472
473/// C Pow2: 2^(exponent + fraction/32768) as Q0 32-bit (exponent 0..30, fraction Q15).
474#[inline]
475pub fn pow2(exponent: i16, fraction: i16) -> i32 {
476 let lx = l_mult(fraction, 32);
477 let i = (((lx >> 16) as i16) & 31) as usize;
478 let a = ((lx >> 1) & 0x7fff) as i16;
479 let mut x = (POW2_TBL[i] as i32) << 16;
480 let tmp = POW2_TBL[i].wrapping_sub(POW2_TBL[i + 1]);
481 x = l_msu(x, tmp, a);
482 let exp = 30i16.wrapping_sub(exponent);
483 l_shr_r(x, exp)
484}
485/// C Log2_norm: x already normalised by `exp` shifts.
486#[inline]
487pub fn log2_norm(x: i32, exp: i16, exponent: &mut i16, fraction: &mut i16) {
488 if x <= 0 {
489 *exponent = 0;
490 *fraction = 0;
491 } else {
492 *exponent = 30i16.wrapping_sub(exp);
493 let x = x >> 10;
494 let i = ((x >> 15) as i16).wrapping_sub(32) as usize;
495 let a = (x & 0x7fff) as i32;
496 let mut y = (LOG2_TBL[i] as i32) << 16;
497 let tmp = LOG2_TBL[i].wrapping_sub(LOG2_TBL[i + 1]) as i32;
498 y = y.wrapping_sub((tmp * a) << 1);
499 *fraction = (y >> 16) as i16;
500 }
501}
502/// C Log2: log2(l_x) = exponent + fraction (Q15).
503#[inline]
504pub fn log2(l_x: i32, exponent: &mut i16, fraction: &mut i16) {
505 let exp = norm_l(l_x);
506 let r = l_x.wrapping_shl(exp as u32);
507 log2_norm(r, exp, exponent, fraction);
508}
509/// C Inv_sqrt: 1/sqrt(x), Q30 result (x Q0).
510#[inline]
511pub fn inv_sqrt(l_x: i32) -> i32 {
512 if l_x <= 0 {
513 return 0x3fff_ffff;
514 }
515 let mut exp = norm_l(l_x);
516 let mut x = l_x << exp;
517 exp = 30 - exp;
518 if (exp & 1) == 0 {
519 x >>= 1;
520 }
521 exp >>= 1;
522 exp += 1;
523 x >>= 9;
524 let i = ((x >> 16) as i16 - 16) as usize;
525 let a = ((x >> 1) as i16 & 0x7fff) as i32;
526 let mut y = (INV_SQRT_TBL[i] as i32) << 16;
527 let tmp = (INV_SQRT_TBL[i] - INV_SQRT_TBL[i + 1]) as i32;
528 y = y.wrapping_sub((tmp * a) << 1);
529 y >> exp
530}
531/// C sqrt_l_exp: returns sqrt mantissa, exponent in *exp.
532#[inline]
533pub fn sqrt_l_exp(l_x: i32, exp: &mut i16) -> i32 {
534 if l_x <= 0 {
535 *exp = 0;
536 return 0;
537 }
538 let e = norm_l(l_x) & !1;
539 let x = l_shl(l_x, e);
540 *exp = e;
541 let x = x >> 10;
542 let mut i = ((x >> 15) & 63) as usize;
543 let a = (x as i16 & 0x7fff) as i16;
544 if i > 15 {
545 i -= 16;
546 }
547 let y = (SQRT_L_TBL[i] as i32) << 16;
548 let tmp = SQRT_L_TBL[i].wrapping_sub(SQRT_L_TBL[i + 1]);
549 l_msu(y, tmp, a)
550}
551/// C gmed_n: median of the first n (<= 9) values of ind (n odd as used by the codec).
552#[inline]
553pub fn gmed_n(ind: &[i16], n: usize) -> i16 {
554 let mut tmp2 = [0i16; 9];
555 let mut tmp = [0usize; 9];
556 tmp2[..n].copy_from_slice(&ind[..n]);
557 let mut ix = 0usize;
558 for i in 0..n {
559 let mut max = -32767i16;
560 for j in 0..n {
561 if tmp2[j] >= max {
562 max = tmp2[j];
563 ix = j;
564 }
565 }
566 tmp2[ix] = -32768;
567 tmp[i] = ix;
568 }
569 ind[tmp[n >> 1]]
570}
571
572#[cfg(test)]
573mod tests {
574 use super::*;
575
576 #[test]
577 fn saturation() {
578 assert_eq!(add(32767, 1), 32767);
579 assert_eq!(sub(-32768, 1), -32768);
580 assert_eq!(abs_s(-32768), 32767);
581 assert_eq!(negate(-32768), 32767);
582 assert_eq!(l_add(i32::MAX, 1), i32::MAX);
583 assert_eq!(l_sub(i32::MIN, 1), i32::MIN);
584 assert_eq!(l_mult(-32768, -32768), i32::MAX);
585 assert_eq!(l_mac(5, -32768, -32768), i32::MAX);
586 assert_eq!(mult(-32768, -32768), 32767);
587 assert_eq!(mult_r(-32768, -32768), 32767);
588 assert_eq!(shl(0x4000, 1), 32767);
589 assert_eq!(shl(-0x4001, 1), -32768);
590 assert_eq!(shl(3, 2), 12);
591 assert_eq!(shl(-8, -2), -2);
592 assert_eq!(shr(1, -15), 32767);
593 assert_eq!(shr(-5, 1), -3);
594 assert_eq!(shr_r(5, 1), 3);
595 assert_eq!(l_shl(0x4000_0000, 1), i32::MAX);
596 assert_eq!(l_shl(-0x4000_0001, 1), i32::MIN);
597 assert_eq!(l_shr_r(5, 1), 3);
598 assert_eq!(l_shr(1, -31), i32::MAX);
599 let mut o = false;
600 add_o(32767, 1, &mut o);
601 assert!(o);
602 }
603
604 #[test]
605 fn norms_and_math() {
606 assert_eq!(norm_s(0), 0);
607 assert_eq!(norm_s(1), 14);
608 assert_eq!(norm_s(-1), 14);
609 assert_eq!(norm_s(-32768), 0);
610 assert_eq!(norm_l(1), 30);
611 assert_eq!(norm_l(-1), 30);
612 assert_eq!(norm_l(i32::MIN), 0);
613 assert_eq!(div_s(1, 2), 16384);
614 assert_eq!(div_s(5, 5), 32767);
615 assert_eq!(round_(0x7fff_ffff), 32767);
616 assert_eq!(pow2(14, 0), 16384);
617 assert_eq!(inv_sqrt(0), 0x3fff_ffff);
618 let (mut e, mut f) = (0, 0);
619 log2(1 << 20, &mut e, &mut f);
620 assert_eq!((e, f), (20, 0));
621 assert_eq!(gmed_n(&[5, 1, 9, 3, 7], 5), 5);
622 let (mut h, mut l) = (0, 0);
623 l_extract(0x1234_5678, &mut h, &mut l);
624 assert_eq!(l_comp(h, l), 0x1234_5678);
625 }
626}
627