]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/ieee754/ldbl-128ibm/s_lroundl.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / ieee754 / ldbl-128ibm / s_lroundl.c
CommitLineData
fb146a76
UD
1/* Round to long int long double floating-point values.
2 IBM extended format long double version.
2b778ceb 3 Copyright (C) 2006-2021 Free Software Foundation, Inc.
fb146a76
UD
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
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
fb146a76 19
f964490f 20#include <math.h>
4a3d3999 21#include <fenv.h>
0d13dfa1 22#include <math_private.h>
f964490f 23#include <math_ldbl_opt.h>
fb146a76
UD
24#include <float.h>
25#include <ieee754.h>
f964490f 26
fb146a76
UD
27long
28__lroundl (long double x)
f964490f 29{
fb146a76
UD
30 double xh, xl;
31 long res, hi, lo;
32
33 ldbl_unpack (x, &xh, &xl);
34
35 /* Limit the range of values handled by the conversion to long.
36 We do this because we aren't sure whether that conversion properly
37 raises FE_INVALID. */
38 if (
39#if __LONG_MAX__ == 2147483647
40 __builtin_expect
41 ((__builtin_fabs (xh) <= (double) __LONG_MAX__ + 2), 1)
42#else
43 __builtin_expect
44 ((__builtin_fabs (xh) <= -(double) (-__LONG_MAX__ - 1)), 1)
45#endif
46#if !defined (FE_INVALID)
47 || 1
48#endif
49 )
50 {
51#if __LONG_MAX__ == 2147483647
52 long long llhi = (long long) xh;
53 if (llhi != (long) llhi)
54 hi = llhi < 0 ? -__LONG_MAX__ - 1 : __LONG_MAX__;
55 else
56 hi = llhi;
57 xh -= hi;
58#else
a1ffb40e 59 if (__glibc_unlikely ((xh == -(double) (-__LONG_MAX__ - 1))))
fb146a76
UD
60 {
61 /* When XH is 9223372036854775808.0, converting to long long will
62 overflow, resulting in an invalid operation. However, XL might
63 be negative and of sufficient magnitude that the overall long
64 double is in fact in range. Avoid raising an exception. In any
65 case we need to convert this value specially, because
66 the converted value is not exactly represented as a double
67 thus subtracting HI from XH suffers rounding error. */
68 hi = __LONG_MAX__;
69 xh = 1.0;
70 }
71 else
72 {
73 hi = (long) xh;
74 xh -= hi;
75 }
76#endif
77 ldbl_canonicalize (&xh, &xl);
78
79 lo = (long) xh;
80
81 /* Peg at max/min values, assuming that the above conversions do so.
82 Strictly speaking, we can return anything for values that overflow,
83 but this is more useful. */
12727488 84 res = (long int) ((unsigned long int) hi + (unsigned long int) lo);
fb146a76
UD
85
86 /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */
a1ffb40e 87 if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0)))
fb146a76
UD
88 goto overflow;
89
90 xh -= lo;
91 ldbl_canonicalize (&xh, &xl);
92
93 hi = res;
94 if (xh > 0.5)
95 {
12727488 96 res += 1UL;
fb146a76
UD
97 }
98 else if (xh == 0.5)
99 {
100 if (xl > 0.0 || (xl == 0.0 && res >= 0))
12727488 101 res += 1UL;
fb146a76
UD
102 }
103 else if (-xh > 0.5)
104 {
12727488 105 res -= 1UL;
fb146a76
UD
106 }
107 else if (-xh == 0.5)
108 {
109 if (xl < 0.0 || (xl == 0.0 && res <= 0))
12727488 110 res -= 1UL;
fb146a76
UD
111 }
112
a1ffb40e 113 if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0)))
fb146a76
UD
114 goto overflow;
115
116 return res;
117 }
118 else
119 {
120 if (xh > 0.0)
121 hi = __LONG_MAX__;
122 else if (xh < 0.0)
123 hi = -__LONG_MAX__ - 1;
124 else
125 /* Nan */
126 hi = 0;
127 }
128
129overflow:
130#ifdef FE_INVALID
131 feraiseexcept (FE_INVALID);
132#endif
133 return hi;
f964490f 134}
fb146a76 135
f964490f 136long_double_symbol (libm, __lroundl, lroundl);