/src/gmp-6.2.1/mpn/sbpi1_div_qr.c
Line | Count | Source |
1 | | /* mpn_sbpi1_div_qr -- Schoolbook division using the Möller-Granlund 3/2 |
2 | | division algorithm. |
3 | | |
4 | | Contributed to the GNU project by Torbjorn Granlund. |
5 | | |
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. |
9 | | |
10 | | Copyright 2007, 2009 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 | | |
39 | | #include "gmp-impl.h" |
40 | | #include "longlong.h" |
41 | | |
42 | | mp_limb_t |
43 | | mpn_sbpi1_div_qr (mp_ptr qp, |
44 | | mp_ptr np, mp_size_t nn, |
45 | | mp_srcptr dp, mp_size_t dn, |
46 | | mp_limb_t dinv) |
47 | 23.7k | { |
48 | 23.7k | mp_limb_t qh; |
49 | 23.7k | mp_size_t i; |
50 | 23.7k | mp_limb_t n1, n0; |
51 | 23.7k | mp_limb_t d1, d0; |
52 | 23.7k | mp_limb_t cy, cy1; |
53 | 23.7k | mp_limb_t q; |
54 | | |
55 | 23.7k | ASSERT (dn > 2); |
56 | 23.7k | ASSERT (nn >= dn); |
57 | 23.7k | ASSERT ((dp[dn-1] & GMP_NUMB_HIGHBIT) != 0); |
58 | | |
59 | 23.7k | np += nn; |
60 | | |
61 | 23.7k | qh = mpn_cmp (np - dn, dp, dn) >= 0; |
62 | 23.7k | if (qh != 0) |
63 | 37 | mpn_sub_n (np - dn, np - dn, dp, dn); |
64 | | |
65 | 23.7k | qp += nn - dn; |
66 | | |
67 | 23.7k | dn -= 2; /* offset dn by 2 for main division loops, |
68 | | saving two iterations in mpn_submul_1. */ |
69 | 23.7k | d1 = dp[dn + 1]; |
70 | 23.7k | d0 = dp[dn + 0]; |
71 | | |
72 | 23.7k | np -= 2; |
73 | | |
74 | 23.7k | n1 = np[1]; |
75 | | |
76 | 514k | for (i = nn - (dn + 2); i > 0; i--) |
77 | 490k | { |
78 | 490k | np--; |
79 | 490k | if (UNLIKELY (n1 == d1) && np[1] == d0) |
80 | 1.70k | { |
81 | 1.70k | q = GMP_NUMB_MASK; |
82 | 1.70k | mpn_submul_1 (np - dn, dp, dn + 2, q); |
83 | 1.70k | n1 = np[1]; /* update n1, last loop's value will now be invalid */ |
84 | 1.70k | } |
85 | 488k | else |
86 | 488k | { |
87 | 488k | udiv_qr_3by2 (q, n1, n0, n1, np[1], np[0], d1, d0, dinv); |
88 | | |
89 | 488k | cy = mpn_submul_1 (np - dn, dp, dn, q); |
90 | | |
91 | 488k | cy1 = n0 < cy; |
92 | 488k | n0 = (n0 - cy) & GMP_NUMB_MASK; |
93 | 488k | cy = n1 < cy1; |
94 | 488k | n1 = (n1 - cy1) & GMP_NUMB_MASK; |
95 | 488k | np[0] = n0; |
96 | | |
97 | 488k | if (UNLIKELY (cy != 0)) |
98 | 406 | { |
99 | 406 | n1 += d1 + mpn_add_n (np - dn, np - dn, dp, dn + 1); |
100 | 406 | q--; |
101 | 406 | } |
102 | 488k | } |
103 | | |
104 | 490k | *--qp = q; |
105 | 490k | } |
106 | 23.7k | np[1] = n1; |
107 | | |
108 | 23.7k | return qh; |
109 | 23.7k | } |