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 2.1 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; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include "gmp.h" |
---|
23 | #include "gmp-impl.h" |
---|
24 | |
---|
25 | |
---|
26 | /* A full new_prec+1 limbs are always retained, even though just new_prec |
---|
27 | would satisfy the requested precision. If size==new_prec+1 then |
---|
28 | certainly new_prec+1 should be kept since no copying is needed in that |
---|
29 | case. If just new_prec was kept for size>new_prec+1 it'd be a bit |
---|
30 | inconsistent. */ |
---|
31 | |
---|
32 | void |
---|
33 | mpf_set_prec (mpf_ptr x, unsigned long int new_prec_in_bits) |
---|
34 | { |
---|
35 | mp_size_t old_prec, new_prec, new_prec_plus1; |
---|
36 | mp_size_t size, sign; |
---|
37 | mp_ptr xp; |
---|
38 | |
---|
39 | new_prec = __GMPF_BITS_TO_PREC (new_prec_in_bits); |
---|
40 | old_prec = PREC(x); |
---|
41 | |
---|
42 | /* do nothing if already the right precision */ |
---|
43 | if (new_prec == old_prec) |
---|
44 | return; |
---|
45 | |
---|
46 | PREC(x) = new_prec; |
---|
47 | new_prec_plus1 = new_prec + 1; |
---|
48 | |
---|
49 | /* retain most significant limbs */ |
---|
50 | sign = SIZ(x); |
---|
51 | size = ABS (sign); |
---|
52 | xp = PTR(x); |
---|
53 | if (size > new_prec_plus1) |
---|
54 | { |
---|
55 | SIZ(x) = (sign >= 0 ? new_prec_plus1 : -new_prec_plus1); |
---|
56 | MPN_COPY_INCR (xp, xp + size - new_prec_plus1, new_prec_plus1); |
---|
57 | } |
---|
58 | |
---|
59 | PTR(x) = __GMP_REALLOCATE_FUNC_LIMBS (xp, old_prec+1, new_prec_plus1); |
---|
60 | } |
---|