1 /* mpn_divrem_1(quot_ptr, qsize, dividend_ptr, dividend_size, divisor_limb) --
2 Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
3 Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
4 Return the single-limb remainder.
5 There are no constraints on the value of the divisor.
7 QUOT_PTR and DIVIDEND_PTR might point to the same limb.
9 Copyright (C) 1991, 1993, 1994, 1996, 1998, 1999, 2000 Free Software
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 the GNU Lesser General Public License as published by
16 the Free Software Foundation; either version 2.1 of the License, or (at your
17 option) any later version.
19 The GNU MP Library is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
22 License for more details.
24 You should have received a copy of the GNU Lesser General Public License
25 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
26 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 MA 02111-1307, USA. */
35 /* __gmpn_divmod_1_internal(quot_ptr,dividend_ptr,dividend_size,divisor_limb)
36 Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
37 Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
38 Return the single-limb remainder.
39 There are no constraints on the value of the divisor.
41 QUOT_PTR and DIVIDEND_PTR might point to the same limb. */
48 #define UDIV_TIME UMUL_TIME
53 __gmpn_divmod_1_internal (mp_ptr quot_ptr,
54 mp_srcptr dividend_ptr, mp_size_t dividend_size,
55 mp_limb_t divisor_limb)
57 __gmpn_divmod_1_internal (quot_ptr, dividend_ptr, dividend_size, divisor_limb)
59 mp_srcptr dividend_ptr;
60 mp_size_t dividend_size;
61 mp_limb_t divisor_limb;
68 /* ??? Should this be handled at all? Rely on callers? */
69 if (dividend_size == 0)
72 /* If multiplication is much faster than division, and the
73 dividend is large, pre-invert the divisor, and use
74 only multiplications in the inner loop. */
76 /* This test should be read:
77 Does it ever help to use udiv_qrnnd_preinv?
78 && Does what we save compensate for the inversion overhead? */
79 if (UDIV_TIME > (2 * UMUL_TIME + 6)
80 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME)
82 int normalization_steps;
84 count_leading_zeros (normalization_steps, divisor_limb);
85 if (normalization_steps != 0)
87 mp_limb_t divisor_limb_inverted;
89 divisor_limb <<= normalization_steps;
90 invert_limb (divisor_limb_inverted, divisor_limb);
92 n1 = dividend_ptr[dividend_size - 1];
93 r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
95 /* Possible optimization:
97 && divisor_limb > ((n1 << normalization_steps)
98 | (dividend_ptr[dividend_size - 2] >> ...)))
99 ...one division less... */
101 for (i = dividend_size - 2; i >= 0; i--)
103 n0 = dividend_ptr[i];
104 udiv_qrnnd_preinv (quot_ptr[i + 1], r, r,
105 ((n1 << normalization_steps)
106 | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
107 divisor_limb, divisor_limb_inverted);
110 udiv_qrnnd_preinv (quot_ptr[0], r, r,
111 n1 << normalization_steps,
112 divisor_limb, divisor_limb_inverted);
113 return r >> normalization_steps;
117 mp_limb_t divisor_limb_inverted;
119 invert_limb (divisor_limb_inverted, divisor_limb);
121 i = dividend_size - 1;
124 if (r >= divisor_limb)
134 n0 = dividend_ptr[i];
135 udiv_qrnnd_preinv (quot_ptr[i], r, r,
136 n0, divisor_limb, divisor_limb_inverted);
143 if (UDIV_NEEDS_NORMALIZATION)
145 int normalization_steps;
147 count_leading_zeros (normalization_steps, divisor_limb);
148 if (normalization_steps != 0)
150 divisor_limb <<= normalization_steps;
152 n1 = dividend_ptr[dividend_size - 1];
153 r = n1 >> (BITS_PER_MP_LIMB - normalization_steps);
155 /* Possible optimization:
157 && divisor_limb > ((n1 << normalization_steps)
158 | (dividend_ptr[dividend_size - 2] >> ...)))
159 ...one division less... */
161 for (i = dividend_size - 2; i >= 0; i--)
163 n0 = dividend_ptr[i];
164 udiv_qrnnd (quot_ptr[i + 1], r, r,
165 ((n1 << normalization_steps)
166 | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))),
170 udiv_qrnnd (quot_ptr[0], r, r,
171 n1 << normalization_steps,
173 return r >> normalization_steps;
176 /* No normalization needed, either because udiv_qrnnd doesn't require
177 it, or because DIVISOR_LIMB is already normalized. */
179 i = dividend_size - 1;
182 if (r >= divisor_limb)
192 n0 = dividend_ptr[i];
193 udiv_qrnnd (quot_ptr[i], r, r, n0, divisor_limb);
203 mpn_divrem_1 (mp_ptr qp, mp_size_t qxn,
204 mp_srcptr np, mp_size_t nn,
207 mpn_divrem_1 (qp, qxn, np, nn, d)
218 /* Develop integer part of quotient. */
219 rlimb = __gmpn_divmod_1_internal (qp + qxn, np, nn, d);
221 /* Develop fraction part of quotient. This is not as fast as it should;
222 the preinvert stuff from __gmpn_divmod_1_internal ought to be used here
224 if (UDIV_NEEDS_NORMALIZATION)
226 int normalization_steps;
228 count_leading_zeros (normalization_steps, d);
229 if (normalization_steps != 0)
231 d <<= normalization_steps;
232 rlimb <<= normalization_steps;
234 for (i = qxn - 1; i >= 0; i--)
235 udiv_qrnnd (qp[i], rlimb, rlimb, 0, d);
237 return rlimb >> normalization_steps;
244 for (i = qxn - 1; i >= 0; i--)
245 udiv_qrnnd (qp[i], rlimb, rlimb, 0, d);