Line | Count | Source (jump to first uncovered line) |
1 | | /* mpn_bdiv_q_1, mpn_pi1_bdiv_q_1 -- schoolbook Hensel division by 1-limb |
2 | | divisor, returning quotient only. |
3 | | |
4 | | THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST |
5 | | CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN |
6 | | FUTURE GNU MP RELEASES. |
7 | | |
8 | | Copyright 2000-2003, 2005, 2009, 2017 Free Software Foundation, Inc. |
9 | | |
10 | | This file is part of the GNU MP Library. |
11 | | |
12 | | The GNU MP Library is free software; you can redistribute it and/or modify |
13 | | it under the terms of either: |
14 | | |
15 | | * the GNU Lesser General Public License as published by the Free |
16 | | Software Foundation; either version 3 of the License, or (at your |
17 | | option) any later version. |
18 | | |
19 | | or |
20 | | |
21 | | * the GNU General Public License as published by the Free Software |
22 | | Foundation; either version 2 of the License, or (at your option) any |
23 | | later version. |
24 | | |
25 | | or both in parallel, as here. |
26 | | |
27 | | The GNU MP Library is distributed in the hope that it will be useful, but |
28 | | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
29 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
30 | | for more details. |
31 | | |
32 | | You should have received copies of the GNU General Public License and the |
33 | | GNU Lesser General Public License along with the GNU MP Library. If not, |
34 | | see https://www.gnu.org/licenses/. */ |
35 | | |
36 | | #include "gmp-impl.h" |
37 | | #include "longlong.h" |
38 | | |
39 | | mp_limb_t |
40 | | mpn_pi1_bdiv_q_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t d, |
41 | | mp_limb_t di, int shift) |
42 | 0 | { |
43 | 0 | mp_size_t i; |
44 | 0 | mp_limb_t c, h, l, u, u_next, dummy; |
45 | |
|
46 | 0 | ASSERT (n >= 1); |
47 | 0 | ASSERT (d != 0); |
48 | 0 | ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n)); |
49 | 0 | ASSERT_MPN (up, n); |
50 | 0 | ASSERT_LIMB (d); |
51 | |
|
52 | 0 | d <<= GMP_NAIL_BITS; |
53 | |
|
54 | 0 | if (shift != 0) |
55 | 0 | { |
56 | 0 | c = 0; |
57 | |
|
58 | 0 | u = up[0]; |
59 | 0 | rp--; |
60 | 0 | for (i = 1; i < n; i++) |
61 | 0 | { |
62 | 0 | u_next = up[i]; |
63 | 0 | u = ((u >> shift) | (u_next << (GMP_NUMB_BITS-shift))) & GMP_NUMB_MASK; |
64 | |
|
65 | 0 | SUBC_LIMB (c, l, u, c); |
66 | |
|
67 | 0 | l = (l * di) & GMP_NUMB_MASK; |
68 | 0 | rp[i] = l; |
69 | |
|
70 | 0 | umul_ppmm (h, dummy, l, d); |
71 | 0 | c += h; |
72 | 0 | u = u_next; |
73 | 0 | } |
74 | |
|
75 | 0 | u = u >> shift; |
76 | 0 | SUBC_LIMB (c, l, u, c); |
77 | |
|
78 | 0 | l = (l * di) & GMP_NUMB_MASK; |
79 | 0 | rp[n] = l; |
80 | 0 | } |
81 | 0 | else |
82 | 0 | { |
83 | 0 | u = up[0]; |
84 | 0 | l = (u * di) & GMP_NUMB_MASK; |
85 | 0 | rp[0] = l; |
86 | 0 | c = 0; |
87 | |
|
88 | 0 | for (i = 1; i < n; i++) |
89 | 0 | { |
90 | 0 | umul_ppmm (h, dummy, l, d); |
91 | 0 | c += h; |
92 | |
|
93 | 0 | u = up[i]; |
94 | 0 | SUBC_LIMB (c, l, u, c); |
95 | |
|
96 | 0 | l = (l * di) & GMP_NUMB_MASK; |
97 | 0 | rp[i] = l; |
98 | 0 | } |
99 | 0 | } |
100 | |
|
101 | 0 | return c; |
102 | 0 | } |
103 | | |
104 | | mp_limb_t |
105 | | mpn_bdiv_q_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t d) |
106 | 0 | { |
107 | 0 | mp_limb_t di; |
108 | 0 | int shift; |
109 | |
|
110 | 0 | ASSERT (n >= 1); |
111 | 0 | ASSERT (d != 0); |
112 | 0 | ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n)); |
113 | 0 | ASSERT_MPN (up, n); |
114 | 0 | ASSERT_LIMB (d); |
115 | |
|
116 | 0 | count_trailing_zeros (shift, d); |
117 | 0 | d >>= shift; |
118 | |
|
119 | 0 | binvert_limb (di, d); |
120 | 0 | return mpn_pi1_bdiv_q_1 (rp, up, n, d, di, shift); |
121 | 0 | } |