source: trunk/third/gmp/mpf/ceilfloor.c @ 22254

Revision 22254, 2.8 KB checked in by ghudson, 19 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r22253, which included commits to RCS files with non-trunk default branches.
Line 
1/* mpf_ceil, mpf_floor -- round an mpf to an integer.
2
3Copyright 2001, 2004 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/* dir==1 for ceil, dir==-1 for floor
27
28   Notice the use of prec+1 ensures mpf_ceil and mpf_floor are equivalent to
29   mpf_set if u is already an integer.  */
30
31static void __gmpf_ceil_or_floor _PROTO ((REGPARM_2_1 (mpf_ptr r, mpf_srcptr u, int dir))) REGPARM_ATTR (1);
32#define mpf_ceil_or_floor(r,u,dir)  __gmpf_ceil_or_floor (REGPARM_2_1 (r, u, dir))
33
34REGPARM_ATTR (1) static void
35mpf_ceil_or_floor (mpf_ptr r, mpf_srcptr u, int dir)
36{
37  mp_ptr     rp, up, p;
38  mp_size_t  size, asize, prec;
39  mp_exp_t   exp;
40
41  size = SIZ(u);
42  if (size == 0)
43    {
44    zero:
45      SIZ(r) = 0;
46      EXP(r) = 0;
47      return;
48    }
49
50  rp = PTR(r);
51  exp = EXP(u);
52  if (exp <= 0)
53    {
54      /* u is only a fraction */
55      if ((size ^ dir) < 0)
56        goto zero;
57      rp[0] = 1;
58      EXP(r) = 1;
59      SIZ(r) = dir;
60      return;
61    }
62  EXP(r) = exp;
63
64  up = PTR(u);
65  asize = ABS (size);
66  up += asize;
67
68  /* skip fraction part of u */
69  asize = MIN (asize, exp);
70
71  /* don't lose precision in the copy */
72  prec = PREC (r) + 1;
73
74  /* skip excess over target precision */
75  asize = MIN (asize, prec);
76
77  up -= asize;
78
79  if ((size ^ dir) >= 0)
80    {
81      /* rounding direction matches sign, must increment if ignored part is
82         non-zero */
83      for (p = PTR(u); p != up; p++)
84        {
85          if (*p != 0)
86            {
87              if (mpn_add_1 (rp, up, asize, CNST_LIMB(1)))
88                {
89                  /* was all 0xFF..FFs, which have become zeros, giving just
90                     a carry */
91                  rp[0] = 1;
92                  asize = 1;
93                  EXP(r)++;
94                }
95              SIZ(r) = (size >= 0 ? asize : -asize);
96              return;
97            }
98        }
99    }
100
101  SIZ(r) = (size >= 0 ? asize : -asize);
102  if (rp != up)
103    MPN_COPY_INCR (rp, up, asize);
104}
105
106
107void
108mpf_ceil (mpf_ptr r, mpf_srcptr u)
109{
110  mpf_ceil_or_floor (r, u, 1);
111}
112
113void
114mpf_floor (mpf_ptr r, mpf_srcptr u)
115{
116  mpf_ceil_or_floor (r, u, -1);
117}
Note: See TracBrowser for help on using the repository browser.