annotate libtwamr/sqrt_l.c @ 521:68fe269b4316

gsmx-to-tw5a: actually write to the output file
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 19 Sep 2024 02:21:08 +0000
parents 8dfb7cbe6b59
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
308
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 *
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * R99 Version 3.3.0
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * REL-4 Version 4.1.0
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 *
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * File : sqrt_l.c
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * Purpose : Computes sqrt(L_x), where L_x is positive.
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 * : If L_var is negative or zero, the result is 0
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 * Description :
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 * The function sqrt(L_x) is approximated by a table and linear
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 * interpolation. The square root is computed using the
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 * following steps:
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 * 1- Normalization of L_x.
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 * 2- If exponent is even then shift right once.
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 * 3- exponent = exponent/2
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 * 4- i = bit25-b31 of L_x; 16<=i<=63 because of normalization.
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 * 5- a = bit10-b24
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 * 6- i -=16
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 * 7- L_y = table[i]<<16 - (table[i] - table[i+1]) * a * 2
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 * 8- return L_y and exponent so caller can do denormalization
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 *
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 /*
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 * MODULE INCLUDE FILE AND VERSION ID
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 #include "namespace.h"
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 #include "sqrt_l.h"
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 /*
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 * INCLUDE FILES
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 #include "typedef.h"
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 #include "basic_op.h"
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 #include "no_count.h"
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 /*
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 * LOCAL VARIABLES AND TABLES
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 #include "sqrt_l.tab" /* Table for sqrt_l_exp() */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 /*
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 * PUBLIC PROGRAM CODE
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 ********************************************************************************
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 Word32 sqrt_l_exp (/* o : output value, Q31 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 Word32 L_x, /* i : input value, Q31 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 Word16 *exp /* o : right shift to be applied to result, Q1 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 )
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 {
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 /*
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 y = sqrt(x)
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 x = f * 2^-e, 0.5 <= f < 1 (normalization)
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 y = sqrt(f) * 2^(-e/2)
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 a) e = 2k --> y = sqrt(f) * 2^-k (k = e div 2,
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 0.707 <= sqrt(f) < 1)
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 b) e = 2k+1 --> y = sqrt(f/2) * 2^-k (k = e div 2,
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 0.5 <= sqrt(f/2) < 0.707)
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 Word16 e, i, a, tmp;
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 Word32 L_y;
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 test ();
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (L_x <= (Word32) 0)
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 {
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 *exp = 0; move16 ();
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 return (Word32) 0;
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 e = norm_l (L_x) & 0xFFFE; logic16 (); /* get next lower EVEN norm. exp */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 L_x = L_shl (L_x, e); /* L_x is normalized to [0.25..1) */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 *exp = e; move16 (); /* return 2*exponent (or Q1) */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 L_x = L_shr (L_x, 9);
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 i = extract_h (L_x); /* Extract b25-b31, 16 <= i <= 63 because
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 of normalization */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 L_x = L_shr (L_x, 1);
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 a = extract_l (L_x); /* Extract b10-b24 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 a = a & (Word16) 0x7fff; logic16 ();
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 i = sub (i, 16); /* 0 <= i <= 47 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 L_y = L_deposit_h (table[i]); /* table[i] << 16 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 tmp = sub (table[i], table[i + 1]); /* table[i] - table[i+1]) */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 L_y = L_msu (L_y, tmp, a); /* L_y -= tmp*a*2 */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 /* L_y = L_shr (L_y, *exp); */ /* denormalization done by caller */
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 return (L_y);
8dfb7cbe6b59 libtwamr: integrated up to bgnscd.c
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 }