1/* Round to long long int long double floating-point values.
2 IBM extended format long double version.
3 Copyright (C) 2006-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 <math.h>
21#include <fenv.h>
22#include <math_private.h>
23#include <math_ldbl_opt.h>
24#include <float.h>
25#include <ieee754.h>
26
27long long
28__llroundl (long double x)
29{
30 double xh, xl;
31 long long res, hi, lo;
32
33 ldbl_unpack (x, &xh, &xl);
34
35 /* Limit the range of values handled by the conversion to long long.
36 We do this because we aren't sure whether that conversion properly
37 raises FE_INVALID. */
38 if (__builtin_expect
39 ((__builtin_fabs (xh) <= -(double) (-__LONG_LONG_MAX__ - 1)), 1)
40#if !defined (FE_INVALID)
41 || 1
42#endif
43 )
44 {
45 if (__glibc_unlikely ((xh == -(double) (-__LONG_LONG_MAX__ - 1))))
46 {
47 /* When XH is 9223372036854775808.0, converting to long long will
48 overflow, resulting in an invalid operation. However, XL might
49 be negative and of sufficient magnitude that the overall long
50 double is in fact in range. Avoid raising an exception. In any
51 case we need to convert this value specially, because
52 the converted value is not exactly represented as a double
53 thus subtracting HI from XH suffers rounding error. */
54 hi = __LONG_LONG_MAX__;
55 xh = 1.0;
56 }
57 else
58 {
59 hi = (long long) xh;
60 xh -= hi;
61 }
62 ldbl_canonicalize (&xh, &xl);
63
64 lo = (long long) xh;
65
66 /* Peg at max/min values, assuming that the above conversions do so.
67 Strictly speaking, we can return anything for values that overflow,
68 but this is more useful. */
69 res = hi + lo;
70
71 /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */
72 if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0)))
73 goto overflow;
74
75 xh -= lo;
76 ldbl_canonicalize (&xh, &xl);
77
78 hi = res;
79 if (xh > 0.5)
80 {
81 res += 1;
82 }
83 else if (xh == 0.5)
84 {
85 if (xl > 0.0 || (xl == 0.0 && res >= 0))
86 res += 1;
87 }
88 else if (-xh > 0.5)
89 {
90 res -= 1;
91 }
92 else if (-xh == 0.5)
93 {
94 if (xl < 0.0 || (xl == 0.0 && res <= 0))
95 res -= 1;
96 }
97
98 if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0)))
99 goto overflow;
100
101 return res;
102 }
103 else
104 {
105 if (xh > 0.0)
106 hi = __LONG_LONG_MAX__;
107 else if (xh < 0.0)
108 hi = -__LONG_LONG_MAX__ - 1;
109 else
110 /* Nan */
111 hi = 0;
112 }
113
114overflow:
115#ifdef FE_INVALID
116 feraiseexcept (FE_INVALID);
117#endif
118 return hi;
119}
120
121long_double_symbol (libm, __llroundl, llroundl);
122

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