comparison src/oper_32b.c @ 0:56410792419a

src: original EFR source from ETSI
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 03 Apr 2024 05:31:37 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:56410792419a
1 /*****************************************************************************
2 * *
3 * This file contains operations in double precision. *
4 * These operations are not standard double precision operations. *
5 * They are used where single precision is not enough but the full 32 bits *
6 * precision is not necessary. For example, the function Div_32() has a *
7 * 24 bits precision which is enough for our purposes. *
8 * *
9 * The double precision numbers use a special representation: *
10 * *
11 * L_32 = hi<<16 + lo<<1 *
12 * *
13 * L_32 is a 32 bit integer. *
14 * hi and lo are 16 bit signed integers. *
15 * As the low part also contains the sign, this allows fast multiplication. *
16 * *
17 * 0x8000 0000 <= L_32 <= 0x7fff fffe. *
18 * *
19 * We will use DPF (Double Precision Format )in this file to specify *
20 * this special format. *
21 *****************************************************************************
22 */
23
24 #include "typedef.h"
25 #include "basic_op.h"
26 #include "oper_32b.h"
27 #include "count.h"
28
29 /*****************************************************************************
30 * *
31 * Function L_Extract() *
32 * *
33 * Extract from a 32 bit integer two 16 bit DPF. *
34 * *
35 * Arguments: *
36 * *
37 * L_32 : 32 bit integer. *
38 * 0x8000 0000 <= L_32 <= 0x7fff ffff. *
39 * hi : b16 to b31 of L_32 *
40 * lo : (L_32 - hi<<16)>>1 *
41 *****************************************************************************
42 */
43
44 void L_Extract (Word32 L_32, Word16 *hi, Word16 *lo)
45 {
46 *hi = extract_h (L_32);
47 *lo = extract_l (L_msu (L_shr (L_32, 1), *hi, 16384));
48 return;
49 }
50
51 /*****************************************************************************
52 * *
53 * Function L_Comp() *
54 * *
55 * Compose from two 16 bit DPF a 32 bit integer. *
56 * *
57 * L_32 = hi<<16 + lo<<1 *
58 * *
59 * Arguments: *
60 * *
61 * hi msb *
62 * lo lsf (with sign) *
63 * *
64 * Return Value : *
65 * *
66 * 32 bit long signed integer (Word32) whose value falls in the *
67 * range : 0x8000 0000 <= L_32 <= 0x7fff fff0. *
68 * *
69 *****************************************************************************
70 */
71
72 Word32 L_Comp (Word16 hi, Word16 lo)
73 {
74 Word32 L_32;
75
76 L_32 = L_deposit_h (hi);
77 return (L_mac (L_32, lo, 1)); /* = hi<<16 + lo<<1 */
78 }
79
80 /*****************************************************************************
81 * Function Mpy_32() *
82 * *
83 * Multiply two 32 bit integers (DPF). The result is divided by 2**31 *
84 * *
85 * L_32 = (hi1*hi2)<<1 + ( (hi1*lo2)>>15 + (lo1*hi2)>>15 )<<1 *
86 * *
87 * This operation can also be viewed as the multiplication of two Q31 *
88 * number and the result is also in Q31. *
89 * *
90 * Arguments: *
91 * *
92 * hi1 hi part of first number *
93 * lo1 lo part of first number *
94 * hi2 hi part of second number *
95 * lo2 lo part of second number *
96 * *
97 *****************************************************************************
98 */
99
100 Word32 Mpy_32 (Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2)
101 {
102 Word32 L_32;
103
104 L_32 = L_mult (hi1, hi2);
105 L_32 = L_mac (L_32, mult (hi1, lo2), 1);
106 L_32 = L_mac (L_32, mult (lo1, hi2), 1);
107
108 return (L_32);
109 }
110
111 /*****************************************************************************
112 * Function Mpy_32_16() *
113 * *
114 * Multiply a 16 bit integer by a 32 bit (DPF). The result is divided *
115 * by 2**15 *
116 * *
117 * *
118 * L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1 *
119 * *
120 * Arguments: *
121 * *
122 * hi hi part of 32 bit number. *
123 * lo lo part of 32 bit number. *
124 * n 16 bit number. *
125 * *
126 *****************************************************************************
127 */
128
129 Word32 Mpy_32_16 (Word16 hi, Word16 lo, Word16 n)
130 {
131 Word32 L_32;
132
133 L_32 = L_mult (hi, n);
134 L_32 = L_mac (L_32, mult (lo, n), 1);
135
136 return (L_32);
137 }
138
139 /*****************************************************************************
140 * *
141 * Function Name : Div_32 *
142 * *
143 * Purpose : *
144 * Fractional integer division of two 32 bit numbers. *
145 * L_num / L_denom. *
146 * L_num and L_denom must be positive and L_num < L_denom. *
147 * L_denom = denom_hi<<16 + denom_lo<<1 *
148 * denom_hi is a normalize number. *
149 * *
150 * Inputs : *
151 * *
152 * L_num *
153 * 32 bit long signed integer (Word32) whose value falls in the *
154 * range : 0x0000 0000 < L_num < L_denom *
155 * *
156 * L_denom = denom_hi<<16 + denom_lo<<1 (DPF) *
157 * *
158 * denom_hi *
159 * 16 bit positive normalized integer whose value falls in the *
160 * range : 0x4000 < hi < 0x7fff *
161 * denom_lo *
162 * 16 bit positive integer whose value falls in the *
163 * range : 0 < lo < 0x7fff *
164 * *
165 * Return Value : *
166 * *
167 * L_div *
168 * 32 bit long signed integer (Word32) whose value falls in the *
169 * range : 0x0000 0000 <= L_div <= 0x7fff ffff. *
170 * *
171 * Algorithm: *
172 * *
173 * - find = 1/L_denom. *
174 * First approximation: approx = 1 / denom_hi *
175 * 1/L_denom = approx * (2.0 - L_denom * approx ) *
176 * *
177 * - result = L_num * (1/L_denom) *
178 *****************************************************************************
179 */
180
181 Word32 Div_32 (Word32 L_num, Word16 denom_hi, Word16 denom_lo)
182 {
183 Word16 approx, hi, lo, n_hi, n_lo;
184 Word32 L_32;
185
186 /* First approximation: 1 / L_denom = 1/denom_hi */
187
188 approx = div_s ((Word16) 0x3fff, denom_hi);
189
190 /* 1/L_denom = approx * (2.0 - L_denom * approx) */
191
192 L_32 = Mpy_32_16 (denom_hi, denom_lo, approx);
193
194 L_32 = L_sub ((Word32) 0x7fffffffL, L_32);
195
196 L_Extract (L_32, &hi, &lo);
197
198 L_32 = Mpy_32_16 (hi, lo, approx);
199
200 /* L_num * (1/L_denom) */
201
202 L_Extract (L_32, &hi, &lo);
203 L_Extract (L_num, &n_hi, &n_lo);
204 L_32 = Mpy_32 (n_hi, n_lo, hi, lo);
205 L_32 = L_shl (L_32, 2);
206
207 return (L_32);
208 }