1 /* Software floating-point emulation.
2 Definitions for IEEE Extended Precision.
3 Copyright (C) 1999-2022 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 In addition to the permissions in the GNU Lesser General Public
12 License, the Free Software Foundation gives you unlimited
13 permission to link the compiled version of this file into
14 combinations with other programs, and to distribute those
15 combinations without any restriction coming from the use of this
16 file. (The Lesser General Public License restrictions do apply in
17 other respects; for example, they cover modification of the file,
18 and distribution when not linked into a combine executable.)
20 The GNU C Library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with the GNU C Library; if not, see
27 <https://www.gnu.org/licenses/>. */
29 #ifndef SOFT_FP_EXTENDED_H
30 #define SOFT_FP_EXTENDED_H 1
32 #if _FP_W_TYPE_SIZE < 32
33 # error "Here's a nickel, kid. Go buy yourself a real computer."
36 #if _FP_W_TYPE_SIZE < 64
37 # define _FP_FRACTBITS_E (4*_FP_W_TYPE_SIZE)
38 # define _FP_FRACTBITS_DW_E (8*_FP_W_TYPE_SIZE)
40 # define _FP_FRACTBITS_E (2*_FP_W_TYPE_SIZE)
41 # define _FP_FRACTBITS_DW_E (4*_FP_W_TYPE_SIZE)
44 #define _FP_FRACBITS_E 64
45 #define _FP_FRACXBITS_E (_FP_FRACTBITS_E - _FP_FRACBITS_E)
46 #define _FP_WFRACBITS_E (_FP_WORKBITS + _FP_FRACBITS_E)
47 #define _FP_WFRACXBITS_E (_FP_FRACTBITS_E - _FP_WFRACBITS_E)
48 #define _FP_EXPBITS_E 15
49 #define _FP_EXPBIAS_E 16383
50 #define _FP_EXPMAX_E 32767
52 #define _FP_QNANBIT_E \
53 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-2) % _FP_W_TYPE_SIZE)
54 #define _FP_QNANBIT_SH_E \
55 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-2+_FP_WORKBITS) % _FP_W_TYPE_SIZE)
56 #define _FP_IMPLBIT_E \
57 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-1) % _FP_W_TYPE_SIZE)
58 #define _FP_IMPLBIT_SH_E \
59 ((_FP_W_TYPE) 1 << (_FP_FRACBITS_E-1+_FP_WORKBITS) % _FP_W_TYPE_SIZE)
60 #define _FP_OVERFLOW_E \
61 ((_FP_W_TYPE) 1 << (_FP_WFRACBITS_E % _FP_W_TYPE_SIZE))
63 #define _FP_WFRACBITS_DW_E (2 * _FP_WFRACBITS_E)
64 #define _FP_WFRACXBITS_DW_E (_FP_FRACTBITS_DW_E - _FP_WFRACBITS_DW_E)
65 #define _FP_HIGHBIT_DW_E \
66 ((_FP_W_TYPE) 1 << (_FP_WFRACBITS_DW_E - 1) % _FP_W_TYPE_SIZE)
68 typedef float XFtype
__attribute__ ((mode (XF
)));
70 #if _FP_W_TYPE_SIZE < 64
75 struct _FP_STRUCT_LAYOUT
77 # if __BYTE_ORDER == __BIG_ENDIAN
78 unsigned long pad1
: _FP_W_TYPE_SIZE
;
79 unsigned long pad2
: (_FP_W_TYPE_SIZE
- 1 - _FP_EXPBITS_E
);
80 unsigned long sign
: 1;
81 unsigned long exp
: _FP_EXPBITS_E
;
82 unsigned long frac1
: _FP_W_TYPE_SIZE
;
83 unsigned long frac0
: _FP_W_TYPE_SIZE
;
85 unsigned long frac0
: _FP_W_TYPE_SIZE
;
86 unsigned long frac1
: _FP_W_TYPE_SIZE
;
87 unsigned exp
: _FP_EXPBITS_E
;
89 # endif /* not bigendian */
94 # define FP_DECL_E(X) _FP_DECL (4, X)
96 # define FP_UNPACK_RAW_E(X, val) \
99 union _FP_UNION_E FP_UNPACK_RAW_E_flo; \
100 FP_UNPACK_RAW_E_flo.flt = (val); \
104 X##_f[0] = FP_UNPACK_RAW_E_flo.bits.frac0; \
105 X##_f[1] = FP_UNPACK_RAW_E_flo.bits.frac1; \
106 X##_f[1] &= ~_FP_IMPLBIT_E; \
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##_f[1] &= ~_FP_IMPLBIT_E; \
123 X##_e = FP_UNPACK_RAW_EP_flo->bits.exp; \
124 X##_s = FP_UNPACK_RAW_EP_flo->bits.sign; \
128 # define FP_PACK_RAW_E(val, X) \
131 union _FP_UNION_E FP_PACK_RAW_E_flo; \
134 X##_f[1] |= _FP_IMPLBIT_E; \
136 X##_f[1] &= ~(_FP_IMPLBIT_E); \
137 FP_PACK_RAW_E_flo.bits.frac0 = X##_f[0]; \
138 FP_PACK_RAW_E_flo.bits.frac1 = X##_f[1]; \
139 FP_PACK_RAW_E_flo.bits.exp = X##_e; \
140 FP_PACK_RAW_E_flo.bits.sign = X##_s; \
142 (val) = FP_PACK_RAW_E_flo.flt; \
146 # define FP_PACK_RAW_EP(val, X) \
149 if (!FP_INHIBIT_RESULTS) \
151 union _FP_UNION_E *FP_PACK_RAW_EP_flo \
152 = (union _FP_UNION_E *) (val); \
155 X##_f[1] |= _FP_IMPLBIT_E; \
157 X##_f[1] &= ~(_FP_IMPLBIT_E); \
158 FP_PACK_RAW_EP_flo->bits.frac0 = X##_f[0]; \
159 FP_PACK_RAW_EP_flo->bits.frac1 = X##_f[1]; \
160 FP_PACK_RAW_EP_flo->bits.exp = X##_e; \
161 FP_PACK_RAW_EP_flo->bits.sign = X##_s; \
166 # define FP_UNPACK_E(X, val) \
169 FP_UNPACK_RAW_E (X, (val)); \
170 _FP_UNPACK_CANONICAL (E, 4, X); \
174 # define FP_UNPACK_EP(X, val) \
177 FP_UNPACK_RAW_EP (X, (val)); \
178 _FP_UNPACK_CANONICAL (E, 4, X); \
182 # define FP_UNPACK_SEMIRAW_E(X, val) \
185 FP_UNPACK_RAW_E (X, (val)); \
186 _FP_UNPACK_SEMIRAW (E, 4, X); \
190 # define FP_UNPACK_SEMIRAW_EP(X, val) \
193 FP_UNPACK_RAW_EP (X, (val)); \
194 _FP_UNPACK_SEMIRAW (E, 4, X); \
198 # define FP_PACK_E(val, X) \
201 _FP_PACK_CANONICAL (E, 4, X); \
202 FP_PACK_RAW_E ((val), X); \
206 # define FP_PACK_EP(val, X) \
209 _FP_PACK_CANONICAL (E, 4, X); \
210 FP_PACK_RAW_EP ((val), X); \
214 # define FP_PACK_SEMIRAW_E(val, X) \
217 _FP_PACK_SEMIRAW (E, 4, X); \
218 FP_PACK_RAW_E ((val), X); \
222 # define FP_PACK_SEMIRAW_EP(val, X) \
225 _FP_PACK_SEMIRAW (E, 4, X); \
226 FP_PACK_RAW_EP ((val), X); \
230 # define FP_ISSIGNAN_E(X) _FP_ISSIGNAN (E, 4, X)
231 # define FP_NEG_E(R, X) _FP_NEG (E, 4, R, X)
232 # define FP_ADD_E(R, X, Y) _FP_ADD (E, 4, R, X, Y)
233 # define FP_SUB_E(R, X, Y) _FP_SUB (E, 4, R, X, Y)
234 # define FP_MUL_E(R, X, Y) _FP_MUL (E, 4, R, X, Y)
235 # define FP_DIV_E(R, X, Y) _FP_DIV (E, 4, R, X, Y)
236 # define FP_SQRT_E(R, X) _FP_SQRT (E, 4, R, X)
237 # define FP_FMA_E(R, X, Y, Z) _FP_FMA (E, 4, 8, R, X, Y, Z)
239 /* Square root algorithms:
240 We have just one right now, maybe Newton approximation
241 should be added for those machines where division is fast.
242 This has special _E version because standard _4 square
243 root would not work (it has to start normally with the
244 second word and not the first), but as we have to do it
245 anyway, we optimize it by doing most of the calculations
246 in two UWtype registers instead of four. */
248 # define _FP_SQRT_MEAT_E(R, S, T, X, q) \
251 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
252 _FP_FRAC_SRL_4 (X, (_FP_WORKBITS)); \
255 T##_f[1] = S##_f[1] + (q); \
256 if (T##_f[1] <= X##_f[1]) \
258 S##_f[1] = T##_f[1] + (q); \
259 X##_f[1] -= T##_f[1]; \
262 _FP_FRAC_SLL_2 (X, 1); \
265 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
268 T##_f[0] = S##_f[0] + (q); \
269 T##_f[1] = S##_f[1]; \
270 if (T##_f[1] < X##_f[1] \
271 || (T##_f[1] == X##_f[1] \
272 && T##_f[0] <= X##_f[0])) \
274 S##_f[0] = T##_f[0] + (q); \
275 S##_f[1] += (T##_f[0] > S##_f[0]); \
276 _FP_FRAC_DEC_2 (X, T); \
279 _FP_FRAC_SLL_2 (X, 1); \
282 _FP_FRAC_SLL_4 (R, (_FP_WORKBITS)); \
283 if (X##_f[0] | X##_f[1]) \
285 if (S##_f[1] < X##_f[1] \
286 || (S##_f[1] == X##_f[1] \
287 && S##_f[0] < X##_f[0])) \
288 R##_f[0] |= _FP_WORK_ROUND; \
289 R##_f[0] |= _FP_WORK_STICKY; \
294 # define FP_CMP_E(r, X, Y, un, ex) _FP_CMP (E, 4, (r), X, Y, (un), (ex))
295 # define FP_CMP_EQ_E(r, X, Y, ex) _FP_CMP_EQ (E, 4, (r), X, Y, (ex))
296 # define FP_CMP_UNORD_E(r, X, Y, ex) _FP_CMP_UNORD (E, 4, (r), X, Y, (ex))
298 # define FP_TO_INT_E(r, X, rsz, rsg) _FP_TO_INT (E, 4, (r), X, (rsz), (rsg))
299 # define FP_TO_INT_ROUND_E(r, X, rsz, rsg) \
300 _FP_TO_INT_ROUND (E, 4, (r), X, (rsz), (rsg))
301 # define FP_FROM_INT_E(X, r, rs, rt) _FP_FROM_INT (E, 4, X, (r), (rs), rt)
303 # define _FP_FRAC_HIGH_E(X) (X##_f[2])
304 # define _FP_FRAC_HIGH_RAW_E(X) (X##_f[1])
306 # define _FP_FRAC_HIGH_DW_E(X) (X##_f[4])
308 #else /* not _FP_W_TYPE_SIZE < 64 */
312 struct _FP_STRUCT_LAYOUT
314 # if __BYTE_ORDER == __BIG_ENDIAN
315 _FP_W_TYPE pad
: (_FP_W_TYPE_SIZE
- 1 - _FP_EXPBITS_E
);
317 unsigned exp
: _FP_EXPBITS_E
;
318 _FP_W_TYPE frac
: _FP_W_TYPE_SIZE
;
320 _FP_W_TYPE frac
: _FP_W_TYPE_SIZE
;
321 unsigned exp
: _FP_EXPBITS_E
;
327 # define FP_DECL_E(X) _FP_DECL (2, X)
329 # define FP_UNPACK_RAW_E(X, val) \
332 union _FP_UNION_E FP_UNPACK_RAW_E_flo; \
333 FP_UNPACK_RAW_E_flo.flt = (val); \
335 X##_f0 = FP_UNPACK_RAW_E_flo.bits.frac; \
336 X##_f0 &= ~_FP_IMPLBIT_E; \
338 X##_e = FP_UNPACK_RAW_E_flo.bits.exp; \
339 X##_s = FP_UNPACK_RAW_E_flo.bits.sign; \
343 # define FP_UNPACK_RAW_EP(X, val) \
346 union _FP_UNION_E *FP_UNPACK_RAW_EP_flo \
347 = (union _FP_UNION_E *) (val); \
349 X##_f0 = FP_UNPACK_RAW_EP_flo->bits.frac; \
350 X##_f0 &= ~_FP_IMPLBIT_E; \
352 X##_e = FP_UNPACK_RAW_EP_flo->bits.exp; \
353 X##_s = FP_UNPACK_RAW_EP_flo->bits.sign; \
357 # define FP_PACK_RAW_E(val, X) \
360 union _FP_UNION_E FP_PACK_RAW_E_flo; \
363 X##_f0 |= _FP_IMPLBIT_E; \
365 X##_f0 &= ~(_FP_IMPLBIT_E); \
366 FP_PACK_RAW_E_flo.bits.frac = X##_f0; \
367 FP_PACK_RAW_E_flo.bits.exp = X##_e; \
368 FP_PACK_RAW_E_flo.bits.sign = X##_s; \
370 (val) = FP_PACK_RAW_E_flo.flt; \
374 # define FP_PACK_RAW_EP(fs, val, X) \
377 if (!FP_INHIBIT_RESULTS) \
379 union _FP_UNION_E *FP_PACK_RAW_EP_flo \
380 = (union _FP_UNION_E *) (val); \
383 X##_f0 |= _FP_IMPLBIT_E; \
385 X##_f0 &= ~(_FP_IMPLBIT_E); \
386 FP_PACK_RAW_EP_flo->bits.frac = X##_f0; \
387 FP_PACK_RAW_EP_flo->bits.exp = X##_e; \
388 FP_PACK_RAW_EP_flo->bits.sign = X##_s; \
394 # define FP_UNPACK_E(X, val) \
397 FP_UNPACK_RAW_E (X, (val)); \
398 _FP_UNPACK_CANONICAL (E, 2, X); \
402 # define FP_UNPACK_EP(X, val) \
405 FP_UNPACK_RAW_EP (X, (val)); \
406 _FP_UNPACK_CANONICAL (E, 2, X); \
410 # define FP_UNPACK_SEMIRAW_E(X, val) \
413 FP_UNPACK_RAW_E (X, (val)); \
414 _FP_UNPACK_SEMIRAW (E, 2, X); \
418 # define FP_UNPACK_SEMIRAW_EP(X, val) \
421 FP_UNPACK_RAW_EP (X, (val)); \
422 _FP_UNPACK_SEMIRAW (E, 2, X); \
426 # define FP_PACK_E(val, X) \
429 _FP_PACK_CANONICAL (E, 2, X); \
430 FP_PACK_RAW_E ((val), X); \
434 # define FP_PACK_EP(val, X) \
437 _FP_PACK_CANONICAL (E, 2, X); \
438 FP_PACK_RAW_EP ((val), X); \
442 # define FP_PACK_SEMIRAW_E(val, X) \
445 _FP_PACK_SEMIRAW (E, 2, X); \
446 FP_PACK_RAW_E ((val), X); \
450 # define FP_PACK_SEMIRAW_EP(val, X) \
453 _FP_PACK_SEMIRAW (E, 2, X); \
454 FP_PACK_RAW_EP ((val), X); \
458 # define FP_ISSIGNAN_E(X) _FP_ISSIGNAN (E, 2, X)
459 # define FP_NEG_E(R, X) _FP_NEG (E, 2, R, X)
460 # define FP_ADD_E(R, X, Y) _FP_ADD (E, 2, R, X, Y)
461 # define FP_SUB_E(R, X, Y) _FP_SUB (E, 2, R, X, Y)
462 # define FP_MUL_E(R, X, Y) _FP_MUL (E, 2, R, X, Y)
463 # define FP_DIV_E(R, X, Y) _FP_DIV (E, 2, R, X, Y)
464 # define FP_SQRT_E(R, X) _FP_SQRT (E, 2, R, X)
465 # define FP_FMA_E(R, X, Y, Z) _FP_FMA (E, 2, 4, R, X, Y, Z)
467 /* Square root algorithms:
468 We have just one right now, maybe Newton approximation
469 should be added for those machines where division is fast.
470 We optimize it by doing most of the calculations
471 in one UWtype registers instead of two, although we don't
473 # define _FP_SQRT_MEAT_E(R, S, T, X, q) \
476 (q) = (_FP_W_TYPE) 1 << (_FP_W_TYPE_SIZE - 1); \
477 _FP_FRAC_SRL_2 (X, (_FP_WORKBITS)); \
480 T##_f0 = S##_f0 + (q); \
481 if (T##_f0 <= X##_f0) \
483 S##_f0 = T##_f0 + (q); \
487 _FP_FRAC_SLL_1 (X, 1); \
490 _FP_FRAC_SLL_2 (R, (_FP_WORKBITS)); \
493 if (S##_f0 < X##_f0) \
494 R##_f0 |= _FP_WORK_ROUND; \
495 R##_f0 |= _FP_WORK_STICKY; \
500 # define FP_CMP_E(r, X, Y, un, ex) _FP_CMP (E, 2, (r), X, Y, (un), (ex))
501 # define FP_CMP_EQ_E(r, X, Y, ex) _FP_CMP_EQ (E, 2, (r), X, Y, (ex))
502 # define FP_CMP_UNORD_E(r, X, Y, ex) _FP_CMP_UNORD (E, 2, (r), X, Y, (ex))
504 # define FP_TO_INT_E(r, X, rsz, rsg) _FP_TO_INT (E, 2, (r), X, (rsz), (rsg))
505 # define FP_TO_INT_ROUND_E(r, X, rsz, rsg) \
506 _FP_TO_INT_ROUND (E, 2, (r), X, (rsz), (rsg))
507 # define FP_FROM_INT_E(X, r, rs, rt) _FP_FROM_INT (E, 2, X, (r), (rs), rt)
509 # define _FP_FRAC_HIGH_E(X) (X##_f1)
510 # define _FP_FRAC_HIGH_RAW_E(X) (X##_f0)
512 # define _FP_FRAC_HIGH_DW_E(X) (X##_f[2])
514 #endif /* not _FP_W_TYPE_SIZE < 64 */
516 #endif /* !SOFT_FP_EXTENDED_H */