]>
Commit | Line | Data |
---|---|---|
41c67149 JM |
1 | /* Round to nearest integer value, rounding halfway cases to even. |
2 | ldbl-128ibm version. | |
2b778ceb | 3 | Copyright (C) 2016-2021 Free Software Foundation, Inc. |
41c67149 JM |
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, see | |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
41c67149 JM |
19 | |
20 | #include <math.h> | |
21 | #include <math_private.h> | |
22 | ||
23 | long double | |
01f28812 | 24 | __roundevenl (long double x) |
41c67149 JM |
25 | { |
26 | double xh, xl, hi; | |
27 | ||
28 | ldbl_unpack (x, &xh, &xl); | |
29 | ||
30 | if (xh != 0 && isfinite (xh)) | |
31 | { | |
01f28812 | 32 | hi = __roundeven (xh); |
41c67149 JM |
33 | if (hi != xh) |
34 | { | |
35 | /* The high part is not an integer; the low part only | |
36 | affects the result if the high part is exactly half way | |
37 | between two integers and the low part is nonzero in the | |
38 | opposite direction to the rounding of the high part. */ | |
39 | double diff = hi - xh; | |
40 | if (fabs (diff) == 0.5) | |
41 | { | |
42 | if (xl < 0 && diff > 0) | |
43 | xh = hi - 1; | |
44 | else if (xl > 0 && diff < 0) | |
45 | xh = hi + 1; | |
46 | else | |
47 | xh = hi; | |
48 | } | |
49 | else | |
50 | xh = hi; | |
51 | xl = 0; | |
52 | } | |
53 | else | |
54 | { | |
55 | /* The high part is a nonzero integer. Rounding the low | |
56 | part to nearest, ties round to even, is always correct, | |
57 | as a high part that is an odd integer together with a low | |
58 | part with magnitude 0.5 is not a valid long double. */ | |
01f28812 | 59 | xl = __roundeven (xl); |
41c67149 JM |
60 | xh = hi; |
61 | ldbl_canonicalize_int (&xh, &xl); | |
62 | } | |
63 | } | |
64 | else | |
65 | /* Quiet signaling NaN arguments. */ | |
66 | xh += xh; | |
67 | ||
68 | return ldbl_pack (xh, xl); | |
69 | } | |
01f28812 | 70 | weak_alias (__roundevenl, roundevenl) |