]>
git.ipfire.org Git - thirdparty/glibc.git/blob - soft-fp/op-common.h
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 || (!X##_e && _FP_FRAC_ZEROP_##wc (X)))); \
1325 /* Version to test unordered. */
1327 #define _FP_CMP_UNORD(fs, wc, ret, X, Y, ex) \
1330 ret = ((X##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (X)) \
1331 || (Y##_e == _FP_EXPMAX_##fs && !_FP_FRAC_ZEROP_##wc (Y))); \
1333 _FP_CMP_CHECK_NAN (fs, wc, X, Y, ex); \
1337 /* Main square root routine. The input value should be cooked. */
1339 #define _FP_SQRT(fs, wc, R, X) \
1342 _FP_FRAC_DECL_##wc (_FP_SQRT_T); \
1343 _FP_FRAC_DECL_##wc (_FP_SQRT_S); \
1344 _FP_W_TYPE _FP_SQRT_q; \
1348 _FP_FRAC_COPY_##wc (R, X); \
1350 R##_c = FP_CLS_NAN; \
1355 R##_s = _FP_NANSIGN_##fs; \
1356 R##_c = FP_CLS_NAN; /* NAN */ \
1357 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1358 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SQRT); \
1363 R##_c = FP_CLS_INF; /* sqrt(+inf) = +inf */ \
1368 R##_c = FP_CLS_ZERO; /* sqrt(+-0) = +-0 */ \
1370 case FP_CLS_NORMAL: \
1374 R##_c = FP_CLS_NAN; /* NAN */ \
1375 R##_s = _FP_NANSIGN_##fs; \
1376 _FP_FRAC_SET_##wc (R, _FP_NANFRAC_##fs); \
1377 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_SQRT); \
1380 R##_c = FP_CLS_NORMAL; \
1382 _FP_FRAC_SLL_##wc (X, 1); \
1383 R##_e = X##_e >> 1; \
1384 _FP_FRAC_SET_##wc (_FP_SQRT_S, _FP_ZEROFRAC_##wc); \
1385 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1386 _FP_SQRT_q = _FP_OVERFLOW_##fs >> 1; \
1387 _FP_SQRT_MEAT_##wc (R, _FP_SQRT_S, _FP_SQRT_T, X, \
1393 /* Convert from FP to integer. Input is raw. */
1395 /* RSIGNED can have following values:
1396 0: the number is required to be 0..(2^rsize)-1, if not, NV is set plus
1397 the result is either 0 or (2^rsize)-1 depending on the sign in such
1399 1: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
1400 NV is set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1401 depending on the sign in such case.
1402 2: the number is required to be -(2^(rsize-1))..(2^(rsize-1))-1, if not,
1403 NV is set plus the result is reduced modulo 2^rsize.
1404 -1: the number is required to be -(2^(rsize-1))..(2^rsize)-1, if not, NV is
1405 set plus the result is either -(2^(rsize-1)) or (2^(rsize-1))-1
1406 depending on the sign in such case. */
1407 #define _FP_TO_INT(fs, wc, r, X, rsize, rsigned) \
1410 if (X##_e < _FP_EXPBIAS_##fs) \
1415 if (!_FP_FRAC_ZEROP_##wc (X)) \
1417 if (!FP_DENORM_ZERO) \
1418 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1419 FP_SET_EXCEPTION (FP_EX_DENORM); \
1423 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1425 else if (rsigned == 2 \
1427 >= ((_FP_EXPMAX_##fs \
1428 < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + rsize - 1) \
1430 : _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs + rsize - 1))) \
1432 /* Overflow resulting in 0. */ \
1434 FP_SET_EXCEPTION (FP_EX_INVALID \
1435 | FP_EX_INVALID_CVI \
1436 | ((FP_EX_INVALID_SNAN \
1437 && _FP_ISSIGNAN (fs, wc, X)) \
1438 ? FP_EX_INVALID_SNAN \
1441 else if (rsigned != 2 \
1442 && (X##_e >= (_FP_EXPMAX_##fs < _FP_EXPBIAS_##fs + rsize \
1444 : (_FP_EXPBIAS_##fs + rsize \
1445 - (rsigned > 0 || X##_s))) \
1446 || (!rsigned && X##_s))) \
1448 /* Overflow or converting to the most negative integer. */ \
1462 if (_FP_EXPBIAS_##fs + rsize - 1 < _FP_EXPMAX_##fs \
1465 && X##_e == _FP_EXPBIAS_##fs + rsize - 1) \
1467 /* Possibly converting to most negative integer; check the \
1469 int _FP_TO_INT_inexact = 0; \
1470 (void) ((_FP_FRACBITS_##fs > rsize) \
1472 _FP_FRAC_SRST_##wc (X, _FP_TO_INT_inexact, \
1473 _FP_FRACBITS_##fs - rsize, \
1474 _FP_FRACBITS_##fs); \
1478 if (!_FP_FRAC_ZEROP_##wc (X)) \
1479 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1480 else if (_FP_TO_INT_inexact) \
1481 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1484 FP_SET_EXCEPTION (FP_EX_INVALID \
1485 | FP_EX_INVALID_CVI \
1486 | ((FP_EX_INVALID_SNAN \
1487 && _FP_ISSIGNAN (fs, wc, X)) \
1488 ? FP_EX_INVALID_SNAN \
1493 int _FP_TO_INT_inexact = 0; \
1494 _FP_FRAC_HIGH_RAW_##fs (X) |= _FP_IMPLBIT_##fs; \
1495 if (X##_e >= _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1) \
1497 _FP_FRAC_ASSEMBLE_##wc (r, X, rsize); \
1498 r <<= X##_e - _FP_EXPBIAS_##fs - _FP_FRACBITS_##fs + 1; \
1502 _FP_FRAC_SRST_##wc (X, _FP_TO_INT_inexact, \
1503 (_FP_FRACBITS_##fs + _FP_EXPBIAS_##fs - 1 \
1505 _FP_FRACBITS_##fs); \
1506 _FP_FRAC_ASSEMBLE_##wc (r, X, rsize); \
1508 if (rsigned && X##_s) \
1510 if (rsigned == 2 && X##_e >= _FP_EXPBIAS_##fs + rsize - 1) \
1512 /* Overflow or converting to the most negative integer. */ \
1513 if (X##_e > _FP_EXPBIAS_##fs + rsize - 1 \
1515 || r != (((typeof (r)) 1) << (rsize - 1))) \
1517 _FP_TO_INT_inexact = 0; \
1518 FP_SET_EXCEPTION (FP_EX_INVALID | FP_EX_INVALID_CVI); \
1521 if (_FP_TO_INT_inexact) \
1522 FP_SET_EXCEPTION (FP_EX_INEXACT); \
1527 /* Convert integer to fp. Output is raw. RTYPE is unsigned even if
1529 #define _FP_FROM_INT(fs, wc, X, r, rsize, rtype) \
1534 rtype _FP_FROM_INT_ur; \
1536 if ((X##_s = (r < 0))) \
1539 _FP_FROM_INT_ur = (rtype) r; \
1540 (void) ((rsize <= _FP_W_TYPE_SIZE) \
1542 int _FP_FROM_INT_lz; \
1543 __FP_CLZ (_FP_FROM_INT_lz, \
1544 (_FP_W_TYPE) _FP_FROM_INT_ur); \
1545 X##_e = (_FP_EXPBIAS_##fs + _FP_W_TYPE_SIZE - 1 \
1546 - _FP_FROM_INT_lz); \
1548 : ((rsize <= 2 * _FP_W_TYPE_SIZE) \
1550 int _FP_FROM_INT_lz; \
1551 __FP_CLZ_2 (_FP_FROM_INT_lz, \
1552 (_FP_W_TYPE) (_FP_FROM_INT_ur \
1553 >> _FP_W_TYPE_SIZE), \
1554 (_FP_W_TYPE) _FP_FROM_INT_ur); \
1555 X##_e = (_FP_EXPBIAS_##fs + 2 * _FP_W_TYPE_SIZE - 1 \
1556 - _FP_FROM_INT_lz); \
1558 : (abort (), 0))); \
1560 if (rsize - 1 + _FP_EXPBIAS_##fs >= _FP_EXPMAX_##fs \
1561 && X##_e >= _FP_EXPMAX_##fs) \
1563 /* Exponent too big; overflow to infinity. (May also \
1564 happen after rounding below.) */ \
1565 _FP_OVERFLOW_SEMIRAW (fs, wc, X); \
1566 goto pack_semiraw; \
1569 if (rsize <= _FP_FRACBITS_##fs \
1570 || X##_e < _FP_EXPBIAS_##fs + _FP_FRACBITS_##fs) \
1572 /* Exactly representable; shift left. */ \
1573 _FP_FRAC_DISASSEMBLE_##wc (X, _FP_FROM_INT_ur, rsize); \
1574 if (_FP_EXPBIAS_##fs + _FP_FRACBITS_##fs - 1 - X##_e > 0) \
1575 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1576 + _FP_FRACBITS_##fs - 1 - X##_e)); \
1580 /* More bits in integer than in floating type; need to \
1582 if (_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 < X##_e) \
1584 = ((_FP_FROM_INT_ur >> (X##_e - _FP_EXPBIAS_##fs \
1585 - _FP_WFRACBITS_##fs + 1)) \
1586 | ((_FP_FROM_INT_ur \
1587 << (rsize - (X##_e - _FP_EXPBIAS_##fs \
1588 - _FP_WFRACBITS_##fs + 1))) \
1590 _FP_FRAC_DISASSEMBLE_##wc (X, _FP_FROM_INT_ur, rsize); \
1591 if ((_FP_EXPBIAS_##fs + _FP_WFRACBITS_##fs - 1 - X##_e) > 0) \
1592 _FP_FRAC_SLL_##wc (X, (_FP_EXPBIAS_##fs \
1593 + _FP_WFRACBITS_##fs - 1 - X##_e)); \
1594 _FP_FRAC_HIGH_##fs (X) &= ~(_FP_W_TYPE) _FP_IMPLBIT_SH_##fs; \
1596 _FP_PACK_SEMIRAW (fs, wc, X); \
1603 _FP_FRAC_SET_##wc (X, _FP_ZEROFRAC_##wc); \
1609 /* Extend from a narrower floating-point format to a wider one. Input
1610 and output are raw. */
1611 #define FP_EXTEND(dfs, sfs, dwc, swc, D, S) \
1614 if (_FP_FRACBITS_##dfs < _FP_FRACBITS_##sfs \
1615 || (_FP_EXPMAX_##dfs - _FP_EXPBIAS_##dfs \
1616 < _FP_EXPMAX_##sfs - _FP_EXPBIAS_##sfs) \
1617 || (_FP_EXPBIAS_##dfs < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1 \
1618 && _FP_EXPBIAS_##dfs != _FP_EXPBIAS_##sfs)) \
1621 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1622 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1624 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1625 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs - _FP_FRACBITS_##sfs)); \
1631 _FP_CHECK_FLUSH_ZERO (sfs, swc, S); \
1632 if (_FP_FRAC_ZEROP_##swc (S)) \
1634 else if (_FP_EXPBIAS_##dfs \
1635 < _FP_EXPBIAS_##sfs + _FP_FRACBITS_##sfs - 1) \
1637 FP_SET_EXCEPTION (FP_EX_DENORM); \
1638 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1639 - _FP_FRACBITS_##sfs)); \
1641 if (FP_TRAPPING_EXCEPTIONS & FP_EX_UNDERFLOW) \
1642 FP_SET_EXCEPTION (FP_EX_UNDERFLOW); \
1647 FP_SET_EXCEPTION (FP_EX_DENORM); \
1648 _FP_FRAC_CLZ_##swc (FP_EXTEND_lz, S); \
1649 _FP_FRAC_SLL_##dwc (D, \
1650 FP_EXTEND_lz + _FP_FRACBITS_##dfs \
1651 - _FP_FRACTBITS_##sfs); \
1652 D##_e = (_FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs + 1 \
1653 + _FP_FRACXBITS_##sfs - FP_EXTEND_lz); \
1658 D##_e = _FP_EXPMAX_##dfs; \
1659 if (!_FP_FRAC_ZEROP_##swc (S)) \
1661 if (_FP_FRAC_SNANP (sfs, S)) \
1662 FP_SET_EXCEPTION (FP_EX_INVALID \
1663 | FP_EX_INVALID_SNAN); \
1664 _FP_FRAC_SLL_##dwc (D, (_FP_FRACBITS_##dfs \
1665 - _FP_FRACBITS_##sfs)); \
1666 _FP_SETQNAN (dfs, dwc, D); \
1673 /* Truncate from a wider floating-point format to a narrower one.
1674 Input and output are semi-raw. */
1675 #define FP_TRUNC(dfs, sfs, dwc, swc, D, S) \
1678 if (_FP_FRACBITS_##sfs < _FP_FRACBITS_##dfs \
1679 || (_FP_EXPBIAS_##sfs < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1 \
1680 && _FP_EXPBIAS_##sfs != _FP_EXPBIAS_##dfs)) \
1683 if (_FP_EXP_NORMAL (sfs, swc, S)) \
1685 D##_e = S##_e + _FP_EXPBIAS_##dfs - _FP_EXPBIAS_##sfs; \
1686 if (D##_e >= _FP_EXPMAX_##dfs) \
1687 _FP_OVERFLOW_SEMIRAW (dfs, dwc, D); \
1692 if (D##_e < 1 - _FP_FRACBITS_##dfs) \
1694 _FP_FRAC_SET_##swc (S, _FP_ZEROFRAC_##swc); \
1695 _FP_FRAC_LOW_##swc (S) |= 1; \
1699 _FP_FRAC_HIGH_##sfs (S) |= _FP_IMPLBIT_SH_##sfs; \
1700 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1701 - _FP_WFRACBITS_##dfs \
1703 _FP_WFRACBITS_##sfs); \
1708 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1709 - _FP_WFRACBITS_##dfs), \
1710 _FP_WFRACBITS_##sfs); \
1711 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1718 _FP_CHECK_FLUSH_ZERO (sfs, swc, S); \
1720 if (_FP_FRAC_ZEROP_##swc (S)) \
1721 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1724 FP_SET_EXCEPTION (FP_EX_DENORM); \
1725 if (_FP_EXPBIAS_##sfs \
1726 < _FP_EXPBIAS_##dfs + _FP_FRACBITS_##dfs - 1) \
1728 _FP_FRAC_SRS_##swc (S, (_FP_WFRACBITS_##sfs \
1729 - _FP_WFRACBITS_##dfs), \
1730 _FP_WFRACBITS_##sfs); \
1731 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1735 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1736 _FP_FRAC_LOW_##dwc (D) |= 1; \
1742 D##_e = _FP_EXPMAX_##dfs; \
1743 if (_FP_FRAC_ZEROP_##swc (S)) \
1744 _FP_FRAC_SET_##dwc (D, _FP_ZEROFRAC_##dwc); \
1747 _FP_CHECK_SIGNAN_SEMIRAW (sfs, swc, S); \
1748 _FP_FRAC_SRL_##swc (S, (_FP_WFRACBITS_##sfs \
1749 - _FP_WFRACBITS_##dfs)); \
1750 _FP_FRAC_COPY_##dwc##_##swc (D, S); \
1751 /* Semi-raw NaN must have all workbits cleared. */ \
1752 _FP_FRAC_LOW_##dwc (D) \
1753 &= ~(_FP_W_TYPE) ((1 << _FP_WORKBITS) - 1); \
1754 _FP_SETQNAN_SEMIRAW (dfs, dwc, D); \
1761 /* Helper primitives. */
1763 /* Count leading zeros in a word. */
1766 /* GCC 3.4 and later provide the builtins for us. */
1767 # define __FP_CLZ(r, x) \
1770 if (sizeof (_FP_W_TYPE) == sizeof (unsigned int)) \
1771 r = __builtin_clz (x); \
1772 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long)) \
1773 r = __builtin_clzl (x); \
1774 else if (sizeof (_FP_W_TYPE) == sizeof (unsigned long long)) \
1775 r = __builtin_clzll (x); \
1780 #endif /* ndef __FP_CLZ */
1782 #define _FP_DIV_HELP_imm(q, r, n, d) \
1785 q = n / d, r = n % d; \
1790 /* A restoring bit-by-bit division primitive. */
1792 #define _FP_DIV_MEAT_N_loop(fs, wc, R, X, Y) \
1795 int _FP_DIV_MEAT_N_loop_count = _FP_WFRACBITS_##fs; \
1796 _FP_FRAC_DECL_##wc (_FP_DIV_MEAT_N_loop_u); \
1797 _FP_FRAC_DECL_##wc (_FP_DIV_MEAT_N_loop_v); \
1798 _FP_FRAC_COPY_##wc (_FP_DIV_MEAT_N_loop_u, X); \
1799 _FP_FRAC_COPY_##wc (_FP_DIV_MEAT_N_loop_v, Y); \
1800 _FP_FRAC_SET_##wc (R, _FP_ZEROFRAC_##wc); \
1801 /* Normalize _FP_DIV_MEAT_N_LOOP_U and _FP_DIV_MEAT_N_LOOP_V. */ \
1802 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_u, _FP_WFRACXBITS_##fs); \
1803 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_v, _FP_WFRACXBITS_##fs); \
1804 /* First round. Since the operands are normalized, either the \
1805 first or second bit will be set in the fraction. Produce a \
1806 normalized result by checking which and adjusting the loop \
1807 count and exponent accordingly. */ \
1808 if (_FP_FRAC_GE_1 (_FP_DIV_MEAT_N_loop_u, _FP_DIV_MEAT_N_loop_v)) \
1810 _FP_FRAC_SUB_##wc (_FP_DIV_MEAT_N_loop_u, \
1811 _FP_DIV_MEAT_N_loop_u, \
1812 _FP_DIV_MEAT_N_loop_v); \
1813 _FP_FRAC_LOW_##wc (R) |= 1; \
1814 _FP_DIV_MEAT_N_loop_count--; \
1818 /* Subsequent rounds. */ \
1821 int _FP_DIV_MEAT_N_loop_msb \
1822 = (_FP_WS_TYPE) _FP_FRAC_HIGH_##wc (_FP_DIV_MEAT_N_loop_u) < 0; \
1823 _FP_FRAC_SLL_##wc (_FP_DIV_MEAT_N_loop_u, 1); \
1824 _FP_FRAC_SLL_##wc (R, 1); \
1825 if (_FP_DIV_MEAT_N_loop_msb \
1826 || _FP_FRAC_GE_1 (_FP_DIV_MEAT_N_loop_u, \
1827 _FP_DIV_MEAT_N_loop_v)) \
1829 _FP_FRAC_SUB_##wc (_FP_DIV_MEAT_N_loop_u, \
1830 _FP_DIV_MEAT_N_loop_u, \
1831 _FP_DIV_MEAT_N_loop_v); \
1832 _FP_FRAC_LOW_##wc (R) |= 1; \
1835 while (--_FP_DIV_MEAT_N_loop_count > 0); \
1836 /* If there's anything left in _FP_DIV_MEAT_N_LOOP_U, the result \
1838 _FP_FRAC_LOW_##wc (R) \
1839 |= !_FP_FRAC_ZEROP_##wc (_FP_DIV_MEAT_N_loop_u); \
1843 #define _FP_DIV_MEAT_1_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 1, R, X, Y)
1844 #define _FP_DIV_MEAT_2_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 2, R, X, Y)
1845 #define _FP_DIV_MEAT_4_loop(fs, R, X, Y) _FP_DIV_MEAT_N_loop (fs, 4, R, X, Y)