1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/* mpn_sbpi1_bdiv_qr -- schoolbook Hensel division with precomputed inverse,
returning quotient and remainder.
Contributed to the GNU project by Niels Möller.
THE FUNCTIONS IN THIS FILE ARE INTERNAL FUNCTIONS WITH MUTABLE INTERFACES.
IT IS ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS
ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
Copyright 2006, 2009, 2011, 2012 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library. If not, see https://www.gnu.org/licenses/. */
#include "gmp.h"
#include "gmp-impl.h"
/* Computes a binary quotient of size qn = nn - dn.
Output:
Q = N * D^{-1} mod B^qn,
R = (N - Q * D) * B^(-qn)
Stores the dn least significant limbs of R at {np + nn - dn, dn},
and returns the borrow from the subtraction N - Q*D.
D must be odd. dinv is (-D)^-1 mod B. */
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)
{
mp_size_t qn;
mp_size_t i;
mp_limb_t rh;
mp_limb_t ql;
ASSERT (dn > 0);
ASSERT (nn > dn);
ASSERT ((dp[0] & 1) != 0);
/* FIXME: Add ASSERTs for allowable overlapping; i.e., that qp = np is OK,
but some over N/Q overlaps will not work. */
qn = nn - dn;
rh = 0;
/* To complete the negation, this value is added to q. */
ql = 1;
while (qn > dn)
{
for (i = 0; i < dn; i++)
{
mp_limb_t q;
q = dinv * np[i];
np[i] = mpn_addmul_1 (np + i, dp, dn, q);
qp[i] = ~q;
}
rh += mpn_add (np + dn, np + dn, qn, np, dn);
ql = mpn_add_1 (qp, qp, dn, ql);
qp += dn; qn -= dn;
np += dn; nn -= dn;
}
for (i = 0; i < qn; i++)
{
mp_limb_t q;
q = dinv * np[i];
np[i] = mpn_addmul_1 (np + i, dp, dn, q);
qp[i] = ~q;
}
rh += mpn_add_n (np + dn, np + dn, np, qn);
ql = mpn_add_1 (qp, qp, qn, ql);
if (UNLIKELY (ql > 0))
{
/* q == 0 */
ASSERT (rh == 0);
return 0;
}
else
{
mp_limb_t cy;
cy = mpn_sub_n (np + qn, np + qn, dp, dn);
ASSERT (cy >= rh);
return cy - rh;
}
}
|