beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpf / set_prc.c
blob30ba06c6e657286a01e6d69c54e105c78a803a5b
1 /* mpf_set_prec(x) -- Change the precision of x.
3 Copyright 1993-1995, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 for more details.
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include "gmp.h"
32 #include "gmp-impl.h"
35 /* A full new_prec+1 limbs are always retained, even though just new_prec
36 would satisfy the requested precision. If size==new_prec+1 then
37 certainly new_prec+1 should be kept since no copying is needed in that
38 case. If just new_prec was kept for size>new_prec+1 it'd be a bit
39 inconsistent. */
41 void
42 mpf_set_prec (mpf_ptr x, mp_bitcnt_t new_prec_in_bits)
44 mp_size_t old_prec, new_prec, new_prec_plus1;
45 mp_size_t size, sign;
46 mp_ptr xp;
48 new_prec = __GMPF_BITS_TO_PREC (new_prec_in_bits);
49 old_prec = PREC(x);
51 /* do nothing if already the right precision */
52 if (new_prec == old_prec)
53 return;
55 PREC(x) = new_prec;
56 new_prec_plus1 = new_prec + 1;
58 /* retain most significant limbs */
59 sign = SIZ(x);
60 size = ABS (sign);
61 xp = PTR(x);
62 if (size > new_prec_plus1)
64 SIZ(x) = (sign >= 0 ? new_prec_plus1 : -new_prec_plus1);
65 MPN_COPY_INCR (xp, xp + size - new_prec_plus1, new_prec_plus1);
68 PTR(x) = __GMP_REALLOCATE_FUNC_LIMBS (xp, old_prec+1, new_prec_plus1);