summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--gmp-impl.h15
-rw-r--r--mpq/abs.c4
-rw-r--r--mpq/get_den.c2
-rw-r--r--mpq/get_num.c2
-rw-r--r--mpq/inv.c4
-rw-r--r--mpq/neg.c4
-rw-r--r--mpq/set.c4
-rw-r--r--mpq/set_d.c6
-rw-r--r--mpq/set_den.c2
-rw-r--r--mpq/set_f.c6
-rw-r--r--mpq/set_num.c2
-rw-r--r--mpq/set_z.c2
-rw-r--r--mpz/bin_uiui.c10
-rw-r--r--mpz/oddfac_1.c12
-rw-r--r--mpz/prodlimbs.c4
16 files changed, 53 insertions, 31 deletions
diff --git a/ChangeLog b/ChangeLog
index ee65eabf0..edd35e338 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,11 @@
* gmp-impl.h (__GMP_WITHIN_CONFIGURE): Use the same #if as in gmp-h.in.
(MPN_NORMALIZE_NOT_ZERO): Tighter ASSERT.
+ (MPZ_NEWALLOC): New macro.
+ * mpq: Use the new macro when possible.
+ * mpz/bin_uiui.c: Likewise.
+ * mpz/oddfac_1.c: Likewise.
+ * mpz/prodlimbs.c: Likewise.
2012-06-04 Torbjorn Granlund <tege@gmplib.org>
diff --git a/gmp-impl.h b/gmp-impl.h
index 4bc5697b4..ce86c3b69 100644
--- a/gmp-impl.h
+++ b/gmp-impl.h
@@ -1880,10 +1880,25 @@ __GMP_DECLSPEC void mpn_copyd (mp_ptr, mp_srcptr, mp_size_t);
__x->_mp_d = TMP_ALLOC_LIMBS (NLIMBS); \
} while (0)
+#if WANT_ASSERT
+static void *
+_mpz_newalloc (mpz_ptr z, mp_size_t n)
+{
+ void * res = _mpz_realloc(z,n);
+ /* If we are checking the code, force a random change to limbs. */
+ ((mp_ptr) res)[0] = ~ ((mp_ptr) res)[ALLOC (z) - 1];
+ return res;
+}
+#else
+#define _mpz_newalloc _mpz_realloc
+#endif
/* Realloc for an mpz_t WHAT if it has less than NEEDED limbs. */
#define MPZ_REALLOC(z,n) (UNLIKELY ((n) > ALLOC(z)) \
? (mp_ptr) _mpz_realloc(z,n) \
: PTR(z))
+#define MPZ_NEWALLOC(z,n) (UNLIKELY ((n) > ALLOC(z)) \
+ ? (mp_ptr) _mpz_newalloc(z,n) \
+ : PTR(z))
#define MPZ_EQUAL_1_P(z) (SIZ(z)==1 && PTR(z)[0] == 1)
diff --git a/mpq/abs.c b/mpq/abs.c
index 262eda33b..3f0caeba9 100644
--- a/mpq/abs.c
+++ b/mpq/abs.c
@@ -33,10 +33,10 @@ mpq_abs (mpq_ptr dst, mpq_srcptr src)
mp_size_t den_size = SIZ(DEN(src));
mp_ptr dp;
- dp = MPZ_REALLOC (NUM(dst), num_abs_size);
+ dp = MPZ_NEWALLOC (NUM(dst), num_abs_size);
MPN_COPY (dp, PTR(NUM(src)), num_abs_size);
- dp = MPZ_REALLOC (DEN(dst), den_size);
+ dp = MPZ_NEWALLOC (DEN(dst), den_size);
SIZ(DEN(dst)) = den_size;
MPN_COPY (dp, PTR(DEN(src)), den_size);
}
diff --git a/mpq/get_den.c b/mpq/get_den.c
index 19d0fcd45..6a969dfe3 100644
--- a/mpq/get_den.c
+++ b/mpq/get_den.c
@@ -26,7 +26,7 @@ mpq_get_den (mpz_ptr den, mpq_srcptr src)
mp_size_t size = SIZ(DEN(src));
mp_ptr dp;
- dp = MPZ_REALLOC (den, size);
+ dp = MPZ_NEWALLOC (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 2029ff7c4..bbc6448fd 100644
--- a/mpq/get_num.c
+++ b/mpq/get_num.c
@@ -27,7 +27,7 @@ mpq_get_num (mpz_ptr num, mpq_srcptr src)
mp_size_t abs_size = ABS (size);
mp_ptr dp;
- dp = MPZ_REALLOC (num, abs_size);
+ dp = MPZ_NEWALLOC (num, abs_size);
SIZ(num) = size;
MPN_COPY (dp, PTR(NUM(src)), abs_size);
diff --git a/mpq/inv.c b/mpq/inv.c
index 0c6b5e8f1..48f0ae75f 100644
--- a/mpq/inv.c
+++ b/mpq/inv.c
@@ -51,10 +51,10 @@ mpq_inv (mpq_ptr dest, mpq_srcptr src)
mp_ptr dp;
den_size = ABS (den_size);
- dp = MPZ_REALLOC (NUM(dest), den_size);
+ dp = MPZ_NEWALLOC (NUM(dest), den_size);
MPN_COPY (dp, PTR(DEN(src)), den_size);
- dp = MPZ_REALLOC (DEN(dest), num_size);
+ dp = MPZ_NEWALLOC (DEN(dest), num_size);
MPN_COPY (dp, PTR(NUM(src)), num_size);
}
}
diff --git a/mpq/neg.c b/mpq/neg.c
index a292cced2..32115c2fc 100644
--- a/mpq/neg.c
+++ b/mpq/neg.c
@@ -34,11 +34,11 @@ mpq_neg (mpq_ptr dst, mpq_srcptr src)
mp_ptr dp;
size = ABS(num_size);
- dp = MPZ_REALLOC (NUM(dst), size);
+ dp = MPZ_NEWALLOC (NUM(dst), size);
MPN_COPY (dp, PTR(NUM(src)), size);
size = SIZ(DEN(src));
- dp = MPZ_REALLOC (DEN(dst), size);
+ dp = MPZ_NEWALLOC (DEN(dst), size);
SIZ(DEN(dst)) = size;
MPN_COPY (dp, PTR(DEN(src)), size);
}
diff --git a/mpq/set.c b/mpq/set.c
index cba41701c..b2dad7e59 100644
--- a/mpq/set.c
+++ b/mpq/set.c
@@ -29,12 +29,12 @@ mpq_set (mpq_ptr dest, mpq_srcptr src)
num_size = SIZ(NUM(src));
abs_num_size = ABS (num_size);
- dp = MPZ_REALLOC (NUM(dest), abs_num_size);
+ dp = MPZ_NEWALLOC (NUM(dest), abs_num_size);
SIZ(NUM(dest)) = num_size;
MPN_COPY (dp, PTR(NUM(src)), abs_num_size);
den_size = SIZ(DEN(src));
- dp = MPZ_REALLOC (DEN(dest), den_size);
+ dp = MPZ_NEWALLOC (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 1936587a6..c56ef5d69 100644
--- a/mpq/set_d.c
+++ b/mpq/set_d.c
@@ -71,7 +71,7 @@ mpq_set_d (mpq_ptr dest, double d)
}
dn = -exp;
- np = MPZ_REALLOC (NUM(dest), 3);
+ np = MPZ_NEWALLOC (NUM(dest), 3);
#if LIMBS_PER_DOUBLE == 4
if ((tp[0] | tp[1] | tp[2]) == 0)
np[0] = tp[3], nn = 1;
@@ -98,7 +98,7 @@ mpq_set_d (mpq_ptr dest, double d)
#endif
dn += nn + 1;
ASSERT_ALWAYS (dn > 0);
- dp = MPZ_REALLOC (DEN(dest), dn);
+ dp = MPZ_NEWALLOC (DEN(dest), dn);
MPN_ZERO (dp, dn - 1);
dp[dn - 1] = 1;
count_trailing_zeros (c, np[0] | dp[0]);
@@ -115,7 +115,7 @@ mpq_set_d (mpq_ptr dest, double d)
else
{
nn = exp;
- np = MPZ_REALLOC (NUM(dest), nn);
+ np = MPZ_NEWALLOC (NUM(dest), nn);
switch (nn)
{
default:
diff --git a/mpq/set_den.c b/mpq/set_den.c
index 45828c503..e9ee17b71 100644
--- a/mpq/set_den.c
+++ b/mpq/set_den.c
@@ -28,7 +28,7 @@ mpq_set_den (mpq_ptr dest, mpz_srcptr den)
mp_size_t abs_size = ABS (size);
mp_ptr dp;
- dp = MPZ_REALLOC (DEN(dest), abs_size);
+ dp = MPZ_NEWALLOC (DEN(dest), abs_size);
SIZ(DEN(dest)) = size;
MPN_COPY (dp, PTR(den), abs_size);
diff --git a/mpq/set_f.c b/mpq/set_f.c
index c62b3c63b..d1e14867b 100644
--- a/mpq/set_f.c
+++ b/mpq/set_f.c
@@ -49,7 +49,7 @@ mpq_set_f (mpq_ptr q, mpf_srcptr f)
/* radix point is to the right of the limbs, no denominator */
mp_ptr num_ptr;
- num_ptr = MPZ_REALLOC (mpq_numref (q), fexp);
+ num_ptr = MPZ_NEWALLOC (mpq_numref (q), fexp);
MPN_ZERO (num_ptr, fexp - abs_fsize);
MPN_COPY (num_ptr + fexp - abs_fsize, fptr, abs_fsize);
@@ -64,8 +64,8 @@ mpq_set_f (mpq_ptr q, mpf_srcptr f)
mp_size_t den_size;
den_size = abs_fsize - fexp;
- num_ptr = MPZ_REALLOC (mpq_numref (q), abs_fsize);
- den_ptr = MPZ_REALLOC (mpq_denref (q), den_size+1);
+ num_ptr = MPZ_NEWALLOC (mpq_numref (q), abs_fsize);
+ den_ptr = MPZ_NEWALLOC (mpq_denref (q), den_size+1);
if (flow & 1)
{
diff --git a/mpq/set_num.c b/mpq/set_num.c
index 4643a7213..5b018c808 100644
--- a/mpq/set_num.c
+++ b/mpq/set_num.c
@@ -27,7 +27,7 @@ mpq_set_num (mpq_ptr dest, mpz_srcptr num)
mp_size_t abs_size = ABS (size);
mp_ptr dp;
- dp = MPZ_REALLOC (NUM(dest), abs_size);
+ dp = MPZ_NEWALLOC (NUM(dest), 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 bda176db3..e6f3ff0b6 100644
--- a/mpq/set_z.c
+++ b/mpq/set_z.c
@@ -29,7 +29,7 @@ mpq_set_z (mpq_ptr dest, mpz_srcptr src)
num_size = SIZ (src);
abs_num_size = ABS (num_size);
- dp = MPZ_REALLOC (NUM(dest), abs_num_size);
+ dp = MPZ_NEWALLOC (NUM(dest), abs_num_size);
SIZ(NUM(dest)) = num_size;
MPN_COPY (dp, PTR(src), abs_num_size);
diff --git a/mpz/bin_uiui.c b/mpz/bin_uiui.c
index 65979c297..a0b988fd5 100644
--- a/mpz/bin_uiui.c
+++ b/mpz/bin_uiui.c
@@ -294,9 +294,9 @@ mpz_bdiv_bin_uiui (mpz_ptr r, unsigned long int n, unsigned long int k)
nn -= np[nn - 1] == 0; /* normalisation */
- MPZ_REALLOC (r, nn);
+ kp = MPZ_NEWALLOC (r, nn);
SIZ(r) = nn;
- MPN_COPY (PTR(r), np, nn);
+ MPN_COPY (kp, np, nn);
TMP_FREE;
}
@@ -312,7 +312,7 @@ mpz_smallk_bin_uiui (mpz_ptr r, unsigned long int n, unsigned long int k)
count_leading_zeros (cnt, (mp_limb_t) n);
cnt = GMP_LIMB_BITS - cnt;
alloc = cnt * k / GMP_NUMB_BITS + 3; /* FIXME: ensure rounding is enough. */
- rp = MPZ_REALLOC (r, alloc);
+ rp = MPZ_NEWALLOC (r, alloc);
MAXFACS (nmax, n);
nmax = MIN (nmax, M);
@@ -411,8 +411,8 @@ mpz_smallkdc_bin_uiui (mpz_ptr r, unsigned long int n, unsigned long int k)
mp_limb_t buffer[ODD_CENTRAL_BINOMIAL_TABLE_LIMIT + 3];
mpz_t t;
- t->_mp_alloc = ODD_CENTRAL_BINOMIAL_TABLE_LIMIT + 3;
- t->_mp_d = buffer;
+ ALLOC (t) = ODD_CENTRAL_BINOMIAL_TABLE_LIMIT + 3;
+ PTR (t) = buffer;
if ((! BIN_UIUI_RECURSIVE_SMALLDC) || k <= ODD_FACTORIAL_TABLE_LIMIT)
mpz_smallk_bin_uiui (t, n, k);
else
diff --git a/mpz/oddfac_1.c b/mpz/oddfac_1.c
index 298ad14ef..a6bc9d263 100644
--- a/mpz/oddfac_1.c
+++ b/mpz/oddfac_1.c
@@ -284,8 +284,10 @@ mpz_oddfac_1 (mpz_ptr x, mp_limb_t n, unsigned flag)
}
else if (n <= ODD_DOUBLEFACTORIAL_TABLE_LIMIT + 1)
{
- MPZ_REALLOC (x, 2);
- umul_ppmm (PTR (x)[1], PTR (x)[0], __gmp_odd2fac_table[(n - 1) >> 1], __gmp_oddfac_table[n >> 1]);
+ mp_ptr px;
+
+ px = MPZ_NEWALLOC (x, 2);
+ umul_ppmm (px[1], px[0], __gmp_odd2fac_table[(n - 1) >> 1], __gmp_oddfac_table[n >> 1]);
SIZ (x) = 2;
}
else
@@ -375,7 +377,7 @@ mpz_oddfac_1 (mpz_ptr x, mp_limb_t n, unsigned flag)
factors = TMP_ALLOC_LIMBS (size);
do {
- mp_ptr square;
+ mp_ptr square, px;
mp_size_t nx, ns;
mp_limb_t cy;
TMP_DECL;
@@ -398,9 +400,9 @@ mpz_oddfac_1 (mpz_ptr x, mp_limb_t n, unsigned flag)
}
ns = SIZ (mswing);
nx = size + ns;
- MPZ_REALLOC (x, nx);
+ px = MPZ_NEWALLOC (x, nx);
ASSERT (ns <= size);
- cy = mpn_mul (PTR(x), square, size, PTR(mswing), ns); /* n!= n$ * floor(n/2)!^2 */
+ cy = mpn_mul (px, square, size, PTR(mswing), ns); /* n!= n$ * floor(n/2)!^2 */
TMP_FREE;
SIZ(x) = nx - (cy == 0);
diff --git a/mpz/prodlimbs.c b/mpz/prodlimbs.c
index bf676419c..867688755 100644
--- a/mpz/prodlimbs.c
+++ b/mpz/prodlimbs.c
@@ -66,7 +66,7 @@ mpz_prodlimbs (mpz_ptr x, mp_ptr factors, mp_size_t j)
size += cy != 0;
};
- prod = MPZ_REALLOC (x, size + 1);
+ prod = MPZ_NEWALLOC (x, size + 1);
cy = mpn_mul_1 (prod, factors, size, factors[i]);
prod[size] = cy;
@@ -86,7 +86,7 @@ mpz_prodlimbs (mpz_ptr x, mp_ptr factors, mp_size_t j)
j = mpz_prodlimbs (x2, factors + i, j);
i = mpz_prodlimbs (x1, factors, i);
size = i + j;
- prod = MPZ_REALLOC (x, size);
+ prod = MPZ_NEWALLOC (x, size);
if (i >= j)
cy = mpn_mul (prod, PTR(x1), i, PTR(x2), j);
else