1 /* Software floating-point emulation.
2 Definitions for IEEE Extended Precision.
3 Copyright (C) 1999-2015 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 Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU Lesser General Public
13 License, the Free Software Foundation gives you unlimited
14 permission to link the compiled version of this file into
15 combinations with other programs, and to distribute those
16 combinations without any restriction coming from the use of this
17 file. (The Lesser General Public License restrictions do apply in
18 other respects; for example, they cover modification of the file,
19 and distribution when not linked into a combine executable.)
21 The GNU C Library is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 Lesser General Public License for more details.
26 You should have received a copy of the GNU Lesser General Public
27 License along with the GNU C Library; if not, see
28 <http://www.gnu.org/licenses/>. */
30 #ifndef SOFT_FP_EXTENDED_H
31 #define SOFT_FP_EXTENDED_H 1
33 #if _FP_W_TYPE_SIZE < 32
34 # error "Here's a nickel, kid. Go buy yourself a real computer."
37 #if _FP_W_TYPE_SIZE < 64
38 # define _FP_FRACTBITS_E (4*_FP_W_TYPE_SIZE)
39 # define _FP_FRACTBITS_DW_E (8*_FP_W_TYPE_SIZE)
41 # define _FP_FRACTBITS_E (2*_FP_W_TYPE_SIZE)
42 # define _FP_FRACTBITS_DW_E (4*_FP_W_TYPE_SIZE)
45 #define _FP_FRACBITS_E 64
46 #define _FP_FRACXBITS_E (_FP_FRACTBITS_E - _FP_FRACBITS_E)
47 #define _FP_WFRACBITS_E (_FP_WORKBITS + _FP_FRACBITS_E)
48 #define _FP_WFRACXBITS_E (_FP_FRACTBITS_E - _FP_WFRACBITS_E)
49 #define _FP_EXPBITS_E 15
50 #define _FP_EXPBIAS_E 16383
51 #define _FP_EXPMAX_E 32767
53 #define _FP_QNANBIT_E \
54 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-2) % _FP_W_TYPE_SIZE)
55 #define _FP_QNANBIT_SH_E \
56 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-2+_FP_WORKBITS) % _FP_W_TYPE_SIZE)
57 #define _FP_IMPLBIT_E \
58 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-1) % _FP_W_TYPE_SIZE)
59 #define _FP_IMPLBIT_SH_E \
60 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-1+_FP_WORKBITS) % _FP_W_TYPE_SIZE)
61 #define _FP_OVERFLOW_E \
62 ((_FP_W_TYPE) 1 << (_FP_WFRACBITS_E % _FP_W_TYPE_SIZE))
64 #define _FP_WFRACBITS_DW_E (2 * _FP_WFRACBITS_E)
65 #define _FP_WFRACXBITS_DW_E (_FP_FRACTBITS_DW_E - _FP_WFRACBITS_DW_E)
66 #define _FP_HIGHBIT_DW_E \
67 ((_FP_W_TYPE) 1 << (_FP_WFRACBITS_DW_E - 1) % _FP_W_TYPE_SIZE)
69 typedef float XFtype
__attribute__ ((mode (XF
)));
71 #if _FP_W_TYPE_SIZE < 64
76 struct _FP_STRUCT_LAYOUT
78 # if __BYTE_ORDER == __BIG_ENDIAN
79 unsigned long pad1
: _FP_W_TYPE_SIZE
;
80 unsigned long pad2
: (_FP_W_TYPE_SIZE
- 1 - _FP_EXPBITS_E
);
81 unsigned long sign
: 1;
82 unsigned long exp
: _FP_EXPBITS_E
;
83 unsigned long frac1
: _FP_W_TYPE_SIZE
;
84 unsigned long frac0
: _FP_W_TYPE_SIZE
;
86 unsigned long frac0
: _FP_W_TYPE_SIZE
;
87 unsigned long frac1
: _FP_W_TYPE_SIZE
;
88 unsigned exp
: _FP_EXPBITS_E
;
90 # endif /* not bigendian */
91 } bits
__attribute__ ((packed
));
95 # define FP_DECL_E(X) _FP_DECL (4, X)
97 # define FP_UNPACK_RAW_E(X, val) \
100 union _FP_UNION_E FP_UNPACK_RAW_E_flo; \
101 FP_UNPACK_RAW_E_flo.flt = (val); \
105 X##_f[0] = FP_UNPACK_RAW_E_flo.bits.frac0; \
106 X##_f[1] = FP_UNPACK_RAW_E_flo.bits.frac1; \
107 X##_e = FP_UNPACK_RAW_E_flo.bits.exp; \
108 X##_s = FP_UNPACK_RAW_E_flo.bits.sign; \
112 # define FP_UNPACK_RAW_EP(X, val) \
115 union _FP_UNION_E *FP_UNPACK_RAW_EP_flo \
116 = (union _FP_UNION_E *) (val); \
120 X##_f[0] = FP_UNPACK_RAW_EP_flo->bits.frac0; \
121 X##_f[1] = FP_UNPACK_RAW_EP_flo->bits.frac1; \
122 X##_e = FP_UNPACK_RAW_EP_flo->bits.exp; \
123 X##_s = FP_UNPACK_RAW_EP_flo->bits.sign; \
127 # define FP_PACK_RAW_E(val, X) \
130 union _FP_UNION_E FP_PACK_RAW_E_flo; \
133 X##_f[1] |= _FP_IMPLBIT_E; \
135 X##_f[1] &= ~(_FP_IMPLBIT_E); \
136 FP_PACK_RAW_E_flo.bits.frac0 = X##_f[0]; \
137 FP_PACK_RAW_E_flo.bits.frac1 = X##_f[1]; \
138 FP_PACK_RAW_E_flo.bits.exp = X##_e; \
139 FP_PACK_RAW_E_flo.bits.sign = X##_s; \
141 (val) = FP_PACK_RAW_E_flo.flt; \
145 # define FP_PACK_RAW_EP(val, X) \
148 if (!FP_INHIBIT_RESULTS) \
150 union _FP_UNION_E *FP_PACK_RAW_EP_flo \
151 = (union _FP_UNION_E *) (val); \
154 X##_f[1] |= _FP_IMPLBIT_E; \
156 X##_f[1] &= ~(_FP_IMPLBIT_E); \
157 FP_PACK_RAW_EP_flo->bits.frac0 = X##_f[0]; \
158 FP_PACK_RAW_EP_flo->bits.frac1 = X##_f[1]; \
159 FP_PACK_RAW_EP_flo->bits.exp = X##_e; \
160 FP_PACK_RAW_EP_flo->bits.sign = X##_s; \
165 # define FP_UNPACK_E(X, val) \
168 FP_UNPACK_RAW_E (X, (val)); \
169 _FP_UNPACK_CANONICAL (E, 4, X); \
173 # define FP_UNPACK_EP(X, val) \
176 FP_UNPACK_RAW_EP (X, (val)); \
177 _FP_UNPACK_CANONICAL (E, 4, X); \
181 # define FP_UNPACK_SEMIRAW_E(X, val) \
184 FP_UNPACK_RAW_E (X, (val)); \
185 _FP_UNPACK_SEMIRAW (E, 4, X); \
189 # define FP_UNPACK_SEMIRAW_EP(X, val) \
192 FP_UNPACK_RAW_EP (X, (val)); \
193 _FP_UNPACK_SEMIRAW (E, 4, X); \
197 # define FP_PACK_E(val, X) \
200 _FP_PACK_CANONICAL (E, 4, X); \
201 FP_PACK_RAW_E ((val), X); \
205 # define FP_PACK_EP(val, X) \
208 _FP_PACK_CANONICAL (E, 4, X); \
209 FP_PACK_RAW_EP ((val), X); \
213 # define FP_PACK_SEMIRAW_E(val, X) \
216 _FP_PACK_SEMIRAW (E, 4, X); \
217 FP_PACK_RAW_E ((val), X); \
221 # define FP_PACK_SEMIRAW_EP(val, X) \
224 _FP_PACK_SEMIRAW (E, 4, X); \
225 FP_PACK_RAW_EP ((val), X); \
229 # define FP_ISSIGNAN_E(X) _FP_ISSIGNAN (E, 4, X)
230 # define FP_NEG_E(R, X) _FP_NEG (E, 4, R, X)
231 # define FP_ADD_E(R, X, Y) _FP_ADD (E, 4, R, X, Y)
232 # define FP_SUB_E(R, X, Y) _FP_SUB (E, 4, R, X, Y)
233 # define FP_MUL_E(R, X, Y) _FP_MUL (E, 4, R, X, Y)
234 # define FP_DIV_E(R, X, Y) _FP_DIV (E, 4, R, X, Y)
235 # define FP_SQRT_E(R, X) _FP_SQRT (E, 4, R, X)
236 # define FP_FMA_E(R, X, Y, Z) _FP_FMA (E, 4, 8, R, X, Y, Z)
238 /* Square root algorithms:
239 We have just one right now, maybe Newton approximation
240 should be added for those machines where division is fast.
241 This has special _E version because standard _4 square
242 root would not work (it has to start normally with the
243 second word and not the first), but as we have to do it
244 anyway, we optimize it by doing most of the calculations
245 in two UWtype registers instead of four. */
247 # define _FP_SQRT_MEAT_E(R, S, T, X, q) \
250 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
251 _FP_FRAC_SRL_4 (X, (_FP_WORKBITS)); \
254 T##_f[1] = S##_f[1] + (q); \
255 if (T##_f[1] <= X##_f[1]) \
257 S##_f[1] = T##_f[1] + (q); \
258 X##_f[1] -= T##_f[1]; \
261 _FP_FRAC_SLL_2 (X, 1); \
264 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
267 T##_f[0] = S##_f[0] + (q); \
268 T##_f[1] = S##_f[1]; \
269 if (T##_f[1] < X##_f[1] \
270 || (T##_f[1] == X##_f[1] \
271 && T##_f[0] <= X##_f[0])) \
273 S##_f[0] = T##_f[0] + (q); \
274 S##_f[1] += (T##_f[0] > S##_f[0]); \
275 _FP_FRAC_DEC_2 (X, T); \
278 _FP_FRAC_SLL_2 (X, 1); \
281 _FP_FRAC_SLL_4 (R, (_FP_WORKBITS)); \
282 if (X##_f[0] | X##_f[1]) \
284 if (S##_f[1] < X##_f[1] \
285 || (S##_f[1] == X##_f[1] \
286 && S##_f[0] < X##_f[0])) \
287 R##_f[0] |= _FP_WORK_ROUND; \
288 R##_f[0] |= _FP_WORK_STICKY; \
293 # define FP_CMP_E(r, X, Y, un, ex) _FP_CMP (E, 4, (r), X, Y, (un), (ex))
294 # define FP_CMP_EQ_E(r, X, Y, ex) _FP_CMP_EQ (E, 4, (r), X, Y, (ex))
295 # define FP_CMP_UNORD_E(r, X, Y, ex) _FP_CMP_UNORD (E, 4, (r), X, Y, (ex))
297 # define FP_TO_INT_E(r, X, rsz, rsg) _FP_TO_INT (E, 4, (r), X, (rsz), (rsg))
298 # define FP_TO_INT_ROUND_E(r, X, rsz, rsg) \
299 _FP_TO_INT_ROUND (E, 4, (r), X, (rsz), (rsg))
300 # define FP_FROM_INT_E(X, r, rs, rt) _FP_FROM_INT (E, 4, X, (r), (rs), rt)
302 # define _FP_FRAC_HIGH_E(X) (X##_f[2])
303 # define _FP_FRAC_HIGH_RAW_E(X) (X##_f[1])
305 # define _FP_FRAC_HIGH_DW_E(X) (X##_f[4])
307 #else /* not _FP_W_TYPE_SIZE < 64 */
311 struct _FP_STRUCT_LAYOUT
313 # if __BYTE_ORDER == __BIG_ENDIAN
314 _FP_W_TYPE pad
: (_FP_W_TYPE_SIZE
- 1 - _FP_EXPBITS_E
);
316 unsigned exp
: _FP_EXPBITS_E
;
317 _FP_W_TYPE frac
: _FP_W_TYPE_SIZE
;
319 _FP_W_TYPE frac
: _FP_W_TYPE_SIZE
;
320 unsigned exp
: _FP_EXPBITS_E
;
326 # define FP_DECL_E(X) _FP_DECL (2, X)
328 # define FP_UNPACK_RAW_E(X, val) \
331 union _FP_UNION_E FP_UNPACK_RAW_E_flo; \
332 FP_UNPACK_RAW_E_flo.flt = (val); \
334 X##_f0 = FP_UNPACK_RAW_E_flo.bits.frac; \
336 X##_e = FP_UNPACK_RAW_E_flo.bits.exp; \
337 X##_s = FP_UNPACK_RAW_E_flo.bits.sign; \
341 # define FP_UNPACK_RAW_EP(X, val) \
344 union _FP_UNION_E *FP_UNPACK_RAW_EP_flo \
345 = (union _FP_UNION_E *) (val); \
347 X##_f0 = FP_UNPACK_RAW_EP_flo->bits.frac; \
349 X##_e = FP_UNPACK_RAW_EP_flo->bits.exp; \
350 X##_s = FP_UNPACK_RAW_EP_flo->bits.sign; \
354 # define FP_PACK_RAW_E(val, X) \
357 union _FP_UNION_E FP_PACK_RAW_E_flo; \
360 X##_f0 |= _FP_IMPLBIT_E; \
362 X##_f0 &= ~(_FP_IMPLBIT_E); \
363 FP_PACK_RAW_E_flo.bits.frac = X##_f0; \
364 FP_PACK_RAW_E_flo.bits.exp = X##_e; \
365 FP_PACK_RAW_E_flo.bits.sign = X##_s; \
367 (val) = FP_PACK_RAW_E_flo.flt; \
371 # define FP_PACK_RAW_EP(fs, val, X) \
374 if (!FP_INHIBIT_RESULTS) \
376 union _FP_UNION_E *FP_PACK_RAW_EP_flo \
377 = (union _FP_UNION_E *) (val); \
380 X##_f0 |= _FP_IMPLBIT_E; \
382 X##_f0 &= ~(_FP_IMPLBIT_E); \
383 FP_PACK_RAW_EP_flo->bits.frac = X##_f0; \
384 FP_PACK_RAW_EP_flo->bits.exp = X##_e; \
385 FP_PACK_RAW_EP_flo->bits.sign = X##_s; \
391 # define FP_UNPACK_E(X, val) \
394 FP_UNPACK_RAW_E (X, (val)); \
395 _FP_UNPACK_CANONICAL (E, 2, X); \
399 # define FP_UNPACK_EP(X, val) \
402 FP_UNPACK_RAW_EP (X, (val)); \
403 _FP_UNPACK_CANONICAL (E, 2, X); \
407 # define FP_UNPACK_SEMIRAW_E(X, val) \
410 FP_UNPACK_RAW_E (X, (val)); \
411 _FP_UNPACK_SEMIRAW (E, 2, X); \
415 # define FP_UNPACK_SEMIRAW_EP(X, val) \
418 FP_UNPACK_RAW_EP (X, (val)); \
419 _FP_UNPACK_SEMIRAW (E, 2, X); \
423 # define FP_PACK_E(val, X) \
426 _FP_PACK_CANONICAL (E, 2, X); \
427 FP_PACK_RAW_E ((val), X); \
431 # define FP_PACK_EP(val, X) \
434 _FP_PACK_CANONICAL (E, 2, X); \
435 FP_PACK_RAW_EP ((val), X); \
439 # define FP_PACK_SEMIRAW_E(val, X) \
442 _FP_PACK_SEMIRAW (E, 2, X); \
443 FP_PACK_RAW_E ((val), X); \
447 # define FP_PACK_SEMIRAW_EP(val, X) \
450 _FP_PACK_SEMIRAW (E, 2, X); \
451 FP_PACK_RAW_EP ((val), X); \
455 # define FP_ISSIGNAN_E(X) _FP_ISSIGNAN (E, 2, X)
456 # define FP_NEG_E(R, X) _FP_NEG (E, 2, R, X)
457 # define FP_ADD_E(R, X, Y) _FP_ADD (E, 2, R, X, Y)
458 # define FP_SUB_E(R, X, Y) _FP_SUB (E, 2, R, X, Y)
459 # define FP_MUL_E(R, X, Y) _FP_MUL (E, 2, R, X, Y)
460 # define FP_DIV_E(R, X, Y) _FP_DIV (E, 2, R, X, Y)
461 # define FP_SQRT_E(R, X) _FP_SQRT (E, 2, R, X)
462 # define FP_FMA_E(R, X, Y, Z) _FP_FMA (E, 2, 4, R, X, Y, Z)
464 /* Square root algorithms:
465 We have just one right now, maybe Newton approximation
466 should be added for those machines where division is fast.
467 We optimize it by doing most of the calculations
468 in one UWtype registers instead of two, although we don't
470 # define _FP_SQRT_MEAT_E(R, S, T, X, q) \
473 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
474 _FP_FRAC_SRL_2 (X, (_FP_WORKBITS)); \
477 T##_f0 = S##_f0 + (q); \
478 if (T##_f0 <= X##_f0) \
480 S##_f0 = T##_f0 + (q); \
484 _FP_FRAC_SLL_1 (X, 1); \
487 _FP_FRAC_SLL_2 (R, (_FP_WORKBITS)); \
490 if (S##_f0 < X##_f0) \
491 R##_f0 |= _FP_WORK_ROUND; \
492 R##_f0 |= _FP_WORK_STICKY; \
497 # define FP_CMP_E(r, X, Y, un, ex) _FP_CMP (E, 2, (r), X, Y, (un), (ex))
498 # define FP_CMP_EQ_E(r, X, Y, ex) _FP_CMP_EQ (E, 2, (r), X, Y, (ex))
499 # define FP_CMP_UNORD_E(r, X, Y, ex) _FP_CMP_UNORD (E, 2, (r), X, Y, (ex))
501 # define FP_TO_INT_E(r, X, rsz, rsg) _FP_TO_INT (E, 2, (r), X, (rsz), (rsg))
502 # define FP_TO_INT_ROUND_E(r, X, rsz, rsg) \
503 _FP_TO_INT_ROUND (E, 2, (r), X, (rsz), (rsg))
504 # define FP_FROM_INT_E(X, r, rs, rt) _FP_FROM_INT (E, 2, X, (r), (rs), rt)
506 # define _FP_FRAC_HIGH_E(X) (X##_f1)
507 # define _FP_FRAC_HIGH_RAW_E(X) (X##_f0)
509 # define _FP_FRAC_HIGH_DW_E(X) (X##_f[2])
511 #endif /* not _FP_W_TYPE_SIZE < 64 */
513 #endif /* !SOFT_FP_EXTENDED_H */