1 /* mpn_sbpi1_bdiv_qr -- schoolbook Hensel division with precomputed inverse,
2    returning quotient and remainder.
3 
4    Contributed to the GNU project by Niels Möller.
5 
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.
9 
10 Copyright 2006, 2009, 2011, 2012 Free Software Foundation, Inc.
11 
12 This file is part of the GNU MP Library.
13 
14 The GNU MP Library is free software; you can redistribute it and/or modify
15 it under the terms of either:
16 
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.
20 
21 or
22 
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.
26 
27 or both in parallel, as here.
28 
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.
33 
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/.  */
37 
38 #include "gmp.h"
39 #include "gmp-impl.h"
40 
41 
42 /* Computes a binary quotient of size qn = nn - dn.
43    Output:
44 
45       Q = N * D^{-1} mod B^qn,
46 
47       R = (N - Q * D) * B^(-qn)
48 
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.
51 
52    D must be odd. dinv is (-D)^-1 mod B. */
53 
54 mp_limb_t
mpn_sbpi1_bdiv_qr(mp_ptr qp,mp_ptr np,mp_size_t nn,mp_srcptr dp,mp_size_t dn,mp_limb_t dinv)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)
58 {
59   mp_size_t qn;
60   mp_size_t i;
61   mp_limb_t rh;
62   mp_limb_t ql;
63 
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.  */
69 
70   qn = nn - dn;
71 
72   rh = 0;
73 
74   /* To complete the negation, this value is added to q. */
75   ql = 1;
76   while (qn > dn)
77     {
78       for (i = 0; i < dn; i++)
79 	{
80 	  mp_limb_t q;
81 
82 	  q = dinv * np[i];
83 	  np[i] = mpn_addmul_1 (np + i, dp, dn, q);
84 	  qp[i] = ~q;
85 	}
86       rh += mpn_add (np + dn, np + dn, qn, np, dn);
87       ql = mpn_add_1 (qp, qp, dn, ql);
88 
89       qp += dn; qn -= dn;
90       np += dn; nn -= dn;
91     }
92 
93   for (i = 0; i < qn; i++)
94     {
95       mp_limb_t q;
96 
97       q = dinv * np[i];
98       np[i] = mpn_addmul_1 (np + i, dp, dn, q);
99       qp[i] = ~q;
100     }
101 
102   rh += mpn_add_n (np + dn, np + dn, np, qn);
103   ql = mpn_add_1 (qp, qp, qn, ql);
104 
105   if (UNLIKELY (ql > 0))
106     {
107       /* q == 0 */
108       ASSERT (rh == 0);
109       return 0;
110     }
111   else
112     {
113       mp_limb_t cy;
114 
115       cy = mpn_sub_n (np + qn, np + qn, dp, dn);
116       ASSERT (cy >= rh);
117       return cy - rh;
118     }
119 }
120