1 /* mpfr_nextabove, mpfr_nextbelow, mpfr_nexttoward -- next representable
2 floating-point number
3 
4 Copyright 1999, 2001-2020 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramba projects, INRIA.
6 
7 This file is part of the GNU MPFR Library.
8 
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
21 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 
24 /* Note concerning the exceptions: In case of NaN result, the NaN flag is
25  * set as usual. No underflow or overflow is generated (this contradicts
26  * the obsolete IEEE 754-1985 standard for Nextafter, but conforms to the
27  * current standard IEEE 754-2008 for nextUp and nextDown).
28  */
29 
30 #include "mpfr-impl.h"
31 
32 void
mpfr_nexttozero(mpfr_ptr x)33 mpfr_nexttozero (mpfr_ptr x)
34 {
35   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
36     {
37       if (MPFR_IS_INF (x))
38         {
39           mpfr_setmax (x, __gmpfr_emax);
40         }
41       else
42         {
43           MPFR_ASSERTN (MPFR_IS_ZERO (x));
44           MPFR_CHANGE_SIGN(x);
45           mpfr_setmin (x, __gmpfr_emin);
46         }
47     }
48   else
49     {
50       mp_size_t xn;
51       int sh;
52       mp_limb_t *xp;
53 
54       xn = MPFR_LIMB_SIZE (x);
55       MPFR_UNSIGNED_MINUS_MODULO (sh, MPFR_PREC(x));
56       xp = MPFR_MANT(x);
57       mpn_sub_1 (xp, xp, xn, MPFR_LIMB_ONE << sh);
58       if (MPFR_UNLIKELY (MPFR_LIMB_MSB (xp[xn-1]) == 0))
59         { /* was an exact power of two: not normalized any more,
60              thus do not use MPFR_GET_EXP. */
61           mpfr_exp_t exp = MPFR_EXP (x);
62           if (MPFR_UNLIKELY (exp == __gmpfr_emin))
63             MPFR_SET_ZERO(x);
64           else
65             {
66               MPFR_SET_EXP (x, exp - 1);
67               /* The following is valid whether xn = 1 or xn > 1. */
68               xp[xn-1] |= MPFR_LIMB_HIGHBIT;
69             }
70         }
71     }
72 }
73 
74 void
mpfr_nexttoinf(mpfr_ptr x)75 mpfr_nexttoinf (mpfr_ptr x)
76 {
77   if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
78     {
79       if (MPFR_IS_ZERO (x))
80         mpfr_setmin (x, __gmpfr_emin);
81     }
82   else
83     {
84       mp_size_t xn;
85       int sh;
86       mp_limb_t *xp;
87 
88       xn = MPFR_LIMB_SIZE (x);
89       MPFR_UNSIGNED_MINUS_MODULO (sh, MPFR_PREC(x));
90       xp = MPFR_MANT(x);
91       if (MPFR_UNLIKELY (mpn_add_1 (xp, xp, xn, MPFR_LIMB_ONE << sh)))
92         /* got 1.0000... */
93         {
94           mpfr_exp_t exp = MPFR_EXP (x);
95           if (MPFR_UNLIKELY (exp == __gmpfr_emax))
96             MPFR_SET_INF (x);
97           else
98             {
99               MPFR_SET_EXP (x, exp + 1);
100               xp[xn-1] = MPFR_LIMB_HIGHBIT;
101             }
102         }
103     }
104 }
105 
106 void
mpfr_nextabove(mpfr_ptr x)107 mpfr_nextabove (mpfr_ptr x)
108 {
109   if (MPFR_UNLIKELY(MPFR_IS_NAN(x)))
110     {
111       __gmpfr_flags |= MPFR_FLAGS_NAN;
112       return;
113     }
114   if (MPFR_IS_NEG(x))
115     mpfr_nexttozero (x);
116   else
117     mpfr_nexttoinf (x);
118 }
119 
120 void
mpfr_nextbelow(mpfr_ptr x)121 mpfr_nextbelow (mpfr_ptr x)
122 {
123   if (MPFR_UNLIKELY(MPFR_IS_NAN(x)))
124     {
125       __gmpfr_flags |= MPFR_FLAGS_NAN;
126       return;
127     }
128 
129   if (MPFR_IS_NEG(x))
130     mpfr_nexttoinf (x);
131   else
132     mpfr_nexttozero (x);
133 }
134 
135 void
mpfr_nexttoward(mpfr_ptr x,mpfr_srcptr y)136 mpfr_nexttoward (mpfr_ptr x, mpfr_srcptr y)
137 {
138   int s;
139 
140   if (MPFR_UNLIKELY(MPFR_IS_NAN(x)))
141     {
142       __gmpfr_flags |= MPFR_FLAGS_NAN;
143       return;
144     }
145   else if (MPFR_UNLIKELY(MPFR_IS_NAN(x) || MPFR_IS_NAN(y)))
146     {
147       MPFR_SET_NAN(x);
148       __gmpfr_flags |= MPFR_FLAGS_NAN;
149       return;
150     }
151 
152   s = mpfr_cmp (x, y);
153   if (s == 0)
154     return;
155   else if (s < 0)
156     mpfr_nextabove (x);
157   else
158     mpfr_nextbelow (x);
159 }
160