1/* Round argument to nearest integral value according to current rounding
2 direction.
3 Copyright (C) 1997-2024 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#include <math-use-builtins.h>
28
29
30long int
31__lrintl (_Float128 x)
32{
33#if USE_LRINTL_BUILTIN
34 return __builtin_lrintl (x);
35#else
36 /* Use generic implementation. */
37 static const _Float128 two112[2] =
38 {
39 L(5.19229685853482762853049632922009600E+33), /* 0x406F000000000000, 0 */
40 L(-5.19229685853482762853049632922009600E+33) /* 0xC06F000000000000, 0 */
41 };
42
43 int32_t j0;
44 uint64_t i0,i1;
45 _Float128 w;
46 _Float128 t;
47 long int result;
48 int sx;
49
50 GET_LDOUBLE_WORDS64 (i0, i1, x);
51 j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
52 sx = i0 >> 63;
53 i0 &= 0x0000ffffffffffffLL;
54 i0 |= 0x0001000000000000LL;
55
56 if (j0 < (int32_t) (8 * sizeof (long int)) - 1)
57 {
58 if (j0 < 48)
59 {
60#if defined FE_INVALID || defined FE_INEXACT
61 /* X < LONG_MAX + 1 implied by J0 < 31. */
62 if (sizeof (long int) == 4
63 && x > (_Float128) LONG_MAX)
64 {
65 /* In the event of overflow we must raise the "invalid"
66 exception, but not "inexact". */
67 t = __nearbyintl (x);
68 feraiseexcept (excepts: t == LONG_MAX ? FE_INEXACT : FE_INVALID);
69 }
70 else
71#endif
72 {
73 w = two112[sx] + x;
74 t = w - two112[sx];
75 }
76 GET_LDOUBLE_WORDS64 (i0, i1, t);
77 j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
78 i0 &= 0x0000ffffffffffffLL;
79 i0 |= 0x0001000000000000LL;
80
81 result = (j0 < 0 ? 0 : i0 >> (48 - j0));
82 }
83 else if (j0 >= 112)
84 result = ((long int) i0 << (j0 - 48)) | (i1 << (j0 - 112));
85 else
86 {
87#if defined FE_INVALID || defined FE_INEXACT
88 /* X < LONG_MAX + 1 implied by J0 < 63. */
89 if (sizeof (long int) == 8
90 && x > (_Float128) LONG_MAX)
91 {
92 /* In the event of overflow we must raise the "invalid"
93 exception, but not "inexact". */
94 t = __nearbyintl (x);
95 feraiseexcept (excepts: t == LONG_MAX ? FE_INEXACT : FE_INVALID);
96 }
97 else
98#endif
99 {
100 w = two112[sx] + x;
101 t = w - two112[sx];
102 }
103 GET_LDOUBLE_WORDS64 (i0, i1, t);
104 j0 = ((i0 >> 48) & 0x7fff) - 0x3fff;
105 i0 &= 0x0000ffffffffffffLL;
106 i0 |= 0x0001000000000000LL;
107
108 if (j0 == 48)
109 result = (long int) i0;
110 else
111 result = ((long int) i0 << (j0 - 48)) | (i1 >> (112 - j0));
112 }
113 }
114 else
115 {
116 /* The number is too large. Unless it rounds to LONG_MIN,
117 FE_INVALID must be raised and the return value is
118 unspecified. */
119#if defined FE_INVALID || defined FE_INEXACT
120 if (x < (_Float128) LONG_MIN
121 && x > (_Float128) LONG_MIN - 1)
122 {
123 /* If truncation produces LONG_MIN, the cast will not raise
124 the exception, but may raise "inexact". */
125 t = __nearbyintl (x);
126 feraiseexcept (excepts: t == LONG_MIN ? FE_INEXACT : FE_INVALID);
127 return LONG_MIN;
128 }
129 else if (FIX_LDBL_LONG_CONVERT_OVERFLOW && x != (_Float128) LONG_MIN)
130 {
131 feraiseexcept (FE_INVALID);
132 return sx == 0 ? LONG_MAX : LONG_MIN;
133 }
134
135#endif
136 return (long int) x;
137 }
138
139 return sx ? -result : result;
140#endif /* ! USE_LRINTL_BUILTIN */
141}
142
143libm_alias_ldouble (__lrint, lrint)
144

source code of glibc/sysdeps/ieee754/ldbl-128/s_lrintl.c