Ciao,
Il Gio, 19 Aprile 2018 4:37 pm, Marc Glisse ha scritto:
> I finally pushed it. It seemed unsafe to keep mpq unaware of lazy
> allocation, in case people start swapping the numerator of a rational with
> a lazy 0 integer or something like that.
If we fear swaps, then ... what about swapping the deoniminator?
Should we support a code like:
mpz_init (saved_denominator);
mpz_swap (saved_denominator, mpq_denref(rational));
/* Code overwriting rational*/
?
If yes, a larger patch is needed, I attach a proposal.
Ĝis,
m
--
http://bodrato.it/
diff -r 6e5b01dd940f mpq/clear.c
--- a/mpq/clear.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/clear.c Fri Apr 20 07:59:01 2018 +0200
@@ -35,5 +35,6 @@
{
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 -r 6e5b01dd940f mpq/clears.c
--- a/mpq/clears.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/clears.c Fri Apr 20 07:59:01 2018 +0200
@@ -42,7 +42,8 @@
{
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 -r 6e5b01dd940f mpq/div.c
--- a/mpq/div.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/div.c Fri Apr 20 07:59:01 2018 +0200
@@ -48,12 +48,9 @@
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 @@
/* 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 -r 6e5b01dd940f mpq/inp_str.c
--- a/mpq/inp_str.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/inp_str.c Fri Apr 20 07:59:01 2018 +0200
@@ -43,7 +43,7 @@
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 -r 6e5b01dd940f mpq/md_2exp.c
--- a/mpq/md_2exp.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/md_2exp.c Fri Apr 20 07:59:01 2018 +0200
@@ -102,7 +102,7 @@
{
SIZ(NUM(dst)) = 0;
SIZ(DEN(dst)) = 1;
- PTR(DEN(dst))[0] = 1;
+ MPZ_NEWALLOC (DEN(dst), 1)[0] = 1;
return;
}
diff -r 6e5b01dd940f mpq/mul.c
--- a/mpq/mul.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/mul.c Fri Apr 20 07:59:01 2018 +0200
@@ -61,7 +61,7 @@
/* 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 -r 6e5b01dd940f mpq/set_d.c
--- a/mpq/set_d.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/set_d.c Fri Apr 20 07:59:01 2018 +0200
@@ -76,7 +76,7 @@
{
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 @@
break;
#endif
}
- *PTR(DEN(dest)) = 1;
+ MPZ_NEWALLOC (DEN(dest), 1)[0] = 1;
SIZ(DEN(dest)) = 1;
}
SIZ(NUM(dest)) = negative ? -nn : nn;
diff -r 6e5b01dd940f mpq/set_f.c
--- a/mpq/set_f.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/set_f.c Fri Apr 20 07:59:01 2018 +0200
@@ -46,7 +46,7 @@
/* 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 @@
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 -r 6e5b01dd940f mpq/set_si.c
--- a/mpq/set_si.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/set_si.c Fri Apr 20 07:59:01 2018 +0200
@@ -59,6 +59,6 @@
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 -r 6e5b01dd940f mpq/set_str.c
--- a/mpq/set_str.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/set_str.c Fri Apr 20 07:59:01 2018 +0200
@@ -49,7 +49,7 @@
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 -r 6e5b01dd940f mpq/set_ui.c
--- a/mpq/set_ui.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/set_ui.c Fri Apr 20 07:59:01 2018 +0200
@@ -55,6 +55,6 @@
SIZ(NUM(dest)) = 1;
}
- PTR(DEN(dest))[0] = den;
+ MPZ_NEWALLOC (DEN(dest), 1)[0] = den;
SIZ(DEN(dest)) = (den != 0);
}
diff -r 6e5b01dd940f mpq/set_z.c
--- a/mpq/set_z.c Wed Apr 18 23:28:26 2018 +0200
+++ b/mpq/set_z.c Fri Apr 20 07:59:01 2018 +0200
@@ -43,6 +43,6 @@
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;
}
_______________________________________________
gmp-devel mailing list
gmp-devel@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-devel