|
Packit |
5c3484 |
/* mpf_div_2exp -- Divide a float by 2^n.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Copyright 1993, 1994, 1996, 2000-2002, 2004 Free Software Foundation, Inc.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
This file is part of the GNU MP Library.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library is free software; you can redistribute it and/or modify
|
|
Packit |
5c3484 |
it under the terms of either:
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
* the GNU Lesser General Public License as published by the Free
|
|
Packit |
5c3484 |
Software Foundation; either version 3 of the License, or (at your
|
|
Packit |
5c3484 |
option) any later version.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
or
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
* the GNU General Public License as published by the Free Software
|
|
Packit |
5c3484 |
Foundation; either version 2 of the License, or (at your option) any
|
|
Packit |
5c3484 |
later version.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
or both in parallel, as here.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library is distributed in the hope that it will be useful, but
|
|
Packit |
5c3484 |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
Packit |
5c3484 |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
Packit |
5c3484 |
for more details.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
You should have received copies of the GNU General Public License and the
|
|
Packit |
5c3484 |
GNU Lesser General Public License along with the GNU MP Library. If not,
|
|
Packit |
5c3484 |
see https://www.gnu.org/licenses/. */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include "gmp.h"
|
|
Packit |
5c3484 |
#include "gmp-impl.h"
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
/* Multiples of GMP_NUMB_BITS in exp simply mean an amount subtracted from
|
|
Packit |
5c3484 |
EXP(u) to set EXP(r). The remainder exp%GMP_NUMB_BITS is then a right
|
|
Packit |
5c3484 |
shift for the limb data.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
If exp%GMP_NUMB_BITS == 0 then there's no shifting, we effectively just
|
|
Packit |
5c3484 |
do an mpz_set with changed EXP(r). Like mpz_set we take prec+1 limbs in
|
|
Packit |
5c3484 |
this case. Although just prec would suffice, it's nice to have
|
|
Packit |
5c3484 |
mpf_div_2exp with exp==0 come out the same as mpz_set.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
When shifting we take up to prec many limbs from the input. Our shift is
|
|
Packit |
5c3484 |
cy = mpn_rshift (PTR(r)+1, PTR(u)+k, ...), where k is the number of low
|
|
Packit |
5c3484 |
limbs dropped from u, and the carry out is stored to PTR(r)[0]. We don't
|
|
Packit |
5c3484 |
try to work extra bits from PTR(u)[k-1] (when k>=1 makes it available)
|
|
Packit |
5c3484 |
into that low carry limb. Just prec limbs (with the high non-zero) from
|
|
Packit |
5c3484 |
the input is enough bits for the application requested precision, no need
|
|
Packit |
5c3484 |
to do extra work.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
If r==u the shift will have overlapping operands. When k>=1 (ie. when
|
|
Packit |
5c3484 |
usize > prec), the overlap is in the style supported by rshift (ie. dst
|
|
Packit |
5c3484 |
<= src).
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
But when r==u and k==0 (ie. usize <= prec), we would have an invalid
|
|
Packit |
5c3484 |
overlap (mpn_rshift (rp+1, rp, ...)). In this case we must instead use
|
|
Packit |
5c3484 |
mpn_lshift (PTR(r), PTR(u), size, NUMB-shift). An lshift by NUMB-shift
|
|
Packit |
5c3484 |
bits gives identical data of course, it's just its overlap restrictions
|
|
Packit |
5c3484 |
which differ.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
In both shift cases, the resulting data is abs_usize+1 limbs. "adj" is
|
|
Packit |
5c3484 |
used to add +1 to that size if the high is non-zero (it may of course
|
|
Packit |
5c3484 |
have become zero by the shifting). EXP(u) is the exponent just above
|
|
Packit |
5c3484 |
those abs_usize+1 limbs, so it gets -1+adj, which means -1 if the high is
|
|
Packit |
5c3484 |
zero, or no change if the high is non-zero.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Enhancements:
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The way mpn_lshift is used means successive mpf_div_2exp calls on the
|
|
Packit |
5c3484 |
same operand will accumulate low zero limbs, until prec+1 limbs is
|
|
Packit |
5c3484 |
reached. This is wasteful for subsequent operations. When abs_usize <=
|
|
Packit |
5c3484 |
prec, we should test the low exp%GMP_NUMB_BITS many bits of PTR(u)[0],
|
|
Packit |
5c3484 |
ie. those which would be shifted out by an mpn_rshift. If they're zero
|
|
Packit |
5c3484 |
then use that mpn_rshift. */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
mpf_div_2exp (mpf_ptr r, mpf_srcptr u, mp_bitcnt_t exp)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mp_srcptr up;
|
|
Packit |
5c3484 |
mp_ptr rp = r->_mp_d;
|
|
Packit |
5c3484 |
mp_size_t usize;
|
|
Packit |
5c3484 |
mp_size_t abs_usize;
|
|
Packit |
5c3484 |
mp_size_t prec = r->_mp_prec;
|
|
Packit |
5c3484 |
mp_exp_t uexp = u->_mp_exp;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
usize = u->_mp_size;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (UNLIKELY (usize == 0))
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
r->_mp_size = 0;
|
|
Packit |
5c3484 |
r->_mp_exp = 0;
|
|
Packit |
5c3484 |
return;
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
abs_usize = ABS (usize);
|
|
Packit |
5c3484 |
up = u->_mp_d;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
if (exp % GMP_NUMB_BITS == 0)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
prec++; /* retain more precision here as we don't need
|
|
Packit |
5c3484 |
to account for carry-out here */
|
|
Packit |
5c3484 |
if (abs_usize > prec)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
up += abs_usize - prec;
|
|
Packit |
5c3484 |
abs_usize = prec;
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
if (rp != up)
|
|
Packit |
5c3484 |
MPN_COPY_INCR (rp, up, abs_usize);
|
|
Packit |
5c3484 |
r->_mp_exp = uexp - exp / GMP_NUMB_BITS;
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
else
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mp_limb_t cy_limb;
|
|
Packit |
5c3484 |
mp_size_t adj;
|
|
Packit |
5c3484 |
if (abs_usize > prec)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
up += abs_usize - prec;
|
|
Packit |
5c3484 |
abs_usize = prec;
|
|
Packit |
5c3484 |
/* Use mpn_rshift since mpn_lshift operates downwards, and we
|
|
Packit |
5c3484 |
therefore would clobber part of U before using that part, in case
|
|
Packit |
5c3484 |
R is the same variable as U. */
|
|
Packit |
5c3484 |
cy_limb = mpn_rshift (rp + 1, up, abs_usize, exp % GMP_NUMB_BITS);
|
|
Packit |
5c3484 |
rp[0] = cy_limb;
|
|
Packit |
5c3484 |
adj = rp[abs_usize] != 0;
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
else
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
cy_limb = mpn_lshift (rp, up, abs_usize,
|
|
Packit |
5c3484 |
GMP_NUMB_BITS - exp % GMP_NUMB_BITS);
|
|
Packit |
5c3484 |
rp[abs_usize] = cy_limb;
|
|
Packit |
5c3484 |
adj = cy_limb != 0;
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
abs_usize += adj;
|
|
Packit |
5c3484 |
r->_mp_exp = uexp - exp / GMP_NUMB_BITS - 1 + adj;
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
r->_mp_size = usize >= 0 ? abs_usize : -abs_usize;
|
|
Packit |
5c3484 |
}
|