(_FP_FRAC_CLEAR_OVERP_1): Define.
[kopensolaris-gnu/glibc.git] / soft-fp / op-1.h
1 /* Software floating-point emulation.
2    Basic one-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_1(X)      _FP_W_TYPE X##_f
26 #define _FP_FRAC_COPY_1(D,S)    (D##_f = S##_f)
27 #define _FP_FRAC_SET_1(X,I)     (X##_f = I)
28 #define _FP_FRAC_HIGH_1(X)      (X##_f)
29 #define _FP_FRAC_LOW_1(X)       (X##_f)
30 #define _FP_FRAC_WORD_1(X,w)    (X##_f)
31
32 #define _FP_FRAC_ADDI_1(X,I)    (X##_f += I)
33 #define _FP_FRAC_SLL_1(X,N)                     \
34   do {                                          \
35     if (__builtin_constant_p(N) && (N) == 1)    \
36       X##_f += X##_f;                           \
37     else                                        \
38       X##_f <<= (N);                            \
39   } while (0)
40 #define _FP_FRAC_SRL_1(X,N)     (X##_f >>= N)
41
42 /* Right shift with sticky-lsb.  */
43 #define _FP_FRAC_SRS_1(X,N,sz)  __FP_FRAC_SRS_1(X##_f, N, sz)
44
45 #define __FP_FRAC_SRS_1(X,N,sz)                                         \
46    (X = (X >> (N) | (__builtin_constant_p(N) && (N) == 1                \
47                      ? X & 1 : (X << (_FP_W_TYPE_SIZE - (N))) != 0)))
48
49 #define _FP_FRAC_ADD_1(R,X,Y)   (R##_f = X##_f + Y##_f)
50 #define _FP_FRAC_SUB_1(R,X,Y)   (R##_f = X##_f - Y##_f)
51 #define _FP_FRAC_DEC_1(X,Y)     (X##_f -= Y##_f)
52 #define _FP_FRAC_CLZ_1(z, X)    __FP_CLZ(z, X##_f)
53
54 /* Predicates */
55 #define _FP_FRAC_NEGP_1(X)      ((_FP_WS_TYPE)X##_f < 0)
56 #define _FP_FRAC_ZEROP_1(X)     (X##_f == 0)
57 #define _FP_FRAC_OVERP_1(fs,X)  (X##_f & _FP_OVERFLOW_##fs)
58 #define _FP_FRAC_CLEAR_OVERP_1(fs,X)    (X##_f &= ~_FP_OVERFLOW_##fs)
59 #define _FP_FRAC_EQ_1(X, Y)     (X##_f == Y##_f)
60 #define _FP_FRAC_GE_1(X, Y)     (X##_f >= Y##_f)
61 #define _FP_FRAC_GT_1(X, Y)     (X##_f > Y##_f)
62
63 #define _FP_ZEROFRAC_1          0
64 #define _FP_MINFRAC_1           1
65 #define _FP_MAXFRAC_1           (~(_FP_WS_TYPE)0)
66
67 /*
68  * Unpack the raw bits of a native fp value.  Do not classify or
69  * normalize the data.
70  */
71
72 #define _FP_UNPACK_RAW_1(fs, X, val)                            \
73   do {                                                          \
74     union _FP_UNION_##fs _flo; _flo.flt = (val);                \
75                                                                 \
76     X##_f = _flo.bits.frac;                                     \
77     X##_e = _flo.bits.exp;                                      \
78     X##_s = _flo.bits.sign;                                     \
79   } while (0)
80
81 #define _FP_UNPACK_RAW_1_P(fs, X, val)                          \
82   do {                                                          \
83     union _FP_UNION_##fs *_flo =                                \
84       (union _FP_UNION_##fs *)(val);                            \
85                                                                 \
86     X##_f = _flo->bits.frac;                                    \
87     X##_e = _flo->bits.exp;                                     \
88     X##_s = _flo->bits.sign;                                    \
89   } while (0)
90
91 /*
92  * Repack the raw bits of a native fp value.
93  */
94
95 #define _FP_PACK_RAW_1(fs, val, X)                              \
96   do {                                                          \
97     union _FP_UNION_##fs _flo;                                  \
98                                                                 \
99     _flo.bits.frac = X##_f;                                     \
100     _flo.bits.exp  = X##_e;                                     \
101     _flo.bits.sign = X##_s;                                     \
102                                                                 \
103     (val) = _flo.flt;                                           \
104   } while (0)
105
106 #define _FP_PACK_RAW_1_P(fs, val, X)                            \
107   do {                                                          \
108     union _FP_UNION_##fs *_flo =                                \
109       (union _FP_UNION_##fs *)(val);                            \
110                                                                 \
111     _flo->bits.frac = X##_f;                                    \
112     _flo->bits.exp  = X##_e;                                    \
113     _flo->bits.sign = X##_s;                                    \
114   } while (0)
115
116
117 /*
118  * Multiplication algorithms:
119  */
120
121 /* Basic.  Assuming the host word size is >= 2*FRACBITS, we can do the
122    multiplication immediately.  */
123
124 #define _FP_MUL_MEAT_1_imm(wfracbits, R, X, Y)                          \
125   do {                                                                  \
126     R##_f = X##_f * Y##_f;                                              \
127     /* Normalize since we know where the msb of the multiplicands       \
128        were (bit B), we know that the msb of the of the product is      \
129        at either 2B or 2B-1.  */                                        \
130     _FP_FRAC_SRS_1(R, wfracbits-1, 2*wfracbits);                        \
131   } while (0)
132
133 /* Given a 1W * 1W => 2W primitive, do the extended multiplication.  */
134
135 #define _FP_MUL_MEAT_1_wide(wfracbits, R, X, Y, doit)                   \
136   do {                                                                  \
137     _FP_W_TYPE _Z_f0, _Z_f1;                                            \
138     doit(_Z_f1, _Z_f0, X##_f, Y##_f);                                   \
139     /* Normalize since we know where the msb of the multiplicands       \
140        were (bit B), we know that the msb of the of the product is      \
141        at either 2B or 2B-1.  */                                        \
142     _FP_FRAC_SRS_2(_Z, wfracbits-1, 2*wfracbits);                       \
143     R##_f = _Z_f0;                                                      \
144   } while (0)
145
146 /* Finally, a simple widening multiply algorithm.  What fun!  */
147
148 #define _FP_MUL_MEAT_1_hard(wfracbits, R, X, Y)                         \
149   do {                                                                  \
150     _FP_W_TYPE _xh, _xl, _yh, _yl, _z_f0, _z_f1, _a_f0, _a_f1;          \
151                                                                         \
152     /* split the words in half */                                       \
153     _xh = X##_f >> (_FP_W_TYPE_SIZE/2);                                 \
154     _xl = X##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1);         \
155     _yh = Y##_f >> (_FP_W_TYPE_SIZE/2);                                 \
156     _yl = Y##_f & (((_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2)) - 1);         \
157                                                                         \
158     /* multiply the pieces */                                           \
159     _z_f0 = _xl * _yl;                                                  \
160     _a_f0 = _xh * _yl;                                                  \
161     _a_f1 = _xl * _yh;                                                  \
162     _z_f1 = _xh * _yh;                                                  \
163                                                                         \
164     /* reassemble into two full words */                                \
165     if ((_a_f0 += _a_f1) < _a_f1)                                       \
166       _z_f1 += (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE/2);                    \
167     _a_f1 = _a_f0 >> (_FP_W_TYPE_SIZE/2);                               \
168     _a_f0 = _a_f0 << (_FP_W_TYPE_SIZE/2);                               \
169     _FP_FRAC_ADD_2(_z, _z, _a);                                         \
170                                                                         \
171     /* normalize */                                                     \
172     _FP_FRAC_SRS_2(_z, wfracbits - 1, 2*wfracbits);                     \
173     R##_f = _z_f0;                                                      \
174   } while (0)
175
176
177 /*
178  * Division algorithms:
179  */
180
181 /* Basic.  Assuming the host word size is >= 2*FRACBITS, we can do the
182    division immediately.  Give this macro either _FP_DIV_HELP_imm for
183    C primitives or _FP_DIV_HELP_ldiv for the ISO function.  Which you
184    choose will depend on what the compiler does with divrem4.  */
185
186 #define _FP_DIV_MEAT_1_imm(fs, R, X, Y, doit)           \
187   do {                                                  \
188     _FP_W_TYPE _q, _r;                                  \
189     X##_f <<= (X##_f < Y##_f                            \
190                ? R##_e--, _FP_WFRACBITS_##fs            \
191                : _FP_WFRACBITS_##fs - 1);               \
192     doit(_q, _r, X##_f, Y##_f);                         \
193     R##_f = _q | (_r != 0);                             \
194   } while (0)
195
196 /* GCC's longlong.h defines a 2W / 1W => (1W,1W) primitive udiv_qrnnd
197    that may be useful in this situation.  This first is for a primitive
198    that requires normalization, the second for one that does not.  Look
199    for UDIV_NEEDS_NORMALIZATION to tell which your machine needs.  */
200
201 #define _FP_DIV_MEAT_1_udiv_norm(fs, R, X, Y)                           \
202   do {                                                                  \
203     _FP_W_TYPE _nh, _nl, _q, _r, _y;                                    \
204                                                                         \
205     /* Normalize Y -- i.e. make the most significant bit set.  */       \
206     _y = Y##_f << _FP_WFRACXBITS_##fs;                                  \
207                                                                         \
208     /* Shift X op correspondingly high, that is, up one full word.  */  \
209     if (X##_f < Y##_f)                                                  \
210       {                                                                 \
211         R##_e--;                                                        \
212         _nl = 0;                                                        \
213         _nh = X##_f;                                                    \
214       }                                                                 \
215     else                                                                \
216       {                                                                 \
217         _nl = X##_f << (_FP_W_TYPE_SIZE - 1);                           \
218         _nh = X##_f >> 1;                                               \
219       }                                                                 \
220                                                                         \
221     udiv_qrnnd(_q, _r, _nh, _nl, _y);                                   \
222     R##_f = _q | (_r != 0);                                             \
223   } while (0)
224
225 #define _FP_DIV_MEAT_1_udiv(fs, R, X, Y)                \
226   do {                                                  \
227     _FP_W_TYPE _nh, _nl, _q, _r;                        \
228     if (X##_f < Y##_f)                                  \
229       {                                                 \
230         R##_e--;                                        \
231         _nl = X##_f << _FP_WFRACBITS_##fs;              \
232         _nh = X##_f >> _FP_WFRACXBITS_##fs;             \
233       }                                                 \
234     else                                                \
235       {                                                 \
236         _nl = X##_f << (_FP_WFRACBITS_##fs - 1);        \
237         _nh = X##_f >> (_FP_WFRACXBITS_##fs + 1);       \
238       }                                                 \
239     udiv_qrnnd(_q, _r, _nh, _nl, Y##_f);                \
240     R##_f = _q | (_r != 0);                             \
241   } while (0)
242   
243   
244 /*
245  * Square root algorithms:
246  * We have just one right now, maybe Newton approximation
247  * should be added for those machines where division is fast.
248  */
249  
250 #define _FP_SQRT_MEAT_1(R, S, T, X, q)                  \
251   do {                                                  \
252     while (q != _FP_WORK_ROUND)                         \
253       {                                                 \
254         T##_f = S##_f + q;                              \
255         if (T##_f <= X##_f)                             \
256           {                                             \
257             S##_f = T##_f + q;                          \
258             X##_f -= T##_f;                             \
259             R##_f += q;                                 \
260           }                                             \
261         _FP_FRAC_SLL_1(X, 1);                           \
262         q >>= 1;                                        \
263       }                                                 \
264     if (X##_f)                                          \
265       {                                                 \
266         if (S##_f < X##_f)                              \
267           R##_f |= _FP_WORK_ROUND;                      \
268         R##_f |= _FP_WORK_STICKY;                       \
269       }                                                 \
270   } while (0)
271
272 /*
273  * Assembly/disassembly for converting to/from integral types.  
274  * No shifting or overflow handled here.
275  */
276
277 #define _FP_FRAC_ASSEMBLE_1(r, X, rsize)        (r = X##_f)
278 #define _FP_FRAC_DISASSEMBLE_1(X, r, rsize)     (X##_f = r)
279
280
281 /*
282  * Convert FP values between word sizes
283  */
284
285 #define _FP_FRAC_CONV_1_1(dfs, sfs, D, S)                               \
286   do {                                                                  \
287     D##_f = S##_f;                                                      \
288     if (_FP_WFRACBITS_##sfs > _FP_WFRACBITS_##dfs)                      \
289       {                                                                 \
290         if (S##_c != FP_CLS_NAN)                                        \
291           _FP_FRAC_SRS_1(D, (_FP_WFRACBITS_##sfs-_FP_WFRACBITS_##dfs),  \
292                          _FP_WFRACBITS_##sfs);                          \
293         else                                                            \
294           _FP_FRAC_SRL_1(D, (_FP_WFRACBITS_##sfs-_FP_WFRACBITS_##dfs)); \
295       }                                                                 \
296     else                                                                \
297       D##_f <<= _FP_WFRACBITS_##dfs - _FP_WFRACBITS_##sfs;              \
298   } while (0)