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