This source file includes following definitions.
- qm_mulu16
- qm_muls16
- qm_add32
- qm_add16
- qm_sub16
- qm_shl32
- qm_shl16
- qm_shr16
- qm_norm32
- qm_log10
1
2
3
4
5
6 #include "phy_qmath.h"
7
8
9
10
11
12
13 u16 qm_mulu16(u16 op1, u16 op2)
14 {
15 return (u16) (((u32) op1 * (u32) op2) >> 16);
16 }
17
18
19
20
21
22
23
24
25
26 s16 qm_muls16(s16 op1, s16 op2)
27 {
28 s32 result;
29 if (op1 == (s16) 0x8000 && op2 == (s16) 0x8000)
30 result = 0x7fffffff;
31 else
32 result = ((s32) (op1) * (s32) (op2));
33
34 return (s16) (result >> 15);
35 }
36
37
38
39
40
41
42 s32 qm_add32(s32 op1, s32 op2)
43 {
44 s32 result;
45 result = op1 + op2;
46 if (op1 < 0 && op2 < 0 && result > 0)
47 result = 0x80000000;
48 else if (op1 > 0 && op2 > 0 && result < 0)
49 result = 0x7fffffff;
50
51 return result;
52 }
53
54
55
56
57
58
59 s16 qm_add16(s16 op1, s16 op2)
60 {
61 s16 result;
62 s32 temp = (s32) op1 + (s32) op2;
63 if (temp > (s32) 0x7fff)
64 result = (s16) 0x7fff;
65 else if (temp < (s32) 0xffff8000)
66 result = (s16) 0xffff8000;
67 else
68 result = (s16) temp;
69
70 return result;
71 }
72
73
74
75
76
77
78 s16 qm_sub16(s16 op1, s16 op2)
79 {
80 s16 result;
81 s32 temp = (s32) op1 - (s32) op2;
82 if (temp > (s32) 0x7fff)
83 result = (s16) 0x7fff;
84 else if (temp < (s32) 0xffff8000)
85 result = (s16) 0xffff8000;
86 else
87 result = (s16) temp;
88
89 return result;
90 }
91
92
93
94
95
96
97
98 s32 qm_shl32(s32 op, int shift)
99 {
100 int i;
101 s32 result;
102 result = op;
103 if (shift > 31)
104 shift = 31;
105 else if (shift < -31)
106 shift = -31;
107 if (shift >= 0) {
108 for (i = 0; i < shift; i++)
109 result = qm_add32(result, result);
110 } else {
111 result = result >> (-shift);
112 }
113
114 return result;
115 }
116
117
118
119
120
121
122
123 s16 qm_shl16(s16 op, int shift)
124 {
125 int i;
126 s16 result;
127 result = op;
128 if (shift > 15)
129 shift = 15;
130 else if (shift < -15)
131 shift = -15;
132 if (shift > 0) {
133 for (i = 0; i < shift; i++)
134 result = qm_add16(result, result);
135 } else {
136 result = result >> (-shift);
137 }
138
139 return result;
140 }
141
142
143
144
145
146
147 s16 qm_shr16(s16 op, int shift)
148 {
149 return qm_shl16(op, -shift);
150 }
151
152
153
154
155
156 s16 qm_norm32(s32 op)
157 {
158 u16 u16extraSignBits;
159 if (op == 0) {
160 return 31;
161 } else {
162 u16extraSignBits = 0;
163 while ((op >> 31) == (op >> 30)) {
164 u16extraSignBits++;
165 op = op << 1;
166 }
167 }
168 return u16extraSignBits;
169 }
170
171
172 static const s16 log_table[] = {
173 0,
174 1455,
175 2866,
176 4236,
177 5568,
178 6863,
179 8124,
180 9352,
181 10549,
182 11716,
183 12855,
184 13968,
185 15055,
186 16117,
187 17156,
188 18173,
189 19168,
190 20143,
191 21098,
192 22034,
193 22952,
194 23852,
195 24736,
196 25604,
197 26455,
198 27292,
199 28114,
200 28922,
201 29717,
202 30498,
203 31267,
204 32024,
205 32767
206 };
207
208 #define LOG_TABLE_SIZE 32
209 #define LOG2_LOG_TABLE_SIZE 5
210 #define Q_LOG_TABLE 15
211 #define LOG10_2 19728
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 void qm_log10(s32 N, s16 qN, s16 *log10N, s16 *qLog10N)
233 {
234 s16 s16norm, s16tableIndex, s16errorApproximation;
235 u16 u16offset;
236 s32 s32log;
237
238
239 s16norm = qm_norm32(N);
240 N = N << s16norm;
241
242
243
244
245
246
247
248 qN = qN + s16norm - 30;
249
250
251
252 s16tableIndex = (s16) (N >> (32 - (2 + LOG2_LOG_TABLE_SIZE)));
253
254
255 s16tableIndex =
256 s16tableIndex & (s16) ((1 << LOG2_LOG_TABLE_SIZE) - 1);
257
258
259 N = N & ((1 << (32 - (2 + LOG2_LOG_TABLE_SIZE))) - 1);
260
261
262
263 u16offset = (u16) (N >> (32 - (2 + LOG2_LOG_TABLE_SIZE + 16)));
264
265
266 s32log = log_table[s16tableIndex];
267
268
269 s16errorApproximation = (s16) qm_mulu16(u16offset,
270 (u16) (log_table[s16tableIndex + 1] -
271 log_table[s16tableIndex]));
272
273
274 s32log = qm_add16((s16) s32log, s16errorApproximation);
275
276
277
278
279 s32log = qm_add32(s32log, ((s32) -qN) << 15);
280
281
282 s16norm = qm_norm32(s32log);
283
284
285
286 s32log = qm_shl32(s32log, s16norm - 16);
287
288
289
290
291
292 *log10N = qm_muls16((s16) s32log, (s16) LOG10_2);
293
294
295 *qLog10N = 15 + s16norm - 16 + 1;
296
297 return;
298 }