| 1 | /* Round argument to nearest integral value according to current rounding |
| 2 | direction. |
| 3 | Copyright (C) 1997-2022 Free Software Foundation, Inc. |
| 4 | This file is part of the GNU C Library. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <https://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <fenv.h> |
| 21 | #include <limits.h> |
| 22 | #include <math.h> |
| 23 | |
| 24 | #include <math_private.h> |
| 25 | #include <libm-alias-ldouble.h> |
| 26 | #include <fix-fp-int-convert-overflow.h> |
| 27 | |
| 28 | static const _Float128 two112[2] = |
| 29 | { |
| 30 | L(5.19229685853482762853049632922009600E+33), /* 0x406F000000000000, 0 */ |
| 31 | L(-5.19229685853482762853049632922009600E+33) /* 0xC06F000000000000, 0 */ |
| 32 | }; |
| 33 | |
| 34 | long int |
| 35 | __lrintl (_Float128 x) |
| 36 | { |
| 37 | int32_t j0; |
| 38 | uint64_t i0,i1; |
| 39 | _Float128 w; |
| 40 | _Float128 t; |
| 41 | long int result; |
| 42 | int sx; |
| 43 | |
| 44 | GET_LDOUBLE_WORDS64 (i0, i1, x); |
| 45 | j0 = ((i0 >> 48) & 0x7fff) - 0x3fff; |
| 46 | sx = i0 >> 63; |
| 47 | i0 &= 0x0000ffffffffffffLL; |
| 48 | i0 |= 0x0001000000000000LL; |
| 49 | |
| 50 | if (j0 < (int32_t) (8 * sizeof (long int)) - 1) |
| 51 | { |
| 52 | if (j0 < 48) |
| 53 | { |
| 54 | #if defined FE_INVALID || defined FE_INEXACT |
| 55 | /* X < LONG_MAX + 1 implied by J0 < 31. */ |
| 56 | if (sizeof (long int) == 4 |
| 57 | && x > (_Float128) LONG_MAX) |
| 58 | { |
| 59 | /* In the event of overflow we must raise the "invalid" |
| 60 | exception, but not "inexact". */ |
| 61 | t = __nearbyintl (x); |
| 62 | feraiseexcept (t == LONG_MAX ? FE_INEXACT : FE_INVALID); |
| 63 | } |
| 64 | else |
| 65 | #endif |
| 66 | { |
| 67 | w = two112[sx] + x; |
| 68 | t = w - two112[sx]; |
| 69 | } |
| 70 | GET_LDOUBLE_WORDS64 (i0, i1, t); |
| 71 | j0 = ((i0 >> 48) & 0x7fff) - 0x3fff; |
| 72 | i0 &= 0x0000ffffffffffffLL; |
| 73 | i0 |= 0x0001000000000000LL; |
| 74 | |
| 75 | result = (j0 < 0 ? 0 : i0 >> (48 - j0)); |
| 76 | } |
| 77 | else if (j0 >= 112) |
| 78 | result = ((long int) i0 << (j0 - 48)) | (i1 << (j0 - 112)); |
| 79 | else |
| 80 | { |
| 81 | #if defined FE_INVALID || defined FE_INEXACT |
| 82 | /* X < LONG_MAX + 1 implied by J0 < 63. */ |
| 83 | if (sizeof (long int) == 8 |
| 84 | && x > (_Float128) LONG_MAX) |
| 85 | { |
| 86 | /* In the event of overflow we must raise the "invalid" |
| 87 | exception, but not "inexact". */ |
| 88 | t = __nearbyintl (x); |
| 89 | feraiseexcept (t == LONG_MAX ? FE_INEXACT : FE_INVALID); |
| 90 | } |
| 91 | else |
| 92 | #endif |
| 93 | { |
| 94 | w = two112[sx] + x; |
| 95 | t = w - two112[sx]; |
| 96 | } |
| 97 | GET_LDOUBLE_WORDS64 (i0, i1, t); |
| 98 | j0 = ((i0 >> 48) & 0x7fff) - 0x3fff; |
| 99 | i0 &= 0x0000ffffffffffffLL; |
| 100 | i0 |= 0x0001000000000000LL; |
| 101 | |
| 102 | if (j0 == 48) |
| 103 | result = (long int) i0; |
| 104 | else |
| 105 | result = ((long int) i0 << (j0 - 48)) | (i1 >> (112 - j0)); |
| 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | /* The number is too large. Unless it rounds to LONG_MIN, |
| 111 | FE_INVALID must be raised and the return value is |
| 112 | unspecified. */ |
| 113 | #if defined FE_INVALID || defined FE_INEXACT |
| 114 | if (x < (_Float128) LONG_MIN |
| 115 | && x > (_Float128) LONG_MIN - 1) |
| 116 | { |
| 117 | /* If truncation produces LONG_MIN, the cast will not raise |
| 118 | the exception, but may raise "inexact". */ |
| 119 | t = __nearbyintl (x); |
| 120 | feraiseexcept (t == LONG_MIN ? FE_INEXACT : FE_INVALID); |
| 121 | return LONG_MIN; |
| 122 | } |
| 123 | else if (FIX_LDBL_LONG_CONVERT_OVERFLOW && x != (_Float128) LONG_MIN) |
| 124 | { |
| 125 | feraiseexcept (FE_INVALID); |
| 126 | return sx == 0 ? LONG_MAX : LONG_MIN; |
| 127 | } |
| 128 | |
| 129 | #endif |
| 130 | return (long int) x; |
| 131 | } |
| 132 | |
| 133 | return sx ? -result : result; |
| 134 | } |
| 135 | |
| 136 | libm_alias_ldouble (__lrint, lrint) |
| 137 | |