]>
Commit | Line | Data |
---|---|---|
b75bc69c JM |
1 | /* Round double value to long int. |
2 | Copyright (C) 1997-2015 Free Software Foundation, Inc. | |
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 | |
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. | |
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 | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <math.h> | |
20 | ||
21 | #include <math_private.h> | |
22 | ||
23 | /* For LP64, lround is an alias for llround. */ | |
24 | #ifndef _LP64 | |
25 | ||
26 | long int | |
27 | __lround (double x) | |
28 | { | |
29 | int32_t j0; | |
30 | int64_t i0; | |
31 | long int result; | |
32 | int sign; | |
33 | ||
34 | EXTRACT_WORDS64 (i0, x); | |
35 | j0 = ((i0 >> 52) & 0x7ff) - 0x3ff; | |
36 | sign = i0 < 0 ? -1 : 1; | |
37 | i0 &= UINT64_C(0xfffffffffffff); | |
38 | i0 |= UINT64_C(0x10000000000000); | |
39 | ||
40 | if (j0 < (int32_t) (8 * sizeof (long int)) - 1) | |
41 | { | |
42 | if (j0 < 0) | |
43 | return j0 < -1 ? 0 : sign; | |
44 | else if (j0 >= 52) | |
45 | result = i0 << (j0 - 52); | |
46 | else | |
47 | { | |
48 | i0 += UINT64_C(0x8000000000000) >> j0; | |
49 | ||
50 | result = i0 >> (52 - j0); | |
51 | } | |
52 | } | |
53 | else | |
54 | { | |
55 | /* The number is too large. It is left implementation defined | |
56 | what happens. */ | |
57 | return (long int) x; | |
58 | } | |
59 | ||
60 | return sign * result; | |
61 | } | |
62 | ||
63 | weak_alias (__lround, lround) | |
64 | # ifdef NO_LONG_DOUBLE | |
65 | strong_alias (__lround, __lroundl) | |
66 | weak_alias (__lround, lroundl) | |
67 | # endif | |
68 | ||
69 | #endif |