1 1.1 mrg /* mpn_div_qr_1 -- mpn by limb division. 2 1.1 mrg 3 1.1 mrg Contributed to the GNU project by Niels Mller and Torbjrn Granlund 4 1.1 mrg 5 1.1 mrg Copyright 1991, 1993, 1994, 1996, 1998-2000, 2002, 2003, 2013 Free Software 6 1.1 mrg Foundation, Inc. 7 1.1 mrg 8 1.1 mrg This file is part of the GNU MP Library. 9 1.1 mrg 10 1.1 mrg The GNU MP Library is free software; you can redistribute it and/or modify 11 1.1 mrg it under the terms of either: 12 1.1 mrg 13 1.1 mrg * the GNU Lesser General Public License as published by the Free 14 1.1 mrg Software Foundation; either version 3 of the License, or (at your 15 1.1 mrg option) any later version. 16 1.1 mrg 17 1.1 mrg or 18 1.1 mrg 19 1.1 mrg * the GNU General Public License as published by the Free Software 20 1.1 mrg Foundation; either version 2 of the License, or (at your option) any 21 1.1 mrg later version. 22 1.1 mrg 23 1.1 mrg or both in parallel, as here. 24 1.1 mrg 25 1.1 mrg The GNU MP Library is distributed in the hope that it will be useful, but 26 1.1 mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 27 1.1 mrg or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 28 1.1 mrg for more details. 29 1.1 mrg 30 1.1 mrg You should have received copies of the GNU General Public License and the 31 1.1 mrg GNU Lesser General Public License along with the GNU MP Library. If not, 32 1.1 mrg see https://www.gnu.org/licenses/. */ 33 1.1 mrg 34 1.1 mrg #include "gmp-impl.h" 35 1.1 mrg #include "longlong.h" 36 1.1 mrg 37 1.1 mrg #ifndef DIV_QR_1_NORM_THRESHOLD 38 1.1 mrg #define DIV_QR_1_NORM_THRESHOLD 3 39 1.1 mrg #endif 40 1.1 mrg #ifndef DIV_QR_1_UNNORM_THRESHOLD 41 1.1 mrg #define DIV_QR_1_UNNORM_THRESHOLD 3 42 1.1 mrg #endif 43 1.1 mrg 44 1.1 mrg #if GMP_NAIL_BITS > 0 45 1.1 mrg #error Nail bits not supported 46 1.1 mrg #endif 47 1.1 mrg 48 1.1 mrg /* Divides {up, n} by d. Writes the n-1 low quotient limbs at {qp, 49 1.1.1.2 mrg * n-1}, and the high quotient limb at *qh. Returns remainder. */ 50 1.1 mrg mp_limb_t 51 1.1 mrg mpn_div_qr_1 (mp_ptr qp, mp_limb_t *qh, mp_srcptr up, mp_size_t n, 52 1.1 mrg mp_limb_t d) 53 1.1 mrg { 54 1.1 mrg unsigned cnt; 55 1.1 mrg mp_limb_t uh; 56 1.1 mrg 57 1.1 mrg ASSERT (n > 0); 58 1.1 mrg ASSERT (d > 0); 59 1.1 mrg 60 1.1 mrg if (d & GMP_NUMB_HIGHBIT) 61 1.1 mrg { 62 1.1 mrg /* Normalized case */ 63 1.1 mrg mp_limb_t dinv, q; 64 1.1 mrg 65 1.1 mrg uh = up[--n]; 66 1.1 mrg 67 1.1 mrg q = (uh >= d); 68 1.1 mrg *qh = q; 69 1.1 mrg uh -= (-q) & d; 70 1.1 mrg 71 1.1 mrg if (BELOW_THRESHOLD (n, DIV_QR_1_NORM_THRESHOLD)) 72 1.1 mrg { 73 1.1 mrg cnt = 0; 74 1.1 mrg plain: 75 1.1 mrg while (n > 0) 76 1.1 mrg { 77 1.1 mrg mp_limb_t ul = up[--n]; 78 1.1 mrg udiv_qrnnd (qp[n], uh, uh, ul, d); 79 1.1 mrg } 80 1.1 mrg return uh >> cnt; 81 1.1 mrg } 82 1.1 mrg invert_limb (dinv, d); 83 1.1 mrg return mpn_div_qr_1n_pi1 (qp, up, n, uh, d, dinv); 84 1.1 mrg } 85 1.1 mrg else 86 1.1 mrg { 87 1.1 mrg /* Unnormalized case */ 88 1.1 mrg mp_limb_t dinv, ul; 89 1.1 mrg 90 1.1 mrg if (! UDIV_NEEDS_NORMALIZATION 91 1.1 mrg && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD)) 92 1.1 mrg { 93 1.1 mrg uh = up[--n]; 94 1.1 mrg udiv_qrnnd (*qh, uh, CNST_LIMB(0), uh, d); 95 1.1 mrg cnt = 0; 96 1.1 mrg goto plain; 97 1.1 mrg } 98 1.1 mrg 99 1.1 mrg count_leading_zeros (cnt, d); 100 1.1 mrg d <<= cnt; 101 1.1 mrg 102 1.1.1.2 mrg #if HAVE_NATIVE_mpn_div_qr_1u_pi1 103 1.1 mrg /* FIXME: Call loop doing on-the-fly normalization */ 104 1.1 mrg #endif 105 1.1 mrg 106 1.1 mrg /* Shift up front, use qp area for shifted copy. A bit messy, 107 1.1 mrg since we have only n-1 limbs available, and shift the high 108 1.1 mrg limb manually. */ 109 1.1 mrg uh = up[--n]; 110 1.1 mrg ul = (uh << cnt) | mpn_lshift (qp, up, n, cnt); 111 1.1 mrg uh >>= (GMP_LIMB_BITS - cnt); 112 1.1 mrg 113 1.1 mrg if (UDIV_NEEDS_NORMALIZATION 114 1.1 mrg && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD)) 115 1.1 mrg { 116 1.1 mrg udiv_qrnnd (*qh, uh, uh, ul, d); 117 1.1 mrg up = qp; 118 1.1 mrg goto plain; 119 1.1 mrg } 120 1.1 mrg invert_limb (dinv, d); 121 1.1 mrg 122 1.1 mrg udiv_qrnnd_preinv (*qh, uh, uh, ul, d, dinv); 123 1.1 mrg return mpn_div_qr_1n_pi1 (qp, qp, n, uh, d, dinv) >> cnt; 124 1.1 mrg } 125 1.1 mrg } 126