1 | /* Return the least floating-point number greater than X. |
2 | Copyright (C) 2016-2022 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 <math_ldbl_opt.h> |
23 | |
24 | /* Return the least floating-point number greater than X. */ |
25 | long double |
26 | __nextupl (long double x) |
27 | { |
28 | int64_t hx, ihx, lx; |
29 | double xhi, xlo, yhi; |
30 | |
31 | ldbl_unpack (x, &xhi, &xlo); |
32 | EXTRACT_WORDS64 (hx, xhi); |
33 | EXTRACT_WORDS64 (lx, xlo); |
34 | ihx = hx & 0x7fffffffffffffffLL; |
35 | |
36 | if (ihx > 0x7ff0000000000000LL) /* x is nan. */ |
37 | return x + x; /* Signal the nan. */ |
38 | if (ihx == 0) |
39 | return LDBL_TRUE_MIN; |
40 | |
41 | long double u; |
42 | if ((hx == 0x7fefffffffffffffLL) && (lx == 0x7c8ffffffffffffeLL)) |
43 | return INFINITY; |
44 | if ((uint64_t) hx >= 0xfff0000000000000ULL) |
45 | { |
46 | u = -0x1.fffffffffffff7ffffffffffff8p+1023L; |
47 | return u; |
48 | } |
49 | if (ihx <= 0x0360000000000000LL) |
50 | { /* x <= LDBL_MIN. */ |
51 | x += LDBL_TRUE_MIN; |
52 | if (x == 0.0L) /* Handle negative LDBL_TRUE_MIN case. */ |
53 | x = -0.0L; |
54 | return x; |
55 | } |
56 | /* If the high double is an exact power of two and the low |
57 | double is the opposite sign, then 1ulp is one less than |
58 | what we might determine from the high double. Similarly |
59 | if X is an exact power of two, and negative, because |
60 | making it a little larger will result in the exponent |
61 | decreasing by one and normalisation of the mantissa. */ |
62 | if ((hx & 0x000fffffffffffffLL) == 0 |
63 | && ((lx != 0 && lx != 0x8000000000000000LL && (hx ^ lx) < 0) |
64 | || ((lx == 0 || lx == 0x8000000000000000LL) && hx < 0))) |
65 | ihx -= 1LL << 52; |
66 | if (ihx < (106LL << 52)) |
67 | { /* ulp will denormal. */ |
68 | INSERT_WORDS64 (yhi, ihx & (0x7ffLL << 52)); |
69 | u = yhi * 0x1p-105; |
70 | } |
71 | else |
72 | { |
73 | INSERT_WORDS64 (yhi, (ihx & (0x7ffLL << 52)) - (105LL << 52)); |
74 | u = yhi; |
75 | } |
76 | return x + u; |
77 | } |
78 | |
79 | weak_alias (__nextupl, nextupl) |
80 | |