diff options
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | mpq/get_d.c | 36 |
2 files changed, 15 insertions, 22 deletions
@@ -3,6 +3,7 @@ * tune/Makefile.am: Disallow parallel make (thanks Vincent Lefevre). * mpq/swap.c: Use *_SWAP_* macros. * mpq/cmp_ui.c: One more little shortcut, comparing fractions to 1. + * mpq/get_d.c: compare (zeros > 0) just once. 2018-02-09 Torbjörn Granlund <tg@gmplib.org> diff --git a/mpq/get_d.c b/mpq/get_d.c index 9d17ad1d5..86de3453c 100644 --- a/mpq/get_d.c +++ b/mpq/get_d.c @@ -104,10 +104,10 @@ mpq_get_d (mpq_srcptr src) { double res; mp_srcptr np, dp; - mp_ptr remp, tp; + mp_ptr remp; mp_size_t nsize = SIZ(NUM(src)); mp_size_t dsize = SIZ(DEN(src)); - mp_size_t qsize, prospective_qsize, zeros, chop, tsize; + mp_size_t qsize, prospective_qsize, zeros; mp_size_t sign_quotient = nsize; long exp; #define N_QLIMBS (1 + (sizeof (double) + GMP_LIMB_BYTES-1) / GMP_LIMB_BYTES) @@ -133,34 +133,26 @@ mpq_get_d (mpq_srcptr src) zeros = qsize - prospective_qsize; /* padding n to get qsize */ exp = (long) -zeros * GMP_NUMB_BITS; /* relative to low of qp */ - chop = MAX (-zeros, 0); /* negative zeros means shorten n */ - np += chop; - nsize -= chop; - zeros += chop; /* now zeros >= 0 */ - - tsize = nsize + zeros; /* size for possible copy of n */ - - if (WANT_TMP_DEBUG) - { - /* separate blocks, for malloc debugging */ - remp = TMP_ALLOC_LIMBS (dsize); - tp = (zeros > 0 ? TMP_ALLOC_LIMBS (tsize) : NULL); - } - else - { - /* one block with conditionalized size, for efficiency */ - remp = TMP_ALLOC_LIMBS (dsize + (zeros > 0 ? tsize : 0)); - tp = remp + dsize; - } - /* zero extend n into temporary space, if necessary */ if (zeros > 0) { + mp_ptr tp; + mp_size_t tsize; + tsize = nsize + zeros; /* size for copy of n */ + + TMP_ALLOC_LIMBS_2 (remp, dsize, tp, tsize); MPN_ZERO (tp, zeros); MPN_COPY (tp+zeros, np, nsize); np = tp; nsize = tsize; } + else /* negative zeros means shorten n */ + { + np -= zeros; + nsize += zeros; + + remp = TMP_ALLOC_LIMBS (dsize); + } ASSERT (qsize == nsize - dsize + 1); mpn_tdiv_qr (qp, remp, (mp_size_t) 0, np, nsize, dp, dsize); |