1 /* mpz_cdiv_q_2exp, mpz_fdiv_q_2exp -- quotient from mpz divided by 2^n.
2
3 Copyright 1991, 1993, 1994, 1996, 1998, 1999, 2001, 2002, 2004, 2012,
4 2015 Free Software Foundation, Inc.
5
6 This file is part of the GNU MP Library.
7
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
10
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
14
15 or
16
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
20
21 or both in parallel, as here.
22
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 for more details.
27
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library. If not,
30 see https://www.gnu.org/licenses/. */
31
32 #include "gmp-impl.h"
33
34
35 /* dir==1 for ceil, dir==-1 for floor */
36
37 static void __gmpz_cfdiv_q_2exp (REGPARM_3_1 (mpz_ptr, mpz_srcptr, mp_bitcnt_t, int)) REGPARM_ATTR (1);
38 #define cfdiv_q_2exp(w,u,cnt,dir) __gmpz_cfdiv_q_2exp (REGPARM_3_1 (w,u,cnt,dir))
39
40 REGPARM_ATTR (1) static void
cfdiv_q_2exp(mpz_ptr w,mpz_srcptr u,mp_bitcnt_t cnt,int dir)41 cfdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt, int dir)
42 {
43 mp_size_t wsize, usize, abs_usize, limb_cnt, i;
44 mp_srcptr up;
45 mp_ptr wp;
46 mp_limb_t round, rmask;
47
48 usize = SIZ (u);
49 abs_usize = ABS (usize);
50 limb_cnt = cnt / GMP_NUMB_BITS;
51 wsize = abs_usize - limb_cnt;
52 if (wsize <= 0)
53 {
54 /* u < 2**cnt, so result 1, 0 or -1 according to rounding */
55 MPZ_NEWALLOC (w, 1)[0] = 1;
56 SIZ(w) = (usize == 0 || (usize ^ dir) < 0 ? 0 : dir);
57 return;
58 }
59
60 /* +1 limb to allow for mpn_add_1 below */
61 wp = MPZ_REALLOC (w, wsize+1);
62
63 /* Check for rounding if direction matches u sign.
64 Set round if we're skipping non-zero limbs. */
65 up = PTR(u);
66 round = 0;
67 rmask = ((usize ^ dir) >= 0 ? MP_LIMB_T_MAX : 0);
68 if (rmask != 0)
69 for (i = 0; i < limb_cnt && round == 0; i++)
70 round = up[i];
71
72 cnt %= GMP_NUMB_BITS;
73 if (cnt != 0)
74 {
75 round |= rmask & mpn_rshift (wp, up + limb_cnt, wsize, cnt);
76 wsize -= (wp[wsize - 1] == 0);
77 }
78 else
79 MPN_COPY_INCR (wp, up + limb_cnt, wsize);
80
81 if (round != 0)
82 {
83 if (wsize != 0)
84 {
85 mp_limb_t cy;
86 cy = mpn_add_1 (wp, wp, wsize, CNST_LIMB(1));
87 wp[wsize] = cy;
88 wsize += cy;
89 }
90 else
91 {
92 /* We shifted something to zero. */
93 wp[0] = 1;
94 wsize = 1;
95 }
96 }
97 SIZ(w) = (usize >= 0 ? wsize : -wsize);
98 }
99
100
101 void
mpz_cdiv_q_2exp(mpz_ptr w,mpz_srcptr u,mp_bitcnt_t cnt)102 mpz_cdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
103 {
104 cfdiv_q_2exp (w, u, cnt, 1);
105 }
106
107 void
mpz_fdiv_q_2exp(mpz_ptr w,mpz_srcptr u,mp_bitcnt_t cnt)108 mpz_fdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
109 {
110 cfdiv_q_2exp (w, u, cnt, -1);
111 }
112