]>
Commit | Line | Data |
---|---|---|
b9b49b44 | 1 | /* Round double value to long long int. |
688903eb | 2 | Copyright (C) 1997-2018 Free Software Foundation, Inc. |
0413b54c UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
0413b54c UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
0413b54c | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
0413b54c | 18 | |
d7025bad | 19 | #include <limits.h> |
0413b54c | 20 | #include <math.h> |
fce14d4e | 21 | #include <math_ldbl_opt.h> |
d7025bad JM |
22 | #include <math_private.h> |
23 | #include <stdint.h> | |
d17542d2 | 24 | #include <libm-alias-double.h> |
0413b54c | 25 | |
da13146d AM |
26 | /* Round to the nearest integer, with values exactly on a 0.5 boundary |
27 | rounded away from zero, regardless of the current rounding mode. | |
28 | If (long long)x, when x is out of range of a long long, clips at | |
29 | LLONG_MAX or LLONG_MIN, then this implementation also clips. */ | |
0413b54c UD |
30 | |
31 | long long int | |
b9b49b44 | 32 | __llround (double x) |
0413b54c | 33 | { |
d7025bad JM |
34 | long long xr; |
35 | if (HAVE_PPC_FCTIDZ) | |
36 | xr = (long long) x; | |
37 | else | |
38 | { | |
39 | /* Avoid incorrect exceptions from libgcc conversions (as of GCC | |
40 | 5): <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59412>. */ | |
41 | if (fabs (x) < 0x1p31) | |
42 | xr = (long long int) (long int) x; | |
43 | else | |
44 | { | |
45 | uint64_t i0; | |
46 | EXTRACT_WORDS64 (i0, x); | |
47 | int exponent = ((i0 >> 52) & 0x7ff) - 0x3ff; | |
48 | if (exponent < 63) | |
49 | { | |
50 | unsigned long long int mant | |
51 | = (i0 & ((1ULL << 52) - 1)) | (1ULL << 52); | |
52 | if (exponent < 52) | |
53 | /* llround is not required to raise "inexact". */ | |
54 | mant >>= 52 - exponent; | |
55 | else | |
56 | mant <<= exponent - 52; | |
57 | xr = (long long int) ((i0 & (1ULL << 63)) != 0 ? -mant : mant); | |
58 | } | |
59 | else if (x == (double) LLONG_MIN) | |
60 | xr = LLONG_MIN; | |
61 | else | |
62 | xr = (long long int) (long int) x << 32; | |
63 | } | |
64 | } | |
65 | /* Avoid spurious "inexact" converting LLONG_MAX to double, and from | |
66 | subtraction when the result is out of range, by returning early | |
67 | for arguments large enough that no rounding is needed. */ | |
68 | if (!(fabs (x) < 0x1p52)) | |
69 | return xr; | |
da13146d AM |
70 | double xrf = (double) xr; |
71 | ||
0413b54c | 72 | if (x >= 0.0) |
da13146d AM |
73 | { |
74 | if (x - xrf >= 0.5) | |
75 | xr += (long long) ((unsigned long long) xr + 1) > 0; | |
76 | } | |
0413b54c | 77 | else |
da13146d AM |
78 | { |
79 | if (xrf - x >= 0.5) | |
80 | xr -= (long long) ((unsigned long long) xr - 1) < 0; | |
81 | } | |
82 | return xr; | |
0413b54c | 83 | } |
d17542d2 | 84 | libm_alias_double (__llround, llround) |