1 /* Exception flags and utilities.
3 Copyright 2001-2015 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
25 unsigned int MPFR_THREAD_ATTR __gmpfr_flags
= 0;
27 mpfr_exp_t MPFR_THREAD_ATTR __gmpfr_emin
= MPFR_EMIN_DEFAULT
;
28 mpfr_exp_t MPFR_THREAD_ATTR __gmpfr_emax
= MPFR_EMAX_DEFAULT
;
41 mpfr_set_emin (mpfr_exp_t exponent
)
43 if (exponent
>= MPFR_EMIN_MIN
&& exponent
<= MPFR_EMIN_MAX
)
45 __gmpfr_emin
= exponent
;
55 mpfr_get_emin_min (void)
61 mpfr_get_emin_max (void)
77 mpfr_set_emax (mpfr_exp_t exponent
)
79 if (exponent
>= MPFR_EMAX_MIN
&& exponent
<= MPFR_EMAX_MAX
)
81 __gmpfr_emax
= exponent
;
91 mpfr_get_emax_min (void)
96 mpfr_get_emax_max (void)
102 #undef mpfr_clear_flags
105 mpfr_clear_flags (void)
110 #undef mpfr_clear_underflow
113 mpfr_clear_underflow (void)
115 __gmpfr_flags
&= MPFR_FLAGS_ALL
^ MPFR_FLAGS_UNDERFLOW
;
118 #undef mpfr_clear_overflow
121 mpfr_clear_overflow (void)
123 __gmpfr_flags
&= MPFR_FLAGS_ALL
^ MPFR_FLAGS_OVERFLOW
;
126 #undef mpfr_clear_divby0
129 mpfr_clear_divby0 (void)
131 __gmpfr_flags
&= MPFR_FLAGS_ALL
^ MPFR_FLAGS_DIVBY0
;
134 #undef mpfr_clear_nanflag
137 mpfr_clear_nanflag (void)
139 __gmpfr_flags
&= MPFR_FLAGS_ALL
^ MPFR_FLAGS_NAN
;
142 #undef mpfr_clear_inexflag
145 mpfr_clear_inexflag (void)
147 __gmpfr_flags
&= MPFR_FLAGS_ALL
^ MPFR_FLAGS_INEXACT
;
150 #undef mpfr_clear_erangeflag
153 mpfr_clear_erangeflag (void)
155 __gmpfr_flags
&= MPFR_FLAGS_ALL
^ MPFR_FLAGS_ERANGE
;
158 #undef mpfr_set_underflow
161 mpfr_set_underflow (void)
163 __gmpfr_flags
|= MPFR_FLAGS_UNDERFLOW
;
166 #undef mpfr_set_overflow
169 mpfr_set_overflow (void)
171 __gmpfr_flags
|= MPFR_FLAGS_OVERFLOW
;
174 #undef mpfr_set_divby0
177 mpfr_set_divby0 (void)
179 __gmpfr_flags
|= MPFR_FLAGS_DIVBY0
;
182 #undef mpfr_set_nanflag
185 mpfr_set_nanflag (void)
187 __gmpfr_flags
|= MPFR_FLAGS_NAN
;
190 #undef mpfr_set_inexflag
193 mpfr_set_inexflag (void)
195 __gmpfr_flags
|= MPFR_FLAGS_INEXACT
;
198 #undef mpfr_set_erangeflag
201 mpfr_set_erangeflag (void)
203 __gmpfr_flags
|= MPFR_FLAGS_ERANGE
;
207 #undef mpfr_check_range
210 mpfr_check_range (mpfr_ptr x
, int t
, mpfr_rnd_t rnd_mode
)
212 if (MPFR_LIKELY( MPFR_IS_PURE_FP(x
)) )
213 { /* x is a non-zero FP */
214 mpfr_exp_t exp
= MPFR_EXP (x
); /* Do not use MPFR_GET_EXP */
215 if (MPFR_UNLIKELY( exp
< __gmpfr_emin
) )
217 /* The following test is necessary because in the rounding to the
218 * nearest mode, mpfr_underflow always rounds away from 0. In
219 * this rounding mode, we need to round to 0 if:
220 * _ |x| < 2^(emin-2), or
221 * _ |x| = 2^(emin-2) and the absolute value of the exact
222 * result is <= 2^(emin-2).
224 if (rnd_mode
== MPFR_RNDN
&&
225 (exp
+ 1 < __gmpfr_emin
||
226 (mpfr_powerof2_raw(x
) &&
227 (MPFR_IS_NEG(x
) ? t
<= 0 : t
>= 0))))
228 rnd_mode
= MPFR_RNDZ
;
229 return mpfr_underflow(x
, rnd_mode
, MPFR_SIGN(x
));
231 if (MPFR_UNLIKELY( exp
> __gmpfr_emax
) )
232 return mpfr_overflow (x
, rnd_mode
, MPFR_SIGN(x
));
234 else if (MPFR_UNLIKELY (t
!= 0 && MPFR_IS_INF (x
)))
236 /* We need to do the following because most MPFR functions are
237 * implemented in the following way:
239 * | Compute an approximation to the result and an error bound.
240 * | Possible underflow/overflow detection -> return.
241 * | If can_round, break (exit the loop).
242 * | Otherwise, increase the working precision and loop.
243 * Round the approximation in the target precision. <== See below
244 * Restore the flags (that could have been set due to underflows
245 * or overflows during the internal computations).
246 * Execute: return mpfr_check_range (...).
247 * The problem is that an overflow could be generated when rounding the
248 * approximation (in general, such an overflow could not be detected
249 * earlier), and the overflow flag is lost when the flags are restored.
250 * This can occur only when the rounding yields an exponent change
251 * and the new exponent is larger than the maximum exponent, so that
252 * an infinity is necessarily obtained.
253 * So, the simplest solution is to detect this overflow case here in
254 * mpfr_check_range, which is easy to do since the rounded result is
255 * necessarily an inexact infinity.
257 __gmpfr_flags
|= MPFR_FLAGS_OVERFLOW
;
259 MPFR_RET (t
); /* propagate inexact ternary value, unlike most functions */
262 #undef mpfr_underflow_p
265 mpfr_underflow_p (void)
267 return __gmpfr_flags
& MPFR_FLAGS_UNDERFLOW
;
270 #undef mpfr_overflow_p
273 mpfr_overflow_p (void)
275 return __gmpfr_flags
& MPFR_FLAGS_OVERFLOW
;
283 return __gmpfr_flags
& MPFR_FLAGS_DIVBY0
;
286 #undef mpfr_nanflag_p
289 mpfr_nanflag_p (void)
291 return __gmpfr_flags
& MPFR_FLAGS_NAN
;
294 #undef mpfr_inexflag_p
297 mpfr_inexflag_p (void)
299 return __gmpfr_flags
& MPFR_FLAGS_INEXACT
;
302 #undef mpfr_erangeflag_p
305 mpfr_erangeflag_p (void)
307 return __gmpfr_flags
& MPFR_FLAGS_ERANGE
;
310 /* #undef mpfr_underflow */
312 /* Note: In the rounding to the nearest mode, mpfr_underflow
313 always rounds away from 0. In this rounding mode, you must call
314 mpfr_underflow with rnd_mode = MPFR_RNDZ if the exact result
315 is <= 2^(emin-2) in absolute value. */
318 mpfr_underflow (mpfr_ptr x
, mpfr_rnd_t rnd_mode
, int sign
)
322 MPFR_ASSERT_SIGN (sign
);
324 if (MPFR_IS_LIKE_RNDZ(rnd_mode
, sign
< 0))
331 mpfr_setmin (x
, __gmpfr_emin
);
334 MPFR_SET_SIGN(x
, sign
);
335 __gmpfr_flags
|= MPFR_FLAGS_INEXACT
| MPFR_FLAGS_UNDERFLOW
;
336 return sign
> 0 ? inex
: -inex
;
339 /* #undef mpfr_overflow */
342 mpfr_overflow (mpfr_ptr x
, mpfr_rnd_t rnd_mode
, int sign
)
346 MPFR_ASSERT_SIGN(sign
);
347 if (MPFR_IS_LIKE_RNDZ(rnd_mode
, sign
< 0))
349 mpfr_setmax (x
, __gmpfr_emax
);
357 MPFR_SET_SIGN(x
,sign
);
358 __gmpfr_flags
|= MPFR_FLAGS_INEXACT
| MPFR_FLAGS_OVERFLOW
;
359 return sign
> 0 ? inex
: -inex
;