Blame mpf/set_prc.c

Packit 5c3484
/* mpf_set_prec(x) -- Change the precision of x.
Packit 5c3484
Packit 5c3484
Copyright 1993-1995, 2000, 2001 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
/* A full new_prec+1 limbs are always retained, even though just new_prec
Packit 5c3484
   would satisfy the requested precision.  If size==new_prec+1 then
Packit 5c3484
   certainly new_prec+1 should be kept since no copying is needed in that
Packit 5c3484
   case.  If just new_prec was kept for size>new_prec+1 it'd be a bit
Packit 5c3484
   inconsistent.  */
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpf_set_prec (mpf_ptr x, mp_bitcnt_t new_prec_in_bits)
Packit 5c3484
{
Packit 5c3484
  mp_size_t  old_prec, new_prec, new_prec_plus1;
Packit 5c3484
  mp_size_t  size, sign;
Packit 5c3484
  mp_ptr     xp;
Packit 5c3484
Packit 5c3484
  new_prec = __GMPF_BITS_TO_PREC (new_prec_in_bits);
Packit 5c3484
  old_prec = PREC(x);
Packit 5c3484
Packit 5c3484
  /* do nothing if already the right precision */
Packit 5c3484
  if (new_prec == old_prec)
Packit 5c3484
    return;
Packit 5c3484
Packit 5c3484
  PREC(x) = new_prec;
Packit 5c3484
  new_prec_plus1 = new_prec + 1;
Packit 5c3484
Packit 5c3484
  /* retain most significant limbs */
Packit 5c3484
  sign = SIZ(x);
Packit 5c3484
  size = ABS (sign);
Packit 5c3484
  xp = PTR(x);
Packit 5c3484
  if (size > new_prec_plus1)
Packit 5c3484
    {
Packit 5c3484
      SIZ(x) = (sign >= 0 ? new_prec_plus1 : -new_prec_plus1);
Packit 5c3484
      MPN_COPY_INCR (xp, xp + size - new_prec_plus1, new_prec_plus1);
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  PTR(x) = __GMP_REALLOCATE_FUNC_LIMBS (xp, old_prec+1, new_prec_plus1);
Packit 5c3484
}