beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / cfdiv_q_2exp.c
blob366d6aa70465507faf9a2b9d9720ba82c4e28e78
1 /* mpz_cdiv_q_2exp, mpz_fdiv_q_2exp -- quotient from mpz divided by 2^n.
3 Copyright 1991, 1993, 1994, 1996, 1998, 1999, 2001, 2002, 2004, 2012,
4 2015 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of either:
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.
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.
21 or both in parallel, as here.
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.
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/. */
32 #include "gmp.h"
33 #include "gmp-impl.h"
36 /* dir==1 for ceil, dir==-1 for floor */
38 static void __gmpz_cfdiv_q_2exp (REGPARM_3_1 (mpz_ptr, mpz_srcptr, mp_bitcnt_t, int)) REGPARM_ATTR (1);
39 #define cfdiv_q_2exp(w,u,cnt,dir) __gmpz_cfdiv_q_2exp (REGPARM_3_1 (w,u,cnt,dir))
41 REGPARM_ATTR (1) static void
42 cfdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt, int dir)
44 mp_size_t wsize, usize, abs_usize, limb_cnt, i;
45 mp_srcptr up;
46 mp_ptr wp;
47 mp_limb_t round, rmask;
49 usize = SIZ (u);
50 abs_usize = ABS (usize);
51 limb_cnt = cnt / GMP_NUMB_BITS;
52 wsize = abs_usize - limb_cnt;
53 if (wsize <= 0)
55 /* u < 2**cnt, so result 1, 0 or -1 according to rounding */
56 PTR(w)[0] = 1;
57 SIZ(w) = (usize == 0 || (usize ^ dir) < 0 ? 0 : dir);
58 return;
61 /* +1 limb to allow for mpn_add_1 below */
62 wp = MPZ_REALLOC (w, wsize+1);
64 /* Check for rounding if direction matches u sign.
65 Set round if we're skipping non-zero limbs. */
66 up = PTR(u);
67 round = 0;
68 rmask = ((usize ^ dir) >= 0 ? MP_LIMB_T_MAX : 0);
69 if (rmask != 0)
70 for (i = 0; i < limb_cnt && round == 0; i++)
71 round = up[i];
73 cnt %= GMP_NUMB_BITS;
74 if (cnt != 0)
76 round |= rmask & mpn_rshift (wp, up + limb_cnt, wsize, cnt);
77 wsize -= (wp[wsize - 1] == 0);
79 else
80 MPN_COPY_INCR (wp, up + limb_cnt, wsize);
82 if (round != 0)
84 if (wsize != 0)
86 mp_limb_t cy;
87 cy = mpn_add_1 (wp, wp, wsize, CNST_LIMB(1));
88 wp[wsize] = cy;
89 wsize += cy;
91 else
93 /* We shifted something to zero. */
94 wp[0] = 1;
95 wsize = 1;
98 SIZ(w) = (usize >= 0 ? wsize : -wsize);
102 void
103 mpz_cdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
105 cfdiv_q_2exp (w, u, cnt, 1);
108 void
109 mpz_fdiv_q_2exp (mpz_ptr w, mpz_srcptr u, mp_bitcnt_t cnt)
111 cfdiv_q_2exp (w, u, cnt, -1);