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)); \
35 _FP_I_TYPE X##_e __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) \
194 int _FP_PACK_SEMIRAW_is_tiny \
195 = X##_e == 0 && !_FP_FRAC_ZEROP_##wc (X); \
196 if (_FP_TININESS_AFTER_ROUNDING \
197 && _FP_PACK_SEMIRAW_is_tiny) \
199 FP_DECL_##fs (_FP_PACK_SEMIRAW_T); \
200 _FP_FRAC_COPY_##wc (_FP_PACK_SEMIRAW_T, X); \
201 _FP_PACK_SEMIRAW_T##_s = X##_s; \
202 _FP_PACK_SEMIRAW_T##_e = X##_e; \
203 _FP_FRAC_SLL_##wc (_FP_PACK_SEMIRAW_T, 1); \
204 _FP_ROUND (wc, _FP_PACK_SEMIRAW_T); \
205 if (_FP_FRAC_OVERP_##wc (fs, _FP_PACK_SEMIRAW_T)) \
206 _FP_PACK_SEMIRAW_is_tiny = 0; \
209 if (_FP_PACK_SEMIRAW_is_tiny) \
211 if ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
212 || (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW)) \
213 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
215 if (_FP_FRAC_HIGH_##fs (X) \
216 & (_FP_OVERFLOW_##fs >> 1)) \
218 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_OVERFLOW_##fs >> 1); \
220 if (X##_e == _FP_EXPMAX_##fs) \
221 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
223 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
224 if (X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
226 if (!_FP_KEEPNANFRACP) \
228 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
229 X##_s = _FP_NANSIGN_##fs; \
232 _FP_SETQNAN (fs, wc, X); \
238 * Before packing the bits back into the native fp result, take care
239 * of such mundane things as rounding and overflow. Also, for some
240 * kinds of fp values, the original parts may not have been fully
241 * extracted -- but that is ok, we can regenerate them now.
244 #define _FP_PACK_CANONICAL(fs, wc, X) \
249 case FP_CLS_NORMAL: \
250 X##_e += _FP_EXPBIAS_##fs; \
254 if (_FP_FRAC_OVERP_##wc (fs, X)) \
256 _FP_FRAC_CLEAR_OVERP_##wc (fs, X); \
259 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
260 if (X##_e >= _FP_EXPMAX_##fs) \
263 switch (FP_ROUNDMODE) \
265 case FP_RND_NEAREST: \
266 X##_c = FP_CLS_INF; \
270 X##_c = FP_CLS_INF; \
274 X##_c = FP_CLS_INF; \
277 if (X##_c == FP_CLS_INF) \
279 /* Overflow to infinity */ \
280 X##_e = _FP_EXPMAX_##fs; \
281 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
285 /* Overflow to maximum normal */ \
286 X##_e = _FP_EXPMAX_##fs - 1; \
287 _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
289 FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
290 FP_SET_EXCEPTION (FP_EX_INEXACT); \
295 /* we've got a denormalized number */ \
296 int _FP_PACK_CANONICAL_is_tiny = 1; \
297 if (_FP_TININESS_AFTER_ROUNDING && X##_e == 0) \
299 FP_DECL_##fs (_FP_PACK_CANONICAL_T); \
300 _FP_FRAC_COPY_##wc (_FP_PACK_CANONICAL_T, X); \
301 _FP_PACK_CANONICAL_T##_s = X##_s; \
302 _FP_PACK_CANONICAL_T##_e = X##_e; \
303 _FP_ROUND (wc, _FP_PACK_CANONICAL_T); \
304 if (_FP_FRAC_OVERP_##wc (fs, _FP_PACK_CANONICAL_T)) \
305 _FP_PACK_CANONICAL_is_tiny = 0; \
307 X##_e = -X##_e + 1; \
308 if (X##_e <= _FP_WFRACBITS_##fs) \
310 _FP_FRAC_SRS_##wc (X, X##_e, _FP_WFRACBITS_##fs); \
312 if (_FP_FRAC_HIGH_##fs (X) \
313 & (_FP_OVERFLOW_##fs >> 1)) \
316 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
317 FP_SET_EXCEPTION (FP_EX_INEXACT); \
322 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
324 if (_FP_PACK_CANONICAL_is_tiny \
325 && ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
326 || (FP_TRAPPING_EXCEPTIONS \
327 & FP_EX_UNDERFLOW))) \
328 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
332 /* underflow to zero */ \
334 if (!_FP_FRAC_ZEROP_##wc (X)) \
336 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
338 _FP_FRAC_LOW_##wc (X) >>= (_FP_WORKBITS); \
340 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
347 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
351 X##_e = _FP_EXPMAX_##fs; \
352 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
356 X##_e = _FP_EXPMAX_##fs; \
357 if (!_FP_KEEPNANFRACP) \
359 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
360 X##_s = _FP_NANSIGN_##fs; \
363 _FP_SETQNAN (fs, wc, X); \
369 /* This one accepts raw argument and not cooked, returns
370 * 1 if X is a signaling NaN.
372 #define _FP_ISSIGNAN(fs, wc, X) \
375 if (X##_e == _FP_EXPMAX_##fs) \
377 if (!_FP_FRAC_ZEROP_##wc (X) \
378 && _FP_FRAC_SNANP (fs, X)) \
388 /* Addition on semi-raw values. */
389 #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) \
392 if (X##_s == Y##_s) \
396 int ediff = X##_e - Y##_e; \
402 /* Y is zero or denormalized. */ \
403 if (_FP_FRAC_ZEROP_##wc (Y)) \
405 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
406 _FP_FRAC_COPY_##wc (R, X); \
411 FP_SET_EXCEPTION (FP_EX_DENORM); \
415 _FP_FRAC_ADD_##wc (R, X, Y); \
418 if (X##_e == _FP_EXPMAX_##fs) \
420 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
421 _FP_FRAC_COPY_##wc (R, X); \
427 else if (X##_e == _FP_EXPMAX_##fs) \
429 /* X is NaN or Inf, Y is normal. */ \
430 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
431 _FP_FRAC_COPY_##wc (R, X); \
435 /* Insert implicit MSB of Y. */ \
436 _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
439 /* Shift the mantissa of Y to the right EDIFF steps; \
440 remember to account later for the implicit MSB of X. */ \
441 if (ediff <= _FP_WFRACBITS_##fs) \
442 _FP_FRAC_SRS_##wc (Y, ediff, _FP_WFRACBITS_##fs); \
443 else if (!_FP_FRAC_ZEROP_##wc (Y)) \
444 _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
445 _FP_FRAC_ADD_##wc (R, X, Y); \
447 else if (ediff < 0) \
453 /* X is zero or denormalized. */ \
454 if (_FP_FRAC_ZEROP_##wc (X)) \
456 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
457 _FP_FRAC_COPY_##wc (R, Y); \
462 FP_SET_EXCEPTION (FP_EX_DENORM); \
466 _FP_FRAC_ADD_##wc (R, Y, X); \
469 if (Y##_e == _FP_EXPMAX_##fs) \
471 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
472 _FP_FRAC_COPY_##wc (R, Y); \
478 else if (Y##_e == _FP_EXPMAX_##fs) \
480 /* Y is NaN or Inf, X is normal. */ \
481 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
482 _FP_FRAC_COPY_##wc (R, Y); \
486 /* Insert implicit MSB of X. */ \
487 _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
490 /* Shift the mantissa of X to the right EDIFF steps; \
491 remember to account later for the implicit MSB of Y. */ \
492 if (ediff <= _FP_WFRACBITS_##fs) \
493 _FP_FRAC_SRS_##wc (X, ediff, _FP_WFRACBITS_##fs); \
494 else if (!_FP_FRAC_ZEROP_##wc (X)) \
495 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
496 _FP_FRAC_ADD_##wc (R, Y, X); \
501 if (!_FP_EXP_NORMAL (fs, wc, X)) \
505 /* X and Y are zero or denormalized. */ \
507 if (_FP_FRAC_ZEROP_##wc (X)) \
509 if (!_FP_FRAC_ZEROP_##wc (Y)) \
510 FP_SET_EXCEPTION (FP_EX_DENORM); \
511 _FP_FRAC_COPY_##wc (R, Y); \
514 else if (_FP_FRAC_ZEROP_##wc (Y)) \
516 FP_SET_EXCEPTION (FP_EX_DENORM); \
517 _FP_FRAC_COPY_##wc (R, X); \
522 FP_SET_EXCEPTION (FP_EX_DENORM); \
523 _FP_FRAC_ADD_##wc (R, X, Y); \
524 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
526 /* Normalized result. */ \
527 _FP_FRAC_HIGH_##fs (R) \
528 &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
536 /* X and Y are NaN or Inf. */ \
537 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
538 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
539 R##_e = _FP_EXPMAX_##fs; \
540 if (_FP_FRAC_ZEROP_##wc (X)) \
541 _FP_FRAC_COPY_##wc (R, Y); \
542 else if (_FP_FRAC_ZEROP_##wc (Y)) \
543 _FP_FRAC_COPY_##wc (R, X); \
545 _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
549 /* The exponents of X and Y, both normal, are equal. The \
550 implicit MSBs will always add to increase the \
552 _FP_FRAC_ADD_##wc (R, X, Y); \
554 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
555 if (R##_e == _FP_EXPMAX_##fs) \
556 /* Overflow to infinity (depending on rounding mode). */ \
557 _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
561 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
564 _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
566 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
567 if (R##_e == _FP_EXPMAX_##fs) \
568 /* Overflow to infinity (depending on rounding mode). */ \
569 _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
576 int ediff = X##_e - Y##_e; \
583 /* Y is zero or denormalized. */ \
584 if (_FP_FRAC_ZEROP_##wc (Y)) \
586 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
587 _FP_FRAC_COPY_##wc (R, X); \
592 FP_SET_EXCEPTION (FP_EX_DENORM); \
596 _FP_FRAC_SUB_##wc (R, X, Y); \
599 if (X##_e == _FP_EXPMAX_##fs) \
601 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
602 _FP_FRAC_COPY_##wc (R, X); \
608 else if (X##_e == _FP_EXPMAX_##fs) \
610 /* X is NaN or Inf, Y is normal. */ \
611 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
612 _FP_FRAC_COPY_##wc (R, X); \
616 /* Insert implicit MSB of Y. */ \
617 _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
620 /* Shift the mantissa of Y to the right EDIFF steps; \
621 remember to account later for the implicit MSB of X. */ \
622 if (ediff <= _FP_WFRACBITS_##fs) \
623 _FP_FRAC_SRS_##wc (Y, ediff, _FP_WFRACBITS_##fs); \
624 else if (!_FP_FRAC_ZEROP_##wc (Y)) \
625 _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
626 _FP_FRAC_SUB_##wc (R, X, Y); \
628 else if (ediff < 0) \
635 /* X is zero or denormalized. */ \
636 if (_FP_FRAC_ZEROP_##wc (X)) \
638 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
639 _FP_FRAC_COPY_##wc (R, Y); \
644 FP_SET_EXCEPTION (FP_EX_DENORM); \
648 _FP_FRAC_SUB_##wc (R, Y, X); \
651 if (Y##_e == _FP_EXPMAX_##fs) \
653 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
654 _FP_FRAC_COPY_##wc (R, Y); \
660 else if (Y##_e == _FP_EXPMAX_##fs) \
662 /* Y is NaN or Inf, X is normal. */ \
663 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
664 _FP_FRAC_COPY_##wc (R, Y); \
668 /* Insert implicit MSB of X. */ \
669 _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
672 /* Shift the mantissa of X to the right EDIFF steps; \
673 remember to account later for the implicit MSB of Y. */ \
674 if (ediff <= _FP_WFRACBITS_##fs) \
675 _FP_FRAC_SRS_##wc (X, ediff, _FP_WFRACBITS_##fs); \
676 else if (!_FP_FRAC_ZEROP_##wc (X)) \
677 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
678 _FP_FRAC_SUB_##wc (R, Y, X); \
683 if (!_FP_EXP_NORMAL (fs, wc, X)) \
687 /* X and Y are zero or denormalized. */ \
689 if (_FP_FRAC_ZEROP_##wc (X)) \
691 _FP_FRAC_COPY_##wc (R, Y); \
692 if (_FP_FRAC_ZEROP_##wc (Y)) \
693 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
696 FP_SET_EXCEPTION (FP_EX_DENORM); \
701 else if (_FP_FRAC_ZEROP_##wc (Y)) \
703 FP_SET_EXCEPTION (FP_EX_DENORM); \
704 _FP_FRAC_COPY_##wc (R, X); \
710 FP_SET_EXCEPTION (FP_EX_DENORM); \
711 _FP_FRAC_SUB_##wc (R, X, Y); \
713 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
715 /* |X| < |Y|, negate result. */ \
716 _FP_FRAC_SUB_##wc (R, Y, X); \
719 else if (_FP_FRAC_ZEROP_##wc (R)) \
720 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
726 /* X and Y are NaN or Inf, of opposite signs. */ \
727 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
728 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
729 R##_e = _FP_EXPMAX_##fs; \
730 if (_FP_FRAC_ZEROP_##wc (X)) \
732 if (_FP_FRAC_ZEROP_##wc (Y)) \
735 R##_s = _FP_NANSIGN_##fs; \
736 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
737 _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
738 FP_SET_EXCEPTION (FP_EX_INVALID); \
744 _FP_FRAC_COPY_##wc (R, Y); \
749 if (_FP_FRAC_ZEROP_##wc (Y)) \
753 _FP_FRAC_COPY_##wc (R, X); \
758 _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
764 /* The exponents of X and Y, both normal, are equal. The \
765 implicit MSBs cancel. */ \
767 _FP_FRAC_SUB_##wc (R, X, Y); \
769 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
771 /* |X| < |Y|, negate result. */ \
772 _FP_FRAC_SUB_##wc (R, Y, X); \
775 else if (_FP_FRAC_ZEROP_##wc (R)) \
778 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
784 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
787 /* Carry into most significant bit of larger one of X and Y, \
788 canceling it; renormalize. */ \
789 _FP_FRAC_HIGH_##fs (R) &= _FP_IMPLBIT_SH_##fs - 1; \
791 _FP_FRAC_CLZ_##wc (diff, R); \
792 diff -= _FP_WFRACXBITS_##fs; \
793 _FP_FRAC_SLL_##wc (R, diff); \
796 /* R is denormalized. */ \
797 diff = diff - R##_e + 1; \
798 _FP_FRAC_SRS_##wc (R, diff, _FP_WFRACBITS_##fs); \
804 _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
812 #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL (fs, wc, R, X, Y, '+')
813 #define _FP_SUB(fs, wc, R, X, Y) \
816 if (!(Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
818 _FP_ADD_INTERNAL (fs, wc, R, X, Y, '-'); \
824 * Main negation routine. The input value is raw.
827 #define _FP_NEG(fs, wc, R, X) \
830 _FP_FRAC_COPY_##wc (R, X); \
838 * Main multiplication routine. The input values should be cooked.
841 #define _FP_MUL(fs, wc, R, X, Y) \
844 R##_s = X##_s ^ Y##_s; \
845 R##_e = X##_e + Y##_e + 1; \
846 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
848 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
849 R##_c = FP_CLS_NORMAL; \
851 _FP_MUL_MEAT_##fs (R, X, Y); \
853 if (_FP_FRAC_OVERP_##wc (fs, R)) \
854 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
859 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
860 _FP_CHOOSENAN (fs, wc, R, X, Y, '*'); \
863 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
864 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
865 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
868 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
869 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
870 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
871 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
872 _FP_FRAC_COPY_##wc (R, X); \
876 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
877 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
878 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
881 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
882 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
883 _FP_FRAC_COPY_##wc (R, Y); \
887 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
888 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
889 R##_s = _FP_NANSIGN_##fs; \
890 R##_c = FP_CLS_NAN; \
891 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
892 FP_SET_EXCEPTION (FP_EX_INVALID); \
902 /* Fused multiply-add. The input values should be cooked. */
904 #define _FP_FMA(fs, wc, dwc, R, X, Y, Z) \
908 T##_s = X##_s ^ Y##_s; \
909 T##_e = X##_e + Y##_e + 1; \
910 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
912 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
918 _FP_FRAC_COPY_##wc (R, Z); \
923 R##_c = FP_CLS_NORMAL; \
927 _FP_MUL_MEAT_##fs (R, X, Y); \
929 if (_FP_FRAC_OVERP_##wc (fs, R)) \
930 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
935 case FP_CLS_NORMAL:; \
936 _FP_FRAC_DECL_##dwc (TD); \
937 _FP_FRAC_DECL_##dwc (ZD); \
938 _FP_FRAC_DECL_##dwc (RD); \
939 _FP_MUL_MEAT_DW_##fs (TD, X, Y); \
941 int tsh = _FP_FRAC_HIGHBIT_DW_##dwc (fs, TD) == 0; \
943 int ediff = T##_e - Z##_e; \
946 int shift = _FP_WFRACBITS_##fs - tsh - ediff; \
947 if (shift <= -_FP_WFRACBITS_##fs) \
948 _FP_FRAC_SET_##dwc (ZD, _FP_MINFRAC_##dwc); \
951 _FP_FRAC_COPY_##dwc##_##wc (ZD, Z); \
953 _FP_FRAC_SRS_##dwc (ZD, -shift, \
954 _FP_WFRACBITS_DW_##fs); \
955 else if (shift > 0) \
956 _FP_FRAC_SLL_##dwc (ZD, shift); \
959 if (T##_s == Z##_s) \
960 _FP_FRAC_ADD_##dwc (RD, TD, ZD); \
963 _FP_FRAC_SUB_##dwc (RD, TD, ZD); \
964 if (_FP_FRAC_NEGP_##dwc (RD)) \
967 _FP_FRAC_SUB_##dwc (RD, ZD, TD); \
975 _FP_FRAC_COPY_##dwc##_##wc (ZD, Z); \
976 _FP_FRAC_SLL_##dwc (ZD, _FP_WFRACBITS_##fs); \
977 int shift = -ediff - tsh; \
978 if (shift >= _FP_WFRACBITS_DW_##fs) \
979 _FP_FRAC_SET_##dwc (TD, _FP_MINFRAC_##dwc); \
980 else if (shift > 0) \
981 _FP_FRAC_SRS_##dwc (TD, shift, \
982 _FP_WFRACBITS_DW_##fs); \
983 if (Z##_s == T##_s) \
984 _FP_FRAC_ADD_##dwc (RD, ZD, TD); \
986 _FP_FRAC_SUB_##dwc (RD, ZD, TD); \
988 if (_FP_FRAC_ZEROP_##dwc (RD)) \
990 if (T##_s == Z##_s) \
993 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
994 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
995 R##_c = FP_CLS_ZERO; \
1000 _FP_FRAC_CLZ_##dwc (rlz, RD); \
1001 rlz -= _FP_WFRACXBITS_DW_##fs; \
1003 int shift = _FP_WFRACBITS_##fs - rlz; \
1005 _FP_FRAC_SRS_##dwc (RD, shift, \
1006 _FP_WFRACBITS_DW_##fs); \
1007 else if (shift < 0) \
1008 _FP_FRAC_SLL_##dwc (RD, -shift); \
1009 _FP_FRAC_COPY_##wc##_##dwc (R, RD); \
1010 R##_c = FP_CLS_NORMAL; \
1016 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1017 _FP_CHOOSENAN (fs, wc, T, X, Y, '*'); \
1020 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1021 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1022 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1025 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1026 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1027 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1028 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1029 _FP_FRAC_COPY_##wc (T, X); \
1033 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
1034 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1035 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1038 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
1039 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
1040 _FP_FRAC_COPY_##wc (T, Y); \
1044 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1045 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1046 T##_s = _FP_NANSIGN_##fs; \
1047 T##_c = FP_CLS_NAN; \
1048 _FP_FRAC_SET_##wc (T, _FP_NANFRAC_##fs); \
1049 FP_SET_EXCEPTION (FP_EX_INVALID); \
1056 /* T = X * Y is zero, infinity or NaN. */ \
1057 switch (_FP_CLS_COMBINE (T##_c, Z##_c)) \
1059 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1060 _FP_CHOOSENAN (fs, wc, R, T, Z, '+'); \
1063 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1064 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1065 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1066 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1067 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1069 _FP_FRAC_COPY_##wc (R, T); \
1073 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1074 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1075 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1076 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1078 _FP_FRAC_COPY_##wc (R, Z); \
1082 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1083 if (T##_s == Z##_s) \
1086 _FP_FRAC_COPY_##wc (R, Z); \
1091 R##_s = _FP_NANSIGN_##fs; \
1092 R##_c = FP_CLS_NAN; \
1093 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1094 FP_SET_EXCEPTION (FP_EX_INVALID); \
1098 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1099 if (T##_s == Z##_s) \
1102 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
1103 _FP_FRAC_COPY_##wc (R, Z); \
1116 * Main division routine. The input values should be cooked.
1119 #define _FP_DIV(fs, wc, R, X, Y) \
1122 R##_s = X##_s ^ Y##_s; \
1123 R##_e = X##_e - Y##_e; \
1124 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
1126 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
1127 R##_c = FP_CLS_NORMAL; \
1129 _FP_DIV_MEAT_##fs (R, X, Y); \
1132 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1133 _FP_CHOOSENAN (fs, wc, R, X, Y, '/'); \
1136 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1137 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1138 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1140 _FP_FRAC_COPY_##wc (R, X); \
1144 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
1145 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1146 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1148 _FP_FRAC_COPY_##wc (R, Y); \
1152 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
1153 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1154 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1155 R##_c = FP_CLS_ZERO; \
1158 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
1159 FP_SET_EXCEPTION (FP_EX_DIVZERO); \
1160 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1161 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1162 R##_c = FP_CLS_INF; \
1165 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1166 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1167 R##_s = _FP_NANSIGN_##fs; \
1168 R##_c = FP_CLS_NAN; \
1169 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1170 FP_SET_EXCEPTION (FP_EX_INVALID); \
1181 * Main differential comparison routine. The inputs should be raw not
1182 * cooked. The return is -1,0,1 for normal values, 2 otherwise.
1185 #define _FP_CMP(fs, wc, ret, X, Y, un) \
1188 /* NANs are unordered */ \
1189 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1190 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1199 __is_zero_x = (!X##_e && _FP_FRAC_ZEROP_##wc (X)) ? 1 : 0; \
1200 __is_zero_y = (!Y##_e && _FP_FRAC_ZEROP_##wc (Y)) ? 1 : 0; \
1202 if (__is_zero_x && __is_zero_y) \
1204 else if (__is_zero_x) \
1205 ret = Y##_s ? 1 : -1; \
1206 else if (__is_zero_y) \
1207 ret = X##_s ? -1 : 1; \
1208 else if (X##_s != Y##_s) \
1209 ret = X##_s ? -1 : 1; \
1210 else if (X##_e > Y##_e) \
1211 ret = X##_s ? -1 : 1; \
1212 else if (X##_e < Y##_e) \
1213 ret = X##_s ? 1 : -1; \
1214 else if (_FP_FRAC_GT_##wc (X, Y)) \
1215 ret = X##_s ? -1 : 1; \
1216 else if (_FP_FRAC_GT_##wc (Y, X)) \
1217 ret = X##_s ? 1 : -1; \
1225 /* Simplification for strict equality. */
1227 #define _FP_CMP_EQ(fs, wc, ret, X, Y) \
1230 /* NANs are unordered */ \
1231 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1232 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1238 ret = !(X##_e == Y##_e \
1239 && _FP_FRAC_EQ_##wc (X, Y) \
1240 && (X##_s == Y##_s || (!X##_e && _FP_FRAC_ZEROP_##wc (X)))); \
1245 /* Version to test unordered. */
1247 #define _FP_CMP_UNORD(fs, wc, ret, X, Y) \
1250 ret = ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1251 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))); \
1256 * Main square root routine. The input value should be cooked.
1259 #define _FP_SQRT(fs, wc, R, X) \
1262 _FP_FRAC_DECL_##wc (T); \
1263 _FP_FRAC_DECL_##wc (S); \
1268 _FP_FRAC_COPY_##wc (R, X); \
1270 R##_c = FP_CLS_NAN; \
1275 R##_s = _FP_NANSIGN_##fs; \
1276 R##_c = FP_CLS_NAN; /* NAN */ \
1277 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1278 FP_SET_EXCEPTION (FP_EX_INVALID); \
1283 R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
1288 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
1290 case FP_CLS_NORMAL: \
1294 R##_c = FP_CLS_NAN; /* NAN */ \
1295 R##_s = _FP_NANSIGN_##fs; \
1296 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1297 FP_SET_EXCEPTION (FP_EX_INVALID); \
1300 R##_c = FP_CLS_NORMAL; \
1302 _FP_FRAC_SLL_##wc (X, 1); \
1303 R##_e = X##_e >> 1; \
1304 _FP_FRAC_SET_##wc (S, _FP_ZEROFRAC_##wc); \
1305 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1306 q = _FP_OVERFLOW_##fs >> 1; \
1307 _FP_SQRT_MEAT_##wc (R, S, T, X, q); \
1313 * Convert from FP to integer. Input is raw.
1316 /* RSIGNED can have following values:
1317 * 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
1318 * the result is either 0 or (2^rsize)-1 depending on the sign in such
1320 * 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
1321 * NV is set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1322 * depending on the sign in such case.
1323 * -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
1324 * set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1325 * depending on the sign in such case.
1327 #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
1330 if (X##_e < _FP_EXPBIAS_##fs) \
1335 if (!_FP_FRAC_ZEROP_##wc (X)) \
1337 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1338 FP_SET_EXCEPTION (FP_EX_DENORM); \
1342 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1344 else if (X##_e >= _FP_EXPBIAS_##fs + rsize - (rsigned > 0 || X##_s) \
1345 || (!rsigned && X##_s)) \
1347 /* Overflow or converting to the most negative integer. */ \
1359 if (rsigned && X##_s && X##_e == _FP_EXPBIAS_##fs + rsize - 1) \
1361 /* Possibly converting to most negative integer; check the \
1364 (void) ((_FP_FRACBITS_##fs > rsize) \
1366 _FP_FRAC_SRST_##wc (X, inexact, \
1367 _FP_FRACBITS_##fs - rsize, \
1368 _FP_FRACBITS_##fs); \
1372 if (!_FP_FRAC_ZEROP_##wc (X)) \
1373 FP_SET_EXCEPTION (FP_EX_INVALID); \
1375 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1378 FP_SET_EXCEPTION (FP_EX_INVALID); \
1382 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
1383 if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
1385 _FP_FRAC_ASSEMBLE_##wc (r, X, rsize); \
1386 r <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
1391 _FP_FRAC_SRST_##wc (X, inexact, \
1392 (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs - 1 \
1394 _FP_FRACBITS_##fs); \
1396 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1397 _FP_FRAC_ASSEMBLE_##wc (r, X, rsize); \
1399 if (rsigned && X##_s) \
1405 /* Convert integer to fp. Output is raw. RTYPE is unsigned even if
1407 #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
1414 if ((X##_s = (r < 0))) \
1418 (void) ((rsize <= _FP_W_TYPE_SIZE) \
1421 __FP_CLZ (lz_, (_FP_W_TYPE) ur_); \
1422 X##_e = _FP_EXPBIAS_##fs + _FP_W_TYPE_SIZE - 1 - lz_; \
1424 : ((rsize <= 2 * _FP_W_TYPE_SIZE) \
1428 (_FP_W_TYPE) (ur_ >> _FP_W_TYPE_SIZE), \
1429 (_FP_W_TYPE) ur_); \
1430 X##_e = (_FP_EXPBIAS_##fs + 2 * _FP_W_TYPE_SIZE - 1 \
1433 : (abort (), 0))); \
1435 if (rsize - 1 + _FP_EXPBIAS_##fs >= _FP_EXPMAX_##fs \
1436 && X##_e >= _FP_EXPMAX_##fs) \
1438 /* Exponent too big; overflow to infinity. (May also \
1439 happen after rounding below.) */ \
1440 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
1441 goto pack_semiraw; \
1444 if (rsize <= _FP_FRACBITS_##fs \
1445 || X##_e < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs) \
1447 /* Exactly representable; shift left. */ \
1448 _FP_FRAC_DISASSEMBLE_##wc (X, ur_, rsize); \
1449 if (_FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1 - X##_e > 0) \
1450 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1451 + _FP_FRACBITS_##fs - 1 - X##_e)); \
1455 /* More bits in integer than in floating type; need to \
1457 if (_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 < X##_e) \
1458 ur_ = ((ur_ >> (X##_e - _FP_EXPBIAS_##fs \
1459 - _FP_WFRACBITS_##fs + 1)) \
1460 | ((ur_ << (rsize - (X##_e - _FP_EXPBIAS_##fs \
1461 - _FP_WFRACBITS_##fs + 1))) \
1463 _FP_FRAC_DISASSEMBLE_##wc (X, ur_, rsize); \
1464 if ((_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 - X##_e) > 0) \
1465 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1466 + _FP_WFRACBITS_##fs - 1 - X##_e)); \
1467 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
1469 _FP_PACK_SEMIRAW (fs, wc, X); \
1476 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
1482 /* Extend from a narrower floating-point format to a wider one. Input
1483 and output are raw. */
1484 #define FP_EXTEND(dfs, sfs, dwc, swc, D, S) \
1487 if (_FP_FRACBITS_##dfs < _FP_FRACBITS_##sfs \
1488 || (_FP_EXPMAX_##dfs - _FP_EXPBIAS_##dfs \
1489 < _FP_EXPMAX_##sfs - _FP_EXPBIAS_##sfs) \
1490 || (_FP_EXPBIAS_##dfs < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1 \
1491 && _FP_EXPBIAS_##dfs != _FP_EXPBIAS_##sfs)) \
1494 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1495 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1497 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1498 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs - _FP_FRACBITS_##sfs)); \
1504 if (_FP_FRAC_ZEROP_##swc (S)) \
1506 else if (_FP_EXPBIAS_##dfs \
1507 < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1) \
1509 FP_SET_EXCEPTION (FP_EX_DENORM); \
1510 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1511 - _FP_FRACBITS_##sfs)); \
1517 FP_SET_EXCEPTION (FP_EX_DENORM); \
1518 _FP_FRAC_CLZ_##swc (_lz, S); \
1519 _FP_FRAC_SLL_##dwc (D, \
1520 _lz + _FP_FRACBITS_##dfs \
1521 - _FP_FRACTBITS_##sfs); \
1522 D##_e = (_FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs + 1 \
1523 + _FP_FRACXBITS_##sfs - _lz); \
1528 D##_e = _FP_EXPMAX_##dfs; \
1529 if (!_FP_FRAC_ZEROP_##swc (S)) \
1531 if (_FP_FRAC_SNANP (sfs, S)) \
1532 FP_SET_EXCEPTION (FP_EX_INVALID); \
1533 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1534 - _FP_FRACBITS_##sfs)); \
1535 _FP_SETQNAN (dfs, dwc, D); \
1542 /* Truncate from a wider floating-point format to a narrower one.
1543 Input and output are semi-raw. */
1544 #define FP_TRUNC(dfs, sfs, dwc, swc, D, S) \
1547 if (_FP_FRACBITS_##sfs < _FP_FRACBITS_##dfs \
1548 || (_FP_EXPBIAS_##sfs < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1 \
1549 && _FP_EXPBIAS_##sfs != _FP_EXPBIAS_##dfs)) \
1552 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1554 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1555 if (D##_e >= _FP_EXPMAX_##dfs) \
1556 _FP_OVERFLOW_SEMIRAW (dfs, dwc, D); \
1561 if (D##_e < 1 - _FP_FRACBITS_##dfs) \
1563 _FP_FRAC_SET_##swc (S, _FP_ZEROFRAC_##swc); \
1564 _FP_FRAC_LOW_##swc (S) |= 1; \
1568 _FP_FRAC_HIGH_##sfs (S) |= _FP_IMPLBIT_SH_##sfs; \
1569 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1570 - _FP_WFRACBITS_##dfs \
1572 _FP_WFRACBITS_##sfs); \
1577 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1578 - _FP_WFRACBITS_##dfs), \
1579 _FP_WFRACBITS_##sfs); \
1580 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1588 if (_FP_FRAC_ZEROP_##swc (S)) \
1589 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1592 FP_SET_EXCEPTION (FP_EX_DENORM); \
1593 if (_FP_EXPBIAS_##sfs \
1594 < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1) \
1596 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1597 - _FP_WFRACBITS_##dfs), \
1598 _FP_WFRACBITS_##sfs); \
1599 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1603 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1604 _FP_FRAC_LOW_##dwc (D) |= 1; \
1610 D##_e = _FP_EXPMAX_##dfs; \
1611 if (_FP_FRAC_ZEROP_##swc (S)) \
1612 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1615 _FP_CHECK_SIGNAN_SEMIRAW (sfs, swc, S); \
1616 _FP_FRAC_SRL_##swc (S, (_FP_WFRACBITS_##sfs \
1617 - _FP_WFRACBITS_##dfs)); \
1618 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1619 /* Semi-raw NaN must have all workbits cleared. */ \
1620 _FP_FRAC_LOW_##dwc (D) \
1621 &= ~(_FP_W_TYPE) ((1 << _FP_WORKBITS) - 1); \
1622 _FP_SETQNAN_SEMIRAW (dfs, dwc, D); \
1630 * Helper primitives.
1633 /* Count leading zeros in a word. */
1636 /* GCC 3.4 and later provide the builtins for us. */
1637 # define __FP_CLZ(r, x) \
1640 if (sizeof (_FP_W_TYPE) == sizeof (unsigned int)) \
1641 r = __builtin_clz (x); \
1642 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long)) \
1643 r = __builtin_clzl (x); \
1644 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long long)) \
1645 r = __builtin_clzll (x); \
1650 #endif /* ndef __FP_CLZ */
1652 #define _FP_DIV_HELP_imm(q, r, n, d) \
1655 q = n / d, r = n % d; \
1660 /* A restoring bit-by-bit division primitive. */
1662 #define _FP_DIV_MEAT_N_loop(fs, wc, R, X, Y) \
1665 int count = _FP_WFRACBITS_##fs; \
1666 _FP_FRAC_DECL_##wc (u); \
1667 _FP_FRAC_DECL_##wc (v); \
1668 _FP_FRAC_COPY_##wc (u, X); \
1669 _FP_FRAC_COPY_##wc (v, Y); \
1670 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1671 /* Normalize U and V. */ \
1672 _FP_FRAC_SLL_##wc (u, _FP_WFRACXBITS_##fs); \
1673 _FP_FRAC_SLL_##wc (v, _FP_WFRACXBITS_##fs); \
1674 /* First round. Since the operands are normalized, either the \
1675 first or second bit will be set in the fraction. Produce a \
1676 normalized result by checking which and adjusting the loop \
1677 count and exponent accordingly. */ \
1678 if (_FP_FRAC_GE_1 (u, v)) \
1680 _FP_FRAC_SUB_##wc (u, u, v); \
1681 _FP_FRAC_LOW_##wc (R) |= 1; \
1686 /* Subsequent rounds. */ \
1689 int msb = (_FP_WS_TYPE) _FP_FRAC_HIGH_##wc (u) < 0; \
1690 _FP_FRAC_SLL_##wc (u, 1); \
1691 _FP_FRAC_SLL_##wc (R, 1); \
1692 if (msb || _FP_FRAC_GE_1 (u, v)) \
1694 _FP_FRAC_SUB_##wc (u, u, v); \
1695 _FP_FRAC_LOW_##wc (R) |= 1; \
1698 while (--count > 0); \
1699 /* If there's anything left in U, the result is inexact. */ \
1700 _FP_FRAC_LOW_##wc (R) |= !_FP_FRAC_ZEROP_##wc (u); \
1704 #define _FP_DIV_MEAT_1_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 1, R, X, Y)
1705 #define _FP_DIV_MEAT_2_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 2, R, X, Y)
1706 #define _FP_DIV_MEAT_4_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 4, R, X, Y)