Rewritten.
[glibc/history.git] / sysdeps / ieee754 / ldbl-128ibm / s_lroundl.c
bloba5251eece098dc87bf26e8bd53924f4683dbb20c
1 /* Round to long int long double floating-point values.
2 IBM extended format long double version.
3 Copyright (C) 2006 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <math.h>
22 #include <fenv_libc.h>
23 #include <math_ldbl_opt.h>
24 #include <float.h>
25 #include <ieee754.h>
27 #ifdef __STDC__
28 long
29 __lroundl (long double x)
30 #else
31 long
32 __lroundl (x)
33 long double x;
34 #endif
36 double xh, xl;
37 long res, hi, lo;
39 ldbl_unpack (x, &xh, &xl);
41 /* Limit the range of values handled by the conversion to long.
42 We do this because we aren't sure whether that conversion properly
43 raises FE_INVALID. */
44 if (
45 #if __LONG_MAX__ == 2147483647
46 __builtin_expect
47 ((__builtin_fabs (xh) <= (double) __LONG_MAX__ + 2), 1)
48 #else
49 __builtin_expect
50 ((__builtin_fabs (xh) <= -(double) (-__LONG_MAX__ - 1)), 1)
51 #endif
52 #if !defined (FE_INVALID)
53 || 1
54 #endif
57 #if __LONG_MAX__ == 2147483647
58 long long llhi = (long long) xh;
59 if (llhi != (long) llhi)
60 hi = llhi < 0 ? -__LONG_MAX__ - 1 : __LONG_MAX__;
61 else
62 hi = llhi;
63 xh -= hi;
64 #else
65 if (__builtin_expect ((xh == -(double) (-__LONG_MAX__ - 1)), 0))
67 /* When XH is 9223372036854775808.0, converting to long long will
68 overflow, resulting in an invalid operation. However, XL might
69 be negative and of sufficient magnitude that the overall long
70 double is in fact in range. Avoid raising an exception. In any
71 case we need to convert this value specially, because
72 the converted value is not exactly represented as a double
73 thus subtracting HI from XH suffers rounding error. */
74 hi = __LONG_MAX__;
75 xh = 1.0;
77 else
79 hi = (long) xh;
80 xh -= hi;
82 #endif
83 ldbl_canonicalize (&xh, &xl);
85 lo = (long) xh;
87 /* Peg at max/min values, assuming that the above conversions do so.
88 Strictly speaking, we can return anything for values that overflow,
89 but this is more useful. */
90 res = hi + lo;
92 /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */
93 if (__builtin_expect (((~(hi ^ lo) & (res ^ hi)) < 0), 0))
94 goto overflow;
96 xh -= lo;
97 ldbl_canonicalize (&xh, &xl);
99 hi = res;
100 if (xh > 0.5)
102 res += 1;
104 else if (xh == 0.5)
106 if (xl > 0.0 || (xl == 0.0 && res >= 0))
107 res += 1;
109 else if (-xh > 0.5)
111 res -= 1;
113 else if (-xh == 0.5)
115 if (xl < 0.0 || (xl == 0.0 && res <= 0))
116 res -= 1;
119 if (__builtin_expect (((~(hi ^ (res - hi)) & (res ^ hi)) < 0), 0))
120 goto overflow;
122 return res;
124 else
126 if (xh > 0.0)
127 hi = __LONG_MAX__;
128 else if (xh < 0.0)
129 hi = -__LONG_MAX__ - 1;
130 else
131 /* Nan */
132 hi = 0;
135 overflow:
136 #ifdef FE_INVALID
137 feraiseexcept (FE_INVALID);
138 #endif
139 return hi;
142 long_double_symbol (libm, __lroundl, lroundl);