2 * Basic two-word fraction declaration and manipulation.
5 #define _FP_FRAC_DECL_2(X) _FP_W_TYPE X##_f0, X##_f1
6 #define _FP_FRAC_COPY_2(D,S) (D##_f0 = S##_f0, D##_f1 = S##_f1)
7 #define _FP_FRAC_SET_2(X,I) __FP_FRAC_SET_2(X, I)
8 #define _FP_FRAC_HIGH_2(X) (X##_f1)
9 #define _FP_FRAC_LOW_2(X) (X##_f0)
10 #define _FP_FRAC_WORD_2(X,w) (X##_f##w)
12 #define _FP_FRAC_SLL_2(X,N) \
14 if ((N) < _FP_W_TYPE_SIZE) \
16 if (__builtin_constant_p(N) && (N) == 1) \
18 X##_f1 = X##_f1 + X##_f1 + (((_FP_WS_TYPE)(X##_f0)) < 0); \
23 X##_f1 = X##_f1 << (N) | X##_f0 >> (_FP_W_TYPE_SIZE - (N)); \
29 X##_f1 = X##_f0 << ((N) - _FP_W_TYPE_SIZE); \
34 #define _FP_FRAC_SRL_2(X,N) \
36 if ((N) < _FP_W_TYPE_SIZE) \
38 X##_f0 = X##_f0 >> (N) | X##_f1 << (_FP_W_TYPE_SIZE - (N)); \
43 X##_f0 = X##_f1 >> ((N) - _FP_W_TYPE_SIZE); \
48 /* Right shift with sticky-lsb. */
49 #define _FP_FRAC_SRS_2(X,N,sz) \
51 if ((N) < _FP_W_TYPE_SIZE) \
53 X##_f0 = (X##_f1 << (_FP_W_TYPE_SIZE - (N)) | X##_f0 >> (N) | \
54 (__builtin_constant_p(N) && (N) == 1 \
56 : (X##_f0 << (_FP_W_TYPE_SIZE - (N))) != 0)); \
61 X##_f0 = (X##_f1 >> ((N) - _FP_W_TYPE_SIZE) | \
62 (((X##_f1 << (2 * _FP_W_TYPE_SIZE - (N))) | \
68 #define _FP_FRAC_ADDI_2(X,I) \
69 __FP_FRAC_ADDI_2(X##_f1, X##_f0, I)
71 #define _FP_FRAC_ADD_2(R,X,Y) \
72 __FP_FRAC_ADD_2(R##_f1, R##_f0, X##_f1, X##_f0, Y##_f1, Y##_f0)
74 #define _FP_FRAC_SUB_2(R,X,Y) \
75 __FP_FRAC_SUB_2(R##_f1, R##_f0, X##_f1, X##_f0, Y##_f1, Y##_f0)
77 #define _FP_FRAC_CLZ_2(R,X) \
84 R += _FP_W_TYPE_SIZE; \
89 #define _FP_FRAC_NEGP_2(X) ((_FP_WS_TYPE)X##_f1 < 0)
90 #define _FP_FRAC_ZEROP_2(X) ((X##_f1 | X##_f0) == 0)
91 #define _FP_FRAC_OVERP_2(fs,X) (X##_f1 & _FP_OVERFLOW_##fs)
92 #define _FP_FRAC_EQ_2(X, Y) (X##_f1 == Y##_f1 && X##_f0 == Y##_f0)
93 #define _FP_FRAC_GT_2(X, Y) \
94 ((X##_f1 > Y##_f1) || (X##_f1 == Y##_f1 && X##_f0 > Y##_f0))
95 #define _FP_FRAC_GE_2(X, Y) \
96 ((X##_f1 > Y##_f1) || (X##_f1 == Y##_f1 && X##_f0 >= Y##_f0))
98 #define _FP_ZEROFRAC_2 0, 0
99 #define _FP_MINFRAC_2 0, 1
105 #define __FP_FRAC_SET_2(X,I1,I0) (X##_f0 = I0, X##_f1 = I1)
107 #define __FP_CLZ_2(R, xh, xl) \
114 R += _FP_W_TYPE_SIZE; \
120 #ifndef __FP_FRAC_ADDI_2
121 #define __FP_FRAC_ADDI_2(xh, xl, i) \
122 (xh += ((xl += i) < i))
124 #ifndef __FP_FRAC_ADD_2
125 #define __FP_FRAC_ADD_2(rh, rl, xh, xl, yh, yl) \
126 (rh = xh + yh + ((rl = xl + yl) < xl))
128 #ifndef __FP_FRAC_SUB_2
129 #define __FP_FRAC_SUB_2(rh, rl, xh, xl, yh, yl) \
130 (rh = xh - yh - ((rl = xl - yl) > xl))
135 #undef __FP_FRAC_ADDI_2
136 #define __FP_FRAC_ADDI_2(xh, xl, i) add_ssaaaa(xh, xl, xh, xl, 0, i)
137 #undef __FP_FRAC_ADD_2
138 #define __FP_FRAC_ADD_2 add_ssaaaa
139 #undef __FP_FRAC_SUB_2
140 #define __FP_FRAC_SUB_2 sub_ddmmss
145 * Unpack the raw bits of a native fp value. Do not classify or
146 * normalize the data.
149 #define _FP_UNPACK_RAW_2(fs, X, val) \
151 union _FP_UNION_##fs _flo; _flo.flt = (val); \
153 X##_f0 = _flo.bits.frac0; \
154 X##_f1 = _flo.bits.frac1; \
155 X##_e = _flo.bits.exp; \
156 X##_s = _flo.bits.sign; \
161 * Repack the raw bits of a native fp value.
164 #define _FP_PACK_RAW_2(fs, val, X) \
166 union _FP_UNION_##fs _flo; \
168 _flo.bits.frac0 = X##_f0; \
169 _flo.bits.frac1 = X##_f1; \
170 _flo.bits.exp = X##_e; \
171 _flo.bits.sign = X##_s; \
178 * Multiplication algorithms:
181 /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
183 #define _FP_MUL_MEAT_2_wide(fs, R, X, Y, doit) \
185 _FP_FRAC_DECL_4(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c); \
187 doit(_FP_FRAC_WORD_4(_z,1), _FP_FRAC_WORD_4(_z,0), X##_f0, Y##_f0); \
188 doit(_b_f1, _b_f0, X##_f0, Y##_f1); \
189 doit(_c_f1, _c_f0, X##_f1, Y##_f0); \
190 doit(_FP_FRAC_WORD_4(_z,3), _FP_FRAC_WORD_4(_z,2), X##_f1, Y##_f1); \
192 __FP_FRAC_ADD_4(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
193 _FP_FRAC_WORD_4(_z,1),_FP_FRAC_WORD_4(_z,0), \
194 0, _b_f1, _b_f0, 0, \
195 _FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
196 _FP_FRAC_WORD_4(_z,1),_FP_FRAC_WORD_4(_z,0)); \
197 __FP_FRAC_ADD_4(_FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
198 _FP_FRAC_WORD_4(_z,1),_FP_FRAC_WORD_4(_z,0), \
199 0, _c_f1, _c_f0, 0, \
200 _FP_FRAC_WORD_4(_z,3),_FP_FRAC_WORD_4(_z,2), \
201 _FP_FRAC_WORD_4(_z,1),_FP_FRAC_WORD_4(_z,0)); \
203 /* Normalize since we know where the msb of the multiplicands \
204 were (bit B), we know that the msb of the of the product is \
205 at either 2B or 2B-1. */ \
206 _FP_FRAC_SRS_4(_z, _FP_WFRACBITS_##fs-1, 2*_FP_WFRACBITS_##fs); \
207 R##_f0 = _FP_FRAC_WORD_4(_z,0); \
208 R##_f1 = _FP_FRAC_WORD_4(_z,1); \
211 /* This next macro appears to be totally broken. Fortunately nowhere
212 * seems to use it :-> The problem is that we define _z[4] but
213 * then use it in _FP_FRAC_SRS_4, which will attempt to access
214 * _z_f[n] which will cause an error. The fix probably involves
215 * declaring it with _FP_FRAC_DECL_4, see previous macro. -- PMM 02/1998
217 #define _FP_MUL_MEAT_2_gmp(fs, R, X, Y) \
219 _FP_W_TYPE _x[2], _y[2], _z[4]; \
220 _x[0] = X##_f0; _x[1] = X##_f1; \
221 _y[0] = Y##_f0; _y[1] = Y##_f1; \
223 mpn_mul_n(_z, _x, _y, 2); \
225 /* Normalize since we know where the msb of the multiplicands \
226 were (bit B), we know that the msb of the of the product is \
227 at either 2B or 2B-1. */ \
228 _FP_FRAC_SRS_4(_z, _FP_WFRACBITS##_fs-1, 2*_FP_WFRACBITS_##fs); \
235 * Division algorithms:
236 * This seems to be giving me difficulties -- PMM
237 * Look, NetBSD seems to be able to comment algorithms. Can't you?
238 * I've thrown printks at the problem.
239 * This now appears to work, but I still don't really know why.
240 * Also, I don't think the result is properly normalised...
243 #define _FP_DIV_MEAT_2_udiv_64(fs, R, X, Y) \
245 extern void _fp_udivmodti4(_FP_W_TYPE q[2], _FP_W_TYPE r[2], \
246 _FP_W_TYPE n1, _FP_W_TYPE n0, \
247 _FP_W_TYPE d1, _FP_W_TYPE d0); \
248 _FP_W_TYPE _n_f3, _n_f2, _n_f1, _n_f0, _r_f1, _r_f0; \
249 _FP_W_TYPE _q_f1, _q_f0, _m_f1, _m_f0; \
250 _FP_W_TYPE _rmem[2], _qmem[2]; \
251 /* I think this check is to ensure that the result is normalised. \
252 * Assuming X,Y normalised (ie in [1.0,2.0)) X/Y will be in \
253 * [0.5,2.0). Furthermore, it will be less than 1.0 iff X < Y. \
254 * In this case we tweak things. (this is based on comments in \
255 * the NetBSD FPU emulation code. ) \
256 * We know X,Y are normalised because we ensure this as part of \
257 * the unpacking process. -- PMM \
259 if (_FP_FRAC_GT_2(X, Y)) \
262 _n_f3 = X##_f1 >> 1; \
263 _n_f2 = X##_f1 << (_FP_W_TYPE_SIZE - 1) | X##_f0 >> 1; \
264 _n_f1 = X##_f0 << (_FP_W_TYPE_SIZE - 1); \
275 /* Normalize, i.e. make the most significant bit of the \
276 denominator set. CHANGED: - 1 to nothing -- PMM */ \
277 _FP_FRAC_SLL_2(Y, _FP_WFRACXBITS_##fs /* -1 */); \
279 /* Do the 256/128 bit division given the 128-bit _fp_udivmodtf4 \
280 primitive snagged from libgcc2.c. */ \
282 _fp_udivmodti4(_qmem, _rmem, _n_f3, _n_f2, 0, Y##_f1); \
284 umul_ppmm(_m_f1, _m_f0, _q_f1, Y##_f0); \
287 if (_FP_FRAC_GT_2(_m, _r)) \
290 _FP_FRAC_ADD_2(_r, _r, Y); \
291 if (_FP_FRAC_GE_2(_r, Y) && _FP_FRAC_GT_2(_m, _r)) \
294 _FP_FRAC_ADD_2(_r, _r, Y); \
297 _FP_FRAC_SUB_2(_r, _r, _m); \
299 _fp_udivmodti4(_qmem, _rmem, _r_f1, _r_f0, 0, Y##_f1); \
301 umul_ppmm(_m_f1, _m_f0, _q_f0, Y##_f0); \
304 if (_FP_FRAC_GT_2(_m, _r)) \
307 _FP_FRAC_ADD_2(_r, _r, Y); \
308 if (_FP_FRAC_GE_2(_r, Y) && _FP_FRAC_GT_2(_m, _r)) \
311 _FP_FRAC_ADD_2(_r, _r, Y); \
314 _FP_FRAC_SUB_2(_r, _r, _m); \
317 R##_f0 = _q_f0 | ((_r_f1 | _r_f0) != 0); \
318 /* adjust so answer is normalized again. I'm not sure what the \
319 * final sz param should be. In practice it's never used since \
320 * N is 1 which is always going to be < _FP_W_TYPE_SIZE... \
322 /* _FP_FRAC_SRS_2(R,1,_FP_WFRACBITS_##fs); */ \
326 #define _FP_DIV_MEAT_2_gmp(fs, R, X, Y) \
328 _FP_W_TYPE _x[4], _y[2], _z[4]; \
329 _y[0] = Y##_f0; _y[1] = Y##_f1; \
331 if (_FP_FRAC_GT_2(X, Y)) \
334 _x[1] = (X##_f0 << (_FP_WFRACBITS-1 - _FP_W_TYPE_SIZE) | \
335 X##_f1 >> (_FP_W_TYPE_SIZE - \
336 (_FP_WFRACBITS-1 - _FP_W_TYPE_SIZE))); \
337 _x[2] = X##_f1 << (_FP_WFRACBITS-1 - _FP_W_TYPE_SIZE); \
341 _x[1] = (X##_f0 << (_FP_WFRACBITS - _FP_W_TYPE_SIZE) | \
342 X##_f1 >> (_FP_W_TYPE_SIZE - \
343 (_FP_WFRACBITS - _FP_W_TYPE_SIZE))); \
344 _x[2] = X##_f1 << (_FP_WFRACBITS - _FP_W_TYPE_SIZE); \
347 (void) mpn_divrem (_z, 0, _x, 4, _y, 2); \
349 R##_f0 = _z[0] | ((_x[0] | _x[1]) != 0); \
354 * Square root algorithms:
355 * We have just one right now, maybe Newton approximation
356 * should be added for those machines where division is fast.
359 #define _FP_SQRT_MEAT_2(R, S, T, X, q) \
363 T##_f1 = S##_f1 + q; \
364 if (T##_f1 <= X##_f1) \
366 S##_f1 = T##_f1 + q; \
370 _FP_FRAC_SLL_2(X, 1); \
373 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
376 T##_f0 = S##_f0 + q; \
378 if (T##_f1 < X##_f1 || \
379 (T##_f1 == X##_f1 && T##_f0 < X##_f0)) \
381 S##_f0 = T##_f0 + q; \
382 if (((_FP_WS_TYPE)T##_f0) < 0 && \
383 ((_FP_WS_TYPE)S##_f0) >= 0) \
385 _FP_FRAC_SUB_2(X, X, T); \
388 _FP_FRAC_SLL_2(X, 1); \
395 * Assembly/disassembly for converting to/from integral types.
396 * No shifting or overflow handled here.
399 #define _FP_FRAC_ASSEMBLE_2(r, X, rsize) \
401 if (rsize <= _FP_W_TYPE_SIZE) \
406 r <<= _FP_W_TYPE_SIZE; \
411 #define _FP_FRAC_DISASSEMBLE_2(X, r, rsize) \
414 X##_f1 = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE); \
418 * Convert FP values between word sizes
421 #define _FP_FRAC_CONV_1_2(dfs, sfs, D, S) \
423 _FP_FRAC_SRS_2(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs), \
424 _FP_WFRACBITS_##sfs); \
428 #define _FP_FRAC_CONV_2_1(dfs, sfs, D, S) \
432 _FP_FRAC_SLL_2(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs)); \