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