1 /* mpn_sbpi1_bdiv_qr -- schoolbook Hensel division with precomputed inverse,
2 returning quotient and remainder.
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 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 a binary quotient of size qn = nn - dn.
34 Q = N * D^{-1} mod B^qn,
36 R = (N - Q * D) * B^(-qn)
38 Stores the dn least significant limbs of R at {np + nn - dn, dn},
39 and returns the borrow from the subtraction N - Q*D.
41 D must be odd. dinv is (-D)^-1 mod B. */
44 mpn_sbpi1_bdiv_qr (mp_ptr qp,
45 mp_ptr np, mp_size_t nn,
46 mp_srcptr dp, mp_size_t dn, mp_limb_t dinv)
55 ASSERT ((dp[0] & 1) != 0);
61 /* To complete the negation, this value is added to q. */
65 for (i = 0; i < dn; i++)
72 np[i] = mpn_addmul_1 (np + i, dp, dn, q);
74 rh += mpn_add (np + dn, np + dn, qn, np, dn);
75 ql = mpn_add_1 (qp, qp, dn, ql);
81 for (i = 0; i < qn; i++)
88 np[i] = mpn_addmul_1 (np + i, dp, dn, q);
91 rh += mpn_add_n (np + dn, np + dn, np, qn);
92 ql = mpn_add_1 (qp, qp, qn, ql);
94 if (UNLIKELY (ql > 0))
104 cy = mpn_sub_n (np + qn, np + qn, dp, dn);