1 /* mpn_sbpi1_bdiv_q -- schoolbook Hensel division with precomputed inverse,
2 returning quotient only.
4 Contributed to the GNU project by Niels Möller.
6 THE FUNCTIONS IN THIS FILE ARE INTERNAL FUNCTIONS WITH MUTABLE INTERFACES.
7 IT IS ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS
8 ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
10 Copyright 2005, 2006, 2009 Free Software Foundation, Inc.
12 This file is part of the GNU MP Library.
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of the GNU Lesser General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or (at your
17 option) any later version.
19 The GNU MP Library is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
22 License for more details.
24 You should have received a copy of the GNU Lesser General Public License
25 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
31 /* Computes Q = N / D mod B^nn, destroys N.
33 D must be odd. dinv is (-D)^-1 mod B.
36 The straightforward way to compute Q is to cancel one limb at a time, using
38 qp[i] = D^{-1} * np[i] (mod B)
41 But we prefer addition to subtraction, since mpn_addmul_1 is often faster
42 than mpn_submul_1. Q = - N / D can be computed by iterating
44 qp[i] = (-D)^{-1} * np[i] (mod B)
47 And then we flip the sign, -Q = (not Q) + 1. */
50 mpn_sbpi1_bdiv_q (mp_ptr qp,
51 mp_ptr np, mp_size_t nn,
52 mp_srcptr dp, mp_size_t dn,
60 ASSERT ((dp[0] & 1) != 0);
62 for (i = nn - dn; i > 0; i--)
67 cy = mpn_addmul_1 (np, dp, dn, q);
68 mpn_add_1 (np + dn, np + dn, i, cy);
73 for (i = dn; i > 1; i--)
78 mpn_addmul_1 (np, dp, i, q);
86 mpn_add_1 (qp - nn + 1, qp - nn + 1, nn, 1);