1 /* Round argument to nearest integral value according to current rounding
3 Copyright (C) 1997-2023 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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.
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.
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/>. */
24 #include <math_private.h>
25 #include <libm-alias-ldouble.h>
26 #include <fix-fp-int-convert-overflow.h>
27 #include <math-use-builtins.h>
31 __llrintl (_Float128 x
)
33 #if USE_LLRINTL_BUILTIN
34 return __builtin_llrintl (x
);
36 /* Use generic implementation. */
37 static const _Float128 two112
[2] =
39 L(5.19229685853482762853049632922009600E+33), /* 0x406F000000000000, 0 */
40 L(-5.19229685853482762853049632922009600E+33) /* 0xC06F000000000000, 0 */
50 GET_LDOUBLE_WORDS64 (i0
, i1
, x
);
51 j0
= ((i0
>> 48) & 0x7fff) - 0x3fff;
53 i0
&= 0x0000ffffffffffffLL
;
54 i0
|= 0x0001000000000000LL
;
56 if (j0
< (int32_t) (8 * sizeof (long long int)) - 1)
58 #if defined FE_INVALID || defined FE_INEXACT
59 /* X < LLONG_MAX + 1 implied by J0 < 63. */
60 if (x
> (_Float128
) LLONG_MAX
)
62 /* In the event of overflow we must raise the "invalid"
63 exception, but not "inexact". */
65 feraiseexcept (t
== LLONG_MAX
? FE_INEXACT
: FE_INVALID
);
73 GET_LDOUBLE_WORDS64 (i0
, i1
, t
);
74 j0
= ((i0
>> 48) & 0x7fff) - 0x3fff;
75 i0
&= 0x0000ffffffffffffLL
;
76 i0
|= 0x0001000000000000LL
;
81 result
= i0
>> (48 - j0
);
83 result
= ((long long int) i0
<< (j0
- 48)) | (i1
>> (112 - j0
));
87 /* The number is too large. Unless it rounds to LLONG_MIN,
88 FE_INVALID must be raised and the return value is
90 #if defined FE_INVALID || defined FE_INEXACT
91 if (x
< (_Float128
) LLONG_MIN
92 && x
> (_Float128
) LLONG_MIN
- 1)
94 /* If truncation produces LLONG_MIN, the cast will not raise
95 the exception, but may raise "inexact". */
97 feraiseexcept (t
== LLONG_MIN
? FE_INEXACT
: FE_INVALID
);
100 else if (FIX_LDBL_LLONG_CONVERT_OVERFLOW
&& x
!= (_Float128
) LLONG_MIN
)
102 feraiseexcept (FE_INVALID
);
103 return sx
== 0 ? LLONG_MAX
: LLONG_MIN
;
107 return (long long int) x
;
110 return sx
? -result
: result
;
111 #endif /* ! USE_LLRINTL_BUILTIN */
114 libm_alias_ldouble (__llrint
, llrint
)