3718250b6e0db468369815850e587026b6ad0ae0
[platform/upstream/glibc.git] / sysdeps / ieee754 / ldbl-128ibm / s_llrintl.c
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.
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, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <math.h>
22 #include <fenv_libc.h>
23 #include <math_ldbl_opt.h>
24 #include <float.h>
25 #include <ieee754.h>
26
27
28 #ifdef __STDC__
29 long long
30 __llrintl (long double x)
31 #else
32 long long
33 __llrintl (x)
34      long double x;
35 #endif
36 {
37   double xh, xl;
38   long long res, hi, lo;
39   int save_round;
40
41   ldbl_unpack (x, &xh, &xl);
42
43   /* Limit the range of values handled by the conversion to long long.
44      We do this because we aren't sure whether that conversion properly
45      raises FE_INVALID.  */
46   if (__builtin_expect
47       ((__builtin_fabs (xh) <= -(double) (-__LONG_LONG_MAX__ - 1)), 1)
48 #if !defined (FE_INVALID)
49       || 1
50 #endif
51     )
52     {
53       save_round = fegetround ();
54
55       if (__builtin_expect ((xh == -(double) (-__LONG_LONG_MAX__ - 1)), 0))
56         {
57           /* When XH is 9223372036854775808.0, converting to long long will
58              overflow, resulting in an invalid operation.  However, XL might
59              be negative and of sufficient magnitude that the overall long
60              double is in fact in range.  Avoid raising an exception.  In any
61              case we need to convert this value specially, because
62              the converted value is not exactly represented as a double
63              thus subtracting HI from XH suffers rounding error.  */
64           hi = __LONG_LONG_MAX__;
65           xh = 1.0;
66         }
67       else
68         {
69           hi = (long long) xh;
70           xh -= hi;
71         }
72       ldbl_canonicalize (&xh, &xl);
73
74       lo = (long long) xh;
75
76       /* Peg at max/min values, assuming that the above conversions do so.
77          Strictly speaking, we can return anything for values that overflow,
78          but this is more useful.  */
79       res = hi + lo;
80
81       /* This is just sign(hi) == sign(lo) && sign(res) != sign(hi).  */
82       if (__builtin_expect (((~(hi ^ lo) & (res ^ hi)) < 0), 0))
83         goto overflow;
84
85       xh -= lo;
86       ldbl_canonicalize (&xh, &xl);
87
88       hi = res;
89       switch (save_round)
90         {
91         case FE_TONEAREST:
92           if (fabs (xh) < 0.5
93               || (fabs (xh) == 0.5
94                   && ((xh > 0.0 && xl < 0.0)
95                       || (xh < 0.0 && xl > 0.0)
96                       || (xl == 0.0 && (res & 1) == 0))))
97             return res;
98
99           if (xh < 0.0)
100             res -= 1;
101           else
102             res += 1;
103           break;
104
105         case FE_TOWARDZERO:
106           if (res > 0 && (xh < 0.0 || (xh == 0.0 && xl < 0.0)))
107             res -= 1;
108           else if (res < 0 && (xh > 0.0 || (xh == 0.0 && xl > 0.0)))
109             res += 1;
110           return res;
111           break;
112
113         case FE_UPWARD:
114           if (xh > 0.0 || (xh == 0.0 && xl > 0.0))
115             res += 1;
116           break;
117
118         case FE_DOWNWARD:
119           if (xh < 0.0 || (xh == 0.0 && xl < 0.0))
120             res -= 1;
121           break;
122         }
123
124       if (__builtin_expect (((~(hi ^ (res - hi)) & (res ^ hi)) < 0), 0))
125         goto overflow;
126
127       return res;
128     }
129   else
130     {
131       if (xh > 0.0)
132         hi = __LONG_LONG_MAX__;
133       else if (xh < 0.0)
134         hi = -__LONG_LONG_MAX__ - 1;
135       else
136         /* Nan */
137         hi = 0;
138     }
139
140 overflow:
141 #ifdef FE_INVALID
142   feraiseexcept (FE_INVALID);
143 #endif
144   return hi;
145 }
146
147 long_double_symbol (libm, __llrintl, llrintl);