beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / sbpi1_div_qr.c
blob0c3e4cb72987199af3f4024891b76109e019018f
1 /* mpn_sbpi1_div_qr -- Schoolbook division using the Möller-Granlund 3/2
2 division algorithm.
4 Contributed to the GNU project by Torbjorn Granlund.
6 THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE. IT IS ONLY
7 SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES. IN FACT, IT IS ALMOST
8 GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GMP RELEASE.
10 Copyright 2007, 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 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/. */
39 #include "gmp.h"
40 #include "gmp-impl.h"
41 #include "longlong.h"
43 mp_limb_t
44 mpn_sbpi1_div_qr (mp_ptr qp,
45 mp_ptr np, mp_size_t nn,
46 mp_srcptr dp, mp_size_t dn,
47 mp_limb_t dinv)
49 mp_limb_t qh;
50 mp_size_t i;
51 mp_limb_t n1, n0;
52 mp_limb_t d1, d0;
53 mp_limb_t cy, cy1;
54 mp_limb_t q;
56 ASSERT (dn > 2);
57 ASSERT (nn >= dn);
58 ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0);
60 np += nn;
62 qh = mpn_cmp (np - dn, dp, dn) >= 0;
63 if (qh != 0)
64 mpn_sub_n (np - dn, np - dn, dp, dn);
66 qp += nn - dn;
68 dn -= 2; /* offset dn by 2 for main division loops,
69 saving two iterations in mpn_submul_1. */
70 d1 = dp[dn + 1];
71 d0 = dp[dn + 0];
73 np -= 2;
75 n1 = np[1];
77 for (i = nn - (dn + 2); i > 0; i--)
79 np--;
80 if (UNLIKELY (n1 == d1) && np[1] == d0)
82 q = GMP_NUMB_MASK;
83 mpn_submul_1 (np - dn, dp, dn + 2, q);
84 n1 = np[1]; /* update n1, last loop's value will now be invalid */
86 else
88 udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv);
90 cy = mpn_submul_1 (np - dn, dp, dn, q);
92 cy1 = n0 < cy;
93 n0 = (n0 - cy) & GMP_NUMB_MASK;
94 cy = n1 < cy1;
95 n1 = (n1 - cy1) & GMP_NUMB_MASK;
96 np[0] = n0;
98 if (UNLIKELY (cy != 0))
100 n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1);
101 q--;
105 *--qp = q;
107 np[1] = n1;
109 return qh;