1 /* mpn_preinv_divrem_1 -- mpn by limb division with pre-inverted divisor.
3 THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
4 CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5 FUTURE GNU MP RELEASES.
7 Copyright 2000-2003 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
14 * the GNU Lesser General Public License as published by the Free
15 Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
20 * the GNU General Public License as published by the Free Software
21 Foundation; either version 2 of the License, or (at your option) any
24 or both in parallel, as here.
26 The GNU MP Library is distributed in the hope that it will be useful, but
27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
31 You should have received copies of the GNU General Public License and the
32 GNU Lesser General Public License along with the GNU MP Library. If not,
33 see https://www.gnu.org/licenses/. */
40 /* Don't bloat a shared library with unused code. */
41 #if USE_PREINV_DIVREM_1
43 /* Same test here for skipping one divide step as in mpn_divrem_1.
45 The main reason for a separate shift==0 case is that not all CPUs give
46 zero for "n0 >> GMP_LIMB_BITS" which would arise in the general case
47 code used on shift==0. shift==0 is also reasonably common in mp_bases
48 big_base, for instance base==10 on a 64-bit limb.
50 Under shift!=0 it would be possible to call mpn_lshift to adjust the
51 dividend all in one go (into the quotient space say), rather than
52 limb-by-limb in the loop. This might help if mpn_lshift is a lot faster
53 than what the compiler can generate for EXTRACT. But this is left to CPU
54 specific implementations to consider, especially since EXTRACT isn't on
57 If size==0 then the result is simply xsize limbs of zeros, but nothing
58 special is done for that, since it wouldn't be a usual call, and
59 certainly never arises from mpn_get_str which is our main caller. */
62 mpn_preinv_divrem_1 (mp_ptr qp
, mp_size_t xsize
,
63 mp_srcptr ap
, mp_size_t size
, mp_limb_t d_unnorm
,
64 mp_limb_t dinv
, int shift
)
66 mp_limb_t ahigh
, qhigh
, r
;
73 ASSERT (d_unnorm
!= 0);
78 count_leading_zeros (want_shift
, d_unnorm
);
79 ASSERT (shift
== want_shift
);
80 invert_limb (want_dinv
, d_unnorm
<< shift
);
81 ASSERT (dinv
== want_dinv
);
84 /* FIXME: What's the correct overlap rule when xsize!=0? */
85 ASSERT (MPN_SAME_OR_SEPARATE_P (qp
+xsize
, ap
, size
));
88 d
= d_unnorm
<< shift
;
89 qp
+= (size
+ xsize
- 1); /* dest high limb */
93 /* High quotient limb is 0 or 1, and skip a divide step. */
96 r
= (qhigh
? r
-d
: r
);
100 for (i
= size
-1; i
>= 0; i
--)
103 udiv_qrnnd_preinv (*qp
, r
, r
, n0
, d
, dinv
);
110 if (ahigh
< d_unnorm
)
120 r
|= n1
>> (GMP_LIMB_BITS
- shift
);
122 for (i
= size
-2; i
>= 0; i
--)
126 udiv_qrnnd_preinv (*qp
, r
, r
,
127 ((n1
<< shift
) | (n0
>> (GMP_LIMB_BITS
- shift
))),
132 udiv_qrnnd_preinv (*qp
, r
, r
, n1
<< shift
, d
, dinv
);
137 for (i
= 0; i
< xsize
; i
++)
139 udiv_qrnnd_preinv (*qp
, r
, r
, CNST_LIMB(0), d
, dinv
);
146 #endif /* USE_PREINV_DIVREM_1 */