Update.
[glibc.git] / soft-fp / op-4.h
blobbbfd433a863114d4123d564cc6b7a8b5424a0f25
1 /* Software floating-point emulation.
2 Basic four-word fraction declaration and manipulation.
3 Copyright (C) 1997,1998,1999 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
23 02111-1307 USA. */
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) \
35 do { \
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; \
40 if (!_up) \
41 for (_i = 3; _i >= _skip; --_i) \
42 X##_f[_i] = X##_f[_i-_skip]; \
43 else \
44 { \
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; \
49 } \
50 for (; _i >= 0; --_i) \
51 X##_f[_i] = 0; \
52 } while (0)
54 /* This one was broken too */
55 #define _FP_FRAC_SRL_4(X,N) \
56 do { \
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; \
61 if (!_down) \
62 for (_i = 0; _i <= 3-_skip; ++_i) \
63 X##_f[_i] = X##_f[_i+_skip]; \
64 else \
65 { \
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; \
70 } \
71 for (; _i < 4; ++_i) \
72 X##_f[_i] = 0; \
73 } while (0)
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_SRS_4(X,N,size) \
82 do { \
83 _FP_I_TYPE _up, _down, _skip, _i; \
84 _FP_W_TYPE _s; \
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) \
89 _s |= X##_f[_i]; \
90 _s |= X##_f[_i] << _up; \
91 /* s is now != 0 if we want to set the LSbit */ \
92 if (!_down) \
93 for (_i = 0; _i <= 3-_skip; ++_i) \
94 X##_f[_i] = X##_f[_i+_skip]; \
95 else \
96 { \
97 for (_i = 0; _i < 3-_skip; ++_i) \
98 X##_f[_i] = X##_f[_i+_skip] >> _down \
99 | X##_f[_i+_skip+1] << _up; \
100 X##_f[_i++] = X##_f[3] >> _down; \
102 for (; _i < 4; ++_i) \
103 X##_f[_i] = 0; \
104 /* don't fix the LSB until the very end when we're sure f[0] is stable */ \
105 X##_f[0] |= (_s != 0); \
106 } while (0)
108 #define _FP_FRAC_ADD_4(R,X,Y) \
109 __FP_FRAC_ADD_4(R##_f[3], R##_f[2], R##_f[1], R##_f[0], \
110 X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
111 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
113 #define _FP_FRAC_SUB_4(R,X,Y) \
114 __FP_FRAC_SUB_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_DEC_4(X,Y) \
119 __FP_FRAC_DEC_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
120 Y##_f[3], Y##_f[2], Y##_f[1], Y##_f[0])
122 #define _FP_FRAC_ADDI_4(X,I) \
123 __FP_FRAC_ADDI_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], I)
125 #define _FP_ZEROFRAC_4 0,0,0,0
126 #define _FP_MINFRAC_4 0,0,0,1
127 #define _FP_MAXFRAC_4 (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0), (~(_FP_WS_TYPE)0)
129 #define _FP_FRAC_ZEROP_4(X) ((X##_f[0] | X##_f[1] | X##_f[2] | X##_f[3]) == 0)
130 #define _FP_FRAC_NEGP_4(X) ((_FP_WS_TYPE)X##_f[3] < 0)
131 #define _FP_FRAC_OVERP_4(fs,X) (_FP_FRAC_HIGH_##fs(X) & _FP_OVERFLOW_##fs)
132 #define _FP_FRAC_CLEAR_OVERP_4(fs,X) (_FP_FRAC_HIGH_##fs(X) &= ~_FP_OVERFLOW_##fs)
134 #define _FP_FRAC_EQ_4(X,Y) \
135 (X##_f[0] == Y##_f[0] && X##_f[1] == Y##_f[1] \
136 && X##_f[2] == Y##_f[2] && X##_f[3] == Y##_f[3])
138 #define _FP_FRAC_GT_4(X,Y) \
139 (X##_f[3] > Y##_f[3] || \
140 (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
141 (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
142 (X##_f[1] == Y##_f[1] && X##_f[0] > Y##_f[0]) \
143 )) \
144 )) \
147 #define _FP_FRAC_GE_4(X,Y) \
148 (X##_f[3] > Y##_f[3] || \
149 (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] || \
150 (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] || \
151 (X##_f[1] == Y##_f[1] && X##_f[0] >= Y##_f[0]) \
152 )) \
153 )) \
157 #define _FP_FRAC_CLZ_4(R,X) \
158 do { \
159 if (X##_f[3]) \
161 __FP_CLZ(R,X##_f[3]); \
163 else if (X##_f[2]) \
165 __FP_CLZ(R,X##_f[2]); \
166 R += _FP_W_TYPE_SIZE; \
168 else if (X##_f[1]) \
170 __FP_CLZ(R,X##_f[1]); \
171 R += _FP_W_TYPE_SIZE*2; \
173 else \
175 __FP_CLZ(R,X##_f[0]); \
176 R += _FP_W_TYPE_SIZE*3; \
178 } while(0)
181 #define _FP_UNPACK_RAW_4(fs, X, val) \
182 do { \
183 union _FP_UNION_##fs _flo; _flo.flt = (val); \
184 X##_f[0] = _flo.bits.frac0; \
185 X##_f[1] = _flo.bits.frac1; \
186 X##_f[2] = _flo.bits.frac2; \
187 X##_f[3] = _flo.bits.frac3; \
188 X##_e = _flo.bits.exp; \
189 X##_s = _flo.bits.sign; \
190 } while (0)
192 #define _FP_UNPACK_RAW_4_P(fs, X, val) \
193 do { \
194 union _FP_UNION_##fs *_flo = \
195 (union _FP_UNION_##fs *)(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; \
203 } while (0)
205 #define _FP_PACK_RAW_4(fs, val, X) \
206 do { \
207 union _FP_UNION_##fs _flo; \
208 _flo.bits.frac0 = X##_f[0]; \
209 _flo.bits.frac1 = X##_f[1]; \
210 _flo.bits.frac2 = X##_f[2]; \
211 _flo.bits.frac3 = X##_f[3]; \
212 _flo.bits.exp = X##_e; \
213 _flo.bits.sign = X##_s; \
214 (val) = _flo.flt; \
215 } while (0)
217 #define _FP_PACK_RAW_4_P(fs, val, X) \
218 do { \
219 union _FP_UNION_##fs *_flo = \
220 (union _FP_UNION_##fs *)(val); \
222 _flo->bits.frac0 = X##_f[0]; \
223 _flo->bits.frac1 = X##_f[1]; \
224 _flo->bits.frac2 = X##_f[2]; \
225 _flo->bits.frac3 = X##_f[3]; \
226 _flo->bits.exp = X##_e; \
227 _flo->bits.sign = X##_s; \
228 } while (0)
231 * Multiplication algorithms:
234 /* Given a 1W * 1W => 2W primitive, do the extended multiplication. */
236 #define _FP_MUL_MEAT_4_wide(wfracbits, R, X, Y, doit) \
237 do { \
238 _FP_FRAC_DECL_8(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c); \
239 _FP_FRAC_DECL_2(_d); _FP_FRAC_DECL_2(_e); _FP_FRAC_DECL_2(_f); \
241 doit(_FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0), X##_f[0], Y##_f[0]); \
242 doit(_b_f1, _b_f0, X##_f[0], Y##_f[1]); \
243 doit(_c_f1, _c_f0, X##_f[1], Y##_f[0]); \
244 doit(_d_f1, _d_f0, X##_f[1], Y##_f[1]); \
245 doit(_e_f1, _e_f0, X##_f[0], Y##_f[2]); \
246 doit(_f_f1, _f_f0, X##_f[2], Y##_f[0]); \
247 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
248 _FP_FRAC_WORD_8(_z,1), 0,_b_f1,_b_f0, \
249 0,0,_FP_FRAC_WORD_8(_z,1)); \
250 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
251 _FP_FRAC_WORD_8(_z,1), 0,_c_f1,_c_f0, \
252 _FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2), \
253 _FP_FRAC_WORD_8(_z,1)); \
254 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
255 _FP_FRAC_WORD_8(_z,2), 0,_d_f1,_d_f0, \
256 0,_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2)); \
257 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
258 _FP_FRAC_WORD_8(_z,2), 0,_e_f1,_e_f0, \
259 _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
260 _FP_FRAC_WORD_8(_z,2)); \
261 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
262 _FP_FRAC_WORD_8(_z,2), 0,_f_f1,_f_f0, \
263 _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3), \
264 _FP_FRAC_WORD_8(_z,2)); \
265 doit(_b_f1, _b_f0, X##_f[0], Y##_f[3]); \
266 doit(_c_f1, _c_f0, X##_f[3], Y##_f[0]); \
267 doit(_d_f1, _d_f0, X##_f[1], Y##_f[2]); \
268 doit(_e_f1, _e_f0, X##_f[2], Y##_f[1]); \
269 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
270 _FP_FRAC_WORD_8(_z,3), 0,_b_f1,_b_f0, \
271 0,_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3)); \
272 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
273 _FP_FRAC_WORD_8(_z,3), 0,_c_f1,_c_f0, \
274 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
275 _FP_FRAC_WORD_8(_z,3)); \
276 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
277 _FP_FRAC_WORD_8(_z,3), 0,_d_f1,_d_f0, \
278 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
279 _FP_FRAC_WORD_8(_z,3)); \
280 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
281 _FP_FRAC_WORD_8(_z,3), 0,_e_f1,_e_f0, \
282 _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4), \
283 _FP_FRAC_WORD_8(_z,3)); \
284 doit(_b_f1, _b_f0, X##_f[2], Y##_f[2]); \
285 doit(_c_f1, _c_f0, X##_f[1], Y##_f[3]); \
286 doit(_d_f1, _d_f0, X##_f[3], Y##_f[1]); \
287 doit(_e_f1, _e_f0, X##_f[2], Y##_f[3]); \
288 doit(_f_f1, _f_f0, X##_f[3], Y##_f[2]); \
289 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
290 _FP_FRAC_WORD_8(_z,4), 0,_b_f1,_b_f0, \
291 0,_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4)); \
292 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
293 _FP_FRAC_WORD_8(_z,4), 0,_c_f1,_c_f0, \
294 _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
295 _FP_FRAC_WORD_8(_z,4)); \
296 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
297 _FP_FRAC_WORD_8(_z,4), 0,_d_f1,_d_f0, \
298 _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5), \
299 _FP_FRAC_WORD_8(_z,4)); \
300 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
301 _FP_FRAC_WORD_8(_z,5), 0,_e_f1,_e_f0, \
302 0,_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5)); \
303 __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
304 _FP_FRAC_WORD_8(_z,5), 0,_f_f1,_f_f0, \
305 _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
306 _FP_FRAC_WORD_8(_z,5)); \
307 doit(_b_f1, _b_f0, X##_f[3], Y##_f[3]); \
308 __FP_FRAC_ADD_2(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6), \
309 _b_f1,_b_f0, \
310 _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6)); \
312 /* Normalize since we know where the msb of the multiplicands \
313 were (bit B), we know that the msb of the of the product is \
314 at either 2B or 2B-1. */ \
315 _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits); \
316 __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2), \
317 _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0)); \
318 } while (0)
320 #define _FP_MUL_MEAT_4_gmp(wfracbits, R, X, Y) \
321 do { \
322 _FP_FRAC_DECL_8(_z); \
324 mpn_mul_n(_z_f, _x_f, _y_f, 4); \
326 /* Normalize since we know where the msb of the multiplicands \
327 were (bit B), we know that the msb of the of the product is \
328 at either 2B or 2B-1. */ \
329 _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits); \
330 __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2), \
331 _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0)); \
332 } while (0)
335 * Helper utility for _FP_DIV_MEAT_4_udiv:
336 * pppp = m * nnn
338 #define umul_ppppmnnn(p3,p2,p1,p0,m,n2,n1,n0) \
339 do { \
340 UWtype _t; \
341 umul_ppmm(p1,p0,m,n0); \
342 umul_ppmm(p2,_t,m,n1); \
343 __FP_FRAC_ADDI_2(p2,p1,_t); \
344 umul_ppmm(p3,_t,m,n2); \
345 __FP_FRAC_ADDI_2(p3,p2,_t); \
346 } while (0)
349 * Division algorithms:
352 #define _FP_DIV_MEAT_4_udiv(fs, R, X, Y) \
353 do { \
354 int _i; \
355 _FP_FRAC_DECL_4(_n); _FP_FRAC_DECL_4(_m); \
356 _FP_FRAC_SET_4(_n, _FP_ZEROFRAC_4); \
357 if (_FP_FRAC_GT_4(X, Y)) \
359 _n_f[3] = X##_f[0] << (_FP_W_TYPE_SIZE - 1); \
360 _FP_FRAC_SRL_4(X, 1); \
362 else \
363 R##_e--; \
365 /* Normalize, i.e. make the most significant bit of the \
366 denominator set. */ \
367 _FP_FRAC_SLL_4(Y, _FP_WFRACXBITS_##fs); \
369 for (_i = 3; ; _i--) \
371 if (X##_f[3] == Y##_f[3]) \
373 /* This is a special case, not an optimization \
374 (X##_f[3]/Y##_f[3] would not fit into UWtype). \
375 As X## is guaranteed to be < Y, R##_f[_i] can be either \
376 (UWtype)-1 or (UWtype)-2. */ \
377 R##_f[_i] = -1; \
378 if (!_i) \
379 break; \
380 __FP_FRAC_SUB_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0], \
381 Y##_f[2], Y##_f[1], Y##_f[0], 0, \
382 X##_f[2], X##_f[1], X##_f[0], _n_f[_i]); \
383 _FP_FRAC_SUB_4(X, Y, X); \
384 if (X##_f[3] > Y##_f[3]) \
386 R##_f[_i] = -2; \
387 _FP_FRAC_ADD_4(X, Y, X); \
390 else \
392 udiv_qrnnd(R##_f[_i], X##_f[3], X##_f[3], X##_f[2], Y##_f[3]); \
393 umul_ppppmnnn(_m_f[3], _m_f[2], _m_f[1], _m_f[0], \
394 R##_f[_i], Y##_f[2], Y##_f[1], Y##_f[0]); \
395 X##_f[2] = X##_f[1]; \
396 X##_f[1] = X##_f[0]; \
397 X##_f[0] = _n_f[_i]; \
398 if (_FP_FRAC_GT_4(_m, X)) \
400 R##_f[_i]--; \
401 _FP_FRAC_ADD_4(X, Y, X); \
402 if (_FP_FRAC_GE_4(X, Y) && _FP_FRAC_GT_4(_m, X)) \
404 R##_f[_i]--; \
405 _FP_FRAC_ADD_4(X, Y, X); \
408 _FP_FRAC_DEC_4(X, _m); \
409 if (!_i) \
411 if (!_FP_FRAC_EQ_4(X, _m)) \
412 R##_f[0] |= _FP_WORK_STICKY; \
413 break; \
417 } while (0)
421 * Square root algorithms:
422 * We have just one right now, maybe Newton approximation
423 * should be added for those machines where division is fast.
426 #define _FP_SQRT_MEAT_4(R, S, T, X, q) \
427 do { \
428 while (q) \
430 T##_f[3] = S##_f[3] + q; \
431 if (T##_f[3] <= X##_f[3]) \
433 S##_f[3] = T##_f[3] + q; \
434 X##_f[3] -= T##_f[3]; \
435 R##_f[3] += q; \
437 _FP_FRAC_SLL_4(X, 1); \
438 q >>= 1; \
440 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
441 while (q) \
443 T##_f[2] = S##_f[2] + q; \
444 T##_f[3] = S##_f[3]; \
445 if (T##_f[3] < X##_f[3] || \
446 (T##_f[3] == X##_f[3] && T##_f[2] <= X##_f[2])) \
448 S##_f[2] = T##_f[2] + q; \
449 S##_f[3] += (T##_f[2] > S##_f[2]); \
450 __FP_FRAC_DEC_2(X##_f[3], X##_f[2], \
451 T##_f[3], T##_f[2]); \
452 R##_f[2] += q; \
454 _FP_FRAC_SLL_4(X, 1); \
455 q >>= 1; \
457 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
458 while (q) \
460 T##_f[1] = S##_f[1] + q; \
461 T##_f[2] = S##_f[2]; \
462 T##_f[3] = S##_f[3]; \
463 if (T##_f[3] < X##_f[3] || \
464 (T##_f[3] == X##_f[3] && (T##_f[2] < X##_f[2] || \
465 (T##_f[2] == X##_f[2] && T##_f[1] <= X##_f[1])))) \
467 S##_f[1] = T##_f[1] + q; \
468 S##_f[2] += (T##_f[1] > S##_f[1]); \
469 S##_f[3] += (T##_f[2] > S##_f[2]); \
470 __FP_FRAC_DEC_3(X##_f[3], X##_f[2], X##_f[1], \
471 T##_f[3], T##_f[2], T##_f[1]); \
472 R##_f[1] += q; \
474 _FP_FRAC_SLL_4(X, 1); \
475 q >>= 1; \
477 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
478 while (q != _FP_WORK_ROUND) \
480 T##_f[0] = S##_f[0] + q; \
481 T##_f[1] = S##_f[1]; \
482 T##_f[2] = S##_f[2]; \
483 T##_f[3] = S##_f[3]; \
484 if (_FP_FRAC_GE_4(X,T)) \
486 S##_f[0] = T##_f[0] + q; \
487 S##_f[1] += (T##_f[0] > S##_f[0]); \
488 S##_f[2] += (T##_f[1] > S##_f[1]); \
489 S##_f[3] += (T##_f[2] > S##_f[2]); \
490 _FP_FRAC_DEC_4(X, T); \
491 R##_f[0] += q; \
493 _FP_FRAC_SLL_4(X, 1); \
494 q >>= 1; \
496 if (!_FP_FRAC_ZEROP_4(X)) \
498 if (_FP_FRAC_GT_4(X,S)) \
499 R##_f[0] |= _FP_WORK_ROUND; \
500 R##_f[0] |= _FP_WORK_STICKY; \
502 } while (0)
506 * Internals
509 #define __FP_FRAC_SET_4(X,I3,I2,I1,I0) \
510 (X##_f[3] = I3, X##_f[2] = I2, X##_f[1] = I1, X##_f[0] = I0)
512 #ifndef __FP_FRAC_ADD_3
513 #define __FP_FRAC_ADD_3(r2,r1,r0,x2,x1,x0,y2,y1,y0) \
514 (r0 = x0 + y0, \
515 r1 = x1 + y1 + (r0 < x0), \
516 r2 = x2 + y2 + (r1 < x1))
517 #endif
519 #ifndef __FP_FRAC_ADD_4
520 #define __FP_FRAC_ADD_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
521 (r0 = x0 + y0, \
522 r1 = x1 + y1 + (r0 < x0), \
523 r2 = x2 + y2 + (r1 < x1), \
524 r3 = x3 + y3 + (r2 < x2))
525 #endif
527 #ifndef __FP_FRAC_SUB_3
528 #define __FP_FRAC_SUB_3(r2,r1,r0,x2,x1,x0,y2,y1,y0) \
529 (r0 = x0 - y0, \
530 r1 = x1 - y1 - (r0 > x0), \
531 r2 = x2 - y2 - (r1 > x1))
532 #endif
534 #ifndef __FP_FRAC_SUB_4
535 #define __FP_FRAC_SUB_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0) \
536 (r0 = x0 - y0, \
537 r1 = x1 - y1 - (r0 > x0), \
538 r2 = x2 - y2 - (r1 > x1), \
539 r3 = x3 - y3 - (r2 > x2))
540 #endif
542 #ifndef __FP_FRAC_DEC_3
543 #define __FP_FRAC_DEC_3(x2,x1,x0,y2,y1,y0) \
544 do { \
545 UWtype _t0, _t1; \
546 _t0 = x0; \
547 x0 -= y0; \
548 _t1 = x1; \
549 x1 -= y1 + (x0 > _t0); \
550 x2 -= y2 + (x1 > _t1); \
551 } while (0)
552 #endif
554 #ifndef __FP_FRAC_DEC_4
555 #define __FP_FRAC_DEC_4(x3,x2,x1,x0,y3,y2,y1,y0) \
556 do { \
557 UWtype _t0, _t1; \
558 _t0 = x0; \
559 x0 -= y0; \
560 _t1 = x1; \
561 x1 -= y1 + (x0 > _t0); \
562 _t0 = x2; \
563 x2 -= y2 + (x1 > _t1); \
564 x3 -= y3 + (x2 > _t0); \
565 } while (0)
566 #endif
568 #ifndef __FP_FRAC_ADDI_4
569 #define __FP_FRAC_ADDI_4(x3,x2,x1,x0,i) \
570 do { \
571 UWtype _t; \
572 _t = ((x0 += i) < i); \
573 x1 += _t; _t = (x1 < _t); \
574 x2 += _t; _t = (x2 < _t); \
575 x3 += _t; \
576 } while (0)
577 #endif
579 /* Convert FP values between word sizes. This appears to be more
580 * complicated than I'd have expected it to be, so these might be
581 * wrong... These macros are in any case somewhat bogus because they
582 * use information about what various FRAC_n variables look like
583 * internally [eg, that 2 word vars are X_f0 and x_f1]. But so do
584 * the ones in op-2.h and op-1.h.
586 #define _FP_FRAC_CONV_1_4(dfs, sfs, D, S) \
587 do { \
588 if (S##_c != FP_CLS_NAN) \
589 _FP_FRAC_SRS_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs), \
590 _FP_WFRACBITS_##sfs); \
591 else \
592 _FP_FRAC_SRL_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs)); \
593 D##_f = S##_f[0]; \
594 } while (0)
596 #define _FP_FRAC_CONV_2_4(dfs, sfs, D, S) \
597 do { \
598 if (S##_c != FP_CLS_NAN) \
599 _FP_FRAC_SRS_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs), \
600 _FP_WFRACBITS_##sfs); \
601 else \
602 _FP_FRAC_SRL_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs)); \
603 D##_f0 = S##_f[0]; \
604 D##_f1 = S##_f[1]; \
605 } while (0)
607 /* Assembly/disassembly for converting to/from integral types.
608 * No shifting or overflow handled here.
610 /* Put the FP value X into r, which is an integer of size rsize. */
611 #define _FP_FRAC_ASSEMBLE_4(r, X, rsize) \
612 do { \
613 if (rsize <= _FP_W_TYPE_SIZE) \
614 r = X##_f[0]; \
615 else if (rsize <= 2*_FP_W_TYPE_SIZE) \
617 r = X##_f[1]; \
618 r <<= _FP_W_TYPE_SIZE; \
619 r += X##_f[0]; \
621 else \
623 /* I'm feeling lazy so we deal with int == 3words (implausible)*/ \
624 /* and int == 4words as a single case. */ \
625 r = X##_f[3]; \
626 r <<= _FP_W_TYPE_SIZE; \
627 r += X##_f[2]; \
628 r <<= _FP_W_TYPE_SIZE; \
629 r += X##_f[1]; \
630 r <<= _FP_W_TYPE_SIZE; \
631 r += X##_f[0]; \
633 } while (0)
635 /* "No disassemble Number Five!" */
636 /* move an integer of size rsize into X's fractional part. We rely on
637 * the _f[] array consisting of words of size _FP_W_TYPE_SIZE to avoid
638 * having to mask the values we store into it.
640 #define _FP_FRAC_DISASSEMBLE_4(X, r, rsize) \
641 do { \
642 X##_f[0] = r; \
643 X##_f[1] = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE); \
644 X##_f[2] = (rsize <= 2*_FP_W_TYPE_SIZE ? 0 : r >> 2*_FP_W_TYPE_SIZE); \
645 X##_f[3] = (rsize <= 3*_FP_W_TYPE_SIZE ? 0 : r >> 3*_FP_W_TYPE_SIZE); \
646 } while (0);
648 #define _FP_FRAC_CONV_4_1(dfs, sfs, D, S) \
649 do { \
650 D##_f[0] = S##_f; \
651 D##_f[1] = D##_f[2] = D##_f[3] = 0; \
652 _FP_FRAC_SLL_4(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs)); \
653 } while (0)
655 #define _FP_FRAC_CONV_4_2(dfs, sfs, D, S) \
656 do { \
657 D##_f[0] = S##_f0; \
658 D##_f[1] = S##_f1; \
659 D##_f[2] = D##_f[3] = 0; \
660 _FP_FRAC_SLL_4(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs)); \
661 } while (0)