lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* mpn_divmod_1(quot_ptr, dividend_ptr, dividend_size, divisor_limb) -- |
| 2 | Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB. |
| 3 | Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR. |
| 4 | Return the single-limb remainder. |
| 5 | There are no constraints on the value of the divisor. |
| 6 | |
| 7 | QUOT_PTR and DIVIDEND_PTR might point to the same limb. |
| 8 | |
| 9 | Copyright (C) 1991-2015 Free Software Foundation, Inc. |
| 10 | |
| 11 | This file is part of the GNU MP Library. |
| 12 | |
| 13 | The GNU MP Library is free software; you can redistribute it and/or modify |
| 14 | it under the terms of the GNU Lesser General Public License as published by |
| 15 | the Free Software Foundation; either version 2.1 of the License, or (at your |
| 16 | option) any later version. |
| 17 | |
| 18 | The GNU MP Library is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 20 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 21 | License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU Lesser General Public License |
| 24 | along with the GNU MP Library; see the file COPYING.LIB. If not, see |
| 25 | <http://www.gnu.org/licenses/>. */ |
| 26 | |
| 27 | #include <gmp.h> |
| 28 | #include "gmp-impl.h" |
| 29 | #include "longlong.h" |
| 30 | |
| 31 | #ifndef UMUL_TIME |
| 32 | #define UMUL_TIME 1 |
| 33 | #endif |
| 34 | |
| 35 | #ifndef UDIV_TIME |
| 36 | #define UDIV_TIME UMUL_TIME |
| 37 | #endif |
| 38 | |
| 39 | /* FIXME: We should be using invert_limb (or invert_normalized_limb) |
| 40 | here (not udiv_qrnnd). */ |
| 41 | |
| 42 | mp_limb_t |
| 43 | #if __STDC__ |
| 44 | mpn_divmod_1 (mp_ptr quot_ptr, |
| 45 | mp_srcptr dividend_ptr, mp_size_t dividend_size, |
| 46 | mp_limb_t divisor_limb) |
| 47 | #else |
| 48 | mpn_divmod_1 (quot_ptr, dividend_ptr, dividend_size, divisor_limb) |
| 49 | mp_ptr quot_ptr; |
| 50 | mp_srcptr dividend_ptr; |
| 51 | mp_size_t dividend_size; |
| 52 | mp_limb_t divisor_limb; |
| 53 | #endif |
| 54 | { |
| 55 | mp_size_t i; |
| 56 | mp_limb_t n1, n0, r; |
| 57 | mp_limb_t dummy __attribute__ ((unused)); |
| 58 | |
| 59 | /* ??? Should this be handled at all? Rely on callers? */ |
| 60 | if (dividend_size == 0) |
| 61 | return 0; |
| 62 | |
| 63 | /* If multiplication is much faster than division, and the |
| 64 | dividend is large, pre-invert the divisor, and use |
| 65 | only multiplications in the inner loop. */ |
| 66 | |
| 67 | /* This test should be read: |
| 68 | Does it ever help to use udiv_qrnnd_preinv? |
| 69 | && Does what we save compensate for the inversion overhead? */ |
| 70 | if (UDIV_TIME > (2 * UMUL_TIME + 6) |
| 71 | && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME) |
| 72 | { |
| 73 | int normalization_steps; |
| 74 | |
| 75 | count_leading_zeros (normalization_steps, divisor_limb); |
| 76 | if (normalization_steps != 0) |
| 77 | { |
| 78 | mp_limb_t divisor_limb_inverted; |
| 79 | |
| 80 | divisor_limb <<= normalization_steps; |
| 81 | |
| 82 | /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The |
| 83 | result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the |
| 84 | most significant bit (with weight 2**N) implicit. */ |
| 85 | |
| 86 | /* Special case for DIVISOR_LIMB == 100...000. */ |
| 87 | if (divisor_limb << 1 == 0) |
| 88 | divisor_limb_inverted = ~(mp_limb_t) 0; |
| 89 | else |
| 90 | udiv_qrnnd (divisor_limb_inverted, dummy, |
| 91 | -divisor_limb, 0, divisor_limb); |
| 92 | |
| 93 | n1 = dividend_ptr[dividend_size - 1]; |
| 94 | r = n1 >> (BITS_PER_MP_LIMB - normalization_steps); |
| 95 | |
| 96 | /* Possible optimization: |
| 97 | if (r == 0 |
| 98 | && divisor_limb > ((n1 << normalization_steps) |
| 99 | | (dividend_ptr[dividend_size - 2] >> ...))) |
| 100 | ...one division less... */ |
| 101 | |
| 102 | for (i = dividend_size - 2; i >= 0; i--) |
| 103 | { |
| 104 | n0 = dividend_ptr[i]; |
| 105 | udiv_qrnnd_preinv (quot_ptr[i + 1], r, r, |
| 106 | ((n1 << normalization_steps) |
| 107 | | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))), |
| 108 | divisor_limb, divisor_limb_inverted); |
| 109 | n1 = n0; |
| 110 | } |
| 111 | udiv_qrnnd_preinv (quot_ptr[0], r, r, |
| 112 | n1 << normalization_steps, |
| 113 | divisor_limb, divisor_limb_inverted); |
| 114 | return r >> normalization_steps; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | mp_limb_t divisor_limb_inverted; |
| 119 | |
| 120 | /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The |
| 121 | result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the |
| 122 | most significant bit (with weight 2**N) implicit. */ |
| 123 | |
| 124 | /* Special case for DIVISOR_LIMB == 100...000. */ |
| 125 | if (divisor_limb << 1 == 0) |
| 126 | divisor_limb_inverted = ~(mp_limb_t) 0; |
| 127 | else |
| 128 | udiv_qrnnd (divisor_limb_inverted, dummy, |
| 129 | -divisor_limb, 0, divisor_limb); |
| 130 | |
| 131 | i = dividend_size - 1; |
| 132 | r = dividend_ptr[i]; |
| 133 | |
| 134 | if (r >= divisor_limb) |
| 135 | r = 0; |
| 136 | else |
| 137 | { |
| 138 | quot_ptr[i] = 0; |
| 139 | i--; |
| 140 | } |
| 141 | |
| 142 | for (; i >= 0; i--) |
| 143 | { |
| 144 | n0 = dividend_ptr[i]; |
| 145 | udiv_qrnnd_preinv (quot_ptr[i], r, r, |
| 146 | n0, divisor_limb, divisor_limb_inverted); |
| 147 | } |
| 148 | return r; |
| 149 | } |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | if (UDIV_NEEDS_NORMALIZATION) |
| 154 | { |
| 155 | int normalization_steps; |
| 156 | |
| 157 | count_leading_zeros (normalization_steps, divisor_limb); |
| 158 | if (normalization_steps != 0) |
| 159 | { |
| 160 | divisor_limb <<= normalization_steps; |
| 161 | |
| 162 | n1 = dividend_ptr[dividend_size - 1]; |
| 163 | r = n1 >> (BITS_PER_MP_LIMB - normalization_steps); |
| 164 | |
| 165 | /* Possible optimization: |
| 166 | if (r == 0 |
| 167 | && divisor_limb > ((n1 << normalization_steps) |
| 168 | | (dividend_ptr[dividend_size - 2] >> ...))) |
| 169 | ...one division less... */ |
| 170 | |
| 171 | for (i = dividend_size - 2; i >= 0; i--) |
| 172 | { |
| 173 | n0 = dividend_ptr[i]; |
| 174 | udiv_qrnnd (quot_ptr[i + 1], r, r, |
| 175 | ((n1 << normalization_steps) |
| 176 | | (n0 >> (BITS_PER_MP_LIMB - normalization_steps))), |
| 177 | divisor_limb); |
| 178 | n1 = n0; |
| 179 | } |
| 180 | udiv_qrnnd (quot_ptr[0], r, r, |
| 181 | n1 << normalization_steps, |
| 182 | divisor_limb); |
| 183 | return r >> normalization_steps; |
| 184 | } |
| 185 | } |
| 186 | /* No normalization needed, either because udiv_qrnnd doesn't require |
| 187 | it, or because DIVISOR_LIMB is already normalized. */ |
| 188 | |
| 189 | i = dividend_size - 1; |
| 190 | r = dividend_ptr[i]; |
| 191 | |
| 192 | if (r >= divisor_limb) |
| 193 | r = 0; |
| 194 | else |
| 195 | { |
| 196 | quot_ptr[i] = 0; |
| 197 | i--; |
| 198 | } |
| 199 | |
| 200 | for (; i >= 0; i--) |
| 201 | { |
| 202 | n0 = dividend_ptr[i]; |
| 203 | udiv_qrnnd (quot_ptr[i], r, r, n0, divisor_limb); |
| 204 | } |
| 205 | return r; |
| 206 | } |
| 207 | } |