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))
48 /* Finish truly unpacking a native fp value by classifying the kind
49 of fp value and normalizing both the exponent and the fraction. */
51 #define _FP_UNPACK_CANONICAL(fs, wc, X) \
57 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
58 _FP_FRAC_SLL_##wc (X, _FP_WORKBITS); \
59 X##_e -= _FP_EXPBIAS_##fs; \
60 X##_c = FP_CLS_NORMAL; \
64 if (_FP_FRAC_ZEROP_##wc (X)) \
65 X##_c = FP_CLS_ZERO; \
66 else if (FP_DENORM_ZERO) \
68 X##_c = FP_CLS_ZERO; \
69 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
70 FP_SET_EXCEPTION (FP_EX_DENORM); \
74 /* A denormalized number. */ \
75 _FP_I_TYPE _FP_UNPACK_CANONICAL_shift; \
76 _FP_FRAC_CLZ_##wc (_FP_UNPACK_CANONICAL_shift, \
78 _FP_UNPACK_CANONICAL_shift -= _FP_FRACXBITS_##fs; \
79 _FP_FRAC_SLL_##wc (X, (_FP_UNPACK_CANONICAL_shift \
81 X##_e -= (_FP_EXPBIAS_##fs - 1 \
82 + _FP_UNPACK_CANONICAL_shift); \
83 X##_c = FP_CLS_NORMAL; \
84 FP_SET_EXCEPTION (FP_EX_DENORM); \
88 case _FP_EXPMAX_##fs: \
89 if (_FP_FRAC_ZEROP_##wc (X)) \
94 /* Check for signaling NaN. */ \
95 if (_FP_FRAC_SNANP (fs, X)) \
96 FP_SET_EXCEPTION (FP_EX_INVALID \
97 | FP_EX_INVALID_SNAN); \
104 /* Finish unpacking an fp value in semi-raw mode: the mantissa is
105 shifted by _FP_WORKBITS but the implicit MSB is not inserted and
106 other classification is not done. */
107 #define _FP_UNPACK_SEMIRAW(fs, wc, X) _FP_FRAC_SLL_##wc (X, _FP_WORKBITS)
109 /* Check whether a raw or semi-raw input value should be flushed to
110 zero, and flush it to zero if so. */
111 #define _FP_CHECK_FLUSH_ZERO(fs, wc, X) \
116 && !_FP_FRAC_ZEROP_##wc (X)) \
118 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
119 FP_SET_EXCEPTION (FP_EX_DENORM); \
124 /* A semi-raw value has overflowed to infinity. Adjust the mantissa
125 and exponent appropriately. */
126 #define _FP_OVERFLOW_SEMIRAW(fs, wc, X) \
129 if (FP_ROUNDMODE == FP_RND_NEAREST \
130 || (FP_ROUNDMODE == FP_RND_PINF && !X##_s) \
131 || (FP_ROUNDMODE == FP_RND_MINF && X##_s)) \
133 X##_e = _FP_EXPMAX_##fs; \
134 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
138 X##_e = _FP_EXPMAX_##fs - 1; \
139 _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
141 FP_SET_EXCEPTION (FP_EX_INEXACT); \
142 FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
146 /* Check for a semi-raw value being a signaling NaN and raise the
147 invalid exception if so. */
148 #define _FP_CHECK_SIGNAN_SEMIRAW(fs, wc, X) \
151 if (X##_e == _FP_EXPMAX_##fs \
152 && !_FP_FRAC_ZEROP_##wc (X) \
153 && _FP_FRAC_SNANP_SEMIRAW (fs, X)) \
154 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SNAN); \
158 /* Choose a NaN result from an operation on two semi-raw NaN
160 #define _FP_CHOOSENAN_SEMIRAW(fs, wc, R, X, Y, OP) \
163 /* _FP_CHOOSENAN expects raw values, so shift as required. */ \
164 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
165 _FP_FRAC_SRL_##wc (Y, _FP_WORKBITS); \
166 _FP_CHOOSENAN (fs, wc, R, X, Y, OP); \
167 _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
171 /* Make the fractional part a quiet NaN, preserving the payload
172 if possible, otherwise make it the canonical quiet NaN and set
173 the sign bit accordingly. */
174 #define _FP_SETQNAN(fs, wc, X) \
177 if (_FP_QNANNEGATEDP) \
179 _FP_FRAC_HIGH_RAW_##fs (X) &= _FP_QNANBIT_##fs - 1; \
180 if (_FP_FRAC_ZEROP_##wc (X)) \
182 X##_s = _FP_NANSIGN_##fs; \
183 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
187 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_QNANBIT_##fs; \
190 #define _FP_SETQNAN_SEMIRAW(fs, wc, X) \
193 if (_FP_QNANNEGATEDP) \
195 _FP_FRAC_HIGH_##fs (X) &= _FP_QNANBIT_SH_##fs - 1; \
196 if (_FP_FRAC_ZEROP_##wc (X)) \
198 X##_s = _FP_NANSIGN_##fs; \
199 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
200 _FP_FRAC_SLL_##wc (X, _FP_WORKBITS); \
204 _FP_FRAC_HIGH_##fs (X) |= _FP_QNANBIT_SH_##fs; \
208 /* Test whether a biased exponent is normal (not zero or maximum). */
209 #define _FP_EXP_NORMAL(fs, wc, X) (((X##_e + 1) & _FP_EXPMAX_##fs) > 1)
211 /* Prepare to pack an fp value in semi-raw mode: the mantissa is
212 rounded and shifted right, with the rounding possibly increasing
213 the exponent (including changing a finite value to infinity). */
214 #define _FP_PACK_SEMIRAW(fs, wc, X) \
217 int _FP_PACK_SEMIRAW_is_tiny \
218 = X##_e == 0 && !_FP_FRAC_ZEROP_##wc (X); \
219 if (_FP_TININESS_AFTER_ROUNDING \
220 && _FP_PACK_SEMIRAW_is_tiny) \
222 FP_DECL_##fs (_FP_PACK_SEMIRAW_T); \
223 _FP_FRAC_COPY_##wc (_FP_PACK_SEMIRAW_T, X); \
224 _FP_PACK_SEMIRAW_T##_s = X##_s; \
225 _FP_PACK_SEMIRAW_T##_e = X##_e; \
226 _FP_FRAC_SLL_##wc (_FP_PACK_SEMIRAW_T, 1); \
227 _FP_ROUND (wc, _FP_PACK_SEMIRAW_T); \
228 if (_FP_FRAC_OVERP_##wc (fs, _FP_PACK_SEMIRAW_T)) \
229 _FP_PACK_SEMIRAW_is_tiny = 0; \
232 if (_FP_PACK_SEMIRAW_is_tiny) \
234 if ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
235 || (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW)) \
236 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
238 if (_FP_FRAC_HIGH_##fs (X) \
239 & (_FP_OVERFLOW_##fs >> 1)) \
241 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_OVERFLOW_##fs >> 1); \
243 if (X##_e == _FP_EXPMAX_##fs) \
244 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
246 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
247 if (X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
249 if (!_FP_KEEPNANFRACP) \
251 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
252 X##_s = _FP_NANSIGN_##fs; \
255 _FP_SETQNAN (fs, wc, X); \
260 /* Before packing the bits back into the native fp result, take care
261 of such mundane things as rounding and overflow. Also, for some
262 kinds of fp values, the original parts may not have been fully
263 extracted -- but that is ok, we can regenerate them now. */
265 #define _FP_PACK_CANONICAL(fs, wc, X) \
270 case FP_CLS_NORMAL: \
271 X##_e += _FP_EXPBIAS_##fs; \
275 if (_FP_FRAC_OVERP_##wc (fs, X)) \
277 _FP_FRAC_CLEAR_OVERP_##wc (fs, X); \
280 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
281 if (X##_e >= _FP_EXPMAX_##fs) \
284 switch (FP_ROUNDMODE) \
286 case FP_RND_NEAREST: \
287 X##_c = FP_CLS_INF; \
291 X##_c = FP_CLS_INF; \
295 X##_c = FP_CLS_INF; \
298 if (X##_c == FP_CLS_INF) \
300 /* Overflow to infinity. */ \
301 X##_e = _FP_EXPMAX_##fs; \
302 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
306 /* Overflow to maximum normal. */ \
307 X##_e = _FP_EXPMAX_##fs - 1; \
308 _FP_FRAC_SET_##wc (X, _FP_MAXFRAC_##wc); \
310 FP_SET_EXCEPTION (FP_EX_OVERFLOW); \
311 FP_SET_EXCEPTION (FP_EX_INEXACT); \
316 /* We've got a denormalized number. */ \
317 int _FP_PACK_CANONICAL_is_tiny = 1; \
318 if (_FP_TININESS_AFTER_ROUNDING && X##_e == 0) \
320 FP_DECL_##fs (_FP_PACK_CANONICAL_T); \
321 _FP_FRAC_COPY_##wc (_FP_PACK_CANONICAL_T, X); \
322 _FP_PACK_CANONICAL_T##_s = X##_s; \
323 _FP_PACK_CANONICAL_T##_e = X##_e; \
324 _FP_ROUND (wc, _FP_PACK_CANONICAL_T); \
325 if (_FP_FRAC_OVERP_##wc (fs, _FP_PACK_CANONICAL_T)) \
326 _FP_PACK_CANONICAL_is_tiny = 0; \
328 X##_e = -X##_e + 1; \
329 if (X##_e <= _FP_WFRACBITS_##fs) \
331 _FP_FRAC_SRS_##wc (X, X##_e, _FP_WFRACBITS_##fs); \
333 if (_FP_FRAC_HIGH_##fs (X) \
334 & (_FP_OVERFLOW_##fs >> 1)) \
337 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
338 FP_SET_EXCEPTION (FP_EX_INEXACT); \
343 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
345 if (_FP_PACK_CANONICAL_is_tiny \
346 && ((FP_CUR_EXCEPTIONS & FP_EX_INEXACT) \
347 || (FP_TRAPPING_EXCEPTIONS \
348 & FP_EX_UNDERFLOW))) \
349 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
353 /* Underflow to zero. */ \
355 if (!_FP_FRAC_ZEROP_##wc (X)) \
357 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
359 _FP_FRAC_LOW_##wc (X) >>= (_FP_WORKBITS); \
361 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
368 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
372 X##_e = _FP_EXPMAX_##fs; \
373 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
377 X##_e = _FP_EXPMAX_##fs; \
378 if (!_FP_KEEPNANFRACP) \
380 _FP_FRAC_SET_##wc (X, _FP_NANFRAC_##fs); \
381 X##_s = _FP_NANSIGN_##fs; \
384 _FP_SETQNAN (fs, wc, X); \
390 /* This one accepts raw argument and not cooked, returns
391 1 if X is a signaling NaN. */
392 #define _FP_ISSIGNAN(fs, wc, X) \
394 int _FP_ISSIGNAN_ret = 0; \
395 if (X##_e == _FP_EXPMAX_##fs) \
397 if (!_FP_FRAC_ZEROP_##wc (X) \
398 && _FP_FRAC_SNANP (fs, X)) \
399 _FP_ISSIGNAN_ret = 1; \
408 /* Addition on semi-raw values. */
409 #define _FP_ADD_INTERNAL(fs, wc, R, X, Y, OP) \
412 _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
413 _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
414 if (X##_s == Y##_s) \
418 int _FP_ADD_INTERNAL_ediff = X##_e - Y##_e; \
419 if (_FP_ADD_INTERNAL_ediff > 0) \
424 /* Y is zero or denormalized. */ \
425 if (_FP_FRAC_ZEROP_##wc (Y)) \
427 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
428 _FP_FRAC_COPY_##wc (R, X); \
433 FP_SET_EXCEPTION (FP_EX_DENORM); \
434 _FP_ADD_INTERNAL_ediff--; \
435 if (_FP_ADD_INTERNAL_ediff == 0) \
437 _FP_FRAC_ADD_##wc (R, X, Y); \
440 if (X##_e == _FP_EXPMAX_##fs) \
442 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
443 _FP_FRAC_COPY_##wc (R, X); \
449 else if (X##_e == _FP_EXPMAX_##fs) \
451 /* X is NaN or Inf, Y is normal. */ \
452 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
453 _FP_FRAC_COPY_##wc (R, X); \
457 /* Insert implicit MSB of Y. */ \
458 _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
461 /* Shift the mantissa of Y to the right \
462 _FP_ADD_INTERNAL_EDIFF steps; remember to account \
463 later for the implicit MSB of X. */ \
464 if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
465 _FP_FRAC_SRS_##wc (Y, _FP_ADD_INTERNAL_ediff, \
466 _FP_WFRACBITS_##fs); \
467 else if (!_FP_FRAC_ZEROP_##wc (Y)) \
468 _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
469 _FP_FRAC_ADD_##wc (R, X, Y); \
471 else if (_FP_ADD_INTERNAL_ediff < 0) \
473 _FP_ADD_INTERNAL_ediff = -_FP_ADD_INTERNAL_ediff; \
477 /* X is zero or denormalized. */ \
478 if (_FP_FRAC_ZEROP_##wc (X)) \
480 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
481 _FP_FRAC_COPY_##wc (R, Y); \
486 FP_SET_EXCEPTION (FP_EX_DENORM); \
487 _FP_ADD_INTERNAL_ediff--; \
488 if (_FP_ADD_INTERNAL_ediff == 0) \
490 _FP_FRAC_ADD_##wc (R, Y, X); \
493 if (Y##_e == _FP_EXPMAX_##fs) \
495 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
496 _FP_FRAC_COPY_##wc (R, Y); \
502 else if (Y##_e == _FP_EXPMAX_##fs) \
504 /* Y is NaN or Inf, X is normal. */ \
505 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
506 _FP_FRAC_COPY_##wc (R, Y); \
510 /* Insert implicit MSB of X. */ \
511 _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
514 /* Shift the mantissa of X to the right \
515 _FP_ADD_INTERNAL_EDIFF steps; remember to account \
516 later for the implicit MSB of Y. */ \
517 if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
518 _FP_FRAC_SRS_##wc (X, _FP_ADD_INTERNAL_ediff, \
519 _FP_WFRACBITS_##fs); \
520 else if (!_FP_FRAC_ZEROP_##wc (X)) \
521 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
522 _FP_FRAC_ADD_##wc (R, Y, X); \
526 /* _FP_ADD_INTERNAL_ediff == 0. */ \
527 if (!_FP_EXP_NORMAL (fs, wc, X)) \
531 /* X and Y are zero or denormalized. */ \
533 if (_FP_FRAC_ZEROP_##wc (X)) \
535 if (!_FP_FRAC_ZEROP_##wc (Y)) \
536 FP_SET_EXCEPTION (FP_EX_DENORM); \
537 _FP_FRAC_COPY_##wc (R, Y); \
540 else if (_FP_FRAC_ZEROP_##wc (Y)) \
542 FP_SET_EXCEPTION (FP_EX_DENORM); \
543 _FP_FRAC_COPY_##wc (R, X); \
548 FP_SET_EXCEPTION (FP_EX_DENORM); \
549 _FP_FRAC_ADD_##wc (R, X, Y); \
550 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
552 /* Normalized result. */ \
553 _FP_FRAC_HIGH_##fs (R) \
554 &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
562 /* X and Y are NaN or Inf. */ \
563 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
564 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
565 R##_e = _FP_EXPMAX_##fs; \
566 if (_FP_FRAC_ZEROP_##wc (X)) \
567 _FP_FRAC_COPY_##wc (R, Y); \
568 else if (_FP_FRAC_ZEROP_##wc (Y)) \
569 _FP_FRAC_COPY_##wc (R, X); \
571 _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
575 /* The exponents of X and Y, both normal, are equal. The \
576 implicit MSBs will always add to increase the \
578 _FP_FRAC_ADD_##wc (R, X, Y); \
580 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
581 if (R##_e == _FP_EXPMAX_##fs) \
582 /* Overflow to infinity (depending on rounding mode). */ \
583 _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
587 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
590 _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
592 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
593 if (R##_e == _FP_EXPMAX_##fs) \
594 /* Overflow to infinity (depending on rounding mode). */ \
595 _FP_OVERFLOW_SEMIRAW (fs, wc, R); \
602 int _FP_ADD_INTERNAL_ediff = X##_e - Y##_e; \
603 if (_FP_ADD_INTERNAL_ediff > 0) \
609 /* Y is zero or denormalized. */ \
610 if (_FP_FRAC_ZEROP_##wc (Y)) \
612 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
613 _FP_FRAC_COPY_##wc (R, X); \
618 FP_SET_EXCEPTION (FP_EX_DENORM); \
619 _FP_ADD_INTERNAL_ediff--; \
620 if (_FP_ADD_INTERNAL_ediff == 0) \
622 _FP_FRAC_SUB_##wc (R, X, Y); \
625 if (X##_e == _FP_EXPMAX_##fs) \
627 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
628 _FP_FRAC_COPY_##wc (R, X); \
634 else if (X##_e == _FP_EXPMAX_##fs) \
636 /* X is NaN or Inf, Y is normal. */ \
637 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
638 _FP_FRAC_COPY_##wc (R, X); \
642 /* Insert implicit MSB of Y. */ \
643 _FP_FRAC_HIGH_##fs (Y) |= _FP_IMPLBIT_SH_##fs; \
646 /* Shift the mantissa of Y to the right \
647 _FP_ADD_INTERNAL_EDIFF steps; remember to account \
648 later for the implicit MSB of X. */ \
649 if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
650 _FP_FRAC_SRS_##wc (Y, _FP_ADD_INTERNAL_ediff, \
651 _FP_WFRACBITS_##fs); \
652 else if (!_FP_FRAC_ZEROP_##wc (Y)) \
653 _FP_FRAC_SET_##wc (Y, _FP_MINFRAC_##wc); \
654 _FP_FRAC_SUB_##wc (R, X, Y); \
656 else if (_FP_ADD_INTERNAL_ediff < 0) \
658 _FP_ADD_INTERNAL_ediff = -_FP_ADD_INTERNAL_ediff; \
663 /* X is zero or denormalized. */ \
664 if (_FP_FRAC_ZEROP_##wc (X)) \
666 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
667 _FP_FRAC_COPY_##wc (R, Y); \
672 FP_SET_EXCEPTION (FP_EX_DENORM); \
673 _FP_ADD_INTERNAL_ediff--; \
674 if (_FP_ADD_INTERNAL_ediff == 0) \
676 _FP_FRAC_SUB_##wc (R, Y, X); \
679 if (Y##_e == _FP_EXPMAX_##fs) \
681 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
682 _FP_FRAC_COPY_##wc (R, Y); \
688 else if (Y##_e == _FP_EXPMAX_##fs) \
690 /* Y is NaN or Inf, X is normal. */ \
691 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
692 _FP_FRAC_COPY_##wc (R, Y); \
696 /* Insert implicit MSB of X. */ \
697 _FP_FRAC_HIGH_##fs (X) |= _FP_IMPLBIT_SH_##fs; \
700 /* Shift the mantissa of X to the right \
701 _FP_ADD_INTERNAL_EDIFF steps; remember to account \
702 later for the implicit MSB of Y. */ \
703 if (_FP_ADD_INTERNAL_ediff <= _FP_WFRACBITS_##fs) \
704 _FP_FRAC_SRS_##wc (X, _FP_ADD_INTERNAL_ediff, \
705 _FP_WFRACBITS_##fs); \
706 else if (!_FP_FRAC_ZEROP_##wc (X)) \
707 _FP_FRAC_SET_##wc (X, _FP_MINFRAC_##wc); \
708 _FP_FRAC_SUB_##wc (R, Y, X); \
713 if (!_FP_EXP_NORMAL (fs, wc, X)) \
717 /* X and Y are zero or denormalized. */ \
719 if (_FP_FRAC_ZEROP_##wc (X)) \
721 _FP_FRAC_COPY_##wc (R, Y); \
722 if (_FP_FRAC_ZEROP_##wc (Y)) \
723 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
726 FP_SET_EXCEPTION (FP_EX_DENORM); \
731 else if (_FP_FRAC_ZEROP_##wc (Y)) \
733 FP_SET_EXCEPTION (FP_EX_DENORM); \
734 _FP_FRAC_COPY_##wc (R, X); \
740 FP_SET_EXCEPTION (FP_EX_DENORM); \
741 _FP_FRAC_SUB_##wc (R, X, Y); \
743 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
745 /* |X| < |Y|, negate result. */ \
746 _FP_FRAC_SUB_##wc (R, Y, X); \
749 else if (_FP_FRAC_ZEROP_##wc (R)) \
750 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
756 /* X and Y are NaN or Inf, of opposite signs. */ \
757 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, X); \
758 _FP_CHECK_SIGNAN_SEMIRAW (fs, wc, Y); \
759 R##_e = _FP_EXPMAX_##fs; \
760 if (_FP_FRAC_ZEROP_##wc (X)) \
762 if (_FP_FRAC_ZEROP_##wc (Y)) \
765 R##_s = _FP_NANSIGN_##fs; \
766 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
767 _FP_FRAC_SLL_##wc (R, _FP_WORKBITS); \
768 FP_SET_EXCEPTION (FP_EX_INVALID \
769 | FP_EX_INVALID_ISI); \
775 _FP_FRAC_COPY_##wc (R, Y); \
780 if (_FP_FRAC_ZEROP_##wc (Y)) \
784 _FP_FRAC_COPY_##wc (R, X); \
789 _FP_CHOOSENAN_SEMIRAW (fs, wc, R, X, Y, OP); \
795 /* The exponents of X and Y, both normal, are equal. The \
796 implicit MSBs cancel. */ \
798 _FP_FRAC_SUB_##wc (R, X, Y); \
800 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
802 /* |X| < |Y|, negate result. */ \
803 _FP_FRAC_SUB_##wc (R, Y, X); \
806 else if (_FP_FRAC_ZEROP_##wc (R)) \
809 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
815 if (_FP_FRAC_HIGH_##fs (R) & _FP_IMPLBIT_SH_##fs) \
817 int _FP_ADD_INTERNAL_diff; \
818 /* Carry into most significant bit of larger one of X and Y, \
819 canceling it; renormalize. */ \
820 _FP_FRAC_HIGH_##fs (R) &= _FP_IMPLBIT_SH_##fs - 1; \
822 _FP_FRAC_CLZ_##wc (_FP_ADD_INTERNAL_diff, R); \
823 _FP_ADD_INTERNAL_diff -= _FP_WFRACXBITS_##fs; \
824 _FP_FRAC_SLL_##wc (R, _FP_ADD_INTERNAL_diff); \
825 if (R##_e <= _FP_ADD_INTERNAL_diff) \
827 /* R is denormalized. */ \
828 _FP_ADD_INTERNAL_diff \
829 = _FP_ADD_INTERNAL_diff - R##_e + 1; \
830 _FP_FRAC_SRS_##wc (R, _FP_ADD_INTERNAL_diff, \
831 _FP_WFRACBITS_##fs); \
836 R##_e -= _FP_ADD_INTERNAL_diff; \
837 _FP_FRAC_HIGH_##fs (R) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
845 #define _FP_ADD(fs, wc, R, X, Y) _FP_ADD_INTERNAL (fs, wc, R, X, Y, '+')
846 #define _FP_SUB(fs, wc, R, X, Y) \
849 if (!(Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
851 _FP_ADD_INTERNAL (fs, wc, R, X, Y, '-'); \
856 /* Main negation routine. The input value is raw. */
858 #define _FP_NEG(fs, wc, R, X) \
861 _FP_FRAC_COPY_##wc (R, X); \
868 /* Main multiplication routine. The input values should be cooked. */
870 #define _FP_MUL(fs, wc, R, X, Y) \
873 R##_s = X##_s ^ Y##_s; \
874 R##_e = X##_e + Y##_e + 1; \
875 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
877 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
878 R##_c = FP_CLS_NORMAL; \
880 _FP_MUL_MEAT_##fs (R, X, Y); \
882 if (_FP_FRAC_OVERP_##wc (fs, R)) \
883 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
888 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
889 _FP_CHOOSENAN (fs, wc, R, X, Y, '*'); \
892 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
893 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
894 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
897 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
898 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
899 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
900 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
901 _FP_FRAC_COPY_##wc (R, X); \
905 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
906 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
907 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
910 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
911 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
912 _FP_FRAC_COPY_##wc (R, Y); \
916 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
917 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
918 R##_s = _FP_NANSIGN_##fs; \
919 R##_c = FP_CLS_NAN; \
920 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
921 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_IMZ); \
931 /* Fused multiply-add. The input values should be cooked. */
933 #define _FP_FMA(fs, wc, dwc, R, X, Y, Z) \
936 FP_DECL_##fs (_FP_FMA_T); \
937 _FP_FMA_T##_s = X##_s ^ Y##_s; \
938 _FP_FMA_T##_e = X##_e + Y##_e + 1; \
939 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
941 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
947 _FP_FRAC_COPY_##wc (R, Z); \
952 R##_c = FP_CLS_NORMAL; \
953 R##_s = _FP_FMA_T##_s; \
954 R##_e = _FP_FMA_T##_e; \
956 _FP_MUL_MEAT_##fs (R, X, Y); \
958 if (_FP_FRAC_OVERP_##wc (fs, R)) \
959 _FP_FRAC_SRS_##wc (R, 1, _FP_WFRACBITS_##fs); \
964 case FP_CLS_NORMAL:; \
965 _FP_FRAC_DECL_##dwc (_FP_FMA_TD); \
966 _FP_FRAC_DECL_##dwc (_FP_FMA_ZD); \
967 _FP_FRAC_DECL_##dwc (_FP_FMA_RD); \
968 _FP_MUL_MEAT_DW_##fs (_FP_FMA_TD, X, Y); \
969 R##_e = _FP_FMA_T##_e; \
971 = _FP_FRAC_HIGHBIT_DW_##dwc (fs, _FP_FMA_TD) == 0; \
972 _FP_FMA_T##_e -= _FP_FMA_tsh; \
973 int _FP_FMA_ediff = _FP_FMA_T##_e - Z##_e; \
974 if (_FP_FMA_ediff >= 0) \
977 = _FP_WFRACBITS_##fs - _FP_FMA_tsh - _FP_FMA_ediff; \
978 if (_FP_FMA_shift <= -_FP_WFRACBITS_##fs) \
979 _FP_FRAC_SET_##dwc (_FP_FMA_ZD, _FP_MINFRAC_##dwc); \
982 _FP_FRAC_COPY_##dwc##_##wc (_FP_FMA_ZD, Z); \
983 if (_FP_FMA_shift < 0) \
984 _FP_FRAC_SRS_##dwc (_FP_FMA_ZD, -_FP_FMA_shift, \
985 _FP_WFRACBITS_DW_##fs); \
986 else if (_FP_FMA_shift > 0) \
987 _FP_FRAC_SLL_##dwc (_FP_FMA_ZD, _FP_FMA_shift); \
989 R##_s = _FP_FMA_T##_s; \
990 if (_FP_FMA_T##_s == Z##_s) \
991 _FP_FRAC_ADD_##dwc (_FP_FMA_RD, _FP_FMA_TD, \
995 _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_TD, \
997 if (_FP_FRAC_NEGP_##dwc (_FP_FMA_RD)) \
1000 _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
1009 _FP_FRAC_COPY_##dwc##_##wc (_FP_FMA_ZD, Z); \
1010 _FP_FRAC_SLL_##dwc (_FP_FMA_ZD, _FP_WFRACBITS_##fs); \
1011 int _FP_FMA_shift = -_FP_FMA_ediff - _FP_FMA_tsh; \
1012 if (_FP_FMA_shift >= _FP_WFRACBITS_DW_##fs) \
1013 _FP_FRAC_SET_##dwc (_FP_FMA_TD, _FP_MINFRAC_##dwc); \
1014 else if (_FP_FMA_shift > 0) \
1015 _FP_FRAC_SRS_##dwc (_FP_FMA_TD, _FP_FMA_shift, \
1016 _FP_WFRACBITS_DW_##fs); \
1017 if (Z##_s == _FP_FMA_T##_s) \
1018 _FP_FRAC_ADD_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
1021 _FP_FRAC_SUB_##dwc (_FP_FMA_RD, _FP_FMA_ZD, \
1024 if (_FP_FRAC_ZEROP_##dwc (_FP_FMA_RD)) \
1026 if (_FP_FMA_T##_s == Z##_s) \
1029 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
1030 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1031 R##_c = FP_CLS_ZERO; \
1036 _FP_FRAC_CLZ_##dwc (_FP_FMA_rlz, _FP_FMA_RD); \
1037 _FP_FMA_rlz -= _FP_WFRACXBITS_DW_##fs; \
1038 R##_e -= _FP_FMA_rlz; \
1039 int _FP_FMA_shift = _FP_WFRACBITS_##fs - _FP_FMA_rlz; \
1040 if (_FP_FMA_shift > 0) \
1041 _FP_FRAC_SRS_##dwc (_FP_FMA_RD, _FP_FMA_shift, \
1042 _FP_WFRACBITS_DW_##fs); \
1043 else if (_FP_FMA_shift < 0) \
1044 _FP_FRAC_SLL_##dwc (_FP_FMA_RD, -_FP_FMA_shift); \
1045 _FP_FRAC_COPY_##wc##_##dwc (R, _FP_FMA_RD); \
1046 R##_c = FP_CLS_NORMAL; \
1052 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1053 _FP_CHOOSENAN (fs, wc, _FP_FMA_T, X, Y, '*'); \
1056 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1057 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1058 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1059 _FP_FMA_T##_s = X##_s; \
1061 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1062 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1063 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1064 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1065 _FP_FRAC_COPY_##wc (_FP_FMA_T, X); \
1066 _FP_FMA_T##_c = X##_c; \
1069 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
1070 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1071 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1072 _FP_FMA_T##_s = Y##_s; \
1074 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
1075 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
1076 _FP_FRAC_COPY_##wc (_FP_FMA_T, Y); \
1077 _FP_FMA_T##_c = Y##_c; \
1080 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1081 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1082 _FP_FMA_T##_s = _FP_NANSIGN_##fs; \
1083 _FP_FMA_T##_c = FP_CLS_NAN; \
1084 _FP_FRAC_SET_##wc (_FP_FMA_T, _FP_NANFRAC_##fs); \
1085 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_IMZ_FMA); \
1092 /* T = X * Y is zero, infinity or NaN. */ \
1093 switch (_FP_CLS_COMBINE (_FP_FMA_T##_c, Z##_c)) \
1095 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1096 _FP_CHOOSENAN (fs, wc, R, _FP_FMA_T, Z, '+'); \
1099 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1100 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1101 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1102 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1103 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1104 R##_s = _FP_FMA_T##_s; \
1105 _FP_FRAC_COPY_##wc (R, _FP_FMA_T); \
1106 R##_c = _FP_FMA_T##_c; \
1109 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1110 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1111 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1112 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1114 _FP_FRAC_COPY_##wc (R, Z); \
1118 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1119 if (_FP_FMA_T##_s == Z##_s) \
1122 _FP_FRAC_COPY_##wc (R, Z); \
1127 R##_s = _FP_NANSIGN_##fs; \
1128 R##_c = FP_CLS_NAN; \
1129 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1130 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_ISI); \
1134 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1135 if (_FP_FMA_T##_s == Z##_s) \
1138 R##_s = (FP_ROUNDMODE == FP_RND_MINF); \
1139 _FP_FRAC_COPY_##wc (R, Z); \
1151 /* Main division routine. The input values should be cooked. */
1153 #define _FP_DIV(fs, wc, R, X, Y) \
1156 R##_s = X##_s ^ Y##_s; \
1157 R##_e = X##_e - Y##_e; \
1158 switch (_FP_CLS_COMBINE (X##_c, Y##_c)) \
1160 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NORMAL): \
1161 R##_c = FP_CLS_NORMAL; \
1163 _FP_DIV_MEAT_##fs (R, X, Y); \
1166 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NAN): \
1167 _FP_CHOOSENAN (fs, wc, R, X, Y, '/'); \
1170 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_NORMAL): \
1171 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_INF): \
1172 case _FP_CLS_COMBINE (FP_CLS_NAN, FP_CLS_ZERO): \
1174 _FP_FRAC_COPY_##wc (R, X); \
1178 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_NAN): \
1179 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NAN): \
1180 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NAN): \
1182 _FP_FRAC_COPY_##wc (R, Y); \
1186 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_INF): \
1187 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_INF): \
1188 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_NORMAL): \
1189 R##_c = FP_CLS_ZERO; \
1192 case _FP_CLS_COMBINE (FP_CLS_NORMAL, FP_CLS_ZERO): \
1193 FP_SET_EXCEPTION (FP_EX_DIVZERO); \
1194 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_ZERO): \
1195 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_NORMAL): \
1196 R##_c = FP_CLS_INF; \
1199 case _FP_CLS_COMBINE (FP_CLS_INF, FP_CLS_INF): \
1200 case _FP_CLS_COMBINE (FP_CLS_ZERO, FP_CLS_ZERO): \
1201 R##_s = _FP_NANSIGN_##fs; \
1202 R##_c = FP_CLS_NAN; \
1203 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1204 FP_SET_EXCEPTION (FP_EX_INVALID \
1205 | (X##_c == FP_CLS_INF \
1206 ? FP_EX_INVALID_IDI \
1207 : FP_EX_INVALID_ZDZ)); \
1217 /* Helper for comparisons. EX is 0 not to raise exceptions, 1 to
1218 raise exceptions for signaling NaN operands, 2 to raise exceptions
1219 for all NaN operands. Conditionals are organized to allow the
1220 compiler to optimize away code based on the value of EX. */
1222 #define _FP_CMP_CHECK_NAN(fs, wc, X, Y, ex) \
1225 /* The arguments are unordered, which may or may not result in \
1229 /* At least some cases of unordered arguments result in \
1230 exceptions; check whether this is one. */ \
1231 if (FP_EX_INVALID_SNAN || FP_EX_INVALID_VC) \
1233 /* Check separately for each case of "invalid" \
1236 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_VC); \
1237 if (_FP_ISSIGNAN (fs, wc, X) \
1238 || _FP_ISSIGNAN (fs, wc, Y)) \
1239 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SNAN); \
1241 /* Otherwise, we only need to check whether to raise an \
1242 exception, not which case or cases it is. */ \
1243 else if ((ex) == 2 \
1244 || _FP_ISSIGNAN (fs, wc, X) \
1245 || _FP_ISSIGNAN (fs, wc, Y)) \
1246 FP_SET_EXCEPTION (FP_EX_INVALID); \
1251 /* Main differential comparison routine. The inputs should be raw not
1252 cooked. The return is -1, 0, 1 for normal values, UN
1255 #define _FP_CMP(fs, wc, ret, X, Y, un, ex) \
1258 /* NANs are unordered. */ \
1259 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1260 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1263 _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
1267 int _FP_CMP_is_zero_x; \
1268 int _FP_CMP_is_zero_y; \
1270 _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
1271 _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
1274 = (!X##_e && _FP_FRAC_ZEROP_##wc (X)) ? 1 : 0; \
1276 = (!Y##_e && _FP_FRAC_ZEROP_##wc (Y)) ? 1 : 0; \
1278 if (_FP_CMP_is_zero_x && _FP_CMP_is_zero_y) \
1280 else if (_FP_CMP_is_zero_x) \
1281 (ret) = Y##_s ? 1 : -1; \
1282 else if (_FP_CMP_is_zero_y) \
1283 (ret) = X##_s ? -1 : 1; \
1284 else if (X##_s != Y##_s) \
1285 (ret) = X##_s ? -1 : 1; \
1286 else if (X##_e > Y##_e) \
1287 (ret) = X##_s ? -1 : 1; \
1288 else if (X##_e < Y##_e) \
1289 (ret) = X##_s ? 1 : -1; \
1290 else if (_FP_FRAC_GT_##wc (X, Y)) \
1291 (ret) = X##_s ? -1 : 1; \
1292 else if (_FP_FRAC_GT_##wc (Y, X)) \
1293 (ret) = X##_s ? 1 : -1; \
1301 /* Simplification for strict equality. */
1303 #define _FP_CMP_EQ(fs, wc, ret, X, Y, ex) \
1306 /* NANs are unordered. */ \
1307 if ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1308 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))) \
1311 _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
1315 _FP_CHECK_FLUSH_ZERO (fs, wc, X); \
1316 _FP_CHECK_FLUSH_ZERO (fs, wc, Y); \
1318 (ret) = !(X##_e == Y##_e \
1319 && _FP_FRAC_EQ_##wc (X, Y) \
1320 && (X##_s == Y##_s \
1321 || (!X##_e && _FP_FRAC_ZEROP_##wc (X)))); \
1326 /* Version to test unordered. */
1328 #define _FP_CMP_UNORD(fs, wc, ret, X, Y, ex) \
1331 (ret) = ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1332 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))); \
1334 _FP_CMP_CHECK_NAN (fs, wc, X, Y, (ex)); \
1338 /* Main square root routine. The input value should be cooked. */
1340 #define _FP_SQRT(fs, wc, R, X) \
1343 _FP_FRAC_DECL_##wc (_FP_SQRT_T); \
1344 _FP_FRAC_DECL_##wc (_FP_SQRT_S); \
1345 _FP_W_TYPE _FP_SQRT_q; \
1349 _FP_FRAC_COPY_##wc (R, X); \
1351 R##_c = FP_CLS_NAN; \
1356 R##_s = _FP_NANSIGN_##fs; \
1357 R##_c = FP_CLS_NAN; /* NAN */ \
1358 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1359 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SQRT); \
1364 R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
1369 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
1371 case FP_CLS_NORMAL: \
1375 R##_c = FP_CLS_NAN; /* NAN */ \
1376 R##_s = _FP_NANSIGN_##fs; \
1377 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1378 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SQRT); \
1381 R##_c = FP_CLS_NORMAL; \
1383 _FP_FRAC_SLL_##wc (X, 1); \
1384 R##_e = X##_e >> 1; \
1385 _FP_FRAC_SET_##wc (_FP_SQRT_S, _FP_ZEROFRAC_##wc); \
1386 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1387 _FP_SQRT_q = _FP_OVERFLOW_##fs >> 1; \
1388 _FP_SQRT_MEAT_##wc (R, _FP_SQRT_S, _FP_SQRT_T, X, \
1394 /* Convert from FP to integer. Input is raw. */
1396 /* RSIGNED can have following values:
1397 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
1398 the result is either 0 or (2^rsize)-1 depending on the sign in such
1400 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
1401 NV is set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1402 depending on the sign in such case.
1403 2: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
1404 NV is set plus the result is reduced modulo 2^rsize.
1405 -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
1406 set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1407 depending on the sign in such case. */
1408 #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
1411 if (X##_e < _FP_EXPBIAS_##fs) \
1416 if (!_FP_FRAC_ZEROP_##wc (X)) \
1418 if (!FP_DENORM_ZERO) \
1419 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1420 FP_SET_EXCEPTION (FP_EX_DENORM); \
1424 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1426 else if ((rsigned) == 2 \
1428 >= ((_FP_EXPMAX_##fs \
1429 < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1) \
1431 : _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1))) \
1433 /* Overflow resulting in 0. */ \
1435 FP_SET_EXCEPTION (FP_EX_INVALID \
1436 | FP_EX_INVALID_CVI \
1437 | ((FP_EX_INVALID_SNAN \
1438 && _FP_ISSIGNAN (fs, wc, X)) \
1439 ? FP_EX_INVALID_SNAN \
1442 else if ((rsigned) != 2 \
1443 && (X##_e >= (_FP_EXPMAX_##fs < _FP_EXPBIAS_##fs + (rsize) \
1445 : (_FP_EXPBIAS_##fs + (rsize) \
1446 - ((rsigned) > 0 || X##_s))) \
1447 || (!(rsigned) && X##_s))) \
1449 /* Overflow or converting to the most negative integer. */ \
1453 (r) <<= (rsize) - 1; \
1463 if (_FP_EXPBIAS_##fs + (rsize) - 1 < _FP_EXPMAX_##fs \
1466 && X##_e == _FP_EXPBIAS_##fs + (rsize) - 1) \
1468 /* Possibly converting to most negative integer; check the \
1470 int _FP_TO_INT_inexact = 0; \
1471 (void) ((_FP_FRACBITS_##fs > (rsize)) \
1473 _FP_FRAC_SRST_##wc (X, _FP_TO_INT_inexact, \
1474 _FP_FRACBITS_##fs - (rsize), \
1475 _FP_FRACBITS_##fs); \
1479 if (!_FP_FRAC_ZEROP_##wc (X)) \
1480 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1481 else if (_FP_TO_INT_inexact) \
1482 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1485 FP_SET_EXCEPTION (FP_EX_INVALID \
1486 | FP_EX_INVALID_CVI \
1487 | ((FP_EX_INVALID_SNAN \
1488 && _FP_ISSIGNAN (fs, wc, X)) \
1489 ? FP_EX_INVALID_SNAN \
1494 int _FP_TO_INT_inexact = 0; \
1495 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
1496 if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
1498 _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
1499 (r) <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
1503 _FP_FRAC_SRST_##wc (X, _FP_TO_INT_inexact, \
1504 (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs - 1 \
1506 _FP_FRACBITS_##fs); \
1507 _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
1509 if ((rsigned) && X##_s) \
1511 if ((rsigned) == 2 && X##_e >= _FP_EXPBIAS_##fs + (rsize) - 1) \
1513 /* Overflow or converting to the most negative integer. */ \
1514 if (X##_e > _FP_EXPBIAS_##fs + (rsize) - 1 \
1516 || (r) != (((typeof (r)) 1) << ((rsize) - 1))) \
1518 _FP_TO_INT_inexact = 0; \
1519 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1522 if (_FP_TO_INT_inexact) \
1523 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1528 /* Convert from floating point to integer, rounding according to the
1529 current rounding direction. Input is raw. RSIGNED is as for
1531 #define _FP_TO_INT_ROUND(fs, wc, r, X, rsize, rsigned) \
1534 if (X##_e < _FP_EXPBIAS_##fs) \
1536 int _FP_TO_INT_ROUND_rounds_away = 0; \
1539 if (_FP_FRAC_ZEROP_##wc (X)) \
1542 goto _FP_TO_INT_ROUND_done; \
1546 FP_SET_EXCEPTION (FP_EX_DENORM); \
1547 if (FP_DENORM_ZERO) \
1550 goto _FP_TO_INT_ROUND_done; \
1554 /* The result is 0, 1 or -1 depending on the rounding mode; \
1555 -1 may cause overflow in the unsigned case. */ \
1556 switch (FP_ROUNDMODE) \
1558 case FP_RND_NEAREST: \
1559 _FP_TO_INT_ROUND_rounds_away \
1560 = (X##_e == _FP_EXPBIAS_##fs - 1 \
1561 && !_FP_FRAC_ZEROP_##wc (X)); \
1564 /* _FP_TO_INT_ROUND_rounds_away is already 0. */ \
1567 _FP_TO_INT_ROUND_rounds_away = !X##_s; \
1570 _FP_TO_INT_ROUND_rounds_away = X##_s; \
1573 if ((rsigned) == 0 && _FP_TO_INT_ROUND_rounds_away && X##_s) \
1575 /* Result of -1 for an unsigned conversion. */ \
1577 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1579 else if ((rsize) == 1 && (rsigned) > 0 \
1580 && _FP_TO_INT_ROUND_rounds_away && !X##_s) \
1582 /* Converting to a 1-bit signed bit-field, which cannot \
1584 (r) = ((rsigned) == 2 ? -1 : 0); \
1585 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1589 (r) = (_FP_TO_INT_ROUND_rounds_away \
1590 ? (X##_s ? -1 : 1) \
1592 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1595 else if ((rsigned) == 2 \
1597 >= ((_FP_EXPMAX_##fs \
1598 < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1) \
1600 : _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + (rsize) - 1))) \
1602 /* Overflow resulting in 0. */ \
1604 FP_SET_EXCEPTION (FP_EX_INVALID \
1605 | FP_EX_INVALID_CVI \
1606 | ((FP_EX_INVALID_SNAN \
1607 && _FP_ISSIGNAN (fs, wc, X)) \
1608 ? FP_EX_INVALID_SNAN \
1611 else if ((rsigned) != 2 \
1612 && (X##_e >= (_FP_EXPMAX_##fs < _FP_EXPBIAS_##fs + (rsize) \
1614 : (_FP_EXPBIAS_##fs + (rsize) \
1615 - ((rsigned) > 0 && !X##_s))) \
1616 || ((rsigned) == 0 && X##_s))) \
1618 /* Definite overflow (does not require rounding to tell). */ \
1619 if ((rsigned) != 0) \
1622 (r) <<= (rsize) - 1; \
1632 FP_SET_EXCEPTION (FP_EX_INVALID \
1633 | FP_EX_INVALID_CVI \
1634 | ((FP_EX_INVALID_SNAN \
1635 && _FP_ISSIGNAN (fs, wc, X)) \
1636 ? FP_EX_INVALID_SNAN \
1641 /* The value is finite, with magnitude at least 1. If \
1642 the conversion is unsigned, the value is positive. \
1643 If RSIGNED is not 2, the value does not definitely \
1644 overflow by virtue of its exponent, but may still turn \
1645 out to overflow after rounding; if RSIGNED is 2, the \
1646 exponent may be such that the value definitely overflows, \
1647 but at least one mantissa bit will not be shifted out. */ \
1648 int _FP_TO_INT_ROUND_inexact = 0; \
1649 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
1650 if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
1652 /* The value is an integer, no rounding needed. */ \
1653 _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
1654 (r) <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
1658 /* May need to shift in order to round (unless there \
1659 are exactly _FP_WORKBITS fractional bits already). */ \
1660 int _FP_TO_INT_ROUND_rshift \
1661 = (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs \
1662 - 1 - _FP_WORKBITS - X##_e); \
1663 if (_FP_TO_INT_ROUND_rshift > 0) \
1664 _FP_FRAC_SRS_##wc (X, _FP_TO_INT_ROUND_rshift, \
1665 _FP_WFRACBITS_##fs); \
1666 else if (_FP_TO_INT_ROUND_rshift < 0) \
1667 _FP_FRAC_SLL_##wc (X, -_FP_TO_INT_ROUND_rshift); \
1668 /* Round like _FP_ROUND, but setting \
1669 _FP_TO_INT_ROUND_inexact instead of directly setting \
1670 the "inexact" exception, since it may turn out we \
1671 should set "invalid" instead. */ \
1672 if (_FP_FRAC_LOW_##wc (X) & 7) \
1674 _FP_TO_INT_ROUND_inexact = 1; \
1675 switch (FP_ROUNDMODE) \
1677 case FP_RND_NEAREST: \
1678 _FP_ROUND_NEAREST (wc, X); \
1681 _FP_ROUND_ZERO (wc, X); \
1684 _FP_ROUND_PINF (wc, X); \
1687 _FP_ROUND_MINF (wc, X); \
1691 _FP_FRAC_SRL_##wc (X, _FP_WORKBITS); \
1692 _FP_FRAC_ASSEMBLE_##wc ((r), X, (rsize)); \
1694 if ((rsigned) != 0 && X##_s) \
1696 /* An exponent of RSIZE - 1 always needs testing for \
1697 overflow (either directly overflowing, or overflowing \
1698 when rounding up results in 2^RSIZE). An exponent of \
1699 RSIZE - 2 can overflow for positive values when rounding \
1700 up to 2^(RSIZE-1), but cannot overflow for negative \
1701 values. Smaller exponents cannot overflow. */ \
1702 if (X##_e >= (_FP_EXPBIAS_##fs + (rsize) - 1 \
1703 - ((rsigned) > 0 && !X##_s))) \
1705 if (X##_e > _FP_EXPBIAS_##fs + (rsize) - 1 \
1706 || (X##_e == _FP_EXPBIAS_##fs + (rsize) - 1 \
1708 ? (r) != (((typeof (r)) 1) << ((rsize) - 1)) \
1709 : ((rsigned) > 0 || (r) == 0))) \
1712 && X##_e == _FP_EXPBIAS_##fs + (rsize) - 2 \
1713 && (r) == (((typeof (r)) 1) << ((rsize) - 1)))) \
1715 if ((rsigned) != 2) \
1717 if ((rsigned) != 0) \
1720 (r) <<= (rsize) - 1; \
1729 _FP_TO_INT_ROUND_inexact = 0; \
1730 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1733 if (_FP_TO_INT_ROUND_inexact) \
1734 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1736 _FP_TO_INT_ROUND_done: ; \
1740 /* Convert integer to fp. Output is raw. RTYPE is unsigned even if
1742 #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
1747 rtype _FP_FROM_INT_ur; \
1749 if ((X##_s = ((r) < 0))) \
1750 (r) = -(rtype) (r); \
1752 _FP_FROM_INT_ur = (rtype) (r); \
1753 (void) (((rsize) <= _FP_W_TYPE_SIZE) \
1755 int _FP_FROM_INT_lz; \
1756 __FP_CLZ (_FP_FROM_INT_lz, \
1757 (_FP_W_TYPE) _FP_FROM_INT_ur); \
1758 X##_e = (_FP_EXPBIAS_##fs + _FP_W_TYPE_SIZE - 1 \
1759 - _FP_FROM_INT_lz); \
1761 : (((rsize) <= 2 * _FP_W_TYPE_SIZE) \
1763 int _FP_FROM_INT_lz; \
1764 __FP_CLZ_2 (_FP_FROM_INT_lz, \
1765 (_FP_W_TYPE) (_FP_FROM_INT_ur \
1766 >> _FP_W_TYPE_SIZE), \
1767 (_FP_W_TYPE) _FP_FROM_INT_ur); \
1768 X##_e = (_FP_EXPBIAS_##fs + 2 * _FP_W_TYPE_SIZE - 1 \
1769 - _FP_FROM_INT_lz); \
1771 : (abort (), 0))); \
1773 if ((rsize) - 1 + _FP_EXPBIAS_##fs >= _FP_EXPMAX_##fs \
1774 && X##_e >= _FP_EXPMAX_##fs) \
1776 /* Exponent too big; overflow to infinity. (May also \
1777 happen after rounding below.) */ \
1778 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
1779 goto pack_semiraw; \
1782 if ((rsize) <= _FP_FRACBITS_##fs \
1783 || X##_e < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs) \
1785 /* Exactly representable; shift left. */ \
1786 _FP_FRAC_DISASSEMBLE_##wc (X, _FP_FROM_INT_ur, (rsize)); \
1787 if (_FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1 - X##_e > 0) \
1788 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1789 + _FP_FRACBITS_##fs - 1 - X##_e)); \
1793 /* More bits in integer than in floating type; need to \
1795 if (_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 < X##_e) \
1797 = ((_FP_FROM_INT_ur >> (X##_e - _FP_EXPBIAS_##fs \
1798 - _FP_WFRACBITS_##fs + 1)) \
1799 | ((_FP_FROM_INT_ur \
1800 << ((rsize) - (X##_e - _FP_EXPBIAS_##fs \
1801 - _FP_WFRACBITS_##fs + 1))) \
1803 _FP_FRAC_DISASSEMBLE_##wc (X, _FP_FROM_INT_ur, (rsize)); \
1804 if ((_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 - X##_e) > 0) \
1805 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1806 + _FP_WFRACBITS_##fs - 1 - X##_e)); \
1807 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
1809 _FP_PACK_SEMIRAW (fs, wc, X); \
1816 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
1822 /* Extend from a narrower floating-point format to a wider one. Input
1823 and output are raw. */
1824 #define FP_EXTEND(dfs, sfs, dwc, swc, D, S) \
1827 if (_FP_FRACBITS_##dfs < _FP_FRACBITS_##sfs \
1828 || (_FP_EXPMAX_##dfs - _FP_EXPBIAS_##dfs \
1829 < _FP_EXPMAX_##sfs - _FP_EXPBIAS_##sfs) \
1830 || (_FP_EXPBIAS_##dfs < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1 \
1831 && _FP_EXPBIAS_##dfs != _FP_EXPBIAS_##sfs)) \
1834 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1835 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1837 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1838 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs - _FP_FRACBITS_##sfs)); \
1844 _FP_CHECK_FLUSH_ZERO (sfs, swc, S); \
1845 if (_FP_FRAC_ZEROP_##swc (S)) \
1847 else if (_FP_EXPBIAS_##dfs \
1848 < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1) \
1850 FP_SET_EXCEPTION (FP_EX_DENORM); \
1851 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1852 - _FP_FRACBITS_##sfs)); \
1854 if (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW) \
1855 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
1860 FP_SET_EXCEPTION (FP_EX_DENORM); \
1861 _FP_FRAC_CLZ_##swc (FP_EXTEND_lz, S); \
1862 _FP_FRAC_SLL_##dwc (D, \
1863 FP_EXTEND_lz + _FP_FRACBITS_##dfs \
1864 - _FP_FRACTBITS_##sfs); \
1865 D##_e = (_FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs + 1 \
1866 + _FP_FRACXBITS_##sfs - FP_EXTEND_lz); \
1871 D##_e = _FP_EXPMAX_##dfs; \
1872 if (!_FP_FRAC_ZEROP_##swc (S)) \
1874 if (_FP_FRAC_SNANP (sfs, S)) \
1875 FP_SET_EXCEPTION (FP_EX_INVALID \
1876 | FP_EX_INVALID_SNAN); \
1877 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1878 - _FP_FRACBITS_##sfs)); \
1879 _FP_SETQNAN (dfs, dwc, D); \
1886 /* Truncate from a wider floating-point format to a narrower one.
1887 Input and output are semi-raw. */
1888 #define FP_TRUNC(dfs, sfs, dwc, swc, D, S) \
1891 if (_FP_FRACBITS_##sfs < _FP_FRACBITS_##dfs \
1892 || (_FP_EXPBIAS_##sfs < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1 \
1893 && _FP_EXPBIAS_##sfs != _FP_EXPBIAS_##dfs)) \
1896 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1898 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1899 if (D##_e >= _FP_EXPMAX_##dfs) \
1900 _FP_OVERFLOW_SEMIRAW (dfs, dwc, D); \
1905 if (D##_e < 1 - _FP_FRACBITS_##dfs) \
1907 _FP_FRAC_SET_##swc (S, _FP_ZEROFRAC_##swc); \
1908 _FP_FRAC_LOW_##swc (S) |= 1; \
1912 _FP_FRAC_HIGH_##sfs (S) |= _FP_IMPLBIT_SH_##sfs; \
1913 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1914 - _FP_WFRACBITS_##dfs \
1916 _FP_WFRACBITS_##sfs); \
1921 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1922 - _FP_WFRACBITS_##dfs), \
1923 _FP_WFRACBITS_##sfs); \
1924 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1931 _FP_CHECK_FLUSH_ZERO (sfs, swc, S); \
1933 if (_FP_FRAC_ZEROP_##swc (S)) \
1934 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1937 FP_SET_EXCEPTION (FP_EX_DENORM); \
1938 if (_FP_EXPBIAS_##sfs \
1939 < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1) \
1941 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1942 - _FP_WFRACBITS_##dfs), \
1943 _FP_WFRACBITS_##sfs); \
1944 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1948 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1949 _FP_FRAC_LOW_##dwc (D) |= 1; \
1955 D##_e = _FP_EXPMAX_##dfs; \
1956 if (_FP_FRAC_ZEROP_##swc (S)) \
1957 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1960 _FP_CHECK_SIGNAN_SEMIRAW (sfs, swc, S); \
1961 _FP_FRAC_SRL_##swc (S, (_FP_WFRACBITS_##sfs \
1962 - _FP_WFRACBITS_##dfs)); \
1963 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1964 /* Semi-raw NaN must have all workbits cleared. */ \
1965 _FP_FRAC_LOW_##dwc (D) \
1966 &= ~(_FP_W_TYPE) ((1 << _FP_WORKBITS) - 1); \
1967 _FP_SETQNAN_SEMIRAW (dfs, dwc, D); \
1974 /* Helper primitives. */
1976 /* Count leading zeros in a word. */
1979 /* GCC 3.4 and later provide the builtins for us. */
1980 # define __FP_CLZ(r, x) \
1983 if (sizeof (_FP_W_TYPE) == sizeof (unsigned int)) \
1984 (r) = __builtin_clz (x); \
1985 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long)) \
1986 (r) = __builtin_clzl (x); \
1987 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long long)) \
1988 (r) = __builtin_clzll (x); \
1993 #endif /* ndef __FP_CLZ */
1995 #define _FP_DIV_HELP_imm(q, r, n, d) \
1998 (q) = (n) / (d), (r) = (n) % (d); \
2003 /* A restoring bit-by-bit division primitive. */
2005 #define _FP_DIV_MEAT_N_loop(fs, wc, R, X, Y) \
2008 int _FP_DIV_MEAT_N_loop_count = _FP_WFRACBITS_##fs; \
2009 _FP_FRAC_DECL_##wc (_FP_DIV_MEAT_N_loop_u); \
2010 _FP_FRAC_DECL_##wc (_FP_DIV_MEAT_N_loop_v); \
2011 _FP_FRAC_COPY_##wc (_FP_DIV_MEAT_N_loop_u, X); \
2012 _FP_FRAC_COPY_##wc (_FP_DIV_MEAT_N_loop_v, Y); \
2013 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
2014 /* Normalize _FP_DIV_MEAT_N_LOOP_U and _FP_DIV_MEAT_N_LOOP_V. */ \
2015 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_u, _FP_WFRACXBITS_##fs); \
2016 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_v, _FP_WFRACXBITS_##fs); \
2017 /* First round. Since the operands are normalized, either the \
2018 first or second bit will be set in the fraction. Produce a \
2019 normalized result by checking which and adjusting the loop \
2020 count and exponent accordingly. */ \
2021 if (_FP_FRAC_GE_1 (_FP_DIV_MEAT_N_loop_u, _FP_DIV_MEAT_N_loop_v)) \
2023 _FP_FRAC_SUB_##wc (_FP_DIV_MEAT_N_loop_u, \
2024 _FP_DIV_MEAT_N_loop_u, \
2025 _FP_DIV_MEAT_N_loop_v); \
2026 _FP_FRAC_LOW_##wc (R) |= 1; \
2027 _FP_DIV_MEAT_N_loop_count--; \
2031 /* Subsequent rounds. */ \
2034 int _FP_DIV_MEAT_N_loop_msb \
2035 = (_FP_WS_TYPE) _FP_FRAC_HIGH_##wc (_FP_DIV_MEAT_N_loop_u) < 0; \
2036 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_u, 1); \
2037 _FP_FRAC_SLL_##wc (R, 1); \
2038 if (_FP_DIV_MEAT_N_loop_msb \
2039 || _FP_FRAC_GE_1 (_FP_DIV_MEAT_N_loop_u, \
2040 _FP_DIV_MEAT_N_loop_v)) \
2042 _FP_FRAC_SUB_##wc (_FP_DIV_MEAT_N_loop_u, \
2043 _FP_DIV_MEAT_N_loop_u, \
2044 _FP_DIV_MEAT_N_loop_v); \
2045 _FP_FRAC_LOW_##wc (R) |= 1; \
2048 while (--_FP_DIV_MEAT_N_loop_count > 0); \
2049 /* If there's anything left in _FP_DIV_MEAT_N_LOOP_U, the result \
2051 _FP_FRAC_LOW_##wc (R) \
2052 |= !_FP_FRAC_ZEROP_##wc (_FP_DIV_MEAT_N_loop_u); \
2056 #define _FP_DIV_MEAT_1_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 1, R, X, Y)
2057 #define _FP_DIV_MEAT_2_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 2, R, X, Y)
2058 #define _FP_DIV_MEAT_4_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 4, R, X, Y)