1 /* Software floating-point emulation.
2 Definitions for IEEE Extended Precision.
3 Copyright (C) 1999 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Jakub Jelinek (jj@ultra.linux.cz).
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #ifndef __MATH_EMU_EXTENDED_H__
24 #define __MATH_EMU_EXTENDED_H__
26 #if _FP_W_TYPE_SIZE < 32
27 #error "Here's a nickel, kid. Go buy yourself a real computer."
30 #if _FP_W_TYPE_SIZE < 64
31 #define _FP_FRACTBITS_E (4*_FP_W_TYPE_SIZE)
33 #define _FP_FRACTBITS_E (2*_FP_W_TYPE_SIZE)
36 #define _FP_FRACBITS_E 64
37 #define _FP_FRACXBITS_E (_FP_FRACTBITS_E - _FP_FRACBITS_E)
38 #define _FP_WFRACBITS_E (_FP_WORKBITS + _FP_FRACBITS_E)
39 #define _FP_WFRACXBITS_E (_FP_FRACTBITS_E - _FP_WFRACBITS_E)
40 #define _FP_EXPBITS_E 15
41 #define _FP_EXPBIAS_E 16383
42 #define _FP_EXPMAX_E 32767
44 #define _FP_QNANBIT_E \
45 ((_FP_W_TYPE)1 << (_FP_FRACBITS_E-2) % _FP_W_TYPE_SIZE)
46 #define _FP_IMPLBIT_E \
47 ((_FP_W_TYPE)1 << (_FP_FRACBITS_E-1) % _FP_W_TYPE_SIZE)
48 #define _FP_OVERFLOW_E \
49 ((_FP_W_TYPE)1 << (_FP_WFRACBITS_E % _FP_W_TYPE_SIZE))
51 #if _FP_W_TYPE_SIZE < 64
58 #if __BYTE_ORDER == __BIG_ENDIAN
59 unsigned long pad1 : _FP_W_TYPE_SIZE;
60 unsigned long pad2 : (_FP_W_TYPE_SIZE - 1 - _FP_EXPBITS_E);
61 unsigned long sign : 1;
62 unsigned long exp : _FP_EXPBITS_E;
63 unsigned long frac1 : _FP_W_TYPE_SIZE;
64 unsigned long frac0 : _FP_W_TYPE_SIZE;
66 unsigned long frac0 : _FP_W_TYPE_SIZE;
67 unsigned long frac1 : _FP_W_TYPE_SIZE;
68 unsigned exp : _FP_EXPBITS_E;
70 #endif /* not bigendian */
71 } bits __attribute__((packed));
75 #define FP_DECL_E(X) _FP_DECL(4,X)
77 #define FP_UNPACK_RAW_E(X, val) \
79 union _FP_UNION_E _flo; _flo.flt = (val); \
81 X##_f[2] = 0; X##_f[3] = 0; \
82 X##_f[0] = _flo.bits.frac0; \
83 X##_f[1] = _flo.bits.frac1; \
84 X##_e = _flo.bits.exp; \
85 X##_s = _flo.bits.sign; \
86 if (!X##_e && (X##_f[1] || X##_f[0]) \
87 && !(X##_f[1] & _FP_IMPLBIT_E)) \
90 FP_SET_EXCEPTION(FP_EX_DENORM); \
94 #define FP_UNPACK_RAW_EP(X, val) \
96 union _FP_UNION_E *_flo = \
97 (union _FP_UNION_E *)(val); \
99 X##_f[2] = 0; X##_f[3] = 0; \
100 X##_f[0] = _flo->bits.frac0; \
101 X##_f[1] = _flo->bits.frac1; \
102 X##_e = _flo->bits.exp; \
103 X##_s = _flo->bits.sign; \
104 if (!X##_e && (X##_f[1] || X##_f[0]) \
105 && !(X##_f[1] & _FP_IMPLBIT_E)) \
108 FP_SET_EXCEPTION(FP_EX_DENORM); \
112 #define FP_PACK_RAW_E(val, X) \
114 union _FP_UNION_E _flo; \
116 if (X##_e) X##_f[1] |= _FP_IMPLBIT_E; \
117 else X##_f[1] &= ~(_FP_IMPLBIT_E); \
118 _flo.bits.frac0 = X##_f[0]; \
119 _flo.bits.frac1 = X##_f[1]; \
120 _flo.bits.exp = X##_e; \
121 _flo.bits.sign = X##_s; \
126 #define FP_PACK_RAW_EP(val, X) \
128 if (!FP_INHIBIT_RESULTS) \
130 union _FP_UNION_E *_flo = \
131 (union _FP_UNION_E *)(val); \
133 if (X##_e) X##_f[1] |= _FP_IMPLBIT_E; \
134 else X##_f[1] &= ~(_FP_IMPLBIT_E); \
135 _flo->bits.frac0 = X##_f[0]; \
136 _flo->bits.frac1 = X##_f[1]; \
137 _flo->bits.exp = X##_e; \
138 _flo->bits.sign = X##_s; \
142 #define FP_UNPACK_E(X,val) \
144 FP_UNPACK_RAW_E(X,val); \
145 _FP_UNPACK_CANONICAL(E,4,X); \
148 #define FP_UNPACK_EP(X,val) \
150 FP_UNPACK_RAW_2_P(X,val); \
151 _FP_UNPACK_CANONICAL(E,4,X); \
154 #define FP_PACK_E(val,X) \
156 _FP_PACK_CANONICAL(E,4,X); \
157 FP_PACK_RAW_E(val,X); \
160 #define FP_PACK_EP(val,X) \
162 _FP_PACK_CANONICAL(E,4,X); \
163 FP_PACK_RAW_EP(val,X); \
166 #define FP_ISSIGNAN_E(X) _FP_ISSIGNAN(E,4,X)
167 #define FP_NEG_E(R,X) _FP_NEG(E,4,R,X)
168 #define FP_ADD_E(R,X,Y) _FP_ADD(E,4,R,X,Y)
169 #define FP_SUB_E(R,X,Y) _FP_SUB(E,4,R,X,Y)
170 #define FP_MUL_E(R,X,Y) _FP_MUL(E,4,R,X,Y)
171 #define FP_DIV_E(R,X,Y) _FP_DIV(E,4,R,X,Y)
172 #define FP_SQRT_E(R,X) _FP_SQRT(E,4,R,X)
175 * Square root algorithms:
176 * We have just one right now, maybe Newton approximation
177 * should be added for those machines where division is fast.
178 * This has special _E version because standard _4 square
179 * root would not work (it has to start normally with the
180 * second word and not the first), but as we have to do it
181 * anyway, we optimize it by doing most of the calculations
182 * in two UWtype registers instead of four.
185 #define _FP_SQRT_MEAT_E(R, S, T, X, q) \
187 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
188 _FP_FRAC_SRL_4(X, (_FP_WORKBITS)); \
191 T##_f[1] = S##_f[1] + q; \
192 if (T##_f[1] <= X##_f[1]) \
194 S##_f[1] = T##_f[1] + q; \
195 X##_f[1] -= T##_f[1]; \
198 _FP_FRAC_SLL_2(X, 1); \
201 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
204 T##_f[0] = S##_f[0] + q; \
205 T##_f[1] = S##_f[1]; \
206 if (T##_f[1] < X##_f[1] || \
207 (T##_f[1] == X##_f[1] && \
208 T##_f[0] <= X##_f[0])) \
210 S##_f[0] = T##_f[0] + q; \
211 S##_f[1] += (T##_f[0] > S##_f[0]); \
212 _FP_FRAC_DEC_2(X, T); \
215 _FP_FRAC_SLL_2(X, 1); \
218 _FP_FRAC_SLL_4(R, (_FP_WORKBITS)); \
219 if (X##_f[0] | X##_f[1]) \
221 if (S##_f[1] < X##_f[1] || \
222 (S##_f[1] == X##_f[1] && \
223 S##_f[0] < X##_f[0])) \
224 R##_f[0] |= _FP_WORK_ROUND; \
225 R##_f[0] |= _FP_WORK_STICKY; \
229 #define FP_CMP_E(r,X,Y,un) _FP_CMP(E,4,r,X,Y,un)
230 #define FP_CMP_EQ_E(r,X,Y) _FP_CMP_EQ(E,4,r,X,Y)
232 #define FP_TO_INT_E(r,X,rsz,rsg) _FP_TO_INT(E,4,r,X,rsz,rsg)
233 #define FP_TO_INT_ROUND_E(r,X,rsz,rsg) _FP_TO_INT_ROUND(E,4,r,X,rsz,rsg)
234 #define FP_FROM_INT_E(X,r,rs,rt) _FP_FROM_INT(E,4,X,r,rs,rt)
236 #define _FP_FRAC_HIGH_E(X) (X##_f[2])
237 #define _FP_FRAC_HIGH_RAW_E(X) (X##_f[1])
239 #else /* not _FP_W_TYPE_SIZE < 64 */
242 long double flt /* __attribute__((mode(TF))) */ ;
244 #if __BYTE_ORDER == __BIG_ENDIAN
245 unsigned long pad : (_FP_W_TYPE_SIZE - 1 - _FP_EXPBITS_E);
247 unsigned exp : _FP_EXPBITS_E;
248 unsigned long frac : _FP_W_TYPE_SIZE;
250 unsigned long frac : _FP_W_TYPE_SIZE;
251 unsigned exp : _FP_EXPBITS_E;
257 #define FP_DECL_E(X) _FP_DECL(2,X)
259 #define FP_UNPACK_RAW_E(X, val) \
261 union _FP_UNION_E _flo; _flo.flt = (val); \
263 X##_f0 = _flo.bits.frac; \
265 X##_e = _flo.bits.exp; \
266 X##_s = _flo.bits.sign; \
267 if (!X##_e && X##_f0 && !(X##_f0 & _FP_IMPLBIT_E)) \
270 FP_SET_EXCEPTION(FP_EX_DENORM); \
274 #define FP_UNPACK_RAW_EP(X, val) \
276 union _FP_UNION_E *_flo = \
277 (union _FP_UNION_E *)(val); \
279 X##_f0 = _flo->bits.frac; \
281 X##_e = _flo->bits.exp; \
282 X##_s = _flo->bits.sign; \
283 if (!X##_e && X##_f0 && !(X##_f0 & _FP_IMPLBIT_E)) \
286 FP_SET_EXCEPTION(FP_EX_DENORM); \
290 #define FP_PACK_RAW_E(val, X) \
292 union _FP_UNION_E _flo; \
294 if (X##_e) X##_f0 |= _FP_IMPLBIT_E; \
295 else X##_f0 &= ~(_FP_IMPLBIT_E); \
296 _flo.bits.frac = X##_f0; \
297 _flo.bits.exp = X##_e; \
298 _flo.bits.sign = X##_s; \
303 #define FP_PACK_RAW_EP(fs, val, X) \
305 if (!FP_INHIBIT_RESULTS) \
307 union _FP_UNION_E *_flo = \
308 (union _FP_UNION_E *)(val); \
310 if (X##_e) X##_f0 |= _FP_IMPLBIT_E; \
311 else X##_f0 &= ~(_FP_IMPLBIT_E); \
312 _flo->bits.frac = X##_f0; \
313 _flo->bits.exp = X##_e; \
314 _flo->bits.sign = X##_s; \
319 #define FP_UNPACK_E(X,val) \
321 FP_UNPACK_RAW_E(X,val); \
322 _FP_UNPACK_CANONICAL(E,2,X); \
325 #define FP_UNPACK_EP(X,val) \
327 FP_UNPACK_RAW_EP(X,val); \
328 _FP_UNPACK_CANONICAL(E,2,X); \
331 #define FP_PACK_E(val,X) \
333 _FP_PACK_CANONICAL(E,2,X); \
334 FP_PACK_RAW_E(val,X); \
337 #define FP_PACK_EP(val,X) \
339 _FP_PACK_CANONICAL(E,2,X); \
340 FP_PACK_RAW_EP(val,X); \
343 #define FP_ISSIGNAN_E(X) _FP_ISSIGNAN(E,2,X)
344 #define FP_NEG_E(R,X) _FP_NEG(E,2,R,X)
345 #define FP_ADD_E(R,X,Y) _FP_ADD(E,2,R,X,Y)
346 #define FP_SUB_E(R,X,Y) _FP_SUB(E,2,R,X,Y)
347 #define FP_MUL_E(R,X,Y) _FP_MUL(E,2,R,X,Y)
348 #define FP_DIV_E(R,X,Y) _FP_DIV(E,2,R,X,Y)
349 #define FP_SQRT_E(R,X) _FP_SQRT(E,2,R,X)
352 * Square root algorithms:
353 * We have just one right now, maybe Newton approximation
354 * should be added for those machines where division is fast.
355 * We optimize it by doing most of the calculations
356 * in one UWtype registers instead of two, although we don't
359 #define _FP_SQRT_MEAT_E(R, S, T, X, q) \
361 q = (_FP_W_TYPE)1 << (_FP_W_TYPE_SIZE - 1); \
362 _FP_FRAC_SRL_2(X, (_FP_WORKBITS)); \
365 T##_f0 = S##_f0 + q; \
366 if (T##_f0 <= X##_f0) \
368 S##_f0 = T##_f0 + q; \
372 _FP_FRAC_SLL_1(X, 1); \
375 _FP_FRAC_SLL_2(R, (_FP_WORKBITS)); \
378 if (S##_f0 < X##_f0) \
379 R##_f0 |= _FP_WORK_ROUND; \
380 R##_f0 |= _FP_WORK_STICKY; \
384 #define FP_CMP_E(r,X,Y,un) _FP_CMP(E,2,r,X,Y,un)
385 #define FP_CMP_EQ_E(r,X,Y) _FP_CMP_EQ(E,2,r,X,Y)
387 #define FP_TO_INT_E(r,X,rsz,rsg) _FP_TO_INT(E,2,r,X,rsz,rsg)
388 #define FP_TO_INT_ROUND_E(r,X,rsz,rsg) _FP_TO_INT_ROUND(E,2,r,X,rsz,rsg)
389 #define FP_FROM_INT_E(X,r,rs,rt) _FP_FROM_INT(E,2,X,r,rs,rt)
391 #define _FP_FRAC_HIGH_E(X) (X##_f1)
392 #define _FP_FRAC_HIGH_RAW_E(X) (X##_f0)
394 #endif /* not _FP_W_TYPE_SIZE < 64 */
396 #endif /* __MATH_EMU_EXTENDED_H__ */