1 /* mpz_cdiv_r_2exp, mpz_fdiv_r_2exp -- remainder from mpz divided by 2^n.
3 Copyright 2001, 2002, 2004 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 the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
24 /* Bit mask of "n" least significant bits of a limb. */
25 #define LOW_MASK(n) ((CNST_LIMB(1) << (n)) - 1)
28 /* dir==1 for ceil, dir==-1 for floor */
30 static void __gmpz_cfdiv_r_2exp
__GMP_PROTO ((REGPARM_3_1 (mpz_ptr w
, mpz_srcptr u
, unsigned long cnt
, int dir
))) REGPARM_ATTR (1);
31 #define cfdiv_r_2exp(w,u,cnt,dir) __gmpz_cfdiv_r_2exp (REGPARM_3_1 (w, u, cnt, dir))
33 REGPARM_ATTR (1) static void
34 cfdiv_r_2exp (mpz_ptr w
, mpz_srcptr u
, unsigned long cnt
, int dir
)
36 mp_size_t usize
, abs_usize
, limb_cnt
, i
;
48 limb_cnt
= cnt
/ GMP_NUMB_BITS
;
50 abs_usize
= ABS (usize
);
52 /* MPZ_REALLOC(w) below is only when w!=u, so we can fetch PTR(u) here
56 if ((usize
^ dir
) < 0)
58 /* Round towards zero, means just truncate */
62 /* if already smaller than limb_cnt then do nothing */
63 if (abs_usize
<= limb_cnt
)
69 i
= MIN (abs_usize
, limb_cnt
+1);
74 /* if smaller than limb_cnt then only the copy is needed */
75 if (abs_usize
<= limb_cnt
)
84 /* Round away from zero, means twos complement if non-zero */
86 /* if u!=0 and smaller than divisor, then must negate */
87 if (abs_usize
<= limb_cnt
)
90 /* if non-zero low limb, then must negate */
91 for (i
= 0; i
< limb_cnt
; i
++)
95 /* if non-zero partial limb, then must negate */
96 if ((up
[limb_cnt
] & LOW_MASK (cnt
)) != 0)
99 /* otherwise low bits of u are zero, so that's the result */
104 /* twos complement negation to get 2**cnt-u */
106 MPZ_REALLOC (w
, limb_cnt
+1);
110 /* Ones complement */
111 i
= MIN (abs_usize
, limb_cnt
+1);
112 mpn_com_n (wp
, up
, i
);
113 for ( ; i
<= limb_cnt
; i
++)
114 wp
[i
] = GMP_NUMB_MAX
;
116 /* Twos complement. Since u!=0 in the relevant part, the twos
117 complement never gives 0 and a carry, so can use MPN_INCR_U. */
118 MPN_INCR_U (wp
, limb_cnt
+1, CNST_LIMB(1));
123 /* Mask the high limb */
125 high
&= LOW_MASK (cnt
);
128 /* Strip any consequent high zeros */
141 SIZ(w
) = (usize
>= 0 ? limb_cnt
: -limb_cnt
);
146 mpz_cdiv_r_2exp (mpz_ptr w
, mpz_srcptr u
, unsigned long cnt
)
148 cfdiv_r_2exp (w
, u
, cnt
, 1);
152 mpz_fdiv_r_2exp (mpz_ptr w
, mpz_srcptr u
, unsigned long cnt
)
154 cfdiv_r_2exp (w
, u
, cnt
, -1);