1 /* Software floating-point emulation.
2 Basic four-word fraction declaration and manipulation.
3 Copyright (C) 1997-2013 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 In addition to the permissions in the GNU Lesser General Public
16 License, the Free Software Foundation gives you unlimited
17 permission to link the compiled version of this file into
18 combinations with other programs, and to distribute those
19 combinations without any restriction coming from the use of this
20 file. (The Lesser General Public License restrictions do apply in
21 other respects; for example, they cover modification of the file,
22 and distribution when not linked into a combine executable.)
24 The GNU C Library is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 Lesser General Public License for more details.
29 You should have received a copy of the GNU Lesser General Public
30 License along with the GNU C Library; if not, see
31 <http://www.gnu.org/licenses/>. */
33 #define _FP_FRAC_DECL_4(X) _FP_W_TYPE X##_f[4]
34 #define _FP_FRAC_COPY_4(D,S) \
35 (D##_f[0] = S##_f[0], D##_f[1] = S##_f[1], \
36 D##_f[2] = S##_f[2], D##_f[3] = S##_f[3])
37 #define _FP_FRAC_SET_4(X,I) __FP_FRAC_SET_4(X, I)
38 #define _FP_FRAC_HIGH_4(X) (X##_f[3])
39 #define _FP_FRAC_LOW_4(X) (X##_f[0])
40 #define _FP_FRAC_WORD_4(X,w) (X##_f[w])
42 #define _FP_FRAC_SLL_4(X,N) \
44 _FP_I_TYPE _up, _down, _skip, _i; \
45 _skip = (N) / _FP_W_TYPE_SIZE; \
46 _up = (N) % _FP_W_TYPE_SIZE; \
47 _down = _FP_W_TYPE_SIZE - _up; \
49 for (_i = 3; _i >= _skip; --_i) \
50 X##_f[_i] = X##_f[_i-_skip]; \
53 for (_i = 3; _i > _skip; --_i) \
54 X##_f[_i] = X##_f[_i-_skip] << _up \
55 | X##_f[_i-_skip-1] >> _down; \
56 X##_f[_i--] = X##_f[0] << _up; \
58 for (; _i >= 0; --_i) \
62 /* This one was broken too */
63 #define _FP_FRAC_SRL_4(X,N) \
65 _FP_I_TYPE _up, _down, _skip, _i; \
66 _skip = (N) / _FP_W_TYPE_SIZE; \
67 _down = (N) % _FP_W_TYPE_SIZE; \
68 _up = _FP_W_TYPE_SIZE - _down; \
70 for (_i = 0; _i <= 3-_skip; ++_i) \
71 X##_f[_i] = X##_f[_i+_skip]; \
74 for (_i = 0; _i < 3-_skip; ++_i) \
75 X##_f[_i] = X##_f[_i+_skip] >> _down \
76 | X##_f[_i+_skip+1] << _up; \
77 X##_f[_i++] = X##_f[3] >> _down; \
79 for (; _i < 4; ++_i) \
84 /* Right shift with sticky-lsb.
85 * What this actually means is that we do a standard right-shift,
86 * but that if any of the bits that fall off the right hand side
87 * were one then we always set the LSbit.
89 #define _FP_FRAC_SRST_4(X,S,N,size) \
91 _FP_I_TYPE _up, _down, _skip, _i; \
93 _skip = (N) / _FP_W_TYPE_SIZE; \
94 _down = (N) % _FP_W_TYPE_SIZE; \
95 _up = _FP_W_TYPE_SIZE - _down; \
96 for (_s = _i = 0; _i < _skip; ++_i) \
99 for (_i = 0; _i <= 3-_skip; ++_i) \
100 X##_f[_i] = X##_f[_i+_skip]; \
103 _s |= X##_f[_i] << _up; \
104 for (_i = 0; _i < 3-_skip; ++_i) \
105 X##_f[_i] = X##_f[_i+_skip] >> _down \
106 | X##_f[_i+_skip+1] << _up; \
107 X##_f[_i++] = X##_f[3] >> _down; \
109 for (; _i < 4; ++_i) \
114 #define _FP_FRAC_SRS_4(X,N,size) \
117 _FP_FRAC_SRST_4(X, _sticky, N, size); \
118 X##_f[0] |= _sticky; \
121 #define _FP_FRAC_ADD_4(R,X,Y) \
122 __FP_FRAC_ADD_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
123 X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
124 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
126 #define _FP_FRAC_SUB_4(R,X,Y) \
127 __FP_FRAC_SUB_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
128 X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
129 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
131 #define _FP_FRAC_DEC_4(X,Y) \
132 __FP_FRAC_DEC_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
133 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
135 #define _FP_FRAC_ADDI_4(X,I) \
136 __FP_FRAC_ADDI_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], I)
138 #define _FP_ZEROFRAC_4 0,0,0,0
139 #define _FP_MINFRAC_4 0,0,0,1
140 #define _FP_MAXFRAC_4 (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0)
142 #define _FP_FRAC_ZEROP_4(X) ((X##_f[0] | X##_f[1] | X##_f[2] | X##_f[3]) == 0)
143 #define _FP_FRAC_NEGP_4(X) ((_FP_WS_TYPE)X##_f[3] < 0)
144 #define _FP_FRAC_OVERP_4(fs,X) (_FP_FRAC_HIGH_##fs(X) & _FP_OVERFLOW_##fs)
145 #define _FP_FRAC_CLEAR_OVERP_4(fs,X) (_FP_FRAC_HIGH_##fs(X) &= ~_FP_OVERFLOW_##fs)
147 #define _FP_FRAC_EQ_4(X,Y) \
148 (X##_f[0] == Y##_f[0] && X##_f[1] == Y##_f[1] \
149 && X##_f[2] == Y##_f[2] && X##_f[3] == Y##_f[3])
151 #define _FP_FRAC_GT_4(X,Y) \
152 (X##_f[3] > Y##_f[3] || \
153 (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
154 (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
155 (X##_f[1] == Y##_f[1] && X##_f[0] > Y##_f[0]) \
160 #define _FP_FRAC_GE_4(X,Y) \
161 (X##_f[3] > Y##_f[3] || \
162 (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
163 (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
164 (X##_f[1] == Y##_f[1] && X##_f[0] >= Y##_f[0]) \
170 #define _FP_FRAC_CLZ_4(R,X) \
174 __FP_CLZ(R,X##_f[3]); \
178 __FP_CLZ(R,X##_f[2]); \
179 R += _FP_W_TYPE_SIZE; \
183 __FP_CLZ(R,X##_f[1]); \
184 R += _FP_W_TYPE_SIZE*2; \
188 __FP_CLZ(R,X##_f[0]); \
189 R += _FP_W_TYPE_SIZE*3; \
194 #define _FP_UNPACK_RAW_4(fs, X, val) \
196 union _FP_UNION_##fs _flo; _flo.flt = (val); \
197 X##_f[0] = _flo.bits.frac0; \
198 X##_f[1] = _flo.bits.frac1; \
199 X##_f[2] = _flo.bits.frac2; \
200 X##_f[3] = _flo.bits.frac3; \
201 X##_e = _flo.bits.exp; \
202 X##_s = _flo.bits.sign; \
205 #define _FP_UNPACK_RAW_4_P(fs, X, val) \
207 union _FP_UNION_##fs *_flo = \
208 (union _FP_UNION_##fs *)(val); \
210 X##_f[0] = _flo->bits.frac0; \
211 X##_f[1] = _flo->bits.frac1; \
212 X##_f[2] = _flo->bits.frac2; \
213 X##_f[3] = _flo->bits.frac3; \
214 X##_e = _flo->bits.exp; \
215 X##_s = _flo->bits.sign; \
218 #define _FP_PACK_RAW_4(fs, val, X) \
220 union _FP_UNION_##fs _flo; \
221 _flo.bits.frac0 = X##_f[0]; \
222 _flo.bits.frac1 = X##_f[1]; \
223 _flo.bits.frac2 = X##_f[2]; \
224 _flo.bits.frac3 = X##_f[3]; \
225 _flo.bits.exp = X##_e; \
226 _flo.bits.sign = X##_s; \
230 #define _FP_PACK_RAW_4_P(fs, val, X) \
232 union _FP_UNION_##fs *_flo = \
233 (union _FP_UNION_##fs *)(val); \
235 _flo->bits.frac0 = X##_f[0]; \
236 _flo->bits.frac1 = X##_f[1]; \
237 _flo->bits.frac2 = X##_f[2]; \
238 _flo->bits.frac3 = X##_f[3]; \
239 _flo->bits.exp = X##_e; \
240 _flo->bits.sign = X##_s; \
244 * Multiplication algorithms:
247 /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
249 #define _FP_MUL_MEAT_4_wide(wfracbits, R, X, Y, doit) \
251 _FP_FRAC_DECL_8(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c); \
252 _FP_FRAC_DECL_2(_d); _FP_FRAC_DECL_2(_e); _FP_FRAC_DECL_2(_f); \
254 doit(_FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0), X##_f[0], Y##_f[0]); \
255 doit(_b_f1, _b_f0, X##_f[0], Y##_f[1]); \
256 doit(_c_f1, _c_f0, X##_f[1], Y##_f[0]); \
257 doit(_d_f1, _d_f0, X##_f[1], Y##_f[1]); \
258 doit(_e_f1, _e_f0, X##_f[0], Y##_f[2]); \
259 doit(_f_f1, _f_f0, X##_f[2], Y##_f[0]); \
260 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
261 _FP_FRAC_WORD_8(_z,1), 0,_b_f1,_b_f0, \
262 0,0,_FP_FRAC_WORD_8(_z,1)); \
263 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
264 _FP_FRAC_WORD_8(_z,1), 0,_c_f1,_c_f0, \
265 _FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
266 _FP_FRAC_WORD_8(_z,1)); \
267 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
268 _FP_FRAC_WORD_8(_z,2), 0,_d_f1,_d_f0, \
269 0,_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2)); \
270 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
271 _FP_FRAC_WORD_8(_z,2), 0,_e_f1,_e_f0, \
272 _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
273 _FP_FRAC_WORD_8(_z,2)); \
274 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
275 _FP_FRAC_WORD_8(_z,2), 0,_f_f1,_f_f0, \
276 _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
277 _FP_FRAC_WORD_8(_z,2)); \
278 doit(_b_f1, _b_f0, X##_f[0], Y##_f[3]); \
279 doit(_c_f1, _c_f0, X##_f[3], Y##_f[0]); \
280 doit(_d_f1, _d_f0, X##_f[1], Y##_f[2]); \
281 doit(_e_f1, _e_f0, X##_f[2], Y##_f[1]); \
282 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
283 _FP_FRAC_WORD_8(_z,3), 0,_b_f1,_b_f0, \
284 0,_FP_FRAC_WORD_8(_z,4),_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,_c_f1,_c_f0, \
287 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
288 _FP_FRAC_WORD_8(_z,3)); \
289 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
290 _FP_FRAC_WORD_8(_z,3), 0,_d_f1,_d_f0, \
291 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
292 _FP_FRAC_WORD_8(_z,3)); \
293 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
294 _FP_FRAC_WORD_8(_z,3), 0,_e_f1,_e_f0, \
295 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
296 _FP_FRAC_WORD_8(_z,3)); \
297 doit(_b_f1, _b_f0, X##_f[2], Y##_f[2]); \
298 doit(_c_f1, _c_f0, X##_f[1], Y##_f[3]); \
299 doit(_d_f1, _d_f0, X##_f[3], Y##_f[1]); \
300 doit(_e_f1, _e_f0, X##_f[2], Y##_f[3]); \
301 doit(_f_f1, _f_f0, X##_f[3], Y##_f[2]); \
302 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
303 _FP_FRAC_WORD_8(_z,4), 0,_b_f1,_b_f0, \
304 0,_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4)); \
305 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
306 _FP_FRAC_WORD_8(_z,4), 0,_c_f1,_c_f0, \
307 _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
308 _FP_FRAC_WORD_8(_z,4)); \
309 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
310 _FP_FRAC_WORD_8(_z,4), 0,_d_f1,_d_f0, \
311 _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
312 _FP_FRAC_WORD_8(_z,4)); \
313 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
314 _FP_FRAC_WORD_8(_z,5), 0,_e_f1,_e_f0, \
315 0,_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5)); \
316 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
317 _FP_FRAC_WORD_8(_z,5), 0,_f_f1,_f_f0, \
318 _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
319 _FP_FRAC_WORD_8(_z,5)); \
320 doit(_b_f1, _b_f0, X##_f[3], Y##_f[3]); \
321 __FP_FRAC_ADD_2(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
323 _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6)); \
325 /* Normalize since we know where the msb of the multiplicands \
326 were (bit B), we know that the msb of the of the product is \
327 at either 2B or 2B-1. */ \
328 _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits); \
329 __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2), \
330 _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0)); \
333 #define _FP_MUL_MEAT_4_gmp(wfracbits, R, X, Y) \
335 _FP_FRAC_DECL_8(_z); \
337 mpn_mul_n(_z_f, _x_f, _y_f, 4); \
339 /* Normalize since we know where the msb of the multiplicands \
340 were (bit B), we know that the msb of the of the product is \
341 at either 2B or 2B-1. */ \
342 _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits); \
343 __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2), \
344 _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0)); \
348 * Helper utility for _FP_DIV_MEAT_4_udiv:
351 #define umul_ppppmnnn(p3,p2,p1,p0,m,n2,n1,n0) \
354 umul_ppmm(p1,p0,m,n0); \
355 umul_ppmm(p2,_t,m,n1); \
356 __FP_FRAC_ADDI_2(p2,p1,_t); \
357 umul_ppmm(p3,_t,m,n2); \
358 __FP_FRAC_ADDI_2(p3,p2,_t); \
362 * Division algorithms:
365 #define _FP_DIV_MEAT_4_udiv(fs, R, X, Y) \
368 _FP_FRAC_DECL_4(_n); _FP_FRAC_DECL_4(_m); \
369 _FP_FRAC_SET_4(_n, _FP_ZEROFRAC_4); \
370 if (_FP_FRAC_GT_4(X, Y)) \
372 _n_f[3] = X##_f[0] << (_FP_W_TYPE_SIZE - 1); \
373 _FP_FRAC_SRL_4(X, 1); \
378 /* Normalize, i.e. make the most significant bit of the \
379 denominator set. */ \
380 _FP_FRAC_SLL_4(Y, _FP_WFRACXBITS_##fs); \
382 for (_i = 3; ; _i--) \
384 if (X##_f[3] == Y##_f[3]) \
386 /* This is a special case, not an optimization \
387 (X##_f[3]/Y##_f[3] would not fit into UWtype). \
388 As X## is guaranteed to be < Y, R##_f[_i] can be either \
389 (UWtype)-1 or (UWtype)-2. */ \
393 __FP_FRAC_SUB_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
394 Y##_f[2], Y##_f[1], Y##_f[0], 0, \
395 X##_f[2], X##_f[1], X##_f[0], _n_f[_i]); \
396 _FP_FRAC_SUB_4(X, Y, X); \
397 if (X##_f[3] > Y##_f[3]) \
400 _FP_FRAC_ADD_4(X, Y, X); \
405 udiv_qrnnd(R##_f[_i], X##_f[3], X##_f[3], X##_f[2], Y##_f[3]); \
406 umul_ppppmnnn(_m_f[3], _m_f[2], _m_f[1], _m_f[0], \
407 R##_f[_i], Y##_f[2], Y##_f[1], Y##_f[0]); \
408 X##_f[2] = X##_f[1]; \
409 X##_f[1] = X##_f[0]; \
410 X##_f[0] = _n_f[_i]; \
411 if (_FP_FRAC_GT_4(_m, X)) \
414 _FP_FRAC_ADD_4(X, Y, X); \
415 if (_FP_FRAC_GE_4(X, Y) && _FP_FRAC_GT_4(_m, X)) \
418 _FP_FRAC_ADD_4(X, Y, X); \
421 _FP_FRAC_DEC_4(X, _m); \
424 if (!_FP_FRAC_EQ_4(X, _m)) \
425 R##_f[0] |= _FP_WORK_STICKY; \
434 * Square root algorithms:
435 * We have just one right now, maybe Newton approximation
436 * should be added for those machines where division is fast.
439 #define _FP_SQRT_MEAT_4(R, S, T, X, q) \
443 T##_f[3] = S##_f[3] + q; \
444 if (T##_f[3] <= X##_f[3]) \
446 S##_f[3] = T##_f[3] + q; \
447 X##_f[3] -= T##_f[3]; \
450 _FP_FRAC_SLL_4(X, 1); \
453 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
456 T##_f[2] = S##_f[2] + q; \
457 T##_f[3] = S##_f[3]; \
458 if (T##_f[3] < X##_f[3] || \
459 (T##_f[3] == X##_f[3] && T##_f[2] <= X##_f[2])) \
461 S##_f[2] = T##_f[2] + q; \
462 S##_f[3] += (T##_f[2] > S##_f[2]); \
463 __FP_FRAC_DEC_2(X##_f[3], X##_f[2], \
464 T##_f[3], T##_f[2]); \
467 _FP_FRAC_SLL_4(X, 1); \
470 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
473 T##_f[1] = S##_f[1] + q; \
474 T##_f[2] = S##_f[2]; \
475 T##_f[3] = S##_f[3]; \
476 if (T##_f[3] < X##_f[3] || \
477 (T##_f[3] == X##_f[3] && (T##_f[2] < X##_f[2] || \
478 (T##_f[2] == X##_f[2] && T##_f[1] <= X##_f[1])))) \
480 S##_f[1] = T##_f[1] + q; \
481 S##_f[2] += (T##_f[1] > S##_f[1]); \
482 S##_f[3] += (T##_f[2] > S##_f[2]); \
483 __FP_FRAC_DEC_3(X##_f[3], X##_f[2], X##_f[1], \
484 T##_f[3], T##_f[2], T##_f[1]); \
487 _FP_FRAC_SLL_4(X, 1); \
490 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
491 while (q != _FP_WORK_ROUND) \
493 T##_f[0] = S##_f[0] + q; \
494 T##_f[1] = S##_f[1]; \
495 T##_f[2] = S##_f[2]; \
496 T##_f[3] = S##_f[3]; \
497 if (_FP_FRAC_GE_4(X,T)) \
499 S##_f[0] = T##_f[0] + q; \
500 S##_f[1] += (T##_f[0] > S##_f[0]); \
501 S##_f[2] += (T##_f[1] > S##_f[1]); \
502 S##_f[3] += (T##_f[2] > S##_f[2]); \
503 _FP_FRAC_DEC_4(X, T); \
506 _FP_FRAC_SLL_4(X, 1); \
509 if (!_FP_FRAC_ZEROP_4(X)) \
511 if (_FP_FRAC_GT_4(X,S)) \
512 R##_f[0] |= _FP_WORK_ROUND; \
513 R##_f[0] |= _FP_WORK_STICKY; \
522 #define __FP_FRAC_SET_4(X,I3,I2,I1,I0) \
523 (X##_f[3] = I3, X##_f[2] = I2, X##_f[1] = I1, X##_f[0] = I0)
525 #ifndef __FP_FRAC_ADD_3
526 #define __FP_FRAC_ADD_3(r2,r1,r0,x2,x1,x0,y2,y1,y0) \
528 _FP_W_TYPE __FP_FRAC_ADD_3_c1, __FP_FRAC_ADD_3_c2; \
530 __FP_FRAC_ADD_3_c1 = r0 < x0; \
532 __FP_FRAC_ADD_3_c2 = r1 < x1; \
533 r1 += __FP_FRAC_ADD_3_c1; \
534 __FP_FRAC_ADD_3_c2 |= r1 < __FP_FRAC_ADD_3_c1; \
535 r2 = x2 + y2 + __FP_FRAC_ADD_3_c2; \
539 #ifndef __FP_FRAC_ADD_4
540 #define __FP_FRAC_ADD_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
542 _FP_W_TYPE _c1, _c2, _c3; \
553 r3 = x3 + y3 + _c3; \
557 #ifndef __FP_FRAC_SUB_3
558 #define __FP_FRAC_SUB_3(r2,r1,r0,x2,x1,x0,y2,y1,y0) \
560 _FP_W_TYPE _c1, _c2; \
566 _c2 |= _c1 && (y1 == x1); \
567 r2 = x2 - y2 - _c2; \
571 #ifndef __FP_FRAC_SUB_4
572 #define __FP_FRAC_SUB_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
574 _FP_W_TYPE _c1, _c2, _c3; \
580 _c2 |= _c1 && (y1 == x1); \
584 _c3 |= _c2 && (y2 == x2); \
585 r3 = x3 - y3 - _c3; \
589 #ifndef __FP_FRAC_DEC_3
590 #define __FP_FRAC_DEC_3(x2,x1,x0,y2,y1,y0) \
592 UWtype _t0, _t1, _t2; \
593 _t0 = x0, _t1 = x1, _t2 = x2; \
594 __FP_FRAC_SUB_3 (x2, x1, x0, _t2, _t1, _t0, y2, y1, y0); \
598 #ifndef __FP_FRAC_DEC_4
599 #define __FP_FRAC_DEC_4(x3,x2,x1,x0,y3,y2,y1,y0) \
601 UWtype _t0, _t1, _t2, _t3; \
602 _t0 = x0, _t1 = x1, _t2 = x2, _t3 = x3; \
603 __FP_FRAC_SUB_4 (x3,x2,x1,x0,_t3,_t2,_t1,_t0, y3,y2,y1,y0); \
607 #ifndef __FP_FRAC_ADDI_4
608 #define __FP_FRAC_ADDI_4(x3,x2,x1,x0,i) \
611 _t = ((x0 += i) < i); \
612 x1 += _t; _t = (x1 < _t); \
613 x2 += _t; _t = (x2 < _t); \
618 /* Convert FP values between word sizes. This appears to be more
619 * complicated than I'd have expected it to be, so these might be
620 * wrong... These macros are in any case somewhat bogus because they
621 * use information about what various FRAC_n variables look like
622 * internally [eg, that 2 word vars are X_f0 and x_f1]. But so do
623 * the ones in op-2.h and op-1.h.
625 #define _FP_FRAC_COPY_1_4(D, S) (D##_f = S##_f[0])
627 #define _FP_FRAC_COPY_2_4(D, S) \
633 /* Assembly/disassembly for converting to/from integral types.
634 * No shifting or overflow handled here.
636 /* Put the FP value X into r, which is an integer of size rsize. */
637 #define _FP_FRAC_ASSEMBLE_4(r, X, rsize) \
639 if (rsize <= _FP_W_TYPE_SIZE) \
641 else if (rsize <= 2*_FP_W_TYPE_SIZE) \
644 r <<= _FP_W_TYPE_SIZE; \
649 /* I'm feeling lazy so we deal with int == 3words (implausible)*/ \
650 /* and int == 4words as a single case. */ \
652 r <<= _FP_W_TYPE_SIZE; \
654 r <<= _FP_W_TYPE_SIZE; \
656 r <<= _FP_W_TYPE_SIZE; \
661 /* "No disassemble Number Five!" */
662 /* move an integer of size rsize into X's fractional part. We rely on
663 * the _f[] array consisting of words of size _FP_W_TYPE_SIZE to avoid
664 * having to mask the values we store into it.
666 #define _FP_FRAC_DISASSEMBLE_4(X, r, rsize) \
669 X##_f[1] = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE); \
670 X##_f[2] = (rsize <= 2*_FP_W_TYPE_SIZE ? 0 : r >> 2*_FP_W_TYPE_SIZE); \
671 X##_f[3] = (rsize <= 3*_FP_W_TYPE_SIZE ? 0 : r >> 3*_FP_W_TYPE_SIZE); \
674 #define _FP_FRAC_COPY_4_1(D, S) \
677 D##_f[1] = D##_f[2] = D##_f[3] = 0; \
680 #define _FP_FRAC_COPY_4_2(D, S) \
684 D##_f[2] = D##_f[3] = 0; \
687 #define _FP_FRAC_COPY_4_4(D,S) _FP_FRAC_COPY_4(D,S)