1 /* Software floating-point emulation. Common operations.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Richard Henderson (rth@cygnus.com),
5 Jakub Jelinek (jj@ultra.linux.cz),
6 David S. Miller (davem@redhat.com) and
7 Peter Maydell (pmaydell@chiark.greenend.org.uk).
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 In addition to the permissions in the GNU Lesser General Public
15 License, the Free Software Foundation gives you unlimited
16 permission to link the compiled version of this file into
17 combinations with other programs, and to distribute those
18 combinations without any restriction coming from the use of this
19 file. (The Lesser General Public License restrictions do apply in
20 other respects; for example, they cover modification of the file,
21 and distribution when not linked into a combine executable.)
23 The GNU C Library is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 Lesser General Public License for more details.
28 You should have received a copy of the GNU Lesser General Public
29 License along with the GNU C Library; if not, see
30 <http://www.gnu.org/licenses/>. */
32 #define _FP_DECL(wc, X) \
33 _FP_I_TYPE X##_c __attribute__ ((unused)); \
34 _FP_I_TYPE X##_s __attribute__ ((unused)); \
36 _FP_FRAC_DECL_##wc (X)
38 /* Test whether the qNaN bit denotes a signaling NaN. */
39 #define _FP_FRAC_SNANP(fs, X) \
41 ? (_FP_FRAC_HIGH_RAW_##fs (X) & _FP_QNANBIT_##fs) \
42 : !(_FP_FRAC_HIGH_RAW_##fs (X) & _FP_QNANBIT_##fs))
43 #define _FP_FRAC_SNANP_SEMIRAW(fs, X) \
45 ? (_FP_FRAC_HIGH_##fs (X) & _FP_QNANBIT_SH_##fs) \
46 : !(_FP_FRAC_HIGH_##fs (X) & _FP_QNANBIT_SH_##fs))
49 * Finish truly unpacking a native fp value by classifying the kind
50 * of fp value and normalizing both the exponent and the fraction.
53 #define _FP_UNPACK_CANONICAL(fs, wc, X) \
59 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
60 _FP_FRAC_SLL_##wc (X, _FP_WORKBITS); \
61 X##_e -= _FP_EXPBIAS_##fs; \
62 X##_c = FP_CLS_NORMAL; \
66 if (_FP_FRAC_ZEROP_##wc (X)) \
67 X##_c = FP_CLS_ZERO; \
70 /* a denormalized number */ \
72 _FP_FRAC_CLZ_##wc (_shift, X); \
73 _shift -= _FP_FRACXBITS_##fs; \
74 _FP_FRAC_SLL_##wc (X, (_shift+_FP_WORKBITS)); \
75 X##_e -= _FP_EXPBIAS_##fs - 1 + _shift; \
76 X##_c = FP_CLS_NORMAL; \
77 FP_SET_EXCEPTION (FP_EX_DENORM); \
81 case _FP_EXPMAX_##fs: \
82 if (_FP_FRAC_ZEROP_##wc (X)) \
87 /* Check for signaling NaN */ \
88 if (_FP_FRAC_SNANP (fs, X)) \
89 FP_SET_EXCEPTION (FP_EX_INVALID); \
96 /* Finish unpacking an fp value in semi-raw mode: the mantissa is
97 shifted by _FP_WORKBITS but the implicit MSB is not inserted and
98 other classification is not done. */
99 #define _FP_UNPACK_SEMIRAW(fs, wc, X) _FP_FRAC_SLL_##wc (X, _FP_WORKBITS)
101 /* A semi-raw value has overflowed to infinity. Adjust the mantissa
102 and exponent appropriately. */
103 #define _FP_OVERFLOW_SEMIRAW(fs, wc, X) \
106 if (FP_ROUNDMODE == FP_RND_NEAREST \
107 || (FP_ROUNDMODE == FP_RND_PINF && !X##_s) \
108 || (FP_ROUNDMODE == FP_RND_MINF && X##_s)) \
110 X##_e = _FP_EXPMAX_##fs; \
111 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
115 X##_e = _FP_EXPMAX_##fs - 1; \
116 _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
118 FP_SET_EXCEPTION (FP_EX_INEXACT); \
119 FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
123 /* Check for a semi-raw value being a signaling NaN and raise the
124 invalid exception if so. */
125 #define _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X) \
128 if (X##_e == _FP_EXPMAX_##fs \
129 && !_FP_FRAC_ZEROP_##wc (X) \
130 && _FP_FRAC_SNANP_SEMIRAW (fs, X)) \
131 FP_SET_EXCEPTION (FP_EX_INVALID); \
135 /* Choose a NaN result from an operation on two semi-raw NaN
137 #define _FP_CHOOSENAN_SEMIRAW(fs, wc, R, X, Y, OP) \
140 /* _FP_CHOOSENAN expects raw values, so shift as required. */ \
141 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
142 _FP_FRAC_SRL_##wc (Y, _FP_WORKBITS); \
143 _FP_CHOOSENAN (fs, wc, R, X, Y, OP); \
144 _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
148 /* Make the fractional part a quiet NaN, preserving the payload
149 if possible, otherwise make it the canonical quiet NaN and set
150 the sign bit accordingly. */
151 #define _FP_SETQNAN(fs, wc, X) \
154 if (_FP_QNANNEGATEDP) \
156 _FP_FRAC_HIGH_RAW_##fs (X) &= _FP_QNANBIT_##fs - 1; \
157 if (_FP_FRAC_ZEROP_##wc (X)) \
159 X##_s = _FP_NANSIGN_##fs; \
160 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
164 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_QNANBIT_##fs; \
167 #define _FP_SETQNAN_SEMIRAW(fs, wc, X) \
170 if (_FP_QNANNEGATEDP) \
172 _FP_FRAC_HIGH_##fs (X) &= _FP_QNANBIT_SH_##fs - 1; \
173 if (_FP_FRAC_ZEROP_##wc (X)) \
175 X##_s = _FP_NANSIGN_##fs; \
176 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
177 _FP_FRAC_SLL_##wc (X, _FP_WORKBITS); \
181 _FP_FRAC_HIGH_##fs (X) |= _FP_QNANBIT_SH_##fs; \
185 /* Test whether a biased exponent is normal (not zero or maximum). */
186 #define _FP_EXP_NORMAL(fs, wc, X) (((X##_e + 1) & _FP_EXPMAX_##fs) > 1)
188 /* Prepare to pack an fp value in semi-raw mode: the mantissa is
189 rounded and shifted right, with the rounding possibly increasing
190 the exponent (including changing a finite value to infinity). */
191 #define _FP_PACK_SEMIRAW(fs, wc, X) \
195 if (X##_e == 0 && !_FP_FRAC_ZEROP_##wc (X)) \
197 if ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
198 || (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW)) \
199 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
201 if (_FP_FRAC_HIGH_##fs (X) \
202 & (_FP_OVERFLOW_##fs >> 1)) \
204 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_OVERFLOW_##fs >> 1); \
206 if (X##_e == _FP_EXPMAX_##fs) \
207 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
209 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
210 if (X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
212 if (!_FP_KEEPNANFRACP) \
214 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
215 X##_s = _FP_NANSIGN_##fs; \
218 _FP_SETQNAN (fs, wc, X); \
224 * Before packing the bits back into the native fp result, take care
225 * of such mundane things as rounding and overflow. Also, for some
226 * kinds of fp values, the original parts may not have been fully
227 * extracted -- but that is ok, we can regenerate them now.
230 #define _FP_PACK_CANONICAL(fs, wc, X) \
235 case FP_CLS_NORMAL: \
236 X##_e += _FP_EXPBIAS_##fs; \
240 if (_FP_FRAC_OVERP_##wc (fs, X)) \
242 _FP_FRAC_CLEAR_OVERP_##wc (fs, X); \
245 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
246 if (X##_e >= _FP_EXPMAX_##fs) \
249 switch (FP_ROUNDMODE) \
251 case FP_RND_NEAREST: \
252 X##_c = FP_CLS_INF; \
256 X##_c = FP_CLS_INF; \
260 X##_c = FP_CLS_INF; \
263 if (X##_c == FP_CLS_INF) \
265 /* Overflow to infinity */ \
266 X##_e = _FP_EXPMAX_##fs; \
267 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
271 /* Overflow to maximum normal */ \
272 X##_e = _FP_EXPMAX_##fs - 1; \
273 _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
275 FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
276 FP_SET_EXCEPTION (FP_EX_INEXACT); \
281 /* we've got a denormalized number */ \
282 X##_e = -X##_e + 1; \
283 if (X##_e <= _FP_WFRACBITS_##fs) \
285 _FP_FRAC_SRS_##wc (X, X##_e, _FP_WFRACBITS_##fs); \
287 if (_FP_FRAC_HIGH_##fs (X) \
288 & (_FP_OVERFLOW_##fs >> 1)) \
291 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
292 FP_SET_EXCEPTION (FP_EX_INEXACT); \
297 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
299 if ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
300 || (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW)) \
301 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
305 /* underflow to zero */ \
307 if (!_FP_FRAC_ZEROP_##wc (X)) \
309 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
311 _FP_FRAC_LOW_##wc (X) >>= (_FP_WORKBITS); \
313 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
320 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
324 X##_e = _FP_EXPMAX_##fs; \
325 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
329 X##_e = _FP_EXPMAX_##fs; \
330 if (!_FP_KEEPNANFRACP) \
332 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
333 X##_s = _FP_NANSIGN_##fs; \
336 _FP_SETQNAN (fs, wc, X); \
342 /* This one accepts raw argument and not cooked, returns
343 * 1 if X is a signaling NaN.
345 #define _FP_ISSIGNAN(fs, wc, X) \
348 if (X##_e == _FP_EXPMAX_##fs) \
350 if (!_FP_FRAC_ZEROP_##wc (X) \
351 && _FP_FRAC_SNANP (fs, X)) \
361 /* Addition on semi-raw values. */
362 #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) \
365 if (X##_s == Y##_s) \
369 int ediff = X##_e - Y##_e; \
375 /* Y is zero or denormalized. */ \
376 if (_FP_FRAC_ZEROP_##wc (Y)) \
378 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
379 _FP_FRAC_COPY_##wc (R, X); \
384 FP_SET_EXCEPTION (FP_EX_DENORM); \
388 _FP_FRAC_ADD_##wc (R, X, Y); \
391 if (X##_e == _FP_EXPMAX_##fs) \
393 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
394 _FP_FRAC_COPY_##wc (R, X); \
400 else if (X##_e == _FP_EXPMAX_##fs) \
402 /* X is NaN or Inf, Y is normal. */ \
403 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
404 _FP_FRAC_COPY_##wc (R, X); \
408 /* Insert implicit MSB of Y. */ \
409 _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
412 /* Shift the mantissa of Y to the right EDIFF steps; \
413 remember to account later for the implicit MSB of X. */ \
414 if (ediff <= _FP_WFRACBITS_##fs) \
415 _FP_FRAC_SRS_##wc (Y, ediff, _FP_WFRACBITS_##fs); \
416 else if (!_FP_FRAC_ZEROP_##wc (Y)) \
417 _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
418 _FP_FRAC_ADD_##wc (R, X, Y); \
420 else if (ediff < 0) \
426 /* X is zero or denormalized. */ \
427 if (_FP_FRAC_ZEROP_##wc (X)) \
429 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
430 _FP_FRAC_COPY_##wc (R, Y); \
435 FP_SET_EXCEPTION (FP_EX_DENORM); \
439 _FP_FRAC_ADD_##wc (R, Y, X); \
442 if (Y##_e == _FP_EXPMAX_##fs) \
444 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
445 _FP_FRAC_COPY_##wc (R, Y); \
451 else if (Y##_e == _FP_EXPMAX_##fs) \
453 /* Y is NaN or Inf, X is normal. */ \
454 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
455 _FP_FRAC_COPY_##wc (R, Y); \
459 /* Insert implicit MSB of X. */ \
460 _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
463 /* Shift the mantissa of X to the right EDIFF steps; \
464 remember to account later for the implicit MSB of Y. */ \
465 if (ediff <= _FP_WFRACBITS_##fs) \
466 _FP_FRAC_SRS_##wc (X, ediff, _FP_WFRACBITS_##fs); \
467 else if (!_FP_FRAC_ZEROP_##wc (X)) \
468 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
469 _FP_FRAC_ADD_##wc (R, Y, X); \
474 if (!_FP_EXP_NORMAL (fs, wc, X)) \
478 /* X and Y are zero or denormalized. */ \
480 if (_FP_FRAC_ZEROP_##wc (X)) \
482 if (!_FP_FRAC_ZEROP_##wc (Y)) \
483 FP_SET_EXCEPTION (FP_EX_DENORM); \
484 _FP_FRAC_COPY_##wc (R, Y); \
487 else if (_FP_FRAC_ZEROP_##wc (Y)) \
489 FP_SET_EXCEPTION (FP_EX_DENORM); \
490 _FP_FRAC_COPY_##wc (R, X); \
495 FP_SET_EXCEPTION (FP_EX_DENORM); \
496 _FP_FRAC_ADD_##wc (R, X, Y); \
497 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
499 /* Normalized result. */ \
500 _FP_FRAC_HIGH_##fs (R) \
501 &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
509 /* X and Y are NaN or Inf. */ \
510 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
511 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
512 R##_e = _FP_EXPMAX_##fs; \
513 if (_FP_FRAC_ZEROP_##wc (X)) \
514 _FP_FRAC_COPY_##wc (R, Y); \
515 else if (_FP_FRAC_ZEROP_##wc (Y)) \
516 _FP_FRAC_COPY_##wc (R, X); \
518 _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
522 /* The exponents of X and Y, both normal, are equal. The \
523 implicit MSBs will always add to increase the \
525 _FP_FRAC_ADD_##wc (R, X, Y); \
527 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
528 if (R##_e == _FP_EXPMAX_##fs) \
529 /* Overflow to infinity (depending on rounding mode). */ \
530 _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
534 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
537 _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
539 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
540 if (R##_e == _FP_EXPMAX_##fs) \
541 /* Overflow to infinity (depending on rounding mode). */ \
542 _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
549 int ediff = X##_e - Y##_e; \
556 /* Y is zero or denormalized. */ \
557 if (_FP_FRAC_ZEROP_##wc (Y)) \
559 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
560 _FP_FRAC_COPY_##wc (R, X); \
565 FP_SET_EXCEPTION (FP_EX_DENORM); \
569 _FP_FRAC_SUB_##wc (R, X, Y); \
572 if (X##_e == _FP_EXPMAX_##fs) \
574 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
575 _FP_FRAC_COPY_##wc (R, X); \
581 else if (X##_e == _FP_EXPMAX_##fs) \
583 /* X is NaN or Inf, Y is normal. */ \
584 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
585 _FP_FRAC_COPY_##wc (R, X); \
589 /* Insert implicit MSB of Y. */ \
590 _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
593 /* Shift the mantissa of Y to the right EDIFF steps; \
594 remember to account later for the implicit MSB of X. */ \
595 if (ediff <= _FP_WFRACBITS_##fs) \
596 _FP_FRAC_SRS_##wc (Y, ediff, _FP_WFRACBITS_##fs); \
597 else if (!_FP_FRAC_ZEROP_##wc (Y)) \
598 _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
599 _FP_FRAC_SUB_##wc (R, X, Y); \
601 else if (ediff < 0) \
608 /* X is zero or denormalized. */ \
609 if (_FP_FRAC_ZEROP_##wc (X)) \
611 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
612 _FP_FRAC_COPY_##wc (R, Y); \
617 FP_SET_EXCEPTION (FP_EX_DENORM); \
621 _FP_FRAC_SUB_##wc (R, Y, X); \
624 if (Y##_e == _FP_EXPMAX_##fs) \
626 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
627 _FP_FRAC_COPY_##wc (R, Y); \
633 else if (Y##_e == _FP_EXPMAX_##fs) \
635 /* Y is NaN or Inf, X is normal. */ \
636 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
637 _FP_FRAC_COPY_##wc (R, Y); \
641 /* Insert implicit MSB of X. */ \
642 _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
645 /* Shift the mantissa of X to the right EDIFF steps; \
646 remember to account later for the implicit MSB of Y. */ \
647 if (ediff <= _FP_WFRACBITS_##fs) \
648 _FP_FRAC_SRS_##wc (X, ediff, _FP_WFRACBITS_##fs); \
649 else if (!_FP_FRAC_ZEROP_##wc (X)) \
650 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
651 _FP_FRAC_SUB_##wc (R, Y, X); \
656 if (!_FP_EXP_NORMAL (fs, wc, X)) \
660 /* X and Y are zero or denormalized. */ \
662 if (_FP_FRAC_ZEROP_##wc (X)) \
664 _FP_FRAC_COPY_##wc (R, Y); \
665 if (_FP_FRAC_ZEROP_##wc (Y)) \
666 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
669 FP_SET_EXCEPTION (FP_EX_DENORM); \
674 else if (_FP_FRAC_ZEROP_##wc (Y)) \
676 FP_SET_EXCEPTION (FP_EX_DENORM); \
677 _FP_FRAC_COPY_##wc (R, X); \
683 FP_SET_EXCEPTION (FP_EX_DENORM); \
684 _FP_FRAC_SUB_##wc (R, X, Y); \
686 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
688 /* |X| < |Y|, negate result. */ \
689 _FP_FRAC_SUB_##wc (R, Y, X); \
692 else if (_FP_FRAC_ZEROP_##wc (R)) \
693 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
699 /* X and Y are NaN or Inf, of opposite signs. */ \
700 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
701 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
702 R##_e = _FP_EXPMAX_##fs; \
703 if (_FP_FRAC_ZEROP_##wc (X)) \
705 if (_FP_FRAC_ZEROP_##wc (Y)) \
708 R##_s = _FP_NANSIGN_##fs; \
709 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
710 _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
711 FP_SET_EXCEPTION (FP_EX_INVALID); \
717 _FP_FRAC_COPY_##wc (R, Y); \
722 if (_FP_FRAC_ZEROP_##wc (Y)) \
726 _FP_FRAC_COPY_##wc (R, X); \
731 _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
737 /* The exponents of X and Y, both normal, are equal. The \
738 implicit MSBs cancel. */ \
740 _FP_FRAC_SUB_##wc (R, X, Y); \
742 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
744 /* |X| < |Y|, negate result. */ \
745 _FP_FRAC_SUB_##wc (R, Y, X); \
748 else if (_FP_FRAC_ZEROP_##wc (R)) \
751 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
757 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
760 /* Carry into most significant bit of larger one of X and Y, \
761 canceling it; renormalize. */ \
762 _FP_FRAC_HIGH_##fs (R) &= _FP_IMPLBIT_SH_##fs - 1; \
764 _FP_FRAC_CLZ_##wc (diff, R); \
765 diff -= _FP_WFRACXBITS_##fs; \
766 _FP_FRAC_SLL_##wc (R, diff); \
769 /* R is denormalized. */ \
770 diff = diff - R##_e + 1; \
771 _FP_FRAC_SRS_##wc (R, diff, _FP_WFRACBITS_##fs); \
777 _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
785 #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL (fs, wc, R, X, Y, '+')
786 #define _FP_SUB(fs, wc, R, X, Y) \
789 if (!(Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
791 _FP_ADD_INTERNAL (fs, wc, R, X, Y, '-'); \
797 * Main negation routine. The input value is raw.
800 #define _FP_NEG(fs, wc, R, X) \
803 _FP_FRAC_COPY_##wc (R, X); \
811 * Main multiplication routine. The input values should be cooked.
814 #define _FP_MUL(fs, wc, R, X, Y) \
817 R##_s = X##_s ^ Y##_s; \
818 R##_e = X##_e + Y##_e + 1; \
819 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
821 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
822 R##_c = FP_CLS_NORMAL; \
824 _FP_MUL_MEAT_##fs (R, X, Y); \
826 if (_FP_FRAC_OVERP_##wc (fs, R)) \
827 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
832 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
833 _FP_CHOOSENAN (fs, wc, R, X, Y, '*'); \
836 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
837 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
838 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
841 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
842 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
843 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
844 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
845 _FP_FRAC_COPY_##wc (R, X); \
849 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
850 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
851 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
854 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
855 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
856 _FP_FRAC_COPY_##wc (R, Y); \
860 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
861 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
862 R##_s = _FP_NANSIGN_##fs; \
863 R##_c = FP_CLS_NAN; \
864 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
865 FP_SET_EXCEPTION (FP_EX_INVALID); \
875 /* Fused multiply-add. The input values should be cooked. */
877 #define _FP_FMA(fs, wc, dwc, R, X, Y, Z) \
881 T##_s = X##_s ^ Y##_s; \
882 T##_e = X##_e + Y##_e + 1; \
883 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
885 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
891 _FP_FRAC_COPY_##wc (R, Z); \
896 R##_c = FP_CLS_NORMAL; \
900 _FP_MUL_MEAT_##fs (R, X, Y); \
902 if (_FP_FRAC_OVERP_##wc (fs, R)) \
903 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
908 case FP_CLS_NORMAL:; \
909 _FP_FRAC_DECL_##dwc (TD); \
910 _FP_FRAC_DECL_##dwc (ZD); \
911 _FP_FRAC_DECL_##dwc (RD); \
912 _FP_MUL_MEAT_DW_##fs (TD, X, Y); \
914 int tsh = _FP_FRAC_HIGHBIT_DW_##dwc (fs, TD) == 0; \
916 int ediff = T##_e - Z##_e; \
919 int shift = _FP_WFRACBITS_##fs - tsh - ediff; \
920 if (shift <= -_FP_WFRACBITS_##fs) \
921 _FP_FRAC_SET_##dwc (ZD, _FP_MINFRAC_##dwc); \
924 _FP_FRAC_COPY_##dwc##_##wc (ZD, Z); \
926 _FP_FRAC_SRS_##dwc (ZD, -shift, \
927 _FP_WFRACBITS_DW_##fs); \
928 else if (shift > 0) \
929 _FP_FRAC_SLL_##dwc (ZD, shift); \
932 if (T##_s == Z##_s) \
933 _FP_FRAC_ADD_##dwc (RD, TD, ZD); \
936 _FP_FRAC_SUB_##dwc (RD, TD, ZD); \
937 if (_FP_FRAC_NEGP_##dwc (RD)) \
940 _FP_FRAC_SUB_##dwc (RD, ZD, TD); \
948 _FP_FRAC_COPY_##dwc##_##wc (ZD, Z); \
949 _FP_FRAC_SLL_##dwc (ZD, _FP_WFRACBITS_##fs); \
950 int shift = -ediff - tsh; \
951 if (shift >= _FP_WFRACBITS_DW_##fs) \
952 _FP_FRAC_SET_##dwc (TD, _FP_MINFRAC_##dwc); \
953 else if (shift > 0) \
954 _FP_FRAC_SRS_##dwc (TD, shift, \
955 _FP_WFRACBITS_DW_##fs); \
956 if (Z##_s == T##_s) \
957 _FP_FRAC_ADD_##dwc (RD, ZD, TD); \
959 _FP_FRAC_SUB_##dwc (RD, ZD, TD); \
961 if (_FP_FRAC_ZEROP_##dwc (RD)) \
963 if (T##_s == Z##_s) \
966 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
967 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
968 R##_c = FP_CLS_ZERO; \
973 _FP_FRAC_CLZ_##dwc (rlz, RD); \
974 rlz -= _FP_WFRACXBITS_DW_##fs; \
976 int shift = _FP_WFRACBITS_##fs - rlz; \
978 _FP_FRAC_SRS_##dwc (RD, shift, \
979 _FP_WFRACBITS_DW_##fs); \
980 else if (shift < 0) \
981 _FP_FRAC_SLL_##dwc (RD, -shift); \
982 _FP_FRAC_COPY_##wc##_##dwc (R, RD); \
983 R##_c = FP_CLS_NORMAL; \
989 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
990 _FP_CHOOSENAN (fs, wc, T, X, Y, '*'); \
993 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
994 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
995 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
998 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
999 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1000 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1001 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1002 _FP_FRAC_COPY_##wc (T, X); \
1006 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
1007 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1008 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1011 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
1012 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
1013 _FP_FRAC_COPY_##wc (T, Y); \
1017 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1018 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1019 T##_s = _FP_NANSIGN_##fs; \
1020 T##_c = FP_CLS_NAN; \
1021 _FP_FRAC_SET_##wc (T, _FP_NANFRAC_##fs); \
1022 FP_SET_EXCEPTION (FP_EX_INVALID); \
1029 /* T = X * Y is zero, infinity or NaN. */ \
1030 switch (_FP_CLS_COMBINE (T##_c, Z##_c)) \
1032 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1033 _FP_CHOOSENAN (fs, wc, R, T, Z, '+'); \
1036 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1037 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1038 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1039 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1040 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1042 _FP_FRAC_COPY_##wc (R, T); \
1046 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1047 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1048 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1049 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1051 _FP_FRAC_COPY_##wc (R, Z); \
1055 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1056 if (T##_s == Z##_s) \
1059 _FP_FRAC_COPY_##wc (R, Z); \
1064 R##_s = _FP_NANSIGN_##fs; \
1065 R##_c = FP_CLS_NAN; \
1066 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1067 FP_SET_EXCEPTION (FP_EX_INVALID); \
1071 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1072 if (T##_s == Z##_s) \
1075 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
1076 _FP_FRAC_COPY_##wc (R, Z); \
1089 * Main division routine. The input values should be cooked.
1092 #define _FP_DIV(fs, wc, R, X, Y) \
1095 R##_s = X##_s ^ Y##_s; \
1096 R##_e = X##_e - Y##_e; \
1097 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
1099 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
1100 R##_c = FP_CLS_NORMAL; \
1102 _FP_DIV_MEAT_##fs (R, X, Y); \
1105 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1106 _FP_CHOOSENAN (fs, wc, R, X, Y, '/'); \
1109 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1110 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1111 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1113 _FP_FRAC_COPY_##wc (R, X); \
1117 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
1118 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1119 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1121 _FP_FRAC_COPY_##wc (R, Y); \
1125 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
1126 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1127 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1128 R##_c = FP_CLS_ZERO; \
1131 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
1132 FP_SET_EXCEPTION (FP_EX_DIVZERO); \
1133 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1134 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1135 R##_c = FP_CLS_INF; \
1138 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1139 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1140 R##_s = _FP_NANSIGN_##fs; \
1141 R##_c = FP_CLS_NAN; \
1142 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1143 FP_SET_EXCEPTION (FP_EX_INVALID); \
1154 * Main differential comparison routine. The inputs should be raw not
1155 * cooked. The return is -1,0,1 for normal values, 2 otherwise.
1158 #define _FP_CMP(fs, wc, ret, X, Y, un) \
1161 /* NANs are unordered */ \
1162 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1163 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1172 __is_zero_x = (!X##_e && _FP_FRAC_ZEROP_##wc (X)) ? 1 : 0; \
1173 __is_zero_y = (!Y##_e && _FP_FRAC_ZEROP_##wc (Y)) ? 1 : 0; \
1175 if (__is_zero_x && __is_zero_y) \
1177 else if (__is_zero_x) \
1178 ret = Y##_s ? 1 : -1; \
1179 else if (__is_zero_y) \
1180 ret = X##_s ? -1 : 1; \
1181 else if (X##_s != Y##_s) \
1182 ret = X##_s ? -1 : 1; \
1183 else if (X##_e > Y##_e) \
1184 ret = X##_s ? -1 : 1; \
1185 else if (X##_e < Y##_e) \
1186 ret = X##_s ? 1 : -1; \
1187 else if (_FP_FRAC_GT_##wc (X, Y)) \
1188 ret = X##_s ? -1 : 1; \
1189 else if (_FP_FRAC_GT_##wc (Y, X)) \
1190 ret = X##_s ? 1 : -1; \
1198 /* Simplification for strict equality. */
1200 #define _FP_CMP_EQ(fs, wc, ret, X, Y) \
1203 /* NANs are unordered */ \
1204 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1205 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1211 ret = !(X##_e == Y##_e \
1212 && _FP_FRAC_EQ_##wc (X, Y) \
1213 && (X##_s == Y##_s || (!X##_e && _FP_FRAC_ZEROP_##wc (X)))); \
1218 /* Version to test unordered. */
1220 #define _FP_CMP_UNORD(fs, wc, ret, X, Y) \
1223 ret = ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1224 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))); \
1229 * Main square root routine. The input value should be cooked.
1232 #define _FP_SQRT(fs, wc, R, X) \
1235 _FP_FRAC_DECL_##wc (T); \
1236 _FP_FRAC_DECL_##wc (S); \
1241 _FP_FRAC_COPY_##wc (R, X); \
1243 R##_c = FP_CLS_NAN; \
1248 R##_s = _FP_NANSIGN_##fs; \
1249 R##_c = FP_CLS_NAN; /* NAN */ \
1250 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1251 FP_SET_EXCEPTION (FP_EX_INVALID); \
1256 R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
1261 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
1263 case FP_CLS_NORMAL: \
1267 R##_c = FP_CLS_NAN; /* NAN */ \
1268 R##_s = _FP_NANSIGN_##fs; \
1269 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1270 FP_SET_EXCEPTION (FP_EX_INVALID); \
1273 R##_c = FP_CLS_NORMAL; \
1275 _FP_FRAC_SLL_##wc (X, 1); \
1276 R##_e = X##_e >> 1; \
1277 _FP_FRAC_SET_##wc (S, _FP_ZEROFRAC_##wc); \
1278 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1279 q = _FP_OVERFLOW_##fs >> 1; \
1280 _FP_SQRT_MEAT_##wc (R, S, T, X, q); \
1286 * Convert from FP to integer. Input is raw.
1289 /* RSIGNED can have following values:
1290 * 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
1291 * the result is either 0 or (2^rsize)-1 depending on the sign in such
1293 * 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
1294 * NV is set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1295 * depending on the sign in such case.
1296 * -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
1297 * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1298 * depending on the sign in such case.
1300 #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
1303 if (X##_e < _FP_EXPBIAS_##fs) \
1308 if (!_FP_FRAC_ZEROP_##wc (X)) \
1310 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1311 FP_SET_EXCEPTION (FP_EX_DENORM); \
1315 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1317 else if (X##_e >= _FP_EXPBIAS_##fs + rsize - (rsigned > 0 || X##_s) \
1318 || (!rsigned && X##_s)) \
1320 /* Overflow or converting to the most negative integer. */ \
1332 if (rsigned && X##_s && X##_e == _FP_EXPBIAS_##fs + rsize - 1) \
1334 /* Possibly converting to most negative integer; check the \
1337 (void) ((_FP_FRACBITS_##fs > rsize) \
1339 _FP_FRAC_SRST_##wc (X, inexact, \
1340 _FP_FRACBITS_##fs - rsize, \
1341 _FP_FRACBITS_##fs); \
1345 if (!_FP_FRAC_ZEROP_##wc (X)) \
1346 FP_SET_EXCEPTION (FP_EX_INVALID); \
1348 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1351 FP_SET_EXCEPTION (FP_EX_INVALID); \
1355 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
1356 if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
1358 _FP_FRAC_ASSEMBLE_##wc (r, X, rsize); \
1359 r <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
1364 _FP_FRAC_SRST_##wc (X, inexact, \
1365 (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs - 1 \
1367 _FP_FRACBITS_##fs); \
1369 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1370 _FP_FRAC_ASSEMBLE_##wc (r, X, rsize); \
1372 if (rsigned && X##_s) \
1378 /* Convert integer to fp. Output is raw. RTYPE is unsigned even if
1380 #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
1387 if ((X##_s = (r < 0))) \
1391 (void) ((rsize <= _FP_W_TYPE_SIZE) \
1394 __FP_CLZ (lz_, (_FP_W_TYPE) ur_); \
1395 X##_e = _FP_EXPBIAS_##fs + _FP_W_TYPE_SIZE - 1 - lz_; \
1397 : ((rsize <= 2 * _FP_W_TYPE_SIZE) \
1401 (_FP_W_TYPE) (ur_ >> _FP_W_TYPE_SIZE), \
1402 (_FP_W_TYPE) ur_); \
1403 X##_e = (_FP_EXPBIAS_##fs + 2 * _FP_W_TYPE_SIZE - 1 \
1406 : (abort (), 0))); \
1408 if (rsize - 1 + _FP_EXPBIAS_##fs >= _FP_EXPMAX_##fs \
1409 && X##_e >= _FP_EXPMAX_##fs) \
1411 /* Exponent too big; overflow to infinity. (May also \
1412 happen after rounding below.) */ \
1413 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
1414 goto pack_semiraw; \
1417 if (rsize <= _FP_FRACBITS_##fs \
1418 || X##_e < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs) \
1420 /* Exactly representable; shift left. */ \
1421 _FP_FRAC_DISASSEMBLE_##wc (X, ur_, rsize); \
1422 if (_FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1 - X##_e > 0) \
1423 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1424 + _FP_FRACBITS_##fs - 1 - X##_e)); \
1428 /* More bits in integer than in floating type; need to \
1430 if (_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 < X##_e) \
1431 ur_ = ((ur_ >> (X##_e - _FP_EXPBIAS_##fs \
1432 - _FP_WFRACBITS_##fs + 1)) \
1433 | ((ur_ << (rsize - (X##_e - _FP_EXPBIAS_##fs \
1434 - _FP_WFRACBITS_##fs + 1))) \
1436 _FP_FRAC_DISASSEMBLE_##wc (X, ur_, rsize); \
1437 if ((_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 - X##_e) > 0) \
1438 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1439 + _FP_WFRACBITS_##fs - 1 - X##_e)); \
1440 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
1442 _FP_PACK_SEMIRAW (fs, wc, X); \
1449 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
1455 /* Extend from a narrower floating-point format to a wider one. Input
1456 and output are raw. */
1457 #define FP_EXTEND(dfs, sfs, dwc, swc, D, S) \
1460 if (_FP_FRACBITS_##dfs < _FP_FRACBITS_##sfs \
1461 || (_FP_EXPMAX_##dfs - _FP_EXPBIAS_##dfs \
1462 < _FP_EXPMAX_##sfs - _FP_EXPBIAS_##sfs) \
1463 || (_FP_EXPBIAS_##dfs < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1 \
1464 && _FP_EXPBIAS_##dfs != _FP_EXPBIAS_##sfs)) \
1467 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1468 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1470 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1471 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs - _FP_FRACBITS_##sfs)); \
1477 if (_FP_FRAC_ZEROP_##swc (S)) \
1479 else if (_FP_EXPBIAS_##dfs \
1480 < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1) \
1482 FP_SET_EXCEPTION (FP_EX_DENORM); \
1483 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1484 - _FP_FRACBITS_##sfs)); \
1490 FP_SET_EXCEPTION (FP_EX_DENORM); \
1491 _FP_FRAC_CLZ_##swc (_lz, S); \
1492 _FP_FRAC_SLL_##dwc (D, \
1493 _lz + _FP_FRACBITS_##dfs \
1494 - _FP_FRACTBITS_##sfs); \
1495 D##_e = (_FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs + 1 \
1496 + _FP_FRACXBITS_##sfs - _lz); \
1501 D##_e = _FP_EXPMAX_##dfs; \
1502 if (!_FP_FRAC_ZEROP_##swc (S)) \
1504 if (_FP_FRAC_SNANP (sfs, S)) \
1505 FP_SET_EXCEPTION (FP_EX_INVALID); \
1506 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1507 - _FP_FRACBITS_##sfs)); \
1508 _FP_SETQNAN (dfs, dwc, D); \
1515 /* Truncate from a wider floating-point format to a narrower one.
1516 Input and output are semi-raw. */
1517 #define FP_TRUNC(dfs, sfs, dwc, swc, D, S) \
1520 if (_FP_FRACBITS_##sfs < _FP_FRACBITS_##dfs \
1521 || (_FP_EXPBIAS_##sfs < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1 \
1522 && _FP_EXPBIAS_##sfs != _FP_EXPBIAS_##dfs)) \
1525 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1527 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1528 if (D##_e >= _FP_EXPMAX_##dfs) \
1529 _FP_OVERFLOW_SEMIRAW (dfs, dwc, D); \
1534 if (D##_e < 1 - _FP_FRACBITS_##dfs) \
1536 _FP_FRAC_SET_##swc (S, _FP_ZEROFRAC_##swc); \
1537 _FP_FRAC_LOW_##swc (S) |= 1; \
1541 _FP_FRAC_HIGH_##sfs (S) |= _FP_IMPLBIT_SH_##sfs; \
1542 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1543 - _FP_WFRACBITS_##dfs \
1545 _FP_WFRACBITS_##sfs); \
1550 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1551 - _FP_WFRACBITS_##dfs), \
1552 _FP_WFRACBITS_##sfs); \
1553 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1561 if (_FP_FRAC_ZEROP_##swc (S)) \
1562 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1565 FP_SET_EXCEPTION (FP_EX_DENORM); \
1566 if (_FP_EXPBIAS_##sfs \
1567 < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1) \
1569 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1570 - _FP_WFRACBITS_##dfs), \
1571 _FP_WFRACBITS_##sfs); \
1572 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1576 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1577 _FP_FRAC_LOW_##dwc (D) |= 1; \
1583 D##_e = _FP_EXPMAX_##dfs; \
1584 if (_FP_FRAC_ZEROP_##swc (S)) \
1585 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1588 _FP_CHECK_SIGNAN_SEMIRAW (sfs, swc, S); \
1589 _FP_FRAC_SRL_##swc (S, (_FP_WFRACBITS_##sfs \
1590 - _FP_WFRACBITS_##dfs)); \
1591 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1592 /* Semi-raw NaN must have all workbits cleared. */ \
1593 _FP_FRAC_LOW_##dwc (D) \
1594 &= ~(_FP_W_TYPE) ((1 << _FP_WORKBITS) - 1); \
1595 _FP_SETQNAN_SEMIRAW (dfs, dwc, D); \
1603 * Helper primitives.
1606 /* Count leading zeros in a word. */
1609 /* GCC 3.4 and later provide the builtins for us. */
1610 # define __FP_CLZ(r, x) \
1613 if (sizeof (_FP_W_TYPE) == sizeof (unsigned int)) \
1614 r = __builtin_clz (x); \
1615 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long)) \
1616 r = __builtin_clzl (x); \
1617 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long long)) \
1618 r = __builtin_clzll (x); \
1623 #endif /* ndef __FP_CLZ */
1625 #define _FP_DIV_HELP_imm(q, r, n, d) \
1628 q = n / d, r = n % d; \
1633 /* A restoring bit-by-bit division primitive. */
1635 #define _FP_DIV_MEAT_N_loop(fs, wc, R, X, Y) \
1638 int count = _FP_WFRACBITS_##fs; \
1639 _FP_FRAC_DECL_##wc (u); \
1640 _FP_FRAC_DECL_##wc (v); \
1641 _FP_FRAC_COPY_##wc (u, X); \
1642 _FP_FRAC_COPY_##wc (v, Y); \
1643 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1644 /* Normalize U and V. */ \
1645 _FP_FRAC_SLL_##wc (u, _FP_WFRACXBITS_##fs); \
1646 _FP_FRAC_SLL_##wc (v, _FP_WFRACXBITS_##fs); \
1647 /* First round. Since the operands are normalized, either the \
1648 first or second bit will be set in the fraction. Produce a \
1649 normalized result by checking which and adjusting the loop \
1650 count and exponent accordingly. */ \
1651 if (_FP_FRAC_GE_1 (u, v)) \
1653 _FP_FRAC_SUB_##wc (u, u, v); \
1654 _FP_FRAC_LOW_##wc (R) |= 1; \
1659 /* Subsequent rounds. */ \
1662 int msb = (_FP_WS_TYPE) _FP_FRAC_HIGH_##wc (u) < 0; \
1663 _FP_FRAC_SLL_##wc (u, 1); \
1664 _FP_FRAC_SLL_##wc (R, 1); \
1665 if (msb || _FP_FRAC_GE_1 (u, v)) \
1667 _FP_FRAC_SUB_##wc (u, u, v); \
1668 _FP_FRAC_LOW_##wc (R) |= 1; \
1671 while (--count > 0); \
1672 /* If there's anything left in U, the result is inexact. */ \
1673 _FP_FRAC_LOW_##wc (R) |= !_FP_FRAC_ZEROP_##wc (u); \
1677 #define _FP_DIV_MEAT_1_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 1, R, X, Y)
1678 #define _FP_DIV_MEAT_2_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 2, R, X, Y)
1679 #define _FP_DIV_MEAT_4_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 4, R, X, Y)