comparison libtwamr/sqrt_l.c @ 308:8dfb7cbe6b59

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