1 /* mpz_cdiv_r_2exp, mpz_fdiv_r_2exp -- remainder from mpz divided by 2^n.
3 Copyright 2001, 2002, 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 /* Bit mask of "n" least significant bits of a limb. */
36 #define LOW_MASK(n) ((CNST_LIMB(1) << (n)) - 1)
39 /* dir==1 for ceil, dir==-1 for floor */
41 static void __gmpz_cfdiv_r_2exp (REGPARM_3_1 (mpz_ptr
, mpz_srcptr
, mp_bitcnt_t
, int)) REGPARM_ATTR (1);
42 #define cfdiv_r_2exp(w,u,cnt,dir) __gmpz_cfdiv_r_2exp (REGPARM_3_1 (w, u, cnt, dir))
44 REGPARM_ATTR (1) static void
45 cfdiv_r_2exp (mpz_ptr w
, mpz_srcptr u
, mp_bitcnt_t cnt
, int dir
)
47 mp_size_t usize
, abs_usize
, limb_cnt
, i
;
59 limb_cnt
= cnt
/ GMP_NUMB_BITS
;
61 abs_usize
= ABS (usize
);
63 /* MPZ_REALLOC(w) below is only when w!=u, so we can fetch PTR(u) here
67 if ((usize
^ dir
) < 0)
69 /* Round towards zero, means just truncate */
73 /* if already smaller than limb_cnt then do nothing */
74 if (abs_usize
<= limb_cnt
)
80 i
= MIN (abs_usize
, limb_cnt
+1);
81 wp
= MPZ_NEWALLOC (w
, i
);
84 /* if smaller than limb_cnt then only the copy is needed */
85 if (abs_usize
<= limb_cnt
)
94 /* Round away from zero, means twos complement if non-zero */
96 /* if u!=0 and smaller than divisor, then must negate */
97 if (abs_usize
<= limb_cnt
)
100 /* if non-zero low limb, then must negate */
101 for (i
= 0; i
< limb_cnt
; i
++)
105 /* if non-zero partial limb, then must negate */
106 if ((up
[limb_cnt
] & LOW_MASK (cnt
)) != 0)
109 /* otherwise low bits of u are zero, so that's the result */
114 /* twos complement negation to get 2**cnt-u */
116 wp
= MPZ_REALLOC (w
, limb_cnt
+1);
119 /* Ones complement */
120 i
= MIN (abs_usize
, limb_cnt
+1);
121 ASSERT_CARRY (mpn_neg (wp
, up
, i
));
122 for ( ; i
<= limb_cnt
; i
++)
123 wp
[i
] = GMP_NUMB_MAX
;
128 /* Mask the high limb */
130 high
&= LOW_MASK (cnt
);
133 /* Strip any consequent high zeros */
146 SIZ(w
) = (usize
>= 0 ? limb_cnt
: -limb_cnt
);
151 mpz_cdiv_r_2exp (mpz_ptr w
, mpz_srcptr u
, mp_bitcnt_t cnt
)
153 cfdiv_r_2exp (w
, u
, cnt
, 1);
157 mpz_fdiv_r_2exp (mpz_ptr w
, mpz_srcptr u
, mp_bitcnt_t cnt
)
159 cfdiv_r_2exp (w
, u
, cnt
, -1);