1 /* Software floating-point emulation.
2 Basic four-word fraction declaration and manipulation.
3 Copyright (C) 1997,1998,1999,2006 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Richard Henderson (rth@cygnus.com),
6 Jakub Jelinek (jj@ultra.linux.cz),
7 David S. Miller (davem@redhat.com) and
8 Peter Maydell (pmaydell@chiark.greenend.org.uk).
10 The GNU C Library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
15 The GNU C Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with the GNU C Library; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #define _FP_FRAC_DECL_4(X) _FP_W_TYPE X##_f[4]
26 #define _FP_FRAC_COPY_4(D,S) \
27 (D##_f[0] = S##_f[0], D##_f[1] = S##_f[1], \
28 D##_f[2] = S##_f[2], D##_f[3] = S##_f[3])
29 #define _FP_FRAC_SET_4(X,I) __FP_FRAC_SET_4(X, I)
30 #define _FP_FRAC_HIGH_4(X) (X##_f[3])
31 #define _FP_FRAC_LOW_4(X) (X##_f[0])
32 #define _FP_FRAC_WORD_4(X,w) (X##_f[w])
34 #define _FP_FRAC_SLL_4(X,N) \
36 _FP_I_TYPE _up, _down, _skip, _i; \
37 _skip = (N) / _FP_W_TYPE_SIZE; \
38 _up = (N) % _FP_W_TYPE_SIZE; \
39 _down = _FP_W_TYPE_SIZE - _up; \
41 for (_i = 3; _i >= _skip; --_i) \
42 X##_f[_i] = X##_f[_i-_skip]; \
45 for (_i = 3; _i > _skip; --_i) \
46 X##_f[_i] = X##_f[_i-_skip] << _up \
47 | X##_f[_i-_skip-1] >> _down; \
48 X##_f[_i--] = X##_f[0] << _up; \
50 for (; _i >= 0; --_i) \
54 /* This one was broken too */
55 #define _FP_FRAC_SRL_4(X,N) \
57 _FP_I_TYPE _up, _down, _skip, _i; \
58 _skip = (N) / _FP_W_TYPE_SIZE; \
59 _down = (N) % _FP_W_TYPE_SIZE; \
60 _up = _FP_W_TYPE_SIZE - _down; \
62 for (_i = 0; _i <= 3-_skip; ++_i) \
63 X##_f[_i] = X##_f[_i+_skip]; \
66 for (_i = 0; _i < 3-_skip; ++_i) \
67 X##_f[_i] = X##_f[_i+_skip] >> _down \
68 | X##_f[_i+_skip+1] << _up; \
69 X##_f[_i++] = X##_f[3] >> _down; \
71 for (; _i < 4; ++_i) \
76 /* Right shift with sticky-lsb.
77 * What this actually means is that we do a standard right-shift,
78 * but that if any of the bits that fall off the right hand side
79 * were one then we always set the LSbit.
81 #define _FP_FRAC_SRST_4(X,S,N,size) \
83 _FP_I_TYPE _up, _down, _skip, _i; \
85 _skip = (N) / _FP_W_TYPE_SIZE; \
86 _down = (N) % _FP_W_TYPE_SIZE; \
87 _up = _FP_W_TYPE_SIZE - _down; \
88 for (_s = _i = 0; _i < _skip; ++_i) \
91 for (_i = 0; _i <= 3-_skip; ++_i) \
92 X##_f[_i] = X##_f[_i+_skip]; \
95 _s |= X##_f[_i] << _up; \
96 for (_i = 0; _i < 3-_skip; ++_i) \
97 X##_f[_i] = X##_f[_i+_skip] >> _down \
98 | X##_f[_i+_skip+1] << _up; \
99 X##_f[_i++] = X##_f[3] >> _down; \
101 for (; _i < 4; ++_i) \
106 #define _FP_FRAC_SRS_4(X,N,size) \
109 _FP_FRAC_SRST_4(X, _sticky, N, size); \
110 X##_f[0] |= _sticky; \
113 #define _FP_FRAC_ADD_4(R,X,Y) \
114 __FP_FRAC_ADD_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
115 X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
116 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
118 #define _FP_FRAC_SUB_4(R,X,Y) \
119 __FP_FRAC_SUB_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
120 X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
121 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
123 #define _FP_FRAC_DEC_4(X,Y) \
124 __FP_FRAC_DEC_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
125 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
127 #define _FP_FRAC_ADDI_4(X,I) \
128 __FP_FRAC_ADDI_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], I)
130 #define _FP_ZEROFRAC_4 0,0,0,0
131 #define _FP_MINFRAC_4 0,0,0,1
132 #define _FP_MAXFRAC_4 (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0)
134 #define _FP_FRAC_ZEROP_4(X) ((X##_f[0] | X##_f[1] | X##_f[2] | X##_f[3]) == 0)
135 #define _FP_FRAC_NEGP_4(X) ((_FP_WS_TYPE)X##_f[3] < 0)
136 #define _FP_FRAC_OVERP_4(fs,X) (_FP_FRAC_HIGH_##fs(X) & _FP_OVERFLOW_##fs)
137 #define _FP_FRAC_CLEAR_OVERP_4(fs,X) (_FP_FRAC_HIGH_##fs(X) &= ~_FP_OVERFLOW_##fs)
139 #define _FP_FRAC_EQ_4(X,Y) \
140 (X##_f[0] == Y##_f[0] && X##_f[1] == Y##_f[1] \
141 && X##_f[2] == Y##_f[2] && X##_f[3] == Y##_f[3])
143 #define _FP_FRAC_GT_4(X,Y) \
144 (X##_f[3] > Y##_f[3] || \
145 (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
146 (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
147 (X##_f[1] == Y##_f[1] && X##_f[0] > Y##_f[0]) \
152 #define _FP_FRAC_GE_4(X,Y) \
153 (X##_f[3] > Y##_f[3] || \
154 (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
155 (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
156 (X##_f[1] == Y##_f[1] && X##_f[0] >= Y##_f[0]) \
162 #define _FP_FRAC_CLZ_4(R,X) \
166 __FP_CLZ(R,X##_f[3]); \
170 __FP_CLZ(R,X##_f[2]); \
171 R += _FP_W_TYPE_SIZE; \
175 __FP_CLZ(R,X##_f[1]); \
176 R += _FP_W_TYPE_SIZE*2; \
180 __FP_CLZ(R,X##_f[0]); \
181 R += _FP_W_TYPE_SIZE*3; \
186 #define _FP_UNPACK_RAW_4(fs, X, val) \
188 union _FP_UNION_##fs _flo; _flo.flt = (val); \
189 X##_f[0] = _flo.bits.frac0; \
190 X##_f[1] = _flo.bits.frac1; \
191 X##_f[2] = _flo.bits.frac2; \
192 X##_f[3] = _flo.bits.frac3; \
193 X##_e = _flo.bits.exp; \
194 X##_s = _flo.bits.sign; \
197 #define _FP_UNPACK_RAW_4_P(fs, X, val) \
199 union _FP_UNION_##fs *_flo = \
200 (union _FP_UNION_##fs *)(val); \
202 X##_f[0] = _flo->bits.frac0; \
203 X##_f[1] = _flo->bits.frac1; \
204 X##_f[2] = _flo->bits.frac2; \
205 X##_f[3] = _flo->bits.frac3; \
206 X##_e = _flo->bits.exp; \
207 X##_s = _flo->bits.sign; \
210 #define _FP_PACK_RAW_4(fs, val, X) \
212 union _FP_UNION_##fs _flo; \
213 _flo.bits.frac0 = X##_f[0]; \
214 _flo.bits.frac1 = X##_f[1]; \
215 _flo.bits.frac2 = X##_f[2]; \
216 _flo.bits.frac3 = X##_f[3]; \
217 _flo.bits.exp = X##_e; \
218 _flo.bits.sign = X##_s; \
222 #define _FP_PACK_RAW_4_P(fs, val, X) \
224 union _FP_UNION_##fs *_flo = \
225 (union _FP_UNION_##fs *)(val); \
227 _flo->bits.frac0 = X##_f[0]; \
228 _flo->bits.frac1 = X##_f[1]; \
229 _flo->bits.frac2 = X##_f[2]; \
230 _flo->bits.frac3 = X##_f[3]; \
231 _flo->bits.exp = X##_e; \
232 _flo->bits.sign = X##_s; \
236 * Multiplication algorithms:
239 /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
241 #define _FP_MUL_MEAT_4_wide(wfracbits, R, X, Y, doit) \
243 _FP_FRAC_DECL_8(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c); \
244 _FP_FRAC_DECL_2(_d); _FP_FRAC_DECL_2(_e); _FP_FRAC_DECL_2(_f); \
246 doit(_FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0), X##_f[0], Y##_f[0]); \
247 doit(_b_f1, _b_f0, X##_f[0], Y##_f[1]); \
248 doit(_c_f1, _c_f0, X##_f[1], Y##_f[0]); \
249 doit(_d_f1, _d_f0, X##_f[1], Y##_f[1]); \
250 doit(_e_f1, _e_f0, X##_f[0], Y##_f[2]); \
251 doit(_f_f1, _f_f0, X##_f[2], Y##_f[0]); \
252 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
253 _FP_FRAC_WORD_8(_z,1), 0,_b_f1,_b_f0, \
254 0,0,_FP_FRAC_WORD_8(_z,1)); \
255 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
256 _FP_FRAC_WORD_8(_z,1), 0,_c_f1,_c_f0, \
257 _FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
258 _FP_FRAC_WORD_8(_z,1)); \
259 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
260 _FP_FRAC_WORD_8(_z,2), 0,_d_f1,_d_f0, \
261 0,_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2)); \
262 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
263 _FP_FRAC_WORD_8(_z,2), 0,_e_f1,_e_f0, \
264 _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
265 _FP_FRAC_WORD_8(_z,2)); \
266 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
267 _FP_FRAC_WORD_8(_z,2), 0,_f_f1,_f_f0, \
268 _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
269 _FP_FRAC_WORD_8(_z,2)); \
270 doit(_b_f1, _b_f0, X##_f[0], Y##_f[3]); \
271 doit(_c_f1, _c_f0, X##_f[3], Y##_f[0]); \
272 doit(_d_f1, _d_f0, X##_f[1], Y##_f[2]); \
273 doit(_e_f1, _e_f0, X##_f[2], Y##_f[1]); \
274 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
275 _FP_FRAC_WORD_8(_z,3), 0,_b_f1,_b_f0, \
276 0,_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3)); \
277 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
278 _FP_FRAC_WORD_8(_z,3), 0,_c_f1,_c_f0, \
279 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
280 _FP_FRAC_WORD_8(_z,3)); \
281 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
282 _FP_FRAC_WORD_8(_z,3), 0,_d_f1,_d_f0, \
283 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
284 _FP_FRAC_WORD_8(_z,3)); \
285 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
286 _FP_FRAC_WORD_8(_z,3), 0,_e_f1,_e_f0, \
287 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
288 _FP_FRAC_WORD_8(_z,3)); \
289 doit(_b_f1, _b_f0, X##_f[2], Y##_f[2]); \
290 doit(_c_f1, _c_f0, X##_f[1], Y##_f[3]); \
291 doit(_d_f1, _d_f0, X##_f[3], Y##_f[1]); \
292 doit(_e_f1, _e_f0, X##_f[2], Y##_f[3]); \
293 doit(_f_f1, _f_f0, X##_f[3], Y##_f[2]); \
294 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
295 _FP_FRAC_WORD_8(_z,4), 0,_b_f1,_b_f0, \
296 0,_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4)); \
297 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
298 _FP_FRAC_WORD_8(_z,4), 0,_c_f1,_c_f0, \
299 _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
300 _FP_FRAC_WORD_8(_z,4)); \
301 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
302 _FP_FRAC_WORD_8(_z,4), 0,_d_f1,_d_f0, \
303 _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
304 _FP_FRAC_WORD_8(_z,4)); \
305 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
306 _FP_FRAC_WORD_8(_z,5), 0,_e_f1,_e_f0, \
307 0,_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5)); \
308 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
309 _FP_FRAC_WORD_8(_z,5), 0,_f_f1,_f_f0, \
310 _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
311 _FP_FRAC_WORD_8(_z,5)); \
312 doit(_b_f1, _b_f0, X##_f[3], Y##_f[3]); \
313 __FP_FRAC_ADD_2(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
315 _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6)); \
317 /* Normalize since we know where the msb of the multiplicands \
318 were (bit B), we know that the msb of the of the product is \
319 at either 2B or 2B-1. */ \
320 _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits); \
321 __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2), \
322 _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0)); \
325 #define _FP_MUL_MEAT_4_gmp(wfracbits, R, X, Y) \
327 _FP_FRAC_DECL_8(_z); \
329 mpn_mul_n(_z_f, _x_f, _y_f, 4); \
331 /* Normalize since we know where the msb of the multiplicands \
332 were (bit B), we know that the msb of the of the product is \
333 at either 2B or 2B-1. */ \
334 _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits); \
335 __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2), \
336 _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0)); \
340 * Helper utility for _FP_DIV_MEAT_4_udiv:
343 #define umul_ppppmnnn(p3,p2,p1,p0,m,n2,n1,n0) \
346 umul_ppmm(p1,p0,m,n0); \
347 umul_ppmm(p2,_t,m,n1); \
348 __FP_FRAC_ADDI_2(p2,p1,_t); \
349 umul_ppmm(p3,_t,m,n2); \
350 __FP_FRAC_ADDI_2(p3,p2,_t); \
354 * Division algorithms:
357 #define _FP_DIV_MEAT_4_udiv(fs, R, X, Y) \
360 _FP_FRAC_DECL_4(_n); _FP_FRAC_DECL_4(_m); \
361 _FP_FRAC_SET_4(_n, _FP_ZEROFRAC_4); \
362 if (_FP_FRAC_GT_4(X, Y)) \
364 _n_f[3] = X##_f[0] << (_FP_W_TYPE_SIZE - 1); \
365 _FP_FRAC_SRL_4(X, 1); \
370 /* Normalize, i.e. make the most significant bit of the \
371 denominator set. */ \
372 _FP_FRAC_SLL_4(Y, _FP_WFRACXBITS_##fs); \
374 for (_i = 3; ; _i--) \
376 if (X##_f[3] == Y##_f[3]) \
378 /* This is a special case, not an optimization \
379 (X##_f[3]/Y##_f[3] would not fit into UWtype). \
380 As X## is guaranteed to be < Y, R##_f[_i] can be either \
381 (UWtype)-1 or (UWtype)-2. */ \
385 __FP_FRAC_SUB_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
386 Y##_f[2], Y##_f[1], Y##_f[0], 0, \
387 X##_f[2], X##_f[1], X##_f[0], _n_f[_i]); \
388 _FP_FRAC_SUB_4(X, Y, X); \
389 if (X##_f[3] > Y##_f[3]) \
392 _FP_FRAC_ADD_4(X, Y, X); \
397 udiv_qrnnd(R##_f[_i], X##_f[3], X##_f[3], X##_f[2], Y##_f[3]); \
398 umul_ppppmnnn(_m_f[3], _m_f[2], _m_f[1], _m_f[0], \
399 R##_f[_i], Y##_f[2], Y##_f[1], Y##_f[0]); \
400 X##_f[2] = X##_f[1]; \
401 X##_f[1] = X##_f[0]; \
402 X##_f[0] = _n_f[_i]; \
403 if (_FP_FRAC_GT_4(_m, X)) \
406 _FP_FRAC_ADD_4(X, Y, X); \
407 if (_FP_FRAC_GE_4(X, Y) && _FP_FRAC_GT_4(_m, X)) \
410 _FP_FRAC_ADD_4(X, Y, X); \
413 _FP_FRAC_DEC_4(X, _m); \
416 if (!_FP_FRAC_EQ_4(X, _m)) \
417 R##_f[0] |= _FP_WORK_STICKY; \
426 * Square root algorithms:
427 * We have just one right now, maybe Newton approximation
428 * should be added for those machines where division is fast.
431 #define _FP_SQRT_MEAT_4(R, S, T, X, q) \
435 T##_f[3] = S##_f[3] + q; \
436 if (T##_f[3] <= X##_f[3]) \
438 S##_f[3] = T##_f[3] + q; \
439 X##_f[3] -= T##_f[3]; \
442 _FP_FRAC_SLL_4(X, 1); \
445 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
448 T##_f[2] = S##_f[2] + q; \
449 T##_f[3] = S##_f[3]; \
450 if (T##_f[3] < X##_f[3] || \
451 (T##_f[3] == X##_f[3] && T##_f[2] <= X##_f[2])) \
453 S##_f[2] = T##_f[2] + q; \
454 S##_f[3] += (T##_f[2] > S##_f[2]); \
455 __FP_FRAC_DEC_2(X##_f[3], X##_f[2], \
456 T##_f[3], T##_f[2]); \
459 _FP_FRAC_SLL_4(X, 1); \
462 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
465 T##_f[1] = S##_f[1] + q; \
466 T##_f[2] = S##_f[2]; \
467 T##_f[3] = S##_f[3]; \
468 if (T##_f[3] < X##_f[3] || \
469 (T##_f[3] == X##_f[3] && (T##_f[2] < X##_f[2] || \
470 (T##_f[2] == X##_f[2] && T##_f[1] <= X##_f[1])))) \
472 S##_f[1] = T##_f[1] + q; \
473 S##_f[2] += (T##_f[1] > S##_f[1]); \
474 S##_f[3] += (T##_f[2] > S##_f[2]); \
475 __FP_FRAC_DEC_3(X##_f[3], X##_f[2], X##_f[1], \
476 T##_f[3], T##_f[2], T##_f[1]); \
479 _FP_FRAC_SLL_4(X, 1); \
482 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
483 while (q != _FP_WORK_ROUND) \
485 T##_f[0] = S##_f[0] + q; \
486 T##_f[1] = S##_f[1]; \
487 T##_f[2] = S##_f[2]; \
488 T##_f[3] = S##_f[3]; \
489 if (_FP_FRAC_GE_4(X,T)) \
491 S##_f[0] = T##_f[0] + q; \
492 S##_f[1] += (T##_f[0] > S##_f[0]); \
493 S##_f[2] += (T##_f[1] > S##_f[1]); \
494 S##_f[3] += (T##_f[2] > S##_f[2]); \
495 _FP_FRAC_DEC_4(X, T); \
498 _FP_FRAC_SLL_4(X, 1); \
501 if (!_FP_FRAC_ZEROP_4(X)) \
503 if (_FP_FRAC_GT_4(X,S)) \
504 R##_f[0] |= _FP_WORK_ROUND; \
505 R##_f[0] |= _FP_WORK_STICKY; \
514 #define __FP_FRAC_SET_4(X,I3,I2,I1,I0) \
515 (X##_f[3] = I3, X##_f[2] = I2, X##_f[1] = I1, X##_f[0] = I0)
517 #ifndef __FP_FRAC_ADD_3
518 #define __FP_FRAC_ADD_3(r2,r1,r0,x2,x1,x0,y2,y1,y0) \
520 _FP_W_TYPE _c1, _c2; \
527 r2 = x2 + y2 + _c2; \
531 #ifndef __FP_FRAC_ADD_4
532 #define __FP_FRAC_ADD_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
534 _FP_W_TYPE _c1, _c2, _c3; \
545 r3 = x3 + y3 + _c3; \
549 #ifndef __FP_FRAC_SUB_3
550 #define __FP_FRAC_SUB_3(r2,r1,r0,x2,x1,x0,y2,y1,y0) \
552 _FP_W_TYPE _c1, _c2; \
559 r2 = x2 - y2 - _c2; \
563 #ifndef __FP_FRAC_SUB_4
564 #define __FP_FRAC_SUB_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
566 _FP_W_TYPE _c1, _c2, _c3; \
577 r3 = x3 - y3 - _c3; \
581 #ifndef __FP_FRAC_DEC_3
582 #define __FP_FRAC_DEC_3(x2,x1,x0,y2,y1,y0) \
584 UWtype _t0, _t1, _t2; \
585 _t0 = x0, _t1 = x1, _t2 = x2; \
586 __FP_FRAC_SUB_3 (x2, x1, x0, _t2, _t1, _t0, y2, y1, y0); \
590 #ifndef __FP_FRAC_DEC_4
591 #define __FP_FRAC_DEC_4(x3,x2,x1,x0,y3,y2,y1,y0) \
593 UWtype _t0, _t1, _t2, _t3; \
594 _t0 = x0, _t1 = x1, _t2 = x2, _t3 = x3; \
595 __FP_FRAC_SUB_4 (x3,x2,x1,x0,_t3,_t2,_t1,_t0, y3,y2,y1,y0); \
599 #ifndef __FP_FRAC_ADDI_4
600 #define __FP_FRAC_ADDI_4(x3,x2,x1,x0,i) \
603 _t = ((x0 += i) < i); \
604 x1 += _t; _t = (x1 < _t); \
605 x2 += _t; _t = (x2 < _t); \
610 /* Convert FP values between word sizes. This appears to be more
611 * complicated than I'd have expected it to be, so these might be
612 * wrong... These macros are in any case somewhat bogus because they
613 * use information about what various FRAC_n variables look like
614 * internally [eg, that 2 word vars are X_f0 and x_f1]. But so do
615 * the ones in op-2.h and op-1.h.
617 #define _FP_FRAC_COPY_1_4(D, S) (D##_f = S##_f[0])
619 #define _FP_FRAC_COPY_2_4(D, S) \
625 /* Assembly/disassembly for converting to/from integral types.
626 * No shifting or overflow handled here.
628 /* Put the FP value X into r, which is an integer of size rsize. */
629 #define _FP_FRAC_ASSEMBLE_4(r, X, rsize) \
631 if (rsize <= _FP_W_TYPE_SIZE) \
633 else if (rsize <= 2*_FP_W_TYPE_SIZE) \
636 r <<= _FP_W_TYPE_SIZE; \
641 /* I'm feeling lazy so we deal with int == 3words (implausible)*/ \
642 /* and int == 4words as a single case. */ \
644 r <<= _FP_W_TYPE_SIZE; \
646 r <<= _FP_W_TYPE_SIZE; \
648 r <<= _FP_W_TYPE_SIZE; \
653 /* "No disassemble Number Five!" */
654 /* move an integer of size rsize into X's fractional part. We rely on
655 * the _f[] array consisting of words of size _FP_W_TYPE_SIZE to avoid
656 * having to mask the values we store into it.
658 #define _FP_FRAC_DISASSEMBLE_4(X, r, rsize) \
661 X##_f[1] = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE); \
662 X##_f[2] = (rsize <= 2*_FP_W_TYPE_SIZE ? 0 : r >> 2*_FP_W_TYPE_SIZE); \
663 X##_f[3] = (rsize <= 3*_FP_W_TYPE_SIZE ? 0 : r >> 3*_FP_W_TYPE_SIZE); \
666 #define _FP_FRAC_COPY_4_1(D, S) \
669 D##_f[1] = D##_f[2] = D##_f[3] = 0; \
672 #define _FP_FRAC_COPY_4_2(D, S) \
676 D##_f[2] = D##_f[3] = 0; \