summaryrefslogtreecommitdiff
path: root/mpq
diff options
context:
space:
mode:
authorMarco Bodrato <bodrato@mail.dm.unipi.it>2014-08-14 08:54:41 +0200
committerMarco Bodrato <bodrato@mail.dm.unipi.it>2014-08-14 08:54:41 +0200
commit0bee0d814557da70edc78fa606a9a2302d7e7ff4 (patch)
tree66726ded54c9756749e24656072f568100c5eba0 /mpq
parentc0acb7d60acb3bbd71aadf24d283bc1a3f8390a1 (diff)
downloadgmp-0bee0d814557da70edc78fa606a9a2302d7e7ff4.tar.gz
mpq/set_d.c: Stricter allocation.
Diffstat (limited to 'mpq')
-rw-r--r--mpq/set_d.c21
1 files changed, 10 insertions, 11 deletions
diff --git a/mpq/set_d.c b/mpq/set_d.c
index 308677db4..bc7921871 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, 2012 Free Software Foundation, Inc.
+Copyright 2000, 2002, 2003, 2012, 2014 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
@@ -81,9 +81,8 @@ mpq_set_d (mpq_ptr dest, double d)
return;
}
- dn = -exp;
- np = MPZ_NEWALLOC (NUM(dest), 3);
#if LIMBS_PER_DOUBLE == 4
+ np = MPZ_NEWALLOC (NUM(dest), 4);
if ((tp[0] | tp[1] | tp[2]) == 0)
np[0] = tp[3], nn = 1;
else if ((tp[0] | tp[1]) == 0)
@@ -94,6 +93,7 @@ mpq_set_d (mpq_ptr dest, double d)
np[3] = tp[3], np[2] = tp[2], np[1] = tp[1], np[0] = tp[0], nn = 4;
#endif
#if LIMBS_PER_DOUBLE == 3
+ np = MPZ_NEWALLOC (NUM(dest), 3);
if ((tp[0] | tp[1]) == 0)
np[0] = tp[2], nn = 1;
else if (tp[0] == 0)
@@ -102,13 +102,14 @@ mpq_set_d (mpq_ptr dest, double d)
np[2] = tp[2], np[1] = tp[1], np[0] = tp[0], nn = 3;
#endif
#if LIMBS_PER_DOUBLE == 2
+ np = MPZ_NEWALLOC (NUM(dest), 2);
if (tp[0] == 0)
np[0] = tp[1], nn = 1;
else
np[1] = tp[1], np[0] = tp[0], nn = 2;
#endif
- dn += nn + 1;
- ASSERT_ALWAYS (dn > 0);
+ dn = nn + 1 - exp;
+ ASSERT (dn > 0); /* -exp >= -1; nn >= 1*/
dp = MPZ_NEWALLOC (DEN(dest), dn);
MPN_ZERO (dp, dn - 1);
dp[dn - 1] = 1;
@@ -117,11 +118,10 @@ mpq_set_d (mpq_ptr dest, double d)
{
mpn_rshift (np, np, nn, c);
nn -= np[nn - 1] == 0;
- mpn_rshift (dp, dp, dn, c);
- dn -= dp[dn - 1] == 0;
+ --dn;
+ dp[dn - 1] = CNST_LIMB(1) << (GMP_LIMB_BITS - c);
}
SIZ(DEN(dest)) = dn;
- SIZ(NUM(dest)) = negative ? -nn : nn;
}
else
{
@@ -158,9 +158,8 @@ mpq_set_d (mpq_ptr dest, double d)
break;
#endif
}
- dp = PTR(DEN(dest));
- dp[0] = 1;
+ *PTR(DEN(dest)) = 1;
SIZ(DEN(dest)) = 1;
- SIZ(NUM(dest)) = negative ? -nn : nn;
}
+ SIZ(NUM(dest)) = negative ? -nn : nn;
}