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