summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/add1sp.c8
-rw-r--r--src/mpfr-impl.h47
-rw-r--r--src/mul.c16
-rw-r--r--src/round_p.c6
-rw-r--r--src/sub1sp.c8
5 files changed, 44 insertions, 41 deletions
diff --git a/src/add1sp.c b/src/add1sp.c
index d9d5dc0ed..cf1931ec5 100644
--- a/src/add1sp.c
+++ b/src/add1sp.c
@@ -25,8 +25,7 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
#include "mpfr-impl.h"
/* Check if we have to check the result of mpfr_add1sp with mpfr_add1 */
-#ifdef MPFR_WANT_ASSERT
-# if MPFR_WANT_ASSERT >= 2
+#if MPFR_WANT_ASSERT >= 2
int mpfr_add1sp2 (mpfr_ptr, mpfr_srcptr, mpfr_srcptr, mpfr_rnd_t);
int mpfr_add1sp (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
@@ -69,9 +68,8 @@ int mpfr_add1sp (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
mpfr_clears (tmpa, tmpb, tmpc, (mpfr_ptr) 0);
return inexact;
}
-# define mpfr_add1sp mpfr_add1sp2
-# endif
-#endif
+# define mpfr_add1sp mpfr_add1sp2
+#endif /* MPFR_WANT_ASSERT >= 2 */
/* Debugging support */
#ifdef DEBUG
diff --git a/src/mpfr-impl.h b/src/mpfr-impl.h
index 91f53ca74..374832747 100644
--- a/src/mpfr-impl.h
+++ b/src/mpfr-impl.h
@@ -372,22 +372,42 @@ __MPFR_DECLSPEC extern const mpfr_t __gmpfr_const_log2_RNDU;
******************** Assertions **********************
******************************************************/
-/* Compile with -DMPFR_WANT_ASSERT to check all assert statements */
+/* MPFR_WANT_ASSERT can take 4 values (the default value is 0):
+ -1 (or below): Do not check any assertion. Discouraged, in particular
+ for a shared library (for time-critical applications, LTO with a
+ static library should also be used anyway).
+ 0: Check normal assertions.
+ 1: Check debugging assertions too.
+ 2 (or above): Additional checks that may take time. For instance,
+ some functions may be tested by using two different implementations
+ and comparing the results.
+*/
/* Note: do not use GMP macros ASSERT_ALWAYS and ASSERT as they are not
expressions, and as a consequence, they cannot be used in a for(),
with a comma operator and so on. */
-/* MPFR_ASSERTN(expr): assertions that should always be checked */
-#define MPFR_ASSERTN(expr) \
- ((void) ((MPFR_LIKELY(expr)) || (ASSERT_FAIL(expr),0)))
-
-/* MPFR_ASSERTD(expr): assertions that should be checked when testing.
+/* MPFR_ASSERTN(expr): assertions that should normally be checked,
+ otherwise give a hint to the compiler.
+ MPFR_ASSERTD(expr): assertions that should be checked when testing,
+ otherwise give a hint to the compiler.
MPFR_DBGRES(assignment): to be used when the result is tested only
in an MPFR_ASSERTD expression (in order to avoid a warning, e.g.
with GCC's -Wunused-but-set-variable, in non-debug mode).
*/
-#ifdef MPFR_WANT_ASSERT
+#ifndef MPFR_WANT_ASSERT
+# define MPFR_WANT_ASSERT 0
+#endif
+
+#if MPFR_WANT_ASSERT < 0
+# undef MPFR_EXP_CHECK
+# define MPFR_ASSERTN(expr) MPFR_ASSUME (expr)
+#else
+# define MPFR_ASSERTN(expr) \
+ ((void) ((MPFR_LIKELY(expr)) || (ASSERT_FAIL(expr),0)))
+#endif
+
+#if MPFR_WANT_ASSERT > 0
# define MPFR_EXP_CHECK 1
# define MPFR_ASSERTD(expr) MPFR_ASSERTN (expr)
# define MPFR_DBGRES(A) (A)
@@ -396,19 +416,6 @@ __MPFR_DECLSPEC extern const mpfr_t __gmpfr_const_log2_RNDU;
# define MPFR_DBGRES(A) ((void) (A))
#endif
-/* Check if the user requested absolutely no assertion (including MPFR_ASSERTN) */
-#if defined(MPFR_WANT_ASSERT)
-# if MPFR_WANT_ASSERT < 0
-# undef MPFR_ASSERTN
-# undef MPFR_ASSERTD
-# undef MPFR_DBGRES
-# undef MPFR_EXP_CHECK
-# define MPFR_ASSERTN(expr) ((void) 0)
-# define MPFR_ASSERTD(expr) ((void) 0)
-# define MPFR_DBGRES(A) ((void) (A))
-# endif
-#endif
-
/* MPFR_ASSUME is like assert(), but it is a hint to a compiler about a
statement of fact in a function call free expression, which allows
the compiler to generate better machine code.
diff --git a/src/mul.c b/src/mul.c
index ea804485e..9421ce99c 100644
--- a/src/mul.c
+++ b/src/mul.c
@@ -28,8 +28,7 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
/* Check if we have to check the result of mpfr_mul.
TODO: Find a better (and faster?) check than using old implementation */
-#ifdef MPFR_WANT_ASSERT
-# if MPFR_WANT_ASSERT >= 3
+#if MPFR_WANT_ASSERT >= 2
int mpfr_mul2 (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode);
static int
@@ -171,9 +170,12 @@ mpfr_mul (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
MPFR_ASSERTN (mpfr_set (tc, c, MPFR_RNDN) == 0);
inexact2 = mpfr_mul3 (ta, tb, tc, rnd_mode);
- inexact1 = mpfr_mul2 (a, b, c, rnd_mode);
- if (mpfr_cmp (ta, a) || inexact1*inexact2 < 0
- || (inexact1*inexact2 == 0 && (inexact1|inexact2) != 0))
+ inexact1 = mpfr_mul2 (a, b, c, rnd_mode);
+ if (MPFR_IS_NAN (ta) && MPFR_IS_NAN (a))
+ {
+ /* Both NaN is OK. */
+ }
+ else if (mpfr_cmp (ta, a) != 0 || ! SAME_SIGN (inexact1, inexact2))
{
fprintf (stderr, "mpfr_mul return different values for %s\n"
"Prec_a = %lu, Prec_b = %lu, Prec_c = %lu\nB = ",
@@ -196,8 +198,8 @@ mpfr_mul (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
}
# define mpfr_mul mpfr_mul2
-# endif
-#endif
+
+#endif /* MPFR_WANT_ASSERT >= 2 */
/****** END OF CHECK *******/
diff --git a/src/round_p.c b/src/round_p.c
index 00899caa0..26471293a 100644
--- a/src/round_p.c
+++ b/src/round_p.c
@@ -23,8 +23,7 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
#include "mpfr-impl.h"
/* Check against mpfr_can_round? */
-#ifdef MPFR_WANT_ASSERT
-# if MPFR_WANT_ASSERT >= 2
+#if MPFR_WANT_ASSERT >= 2
int mpfr_round_p_2 (mp_limb_t *, mp_size_t, mpfr_exp_t, mpfr_prec_t);
int
mpfr_round_p (mp_limb_t *bp, mp_size_t bn, mpfr_exp_t err0, mpfr_prec_t prec)
@@ -45,8 +44,7 @@ mpfr_round_p (mp_limb_t *bp, mp_size_t bn, mpfr_exp_t err0, mpfr_prec_t prec)
return i1;
}
# define mpfr_round_p mpfr_round_p_2
-# endif
-#endif
+#endif /* MPFR_WANT_ASSERT >= 2 */
/*
* Assuming {bp, bn} is an approximation of a non-singular number
diff --git a/src/sub1sp.c b/src/sub1sp.c
index 272a967da..d89d6a8dd 100644
--- a/src/sub1sp.c
+++ b/src/sub1sp.c
@@ -25,8 +25,7 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
#include "mpfr-impl.h"
/* Check if we have to check the result of mpfr_sub1sp with mpfr_sub1 */
-#ifdef MPFR_WANT_ASSERT
-# if MPFR_WANT_ASSERT >= 2
+#if MPFR_WANT_ASSERT >= 2
int mpfr_sub1sp2 (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode);
int mpfr_sub1sp (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
@@ -67,9 +66,8 @@ int mpfr_sub1sp (mpfr_ptr a, mpfr_srcptr b, mpfr_srcptr c, mpfr_rnd_t rnd_mode)
mpfr_clears (tmpa, tmpb, tmpc, (mpfr_ptr) 0);
return inexact;
}
-# define mpfr_sub1sp mpfr_sub1sp2
-# endif
-#endif
+# define mpfr_sub1sp mpfr_sub1sp2
+#endif /* MPFR_WANT_ASSERT >= 2 */
/* Debugging support */
#ifdef DEBUG