source: trunk/third/gmp/mpf/set_prc.c @ 18191

Revision 18191, 1.9 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
1/* mpf_set_prec(x) -- Change the precision of x.
2
3Copyright 1993, 1994, 1995, 2000, 2001 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 2.1 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20MA 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
32void
33mpf_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}
Note: See TracBrowser for help on using the repository browser.