w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

exceptions.c
Go to the documentation of this file.
1 /* Exception flags and utilities. Constructors and destructors (debug).
2 
3 Copyright 2001-2020 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramba projects, INRIA.
5 
6 This file is part of the GNU MPFR Library.
7 
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.
12 
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.
17 
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 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #include "mpfr-impl.h"
24 
28 
29 #undef mpfr_get_emin
30 
33 {
34  return __gmpfr_emin;
35 }
36 
37 #undef mpfr_set_emin
38 
39 int
41 {
42  if (MPFR_LIKELY (exponent >= MPFR_EMIN_MIN && exponent <= MPFR_EMIN_MAX))
43  {
44  __gmpfr_emin = exponent;
45  return 0;
46  }
47  else
48  {
49  return 1;
50  }
51 }
52 
55 {
56  return MPFR_EMIN_MIN;
57 }
58 
61 {
62  return MPFR_EMIN_MAX;
63 }
64 
65 #undef mpfr_get_emax
66 
69 {
70  return __gmpfr_emax;
71 }
72 
73 #undef mpfr_set_emax
74 
75 int
77 {
78  if (MPFR_LIKELY (exponent >= MPFR_EMAX_MIN && exponent <= MPFR_EMAX_MAX))
79  {
80  __gmpfr_emax = exponent;
81  return 0;
82  }
83  else
84  {
85  return 1;
86  }
87 }
88 
91 {
92  return MPFR_EMAX_MIN;
93 }
94 
97 {
98  return MPFR_EMAX_MAX;
99 }
100 
101 
102 #undef mpfr_flags_clear
103 
106 {
108 }
109 
110 #undef mpfr_flags_set
111 
114 {
115  __gmpfr_flags |= mask;
116 }
117 
118 #undef mpfr_flags_test
119 
122 {
123  return __gmpfr_flags & mask;
124 }
125 
126 #undef mpfr_flags_save
127 
130 {
131  return __gmpfr_flags;
132 }
133 
134 #undef mpfr_flags_restore
135 
138 {
139  __gmpfr_flags =
141  (flags & mask);
142 }
143 
144 
145 #undef mpfr_clear_flags
146 
147 void
149 {
150  __gmpfr_flags = 0;
151 }
152 
153 #undef mpfr_clear_underflow
154 
157 {
159 }
160 
161 #undef mpfr_clear_overflow
162 
165 {
167 }
168 
169 #undef mpfr_clear_divby0
170 
173 {
175 }
176 
177 #undef mpfr_clear_nanflag
178 
181 {
183 }
184 
185 #undef mpfr_clear_inexflag
186 
189 {
191 }
192 
193 #undef mpfr_clear_erangeflag
194 
197 {
199 }
200 
201 #undef mpfr_set_underflow
202 
205 {
207 }
208 
209 #undef mpfr_set_overflow
210 
213 {
215 }
216 
217 #undef mpfr_set_divby0
218 
221 {
223 }
224 
225 #undef mpfr_set_nanflag
226 
229 {
231 }
232 
233 #undef mpfr_set_inexflag
234 
237 {
239 }
240 
241 #undef mpfr_set_erangeflag
242 
245 {
247 }
248 
249 
250 #undef mpfr_check_range
251 
252 /* Note: It is possible that for pure FP numbers, EXP(x) < MPFR_EMIN_MIN,
253  but the caller must make sure that the difference remains small enough
254  to avoid reaching the special exponent values. */
255 int
257 {
258  if (MPFR_LIKELY (! MPFR_IS_SINGULAR (x)))
259  { /* x is a non-zero FP */
260  mpfr_exp_t exp = MPFR_EXP (x); /* Do not use MPFR_GET_EXP */
261 
264  {
265  /* The following test is necessary because in the rounding to the
266  * nearest mode, mpfr_underflow always rounds away from 0. In
267  * this rounding mode, we need to round to 0 if:
268  * _ |x| < 2^(emin-2), or
269  * _ |x| = 2^(emin-2) and the absolute value of the exact
270  * result is <= 2^(emin-2).
271  */
272  if (rnd_mode == MPFR_RNDN &&
273  (exp + 1 < __gmpfr_emin ||
274  (mpfr_powerof2_raw(x) &&
275  (MPFR_IS_NEG(x) ? t <= 0 : t >= 0))))
276  rnd_mode = MPFR_RNDZ;
277  return mpfr_underflow (x, rnd_mode, MPFR_SIGN(x));
278  }
280  return mpfr_overflow (x, rnd_mode, MPFR_SIGN(x));
281  }
282  else if (MPFR_UNLIKELY (t != 0 && MPFR_IS_INF (x)))
283  {
284  /* We need to do the following because most MPFR functions are
285  * implemented in the following way:
286  * Ziv's loop:
287  * | Compute an approximation to the result and an error bound.
288  * | Possible underflow/overflow detection -> return.
289  * | If can_round, break (exit the loop).
290  * | Otherwise, increase the working precision and loop.
291  * Round the approximation in the target precision. <== See below
292  * Restore the flags (that could have been set due to underflows
293  * or overflows during the internal computations).
294  * Execute: return mpfr_check_range (...).
295  * The problem is that an overflow could be generated when rounding the
296  * approximation (in general, such an overflow could not be detected
297  * earlier), and the overflow flag is lost when the flags are restored.
298  * This can occur only when the rounding yields an exponent change
299  * and the new exponent is larger than the maximum exponent, so that
300  * an infinity is necessarily obtained.
301  * So, the simplest solution is to detect this overflow case here in
302  * mpfr_check_range, which is easy to do since the rounded result is
303  * necessarily an inexact infinity.
304  */
306  }
307  MPFR_RET (t); /* propagate inexact ternary value, unlike most functions */
308 }
309 
310 
311 #undef mpfr_underflow_p
312 
315 {
318 }
319 
320 #undef mpfr_overflow_p
321 
324 {
327 }
328 
329 #undef mpfr_divby0_p
330 
333 {
336 }
337 
338 #undef mpfr_nanflag_p
339 
342 {
344  return __gmpfr_flags & MPFR_FLAGS_NAN;
345 }
346 
347 #undef mpfr_inexflag_p
348 
351 {
354 }
355 
356 #undef mpfr_erangeflag_p
357 
360 {
363 }
364 
365 
366 /* #undef mpfr_underflow */
367 
368 /* Note: In the rounding to the nearest mode, mpfr_underflow
369  always rounds away from 0. In this rounding mode, you must call
370  mpfr_underflow with rnd_mode = MPFR_RNDZ if the exact result
371  is <= 2^(emin-2) in absolute value.
372  We chose the default to round away from zero instead of toward zero
373  because rounding away from zero (MPFR_RNDA) wasn't supported at that
374  time (r1910), so that the caller had no way to change rnd_mode to
375  this mode. */
376 
379 {
380  int inex;
381 
383  (("rnd=%d sign=%d", rnd_mode, sign),
384  ("x[%Pu]=%.*Rg", mpfr_get_prec (x), mpfr_log_prec, x));
385 
387 
388  if (MPFR_IS_LIKE_RNDZ(rnd_mode, sign < 0))
389  {
390  MPFR_SET_ZERO(x);
391  inex = -1;
392  }
393  else
394  {
396  inex = 1;
397  }
398  MPFR_SET_SIGN(x, sign);
400  return sign > 0 ? inex : -inex;
401 }
402 
403 /* #undef mpfr_overflow */
404 
407 {
408  int inex;
409 
411  (("rnd=%d sign=%d", rnd_mode, sign),
412  ("x[%Pu]=%.*Rg", mpfr_get_prec (x), mpfr_log_prec, x));
413 
415 
416  if (MPFR_IS_LIKE_RNDZ(rnd_mode, sign < 0))
417  {
419  inex = -1;
420  }
421  else
422  {
423  MPFR_SET_INF(x);
424  inex = 1;
425  }
426  MPFR_SET_SIGN(x, sign);
428  return sign > 0 ? inex : -inex;
429 }
430 
431 /**************************************************************************/
432 
433 /* Code related to constructors and destructors (for debugging) should
434  be put here. The reason is that such code must be in an object file
435  that will be kept by the linker for symbol resolution, and symbols
436  __gmpfr_emin and __gmpfr_emax from this file will be used by every
437  program calling a MPFR math function (where rounding is involved). */
438 
439 #if defined MPFR_DEBUG_PREDICTION
440 
441 /* Print prediction statistics at the end of a program.
442  *
443  * Code to debug branch prediction, based on Ulrich Drepper's paper
444  * "What Every Programmer Should Know About Memory":
445  * http://people.freebsd.org/~lstewart/articles/cpumemory.pdf
446  */
447 
448 extern long int __start_predict_data;
449 extern long int __stop_predict_data;
450 extern long int __start_predict_line;
451 extern const char *__start_predict_file;
452 
453 static void __attribute__ ((destructor))
454 predprint (void)
455 {
456  long int *s = &__start_predict_data;
457  long int *e = &__stop_predict_data;
458  long int *sl = &__start_predict_line;
459  const char **sf = &__start_predict_file;
460 
461  while (s < e)
462  {
463  printf("%s:%ld: incorrect=%ld, correct=%ld%s\n",
464  *sf, *sl, s[0], s[1],
465  s[0] > s[1] ? " <==== WARNING" : "");
466  ++sl;
467  ++sf;
468  s += 2;
469  }
470 }
471 
472 #endif
473 
474 #if MPFR_WANT_ASSERT >= 2
475 
476 /* Similar to flags_out in tests/tests.c */
477 
478 void
479 flags_fout (FILE *stream, mpfr_flags_t flags)
480 {
481  int none = 1;
482 
484  none = 0, fprintf (stream, " underflow");
486  none = 0, fprintf (stream, " overflow");
487  if (flags & MPFR_FLAGS_NAN)
488  none = 0, fprintf (stream, " nan");
490  none = 0, fprintf (stream, " inexact");
491  if (flags & MPFR_FLAGS_ERANGE)
492  none = 0, fprintf (stream, " erange");
493  if (none)
494  fprintf (stream, " none");
495  fprintf (stream, " (%u)\n", flags);
496 }
497 
498 #endif
double __cdecl exp(double _X)
#define none
Definition: ctie-k.c:30
int printf()
void mpfr_clear_inexflag(void)
Definition: exceptions.c:188
void mpfr_clear_overflow(void)
Definition: exceptions.c:164
int mpfr_overflow_p(void)
Definition: exceptions.c:323
mpfr_exp_t mpfr_get_emin_max(void)
Definition: exceptions.c:60
int mpfr_underflow(mpfr_ptr x, mpfr_rnd_t rnd_mode, int sign)
Definition: exceptions.c:378
mpfr_exp_t mpfr_get_emax(void)
Definition: exceptions.c:68
void mpfr_clear_flags(void)
Definition: exceptions.c:148
int mpfr_check_range(mpfr_ptr x, int t, mpfr_rnd_t rnd_mode)
Definition: exceptions.c:256
mpfr_exp_t __gmpfr_emax
Definition: exceptions.c:27
void mpfr_set_divby0(void)
Definition: exceptions.c:220
int mpfr_divby0_p(void)
Definition: exceptions.c:332
int mpfr_set_emax(mpfr_exp_t exponent)
Definition: exceptions.c:76
int mpfr_nanflag_p(void)
Definition: exceptions.c:341
int mpfr_erangeflag_p(void)
Definition: exceptions.c:359
void mpfr_set_overflow(void)
Definition: exceptions.c:212
mpfr_flags_t mpfr_flags_test(mpfr_flags_t mask)
Definition: exceptions.c:121
void mpfr_clear_divby0(void)
Definition: exceptions.c:172
int mpfr_inexflag_p(void)
Definition: exceptions.c:350
void mpfr_flags_set(mpfr_flags_t mask)
Definition: exceptions.c:113
void mpfr_flags_clear(mpfr_flags_t mask)
Definition: exceptions.c:105
int mpfr_overflow(mpfr_ptr x, mpfr_rnd_t rnd_mode, int sign)
Definition: exceptions.c:406
mpfr_exp_t __gmpfr_emin
Definition: exceptions.c:26
mpfr_exp_t mpfr_get_emax_max(void)
Definition: exceptions.c:96
void mpfr_clear_erangeflag(void)
Definition: exceptions.c:196
mpfr_exp_t mpfr_get_emin_min(void)
Definition: exceptions.c:54
void mpfr_set_underflow(void)
Definition: exceptions.c:204
void mpfr_clear_underflow(void)
Definition: exceptions.c:156
mpfr_exp_t mpfr_get_emax_min(void)
Definition: exceptions.c:90
void mpfr_clear_nanflag(void)
Definition: exceptions.c:180
int mpfr_set_emin(mpfr_exp_t exponent)
Definition: exceptions.c:40
int mpfr_underflow_p(void)
Definition: exceptions.c:314
void mpfr_set_erangeflag(void)
Definition: exceptions.c:244
void mpfr_set_nanflag(void)
Definition: exceptions.c:228
void mpfr_set_inexflag(void)
Definition: exceptions.c:236
mpfr_exp_t mpfr_get_emin(void)
Definition: exceptions.c:32
mpfr_flags_t mpfr_flags_save(void)
Definition: exceptions.c:129
mpfr_flags_t __gmpfr_flags
Definition: exceptions.c:25
void mpfr_flags_restore(mpfr_flags_t flags, mpfr_flags_t mask)
Definition: exceptions.c:137
#define s
Definition: afcover.h:80
#define INT_MAX
Definition: c-minmax.h:53
#define fprintf
Definition: mendex.h:64
#define __attribute__(A)
Definition: synctex.c:338
#define MPFR_IS_INF(x)
Definition: mpfr-impl.h:1082
#define MPFR_IS_LIKE_RNDZ(rnd, neg)
Definition: mpfr-impl.h:1207
#define MPFR_UNLIKELY(x)
Definition: mpfr-impl.h:1490
#define MPFR_LIKELY(x)
Definition: mpfr-impl.h:1489
#define MPFR_EMIN_MIN
Definition: mpfr-impl.h:1031
#define MPFR_IS_NEG(x)
Definition: mpfr-impl.h:1140
#define MPFR_EMAX_MAX
Definition: mpfr-impl.h:1034
#define MPFR_EMAX_MIN
Definition: mpfr-impl.h:1033
#define MPFR_ASSERT_SIGN(s)
Definition: mpfr-impl.h:1149
#define MPFR_SET_ZERO(x)
Definition: mpfr-impl.h:1085
int mpfr_powerof2_raw(mpfr_srcptr)
Definition: powerof2.c:32
#define MPFR_COLD_FUNCTION_ATTR
Definition: mpfr-impl.h:200
#define MPFR_THREAD_VAR(T, N, V)
Definition: mpfr-impl.h:322
#define MPFR_RET(I)
Definition: mpfr-impl.h:1182
#define MPFR_SET_INF(x)
Definition: mpfr-impl.h:1083
#define MPFR_EMIN_MAX
Definition: mpfr-impl.h:1032
#define MPFR_LOG_FUNC(x, y)
Definition: mpfr-impl.h:2227
void mpfr_setmin(mpfr_ptr, mpfr_exp_t)
Definition: setmin.c:28
#define MPFR_ASSERTD(expr)
Definition: mpfr-impl.h:516
#define MPFR_IS_NORMALIZED(x)
Definition: mpfr-impl.h:1090
#define MPFR_EXP(x)
Definition: mpfr-impl.h:959
void mpfr_setmax(mpfr_ptr, mpfr_exp_t)
Definition: setmax.c:28
#define MPFR_IS_SINGULAR(x)
Definition: mpfr-impl.h:1100
#define MPFR_SET_SIGN(x, s)
Definition: mpfr-impl.h:1151
#define MPFR_STAT_STATIC_ASSERT(c)
Definition: mpfr-sassert.h:69
#define MPFR_FLAGS_UNDERFLOW
Definition: mpfr.h:77
#define MPFR_FLAGS_NAN
Definition: mpfr.h:79
#define MPFR_EMIN_DEFAULT
Definition: mpfr.h:209
#define MPFR_FLAGS_ALL
Definition: mpfr.h:83
#define MPFR_EMAX_DEFAULT
Definition: mpfr.h:208
#define MPFR_FLAGS_DIVBY0
Definition: mpfr.h:82
#define MPFR_FLAGS_ERANGE
Definition: mpfr.h:81
#define MPFR_FLAGS_OVERFLOW
Definition: mpfr.h:78
#define mpfr_get_prec(_x)
Definition: mpfr.h:848
#define MPFR_SIGN(x)
Definition: mpfr.h:260
unsigned int mpfr_flags_t
Definition: mpfr.h:74
mpfr_rnd_t
Definition: mpfr.h:102
@ MPFR_RNDN
Definition: mpfr.h:103
@ MPFR_RNDZ
Definition: mpfr.h:104
long mpfr_exp_t
Definition: mpfr.h:195
#define MPFR_FLAGS_INEXACT
Definition: mpfr.h:80
float x
Definition: cordic.py:15
#define sign(x)
struct stream_s stream
Definition: pts_fax.h:93
#define flags
#define mask(n)
Definition: lbitlib.c:93
ShellFileEnvironment e
Definition: sh6.c:388
Definition: tfmaux.c:31
Definition: dvips.h:235
#define FILE
Definition: t1stdio.h:34