github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/mpn/generic/div_qr_1.c (about) 1 /* mpn_div_qr_1 -- mpn by limb division. 2 3 Contributed to the GNU project by Niels Möller and Torbjörn Granlund 4 5 Copyright 1991, 1993, 1994, 1996, 1998-2000, 2002, 2003, 2013 Free Software 6 Foundation, Inc. 7 8 This file is part of the GNU MP Library. 9 10 The GNU MP Library is free software; you can redistribute it and/or modify 11 it under the terms of either: 12 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. 16 17 or 18 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. 22 23 or both in parallel, as here. 24 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. 29 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/. */ 33 34 #include "gmp.h" 35 #include "gmp-impl.h" 36 #include "longlong.h" 37 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 44 45 #if GMP_NAIL_BITS > 0 46 #error Nail bits not supported 47 #endif 48 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) 54 { 55 unsigned cnt; 56 mp_limb_t uh; 57 58 ASSERT (n > 0); 59 ASSERT (d > 0); 60 61 if (d & GMP_NUMB_HIGHBIT) 62 { 63 /* Normalized case */ 64 mp_limb_t dinv, q; 65 66 uh = up[--n]; 67 68 q = (uh >= d); 69 *qh = q; 70 uh -= (-q) & d; 71 72 if (BELOW_THRESHOLD (n, DIV_QR_1_NORM_THRESHOLD)) 73 { 74 cnt = 0; 75 plain: 76 while (n > 0) 77 { 78 mp_limb_t ul = up[--n]; 79 udiv_qrnnd (qp[n], uh, uh, ul, d); 80 } 81 return uh >> cnt; 82 } 83 invert_limb (dinv, d); 84 return mpn_div_qr_1n_pi1 (qp, up, n, uh, d, dinv); 85 } 86 else 87 { 88 /* Unnormalized case */ 89 mp_limb_t dinv, ul; 90 91 if (! UDIV_NEEDS_NORMALIZATION 92 && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD)) 93 { 94 uh = up[--n]; 95 udiv_qrnnd (*qh, uh, CNST_LIMB(0), uh, d); 96 cnt = 0; 97 goto plain; 98 } 99 100 count_leading_zeros (cnt, d); 101 d <<= cnt; 102 103 #if HAVE_NATIVE_div_qr_1u_pi1 104 /* FIXME: Call loop doing on-the-fly normalization */ 105 #endif 106 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); 113 114 if (UDIV_NEEDS_NORMALIZATION 115 && BELOW_THRESHOLD (n, DIV_QR_1_UNNORM_THRESHOLD)) 116 { 117 udiv_qrnnd (*qh, uh, uh, ul, d); 118 up = qp; 119 goto plain; 120 } 121 invert_limb (dinv, d); 122 123 udiv_qrnnd_preinv (*qh, uh, uh, ul, d, dinv); 124 return mpn_div_qr_1n_pi1 (qp, qp, n, uh, d, dinv) >> cnt; 125 } 126 }