beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / sbpi1_bdiv_qr.c
blob0e56f5814873f80a7cd87f5d4976d8372b3621d6
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, 2011, 2012 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 either:
17 * the GNU Lesser General Public License as published by the Free
18 Software Foundation; either version 3 of the License, or (at your
19 option) any later version.
23 * the GNU General Public License as published by the Free Software
24 Foundation; either version 2 of the License, or (at your option) any
25 later version.
27 or both in parallel, as here.
29 The GNU MP Library is distributed in the hope that it will be useful, but
30 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
31 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
32 for more details.
34 You should have received copies of the GNU General Public License and the
35 GNU Lesser General Public License along with the GNU MP Library. If not,
36 see https://www.gnu.org/licenses/. */
38 #include "gmp.h"
39 #include "gmp-impl.h"
42 /* Computes a binary quotient of size qn = nn - dn.
43 Output:
45 Q = N * D^{-1} mod B^qn,
47 R = (N - Q * D) * B^(-qn)
49 Stores the dn least significant limbs of R at {np + nn - dn, dn},
50 and returns the borrow from the subtraction N - Q*D.
52 D must be odd. dinv is (-D)^-1 mod B. */
54 mp_limb_t
55 mpn_sbpi1_bdiv_qr (mp_ptr qp,
56 mp_ptr np, mp_size_t nn,
57 mp_srcptr dp, mp_size_t dn, mp_limb_t dinv)
59 mp_size_t qn;
60 mp_size_t i;
61 mp_limb_t rh;
62 mp_limb_t ql;
64 ASSERT (dn > 0);
65 ASSERT (nn > dn);
66 ASSERT ((dp[0] & 1) != 0);
67 /* FIXME: Add ASSERTs for allowable overlapping; i.e., that qp = np is OK,
68 but some over N/Q overlaps will not work. */
70 qn = nn - dn;
72 rh = 0;
74 /* To complete the negation, this value is added to q. */
75 ql = 1;
76 while (qn > dn)
78 for (i = 0; i < dn; i++)
80 mp_limb_t q;
82 q = dinv * np[i];
83 np[i] = mpn_addmul_1 (np + i, dp, dn, q);
84 qp[i] = ~q;
86 rh += mpn_add (np + dn, np + dn, qn, np, dn);
87 ql = mpn_add_1 (qp, qp, dn, ql);
89 qp += dn; qn -= dn;
90 np += dn; nn -= dn;
93 for (i = 0; i < qn; i++)
95 mp_limb_t q;
97 q = dinv * np[i];
98 np[i] = mpn_addmul_1 (np + i, dp, dn, q);
99 qp[i] = ~q;
102 rh += mpn_add_n (np + dn, np + dn, np, qn);
103 ql = mpn_add_1 (qp, qp, qn, ql);
105 if (UNLIKELY (ql > 0))
107 /* q == 0 */
108 ASSERT (rh == 0);
109 return 0;
111 else
113 mp_limb_t cy;
115 cy = mpn_sub_n (np + qn, np + qn, dp, dn);
116 ASSERT (cy >= rh);
117 return cy - rh;