1 /* Software floating-point emulation.
2 Definitions for IEEE Extended Precision.
3 Copyright (C) 1999-2018 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 */
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##_f[1] &= ~_FP_IMPLBIT_E; \
108 X##_e = FP_UNPACK_RAW_E_flo.bits.exp; \
109 X##_s = FP_UNPACK_RAW_E_flo.bits.sign; \
113 # define FP_UNPACK_RAW_EP(X, val) \
116 union _FP_UNION_E *FP_UNPACK_RAW_EP_flo \
117 = (union _FP_UNION_E *) (val); \
121 X##_f[0] = FP_UNPACK_RAW_EP_flo->bits.frac0; \
122 X##_f[1] = FP_UNPACK_RAW_EP_flo->bits.frac1; \
123 X##_f[1] &= ~_FP_IMPLBIT_E; \
124 X##_e = FP_UNPACK_RAW_EP_flo->bits.exp; \
125 X##_s = FP_UNPACK_RAW_EP_flo->bits.sign; \
129 # define FP_PACK_RAW_E(val, X) \
132 union _FP_UNION_E FP_PACK_RAW_E_flo; \
135 X##_f[1] |= _FP_IMPLBIT_E; \
137 X##_f[1] &= ~(_FP_IMPLBIT_E); \
138 FP_PACK_RAW_E_flo.bits.frac0 = X##_f[0]; \
139 FP_PACK_RAW_E_flo.bits.frac1 = X##_f[1]; \
140 FP_PACK_RAW_E_flo.bits.exp = X##_e; \
141 FP_PACK_RAW_E_flo.bits.sign = X##_s; \
143 (val) = FP_PACK_RAW_E_flo.flt; \
147 # define FP_PACK_RAW_EP(val, X) \
150 if (!FP_INHIBIT_RESULTS) \
152 union _FP_UNION_E *FP_PACK_RAW_EP_flo \
153 = (union _FP_UNION_E *) (val); \
156 X##_f[1] |= _FP_IMPLBIT_E; \
158 X##_f[1] &= ~(_FP_IMPLBIT_E); \
159 FP_PACK_RAW_EP_flo->bits.frac0 = X##_f[0]; \
160 FP_PACK_RAW_EP_flo->bits.frac1 = X##_f[1]; \
161 FP_PACK_RAW_EP_flo->bits.exp = X##_e; \
162 FP_PACK_RAW_EP_flo->bits.sign = X##_s; \
167 # define FP_UNPACK_E(X, val) \
170 FP_UNPACK_RAW_E (X, (val)); \
171 _FP_UNPACK_CANONICAL (E, 4, X); \
175 # define FP_UNPACK_EP(X, val) \
178 FP_UNPACK_RAW_EP (X, (val)); \
179 _FP_UNPACK_CANONICAL (E, 4, X); \
183 # define FP_UNPACK_SEMIRAW_E(X, val) \
186 FP_UNPACK_RAW_E (X, (val)); \
187 _FP_UNPACK_SEMIRAW (E, 4, X); \
191 # define FP_UNPACK_SEMIRAW_EP(X, val) \
194 FP_UNPACK_RAW_EP (X, (val)); \
195 _FP_UNPACK_SEMIRAW (E, 4, X); \
199 # define FP_PACK_E(val, X) \
202 _FP_PACK_CANONICAL (E, 4, X); \
203 FP_PACK_RAW_E ((val), X); \
207 # define FP_PACK_EP(val, X) \
210 _FP_PACK_CANONICAL (E, 4, X); \
211 FP_PACK_RAW_EP ((val), X); \
215 # define FP_PACK_SEMIRAW_E(val, X) \
218 _FP_PACK_SEMIRAW (E, 4, X); \
219 FP_PACK_RAW_E ((val), X); \
223 # define FP_PACK_SEMIRAW_EP(val, X) \
226 _FP_PACK_SEMIRAW (E, 4, X); \
227 FP_PACK_RAW_EP ((val), X); \
231 # define FP_ISSIGNAN_E(X) _FP_ISSIGNAN (E, 4, X)
232 # define FP_NEG_E(R, X) _FP_NEG (E, 4, R, X)
233 # define FP_ADD_E(R, X, Y) _FP_ADD (E, 4, R, X, Y)
234 # define FP_SUB_E(R, X, Y) _FP_SUB (E, 4, R, X, Y)
235 # define FP_MUL_E(R, X, Y) _FP_MUL (E, 4, R, X, Y)
236 # define FP_DIV_E(R, X, Y) _FP_DIV (E, 4, R, X, Y)
237 # define FP_SQRT_E(R, X) _FP_SQRT (E, 4, R, X)
238 # define FP_FMA_E(R, X, Y, Z) _FP_FMA (E, 4, 8, R, X, Y, Z)
240 /* Square root algorithms:
241 We have just one right now, maybe Newton approximation
242 should be added for those machines where division is fast.
243 This has special _E version because standard _4 square
244 root would not work (it has to start normally with the
245 second word and not the first), but as we have to do it
246 anyway, we optimize it by doing most of the calculations
247 in two UWtype registers instead of four. */
249 # define _FP_SQRT_MEAT_E(R, S, T, X, q) \
252 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
253 _FP_FRAC_SRL_4 (X, (_FP_WORKBITS)); \
256 T##_f[1] = S##_f[1] + (q); \
257 if (T##_f[1] <= X##_f[1]) \
259 S##_f[1] = T##_f[1] + (q); \
260 X##_f[1] -= T##_f[1]; \
263 _FP_FRAC_SLL_2 (X, 1); \
266 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
269 T##_f[0] = S##_f[0] + (q); \
270 T##_f[1] = S##_f[1]; \
271 if (T##_f[1] < X##_f[1] \
272 || (T##_f[1] == X##_f[1] \
273 && T##_f[0] <= X##_f[0])) \
275 S##_f[0] = T##_f[0] + (q); \
276 S##_f[1] += (T##_f[0] > S##_f[0]); \
277 _FP_FRAC_DEC_2 (X, T); \
280 _FP_FRAC_SLL_2 (X, 1); \
283 _FP_FRAC_SLL_4 (R, (_FP_WORKBITS)); \
284 if (X##_f[0] | X##_f[1]) \
286 if (S##_f[1] < X##_f[1] \
287 || (S##_f[1] == X##_f[1] \
288 && S##_f[0] < X##_f[0])) \
289 R##_f[0] |= _FP_WORK_ROUND; \
290 R##_f[0] |= _FP_WORK_STICKY; \
295 # define FP_CMP_E(r, X, Y, un, ex) _FP_CMP (E, 4, (r), X, Y, (un), (ex))
296 # define FP_CMP_EQ_E(r, X, Y, ex) _FP_CMP_EQ (E, 4, (r), X, Y, (ex))
297 # define FP_CMP_UNORD_E(r, X, Y, ex) _FP_CMP_UNORD (E, 4, (r), X, Y, (ex))
299 # define FP_TO_INT_E(r, X, rsz, rsg) _FP_TO_INT (E, 4, (r), X, (rsz), (rsg))
300 # define FP_TO_INT_ROUND_E(r, X, rsz, rsg) \
301 _FP_TO_INT_ROUND (E, 4, (r), X, (rsz), (rsg))
302 # define FP_FROM_INT_E(X, r, rs, rt) _FP_FROM_INT (E, 4, X, (r), (rs), rt)
304 # define _FP_FRAC_HIGH_E(X) (X##_f[2])
305 # define _FP_FRAC_HIGH_RAW_E(X) (X##_f[1])
307 # define _FP_FRAC_HIGH_DW_E(X) (X##_f[4])
309 #else /* not _FP_W_TYPE_SIZE < 64 */
313 struct _FP_STRUCT_LAYOUT
315 # if __BYTE_ORDER == __BIG_ENDIAN
316 _FP_W_TYPE pad
: (_FP_W_TYPE_SIZE
- 1 - _FP_EXPBITS_E
);
318 unsigned exp
: _FP_EXPBITS_E
;
319 _FP_W_TYPE frac
: _FP_W_TYPE_SIZE
;
321 _FP_W_TYPE frac
: _FP_W_TYPE_SIZE
;
322 unsigned exp
: _FP_EXPBITS_E
;
328 # define FP_DECL_E(X) _FP_DECL (2, X)
330 # define FP_UNPACK_RAW_E(X, val) \
333 union _FP_UNION_E FP_UNPACK_RAW_E_flo; \
334 FP_UNPACK_RAW_E_flo.flt = (val); \
336 X##_f0 = FP_UNPACK_RAW_E_flo.bits.frac; \
337 X##_f0 &= ~_FP_IMPLBIT_E; \
339 X##_e = FP_UNPACK_RAW_E_flo.bits.exp; \
340 X##_s = FP_UNPACK_RAW_E_flo.bits.sign; \
344 # define FP_UNPACK_RAW_EP(X, val) \
347 union _FP_UNION_E *FP_UNPACK_RAW_EP_flo \
348 = (union _FP_UNION_E *) (val); \
350 X##_f0 = FP_UNPACK_RAW_EP_flo->bits.frac; \
351 X##_f0 &= ~_FP_IMPLBIT_E; \
353 X##_e = FP_UNPACK_RAW_EP_flo->bits.exp; \
354 X##_s = FP_UNPACK_RAW_EP_flo->bits.sign; \
358 # define FP_PACK_RAW_E(val, X) \
361 union _FP_UNION_E FP_PACK_RAW_E_flo; \
364 X##_f0 |= _FP_IMPLBIT_E; \
366 X##_f0 &= ~(_FP_IMPLBIT_E); \
367 FP_PACK_RAW_E_flo.bits.frac = X##_f0; \
368 FP_PACK_RAW_E_flo.bits.exp = X##_e; \
369 FP_PACK_RAW_E_flo.bits.sign = X##_s; \
371 (val) = FP_PACK_RAW_E_flo.flt; \
375 # define FP_PACK_RAW_EP(fs, val, X) \
378 if (!FP_INHIBIT_RESULTS) \
380 union _FP_UNION_E *FP_PACK_RAW_EP_flo \
381 = (union _FP_UNION_E *) (val); \
384 X##_f0 |= _FP_IMPLBIT_E; \
386 X##_f0 &= ~(_FP_IMPLBIT_E); \
387 FP_PACK_RAW_EP_flo->bits.frac = X##_f0; \
388 FP_PACK_RAW_EP_flo->bits.exp = X##_e; \
389 FP_PACK_RAW_EP_flo->bits.sign = X##_s; \
395 # define FP_UNPACK_E(X, val) \
398 FP_UNPACK_RAW_E (X, (val)); \
399 _FP_UNPACK_CANONICAL (E, 2, X); \
403 # define FP_UNPACK_EP(X, val) \
406 FP_UNPACK_RAW_EP (X, (val)); \
407 _FP_UNPACK_CANONICAL (E, 2, X); \
411 # define FP_UNPACK_SEMIRAW_E(X, val) \
414 FP_UNPACK_RAW_E (X, (val)); \
415 _FP_UNPACK_SEMIRAW (E, 2, X); \
419 # define FP_UNPACK_SEMIRAW_EP(X, val) \
422 FP_UNPACK_RAW_EP (X, (val)); \
423 _FP_UNPACK_SEMIRAW (E, 2, X); \
427 # define FP_PACK_E(val, X) \
430 _FP_PACK_CANONICAL (E, 2, X); \
431 FP_PACK_RAW_E ((val), X); \
435 # define FP_PACK_EP(val, X) \
438 _FP_PACK_CANONICAL (E, 2, X); \
439 FP_PACK_RAW_EP ((val), X); \
443 # define FP_PACK_SEMIRAW_E(val, X) \
446 _FP_PACK_SEMIRAW (E, 2, X); \
447 FP_PACK_RAW_E ((val), X); \
451 # define FP_PACK_SEMIRAW_EP(val, X) \
454 _FP_PACK_SEMIRAW (E, 2, X); \
455 FP_PACK_RAW_EP ((val), X); \
459 # define FP_ISSIGNAN_E(X) _FP_ISSIGNAN (E, 2, X)
460 # define FP_NEG_E(R, X) _FP_NEG (E, 2, R, X)
461 # define FP_ADD_E(R, X, Y) _FP_ADD (E, 2, R, X, Y)
462 # define FP_SUB_E(R, X, Y) _FP_SUB (E, 2, R, X, Y)
463 # define FP_MUL_E(R, X, Y) _FP_MUL (E, 2, R, X, Y)
464 # define FP_DIV_E(R, X, Y) _FP_DIV (E, 2, R, X, Y)
465 # define FP_SQRT_E(R, X) _FP_SQRT (E, 2, R, X)
466 # define FP_FMA_E(R, X, Y, Z) _FP_FMA (E, 2, 4, R, X, Y, Z)
468 /* Square root algorithms:
469 We have just one right now, maybe Newton approximation
470 should be added for those machines where division is fast.
471 We optimize it by doing most of the calculations
472 in one UWtype registers instead of two, although we don't
474 # define _FP_SQRT_MEAT_E(R, S, T, X, q) \
477 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
478 _FP_FRAC_SRL_2 (X, (_FP_WORKBITS)); \
481 T##_f0 = S##_f0 + (q); \
482 if (T##_f0 <= X##_f0) \
484 S##_f0 = T##_f0 + (q); \
488 _FP_FRAC_SLL_1 (X, 1); \
491 _FP_FRAC_SLL_2 (R, (_FP_WORKBITS)); \
494 if (S##_f0 < X##_f0) \
495 R##_f0 |= _FP_WORK_ROUND; \
496 R##_f0 |= _FP_WORK_STICKY; \
501 # define FP_CMP_E(r, X, Y, un, ex) _FP_CMP (E, 2, (r), X, Y, (un), (ex))
502 # define FP_CMP_EQ_E(r, X, Y, ex) _FP_CMP_EQ (E, 2, (r), X, Y, (ex))
503 # define FP_CMP_UNORD_E(r, X, Y, ex) _FP_CMP_UNORD (E, 2, (r), X, Y, (ex))
505 # define FP_TO_INT_E(r, X, rsz, rsg) _FP_TO_INT (E, 2, (r), X, (rsz), (rsg))
506 # define FP_TO_INT_ROUND_E(r, X, rsz, rsg) \
507 _FP_TO_INT_ROUND (E, 2, (r), X, (rsz), (rsg))
508 # define FP_FROM_INT_E(X, r, rs, rt) _FP_FROM_INT (E, 2, X, (r), (rs), rt)
510 # define _FP_FRAC_HIGH_E(X) (X##_f1)
511 # define _FP_FRAC_HIGH_RAW_E(X) (X##_f0)
513 # define _FP_FRAC_HIGH_DW_E(X) (X##_f[2])
515 #endif /* not _FP_W_TYPE_SIZE < 64 */
517 #endif /* !SOFT_FP_EXTENDED_H */