1 | /* Return the least floating-point number greater than X. |
2 | Copyright (C) 2016-2021 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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <float.h> |
20 | #include <math.h> |
21 | #include <math_private.h> |
22 | #include <libm-alias-ldouble.h> |
23 | |
24 | /* Return the least floating-point number greater than X. */ |
25 | long double |
26 | __nextupl (long double x) |
27 | { |
28 | uint32_t hx, ix; |
29 | uint32_t lx; |
30 | int32_t esx; |
31 | |
32 | GET_LDOUBLE_WORDS (esx, hx, lx, x); |
33 | ix = esx & 0x7fff; |
34 | |
35 | if (((ix == 0x7fff) && (((hx & 0x7fffffff) | lx) != 0))) /* x is nan. */ |
36 | return x + x; |
37 | if ((ix | hx | lx) == 0) |
38 | return LDBL_TRUE_MIN; |
39 | if (esx >= 0) |
40 | { /* x > 0. */ |
41 | if (isinf (x)) |
42 | return x; |
43 | lx += 1; |
44 | if (lx == 0) |
45 | { |
46 | hx += 1; |
47 | #if LDBL_MIN_EXP == -16381 |
48 | if (hx == 0 || (esx == 0 && hx == 0x80000000)) |
49 | #else |
50 | if (hx == 0) |
51 | #endif |
52 | { |
53 | esx += 1; |
54 | hx |= 0x80000000; |
55 | } |
56 | } |
57 | } |
58 | else |
59 | { /* x < 0. */ |
60 | if (lx == 0) |
61 | { |
62 | #if LDBL_MIN_EXP == -16381 |
63 | if (hx <= 0x80000000 && esx != 0xffff8000) |
64 | { |
65 | esx -= 1; |
66 | hx = hx - 1; |
67 | if ((esx & 0x7fff) > 0) |
68 | hx |= 0x80000000; |
69 | } |
70 | else |
71 | hx -= 1; |
72 | #else |
73 | if (ix != 0 && hx == 0x80000000) |
74 | hx = 0; |
75 | if (hx == 0) |
76 | esx -= 1; |
77 | hx -= 1; |
78 | #endif |
79 | } |
80 | lx -= 1; |
81 | } |
82 | SET_LDOUBLE_WORDS (x, esx, hx, lx); |
83 | return x; |
84 | } |
85 | |
86 | libm_alias_ldouble (__nextup, nextup) |
87 | |