summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpelissip <pelissip@280ebfd0-de03-0410-8827-d642c229c3f4>2004-09-29 12:23:54 +0000
committerpelissip <pelissip@280ebfd0-de03-0410-8827-d642c229c3f4>2004-09-29 12:23:54 +0000
commit795c8ef5f9daa924b5592582240c9ba9a58034c1 (patch)
treeb424ebc4faba0f2e84f0ea3aaf8dbd3a42e4d4a8
parent8829f04a918c0433e272f20a93a5bdc1fc4e85f0 (diff)
downloadmpfr-795c8ef5f9daa924b5592582240c9ba9a58034c1.tar.gz
Add mpfr_uintmax_p and mpfr_intmax_p.
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@3006 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--Makefile.am2
-rw-r--r--fits_intmax.c107
-rw-r--r--fits_uintmax.c97
-rw-r--r--mpfr.h32
-rw-r--r--tests/tfits.c84
5 files changed, 311 insertions, 11 deletions
diff --git a/Makefile.am b/Makefile.am
index a158d9f26..c940fba33 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -7,7 +7,7 @@ include_HEADERS = mpfr.h mpf2mpfr.h
lib_LIBRARIES = libmpfr.a
-libmpfr_a_SOURCES = mpfr.h mpf2mpfr.h mpfr-gmp.h mpfr-impl.h mpfr-longlong.h mpfr-test.h log_b2.h exceptions.c save_expo.c extract.c uceil_exp2.c uceil_log2.c ufloor_log2.c add.c add1.c add_one_ulp.c add_ui.c agm.c clear.c cmp.c cmp_abs.c cmp_si.c cmp_ui.c comparisons.c div_2exp.c div_2si.c div_2ui.c div.c div_ui.c dump.c eq.c exp10.c exp2.c exp3.c exp.c frac.c get_d.c get_exp.c get_str.c init.c inp_str.c isinteger.c isinf.c isnan.c isnum.c const_log2.c log.c mul_2exp.c mul_2si.c mul_2ui.c mul.c mul_ui.c neg.c next.c out_str.c const_pi.c pow.c pow_si.c pow_ui.c print_raw.c print_rnd_mode.c random2.c random.c reldiff.c round_prec.c set.c setmax.c setmin.c set_d.c set_dfl_prec.c set_exp.c set_rnd.c set_f.c set_prc_raw.c set_prec.c set_q.c set_si.c set_str.c set_str_raw.c set_ui.c set_z.c sqrt.c sqrt_ui.c sub.c sub1.c sub_one_ulp.c sub_ui.c rint.c ui_div.c ui_sub.c urandomb.c get_z_exp.c swap.c factorial.c cosh.c sinh.c tanh.c acosh.c asinh.c atanh.c atan.c cmp2.c exp_2.c asin.c const_euler.c cos.c sin.c tan.c fma.c hypot.c log1p.c expm1.c log2.c log10.c ui_pow.c ui_pow_ui.c minmax.c dim.c copysign.c gmp_op.c init2.c acos.c sin_cos.c set_nan.c set_inf.c powerof2.c gamma.c set_ld.c get_ld.c cbrt.c volatile.c fits_s.h fits_sshort.c fits_sint.c fits_slong.c fits_u.h fits_ushort.c fits_uint.c fits_ulong.c get_si.c get_ui.c zeta.c cmp_d.c erf.c inits.c inits2.c clears.c sgn.c check.c sub1sp.c version.c mpn_exp.c mpfr-gmp.c mp_clz_tab.c sum.c add1sp.c free_cache.c si_op.c cmp_ld.c set_ui_2exp.c set_si_2exp.c set_uj.c set_sj.c get_sj.c get_uj.c get_z.c iszero.c cache.c sqr.c int_ceil_log2.c isqrt.c strtofr.c
+libmpfr_a_SOURCES = mpfr.h mpf2mpfr.h mpfr-gmp.h mpfr-impl.h mpfr-longlong.h mpfr-test.h log_b2.h exceptions.c save_expo.c extract.c uceil_exp2.c uceil_log2.c ufloor_log2.c add.c add1.c add_one_ulp.c add_ui.c agm.c clear.c cmp.c cmp_abs.c cmp_si.c cmp_ui.c comparisons.c div_2exp.c div_2si.c div_2ui.c div.c div_ui.c dump.c eq.c exp10.c exp2.c exp3.c exp.c frac.c get_d.c get_exp.c get_str.c init.c inp_str.c isinteger.c isinf.c isnan.c isnum.c const_log2.c log.c mul_2exp.c mul_2si.c mul_2ui.c mul.c mul_ui.c neg.c next.c out_str.c const_pi.c pow.c pow_si.c pow_ui.c print_raw.c print_rnd_mode.c random2.c random.c reldiff.c round_prec.c set.c setmax.c setmin.c set_d.c set_dfl_prec.c set_exp.c set_rnd.c set_f.c set_prc_raw.c set_prec.c set_q.c set_si.c set_str.c set_str_raw.c set_ui.c set_z.c sqrt.c sqrt_ui.c sub.c sub1.c sub_one_ulp.c sub_ui.c rint.c ui_div.c ui_sub.c urandomb.c get_z_exp.c swap.c factorial.c cosh.c sinh.c tanh.c acosh.c asinh.c atanh.c atan.c cmp2.c exp_2.c asin.c const_euler.c cos.c sin.c tan.c fma.c hypot.c log1p.c expm1.c log2.c log10.c ui_pow.c ui_pow_ui.c minmax.c dim.c copysign.c gmp_op.c init2.c acos.c sin_cos.c set_nan.c set_inf.c powerof2.c gamma.c set_ld.c get_ld.c cbrt.c volatile.c fits_s.h fits_sshort.c fits_sint.c fits_slong.c fits_u.h fits_ushort.c fits_uint.c fits_ulong.c fits_uintmax.c fits_intmax.c get_si.c get_ui.c zeta.c cmp_d.c erf.c inits.c inits2.c clears.c sgn.c check.c sub1sp.c version.c mpn_exp.c mpfr-gmp.c mp_clz_tab.c sum.c add1sp.c free_cache.c si_op.c cmp_ld.c set_ui_2exp.c set_si_2exp.c set_uj.c set_sj.c get_sj.c get_uj.c get_z.c iszero.c cache.c sqr.c int_ceil_log2.c isqrt.c strtofr.c
libmpfr_a_LIBADD = @LIBOBJS@
diff --git a/fits_intmax.c b/fits_intmax.c
new file mode 100644
index 000000000..2211357ea
--- /dev/null
+++ b/fits_intmax.c
@@ -0,0 +1,107 @@
+/* mpfr_fits_intmax_p -- test whether an mpfr fits an intmax_t.
+
+Copyright 2004 Free Software Foundation.
+Contributed by the Spaces project, INRIA Lorraine.
+
+This file is part of the MPFR Library.
+
+The MPFR Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or (at your
+option) any later version.
+
+The MPFR Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the MPFR Library; see the file COPYING.LIB. If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+MA 02111-1307, USA. */
+
+#if HAVE_CONFIG_H
+# include "config.h" /* for a build within gmp */
+#endif
+
+/* The ISO C99 standard specifies that in C++ implementations the
+ INTMAX_MAX, ... macros should only be defined if explicitly requested. */
+#if defined __cplusplus
+# define __STDC_LIMIT_MACROS
+# define __STDC_CONSTANT_MACROS
+#endif
+
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+
+#include "mpfr-impl.h"
+
+#ifdef _MPFR_H_HAVE_INTMAX_T
+
+/* We can't use fits_s.h <= mpfr_cmp_ui */
+int
+mpfr_fits_intmax_p (mpfr_srcptr f, mp_rnd_t rnd)
+{
+ mp_exp_t exp;
+ mp_prec_t prec;
+ intmax_t s;
+ mpfr_t x, y;
+ int neg;
+ int res;
+
+ if (MPFR_IS_NAN (f) || MPFR_IS_INF (f))
+ return 0; /* does not fit */
+
+ if (MPFR_IS_ZERO (f))
+ return 1; /* zero always fits */
+
+ /* now it fits if either
+ (a) MINIMUM <= f <= MAXIMUM
+ (b) or MINIMUM <= round(f, prec(slong), rnd) <= MAXIMUM */
+
+ exp = MPFR_EXP (f);
+ if (exp < 1)
+ return 1; /* |f| < 1: always fits */
+
+ neg = (MPFR_SIGN (f) > 0) ? 0 : 1;
+
+ /* let EXTREMUM be MAXIMUM if f > 0, and MINIMUM if f < 0 */
+
+ /* first compute prec(EXTREMUM), this could be done at configure time */
+ s = (neg) ? INTMAX_MIN : INTMAX_MAX;
+ for (prec = 0; s != 0; s /= 2, prec ++);
+
+ /* EXTREMUM needs prec bits, i.e. 2^(prec-1) <= |EXTREMUM| < 2^prec */
+
+ /* if exp < prec - 1, then f < 2^(prec-1) < |EXTREMUM| */
+ if ((mpfr_prec_t) exp < prec - 1)
+ return 1;
+
+ /* if exp > prec + 1, then f >= 2^prec > EXTREMUM */
+ if ((mpfr_prec_t) exp > prec + 1)
+ return 0;
+
+ /* remains cases exp = prec-1 to prec+1 */
+
+ /* hard case: first round to prec bits, then check */
+ mpfr_init2 (x, prec);
+ mpfr_init2 (y, prec);
+
+ mpfr_set (x, f, rnd);
+ mpfr_set_sj (y, neg ? INTMAX_MIN : INTMAX_MAX, GMP_RNDN);
+
+ res = (neg
+ ? (mpfr_cmp (x, y) >= 0)
+ : (mpfr_cmp (x, y) <= 0));
+
+ mpfr_clear (y);
+ mpfr_clear (x);
+
+ return res;
+}
+
+#endif
diff --git a/fits_uintmax.c b/fits_uintmax.c
new file mode 100644
index 000000000..8c2d8a657
--- /dev/null
+++ b/fits_uintmax.c
@@ -0,0 +1,97 @@
+/* mpfr_fits_uintmax_p -- test whether an mpfr fits an uintmax_t.
+
+Copyright 2004 Free Software Foundation.
+Contributed by the Spaces project, INRIA Lorraine.
+
+This file is part of the MPFR Library.
+
+The MPFR Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or (at your
+option) any later version.
+
+The MPFR Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the MPFR Library; see the file COPYING.LIB. If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+MA 02111-1307, USA. */
+
+#if HAVE_CONFIG_H
+# include "config.h" /* for a build within gmp */
+#endif
+
+/* The ISO C99 standard specifies that in C++ implementations the
+ INTMAX_MAX, ... macros should only be defined if explicitly requested. */
+#if defined __cplusplus
+# define __STDC_LIMIT_MACROS
+# define __STDC_CONSTANT_MACROS
+#endif
+
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+
+#include "mpfr-impl.h"
+
+#ifdef _MPFR_H_HAVE_INTMAX_T
+
+/* We can't use fits_u.h <= mpfr_cmp_ui */
+int
+mpfr_fits_uintmax_p (mpfr_srcptr f, mp_rnd_t rnd)
+{
+ mp_exp_t exp;
+ mp_prec_t prec;
+ uintmax_t s;
+ mpfr_t x, y;
+ int res;
+
+ if (MPFR_IS_NAN(f) || MPFR_IS_INF(f) || MPFR_SIGN(f) < 0)
+ return 0; /* does not fit */
+
+ if (MPFR_IS_ZERO(f))
+ return 1; /* zero always fits */
+
+ /* now it fits if
+ (a) f <= MAXIMUM
+ (b) round(f, prec(slong), rnd) <= MAXIMUM */
+
+ exp = MPFR_EXP(f);
+ if (exp < 1)
+ return 1; /* |f| < 1: always fits */
+
+ /* first compute prec(MAXIMUM) */
+ for (s = UINTMAX_MAX, prec = 0; s != 0; s /= 2, prec ++);
+
+ /* MAXIMUM needs prec bits, i.e. 2^(prec-1) <= |MAXIMUM| < 2^prec */
+
+ /* if exp < prec - 1, then f < 2^(prec-1) < |MAXIMUM| */
+ if ((mpfr_prec_t) exp < prec - 1)
+ return 1;
+
+ /* if exp > prec + 1, then f >= 2^prec > MAXIMUM */
+ if ((mpfr_prec_t) exp > prec + 1)
+ return 0;
+
+ /* remains cases exp = prec-1 to prec+1 */
+
+ /* hard case: first round to prec bits, then check */
+ mpfr_init2 (x, prec);
+ mpfr_init2 (y, prec);
+ mpfr_set (x, f, rnd);
+ res = mpfr_set_uj (y, UINTMAX_MAX, GMP_RNDN);
+ MPFR_ASSERTD (res == 0);
+ res = mpfr_cmp (x, y) <= 0;
+ mpfr_clear (y);
+ mpfr_clear (x);
+
+ return res;
+}
+
+#endif
diff --git a/mpfr.h b/mpfr.h
index 904627572..fa1483734 100644
--- a/mpfr.h
+++ b/mpfr.h
@@ -58,22 +58,23 @@ typedef enum {
#define MPFR_FLAGS_OVERFLOW 2
#define MPFR_FLAGS_NAN 4
#define MPFR_FLAGS_INEXACT 8
-#define MPFR_FLAGS_ALL 15
+#define MPFR_FLAGS_ERANGE 16
+#define MPFR_FLAGS_ALL 31
/* Define precision : 1 (short), 2 (int) or 3 (long) (DON'T USE IT!)*/
-#ifndef MPFR_PREC_FORMAT
+#ifndef _MPFR_PREC_FORMAT
# if __GMP_MP_SIZE_T_INT == 1
-# define MPFR_PREC_FORMAT 2
+# define _MPFR_PREC_FORMAT 2
# else
-# define MPFR_PREC_FORMAT 3
+# define _MPFR_PREC_FORMAT 3
# endif
#endif
-#if MPFR_PREC_FORMAT == 1
+#if _MPFR_PREC_FORMAT == 1
typedef unsigned short mpfr_prec_t;
-#elif MPFR_PREC_FORMAT == 2
+#elif _MPFR_PREC_FORMAT == 2
typedef unsigned int mpfr_prec_t;
-#elif MPFR_PREC_FORMAT == 3
+#elif _MPFR_PREC_FORMAT == 3
typedef unsigned long mpfr_prec_t;
#else
# error "Invalid MPFR Prec format"
@@ -183,10 +184,14 @@ void mpfr_clear_underflow _MPFR_PROTO ((void));
void mpfr_clear_overflow _MPFR_PROTO ((void));
void mpfr_clear_nanflag _MPFR_PROTO ((void));
void mpfr_clear_inexflag _MPFR_PROTO ((void));
+void mpfr_clear_erangeflag _MPFR_PROTO ((void));
+
int mpfr_underflow_p _MPFR_PROTO ((void));
int mpfr_overflow_p _MPFR_PROTO ((void));
int mpfr_nanflag_p _MPFR_PROTO ((void));
int mpfr_inexflag_p _MPFR_PROTO ((void));
+int mpfr_erangeflag_p _MPFR_PROTO ((void));
+
int mpfr_check_range _MPFR_PROTO ((mpfr_ptr, int, mpfr_rnd_t));
void mpfr_init2 _MPFR_PROTO ((mpfr_ptr, mpfr_prec_t));
@@ -363,6 +368,8 @@ int mpfr_fits_uint_p _MPFR_PROTO((mpfr_srcptr, mpfr_rnd_t));
int mpfr_fits_sint_p _MPFR_PROTO((mpfr_srcptr, mpfr_rnd_t));
int mpfr_fits_ushort_p _MPFR_PROTO((mpfr_srcptr, mpfr_rnd_t));
int mpfr_fits_sshort_p _MPFR_PROTO((mpfr_srcptr, mpfr_rnd_t));
+int mpfr_fits_uintmax_p _MPFR_PROTO((mpfr_srcptr, mpfr_rnd_t));
+int mpfr_fits_intmax_p _MPFR_PROTO((mpfr_srcptr, mpfr_rnd_t));
void mpfr_extract _MPFR_PROTO((mpz_ptr, mpfr_srcptr, unsigned int));
void mpfr_swap _MPFR_PROTO((mpfr_ptr, mpfr_ptr));
@@ -480,6 +487,8 @@ int mpfr_strtofr _MPFR_PROTO ((mpfr_ptr, __gmp_const char *, char **,
((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_NAN))
#define mpfr_clear_inexflag() \
((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_INEXACT))
+#define mpfr_clear_erangeflag() \
+ ((void) (__gmpfr_flags &= MPFR_FLAGS_ALL ^ MPFR_FLAGS_ERANGE))
#define mpfr_underflow_p() \
((int) (__gmpfr_flags & MPFR_FLAGS_UNDERFLOW))
#define mpfr_overflow_p() \
@@ -488,7 +497,9 @@ int mpfr_strtofr _MPFR_PROTO ((mpfr_ptr, __gmp_const char *, char **,
((int) (__gmpfr_flags & MPFR_FLAGS_NAN))
#define mpfr_inexflag_p() \
((int) (__gmpfr_flags & MPFR_FLAGS_INEXACT))
-
+#define mpfr_erangeflag_p() \
+ ((int) (__gmpfr_flags & MPFR_FLAGS_ERANGE))
+
#define mpfr_round(a,b) mpfr_rint((a), (b), GMP_RNDNA)
#define mpfr_trunc(a,b) mpfr_rint((a), (b), GMP_RNDZ)
#define mpfr_ceil(a,b) mpfr_rint((a), (b), GMP_RNDU)
@@ -525,8 +536,9 @@ int mpfr_strtofr _MPFR_PROTO ((mpfr_ptr, __gmp_const char *, char **,
/* When using GCC, optimize certain common comparisons.
Remove ICC since it defines __GNUC__, but produces a
- huge number of warnings if you use this code */
-#if defined (__GNUC__) && !defined(__ICC)
+ huge number of warnings if you use this code
+ Remove C++ too, since it complains too much... */
+#if defined (__GNUC__) && !defined(__ICC) && !defined(__cplusplus)
#if (__GNUC__ >= 2)
#undef mpfr_cmp_ui
#define mpfr_cmp_ui(_f,_u) \
diff --git a/tests/tfits.c b/tests/tfits.c
index 19d8325c1..7b33b9fa2 100644
--- a/tests/tfits.c
+++ b/tests/tfits.c
@@ -21,15 +21,35 @@ along with the MPFR Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
+#if HAVE_CONFIG_H
+# include "config.h" /* for a build within gmp */
+#endif
+
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
+/* The ISO C99 standard specifies that in C++ implementations the
+ INTMAX_MAX, ... macros should only be defined if explicitly requested. */
+#if defined __cplusplus
+# define __STDC_LIMIT_MACROS
+# define __STDC_CONSTANT_MACROS
+#endif
+
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+
#include "mpfr-test.h"
#define ERROR1 { printf("Initial error for x="); mpfr_dump(x); exit(1); }
#define ERROR2 { printf("Error for x="); mpfr_dump(x); exit(1); }
+static void check_intmax (void);
+
int
main (void)
{
@@ -145,6 +165,70 @@ main (void)
mpfr_clear (x);
+ check_intmax ();
+
tests_end_mpfr ();
return 0;
}
+
+static void check_intmax (void)
+{
+#ifdef _MPFR_H_HAVE_INTMAX_T
+ mpfr_t x;
+
+ mpfr_init2 (x, sizeof(uintmax_t)*CHAR_BIT);
+
+ /* Check NAN */
+ mpfr_set_nan(x);
+ if (mpfr_fits_uintmax_p(x, GMP_RNDN))
+ ERROR1;
+ if (mpfr_fits_intmax_p(x, GMP_RNDN))
+ ERROR1;
+
+ /* Check INF */
+ mpfr_set_inf(x, 1);
+ if (mpfr_fits_uintmax_p(x, GMP_RNDN))
+ ERROR1;
+ if (mpfr_fits_intmax_p(x, GMP_RNDN))
+ ERROR1;
+
+ /* Check Zero */
+ MPFR_SET_ZERO(x);
+ if (!mpfr_fits_uintmax_p(x, GMP_RNDN))
+ ERROR2;
+ if (!mpfr_fits_intmax_p(x, GMP_RNDN))
+ ERROR2;
+
+ /* Check small op */
+ mpfr_set_str1 (x, "1@-1");
+ if (!mpfr_fits_uintmax_p(x, GMP_RNDN))
+ ERROR2;
+ if (!mpfr_fits_intmax_p(x, GMP_RNDN))
+ ERROR2;
+
+ /* Check all other values */
+ mpfr_set_uj (x, UINTMAX_MAX, GMP_RNDN);
+ mpfr_add_ui (x, x, 1, GMP_RNDN);
+ if (mpfr_fits_uintmax_p (x, GMP_RNDN))
+ ERROR1;
+ mpfr_set_uj (x, UINTMAX_MAX, GMP_RNDN);
+ if (!mpfr_fits_uintmax_p (x, GMP_RNDN))
+ ERROR2;
+ mpfr_set_sj (x, INTMAX_MAX, GMP_RNDN);
+ mpfr_add_ui (x, x, 1, GMP_RNDN);
+ if (mpfr_fits_intmax_p (x, GMP_RNDN))
+ ERROR1;
+ mpfr_set_sj (x, INTMAX_MAX, GMP_RNDN);
+ if (!mpfr_fits_intmax_p (x, GMP_RNDN))
+ ERROR2;
+ mpfr_set_sj (x, INTMAX_MIN, GMP_RNDN);
+ if (!mpfr_fits_intmax_p (x, GMP_RNDN))
+ ERROR2;
+ mpfr_sub_ui (x, x, 1, GMP_RNDN);
+ if (mpfr_fits_intmax_p (x, GMP_RNDN))
+ ERROR1;
+
+ mpfr_clear (x);
+#endif
+}
+