-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoper_32b.c
313 lines (270 loc) · 13.8 KB
/
oper_32b.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
303
304
305
306
307
308
309
310
311
312
313
/*
ITU-T G.729A Speech Coder ANSI-C Source Code
Version 1.1 Last modified: September 1996
Copyright (c) 1996,
AT&T, France Telecom, NTT, Universite de Sherbrooke, Lucent Technologies
All rights reserved.
*/
#include "typedef.h"
#include "basic_op.h"
#include "oper_32b.h"
/*___________________________________________________________________________
| |
| This file contains operations in double precision. |
| These operations are not 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 which is enough for our purposes. |
| |
| The double precision numbers use a special representation: |
| |
| L_32 = hi<<16 + lo<<1 |
| |
| L_32 is a 32 bit integer. |
| hi and lo are 16 bit signed integers. |
| As the low part also contains the sign, this allows fast multiplication. |
| |
| 0x8000 0000 <= L_32 <= 0x7fff fffe. |
| |
| We will use DPF (Double Precision Format )in this file to specify |
| this special format. |
|___________________________________________________________________________|
*/
/*___________________________________________________________________________
| |
| Function L_Extract() |
| |
| Extract from a 32 bit integer two 16 bit DPF. |
| |
| Arguments: |
| |
| L_32 : 32 bit integer. |
| 0x8000 0000 <= L_32 <= 0x7fff ffff. |
| hi : b16 to b31 of L_32 |
| lo : (L_32 - hi<<16)>>1 |
|___________________________________________________________________________|
*/
void L_Extract(int32_t L_32, int16_t *hi, int16_t *lo)
{
*hi = extract_h(L_32);
*lo = extract_l( L_msu( L_shr(L_32, 1) , *hi, 16384)); /* lo = L_32>>1 */
return;
}
/*___________________________________________________________________________
| |
| Function L_Comp() |
| |
| Compose from two 16 bit DPF a 32 bit integer. |
| |
| L_32 = hi<<16 + lo<<1 |
| |
| Arguments: |
| |
| hi msb |
| lo lsf (with sign) |
| |
| Return Value : |
| |
| 32 bit long signed integer (int32_t) whose value falls in the |
| range : 0x8000 0000 <= L_32 <= 0x7fff fff0. |
| |
|___________________________________________________________________________|
*/
int32_t L_Comp(int16_t hi, int16_t lo)
{
int32_t L_32;
L_32 = L_deposit_h(hi);
return( L_mac(L_32, lo, 1)); /* = hi<<16 + lo<<1 */
}
/*___________________________________________________________________________
| Function Mpy_32() |
| |
| Multiply two 32 bit integers (DPF). The result is divided by 2**31 |
| |
| L_32 = (hi1*hi2)<<1 + ( (hi1*lo2)>>15 + (lo1*hi2)>>15 )<<1 |
| |
| This operation can also be viewed as the multiplication of two Q31 |
| number and the result is also in Q31. |
| |
| Arguments: |
| |
| hi1 hi part of first number |
| lo1 lo part of first number |
| hi2 hi part of second number |
| lo2 lo part of second number |
| |
|___________________________________________________________________________|
*/
G729_INLINE int32_t Mpy_32(int16_t hi1, int16_t lo1, int16_t hi2, int16_t lo2)
{
#if G729_ARM
register int32_t product32;
register int32_t L_sum;
register int32_t L_product, result;
register int32_t ra = hi1;
register int32_t rb = lo1;
register int32_t rc = hi2;
register int32_t rd = lo2;
asm volatile("smulbb %0, %1, %2"
: "=r"(L_product)
: "r"(ra), "r"(rc)
);
asm volatile("mov %0, #0"
: "=r"(result)
);
asm volatile("qdadd %0, %1, %2"
: "=r"(L_sum)
: "r"(result), "r"(L_product)
);
asm volatile("smulbb %0, %1, %2"
: "=r"(product32)
: "r"(ra), "r"(rd)
);
asm volatile("mov %0, %1, ASR #15"
: "=r"(ra)
: "r"(product32)
);
asm volatile("qdadd %0, %1, %2"
: "=r"(L_product)
: "r"(L_sum), "r"(ra)
);
asm volatile("smulbb %0, %1, %2"
: "=r"(product32)
: "r"(rb), "r"(rc)
);
asm volatile("mov %0, %1, ASR #15"
: "=r"(rb)
: "r"(product32)
);
asm volatile("qdadd %0, %1, %2"
: "=r"(L_sum)
: "r"(L_product), "r"(rb)
);
return (L_sum);
#else
int32_t L_32;
L_32 = L_mult(hi1, hi2);
L_32 = L_mac(L_32, mult(hi1, lo2) , 1);
L_32 = L_mac(L_32, mult(lo1, hi2) , 1);
return( L_32 );
#endif
}
/*___________________________________________________________________________
| Function Mpy_32_16() |
| |
| Multiply a 16 bit integer by a 32 bit (DPF). The result is divided |
| by 2**15 |
| |
| This operation can also be viewed as the multiplication of a Q31 |
| number by a Q15 number, the result is in Q31. |
| |
| L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1 |
| |
| Arguments: |
| |
| hi hi part of 32 bit number. |
| lo lo part of 32 bit number. |
| n 16 bit number. |
| |
|___________________________________________________________________________|
*/
G729_INLINE int32_t Mpy_32_16(int16_t hi, int16_t lo, int16_t n)
{
#if G729_ARM
register int32_t ra = hi;
register int32_t rb = lo;
register int32_t rc = n;
int32_t result, L_product;
asm volatile("smulbb %0, %1, %2"
: "=r"(L_product)
: "r"(ra), "r"(rc)
);
asm volatile("mov %0, #0"
: "=r"(result)
);
asm volatile("qdadd %0, %1, %2"
: "=r"(L_product)
: "r"(result), "r"(L_product)
);
asm volatile("smulbb %0, %1, %2"
: "=r"(result)
: "r"(rb), "r"(rc)
);
asm volatile("mov %0, %1, ASR #15"
: "=r"(ra)
: "r"(result)
);
asm volatile("qdadd %0, %1, %2"
: "=r"(result)
: "r"(L_product), "r"(ra)
);
return (result);
#else
int32_t L_32;
L_32 = L_mult(hi, n);
L_32 = L_mac(L_32, mult(lo, n) , 1);
return( L_32 );
#endif
}
/*___________________________________________________________________________
| |
| Function Name : Div_32 |
| |
| Purpose : |
| Fractional 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<<16 + denom_lo<<1 |
| denom_hi is a normalize number. |
| The result is in Q30. |
| |
| Inputs : |
| |
| L_num |
| 32 bit long signed integer (int32_t) whose value falls in the |
| range : 0x0000 0000 < L_num < L_denom |
| |
| L_denom = denom_hi<<16 + denom_lo<<1 (DPF) |
| |
| denom_hi |
| 16 bit positive normalized integer whose value falls in the |
| range : 0x4000 < hi < 0x7fff |
| denom_lo |
| 16 bit positive integer whose value falls in the |
| range : 0 < lo < 0x7fff |
| |
| Return Value : |
| |
| L_div |
| 32 bit long signed integer (int32_t) whose value falls in the |
| range : 0x0000 0000 <= L_div <= 0x7fff ffff. |
| It's a Q31 value |
| |
| 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) |
|___________________________________________________________________________|
*/
int32_t Div_32(int32_t L_num, int16_t denom_hi, int16_t denom_lo)
{
int16_t approx, hi, lo, n_hi, n_lo;
int32_t L_32;
/* First approximation: 1 / L_denom = 1/denom_hi */
approx = div_s( (int16_t)0x3fff, denom_hi); /* result in Q14 */
/* Note: 3fff = 0.5 in Q15 */
/* 1/L_denom = approx * (2.0 - L_denom * approx) */
L_32 = Mpy_32_16(denom_hi, denom_lo, approx); /* result in Q30 */
L_32 = L_sub( (int32_t)0x7fffffffL, L_32); /* result in Q30 */
L_Extract(L_32, &hi, &lo);
L_32 = Mpy_32_16(hi, lo, approx); /* = 1/L_denom in Q29 */
/* L_num * (1/L_denom) */
L_Extract(L_32, &hi, &lo);
L_Extract(L_num, &n_hi, &n_lo);
L_32 = Mpy_32(n_hi, n_lo, hi, lo); /* result in Q29 */
L_32 = L_shl(L_32, 2); /* From Q29 to Q31 */
return( L_32 );
}