beta-0.89.2
[luatex.git] / source / libs / mpfr / mpfr-3.1.3 / src / exceptions.c
blobdacca4eaa1bc5aac5e3e271c983fb8a30f6d2c0b
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;
30 #undef mpfr_get_emin
32 mpfr_exp_t
33 mpfr_get_emin (void)
35 return __gmpfr_emin;
38 #undef mpfr_set_emin
40 int
41 mpfr_set_emin (mpfr_exp_t exponent)
43 if (exponent >= MPFR_EMIN_MIN && exponent <= MPFR_EMIN_MAX)
45 __gmpfr_emin = exponent;
46 return 0;
48 else
50 return 1;
54 mpfr_exp_t
55 mpfr_get_emin_min (void)
57 return MPFR_EMIN_MIN;
60 mpfr_exp_t
61 mpfr_get_emin_max (void)
63 return MPFR_EMIN_MAX;
66 #undef mpfr_get_emax
68 mpfr_exp_t
69 mpfr_get_emax (void)
71 return __gmpfr_emax;
74 #undef mpfr_set_emax
76 int
77 mpfr_set_emax (mpfr_exp_t exponent)
79 if (exponent >= MPFR_EMAX_MIN && exponent <= MPFR_EMAX_MAX)
81 __gmpfr_emax = exponent;
82 return 0;
84 else
86 return 1;
90 mpfr_exp_t
91 mpfr_get_emax_min (void)
93 return MPFR_EMAX_MIN;
95 mpfr_exp_t
96 mpfr_get_emax_max (void)
98 return MPFR_EMAX_MAX;
102 #undef mpfr_clear_flags
104 void
105 mpfr_clear_flags (void)
107 __gmpfr_flags = 0;
110 #undef mpfr_clear_underflow
112 void
113 mpfr_clear_underflow (void)
115 __gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_UNDERFLOW;
118 #undef mpfr_clear_overflow
120 void
121 mpfr_clear_overflow (void)
123 __gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_OVERFLOW;
126 #undef mpfr_clear_divby0
128 void
129 mpfr_clear_divby0 (void)
131 __gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_DIVBY0;
134 #undef mpfr_clear_nanflag
136 void
137 mpfr_clear_nanflag (void)
139 __gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_NAN;
142 #undef mpfr_clear_inexflag
144 void
145 mpfr_clear_inexflag (void)
147 __gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_INEXACT;
150 #undef mpfr_clear_erangeflag
152 void
153 mpfr_clear_erangeflag (void)
155 __gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE;
158 #undef mpfr_set_underflow
160 void
161 mpfr_set_underflow (void)
163 __gmpfr_flags |= MPFR_FLAGS_UNDERFLOW;
166 #undef mpfr_set_overflow
168 void
169 mpfr_set_overflow (void)
171 __gmpfr_flags |= MPFR_FLAGS_OVERFLOW;
174 #undef mpfr_set_divby0
176 void
177 mpfr_set_divby0 (void)
179 __gmpfr_flags |= MPFR_FLAGS_DIVBY0;
182 #undef mpfr_set_nanflag
184 void
185 mpfr_set_nanflag (void)
187 __gmpfr_flags |= MPFR_FLAGS_NAN;
190 #undef mpfr_set_inexflag
192 void
193 mpfr_set_inexflag (void)
195 __gmpfr_flags |= MPFR_FLAGS_INEXACT;
198 #undef mpfr_set_erangeflag
200 void
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:
238 * Ziv's loop:
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;
278 #undef mpfr_divby0_p
281 mpfr_divby0_p (void)
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)
320 int inex;
322 MPFR_ASSERT_SIGN (sign);
324 if (MPFR_IS_LIKE_RNDZ(rnd_mode, sign < 0))
326 MPFR_SET_ZERO(x);
327 inex = -1;
329 else
331 mpfr_setmin (x, __gmpfr_emin);
332 inex = 1;
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)
344 int inex;
346 MPFR_ASSERT_SIGN(sign);
347 if (MPFR_IS_LIKE_RNDZ(rnd_mode, sign < 0))
349 mpfr_setmax (x, __gmpfr_emax);
350 inex = -1;
352 else
354 MPFR_SET_INF(x);
355 inex = 1;
357 MPFR_SET_SIGN(x,sign);
358 __gmpfr_flags |= MPFR_FLAGS_INEXACT | MPFR_FLAGS_OVERFLOW;
359 return sign > 0 ? inex : -inex;