blob: 9bb663866f84d8a85b398b3957d214ef0053c9e7 [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/* mpf_ceil, mpf_floor -- round an mpf to an integer.
2
3Copyright 2001, 2004, 2012 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 either:
9
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13
14or
15
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
19
20or both in parallel, as here.
21
22The GNU MP Library is distributed in the hope that it will be useful, but
23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25for more details.
26
27You should have received copies of the GNU General Public License and the
28GNU Lesser General Public License along with the GNU MP Library. If not,
29see https://www.gnu.org/licenses/. */
30
31#include "gmp-impl.h"
32
33
34/* dir==1 for ceil, dir==-1 for floor
35
36 Notice the use of prec+1 ensures mpf_ceil and mpf_floor are equivalent to
37 mpf_set if u is already an integer. */
38
39static void __gmpf_ceil_or_floor (REGPARM_2_1 (mpf_ptr, mpf_srcptr, int)) REGPARM_ATTR (1);
40#define mpf_ceil_or_floor(r,u,dir) __gmpf_ceil_or_floor (REGPARM_2_1 (r, u, dir))
41
42REGPARM_ATTR (1) static void
43mpf_ceil_or_floor (mpf_ptr r, mpf_srcptr u, int dir)
44{
45 mp_ptr rp, up, p;
46 mp_size_t size, asize, prec;
47 mp_exp_t exp;
48
49 size = SIZ(u);
50 if (size == 0)
51 {
52 zero:
53 SIZ(r) = 0;
54 EXP(r) = 0;
55 return;
56 }
57
58 rp = PTR(r);
59 exp = EXP(u);
60 if (exp <= 0)
61 {
62 /* u is only a fraction */
63 if ((size ^ dir) < 0)
64 goto zero;
65 rp[0] = 1;
66 EXP(r) = 1;
67 SIZ(r) = dir;
68 return;
69 }
70 EXP(r) = exp;
71
72 up = PTR(u);
73 asize = ABS (size);
74 up += asize;
75
76 /* skip fraction part of u */
77 asize = MIN (asize, exp);
78
79 /* don't lose precision in the copy */
80 prec = PREC (r) + 1;
81
82 /* skip excess over target precision */
83 asize = MIN (asize, prec);
84
85 up -= asize;
86
87 if ((size ^ dir) >= 0)
88 {
89 /* rounding direction matches sign, must increment if ignored part is
90 non-zero */
91 for (p = PTR(u); p != up; p++)
92 {
93 if (*p != 0)
94 {
95 if (mpn_add_1 (rp, up, asize, CNST_LIMB(1)))
96 {
97 /* was all 0xFF..FFs, which have become zeros, giving just
98 a carry */
99 rp[0] = 1;
100 asize = 1;
101 EXP(r)++;
102 }
103 SIZ(r) = (size >= 0 ? asize : -asize);
104 return;
105 }
106 }
107 }
108
109 SIZ(r) = (size >= 0 ? asize : -asize);
110 if (rp != up)
111 MPN_COPY_INCR (rp, up, asize);
112}
113
114
115void
116mpf_ceil (mpf_ptr r, mpf_srcptr u)
117{
118 mpf_ceil_or_floor (r, u, 1);
119}
120
121void
122mpf_floor (mpf_ptr r, mpf_srcptr u)
123{
124 mpf_ceil_or_floor (r, u, -1);
125}