diff options
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | mpq/abs.c | 12 | ||||
-rw-r--r-- | mpq/get_den.c | 10 | ||||
-rw-r--r-- | mpq/get_num.c | 11 | ||||
-rw-r--r-- | mpq/inv.c | 35 | ||||
-rw-r--r-- | mpq/md_2exp.c | 10 | ||||
-rw-r--r-- | mpq/neg.c | 19 | ||||
-rw-r--r-- | mpq/set.c | 13 | ||||
-rw-r--r-- | mpq/set_d.c | 8 | ||||
-rw-r--r-- | mpq/set_den.c | 10 | ||||
-rw-r--r-- | mpq/set_num.c | 9 | ||||
-rw-r--r-- | mpq/set_z.c | 7 |
12 files changed, 75 insertions, 73 deletions
@@ -1,3 +1,7 @@ +2012-06-01 Marco Bodrato <bodrato@mail.dm.unipi.it> + + * mpq: Use more macros and MPZ_REALLOC return value when possible. + 2012-05-31 Marc Glisse <marc.glisse@inria.fr> * gmpxx.h (mpq_class::mpq_class): Handle mpq_class(0,1). @@ -1,6 +1,6 @@ /* mpq_abs -- absolute value of a rational. -Copyright 2000, 2001 Free Software Foundation, Inc. +Copyright 2000, 2001, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -31,14 +31,14 @@ mpq_abs (mpq_ptr dst, mpq_srcptr src) if (dst != src) { mp_size_t den_size = SIZ(DEN(src)); + mp_ptr dp; - MPZ_REALLOC (mpq_numref(dst), num_abs_size); - MPZ_REALLOC (mpq_denref(dst), den_size); - - MPN_COPY (PTR(NUM(dst)), PTR(NUM(src)), num_abs_size); - MPN_COPY (PTR(DEN(dst)), PTR(DEN(src)), den_size); + dp = MPZ_REALLOC (NUM(dst), num_abs_size); + MPN_COPY (dp, PTR(NUM(src)), num_abs_size); + dp = MPZ_REALLOC (DEN(dst), den_size); SIZ(DEN(dst)) = den_size; + MPN_COPY (dp, PTR(DEN(src)), den_size); } SIZ(NUM(dst)) = num_abs_size; diff --git a/mpq/get_den.c b/mpq/get_den.c index c883824a2..19d0fcd45 100644 --- a/mpq/get_den.c +++ b/mpq/get_den.c @@ -1,6 +1,6 @@ /* mpq_get_den(den,rat_src) -- Set DEN to the denominator of RAT_SRC. -Copyright 1991, 1994, 1995, 2001 Free Software Foundation, Inc. +Copyright 1991, 1994, 1995, 2001, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -21,12 +21,12 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ #include "gmp-impl.h" void -mpq_get_den (MP_INT *den, const MP_RAT *src) +mpq_get_den (mpz_ptr den, mpq_srcptr src) { mp_size_t size = SIZ(DEN(src)); + mp_ptr dp; - MPZ_REALLOC (den, size); - - MPN_COPY (PTR(den), PTR(DEN(src)), size); + dp = MPZ_REALLOC (den, size); SIZ(den) = size; + MPN_COPY (dp, PTR(DEN(src)), size); } diff --git a/mpq/get_num.c b/mpq/get_num.c index f714da279..2029ff7c4 100644 --- a/mpq/get_num.c +++ b/mpq/get_num.c @@ -1,6 +1,6 @@ /* mpq_get_num(num,rat_src) -- Set NUM to the numerator of RAT_SRC. -Copyright 1991, 1994, 1995, 2001 Free Software Foundation, Inc. +Copyright 1991, 1994, 1995, 2001, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -21,13 +21,14 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ #include "gmp-impl.h" void -mpq_get_num (MP_INT *num, const MP_RAT *src) +mpq_get_num (mpz_ptr num, mpq_srcptr src) { mp_size_t size = SIZ(NUM(src)); mp_size_t abs_size = ABS (size); + mp_ptr dp; - MPZ_REALLOC (num, abs_size); - - MPN_COPY (PTR(num), PTR(NUM(src)), abs_size); + dp = MPZ_REALLOC (num, abs_size); SIZ(num) = size; + + MPN_COPY (dp, PTR(NUM(src)), abs_size); } @@ -1,7 +1,7 @@ /* mpq_inv(dest,src) -- invert a rational number, i.e. set DEST to SRC with the numerator and denominator swapped. -Copyright 1991, 1994, 1995, 2000, 2001 Free Software Foundation, Inc. +Copyright 1991, 1994, 1995, 2000, 2001, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -22,44 +22,39 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ #include "gmp-impl.h" void -mpq_inv (MP_RAT *dest, const MP_RAT *src) +mpq_inv (mpq_ptr dest, mpq_srcptr src) { mp_size_t num_size = SIZ(NUM(src)); mp_size_t den_size = SIZ(DEN(src)); - if (UNLIKELY (num_size == 0)) - DIVIDE_BY_ZERO; - if (num_size < 0) { num_size = -num_size; den_size = -den_size; } + else if (UNLIKELY (num_size == 0)) + DIVIDE_BY_ZERO; + SIZ(DEN(dest)) = num_size; SIZ(NUM(dest)) = den_size; - /* If dest == src we may just swap the numerator and denominator, but - we have to ensure the new denominator is positive. */ + /* If dest == src we may just swap the numerator and denominator; + we ensured that the new denominator is positive. */ if (dest == src) { - mp_size_t alloc = ALLOC(NUM(dest)); - mp_ptr limb_ptr = PTR(NUM(dest)); - - ALLOC(NUM(dest)) = ALLOC(DEN(dest)); - PTR(NUM(dest)) = PTR(DEN(dest)); - - ALLOC(DEN(dest)) = alloc; - PTR(DEN(dest)) = limb_ptr; + MP_PTR_SWAP (PTR(NUM(dest)), PTR(DEN(dest))); + MP_SIZE_T_SWAP (ALLOC(NUM(dest)), ALLOC(DEN(dest))); } else { - den_size = ABS (den_size); + mp_ptr dp; - MPZ_REALLOC (NUM(dest), den_size); - MPZ_REALLOC (DEN(dest), num_size); + den_size = ABS (den_size); + dp = MPZ_REALLOC (NUM(dest), den_size); + MPN_COPY (dp, PTR(DEN(src)), den_size); - MPN_COPY (PTR(NUM(dest)), PTR(DEN(src)), den_size); - MPN_COPY (PTR(DEN(dest)), PTR(NUM(src)), num_size); + dp = MPZ_REALLOC (DEN(dest), num_size); + MPN_COPY (dp, PTR(NUM(src)), num_size); } } diff --git a/mpq/md_2exp.c b/mpq/md_2exp.c index 2038c4fc8..bad4fc35b 100644 --- a/mpq/md_2exp.c +++ b/mpq/md_2exp.c @@ -1,7 +1,7 @@ /* mpq_mul_2exp, mpq_div_2exp - multiply or divide by 2^N */ /* -Copyright 2000, 2002 Free Software Foundation, Inc. +Copyright 2000, 2002, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -82,14 +82,13 @@ mord_2exp (mpz_ptr ldst, mpz_ptr rdst, mpz_srcptr lsrc, mpz_srcptr rsrc, void mpq_mul_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n) { - mord_2exp (mpq_numref (dst), mpq_denref (dst), - mpq_numref (src), mpq_denref (src), n); + mord_2exp (NUM(dst), DEN(dst), NUM(src), DEN(src), n); } void mpq_div_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n) { - if (SIZ (mpq_numref(src)) == 0) + if (SIZ(NUM(src)) == 0) { SIZ(NUM(dst)) = 0; SIZ(DEN(dst)) = 1; @@ -97,6 +96,5 @@ mpq_div_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n) return; } - mord_2exp (mpq_denref (dst), mpq_numref (dst), - mpq_denref (src), mpq_numref (src), n); + mord_2exp (DEN(dst), NUM(dst), DEN(src), NUM(src), n); } @@ -1,6 +1,6 @@ /* mpq_neg -- negate a rational. -Copyright 2000, 2001 Free Software Foundation, Inc. +Copyright 2000, 2001, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -30,16 +30,17 @@ mpq_neg (mpq_ptr dst, mpq_srcptr src) if (src != dst) { - mp_size_t num_abs_size = ABS(num_size); - mp_size_t den_size = SIZ(DEN(src)); + mp_size_t size; + mp_ptr dp; - MPZ_REALLOC (mpq_numref(dst), num_abs_size); - MPZ_REALLOC (mpq_denref(dst), den_size); + size = ABS(num_size); + dp = MPZ_REALLOC (NUM(dst), size); + MPN_COPY (dp, PTR(NUM(src)), size); - MPN_COPY (PTR(NUM(dst)), PTR(NUM(src)), num_abs_size); - MPN_COPY (PTR(DEN(dst)), PTR(DEN(src)), den_size); - - SIZ(DEN(dst)) = den_size; + size = SIZ(DEN(src)); + dp = MPZ_REALLOC (DEN(dst), size); + SIZ(DEN(dst)) = size; + MPN_COPY (dp, PTR(DEN(src)), size); } SIZ(NUM(dst)) = -num_size; @@ -1,6 +1,6 @@ /* mpq_set(dest,src) -- Set DEST to SRC. -Copyright 1991, 1994, 1995, 2001 Free Software Foundation, Inc. +Copyright 1991, 1994, 1995, 2001, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -21,19 +21,20 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ #include "gmp-impl.h" void -mpq_set (MP_RAT *dest, const MP_RAT *src) +mpq_set (mpq_ptr dest, mpq_srcptr src) { mp_size_t num_size, den_size; mp_size_t abs_num_size; + mp_ptr dp; num_size = SIZ(NUM(src)); abs_num_size = ABS (num_size); - MPZ_REALLOC (NUM(dest), abs_num_size); - MPN_COPY (PTR(NUM(dest)), PTR(NUM(src)), abs_num_size); + dp = MPZ_REALLOC (NUM(dest), abs_num_size); SIZ(NUM(dest)) = num_size; + MPN_COPY (dp, PTR(NUM(src)), abs_num_size); den_size = SIZ(DEN(src)); - MPZ_REALLOC (DEN(dest), den_size); - MPN_COPY (PTR(DEN(dest)), PTR(DEN(src)), den_size); + dp = MPZ_REALLOC (DEN(dest), den_size); SIZ(DEN(dest)) = den_size; + MPN_COPY (dp, PTR(DEN(src)), den_size); } diff --git a/mpq/set_d.c b/mpq/set_d.c index 07e2c3b2b..1936587a6 100644 --- a/mpq/set_d.c +++ b/mpq/set_d.c @@ -1,6 +1,6 @@ /* mpq_set_d(mpq_t q, double d) -- Set q to d without rounding. -Copyright 2000, 2002, 2003 Free Software Foundation, Inc. +Copyright 2000, 2002, 2003, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -98,8 +98,7 @@ mpq_set_d (mpq_ptr dest, double d) #endif dn += nn + 1; ASSERT_ALWAYS (dn > 0); - MPZ_REALLOC (DEN(dest), dn); - dp = PTR(DEN(dest)); + dp = MPZ_REALLOC (DEN(dest), dn); MPN_ZERO (dp, dn - 1); dp[dn - 1] = 1; count_trailing_zeros (c, np[0] | dp[0]); @@ -116,8 +115,7 @@ mpq_set_d (mpq_ptr dest, double d) else { nn = exp; - MPZ_REALLOC (NUM(dest), nn); - np = PTR(NUM(dest)); + np = MPZ_REALLOC (NUM(dest), nn); switch (nn) { default: diff --git a/mpq/set_den.c b/mpq/set_den.c index c030f2978..45828c503 100644 --- a/mpq/set_den.c +++ b/mpq/set_den.c @@ -1,6 +1,7 @@ /* mpq_set_den(dest,den) -- Set the denominator of DEST from DEN. -Copyright 1991, 1994, 1995, 1996, 2000, 2001 Free Software Foundation, Inc. +Copyright 1991, 1994, 1995, 1996, 2000, 2001, 2012 Free Software +Foundation, Inc. This file is part of the GNU MP Library. @@ -21,13 +22,14 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ #include "gmp-impl.h" void -mpq_set_den (MP_RAT *dest, const MP_INT *den) +mpq_set_den (mpq_ptr dest, mpz_srcptr den) { mp_size_t size = SIZ (den); mp_size_t abs_size = ABS (size); + mp_ptr dp; - MPZ_REALLOC (DEN(dest), abs_size); + dp = MPZ_REALLOC (DEN(dest), abs_size); - MPN_COPY (PTR(DEN(dest)), PTR(den), abs_size); SIZ(DEN(dest)) = size; + MPN_COPY (dp, PTR(den), abs_size); } diff --git a/mpq/set_num.c b/mpq/set_num.c index 1f011fdc6..4643a7213 100644 --- a/mpq/set_num.c +++ b/mpq/set_num.c @@ -1,6 +1,6 @@ /* mpq_set_num(dest,num) -- Set the numerator of DEST from NUM. -Copyright 1991, 1994, 1995, 2001 Free Software Foundation, Inc. +Copyright 1991, 1994, 1995, 2001, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -21,13 +21,14 @@ along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ #include "gmp-impl.h" void -mpq_set_num (MP_RAT *dest, const MP_INT *num) +mpq_set_num (mpq_ptr dest, mpz_srcptr num) { mp_size_t size = SIZ (num); mp_size_t abs_size = ABS (size); + mp_ptr dp; - MPZ_REALLOC (NUM(dest), abs_size); + dp = MPZ_REALLOC (NUM(dest), abs_size); - MPN_COPY (PTR(NUM(dest)), PTR(num), abs_size); SIZ(NUM(dest)) = size; + MPN_COPY (dp, PTR(num), abs_size); } diff --git a/mpq/set_z.c b/mpq/set_z.c index b13a31209..bda176db3 100644 --- a/mpq/set_z.c +++ b/mpq/set_z.c @@ -1,6 +1,6 @@ /* mpq_set_z (dest,src) -- Set DEST to SRC. -Copyright 1996, 2001 Free Software Foundation, Inc. +Copyright 1996, 2001, 2012 Free Software Foundation, Inc. This file is part of the GNU MP Library. @@ -25,12 +25,13 @@ mpq_set_z (mpq_ptr dest, mpz_srcptr src) { mp_size_t num_size; mp_size_t abs_num_size; + mp_ptr dp; num_size = SIZ (src); abs_num_size = ABS (num_size); - MPZ_REALLOC (NUM(dest), abs_num_size); - MPN_COPY (PTR(NUM(dest)), PTR(src), abs_num_size); + dp = MPZ_REALLOC (NUM(dest), abs_num_size); SIZ(NUM(dest)) = num_size; + MPN_COPY (dp, PTR(src), abs_num_size); PTR(DEN(dest))[0] = 1; SIZ(DEN(dest)) = 1; |