]>
Commit | Line | Data |
---|---|---|
b9b49b44 | 1 | /* Round a double value to a long long in the current rounding mode. |
bfff8b1b | 2 | Copyright (C) 1997-2017 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 | |
e8dab947 | 19 | #include <limits.h> |
f964490f RM |
20 | #include <math.h> |
21 | #include <math_ldbl_opt.h> | |
e8dab947 JM |
22 | #include <math_private.h> |
23 | #include <stdint.h> | |
0413b54c UD |
24 | |
25 | long long int | |
b9b49b44 | 26 | __llrint (double x) |
0413b54c | 27 | { |
e8dab947 JM |
28 | double rx = __rint (x); |
29 | if (HAVE_PPC_FCTIDZ || rx != x) | |
30 | return (long long int) rx; | |
31 | else | |
32 | { | |
33 | /* Avoid incorrect exceptions from libgcc conversions (as of GCC | |
34 | 5): <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59412>. */ | |
35 | if (fabs (rx) < 0x1p31) | |
36 | return (long long int) (long int) rx; | |
37 | uint64_t i0; | |
38 | EXTRACT_WORDS64 (i0, rx); | |
39 | int exponent = ((i0 >> 52) & 0x7ff) - 0x3ff; | |
40 | if (exponent < 63) | |
41 | { | |
42 | unsigned long long int mant | |
43 | = (i0 & ((1ULL << 52) - 1)) | (1ULL << 52); | |
44 | if (exponent < 52) | |
45 | mant >>= 52 - exponent; | |
46 | else | |
47 | mant <<= exponent - 52; | |
48 | return (long long int) ((i0 & (1ULL << 63)) != 0 ? -mant : mant); | |
49 | } | |
50 | else if (rx == (double) LLONG_MIN) | |
51 | return LLONG_MIN; | |
52 | else | |
53 | return (long long int) (long int) rx << 32; | |
54 | } | |
0413b54c UD |
55 | } |
56 | weak_alias (__llrint, llrint) | |
b9b49b44 UD |
57 | #ifdef NO_LONG_DOUBLE |
58 | strong_alias (__llrint, __llrintl) | |
59 | weak_alias (__llrint, llrintl) | |
60 | #endif | |
f964490f RM |
61 | #if LONG_DOUBLE_COMPAT(libm, GLIBC_2_1) |
62 | compat_symbol (libm, __llrint, llrintl, GLIBC_2_1); | |
63 | #endif |