Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / ieee754 / ldbl-128ibm / s_llroundl.c
blob4a6e2d5f839ccfb8f169e7a068a3bc858dbea0c6
1 /* Round to long long int long double floating-point values.
2 IBM extended format long double version.
3 Copyright (C) 2006-2014 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 <http://www.gnu.org/licenses/>. */
20 #include <math.h>
21 #include <fenv_libc.h>
22 #include <math_ldbl_opt.h>
23 #include <float.h>
24 #include <ieee754.h>
26 long long
27 __llroundl (long double x)
29 double xh, xl;
30 long long res, hi, lo;
32 ldbl_unpack (x, &xh, &xl);
34 /* Limit the range of values handled by the conversion to long long.
35 We do this because we aren't sure whether that conversion properly
36 raises FE_INVALID. */
37 if (__builtin_expect
38 ((__builtin_fabs (xh) <= -(double) (-__LONG_LONG_MAX__ - 1)), 1)
39 #if !defined (FE_INVALID)
40 || 1
41 #endif
44 if (__builtin_expect ((xh == -(double) (-__LONG_LONG_MAX__ - 1)), 0))
46 /* When XH is 9223372036854775808.0, converting to long long will
47 overflow, resulting in an invalid operation. However, XL might
48 be negative and of sufficient magnitude that the overall long
49 double is in fact in range. Avoid raising an exception. In any
50 case we need to convert this value specially, because
51 the converted value is not exactly represented as a double
52 thus subtracting HI from XH suffers rounding error. */
53 hi = __LONG_LONG_MAX__;
54 xh = 1.0;
56 else
58 hi = (long long) xh;
59 xh -= hi;
61 ldbl_canonicalize (&xh, &xl);
63 lo = (long long) xh;
65 /* Peg at max/min values, assuming that the above conversions do so.
66 Strictly speaking, we can return anything for values that overflow,
67 but this is more useful. */
68 res = hi + lo;
70 /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */
71 if (__builtin_expect (((~(hi ^ lo) & (res ^ hi)) < 0), 0))
72 goto overflow;
74 xh -= lo;
75 ldbl_canonicalize (&xh, &xl);
77 hi = res;
78 if (xh > 0.5)
80 res += 1;
82 else if (xh == 0.5)
84 if (xl > 0.0 || (xl == 0.0 && res >= 0))
85 res += 1;
87 else if (-xh > 0.5)
89 res -= 1;
91 else if (-xh == 0.5)
93 if (xl < 0.0 || (xl == 0.0 && res <= 0))
94 res -= 1;
97 if (__builtin_expect (((~(hi ^ (res - hi)) & (res ^ hi)) < 0), 0))
98 goto overflow;
100 return res;
102 else
104 if (xh > 0.0)
105 hi = __LONG_LONG_MAX__;
106 else if (xh < 0.0)
107 hi = -__LONG_LONG_MAX__ - 1;
108 else
109 /* Nan */
110 hi = 0;
113 overflow:
114 #ifdef FE_INVALID
115 feraiseexcept (FE_INVALID);
116 #endif
117 return hi;
120 long_double_symbol (libm, __llroundl, llroundl);