1 /* mpf_ceil, mpf_floor -- round an mpf to an integer.
3 Copyright 2001, 2004, 2012 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
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.
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
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
35 /* dir==1 for ceil, dir==-1 for floor
37 Notice the use of prec+1 ensures mpf_ceil and mpf_floor are equivalent to
38 mpf_set if u is already an integer. */
40 static void __gmpf_ceil_or_floor (REGPARM_2_1 (mpf_ptr
, mpf_srcptr
, int)) REGPARM_ATTR (1);
41 #define mpf_ceil_or_floor(r,u,dir) __gmpf_ceil_or_floor (REGPARM_2_1 (r, u, dir))
43 REGPARM_ATTR (1) static void
44 mpf_ceil_or_floor (mpf_ptr r
, mpf_srcptr u
, int dir
)
47 mp_size_t size
, asize
, prec
;
63 /* u is only a fraction */
77 /* skip fraction part of u */
78 asize
= MIN (asize
, exp
);
80 /* don't lose precision in the copy */
83 /* skip excess over target precision */
84 asize
= MIN (asize
, prec
);
88 if ((size
^ dir
) >= 0)
90 /* rounding direction matches sign, must increment if ignored part is
92 for (p
= PTR(u
); p
!= up
; p
++)
96 if (mpn_add_1 (rp
, up
, asize
, CNST_LIMB(1)))
98 /* was all 0xFF..FFs, which have become zeros, giving just
104 SIZ(r
) = (size
>= 0 ? asize
: -asize
);
110 SIZ(r
) = (size
>= 0 ? asize
: -asize
);
112 MPN_COPY_INCR (rp
, up
, asize
);
117 mpf_ceil (mpf_ptr r
, mpf_srcptr u
)
119 mpf_ceil_or_floor (r
, u
, 1);
123 mpf_floor (mpf_ptr r
, mpf_srcptr u
)
125 mpf_ceil_or_floor (r
, u
, -1);