* soft-fp/soft-fp.h (FP_EX_UNDERFLOW): Define to 0.
[platform/upstream/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 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.
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    Lesser General Public License for more details.
19
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.  */
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 #define _FP_FRAC_CLEAR_OVERP_4(fs,X)  (_FP_FRAC_HIGH_##fs(X) &= ~_FP_OVERFLOW_##fs)
133
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])
137
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   ))                                                    \
145  )
146
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   ))                                                    \
154  )
155
156
157 #define _FP_FRAC_CLZ_4(R,X)             \
158   do {                                  \
159     if (X##_f[3])                       \
160     {                                   \
161         __FP_CLZ(R,X##_f[3]);           \
162     }                                   \
163     else if (X##_f[2])                  \
164     {                                   \
165         __FP_CLZ(R,X##_f[2]);           \
166         R += _FP_W_TYPE_SIZE;           \
167     }                                   \
168     else if (X##_f[1])                  \
169     {                                   \
170         __FP_CLZ(R,X##_f[1]);           \
171         R += _FP_W_TYPE_SIZE*2;         \
172     }                                   \
173     else                                \
174     {                                   \
175         __FP_CLZ(R,X##_f[0]);           \
176         R += _FP_W_TYPE_SIZE*3;         \
177     }                                   \
178   } while(0)
179
180
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)
191
192 #define _FP_UNPACK_RAW_4_P(fs, X, val)                          \
193   do {                                                          \
194     union _FP_UNION_##fs *_flo =                                \
195       (union _FP_UNION_##fs *)(val);                            \
196                                                                 \
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)
204
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)
216
217 #define _FP_PACK_RAW_4_P(fs, val, X)                            \
218   do {                                                          \
219     union _FP_UNION_##fs *_flo =                                \
220       (union _FP_UNION_##fs *)(val);                            \
221                                                                 \
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)
229
230 /*
231  * Multiplication algorithms:
232  */
233
234 /* Given a 1W * 1W => 2W primitive, do the extended multiplication.  */
235
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);          \
240                                                                             \
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));           \
311                                                                             \
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)
319
320 #define _FP_MUL_MEAT_4_gmp(wfracbits, R, X, Y)                              \
321   do {                                                                      \
322     _FP_FRAC_DECL_8(_z);                                                    \
323                                                                             \
324     mpn_mul_n(_z_f, _x_f, _y_f, 4);                                         \
325                                                                             \
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)
333
334 /*
335  * Helper utility for _FP_DIV_MEAT_4_udiv:
336  * pppp = m * nnn
337  */
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)
347
348 /*
349  * Division algorithms:
350  */
351
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))                                                \
358       {                                                                     \
359         _n_f[3] = X##_f[0] << (_FP_W_TYPE_SIZE - 1);                        \
360         _FP_FRAC_SRL_4(X, 1);                                               \
361       }                                                                     \
362     else                                                                    \
363       R##_e--;                                                              \
364                                                                             \
365     /* Normalize, i.e. make the most significant bit of the                 \
366        denominator set. */                                                  \
367     _FP_FRAC_SLL_4(Y, _FP_WFRACXBITS_##fs);                                 \
368                                                                             \
369     for (_i = 3; ; _i--)                                                    \
370       {                                                                     \
371         if (X##_f[3] == Y##_f[3])                                           \
372           {                                                                 \
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])                                        \
385               {                                                             \
386                 R##_f[_i] = -2;                                             \
387                 _FP_FRAC_ADD_4(X, Y, X);                                    \
388               }                                                             \
389           }                                                                 \
390         else                                                                \
391           {                                                                 \
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))                                       \
399               {                                                             \
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))            \
403                   {                                                         \
404                     R##_f[_i]--;                                            \
405                     _FP_FRAC_ADD_4(X, Y, X);                                \
406                   }                                                         \
407               }                                                             \
408             _FP_FRAC_DEC_4(X, _m);                                          \
409             if (!_i)                                                        \
410               {                                                             \
411                 if (!_FP_FRAC_EQ_4(X, _m))                                  \
412                   R##_f[0] |= _FP_WORK_STICKY;                              \
413                 break;                                                      \
414               }                                                             \
415           }                                                                 \
416       }                                                                     \
417   } while (0)
418
419
420 /*
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.
424  */
425  
426 #define _FP_SQRT_MEAT_4(R, S, T, X, q)                          \
427   do {                                                          \
428     while (q)                                                   \
429       {                                                         \
430         T##_f[3] = S##_f[3] + q;                                \
431         if (T##_f[3] <= X##_f[3])                               \
432           {                                                     \
433             S##_f[3] = T##_f[3] + q;                            \
434             X##_f[3] -= T##_f[3];                               \
435             R##_f[3] += q;                                      \
436           }                                                     \
437         _FP_FRAC_SLL_4(X, 1);                                   \
438         q >>= 1;                                                \
439       }                                                         \
440     q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1);                 \
441     while (q)                                                   \
442       {                                                         \
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]))     \
447           {                                                     \
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;                                      \
453           }                                                     \
454         _FP_FRAC_SLL_4(X, 1);                                   \
455         q >>= 1;                                                \
456       }                                                         \
457     q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1);                 \
458     while (q)                                                   \
459       {                                                         \
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]))))  \
466           {                                                     \
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;                                      \
473           }                                                     \
474         _FP_FRAC_SLL_4(X, 1);                                   \
475         q >>= 1;                                                \
476       }                                                         \
477     q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1);                 \
478     while (q != _FP_WORK_ROUND)                                 \
479       {                                                         \
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))                                 \
485           {                                                     \
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;                                      \
492           }                                                     \
493         _FP_FRAC_SLL_4(X, 1);                                   \
494         q >>= 1;                                                \
495       }                                                         \
496     if (!_FP_FRAC_ZEROP_4(X))                                   \
497       {                                                         \
498         if (_FP_FRAC_GT_4(X,S))                                 \
499           R##_f[0] |= _FP_WORK_ROUND;                           \
500         R##_f[0] |= _FP_WORK_STICKY;                            \
501       }                                                         \
502   } while (0)
503
504
505 /*
506  * Internals 
507  */
508
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)
511
512 #ifndef __FP_FRAC_ADD_3
513 #define __FP_FRAC_ADD_3(r2,r1,r0,x2,x1,x0,y2,y1,y0)             \
514   do {                                                          \
515     int _c1, _c2;                                                       \
516     r0 = x0 + y0;                                               \
517     _c1 = r0 < x0;                                              \
518     r1 = x1 + y1;                                               \
519     _c2 = r1 < x1;                                              \
520     r1 += _c1;                                                  \
521     _c2 |= r1 < _c1;                                            \
522     r2 = x2 + y2 + _c2;                                         \
523   } while (0)
524 #endif
525
526 #ifndef __FP_FRAC_ADD_4
527 #define __FP_FRAC_ADD_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0)    \
528   do {                                                          \
529     int _c1, _c2, _c3;                                          \
530     r0 = x0 + y0;                                               \
531     _c1 = r0 < x0;                                              \
532     r1 = x1 + y1;                                               \
533     _c2 = r1 < x1;                                              \
534     r1 += _c1;                                                  \
535     _c2 |= r1 < _c1;                                            \
536     r2 = x2 + y2;                                               \
537     _c3 = r2 < x2;                                              \
538     r2 += _c2;                                                  \
539     _c3 |= r2 < _c2;                                            \
540     r3 = x3 + y3 + _c3;                                         \
541   } while (0)
542 #endif
543
544 #ifndef __FP_FRAC_SUB_3
545 #define __FP_FRAC_SUB_3(r2,r1,r0,x2,x1,x0,y2,y1,y0)             \
546   do {                                                          \
547     int _c1, _c2;                                                       \
548     r0 = x0 - y0;                                               \
549     _c1 = r0 > x0;                                              \
550     r1 = x1 - y1;                                               \
551     _c2 = r1 > x1;                                              \
552     r1 -= _c1;                                                  \
553     _c2 |= r1 > _c1;                                            \
554     r2 = x2 - y2 - _c2;                                         \
555   } while (0)
556 #endif
557
558 #ifndef __FP_FRAC_SUB_4
559 #define __FP_FRAC_SUB_4(r3,r2,r1,r0,x3,x2,x1,x0,y3,y2,y1,y0)    \
560   do {                                                          \
561     int _c1, _c2, _c3;                                          \
562     r0 = x0 - y0;                                               \
563     _c1 = r0 > x0;                                              \
564     r1 = x1 - y1;                                               \
565     _c2 = r1 > x1;                                              \
566     r1 -= _c1;                                                  \
567     _c2 |= r1 > _c1;                                            \
568     r2 = x2 - y2;                                               \
569     _c3 = r2 > x2;                                              \
570     r2 -= _c2;                                                  \
571     _c3 |= r2 > _c2;                                            \
572     r3 = x3 - y3 - _c3;                                         \
573   } while (0)
574 #endif
575
576 #ifndef __FP_FRAC_DEC_3
577 #define __FP_FRAC_DEC_3(x2,x1,x0,y2,y1,y0)                              \
578   do {                                                                  \
579     UWtype _t0, _t1, _t2;                                               \
580     _t0 = x0, _t1 = x1, _t2 = x2;                                       \
581     __FP_FRAC_SUB_3 (x2, x1, x0, _t2, _t1, _t0, y2, y1, y0);            \
582   } while (0)
583 #endif
584
585 #ifndef __FP_FRAC_DEC_4
586 #define __FP_FRAC_DEC_4(x3,x2,x1,x0,y3,y2,y1,y0)                        \
587   do {                                                                  \
588     UWtype _t0, _t1, _t2, _t3;                                          \
589     _t0 = x0, _t1 = x1, _t2 = x2, _t3 = x3;                             \
590     __FP_FRAC_SUB_4 (x3,x2,x1,x0,_t3,_t2,_t1,_t0, y3,y2,y1,y0);         \
591   } while (0)
592 #endif
593
594 #ifndef __FP_FRAC_ADDI_4
595 #define __FP_FRAC_ADDI_4(x3,x2,x1,x0,i)                                 \
596   do {                                                                  \
597     UWtype _t;                                                          \
598     _t = ((x0 += i) < i);                                               \
599     x1 += _t; _t = (x1 < _t);                                           \
600     x2 += _t; _t = (x2 < _t);                                           \
601     x3 += _t;                                                           \
602   } while (0)
603 #endif
604
605 /* Convert FP values between word sizes. This appears to be more
606  * complicated than I'd have expected it to be, so these might be
607  * wrong... These macros are in any case somewhat bogus because they
608  * use information about what various FRAC_n variables look like 
609  * internally [eg, that 2 word vars are X_f0 and x_f1]. But so do
610  * the ones in op-2.h and op-1.h. 
611  */
612 #define _FP_FRAC_CONV_1_4(dfs, sfs, D, S)                               \
613    do {                                                                 \
614      if (S##_c != FP_CLS_NAN)                                           \
615        _FP_FRAC_SRS_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs),   \
616                           _FP_WFRACBITS_##sfs);                         \
617      else                                                               \
618        _FP_FRAC_SRL_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs));  \
619      D##_f = S##_f[0];                                                  \
620   } while (0)
621
622 #define _FP_FRAC_CONV_2_4(dfs, sfs, D, S)                               \
623    do {                                                                 \
624      if (S##_c != FP_CLS_NAN)                                           \
625        _FP_FRAC_SRS_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs),   \
626                       _FP_WFRACBITS_##sfs);                             \
627      else                                                               \
628        _FP_FRAC_SRL_4(S, (_FP_WFRACBITS_##sfs - _FP_WFRACBITS_##dfs));  \
629      D##_f0 = S##_f[0];                                                 \
630      D##_f1 = S##_f[1];                                                 \
631   } while (0)
632
633 /* Assembly/disassembly for converting to/from integral types.  
634  * No shifting or overflow handled here.
635  */
636 /* Put the FP value X into r, which is an integer of size rsize. */
637 #define _FP_FRAC_ASSEMBLE_4(r, X, rsize)                                \
638   do {                                                                  \
639     if (rsize <= _FP_W_TYPE_SIZE)                                       \
640       r = X##_f[0];                                                     \
641     else if (rsize <= 2*_FP_W_TYPE_SIZE)                                \
642     {                                                                   \
643       r = X##_f[1];                                                     \
644       r <<= _FP_W_TYPE_SIZE;                                            \
645       r += X##_f[0];                                                    \
646     }                                                                   \
647     else                                                                \
648     {                                                                   \
649       /* I'm feeling lazy so we deal with int == 3words (implausible)*/ \
650       /* and int == 4words as a single case.                     */     \
651       r = X##_f[3];                                                     \
652       r <<= _FP_W_TYPE_SIZE;                                            \
653       r += X##_f[2];                                                    \
654       r <<= _FP_W_TYPE_SIZE;                                            \
655       r += X##_f[1];                                                    \
656       r <<= _FP_W_TYPE_SIZE;                                            \
657       r += X##_f[0];                                                    \
658     }                                                                   \
659   } while (0)
660
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.
665  */
666 #define _FP_FRAC_DISASSEMBLE_4(X, r, rsize)                             \
667   do {                                                                  \
668     X##_f[0] = r;                                                       \
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); \
672   } while (0);
673
674 #define _FP_FRAC_CONV_4_1(dfs, sfs, D, S)                               \
675    do {                                                                 \
676      D##_f[0] = S##_f;                                                  \
677      D##_f[1] = D##_f[2] = D##_f[3] = 0;                                \
678      _FP_FRAC_SLL_4(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs));    \
679    } while (0)
680
681 #define _FP_FRAC_CONV_4_2(dfs, sfs, D, S)                               \
682    do {                                                                 \
683      D##_f[0] = S##_f0;                                                 \
684      D##_f[1] = S##_f1;                                                 \
685      D##_f[2] = D##_f[3] = 0;                                           \
686      _FP_FRAC_SLL_4(D, (_FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs));    \
687    } while (0)
688