-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfexp_tet.c
303 lines (269 loc) · 6.29 KB
/
fexp_tet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/************************************************************************
*
* FILENAME : fexp_tet.c
*
* DESCRIPTION : Library of special functions for operations in
* extended precision used in the TETRA speech codec
*
************************************************************************
*
* FUNCTIONS : - L_comp()
* - L_extract()
* - mpy_mix()
* - mpy_32()
* - div_32()
*
************************************************************************
*
* INCLUDED FILES : source.h
*
************************************************************************
*
* COMMENTS : This subsection contains operations in double precision.
* These operations are non standard double precision
* operations.
*
* They are used where single precision is not enough but the
* full 32 bits precision is not necessary. For example, the
* function div_32() has a 24 bits precision.
*
* The double precision numbers use a special representation :
*
* L_32 = hi<<15 + lo
*
* L_32 is a 32 bit integer with b30 == b31.
* hi and lo are 16 bit signed integers.
* As the low part also contains the sign, this allows fast
* multiplication.
*
* 0xc000 0000 <= L_32 <= 0x3fff ffff.
*
* In general, DPF is used to specify this special
* format.
*
************************************************************************/
#include "source.h"
/************************************************************************
*
* Function Name : L_comp
*
* Purpose :
*
* Compose from two 16 bit DPF a normal 32 bit integer.
* L_32 = hi<<15 + lo
*
* Complexity Weight : 2
*
* Inputs :
*
* hi
* msb
*
* lo
* lsb (with sign)
*
* Outputs :
*
* none
*
* Returned Value :
*
* L_32
* 32 bit long signed integer (Word32) whose value falls in the
* range : 0xc000 0000 <= L_32 <= 0x3fff ffff.
*
************************************************************************/
Word32 L_comp(Word16 hi, Word16 lo)
{
return(add_sh( Load_sh( lo,(Word16)0 ), hi, (Word16)15 ));
}
/************************************************************************
*
* Function Name : L_extract
*
* Purpose :
*
* Extract from a 31 bit integer two 16 bit DPF.
*
* Complexity Weight : 5
*
* Inputs :
*
* L_32
* 32 bit long signed integer (Word32) with b30 == b31
* whose value falls in the range : 0xc000 0000 <= L_32 <= 0x3fff ffff.
*
* Outputs :
*
* hi
* b15 to b30 of L_32
*
* lo
* L_32 - hi<<15
*
* Returned Value :
*
* none
*
************************************************************************/
void L_extract(Word32 L_32, Word16 *hi, Word16 *lo)
{
*hi = extract_h( L_shl( L_32,(Word16)1 ) );
*lo = extract_l( sub_sh( L_32, *hi, (Word16)15 ) );
return;
}
/************************************************************************
*
* Function Name : mpy_mix
*
* Purpose :
*
* Multiply a 16 bit integer by a 32 bit (DPF).
* The result is divided by 2**16
* L_32 = hi1*lo2 + (lo1*lo2)>>15
*
* Complexity Weight : 4
*
* Inputs :
*
* hi1
* hi part of 32 bit number
*
* lo1
* lo part of 32 bit number
*
* lo2
* 16 bit number
*
* Outputs :
*
* none
*
* Returned Value :
*
* L_var_out
* 32 bit long signed integer (Word32) whose value falls in the
* range : 0x0000 0000 <= L_var_out <= 0x7fff ffff.
*
************************************************************************/
Word32 mpy_mix(Word16 hi1, Word16 lo1, Word16 lo2)
{
Word16 p1;
Word32 L_32;
p1 = extract_h(L_mult0(lo1, lo2));
L_32 = L_mult0(hi1,lo2 );
return(add_sh( L_32, p1, (Word16)1 ));
}
/************************************************************************
*
* Function Name : mpy_32
*
* Purpose :
*
* Multiply two 32 bit integers (DPF). The result is divided by 2**32
* L_32 = hi1*hi2 + (hi1*lo2)>>15 + (lo1*hi2)>>15)
*
* Complexity Weight : 7
*
* Inputs :
*
* hi1
* hi part of first number
*
* lo1
* lo part of first number
*
* hi2
* hi part of second number
*
* lo2
* lo part of second number
*
* Outputs :
*
* none
*
* Returned Value :
*
* L_var_out
* 32 bit long signed integer (Word32) whose value falls in the
* range : 0x0000 0000 <= L_var_out <= 0x7fff ffff.
*
*************************************************************************/
Word32 mpy_32(Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2)
{
Word16 p1, p2;
Word32 L_32;
p1 = extract_h(L_mult0(hi1, lo2));
p2 = extract_h(L_mult0(lo1, hi2));
L_32 = L_mult0(hi1, hi2);
L_32 = add_sh( L_32, p1, (Word16)1 );
return(add_sh( L_32, p2, (Word16)1 ));
}
/************************************************************************
*
* Function Name : div_32
*
* Purpose :
*
* Fractionnal integer division of two 32 bit numbers.
* L_num / L_denom
* L_num and L_denom must be positive and L_num < L_denom
* L_denom = denom_hi<<15 + denom_lo
* denom_hi is a normalized number
* The result is in Q30
*
* Complexity Weight : 52
*
* Inputs :
*
* L_num
* 32 bit long signed integer (Word32) whose value falls in the
* range : 0x0000 0000 <= L_num <= L_denom.
*
* (L_denom = denom_hi<<15 + denom_lo)
*
* denom_hi
* 16 bit normalized integer whose value falls in the
* range : 0x4000000 < hi < 0x7fff ffff.
*
* denom_lo
* 16 bit positive integer whose value falls in the
* range : 0 < lo < 0x7fff ffff.
*
* Outputs :
*
* none
*
* Returned Value :
*
* L_div
* 32 bit long signed integer (Word32) whose value falls in the
* range : 0x0000 0000 <= L_div <= 0x3fff ffff.
* L_div is a Q30 value (point between b30 and b29)
*
* Algorithm :
*
* - find = 1/L_denom
* First approximation: approx = 1 / denom_hi
* 1/L_denom = approx * (2.0 - L_denom * approx )
* - result = L_num * (1/L_denom)
*
************************************************************************/
Word32 div_32(Word32 L_num, Word16 denom_hi, Word16 denom_lo)
{
Word16 approx, hi, lo, n_hi, n_lo;
Word32 t0;
/* First approximation: 1 / L_denom = 1/denom_hi */
approx = div_s( (Word16)0x3fff, denom_hi); /* result in Q15 */
/* 1/L_denom = approx * (2.0 - L_denom * approx) */
t0 = mpy_mix(denom_hi, denom_lo, approx); /* result in Q29 */
t0 = L_sub( (Word32)0x40000000, t0); /* result in Q29 */
L_extract(t0, &hi, &lo);
t0 = mpy_mix(hi, lo, approx); /* = 1/L_denom in Q28 */
/* L_num * (1/L_denom) */
L_extract(t0, &hi, &lo);
L_extract(L_num, &n_hi, &n_lo);
t0 = mpy_32(n_hi, n_lo, hi, lo);
return( L_shl( t0,(Word16)2) ); /* From Q28 to Q30 */
}