beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / div_qr_1.c
blob09401ac5358170549e5c75c4ba8de4084024445b
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
6 Foundation, Inc.
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
21 later version.
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
28 for more details.
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/. */
34 #include "gmp.h"
35 #include "gmp-impl.h"
36 #include "longlong.h"
38 #ifndef DIV_QR_1_NORM_THRESHOLD
39 #define DIV_QR_1_NORM_THRESHOLD 3
40 #endif
41 #ifndef DIV_QR_1_UNNORM_THRESHOLD
42 #define DIV_QR_1_UNNORM_THRESHOLD 3
43 #endif
45 #if GMP_NAIL_BITS > 0
46 #error Nail bits not supported
47 #endif
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. */
51 mp_limb_t
52 mpn_div_qr_1 (mp_ptr qp, mp_limb_t *qh, mp_srcptr up, mp_size_t n,
53 mp_limb_t d)
55 unsigned cnt;
56 mp_limb_t uh;
58 ASSERT (n > 0);
59 ASSERT (d > 0);
61 if (d & GMP_NUMB_HIGHBIT)
63 /* Normalized case */
64 mp_limb_t dinv, q;
66 uh = up[--n];
68 q = (uh >= d);
69 *qh = q;
70 uh -= (-q) & d;
72 if (BELOW_THRESHOLD (n, DIV_QR_1_NORM_THRESHOLD))
74 cnt = 0;
75 plain:
76 while (n > 0)
78 mp_limb_t ul = up[--n];
79 udiv_qrnnd (qp[n], uh, uh, ul, d);
81 return uh >> cnt;
83 invert_limb (dinv, d);
84 return mpn_div_qr_1n_pi1 (qp, up, n, uh, d, dinv);
86 else
88 /* Unnormalized case */
89 mp_limb_t dinv, ul;
91 if (! UDIV_NEEDS_NORMALIZATION
92 && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD))
94 uh = up[--n];
95 udiv_qrnnd (*qh, uh, CNST_LIMB(0), uh, d);
96 cnt = 0;
97 goto plain;
100 count_leading_zeros (cnt, d);
101 d <<= cnt;
103 #if HAVE_NATIVE_div_qr_1u_pi1
104 /* FIXME: Call loop doing on-the-fly normalization */
105 #endif
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
109 limb manually. */
110 uh = up[--n];
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);
118 up = qp;
119 goto plain;
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;