-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdspfunc.c
181 lines (148 loc) · 7.74 KB
/
dspfunc.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
/*
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 "ld8a.h"
#include "tab_ld8a.h"
/*___________________________________________________________________________
| |
| Function Name : Pow2() |
| |
| L_x = pow(2.0, exponent.fraction) |
|---------------------------------------------------------------------------|
| Algorithm: |
| |
| The function Pow2(L_x) is approximated by a table and linear |
| interpolation. |
| |
| 1- i = bit10-b15 of fraction, 0 <= i <= 31 |
| 2- a = bit0-b9 of fraction |
| 3- L_x = tabpow[i]<<16 - (tabpow[i] - tabpow[i+1]) * a * 2 |
| 4- L_x = L_x >> (30-exponent) (with rounding) |
|___________________________________________________________________________|
*/
int32_t Pow2( /* (o) Q0 : result (range: 0<=val<=0x7fffffff) */
int16_t exponent, /* (i) Q0 : Integer part. (range: 0<=val<=30) */
int16_t fraction /* (i) Q15 : Fractional part. (range: 0.0<=val<1.0) */
)
{
int16_t exp, i, a, tmp;
int32_t L_x;
L_x = L_mult(fraction, 32); /* L_x = fraction<<6 */
i = extract_h(L_x); /* Extract b10-b15 of fraction */
L_x = L_shr(L_x, 1);
a = extract_l(L_x); /* Extract b0-b9 of fraction */
a = a & (int16_t)0x7fff;
L_x = L_deposit_h(tabpow[i]); /* tabpow[i] << 16 */
tmp = sub(tabpow[i], tabpow[i+1]); /* tabpow[i] - tabpow[i+1] */
L_x = L_msu(L_x, tmp, a); /* L_x -= tmp*a*2 */
exp = sub(30, exponent);
L_x = L_shr_r(L_x, exp);
return(L_x);
}
/*___________________________________________________________________________
| |
| Function Name : Log2() |
| |
| Compute log2(L_x). |
| L_x is positive. |
| |
| if L_x is negative or zero, result is 0. |
|---------------------------------------------------------------------------|
| Algorithm: |
| |
| The function Log2(L_x) is approximated by a table and linear |
| interpolation. |
| |
| 1- Normalization of L_x. |
| 2- exponent = 30-exponent |
| 3- i = bit25-b31 of L_x, 32 <= i <= 63 ->because of normalization. |
| 4- a = bit10-b24 |
| 5- i -=32 |
| 6- fraction = tablog[i]<<16 - (tablog[i] - tablog[i+1]) * a * 2 |
|___________________________________________________________________________|
*/
void Log2(
int32_t L_x, /* (i) Q0 : input value */
int16_t *exponent, /* (o) Q0 : Integer part of Log2. (range: 0<=val<=30) */
int16_t *fraction /* (o) Q15: Fractional part of Log2. (range: 0<=val<1) */
)
{
int16_t exp, i, a, tmp;
int32_t L_y;
if( L_x <= (int32_t)0 )
{
*exponent = 0;
*fraction = 0;
return;
}
exp = norm_l(L_x);
L_x = L_shl(L_x, exp ); /* L_x is normalized */
*exponent = sub(30, exp);
L_x = L_shr(L_x, 9);
i = extract_h(L_x); /* Extract b25-b31 */
L_x = L_shr(L_x, 1);
a = extract_l(L_x); /* Extract b10-b24 of fraction */
a = a & (int16_t)0x7fff;
i = sub(i, 32);
L_y = L_deposit_h(tablog[i]); /* tablog[i] << 16 */
tmp = sub(tablog[i], tablog[i+1]); /* tablog[i] - tablog[i+1] */
L_y = L_msu(L_y, tmp, a); /* L_y -= tmp*a*2 */
*fraction = extract_h( L_y);
return;
}
/*___________________________________________________________________________
| |
| Function Name : Inv_sqrt |
| |
| Compute 1/sqrt(L_x). |
| L_x is positive. |
| |
| if L_x is negative or zero, result is 1 (3fff ffff). |
|---------------------------------------------------------------------------|
| Algorithm: |
| |
| The function 1/sqrt(L_x) is approximated by a table and linear |
| interpolation. |
| |
| 1- Normalization of L_x. |
| 2- If (30-exponent) is even then shift right once. |
| 3- exponent = (30-exponent)/2 +1 |
| 4- i = bit25-b31 of L_x, 16 <= i <= 63 ->because of normalization. |
| 5- a = bit10-b24 |
| 6- i -=16 |
| 7- L_y = tabsqr[i]<<16 - (tabsqr[i] - tabsqr[i+1]) * a * 2 |
| 8- L_y >>= exponent |
|___________________________________________________________________________|
*/
int32_t Inv_sqrt( /* (o) Q30 : output value (range: 0<=val<1) */
int32_t L_x /* (i) Q0 : input value (range: 0<=val<=7fffffff) */
)
{
int16_t exp, i, a, tmp;
int32_t L_y;
if( L_x <= (int32_t)0) return ( (int32_t)0x3fffffffL);
exp = norm_l(L_x);
L_x = L_shl(L_x, exp ); /* L_x is normalize */
exp = sub(30, exp);
if( (exp & 1) == 0 ) /* If exponent even -> shift right */
L_x = L_shr(L_x, 1);
exp = shr(exp, 1);
exp = add(exp, 1);
L_x = L_shr(L_x, 9);
i = extract_h(L_x); /* Extract b25-b31 */
L_x = L_shr(L_x, 1);
a = extract_l(L_x); /* Extract b10-b24 */
a = a & (int16_t)0x7fff;
i = sub(i, 16);
L_y = L_deposit_h(tabsqr[i]); /* tabsqr[i] << 16 */
tmp = sub(tabsqr[i], tabsqr[i+1]); /* tabsqr[i] - tabsqr[i+1]) */
L_y = L_msu(L_y, tmp, a); /* L_y -= tmp*a*2 */
L_y = L_shr(L_y, exp); /* denormalization */
return(L_y);
}