1
/* mpn_sb_divrem_mn -- Divide natural numbers, producing both remainder and
4
THE FUNCTIONS IN THIS FILE ARE INTERNAL FUNCTIONS WITH MUTABLE
5
INTERFACES. IT IS ONLY SAFE TO REACH THEM THROUGH DOCUMENTED INTERFACES.
6
IN FACT, IT IS ALMOST GUARANTEED THAT THEY'LL CHANGE OR DISAPPEAR IN A
10
Copyright 1993, 1994, 1995, 1996, 2000, 2001 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 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. */
34
/* The size where udiv_qrnnd_preinv should be used rather than udiv_qrnnd,
35
meaning the quotient size where that should happen, the quotient size
36
being how many udiv divisions will be done. */
38
#ifndef SB_PREINV_THRESHOLD
39
# if UDIV_PREINV_ALWAYS
40
# define SB_PREINV_THRESHOLD 0
42
# ifdef DIVREM_1_NORM_THRESHOLD
43
# define SB_PREINV_THRESHOLD DIVREM_1_NORM_THRESHOLD
45
# if UDIV_TIME <= UDIV_NORM_PREINV_TIME
46
# define SB_PREINV_THRESHOLD MP_LIMB_T_MAX
48
# define SB_PREINV_THRESHOLD \
49
(1 + UDIV_TIME / (UDIV_TIME - UDIV_NORM_PREINV_TIME))
56
/* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
57
the NSIZE-DSIZE least significant quotient limbs at QP
58
and the DSIZE long remainder at NP.
59
Return the most significant limb of the quotient, this is always 0 or 1.
63
1. The most significant bit of the divisor must be set.
64
2. QP must either not overlap with the input operands at all, or
65
QP + DSIZE >= NP must hold true. (This means that it's
66
possible to put the quotient in the high part of NUM, right after the
73
mpn_sb_divrem_mn (mp_ptr qp,
74
mp_ptr np, mp_size_t nsize,
75
mp_srcptr dp, mp_size_t dsize)
77
mp_limb_t most_significant_q_limb = 0;
78
mp_size_t qsize = nsize - dsize;
85
ASSERT (nsize >= dsize);
86
ASSERT (dp[dsize-1] & MP_LIMB_T_HIGHBIT);
87
ASSERT (! MPN_OVERLAP_P (np, nsize, dp, dsize));
88
ASSERT (! MPN_OVERLAP_P (qp, nsize-dsize, dp, dsize));
89
ASSERT (! MPN_OVERLAP_P (qp, nsize-dsize, np, nsize) || qp+dsize >= np);
98
if (n0 > dx || mpn_cmp (np, dp, dsize - 1) >= 0)
100
mpn_sub_n (np, np, dp, dsize);
101
most_significant_q_limb = 1;
105
/* use_preinv is possibly a constant, but it's left to the compiler to
106
optimize away the unused code in that case. */
107
use_preinv = ABOVE_THRESHOLD (qsize, SB_PREINV_THRESHOLD);
109
invert_limb (dxinv, dx);
111
for (i = qsize-1; i >= 0; i--)
122
/* This might over-estimate q, but it's probably not worth
123
the extra code here to find out. */
127
cy_limb = mpn_submul_1 (np, dp, dsize, q);
129
/* This should be faster on many machines */
130
cy_limb = mpn_sub_n (np + 1, np + 1, dp, dsize);
131
cy = mpn_add_n (np, np, dp, dsize);
137
mpn_add_n (np, np, dp, dsize);
145
mp_limb_t rx, r1, r0, p1, p0;
147
/* "workaround" avoids a problem with gcc 2.7.2.3 i386 register
148
usage when np[dsize-1] is used in an asm statement like
149
umul_ppmm in udiv_qrnnd_preinv. The symptom is seg faults due
150
to registers being clobbered. gcc 2.95 i386 doesn't have the
153
mp_limb_t workaround = np[dsize - 1];
155
udiv_qrnnd_preinv (q, r1, nx, workaround, dx, dxinv);
157
udiv_qrnnd (q, r1, nx, workaround, dx);
159
umul_ppmm (p1, p0, d1, q);
163
if (r1 < p1 || (r1 == p1 && r0 < p0))
172
p1 += r0 < p0; /* cannot carry! */
173
rx -= r1 < p1; /* may become 11..1 if q is still too large */
177
cy_limb = mpn_submul_1 (np, dp, dsize - 2, q);
189
mpn_add_n (np, np, dp, dsize);
197
/* ______ ______ ______
198
|__rx__|__r1__|__r0__| partial remainder
200
- |__p1__|__p0__| partial product to subtract
204
rx is -1, 0 or 1. If rx=1, then q is correct (it should match
205
carry out). If rx=-1 then q is too large. If rx=0, then q might
206
be too large, but it is most likely correct.
209
return most_significant_q_limb;