]>
Commit | Line | Data |
---|---|---|
63551311 | 1 | /* Round long double to integer away from zero. |
688903eb | 2 | Copyright (C) 1997-2018 Free Software Foundation, Inc. |
63551311 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
63551311 UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
63551311 | 15 | |
41bdb6e2 | 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/>. */ | |
63551311 UD |
19 | |
20 | #include <math.h> | |
21 | ||
1ed0291c | 22 | #include <math_private.h> |
86f9568a | 23 | #include <libm-alias-ldouble.h> |
63551311 UD |
24 | |
25 | ||
63551311 UD |
26 | long double |
27 | __roundl (long double x) | |
28 | { | |
29 | int32_t j0; | |
24ab7723 | 30 | uint32_t se, i1, i0; |
63551311 UD |
31 | |
32 | GET_LDOUBLE_WORDS (se, i0, i1, x); | |
33 | j0 = (se & 0x7fff) - 0x3fff; | |
34 | if (j0 < 31) | |
35 | { | |
36 | if (j0 < 0) | |
37 | { | |
d7826aa1 UD |
38 | se &= 0x8000; |
39 | i0 = i1 = 0; | |
40 | if (j0 == -1) | |
63551311 | 41 | { |
d7826aa1 UD |
42 | se |= 0x3fff; |
43 | i0 = 0x80000000; | |
63551311 UD |
44 | } |
45 | } | |
46 | else | |
47 | { | |
24ab7723 | 48 | uint32_t i = 0x7fffffff >> j0; |
63551311 UD |
49 | if (((i0 & i) | i1) == 0) |
50 | /* X is integral. */ | |
51 | return x; | |
d7826aa1 | 52 | |
24ab7723 | 53 | uint32_t j = i0 + (0x40000000 >> j0); |
d7826aa1 UD |
54 | if (j < i0) |
55 | se += 1; | |
56 | i0 = (j & ~i) | 0x80000000; | |
57 | i1 = 0; | |
63551311 UD |
58 | } |
59 | } | |
60 | else if (j0 > 62) | |
61 | { | |
62 | if (j0 == 0x4000) | |
63 | /* Inf or NaN. */ | |
64 | return x + x; | |
65 | else | |
66 | return x; | |
67 | } | |
68 | else | |
69 | { | |
24ab7723 | 70 | uint32_t i = 0xffffffff >> (j0 - 31); |
63551311 UD |
71 | if ((i1 & i) == 0) |
72 | /* X is integral. */ | |
73 | return x; | |
74 | ||
24ab7723 | 75 | uint32_t j = i1 + (1 << (62 - j0)); |
d7826aa1 | 76 | if (j < i1) |
63551311 | 77 | { |
24ab7723 | 78 | uint32_t k = i0 + 1; |
d7826aa1 | 79 | if (k < i0) |
63551311 | 80 | { |
d7826aa1 UD |
81 | se += 1; |
82 | k |= 0x80000000; | |
63551311 | 83 | } |
d7826aa1 | 84 | i0 = k; |
63551311 | 85 | } |
d7826aa1 | 86 | i1 = j; |
63551311 UD |
87 | i1 &= ~i; |
88 | } | |
89 | ||
90 | SET_LDOUBLE_WORDS (x, se, i0, i1); | |
91 | return x; | |
92 | } | |
86f9568a | 93 | libm_alias_ldouble (__round, round) |