comparison libtwamr/oper_32b.c @ 253:54f6bc41ed10

libtwamr: integrate a* modules
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 05 Apr 2024 06:08:15 +0000
parents libgsmefr/oper_32b.c@92479d9a8e38
children
comparison
equal deleted inserted replaced
252:57b4053559ff 253:54f6bc41ed10
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 "namespace.h"
26 #include "basic_op.h"
27 #include "oper_32b.h"
28 #include "no_count.h"
29
30 /*****************************************************************************
31 * *
32 * Function L_Extract() *
33 * *
34 * Extract from a 32 bit integer two 16 bit DPF. *
35 * *
36 * Arguments: *
37 * *
38 * L_32 : 32 bit integer. *
39 * 0x8000 0000 <= L_32 <= 0x7fff ffff. *
40 * hi : b16 to b31 of L_32 *
41 * lo : (L_32 - hi<<16)>>1 *
42 *****************************************************************************
43 */
44
45 void L_Extract (Word32 L_32, Word16 *hi, Word16 *lo)
46 {
47 *hi = extract_h (L_32);
48 *lo = extract_l (L_msu (L_shr (L_32, 1), *hi, 16384));
49 return;
50 }
51
52 /*****************************************************************************
53 * *
54 * Function L_Comp() *
55 * *
56 * Compose from two 16 bit DPF a 32 bit integer. *
57 * *
58 * L_32 = hi<<16 + lo<<1 *
59 * *
60 * Arguments: *
61 * *
62 * hi msb *
63 * lo lsf (with sign) *
64 * *
65 * Return Value : *
66 * *
67 * 32 bit long signed integer (Word32) whose value falls in the *
68 * range : 0x8000 0000 <= L_32 <= 0x7fff fff0. *
69 * *
70 *****************************************************************************
71 */
72
73 Word32 L_Comp (Word16 hi, Word16 lo)
74 {
75 Word32 L_32;
76
77 L_32 = L_deposit_h (hi);
78 return (L_mac (L_32, lo, 1)); /* = hi<<16 + lo<<1 */
79 }
80
81 /*****************************************************************************
82 * Function Mpy_32() *
83 * *
84 * Multiply two 32 bit integers (DPF). The result is divided by 2**31 *
85 * *
86 * L_32 = (hi1*hi2)<<1 + ( (hi1*lo2)>>15 + (lo1*hi2)>>15 )<<1 *
87 * *
88 * This operation can also be viewed as the multiplication of two Q31 *
89 * number and the result is also in Q31. *
90 * *
91 * Arguments: *
92 * *
93 * hi1 hi part of first number *
94 * lo1 lo part of first number *
95 * hi2 hi part of second number *
96 * lo2 lo part of second number *
97 * *
98 *****************************************************************************
99 */
100
101 Word32 Mpy_32 (Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2)
102 {
103 Word32 L_32;
104
105 L_32 = L_mult (hi1, hi2);
106 L_32 = L_mac (L_32, mult (hi1, lo2), 1);
107 L_32 = L_mac (L_32, mult (lo1, hi2), 1);
108
109 return (L_32);
110 }
111
112 /*****************************************************************************
113 * Function Mpy_32_16() *
114 * *
115 * Multiply a 16 bit integer by a 32 bit (DPF). The result is divided *
116 * by 2**15 *
117 * *
118 * *
119 * L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1 *
120 * *
121 * Arguments: *
122 * *
123 * hi hi part of 32 bit number. *
124 * lo lo part of 32 bit number. *
125 * n 16 bit number. *
126 * *
127 *****************************************************************************
128 */
129
130 Word32 Mpy_32_16 (Word16 hi, Word16 lo, Word16 n)
131 {
132 Word32 L_32;
133
134 L_32 = L_mult (hi, n);
135 L_32 = L_mac (L_32, mult (lo, n), 1);
136
137 return (L_32);
138 }
139
140 /*****************************************************************************
141 * *
142 * Function Name : Div_32 *
143 * *
144 * Purpose : *
145 * Fractional integer division of two 32 bit numbers. *
146 * L_num / L_denom. *
147 * L_num and L_denom must be positive and L_num < L_denom. *
148 * L_denom = denom_hi<<16 + denom_lo<<1 *
149 * denom_hi is a normalize number. *
150 * *
151 * Inputs : *
152 * *
153 * L_num *
154 * 32 bit long signed integer (Word32) whose value falls in the *
155 * range : 0x0000 0000 < L_num < L_denom *
156 * *
157 * L_denom = denom_hi<<16 + denom_lo<<1 (DPF) *
158 * *
159 * denom_hi *
160 * 16 bit positive normalized integer whose value falls in the *
161 * range : 0x4000 < hi < 0x7fff *
162 * denom_lo *
163 * 16 bit positive integer whose value falls in the *
164 * range : 0 < lo < 0x7fff *
165 * *
166 * Return Value : *
167 * *
168 * L_div *
169 * 32 bit long signed integer (Word32) whose value falls in the *
170 * range : 0x0000 0000 <= L_div <= 0x7fff ffff. *
171 * *
172 * Algorithm: *
173 * *
174 * - find = 1/L_denom. *
175 * First approximation: approx = 1 / denom_hi *
176 * 1/L_denom = approx * (2.0 - L_denom * approx ) *
177 * *
178 * - result = L_num * (1/L_denom) *
179 *****************************************************************************
180 */
181
182 Word32 Div_32 (Word32 L_num, Word16 denom_hi, Word16 denom_lo)
183 {
184 Word16 approx, hi, lo, n_hi, n_lo;
185 Word32 L_32;
186
187 /* First approximation: 1 / L_denom = 1/denom_hi */
188
189 approx = div_s ((Word16) 0x3fff, denom_hi);
190
191 /* 1/L_denom = approx * (2.0 - L_denom * approx) */
192
193 L_32 = Mpy_32_16 (denom_hi, denom_lo, approx);
194
195 L_32 = L_sub ((Word32) 0x7fffffffL, L_32);
196
197 L_Extract (L_32, &hi, &lo);
198
199 L_32 = Mpy_32_16 (hi, lo, approx);
200
201 /* L_num * (1/L_denom) */
202
203 L_Extract (L_32, &hi, &lo);
204 L_Extract (L_num, &n_hi, &n_lo);
205 L_32 = Mpy_32 (n_hi, n_lo, hi, lo);
206 L_32 = L_shl (L_32, 2);
207
208 return (L_32);
209 }