set_prc.c
Upload User: qaz666999
Upload Date: 2022-08-06
Package Size: 2570k
Code Size: 2k
Category:

Algorithm

Development Platform:

Unix_Linux

  1. /* mpf_set_prec(x) -- Change the precision of x.
  2. Copyright 1993, 1994, 1995, 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of the GNU MP Library.
  4. The GNU MP Library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at your
  7. option) any later version.
  8. The GNU MP Library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
  14. #include "gmp.h"
  15. #include "gmp-impl.h"
  16. /* A full new_prec+1 limbs are always retained, even though just new_prec
  17.    would satisfy the requested precision.  If size==new_prec+1 then
  18.    certainly new_prec+1 should be kept since no copying is needed in that
  19.    case.  If just new_prec was kept for size>new_prec+1 it'd be a bit
  20.    inconsistent.  */
  21. void
  22. mpf_set_prec (mpf_ptr x, mp_bitcnt_t new_prec_in_bits)
  23. {
  24.   mp_size_t  old_prec, new_prec, new_prec_plus1;
  25.   mp_size_t  size, sign;
  26.   mp_ptr     xp;
  27.   new_prec = __GMPF_BITS_TO_PREC (new_prec_in_bits);
  28.   old_prec = PREC(x);
  29.   /* do nothing if already the right precision */
  30.   if (new_prec == old_prec)
  31.     return;
  32.   PREC(x) = new_prec;
  33.   new_prec_plus1 = new_prec + 1;
  34.   /* retain most significant limbs */
  35.   sign = SIZ(x);
  36.   size = ABS (sign);
  37.   xp = PTR(x);
  38.   if (size > new_prec_plus1)
  39.     {
  40.       SIZ(x) = (sign >= 0 ? new_prec_plus1 : -new_prec_plus1);
  41.       MPN_COPY_INCR (xp, xp + size - new_prec_plus1, new_prec_plus1);
  42.     }
  43.   PTR(x) = __GMP_REALLOCATE_FUNC_LIMBS (xp, old_prec+1, new_prec_plus1);
  44. }