]>
Commit | Line | Data |
---|---|---|
f964490f RM |
1 | /* Round to int long double floating-point values. |
2 | IBM extended format long double version. | |
d4697bc9 | 3 | Copyright (C) 2006-2014 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 RM |
19 | |
20 | /* This has been coded in assembler because GCC makes such a mess of it | |
21 | when it's coded in C. */ | |
22 | ||
23 | #include <math.h> | |
5c68d401 | 24 | #include <fenv_libc.h> |
f964490f RM |
25 | #include <math_ldbl_opt.h> |
26 | #include <float.h> | |
27 | #include <ieee754.h> | |
28 | ||
29 | ||
f964490f RM |
30 | long double |
31 | __rintl (long double x) | |
f964490f | 32 | { |
5c68d401 | 33 | double xh, xl, hi, lo; |
f964490f | 34 | |
5c68d401 RM |
35 | ldbl_unpack (x, &xh, &xl); |
36 | ||
37 | /* Return Inf, Nan, +/-0 unchanged. */ | |
38 | if (__builtin_expect (xh != 0.0 | |
39 | && __builtin_isless (__builtin_fabs (xh), | |
40 | __builtin_inf ()), 1)) | |
f964490f | 41 | { |
5c68d401 | 42 | double orig_xh; |
41e8926a | 43 | int save_round = __fegetround (); |
5c68d401 RM |
44 | |
45 | /* Long double arithmetic, including the canonicalisation below, | |
46 | only works in round-to-nearest mode. */ | |
47 | fesetround (FE_TONEAREST); | |
48 | ||
49 | /* Convert the high double to integer. */ | |
50 | orig_xh = xh; | |
51 | hi = ldbl_nearbyint (xh); | |
52 | ||
53 | /* Subtract integral high part from the value. If the low double | |
54 | happens to be exactly 0.5 or -0.5, you might think that this | |
55 | subtraction could result in an incorrect conversion. For | |
56 | instance, subtracting an odd number would cause this function | |
57 | to round in the wrong direction. However, if we have a | |
58 | canonical long double with the low double 0.5 or -0.5, then the | |
59 | high double must be even. */ | |
60 | xh -= hi; | |
61 | ldbl_canonicalize (&xh, &xl); | |
62 | ||
63 | /* Now convert the low double, adjusted for any remainder from the | |
64 | high double. */ | |
65 | lo = ldbl_nearbyint (xh); | |
66 | ||
67 | xh -= lo; | |
68 | ldbl_canonicalize (&xh, &xl); | |
69 | ||
70 | switch (save_round) | |
f964490f | 71 | { |
5c68d401 RM |
72 | case FE_TONEAREST: |
73 | if (xl > 0.0 && xh == 0.5) | |
74 | lo += 1.0; | |
75 | else if (xl < 0.0 && -xh == 0.5) | |
76 | lo -= 1.0; | |
77 | break; | |
78 | ||
79 | case FE_TOWARDZERO: | |
80 | if (orig_xh < 0.0) | |
81 | goto do_up; | |
82 | /* Fall thru */ | |
83 | ||
84 | case FE_DOWNWARD: | |
85 | if (xh < 0.0 || (xh == 0.0 && xl < 0.0)) | |
86 | lo -= 1.0; | |
87 | break; | |
88 | ||
89 | case FE_UPWARD: | |
90 | do_up: | |
91 | if (xh > 0.0 || (xh == 0.0 && xl > 0.0)) | |
92 | lo += 1.0; | |
93 | break; | |
f964490f | 94 | } |
5c68d401 RM |
95 | |
96 | /* Ensure the final value is canonical. In certain cases, | |
97 | rounding causes hi,lo calculated so far to be non-canonical. */ | |
98 | xh = hi; | |
99 | xl = lo; | |
100 | ldbl_canonicalize (&xh, &xl); | |
101 | ||
102 | /* Ensure we return -0 rather than +0 when appropriate. */ | |
103 | if (orig_xh < 0.0) | |
104 | xh = -__builtin_fabs (xh); | |
105 | ||
106 | fesetround (save_round); | |
f964490f RM |
107 | } |
108 | ||
5c68d401 | 109 | return ldbl_pack (xh, xl); |
f964490f RM |
110 | } |
111 | ||
112 | long_double_symbol (libm, __rintl, rintl); |