From 50af3bf618a4a1e693b129779ed8cbe7c88dc430 Mon Sep 17 00:00:00 2001 From: Marco Bodrato Date: Thu, 26 Apr 2018 23:37:52 +0200 Subject: mpq/: Support lazy mpq_t also in the denominator. --- mpq/clear.c | 3 ++- mpq/clears.c | 3 ++- mpq/div.c | 9 +++------ mpq/inp_str.c | 2 +- mpq/md_2exp.c | 2 +- mpq/mul.c | 2 +- mpq/set_d.c | 4 ++-- mpq/set_f.c | 4 ++-- mpq/set_si.c | 2 +- mpq/set_str.c | 2 +- mpq/set_ui.c | 2 +- mpq/set_z.c | 2 +- 12 files changed, 18 insertions(+), 19 deletions(-) (limited to 'mpq') diff --git a/mpq/clear.c b/mpq/clear.c index c5baa77f4..89c7933c6 100644 --- a/mpq/clear.c +++ b/mpq/clear.c @@ -35,5 +35,6 @@ mpq_clear (mpq_t x) { if (ALLOC (NUM(x))) __GMP_FREE_FUNC_LIMBS (PTR(NUM(x)), ALLOC(NUM(x))); - __GMP_FREE_FUNC_LIMBS (PTR(DEN(x)), ALLOC(DEN(x))); + if (ALLOC (DEN(x))) + __GMP_FREE_FUNC_LIMBS (PTR(DEN(x)), ALLOC(DEN(x))); } diff --git a/mpq/clears.c b/mpq/clears.c index 1631067f6..ad1cf5e0e 100644 --- a/mpq/clears.c +++ b/mpq/clears.c @@ -42,7 +42,8 @@ mpq_clears (mpq_ptr x, ...) { if (ALLOC (NUM(x))) __GMP_FREE_FUNC_LIMBS (PTR(NUM(x)), ALLOC(NUM(x))); - __GMP_FREE_FUNC_LIMBS (PTR(DEN(x)), ALLOC(DEN(x))); + if (ALLOC (DEN(x))) + __GMP_FREE_FUNC_LIMBS (PTR(DEN(x)), ALLOC(DEN(x))); x = va_arg (ap, mpq_ptr); } while (x != NULL); diff --git a/mpq/div.c b/mpq/div.c index c3a60a7e6..32d77b645 100644 --- a/mpq/div.c +++ b/mpq/div.c @@ -48,12 +48,9 @@ mpq_div (mpq_ptr quot, mpq_srcptr op1, mpq_srcptr op2) if (UNLIKELY (quot == op2)) { - if (op1 == op2) + if (UNLIKELY (op1 == op2)) { - PTR(NUM(quot))[0] = 1; - SIZ(NUM(quot)) = 1; - PTR(DEN(quot))[0] = 1; - SIZ(DEN(quot)) = 1; + mpq_set_ui (quot, 1, 1); return; } @@ -82,7 +79,7 @@ mpq_div (mpq_ptr quot, mpq_srcptr op1, mpq_srcptr op2) /* We special case this to simplify allocation logic; gcd(0,x) = x is a singular case for the allocations. */ SIZ(NUM(quot)) = 0; - PTR(DEN(quot))[0] = 1; + MPZ_NEWALLOC (DEN(quot), 1)[0] = 1; SIZ(DEN(quot)) = 1; return; } diff --git a/mpq/inp_str.c b/mpq/inp_str.c index e2407eb9b..b1fbbc9e1 100644 --- a/mpq/inp_str.c +++ b/mpq/inp_str.c @@ -43,7 +43,7 @@ mpq_inp_str (mpq_ptr q, FILE *fp, int base) fp = stdin; SIZ(DEN(q)) = 1; - PTR(DEN(q))[0] = 1; + MPZ_NEWALLOC (DEN(q), 1)[0] = 1; nread = mpz_inp_str (mpq_numref(q), fp, base); if (nread == 0) diff --git a/mpq/md_2exp.c b/mpq/md_2exp.c index 13623d83a..76f1f4436 100644 --- a/mpq/md_2exp.c +++ b/mpq/md_2exp.c @@ -102,7 +102,7 @@ mpq_div_2exp (mpq_ptr dst, mpq_srcptr src, mp_bitcnt_t n) { SIZ(NUM(dst)) = 0; SIZ(DEN(dst)) = 1; - PTR(DEN(dst))[0] = 1; + MPZ_NEWALLOC (DEN(dst), 1)[0] = 1; return; } diff --git a/mpq/mul.c b/mpq/mul.c index 3fcae2fc7..270dafc95 100644 --- a/mpq/mul.c +++ b/mpq/mul.c @@ -61,7 +61,7 @@ mpq_mul (mpq_ptr prod, mpq_srcptr op1, mpq_srcptr op2) /* We special case this to simplify allocation logic; gcd(0,x) = x is a singular case for the allocations. */ SIZ(NUM(prod)) = 0; - PTR(DEN(prod))[0] = 1; + MPZ_NEWALLOC (DEN(prod), 1)[0] = 1; SIZ(DEN(prod)) = 1; return; } diff --git a/mpq/set_d.c b/mpq/set_d.c index 3c02b876d..9d900d8d6 100644 --- a/mpq/set_d.c +++ b/mpq/set_d.c @@ -76,7 +76,7 @@ mpq_set_d (mpq_ptr dest, double d) { SIZ(NUM(dest)) = 0; SIZ(DEN(dest)) = 1; - PTR(DEN(dest))[0] = 1; + MPZ_NEWALLOC (DEN(dest), 1)[0] = 1; return; } @@ -157,7 +157,7 @@ mpq_set_d (mpq_ptr dest, double d) break; #endif } - *PTR(DEN(dest)) = 1; + MPZ_NEWALLOC (DEN(dest), 1)[0] = 1; SIZ(DEN(dest)) = 1; } SIZ(NUM(dest)) = negative ? -nn : nn; diff --git a/mpq/set_f.c b/mpq/set_f.c index a84d2d08b..14d4d0537 100644 --- a/mpq/set_f.c +++ b/mpq/set_f.c @@ -46,7 +46,7 @@ mpq_set_f (mpq_ptr q, mpf_srcptr f) /* set q=0 */ SIZ(NUM(q)) = 0; SIZ(DEN(q)) = 1; - PTR(DEN(q))[0] = 1; + MPZ_NEWALLOC (DEN(q), 1)[0] = 1; return; } @@ -65,7 +65,7 @@ mpq_set_f (mpq_ptr q, mpf_srcptr f) SIZ(NUM(q)) = fsize >= 0 ? fexp : -fexp; SIZ(DEN(q)) = 1; - PTR(DEN(q))[0] = 1; + MPZ_NEWALLOC (DEN(q), 1)[0] = 1; } else { diff --git a/mpq/set_si.c b/mpq/set_si.c index ea7e5c15b..0a4514463 100644 --- a/mpq/set_si.c +++ b/mpq/set_si.c @@ -59,6 +59,6 @@ mpq_set_si (mpq_t dest, signed long int num, unsigned long int den) SIZ(NUM(dest)) = num > 0 ? 1 : -1; } - PTR(DEN(dest))[0] = den; + MPZ_NEWALLOC (DEN(dest), 1)[0] = den; SIZ(DEN(dest)) = (den != 0); } diff --git a/mpq/set_str.c b/mpq/set_str.c index 0e0e75299..a176c251e 100644 --- a/mpq/set_str.c +++ b/mpq/set_str.c @@ -49,7 +49,7 @@ mpq_set_str (mpq_ptr q, const char *str, int base) if (slash == NULL) { SIZ(DEN(q)) = 1; - PTR(DEN(q))[0] = 1; + MPZ_NEWALLOC (DEN(q), 1)[0] = 1; return mpz_set_str (mpq_numref(q), str, base); } diff --git a/mpq/set_ui.c b/mpq/set_ui.c index 32c23dcdf..d977fed84 100644 --- a/mpq/set_ui.c +++ b/mpq/set_ui.c @@ -55,6 +55,6 @@ mpq_set_ui (mpq_t dest, unsigned long int num, unsigned long int den) SIZ(NUM(dest)) = 1; } - PTR(DEN(dest))[0] = den; + MPZ_NEWALLOC (DEN(dest), 1)[0] = den; SIZ(DEN(dest)) = (den != 0); } diff --git a/mpq/set_z.c b/mpq/set_z.c index af00056c1..e60174cdd 100644 --- a/mpq/set_z.c +++ b/mpq/set_z.c @@ -43,6 +43,6 @@ mpq_set_z (mpq_ptr dest, mpz_srcptr src) dp = MPZ_NEWALLOC (NUM(dest), abs_num_size); MPN_COPY (dp, PTR(src), abs_num_size); - PTR(DEN(dest))[0] = 1; + MPZ_NEWALLOC (DEN(dest), 1)[0] = 1; SIZ(DEN(dest)) = 1; } -- cgit v1.2.1