beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / bdiv_q_1.c
blob74b247d5a9ab9639a2eec55a1ac6ad177bc4b136
1 /* mpn_bdiv_q_1, mpn_pi1_bdiv_q_1 -- schoolbook Hensel division by 1-limb
2 divisor, returning quotient only.
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.
8 Copyright 2000-2003, 2005, 2009 Free Software Foundation, Inc.
10 This file is part of the GNU MP Library.
12 The GNU MP Library is free software; you can redistribute it and/or modify
13 it under the terms of either:
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.
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.
25 or both in parallel, as here.
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.
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/. */
36 #include "gmp.h"
37 #include "gmp-impl.h"
38 #include "longlong.h"
40 mp_limb_t
41 mpn_pi1_bdiv_q_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t d,
42 mp_limb_t di, int shift)
44 mp_size_t i;
45 mp_limb_t c, h, l, u, u_next, dummy;
47 ASSERT (n >= 1);
48 ASSERT (d != 0);
49 ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
50 ASSERT_MPN (up, n);
51 ASSERT_LIMB (d);
53 d <<= GMP_NAIL_BITS;
55 if (shift != 0)
57 c = 0;
59 u = up[0];
60 rp--;
61 for (i = 1; i < n; i++)
63 u_next = up[i];
64 u = ((u >> shift) | (u_next << (GMP_NUMB_BITS-shift))) & GMP_NUMB_MASK;
66 SUBC_LIMB (c, l, u, c);
68 l = (l * di) & GMP_NUMB_MASK;
69 rp[i] = l;
71 umul_ppmm (h, dummy, l, d);
72 c += h;
73 u = u_next;
76 u = u >> shift;
77 l = u - c;
78 l = (l * di) & GMP_NUMB_MASK;
79 rp[i] = l;
81 else
83 u = up[0];
84 l = (u * di) & GMP_NUMB_MASK;
85 rp[0] = l;
86 c = 0;
88 for (i = 1; i < n; i++)
90 umul_ppmm (h, dummy, l, d);
91 c += h;
93 u = up[i];
94 SUBC_LIMB (c, l, u, c);
96 l = (l * di) & GMP_NUMB_MASK;
97 rp[i] = l;
101 return c;
104 mp_limb_t
105 mpn_bdiv_q_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t d)
107 mp_limb_t di;
108 int shift;
110 ASSERT (n >= 1);
111 ASSERT (d != 0);
112 ASSERT (MPN_SAME_OR_SEPARATE_P (rp, up, n));
113 ASSERT_MPN (up, n);
114 ASSERT_LIMB (d);
116 if ((d & 1) == 0)
118 count_trailing_zeros (shift, d);
119 d >>= shift;
121 else
122 shift = 0;
124 binvert_limb (di, d);
125 return mpn_pi1_bdiv_q_1 (rp, up, n, d, di, shift);