beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / sbpi1_divappr_q.c
blob3e7cf91ba610ba2c3e41451960f3a23b3ea9b754
1 /* mpn_sbpi1_divappr_q -- Schoolbook division using the Möller-Granlund 3/2
2 division algorithm, returning approximate quotient. The quotient returned
3 is either correct, or one too large.
5 Contributed to the GNU project by Torbjorn Granlund.
7 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
8 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
9 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
11 Copyright 2007, 2009 Free Software Foundation, Inc.
13 This file is part of the GNU MP Library.
15 The GNU MP Library is free software; you can redistribute it and/or modify
16 it under the terms of either:
18 * the GNU Lesser General Public License as published by the Free
19 Software Foundation; either version 3 of the License, or (at your
20 option) any later version.
24 * the GNU General Public License as published by the Free Software
25 Foundation; either version 2 of the License, or (at your option) any
26 later version.
28 or both in parallel, as here.
30 The GNU MP Library is distributed in the hope that it will be useful, but
31 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
32 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
33 for more details.
35 You should have received copies of the GNU General Public License and the
36 GNU Lesser General Public License along with the GNU MP Library. If not,
37 see https://www.gnu.org/licenses/. */
40 #include "gmp.h"
41 #include "gmp-impl.h"
42 #include "longlong.h"
44 mp_limb_t
45 mpn_sbpi1_divappr_q (mp_ptr qp,
46 mp_ptr np, mp_size_t nn,
47 mp_srcptr dp, mp_size_t dn,
48 mp_limb_t dinv)
50 mp_limb_t qh;
51 mp_size_t qn, i;
52 mp_limb_t n1, n0;
53 mp_limb_t d1, d0;
54 mp_limb_t cy, cy1;
55 mp_limb_t q;
56 mp_limb_t flag;
58 ASSERT (dn > 2);
59 ASSERT (nn >= dn);
60 ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0);
62 np += nn;
64 qn = nn - dn;
65 if (qn + 1 < dn)
67 dp += dn - (qn + 1);
68 dn = qn + 1;
71 qh = mpn_cmp (np - dn, dp, dn) >= 0;
72 if (qh != 0)
73 mpn_sub_n (np - dn, np - dn, dp, dn);
75 qp += qn;
77 dn -= 2; /* offset dn by 2 for main division loops,
78 saving two iterations in mpn_submul_1. */
79 d1 = dp[dn + 1];
80 d0 = dp[dn + 0];
82 np -= 2;
84 n1 = np[1];
86 for (i = qn - (dn + 2); i >= 0; i--)
88 np--;
89 if (UNLIKELY (n1 == d1) && np[1] == d0)
91 q = GMP_NUMB_MASK;
92 mpn_submul_1 (np - dn, dp, dn + 2, q);
93 n1 = np[1]; /* update n1, last loop's value will now be invalid */
95 else
97 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
99 cy = mpn_submul_1 (np - dn, dp, dn, q);
101 cy1 = n0 < cy;
102 n0 = (n0 - cy) & GMP_NUMB_MASK;
103 cy = n1 < cy1;
104 n1 -= cy1;
105 np[0] = n0;
107 if (UNLIKELY (cy != 0))
109 n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
110 q--;
114 *--qp = q;
117 flag = ~CNST_LIMB(0);
119 if (dn >= 0)
121 for (i = dn; i > 0; i--)
123 np--;
124 if (UNLIKELY (n1 >= (d1 & flag)))
126 q = GMP_NUMB_MASK;
127 cy = mpn_submul_1 (np - dn, dp, dn + 2, q);
129 if (UNLIKELY (n1 != cy))
131 if (n1 < (cy & flag))
133 q--;
134 mpn_add_n (np - dn, np - dn, dp, dn + 2);
136 else
137 flag = 0;
139 n1 = np[1];
141 else
143 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
145 cy = mpn_submul_1 (np - dn, dp, dn, q);
147 cy1 = n0 < cy;
148 n0 = (n0 - cy) & GMP_NUMB_MASK;
149 cy = n1 < cy1;
150 n1 -= cy1;
151 np[0] = n0;
153 if (UNLIKELY (cy != 0))
155 n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
156 q--;
160 *--qp = q;
162 /* Truncate operands. */
163 dn--;
164 dp++;
167 np--;
168 if (UNLIKELY (n1 >= (d1 & flag)))
170 q = GMP_NUMB_MASK;
171 cy = mpn_submul_1 (np, dp, 2, q);
173 if (UNLIKELY (n1 != cy))
175 if (n1 < (cy & flag))
177 q--;
178 add_ssaaaa (np[1], np[0], np[1], np[0], dp[1], dp[0]);
180 else
181 flag = 0;
183 n1 = np[1];
185 else
187 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
189 np[1] = n1;
190 np[0] = n0;
193 *--qp = q;
196 ASSERT_ALWAYS (np[1] == n1);
198 return qh;