2.9
[glibc/nacl-glibc.git] / sysdeps / ieee754 / ldbl-128ibm / s_llroundl.c
blob9e25024f96407df1f559023865936640c41d9bf6
1 /* Round to long 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 long
29 __llroundl (long double x)
30 #else
31 long long
32 __llroundl (x)
33 long double x;
34 #endif
36 double xh, xl;
37 long long res, hi, lo;
39 ldbl_unpack (x, &xh, &xl);
41 /* Limit the range of values handled by the conversion to long long.
42 We do this because we aren't sure whether that conversion properly
43 raises FE_INVALID. */
44 if (__builtin_expect
45 ((__builtin_fabs (xh) <= -(double) (-__LONG_LONG_MAX__ - 1)), 1)
46 #if !defined (FE_INVALID)
47 || 1
48 #endif
51 if (__builtin_expect ((xh == -(double) (-__LONG_LONG_MAX__ - 1)), 0))
53 /* When XH is 9223372036854775808.0, converting to long long will
54 overflow, resulting in an invalid operation. However, XL might
55 be negative and of sufficient magnitude that the overall long
56 double is in fact in range. Avoid raising an exception. In any
57 case we need to convert this value specially, because
58 the converted value is not exactly represented as a double
59 thus subtracting HI from XH suffers rounding error. */
60 hi = __LONG_LONG_MAX__;
61 xh = 1.0;
63 else
65 hi = (long long) xh;
66 xh -= hi;
68 ldbl_canonicalize (&xh, &xl);
70 lo = (long long) xh;
72 /* Peg at max/min values, assuming that the above conversions do so.
73 Strictly speaking, we can return anything for values that overflow,
74 but this is more useful. */
75 res = hi + lo;
77 /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */
78 if (__builtin_expect (((~(hi ^ lo) & (res ^ hi)) < 0), 0))
79 goto overflow;
81 xh -= lo;
82 ldbl_canonicalize (&xh, &xl);
84 hi = res;
85 if (xh > 0.5)
87 res += 1;
89 else if (xh == 0.5)
91 if (xl > 0.0 || (xl == 0.0 && res >= 0))
92 res += 1;
94 else if (-xh > 0.5)
96 res -= 1;
98 else if (-xh == 0.5)
100 if (xl < 0.0 || (xl == 0.0 && res <= 0))
101 res -= 1;
104 if (__builtin_expect (((~(hi ^ (res - hi)) & (res ^ hi)) < 0), 0))
105 goto overflow;
107 return res;
109 else
111 if (xh > 0.0)
112 hi = __LONG_LONG_MAX__;
113 else if (xh < 0.0)
114 hi = -__LONG_LONG_MAX__ - 1;
115 else
116 /* Nan */
117 hi = 0;
120 overflow:
121 #ifdef FE_INVALID
122 feraiseexcept (FE_INVALID);
123 #endif
124 return hi;
127 long_double_symbol (libm, __llroundl, llroundl);