(_SC_IOV_MAX): New definition.
[kopensolaris-gnu/glibc.git] / soft-fp / op-4.h
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).
9
10    The GNU C Library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Library General Public License as
12    published by the Free Software Foundation; either version 2 of the
13    License, or (at your option) any later version.
14
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    Library General Public License for more details.
19
20    You should have received a copy of the GNU Library General Public
21    License along with the GNU C Library; see the file COPYING.LIB.  If
22    not, write to the Free Software Foundation, Inc.,
23    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
24
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])
33
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)
53
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)
74
75
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.
80  */
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;                                \
101       }                                                                 \
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)
107
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])
112
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])
117
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])
121
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)
124
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)
128
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
133 #define _FP_FRAC_EQ_4(X,Y)                              \
134  (X##_f[0] == Y##_f[0] && X##_f[1] == Y##_f[1]          \
135   && X##_f[2] == Y##_f[2] && X##_f[3] == Y##_f[3])
136
137 #define _FP_FRAC_GT_4(X,Y)                              \
138  (X##_f[3] > Y##_f[3] ||                                \
139   (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] ||      \
140    (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] ||     \
141     (X##_f[1] == Y##_f[1] && X##_f[0] > Y##_f[0])       \
142    ))                                                   \
143   ))                                                    \
144  )
145
146 #define _FP_FRAC_GE_4(X,Y)                              \
147  (X##_f[3] > Y##_f[3] ||                                \
148   (X##_f[3] == Y##_f[3] && (X##_f[2] > Y##_f[2] ||      \
149    (X##_f[2] == Y##_f[2] && (X##_f[1] > Y##_f[1] ||     \
150     (X##_f[1] == Y##_f[1] && X##_f[0] >= Y##_f[0])      \
151    ))                                                   \
152   ))                                                    \
153  )
154
155
156 #define _FP_FRAC_CLZ_4(R,X)             \
157   do {                                  \
158     if (X##_f[3])                       \
159     {                                   \
160         __FP_CLZ(R,X##_f[3]);           \
161     }                                   \
162     else if (X##_f[2])                  \
163     {                                   \
164         __FP_CLZ(R,X##_f[2]);           \
165         R += _FP_W_TYPE_SIZE;           \
166     }                                   \
167     else if (X##_f[1])                  \
168     {                                   \
169         __FP_CLZ(R,X##_f[2]);           \
170         R += _FP_W_TYPE_SIZE*2;         \
171     }                                   \
172     else                                \
173     {                                   \
174         __FP_CLZ(R,X##_f[0]);           \
175         R += _FP_W_TYPE_SIZE*3;         \
176     }                                   \
177   } while(0)
178
179
180 #define _FP_UNPACK_RAW_4(fs, X, val)                            \
181   do {                                                          \
182     union _FP_UNION_##fs _flo; _flo.flt = (val);                \
183     X##_f[0] = _flo.bits.frac0;                                 \
184     X##_f[1] = _flo.bits.frac1;                                 \
185     X##_f[2] = _flo.bits.frac2;                                 \
186     X##_f[3] = _flo.bits.frac3;                                 \
187     X##_e  = _flo.bits.exp;                                     \
188     X##_s  = _flo.bits.sign;                                    \
189   } while (0)
190
191 #define _FP_UNPACK_RAW_4_P(fs, X, val)                          \
192   do {                                                          \
193     union _FP_UNION_##fs *_flo =                                \
194       (union _FP_UNION_##fs *)(val);                            \
195                                                                 \
196     X##_f[0] = _flo->bits.frac0;                                \
197     X##_f[1] = _flo->bits.frac1;                                \
198     X##_f[2] = _flo->bits.frac2;                                \
199     X##_f[3] = _flo->bits.frac3;                                \
200     X##_e  = _flo->bits.exp;                                    \
201     X##_s  = _flo->bits.sign;                                   \
202   } while (0)
203
204 #define _FP_PACK_RAW_4(fs, val, X)                              \
205   do {                                                          \
206     union _FP_UNION_##fs _flo;                                  \
207     _flo.bits.frac0 = X##_f[0];                                 \
208     _flo.bits.frac1 = X##_f[1];                                 \
209     _flo.bits.frac2 = X##_f[2];                                 \
210     _flo.bits.frac3 = X##_f[3];                                 \
211     _flo.bits.exp   = X##_e;                                    \
212     _flo.bits.sign  = X##_s;                                    \
213     (val) = _flo.flt;                                           \
214   } while (0)
215
216 #define _FP_PACK_RAW_4_P(fs, val, X)                            \
217   do {                                                          \
218     union _FP_UNION_##fs *_flo =                                \
219       (union _FP_UNION_##fs *)(val);                            \
220                                                                 \
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;                                   \
227   } while (0)
228
229 /*
230  * Multiplication algorithms:
231  */
232
233 /* Given a 1W * 1W => 2W primitive, do the extended multiplication.  */
234
235 #define _FP_MUL_MEAT_4_wide(wfracbits, R, X, Y, doit)                       \
236   do {                                                                      \
237     _FP_FRAC_DECL_8(_z); _FP_FRAC_DECL_2(_b); _FP_FRAC_DECL_2(_c);          \
238     _FP_FRAC_DECL_2(_d); _FP_FRAC_DECL_2(_e); _FP_FRAC_DECL_2(_f);          \
239                                                                             \
240     doit(_FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0), X##_f[0], Y##_f[0]); \
241     doit(_b_f1, _b_f0, X##_f[0], Y##_f[1]);                                 \
242     doit(_c_f1, _c_f0, X##_f[1], Y##_f[0]);                                 \
243     doit(_d_f1, _d_f0, X##_f[1], Y##_f[1]);                                 \
244     doit(_e_f1, _e_f0, X##_f[0], Y##_f[2]);                                 \
245     doit(_f_f1, _f_f0, X##_f[2], Y##_f[0]);                                 \
246     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2),            \
247                     _FP_FRAC_WORD_8(_z,1), 0,_b_f1,_b_f0,                   \
248                     0,0,_FP_FRAC_WORD_8(_z,1));                             \
249     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2),            \
250                     _FP_FRAC_WORD_8(_z,1), 0,_c_f1,_c_f0,                   \
251                     _FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2),            \
252                     _FP_FRAC_WORD_8(_z,1));                                 \
253     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3),            \
254                     _FP_FRAC_WORD_8(_z,2), 0,_d_f1,_d_f0,                   \
255                     0,_FP_FRAC_WORD_8(_z,3),_FP_FRAC_WORD_8(_z,2));         \
256     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3),            \
257                     _FP_FRAC_WORD_8(_z,2), 0,_e_f1,_e_f0,                   \
258                     _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3),            \
259                     _FP_FRAC_WORD_8(_z,2));                                 \
260     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3),            \
261                     _FP_FRAC_WORD_8(_z,2), 0,_f_f1,_f_f0,                   \
262                     _FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3),            \
263                     _FP_FRAC_WORD_8(_z,2));                                 \
264     doit(_b_f1, _b_f0, X##_f[0], Y##_f[3]);                                 \
265     doit(_c_f1, _c_f0, X##_f[3], Y##_f[0]);                                 \
266     doit(_d_f1, _d_f0, X##_f[1], Y##_f[2]);                                 \
267     doit(_e_f1, _e_f0, X##_f[2], Y##_f[1]);                                 \
268     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4),            \
269                     _FP_FRAC_WORD_8(_z,3), 0,_b_f1,_b_f0,                   \
270                     0,_FP_FRAC_WORD_8(_z,4),_FP_FRAC_WORD_8(_z,3));         \
271     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4),            \
272                     _FP_FRAC_WORD_8(_z,3), 0,_c_f1,_c_f0,                   \
273                     _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4),            \
274                     _FP_FRAC_WORD_8(_z,3));                                 \
275     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4),            \
276                     _FP_FRAC_WORD_8(_z,3), 0,_d_f1,_d_f0,                   \
277                     _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4),            \
278                     _FP_FRAC_WORD_8(_z,3));                                 \
279     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4),            \
280                     _FP_FRAC_WORD_8(_z,3), 0,_e_f1,_e_f0,                   \
281                     _FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4),            \
282                     _FP_FRAC_WORD_8(_z,3));                                 \
283     doit(_b_f1, _b_f0, X##_f[2], Y##_f[2]);                                 \
284     doit(_c_f1, _c_f0, X##_f[1], Y##_f[3]);                                 \
285     doit(_d_f1, _d_f0, X##_f[3], Y##_f[1]);                                 \
286     doit(_e_f1, _e_f0, X##_f[2], Y##_f[3]);                                 \
287     doit(_f_f1, _f_f0, X##_f[3], Y##_f[2]);                                 \
288     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5),            \
289                     _FP_FRAC_WORD_8(_z,4), 0,_b_f1,_b_f0,                   \
290                     0,_FP_FRAC_WORD_8(_z,5),_FP_FRAC_WORD_8(_z,4));         \
291     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5),            \
292                     _FP_FRAC_WORD_8(_z,4), 0,_c_f1,_c_f0,                   \
293                     _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5),            \
294                     _FP_FRAC_WORD_8(_z,4));                                 \
295     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5),            \
296                     _FP_FRAC_WORD_8(_z,4), 0,_d_f1,_d_f0,                   \
297                     _FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5),            \
298                     _FP_FRAC_WORD_8(_z,4));                                 \
299     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6),            \
300                     _FP_FRAC_WORD_8(_z,5), 0,_e_f1,_e_f0,                   \
301                     0,_FP_FRAC_WORD_8(_z,6),_FP_FRAC_WORD_8(_z,5));         \
302     __FP_FRAC_ADD_3(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6),            \
303                     _FP_FRAC_WORD_8(_z,5), 0,_f_f1,_f_f0,                   \
304                     _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6),            \
305                     _FP_FRAC_WORD_8(_z,5));                                 \
306     doit(_b_f1, _b_f0, X##_f[3], Y##_f[3]);                                 \
307     __FP_FRAC_ADD_2(_FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6),            \
308                     _b_f1,_b_f0,                                            \
309                     _FP_FRAC_WORD_8(_z,7),_FP_FRAC_WORD_8(_z,6));           \
310                                                                             \
311     /* Normalize since we know where the msb of the multiplicands           \
312        were (bit B), we know that the msb of the of the product is          \
313        at either 2B or 2B-1.  */                                            \
314     _FP_FRAC_SRS_8(_z, wfracbits-1, 2*wfracbits);                           \
315     __FP_FRAC_SET_4(R, _FP_FRAC_WORD_8(_z,3), _FP_FRAC_WORD_8(_z,2),        \
316                     _FP_FRAC_WORD_8(_z,1), _FP_FRAC_WORD_8(_z,0));          \
317   } while (0)
318
319 #define _FP_MUL_MEAT_4_gmp(wfracbits, R, X, Y)                              \
320   do {                                                                      \
321     _FP_FRAC_DECL_8(_z);                                                    \
322                                                                             \
323     mpn_mul_n(_z_f, _x_f, _y_f, 4);                                         \
324                                                                             \
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));          \
331   } while (0)
332
333 /*
334  * Helper utility for _FP_DIV_MEAT_4_udiv:
335  * pppp = m * nnn
336  */
337 #define umul_ppppmnnn(p3,p2,p1,p0,m,n2,n1,n0)                               \
338   do {                                                                      \
339     UWtype _t;                                                              \
340     umul_ppmm(p1,p0,m,n0);                                                  \
341     umul_ppmm(p2,_t,m,n1);                                                  \
342     __FP_FRAC_ADDI_2(p2,p1,_t);                                             \
343     umul_ppmm(p3,_t,m,n2);                                                  \
344     __FP_FRAC_ADDI_2(p3,p2,_t);                                             \
345   } while (0)
346
347 /*
348  * Division algorithms:
349  */
350
351 #define _FP_DIV_MEAT_4_udiv(fs, R, X, Y)                                    \
352   do {                                                                      \
353     int _i;                                                                 \
354     _FP_FRAC_DECL_4(_n); _FP_FRAC_DECL_4(_m);                               \
355     _FP_FRAC_SET_4(_n, _FP_ZEROFRAC_4);                                     \
356     if (_FP_FRAC_GT_4(X, Y))                                                \
357       {                                                                     \
358         _n_f[3] = X##_f[0] << (_FP_W_TYPE_SIZE - 1);                        \
359         _FP_FRAC_SRL_4(X, 1);                                               \
360       }                                                                     \
361     else                                                                    \
362       R##_e--;                                                              \
363                                                                             \
364     /* Normalize, i.e. make the most significant bit of the                 \
365        denominator set. */                                                  \
366     _FP_FRAC_SLL_4(Y, _FP_WFRACXBITS_##fs);                                 \
367                                                                             \
368     for (_i = 3; ; _i--)                                                    \
369       {                                                                     \
370         if (X##_f[3] == Y##_f[3])                                           \
371           {                                                                 \
372             /* This is a special case, not an optimization                  \
373                (X##_f[3]/Y##_f[3] would not fit into UWtype).               \
374                As X## is guaranteed to be < Y,  R##_f[_i] can be either     \
375                (UWtype)-1 or (UWtype)-2.  */                                \
376             R##_f[_i] = -1;                                                 \
377             if (!_i)                                                        \
378               break;                                                        \
379             __FP_FRAC_SUB_4(X##_f[3], X##_f[2], X##_f[1], X##_f[0],         \
380                             Y##_f[2], Y##_f[1], Y##_f[0], 0,                \
381                             X##_f[2], X##_f[1], X##_f[0], _n_f[_i]);        \
382             _FP_FRAC_SUB_4(X, Y, X);                                        \
383             if (X##_f[3] > Y##_f[3])                                        \
384               {                                                             \
385                 R##_f[_i] = -2;                                             \
386                 _FP_FRAC_ADD_4(X, Y, X);                                    \
387               }                                                             \
388           }                                                                 \
389         else                                                                \
390           {                                                                 \
391             udiv_qrnnd(R##_f[_i], X##_f[3], X##_f[3], X##_f[2], Y##_f[3]);  \
392             umul_ppppmnnn(_m_f[3], _m_f[2], _m_f[1], _m_f[0],               \
393                           R##_f[_i], Y##_f[2], Y##_f[1], Y##_f[0]);         \
394             X##_f[2] = X##_f[1];                                            \
395             X##_f[1] = X##_f[0];                                            \
396             X##_f[0] = _n_f[_i];                                            \
397             if (_FP_FRAC_GT_4(_m, X))                                       \
398               {                                                             \
399                 R##_f[_i]--;                                                \
400                 _FP_FRAC_ADD_4(X, Y, X);                                    \
401                 if (_FP_FRAC_GE_4(X, Y) && _FP_FRAC_GT_4(_m, X))            \
402                   {                                                         \
403                     R##_f[_i]--;                                            \
404                     _FP_FRAC_ADD_4(X, Y, X);                                \
405                   }                                                         \
406               }                                                             \
407             _FP_FRAC_DEC_4(X, _m);                                          \
408             if (!_i)                                                        \
409               {                                                             \
410                 if (!_FP_FRAC_EQ_4(X, _m))                                  \
411                   R##_f[0] |= _FP_WORK_STICKY;                              \
412                 break;                                                      \
413               }                                                             \
414           }                                                                 \
415       }                                                                     \
416   } while (0)
417
418
419 /*
420  * Square root algorithms:
421  * We have just one right now, maybe Newton approximation
422  * should be added for those machines where division is fast.
423  */
424  
425 #define _FP_SQRT_MEAT_4(R, S, T, X, q)                          \
426   do {                                                          \
427     while (q)                                                   \
428       {                                                         \
429         T##_f[3] = S##_f[3] + q;                                \
430         if (T##_f[3] <= X##_f[3])                               \
431           {                                                     \
432             S##_f[3] = T##_f[3] + q;                            \
433             X##_f[3] -= T##_f[3];                               \
434             R##_f[3] += q;                                      \
435           }                                                     \
436         _FP_FRAC_SLL_4(X, 1);                                   \
437         q >>= 1;                                                \
438       }                                                         \
439     q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1);                 \
440     while (q)                                                   \
441       {                                                         \
442         T##_f[2] = S##_f[2] + q;                                \
443         T##_f[3] = S##_f[3];                                    \
444         if (T##_f[3] < X##_f[3] ||                              \
445             (T##_f[3] == X##_f[3] && T##_f[2] <= X##_f[2]))     \
446           {                                                     \
447             S##_f[2] = T##_f[2] + q;                            \
448             S##_f[3] += (T##_f[2] > S##_f[2]);                  \
449             __FP_FRAC_DEC_2(X##_f[3], X##_f[2],                 \
450                             T##_f[3], T##_f[2]);                \
451             R##_f[2] += q;                                      \
452           }                                                     \
453         _FP_FRAC_SLL_4(X, 1);                                   \
454         q >>= 1;                                                \
455       }                                                         \
456     q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1);                 \
457     while (q)                                                   \
458       {                                                         \
459         T##_f[1] = S##_f[1] + q;                                \
460         T##_f[2] = S##_f[2];                                    \
461         T##_f[3] = S##_f[3];                                    \
462         if (T##_f[3] < X##_f[3] ||                              \
463             (T##_f[3] == X##_f[3] && (T##_f[2] < X##_f[2] ||    \
464              (T##_f[2] == X##_f[2] && T##_f[1] <= X##_f[1]))))  \
465           {                                                     \
466             S##_f[1] = T##_f[1] + q;                            \
467             S##_f[2] += (T##_f[1] > S##_f[1]);                  \
468             S##_f[3] += (T##_f[2] > S##_f[2]);                  \
469             __FP_FRAC_DEC_3(X##_f[3], X##_f[2], X##_f[1],       \
470                             T##_f[3], T##_f[2], T##_f[1]);      \
471             R##_f[1] += q;                                      \
472           }                                                     \
473         _FP_FRAC_SLL_4(X, 1);                                   \
474         q >>= 1;                                                \
475       }                                                         \
476     q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1);                 \
477     while (q != _FP_WORK_ROUND)                                 \
478       {                                                         \
479         T##_f[0] = S##_f[0] + q;                                \
480         T##_f[1] = S##_f[1];                                    \
481         T##_f[2] = S##_f[2];                                    \
482         T##_f[3] = S##_f[3];                                    \
483         if (_FP_FRAC_GE_4(X,T))                                 \
484           {                                                     \
485             S##_f[0] = T##_f[0] + q;                            \
486             S##_f[1] += (T##_f[0] > S##_f[0]);                  \
487             S##_f[2] += (T##_f[1] > S##_f[1]);                  \
488             S##_f[3] += (T##_f[2] > S##_f[2]);                  \
489             _FP_FRAC_DEC_4(X, T);                               \
490             R##_f[0] += q;                                      \
491           }                                                     \
492         _FP_FRAC_SLL_4(X, 1);                                   \
493         q >>= 1;                                                \
494       }                                                         \
495     if (!_FP_FRAC_ZEROP_4(X))                                   \
496       {                                                         \
497         if (_FP_FRAC_GT_4(X,S))                                 \
498           R##_f[0] |= _FP_WORK_ROUND;                           \
499         R##_f[0] |= _FP_WORK_STICKY;                            \
500       }                                                         \
501   } while (0)
502
503
504 /*
505  * Internals 
506  */
507
508 #define __FP_FRAC_SET_4(X,I3,I2,I1,I0)                                  \
509   (X##_f[3] = I3, X##_f[2] = I2, X##_f[1] = I1, X##_f[0] = I0)
510
511 #ifndef __FP_FRAC_ADD_3
512 #define __FP_FRAC_ADD_3(r2,r1,r0,x2,x1,x0,y2,y1,y0)                     \
513   (r0 = x0 + y0,                                                        \
514    r1 = x1 + y1 + (r0 < x0),                                            \
515    r2 = x2 + y2 + (r1 < x1))
516 #endif
517
518 #ifndef __FP_FRAC_ADD_4
519 #define __FP_FRAC_ADD_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0)            \
520   (r0 = x0 + y0,                                                        \
521    r1 = x1 + y1 + (r0 < x0),                                            \
522    r2 = x2 + y2 + (r1 < x1),                                            \
523    r3 = x3 + y3 + (r2 < x2))
524 #endif
525
526 #ifndef __FP_FRAC_SUB_3
527 #define __FP_FRAC_SUB_3(r2,r1,r0,x2,x1,x0,y2,y1,y0)                     \
528   (r0 = x0 - y0,                                                        \
529    r1 = x1 - y1 - (r0 > x0),                                            \
530    r2 = x2 - y2 - (r1 > x1))
531 #endif
532
533 #ifndef __FP_FRAC_SUB_4
534 #define __FP_FRAC_SUB_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0)            \
535   (r0 = x0 - y0,                                                        \
536    r1 = x1 - y1 - (r0 > x0),                                            \
537    r2 = x2 - y2 - (r1 > x1),                                            \
538    r3 = x3 - y3 - (r2 > x2))
539 #endif
540
541 #ifndef __FP_FRAC_DEC_3
542 #define __FP_FRAC_DEC_3(x2,x1,x0,y2,y1,y0)                              \
543   do {                                                                  \
544     UWtype _t0, _t1;                                                    \
545     _t0 = x0;                                                           \
546     x0 -= y0;                                                           \
547     _t1 = x1;                                                           \
548     x1 -= y1 + (x0 > _t0);                                              \
549     x2 -= y2 + (x1 > _t1);                                              \
550   } while (0)
551 #endif
552
553 #ifndef __FP_FRAC_DEC_4
554 #define __FP_FRAC_DEC_4(x3,x2,x1,x0,y3,y2,y1,y0)                        \
555   do {                                                                  \
556     UWtype _t0, _t1;                                                    \
557     _t0 = x0;                                                           \
558     x0 -= y0;                                                           \
559     _t1 = x1;                                                           \
560     x1 -= y1 + (x0 > _t0);                                              \
561     _t0 = x2;                                                           \
562     x2 -= y2 + (x1 > _t1);                                              \
563     x3 -= y3 + (x2 > _t0);                                              \
564   } while (0)
565 #endif
566
567 #ifndef __FP_FRAC_ADDI_4
568 #define __FP_FRAC_ADDI_4(x3,x2,x1,x0,i)                                 \
569   do {                                                                  \
570     UWtype _t;                                                          \
571     _t = ((x0 += i) < i);                                               \
572     x1 += _t; _t = (x1 < _t);                                           \
573     x2 += _t; _t = (x2 < _t);                                           \
574     x3 += _t;                                                           \
575   } while (0)
576 #endif
577
578 /* Convert FP values between word sizes. This appears to be more
579  * complicated than I'd have expected it to be, so these might be
580  * wrong... These macros are in any case somewhat bogus because they
581  * use information about what various FRAC_n variables look like 
582  * internally [eg, that 2 word vars are X_f0 and x_f1]. But so do
583  * the ones in op-2.h and op-1.h. 
584  */
585 #define _FP_FRAC_CONV_1_4(dfs, sfs, D, S)                               \
586    do {                                                                 \
587      if (S##_c != FP_CLS_NAN)                                           \
588        _FP_FRAC_SRS_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs),   \
589                           _FP_WFRACBITS_##sfs);                         \
590      else                                                               \
591        _FP_FRAC_SRL_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs));  \
592      D##_f = S##_f[0];                                                  \
593   } while (0)
594
595 #define _FP_FRAC_CONV_2_4(dfs, sfs, D, S)                               \
596    do {                                                                 \
597      if (S##_c != FP_CLS_NAN)                                           \
598        _FP_FRAC_SRS_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs),   \
599                       _FP_WFRACBITS_##sfs);                             \
600      else                                                               \
601        _FP_FRAC_SRL_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs));  \
602      D##_f0 = S##_f[0];                                                 \
603      D##_f1 = S##_f[1];                                                 \
604   } while (0)
605
606 /* Assembly/disassembly for converting to/from integral types.  
607  * No shifting or overflow handled here.
608  */
609 /* Put the FP value X into r, which is an integer of size rsize. */
610 #define _FP_FRAC_ASSEMBLE_4(r, X, rsize)                                \
611   do {                                                                  \
612     if (rsize <= _FP_W_TYPE_SIZE)                                       \
613       r = X##_f[0];                                                     \
614     else if (rsize <= 2*_FP_W_TYPE_SIZE)                                \
615     {                                                                   \
616       r = X##_f[1];                                                     \
617       r <<= _FP_W_TYPE_SIZE;                                            \
618       r += X##_f[0];                                                    \
619     }                                                                   \
620     else                                                                \
621     {                                                                   \
622       /* I'm feeling lazy so we deal with int == 3words (implausible)*/ \
623       /* and int == 4words as a single case.                     */     \
624       r = X##_f[3];                                                     \
625       r <<= _FP_W_TYPE_SIZE;                                            \
626       r += X##_f[2];                                                    \
627       r <<= _FP_W_TYPE_SIZE;                                            \
628       r += X##_f[1];                                                    \
629       r <<= _FP_W_TYPE_SIZE;                                            \
630       r += X##_f[0];                                                    \
631     }                                                                   \
632   } while (0)
633
634 /* "No disassemble Number Five!" */
635 /* move an integer of size rsize into X's fractional part. We rely on
636  * the _f[] array consisting of words of size _FP_W_TYPE_SIZE to avoid
637  * having to mask the values we store into it.
638  */
639 #define _FP_FRAC_DISASSEMBLE_4(X, r, rsize)                             \
640   do {                                                                  \
641     X##_f[0] = r;                                                       \
642     X##_f[1] = (rsize <= _FP_W_TYPE_SIZE ? 0 : r >> _FP_W_TYPE_SIZE);   \
643     X##_f[2] = (rsize <= 2*_FP_W_TYPE_SIZE ? 0 : r >> 2*_FP_W_TYPE_SIZE); \
644     X##_f[3] = (rsize <= 3*_FP_W_TYPE_SIZE ? 0 : r >> 3*_FP_W_TYPE_SIZE); \
645   } while (0);
646
647 #define _FP_FRAC_CONV_4_1(dfs, sfs, D, S)                               \
648    do {                                                                 \
649      D##_f[0] = S##_f;                                                  \
650      D##_f[1] = D##_f[2] = D##_f[3] = 0;                                \
651      _FP_FRAC_SLL_4(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs));    \
652    } while (0)
653
654 #define _FP_FRAC_CONV_4_2(dfs, sfs, D, S)                               \
655    do {                                                                 \
656      D##_f[0] = S##_f0;                                                 \
657      D##_f[1] = S##_f1;                                                 \
658      D##_f[2] = D##_f[3] = 0;                                           \
659      _FP_FRAC_SLL_4(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs));    \
660    } while (0)
661