1 /* mpn_div_qr_1 -- mpn by limb division.
3 Contributed to the GNU project by Niels Möller and Torbjörn Granlund
5 Copyright 1991, 1993, 1994, 1996, 1998-2000, 2002, 2003, 2013 Free Software
8 This file is part of the GNU MP Library.
10 The GNU MP Library is free software; you can redistribute it and/or modify
11 it under the terms of either:
13 * the GNU Lesser General Public License as published by the Free
14 Software Foundation; either version 3 of the License, or (at your
15 option) any later version.
19 * the GNU General Public License as published by the Free Software
20 Foundation; either version 2 of the License, or (at your option) any
23 or both in parallel, as here.
25 The GNU MP Library is distributed in the hope that it will be useful, but
26 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30 You should have received copies of the GNU General Public License and the
31 GNU Lesser General Public License along with the GNU MP Library. If not,
32 see https://www.gnu.org/licenses/. */
38 #ifndef DIV_QR_1_NORM_THRESHOLD
39 #define DIV_QR_1_NORM_THRESHOLD 3
41 #ifndef DIV_QR_1_UNNORM_THRESHOLD
42 #define DIV_QR_1_UNNORM_THRESHOLD 3
46 #error Nail bits not supported
49 /* Divides {up, n} by d. Writes the n-1 low quotient limbs at {qp,
50 * n-1}, and the high quote limb at *qh. Returns remainder. */
52 mpn_div_qr_1 (mp_ptr qp
, mp_limb_t
*qh
, mp_srcptr up
, mp_size_t n
,
61 if (d
& GMP_NUMB_HIGHBIT
)
72 if (BELOW_THRESHOLD (n
, DIV_QR_1_NORM_THRESHOLD
))
78 mp_limb_t ul
= up
[--n
];
79 udiv_qrnnd (qp
[n
], uh
, uh
, ul
, d
);
83 invert_limb (dinv
, d
);
84 return mpn_div_qr_1n_pi1 (qp
, up
, n
, uh
, d
, dinv
);
88 /* Unnormalized case */
91 if (! UDIV_NEEDS_NORMALIZATION
92 && BELOW_THRESHOLD (n
, DIV_QR_1_UNNORM_THRESHOLD
))
95 udiv_qrnnd (*qh
, uh
, CNST_LIMB(0), uh
, d
);
100 count_leading_zeros (cnt
, d
);
103 #if HAVE_NATIVE_div_qr_1u_pi1
104 /* FIXME: Call loop doing on-the-fly normalization */
107 /* Shift up front, use qp area for shifted copy. A bit messy,
108 since we have only n-1 limbs available, and shift the high
111 ul
= (uh
<< cnt
) | mpn_lshift (qp
, up
, n
, cnt
);
112 uh
>>= (GMP_LIMB_BITS
- cnt
);
114 if (UDIV_NEEDS_NORMALIZATION
115 && BELOW_THRESHOLD (n
, DIV_QR_1_UNNORM_THRESHOLD
))
117 udiv_qrnnd (*qh
, uh
, uh
, ul
, d
);
121 invert_limb (dinv
, d
);
123 udiv_qrnnd_preinv (*qh
, uh
, uh
, ul
, d
, dinv
);
124 return mpn_div_qr_1n_pi1 (qp
, qp
, n
, uh
, d
, dinv
) >> cnt
;