1 /* mpn/bdivmod.c: mpn_bdivmod for computing U/V mod 2^d.
3 Copyright (C) 1991, 1993, 1994, 1995, 1996, 1999, 2000 Free Software
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 the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
23 /* q_high = mpn_bdivmod (qp, up, usize, vp, vsize, d).
25 Puts the low d/BITS_PER_MP_LIMB limbs of Q = U / V mod 2^d at qp, and
26 returns the high d%BITS_PER_MP_LIMB bits of Q as the result.
28 Also, U - Q * V mod 2^(usize*BITS_PER_MP_LIMB) is placed at up. Since the
29 low d/BITS_PER_MP_LIMB limbs of this difference are zero, the code allows
30 the limb vectors at qp to overwrite the low limbs at up, provided qp <= up.
34 2. usize * BITS_PER_MP_LIMB >= d.
35 3. If Q and U overlap, qp <= up.
37 Ken Weber (kweber@mat.ufrgs.br, kweber@mcs.kent.edu)
39 Funding for this work has been partially provided by Conselho Nacional
40 de Desenvolvimento Cienti'fico e Tecnolo'gico (CNPq) do Brazil, Grant
41 301314194-2, and was done while I was a visiting reseacher in the Instituto
42 de Matema'tica at Universidade Federal do Rio Grande do Sul (UFRGS).
45 T. Jebelean, An algorithm for exact division, Journal of Symbolic
46 Computation, v. 15, 1993, pp. 169-180.
48 K. Weber, The accelerated integer GCD algorithm, ACM Transactions on
49 Mathematical Software, v. 21 (March), 1995, pp. 111-122. */
57 mpn_bdivmod (mp_ptr qp, mp_ptr up, mp_size_t usize,
58 mp_srcptr vp, mp_size_t vsize, unsigned long int d)
60 mpn_bdivmod (qp, up, usize, vp, vsize, d)
71 /* 1/V mod 2^BITS_PER_MP_LIMB. */
72 modlimb_invert (v_inv, vp[0]);
74 /* Fast code for two cases previously used by the accel part of mpn_gcd.
75 (Could probably remove this now it's inlined there.) */
76 if (usize == 2 && vsize == 2 &&
77 (d == BITS_PER_MP_LIMB || d == 2*BITS_PER_MP_LIMB))
80 mp_limb_t q = up[0] * v_inv;
81 umul_ppmm (hi, lo, q, vp[0]);
82 up[0] = 0, up[1] -= hi + q*vp[1], qp[0] = q;
83 if (d == 2*BITS_PER_MP_LIMB)
84 q = up[1] * v_inv, up[1] = 0, qp[1] = q;
89 while (d >= BITS_PER_MP_LIMB)
91 mp_limb_t q = up[0] * v_inv;
92 mp_limb_t b = mpn_submul_1 (up, vp, MIN (usize, vsize), q);
94 mpn_sub_1 (up + vsize, up + vsize, usize - vsize, b);
95 d -= BITS_PER_MP_LIMB;
103 mp_limb_t q = (up[0] * v_inv) & (((mp_limb_t)1<<d) - 1);
109 b = mpn_sub_n (up, up, vp, MIN (usize, vsize));
112 b = mpn_submul_1 (up, vp, MIN (usize, vsize), q);
115 mpn_sub_1 (up + vsize, up + vsize, usize - vsize, b);