summaryrefslogtreecommitdiff
path: root/mpf/ceilfloor.c
blob: 9bb663866f84d8a85b398b3957d214ef0053c9e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* mpf_ceil, mpf_floor -- round an mpf to an integer.

Copyright 2001, 2004, 2012 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */

#include "gmp-impl.h"


/* dir==1 for ceil, dir==-1 for floor

   Notice the use of prec+1 ensures mpf_ceil and mpf_floor are equivalent to
   mpf_set if u is already an integer.  */

static void __gmpf_ceil_or_floor (REGPARM_2_1 (mpf_ptr, mpf_srcptr, int)) REGPARM_ATTR (1);
#define mpf_ceil_or_floor(r,u,dir)  __gmpf_ceil_or_floor (REGPARM_2_1 (r, u, dir))

REGPARM_ATTR (1) static void
mpf_ceil_or_floor (mpf_ptr r, mpf_srcptr u, int dir)
{
  mp_ptr     rp, up, p;
  mp_size_t  size, asize, prec;
  mp_exp_t   exp;

  size = SIZ(u);
  if (size == 0)
    {
    zero:
      SIZ(r) = 0;
      EXP(r) = 0;
      return;
    }

  rp = PTR(r);
  exp = EXP(u);
  if (exp <= 0)
    {
      /* u is only a fraction */
      if ((size ^ dir) < 0)
        goto zero;
      rp[0] = 1;
      EXP(r) = 1;
      SIZ(r) = dir;
      return;
    }
  EXP(r) = exp;

  up = PTR(u);
  asize = ABS (size);
  up += asize;

  /* skip fraction part of u */
  asize = MIN (asize, exp);

  /* don't lose precision in the copy */
  prec = PREC (r) + 1;

  /* skip excess over target precision */
  asize = MIN (asize, prec);

  up -= asize;

  if ((size ^ dir) >= 0)
    {
      /* rounding direction matches sign, must increment if ignored part is
         non-zero */
      for (p = PTR(u); p != up; p++)
        {
          if (*p != 0)
            {
              if (mpn_add_1 (rp, up, asize, CNST_LIMB(1)))
                {
                  /* was all 0xFF..FFs, which have become zeros, giving just
                     a carry */
                  rp[0] = 1;
                  asize = 1;
                  EXP(r)++;
                }
              SIZ(r) = (size >= 0 ? asize : -asize);
              return;
            }
        }
    }

  SIZ(r) = (size >= 0 ? asize : -asize);
  if (rp != up)
    MPN_COPY_INCR (rp, up, asize);
}


void
mpf_ceil (mpf_ptr r, mpf_srcptr u)
{
  mpf_ceil_or_floor (r, u, 1);
}

void
mpf_floor (mpf_ptr r, mpf_srcptr u)
{
  mpf_ceil_or_floor (r, u, -1);
}