summaryrefslogtreecommitdiff
path: root/mpq
diff options
context:
space:
mode:
authortege <tege@gmplib.org>2000-06-17 18:39:38 +0200
committertege <tege@gmplib.org>2000-06-17 18:39:38 +0200
commit8fc124812a4aa55863e45ec48b85f79980264df5 (patch)
tree5812484e4cdac4273bc8a8a831db2ee083044d94 /mpq
parent7b5d15e6ddea88647bc460634d0aff248e8c9b26 (diff)
downloadgmp-8fc124812a4aa55863e45ec48b85f79980264df5.tar.gz
Special case 0.0. Don't call mpn_rshift with 0 count.
Allocate correct amount of memory for numerator. Delete spurious ASSERT_ALWAYS(1).
Diffstat (limited to 'mpq')
-rw-r--r--mpq/set_d.c28
1 files changed, 18 insertions, 10 deletions
diff --git a/mpq/set_d.c b/mpq/set_d.c
index 1f38ac746..37c6452c5 100644
--- a/mpq/set_d.c
+++ b/mpq/set_d.c
@@ -57,6 +57,14 @@ mpq_set_d (dest, d)
if (exp <= 1)
#endif
{
+ if (d == 0.0)
+ {
+ SIZ(&(dest->_mp_num)) = 0;
+ SIZ(&(dest->_mp_den)) = 1;
+ PTR(&(dest->_mp_den))[0] = 1;
+ return;
+ }
+
dn = -exp;
if (dest->_mp_num._mp_alloc < 3)
_mpz_realloc (&(dest->_mp_num), 3);
@@ -75,23 +83,27 @@ mpq_set_d (dest, d)
np[1] = tp[1], np[0] = tp[0], nn = 2;
#endif
dn += nn + 1;
- ASSERT_ALWAYS (dn > 0);
if (dest->_mp_den._mp_alloc < dn)
_mpz_realloc (&(dest->_mp_den), dn);
dp = PTR(&(dest->_mp_den));
MPN_ZERO (dp, dn - 1);
dp[dn - 1] = 1;
+ count_trailing_zeros (c, np[0] | dp[0]);
+ if (c != 0)
+ {
+ mpn_rshift (np, np, nn, c);
+ nn -= np[nn - 1] == 0;
+ mpn_rshift (dp, dp, dn, c);
+ dn -= dp[dn - 1] == 0;
+ }
SIZ(&(dest->_mp_den)) = dn;
SIZ(&(dest->_mp_num)) = negative ? -nn : nn;
- count_trailing_zeros (c, np[0] | dp[0]);
- mpn_rshift (np, np, nn, c);
- mpn_rshift (dp, dp, dn, c);
}
else
{
nn = exp;
- if (dest->_mp_num._mp_alloc < 3)
- _mpz_realloc (&(dest->_mp_num), 3);
+ if (dest->_mp_num._mp_alloc < nn)
+ _mpz_realloc (&(dest->_mp_num), nn);
np = PTR(&(dest->_mp_num));
#if BITS_PER_MP_LIMB == 32
switch (nn)
@@ -106,8 +118,6 @@ mpq_set_d (dest, d)
case 2:
np[1] = tp[2], np[0] = tp[1];
break;
- case 1:
- ASSERT_ALWAYS (1);
}
#else
switch (nn)
@@ -119,8 +129,6 @@ mpq_set_d (dest, d)
case 2:
np[1] = tp[1], np[0] = tp[0];
break;
- case 1:
- ASSERT_ALWAYS (1);
}
#endif
dp = PTR(&(dest->_mp_den));