1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20 
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24 
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30 
31  Pathname: ./audio/gsm-amr/c/src/sqrt_l.c
32 
33 ------------------------------------------------------------------------------
34  REVISION HISTORY
35 
36 
37  Description: Updated template. Changed function interface to pass in a
38               pointer to overflow flag into the function instead of using a
39               global flag. Changed name of an input pointer from "exp" to "pExp"
40               for clarity. Removed inclusion of unwanted header files.
41 
42  Description: Removed inclusion of sqrt_l.tab file. Changed the array name
43               "table" to "sqrt_l_tbl". Fixed typos.
44 
45  Who:                           Date:
46  Description:
47 
48 ------------------------------------------------------------------------------
49 */
50 
51 /*----------------------------------------------------------------------------
52 ; INCLUDES
53 ----------------------------------------------------------------------------*/
54 #include    "sqrt_l.h"
55 #include    "typedef.h"
56 #include    "basic_op.h"
57 
58 /*----------------------------------------------------------------------------
59 ; MACROS
60 ; Define module specific macros here
61 ----------------------------------------------------------------------------*/
62 
63 
64 /*----------------------------------------------------------------------------
65 ; DEFINES
66 ; Include all pre-processor statements here. Include conditional
67 ; compile variables also.
68 ----------------------------------------------------------------------------*/
69 
70 
71 /*----------------------------------------------------------------------------
72 ; LOCAL FUNCTION DEFINITIONS
73 ; Function Prototype declaration
74 ----------------------------------------------------------------------------*/
75 
76 
77 /*----------------------------------------------------------------------------
78 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
79 ; Variable declaration - defined here and used outside this module
80 ----------------------------------------------------------------------------*/
81 
82 
83 /*
84 ------------------------------------------------------------------------------
85  FUNCTION NAME: sqrt_l_exp
86 ------------------------------------------------------------------------------
87  INPUT AND OUTPUT DEFINITIONS
88 
89  Inputs:
90     L_x = input value (Word32)
91     pExp = pointer to right shift to be applied to result
92     pOverflow = pointer to overflow flag
93 
94  Outputs:
95     pOverflow -> if the Inv_sqrt operation resulted in an overflow.
96 
97  Returns:
98     L_y = squareroot of L_x (Word32)
99 
100  Global Variables Used:
101     None.
102 
103  Local Variables Needed:
104     None.
105 
106 ------------------------------------------------------------------------------
107  FUNCTION DESCRIPTION
108 
109  This function computes sqrt(L_x),  where  L_x is positive.
110  If L_var is negative or zero, the result is 0
111 
112  The function sqrt(L_x) is approximated by a table and linear
113  interpolation. The square root is computed using the
114  following steps:
115     1- Normalization of L_x.
116     2- If exponent is even then shift right once.
117     3- exponent = exponent/2
118     4- i = bit25-b31 of L_x;  16<=i<=63  because of normalization.
119     5- a = bit10-b24
120     6- i -=16
121     7- L_y = table[i]<<16 - (table[i] - table[i+1]) * a * 2
122     8- return L_y and exponent so caller can do denormalization
123 
124 ------------------------------------------------------------------------------
125  REQUIREMENTS
126 
127  None.
128 
129 ------------------------------------------------------------------------------
130  REFERENCES
131 
132  sqrt_l.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
133 
134 ------------------------------------------------------------------------------
135  PSEUDO-CODE
136 
137 Word32 sqrt_l_exp (     // o : output value
138     Word32 L_x,         // i : input value
139     Word16 *exp         // o : right shift to be applied to result
140 )
141 {
142 
143 //          y = sqrt(x)
144 //          x = f * 2^-e,   0.5 <= f < 1   (normalization)
145 //          y = sqrt(f) * 2^(-e/2)
146 //
147 //          a) e = 2k   --> y = sqrt(f)   * 2^-k  (k = e div 2,
148 //                                                 0.707 <= sqrt(f) < 1)
149 //          b) e = 2k+1 --> y = sqrt(f/2) * 2^-k  (k = e div 2,
150                                                  0.5 <= sqrt(f/2) < 0.707)
151 
152 
153     Word16 e, i, a, tmp;
154     Word32 L_y;
155 
156     if (L_x <= (Word32) 0)
157     {
158         *exp = 0;
159         return (Word32) 0;
160     }
161 
162 * The reference ETSI code uses a global Overflow flag. In the actual
163 * implementation a pointer to the overflow flag is passed into the function.
164 * This pointer is in turn passed into the basic math functions such as add(),
165 * L_shl(), L_shr(), sub() called by this module.
166 
167     e = norm_l (L_x) & 0xFFFE;              // get next lower EVEN norm. exp
168     L_x = L_shl (L_x, e);                   // L_x is normalized to [0.25..1)
169     *exp = e;                               // return 2*exponent (or Q1)
170 
171     L_x = L_shr (L_x, 9);
172     i = extract_h (L_x);                    // Extract b25-b31, 16 <= i <= 63
173                                                 because of normalization
174     L_x = L_shr (L_x, 1);
175     a = extract_l (L_x);                    // Extract b10-b24
176     a = a & (Word16) 0x7fff;
177 
178     i = sub (i, 16);                        // 0 <= i <= 47
179 
180     L_y = L_deposit_h (table[i]);           // table[i] << 16
181     tmp = sub (table[i], table[i + 1]);     // table[i] - table[i+1])
182     L_y = L_msu (L_y, tmp, a);              // L_y -= tmp*a*2
183 
184     return (L_y);
185 }
186 
187 ------------------------------------------------------------------------------
188  RESOURCES USED [optional]
189 
190  When the code is written for a specific target processor the
191  the resources used should be documented below.
192 
193  HEAP MEMORY USED: x bytes
194 
195  STACK MEMORY USED: x bytes
196 
197  CLOCK CYCLES: (cycle count equation for this function) + (variable
198                 used to represent cycle count for each subroutine
199                 called)
200      where: (cycle count variable) = cycle count for [subroutine
201                                      name]
202 
203 ------------------------------------------------------------------------------
204  CAUTION [optional]
205  [State any special notes, constraints or cautions for users of this function]
206 
207 ------------------------------------------------------------------------------
208 */
209 
sqrt_l_exp(Word32 L_x,Word16 * pExp,Flag * pOverflow)210 Word32 sqrt_l_exp(      /* o : output value,                          Q31 */
211     Word32 L_x,         /* i : input value,                           Q31 */
212     Word16 *pExp,       /* o : right shift to be applied to result,   Q1  */
213     Flag   *pOverflow   /* i : pointer to overflow flag */
214 )
215 
216 {
217     Word16 e;
218     Word16 i;
219     Word16 a;
220     Word16 tmp;
221     Word32 L_y;
222 
223     /*
224           y = sqrt(x)
225           x = f * 2^-e,   0.5 <= f < 1   (normalization)
226           y = sqrt(f) * 2^(-e/2)
227           a) e = 2k   --> y = sqrt(f)   * 2^-k  (k = e div 2,
228                                                  0.707 <= sqrt(f) < 1)
229           b) e = 2k+1 --> y = sqrt(f/2) * 2^-k  (k = e div 2,
230                                                  0.5 <= sqrt(f/2) < 0.707)
231      */
232 
233     if (L_x <= (Word32) 0)
234     {
235         *pExp = 0;
236         return (Word32) 0;
237     }
238 
239     e = norm_l(L_x) & 0xFFFE;               /* get next lower EVEN norm. exp  */
240     L_x = L_shl(L_x, e, pOverflow);         /* L_x is normalized to [0.25..1) */
241     *pExp = e;                              /* return 2*exponent (or Q1)      */
242 
243     L_x >>= 10;
244     i = (Word16)(L_x >> 15) & 63;            /* Extract b25-b31, 16<= i <=63  */
245     /* because of normalization       */
246 
247     a = (Word16)(L_x);                      /* Extract b10-b24 */
248     a &= (Word16) 0x7fff;
249 
250     if (i > 15)
251     {
252         i -= 16;                              /* 0 <= i <= 47                   */
253     }
254 
255     L_y = L_deposit_h(sqrt_l_tbl[i]);       /* sqrt_l_tbl[i] << 16            */
256 
257     /* sqrt_l_tbl[i] - sqrt_l_tbl[i+1]) */
258     tmp = sub(sqrt_l_tbl[i], sqrt_l_tbl[i + 1], pOverflow);
259 
260     L_y = L_msu(L_y, tmp, a, pOverflow);    /* L_y -= tmp*a*2                 */
261 
262     /* L_y = L_shr (L_y, *exp); */          /* denormalization done by caller */
263 
264     return (L_y);
265 }
266 
267