raw
smg_comms_c_wrappers    1 /* mpihelp-div.c  -  MPI helper functions
smg_comms_c_wrappers 2 * Modified by No Such Labs. (C) 2015. See README.
smg_comms_c_wrappers 3 *
smg_comms_c_wrappers 4 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
smg_comms_c_wrappers 5 * SHA256(gnupg-1.4.10.tar.gz):
smg_comms_c_wrappers 6 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
smg_comms_c_wrappers 7 * (C) 1994-2005 Free Software Foundation, Inc.
smg_comms_c_wrappers 8 *
smg_comms_c_wrappers 9 * This program is free software: you can redistribute it and/or modify
smg_comms_c_wrappers 10 * it under the terms of the GNU General Public License as published by
smg_comms_c_wrappers 11 * the Free Software Foundation, either version 3 of the License, or
smg_comms_c_wrappers 12 * (at your option) any later version.
smg_comms_c_wrappers 13 *
smg_comms_c_wrappers 14 * This program is distributed in the hope that it will be useful,
smg_comms_c_wrappers 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
smg_comms_c_wrappers 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
smg_comms_c_wrappers 17 * GNU General Public License for more details.
smg_comms_c_wrappers 18 *
smg_comms_c_wrappers 19 * You should have received a copy of the GNU General Public License
smg_comms_c_wrappers 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
smg_comms_c_wrappers 21 */
smg_comms_c_wrappers 22
smg_comms_c_wrappers 23 #include <stdio.h>
smg_comms_c_wrappers 24 #include <stdlib.h>
smg_comms_c_wrappers 25
smg_comms_c_wrappers 26 #include "knobs.h"
smg_comms_c_wrappers 27 #include "mpi-internal.h"
smg_comms_c_wrappers 28 #include "longlong.h"
smg_comms_c_wrappers 29
smg_comms_c_wrappers 30 #ifndef UMUL_TIME
smg_comms_c_wrappers 31 #define UMUL_TIME 1
smg_comms_c_wrappers 32 #endif
smg_comms_c_wrappers 33 #ifndef UDIV_TIME
smg_comms_c_wrappers 34 #define UDIV_TIME UMUL_TIME
smg_comms_c_wrappers 35 #endif
smg_comms_c_wrappers 36
smg_comms_c_wrappers 37 /* FIXME: We should be using invert_limb (or invert_normalized_limb)
smg_comms_c_wrappers 38 * here (not udiv_qrnnd).
smg_comms_c_wrappers 39 */
smg_comms_c_wrappers 40
smg_comms_c_wrappers 41 mpi_limb_t
smg_comms_c_wrappers 42 mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
smg_comms_c_wrappers 43 mpi_limb_t divisor_limb)
smg_comms_c_wrappers 44 {
smg_comms_c_wrappers 45 mpi_size_t i;
smg_comms_c_wrappers 46 mpi_limb_t n1, n0, r;
smg_comms_c_wrappers 47 int dummy;
smg_comms_c_wrappers 48
smg_comms_c_wrappers 49 /* Botch: Should this be handled at all? Rely on callers? */
smg_comms_c_wrappers 50 if( !dividend_size )
smg_comms_c_wrappers 51 return 0;
smg_comms_c_wrappers 52
smg_comms_c_wrappers 53 /* If multiplication is much faster than division, and the
smg_comms_c_wrappers 54 * dividend is large, pre-invert the divisor, and use
smg_comms_c_wrappers 55 * only multiplications in the inner loop.
smg_comms_c_wrappers 56 *
smg_comms_c_wrappers 57 * This test should be read:
smg_comms_c_wrappers 58 * Does it ever help to use udiv_qrnnd_preinv?
smg_comms_c_wrappers 59 * && Does what we save compensate for the inversion overhead?
smg_comms_c_wrappers 60 */
smg_comms_c_wrappers 61 if( UDIV_TIME > (2 * UMUL_TIME + 6)
smg_comms_c_wrappers 62 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
smg_comms_c_wrappers 63 int normalization_steps;
smg_comms_c_wrappers 64
smg_comms_c_wrappers 65 count_leading_zeros( normalization_steps, divisor_limb );
smg_comms_c_wrappers 66 if( normalization_steps ) {
smg_comms_c_wrappers 67 mpi_limb_t divisor_limb_inverted;
smg_comms_c_wrappers 68
smg_comms_c_wrappers 69 divisor_limb <<= normalization_steps;
smg_comms_c_wrappers 70
smg_comms_c_wrappers 71 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
smg_comms_c_wrappers 72 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
smg_comms_c_wrappers 73 * most significant bit (with weight 2**N) implicit.
smg_comms_c_wrappers 74 *
smg_comms_c_wrappers 75 * Special case for DIVISOR_LIMB == 100...000.
smg_comms_c_wrappers 76 */
smg_comms_c_wrappers 77 if( !(divisor_limb << 1) )
smg_comms_c_wrappers 78 divisor_limb_inverted = ~(mpi_limb_t)0;
smg_comms_c_wrappers 79 else
smg_comms_c_wrappers 80 udiv_qrnnd(divisor_limb_inverted, dummy,
smg_comms_c_wrappers 81 -divisor_limb, 0, divisor_limb);
smg_comms_c_wrappers 82
smg_comms_c_wrappers 83 n1 = dividend_ptr[dividend_size - 1];
smg_comms_c_wrappers 84 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
smg_comms_c_wrappers 85
smg_comms_c_wrappers 86 /* Possible optimization:
smg_comms_c_wrappers 87 * if (r == 0
smg_comms_c_wrappers 88 * && divisor_limb > ((n1 << normalization_steps)
smg_comms_c_wrappers 89 * | (dividend_ptr[dividend_size - 2] >> ...)))
smg_comms_c_wrappers 90 * ...one division less...
smg_comms_c_wrappers 91 */
smg_comms_c_wrappers 92 for( i = dividend_size - 2; i >= 0; i--) {
smg_comms_c_wrappers 93 n0 = dividend_ptr[i];
smg_comms_c_wrappers 94 UDIV_QRNND_PREINV(dummy, r, r,
smg_comms_c_wrappers 95 ((n1 << normalization_steps)
smg_comms_c_wrappers 96 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
smg_comms_c_wrappers 97 divisor_limb, divisor_limb_inverted);
smg_comms_c_wrappers 98 n1 = n0;
smg_comms_c_wrappers 99 }
smg_comms_c_wrappers 100 UDIV_QRNND_PREINV(dummy, r, r,
smg_comms_c_wrappers 101 n1 << normalization_steps,
smg_comms_c_wrappers 102 divisor_limb, divisor_limb_inverted);
smg_comms_c_wrappers 103 return r >> normalization_steps;
smg_comms_c_wrappers 104 }
smg_comms_c_wrappers 105 else {
smg_comms_c_wrappers 106 mpi_limb_t divisor_limb_inverted;
smg_comms_c_wrappers 107
smg_comms_c_wrappers 108 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
smg_comms_c_wrappers 109 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
smg_comms_c_wrappers 110 * most significant bit (with weight 2**N) implicit.
smg_comms_c_wrappers 111 *
smg_comms_c_wrappers 112 * Special case for DIVISOR_LIMB == 100...000.
smg_comms_c_wrappers 113 */
smg_comms_c_wrappers 114 if( !(divisor_limb << 1) )
smg_comms_c_wrappers 115 divisor_limb_inverted = ~(mpi_limb_t)0;
smg_comms_c_wrappers 116 else
smg_comms_c_wrappers 117 udiv_qrnnd(divisor_limb_inverted, dummy,
smg_comms_c_wrappers 118 -divisor_limb, 0, divisor_limb);
smg_comms_c_wrappers 119
smg_comms_c_wrappers 120 i = dividend_size - 1;
smg_comms_c_wrappers 121 r = dividend_ptr[i];
smg_comms_c_wrappers 122
smg_comms_c_wrappers 123 if( r >= divisor_limb )
smg_comms_c_wrappers 124 r = 0;
smg_comms_c_wrappers 125 else
smg_comms_c_wrappers 126 i--;
smg_comms_c_wrappers 127
smg_comms_c_wrappers 128 for( ; i >= 0; i--) {
smg_comms_c_wrappers 129 n0 = dividend_ptr[i];
smg_comms_c_wrappers 130 UDIV_QRNND_PREINV(dummy, r, r,
smg_comms_c_wrappers 131 n0, divisor_limb, divisor_limb_inverted);
smg_comms_c_wrappers 132 }
smg_comms_c_wrappers 133 return r;
smg_comms_c_wrappers 134 }
smg_comms_c_wrappers 135 }
smg_comms_c_wrappers 136 else {
smg_comms_c_wrappers 137 if( UDIV_NEEDS_NORMALIZATION ) {
smg_comms_c_wrappers 138 int normalization_steps;
smg_comms_c_wrappers 139
smg_comms_c_wrappers 140 count_leading_zeros(normalization_steps, divisor_limb);
smg_comms_c_wrappers 141 if( normalization_steps ) {
smg_comms_c_wrappers 142 divisor_limb <<= normalization_steps;
smg_comms_c_wrappers 143
smg_comms_c_wrappers 144 n1 = dividend_ptr[dividend_size - 1];
smg_comms_c_wrappers 145 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
smg_comms_c_wrappers 146
smg_comms_c_wrappers 147 /* Possible optimization:
smg_comms_c_wrappers 148 * if (r == 0
smg_comms_c_wrappers 149 * && divisor_limb > ((n1 << normalization_steps)
smg_comms_c_wrappers 150 * | (dividend_ptr[dividend_size - 2] >> ...)))
smg_comms_c_wrappers 151 * ...one division less...
smg_comms_c_wrappers 152 */
smg_comms_c_wrappers 153 for(i = dividend_size - 2; i >= 0; i--) {
smg_comms_c_wrappers 154 n0 = dividend_ptr[i];
smg_comms_c_wrappers 155 udiv_qrnnd (dummy, r, r,
smg_comms_c_wrappers 156 ((n1 << normalization_steps)
smg_comms_c_wrappers 157 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
smg_comms_c_wrappers 158 divisor_limb);
smg_comms_c_wrappers 159 n1 = n0;
smg_comms_c_wrappers 160 }
smg_comms_c_wrappers 161 udiv_qrnnd (dummy, r, r,
smg_comms_c_wrappers 162 n1 << normalization_steps,
smg_comms_c_wrappers 163 divisor_limb);
smg_comms_c_wrappers 164 return r >> normalization_steps;
smg_comms_c_wrappers 165 }
smg_comms_c_wrappers 166 }
smg_comms_c_wrappers 167 /* No normalization needed, either because udiv_qrnnd doesn't require
smg_comms_c_wrappers 168 * it, or because DIVISOR_LIMB is already normalized. */
smg_comms_c_wrappers 169 i = dividend_size - 1;
smg_comms_c_wrappers 170 r = dividend_ptr[i];
smg_comms_c_wrappers 171
smg_comms_c_wrappers 172 if(r >= divisor_limb)
smg_comms_c_wrappers 173 r = 0;
smg_comms_c_wrappers 174 else
smg_comms_c_wrappers 175 i--;
smg_comms_c_wrappers 176
smg_comms_c_wrappers 177 for(; i >= 0; i--) {
smg_comms_c_wrappers 178 n0 = dividend_ptr[i];
smg_comms_c_wrappers 179 udiv_qrnnd (dummy, r, r, n0, divisor_limb);
smg_comms_c_wrappers 180 }
smg_comms_c_wrappers 181 return r;
smg_comms_c_wrappers 182 }
smg_comms_c_wrappers 183 }
smg_comms_c_wrappers 184
smg_comms_c_wrappers 185 /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
smg_comms_c_wrappers 186 * the NSIZE-DSIZE least significant quotient limbs at QP
smg_comms_c_wrappers 187 * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
smg_comms_c_wrappers 188 * non-zero, generate that many fraction bits and append them after the
smg_comms_c_wrappers 189 * other quotient limbs.
smg_comms_c_wrappers 190 * Return the most significant limb of the quotient, this is always 0 or 1.
smg_comms_c_wrappers 191 *
smg_comms_c_wrappers 192 * Preconditions:
smg_comms_c_wrappers 193 * 0. NSIZE >= DSIZE.
smg_comms_c_wrappers 194 * 1. The most significant bit of the divisor must be set.
smg_comms_c_wrappers 195 * 2. QP must either not overlap with the input operands at all, or
smg_comms_c_wrappers 196 * QP + DSIZE >= NP must hold true. (This means that it's
smg_comms_c_wrappers 197 * possible to put the quotient in the high part of NUM, right after the
smg_comms_c_wrappers 198 * remainder in NUM.
smg_comms_c_wrappers 199 * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
smg_comms_c_wrappers 200 */
smg_comms_c_wrappers 201
smg_comms_c_wrappers 202 mpi_limb_t
smg_comms_c_wrappers 203 mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
smg_comms_c_wrappers 204 mpi_ptr_t np, mpi_size_t nsize,
smg_comms_c_wrappers 205 mpi_ptr_t dp, mpi_size_t dsize)
smg_comms_c_wrappers 206 {
smg_comms_c_wrappers 207 mpi_limb_t most_significant_q_limb = 0;
smg_comms_c_wrappers 208
smg_comms_c_wrappers 209 switch(dsize) {
smg_comms_c_wrappers 210 case 0:
smg_comms_c_wrappers 211 /* We are asked to divide by zero, so go ahead and do it! (To make
smg_comms_c_wrappers 212 the compiler not remove this statement, return the value.) */
smg_comms_c_wrappers 213 return 1 / dsize;
smg_comms_c_wrappers 214
smg_comms_c_wrappers 215 case 1:
smg_comms_c_wrappers 216 {
smg_comms_c_wrappers 217 mpi_size_t i;
smg_comms_c_wrappers 218 mpi_limb_t n1;
smg_comms_c_wrappers 219 mpi_limb_t d;
smg_comms_c_wrappers 220
smg_comms_c_wrappers 221 d = dp[0];
smg_comms_c_wrappers 222 n1 = np[nsize - 1];
smg_comms_c_wrappers 223
smg_comms_c_wrappers 224 if( n1 >= d ) {
smg_comms_c_wrappers 225 n1 -= d;
smg_comms_c_wrappers 226 most_significant_q_limb = 1;
smg_comms_c_wrappers 227 }
smg_comms_c_wrappers 228
smg_comms_c_wrappers 229 qp += qextra_limbs;
smg_comms_c_wrappers 230 for( i = nsize - 2; i >= 0; i--)
smg_comms_c_wrappers 231 udiv_qrnnd( qp[i], n1, n1, np[i], d );
smg_comms_c_wrappers 232 qp -= qextra_limbs;
smg_comms_c_wrappers 233
smg_comms_c_wrappers 234 for( i = qextra_limbs - 1; i >= 0; i-- )
smg_comms_c_wrappers 235 udiv_qrnnd (qp[i], n1, n1, 0, d);
smg_comms_c_wrappers 236
smg_comms_c_wrappers 237 np[0] = n1;
smg_comms_c_wrappers 238 }
smg_comms_c_wrappers 239 break;
smg_comms_c_wrappers 240
smg_comms_c_wrappers 241 case 2:
smg_comms_c_wrappers 242 {
smg_comms_c_wrappers 243 mpi_size_t i;
smg_comms_c_wrappers 244 mpi_limb_t n1, n0, n2;
smg_comms_c_wrappers 245 mpi_limb_t d1, d0;
smg_comms_c_wrappers 246
smg_comms_c_wrappers 247 np += nsize - 2;
smg_comms_c_wrappers 248 d1 = dp[1];
smg_comms_c_wrappers 249 d0 = dp[0];
smg_comms_c_wrappers 250 n1 = np[1];
smg_comms_c_wrappers 251 n0 = np[0];
smg_comms_c_wrappers 252
smg_comms_c_wrappers 253 if( n1 >= d1 && (n1 > d1 || n0 >= d0) ) {
smg_comms_c_wrappers 254 sub_ddmmss (n1, n0, n1, n0, d1, d0);
smg_comms_c_wrappers 255 most_significant_q_limb = 1;
smg_comms_c_wrappers 256 }
smg_comms_c_wrappers 257
smg_comms_c_wrappers 258 for( i = qextra_limbs + nsize - 2 - 1; i >= 0; i-- ) {
smg_comms_c_wrappers 259 mpi_limb_t q;
smg_comms_c_wrappers 260 mpi_limb_t r;
smg_comms_c_wrappers 261
smg_comms_c_wrappers 262 if( i >= qextra_limbs )
smg_comms_c_wrappers 263 np--;
smg_comms_c_wrappers 264 else
smg_comms_c_wrappers 265 np[0] = 0;
smg_comms_c_wrappers 266
smg_comms_c_wrappers 267 if( n1 == d1 ) {
smg_comms_c_wrappers 268 /* Q should be either 111..111 or 111..110. Need special
smg_comms_c_wrappers 269 * treatment of this rare case as normal division would
smg_comms_c_wrappers 270 * give overflow. */
smg_comms_c_wrappers 271 q = ~(mpi_limb_t)0;
smg_comms_c_wrappers 272
smg_comms_c_wrappers 273 r = n0 + d1;
smg_comms_c_wrappers 274 if( r < d1 ) { /* Carry in the addition? */
smg_comms_c_wrappers 275 add_ssaaaa( n1, n0, r - d0, np[0], 0, d0 );
smg_comms_c_wrappers 276 qp[i] = q;
smg_comms_c_wrappers 277 continue;
smg_comms_c_wrappers 278 }
smg_comms_c_wrappers 279 n1 = d0 - (d0 != 0?1:0);
smg_comms_c_wrappers 280 n0 = -d0;
smg_comms_c_wrappers 281 }
smg_comms_c_wrappers 282 else {
smg_comms_c_wrappers 283 udiv_qrnnd (q, r, n1, n0, d1);
smg_comms_c_wrappers 284 umul_ppmm (n1, n0, d0, q);
smg_comms_c_wrappers 285 }
smg_comms_c_wrappers 286
smg_comms_c_wrappers 287 n2 = np[0];
smg_comms_c_wrappers 288 q_test:
smg_comms_c_wrappers 289 if( n1 > r || (n1 == r && n0 > n2) ) {
smg_comms_c_wrappers 290 /* The estimated Q was too large. */
smg_comms_c_wrappers 291 q--;
smg_comms_c_wrappers 292 sub_ddmmss (n1, n0, n1, n0, 0, d0);
smg_comms_c_wrappers 293 r += d1;
smg_comms_c_wrappers 294 if( r >= d1 ) /* If not carry, test Q again. */
smg_comms_c_wrappers 295 goto q_test;
smg_comms_c_wrappers 296 }
smg_comms_c_wrappers 297
smg_comms_c_wrappers 298 qp[i] = q;
smg_comms_c_wrappers 299 sub_ddmmss (n1, n0, r, n2, n1, n0);
smg_comms_c_wrappers 300 }
smg_comms_c_wrappers 301 np[1] = n1;
smg_comms_c_wrappers 302 np[0] = n0;
smg_comms_c_wrappers 303 }
smg_comms_c_wrappers 304 break;
smg_comms_c_wrappers 305
smg_comms_c_wrappers 306 default:
smg_comms_c_wrappers 307 {
smg_comms_c_wrappers 308 mpi_size_t i;
smg_comms_c_wrappers 309 mpi_limb_t dX, d1, n0;
smg_comms_c_wrappers 310
smg_comms_c_wrappers 311 np += nsize - dsize;
smg_comms_c_wrappers 312 dX = dp[dsize - 1];
smg_comms_c_wrappers 313 d1 = dp[dsize - 2];
smg_comms_c_wrappers 314 n0 = np[dsize - 1];
smg_comms_c_wrappers 315
smg_comms_c_wrappers 316 if( n0 >= dX ) {
smg_comms_c_wrappers 317 if(n0 > dX || mpihelp_cmp(np, dp, dsize - 1) >= 0 ) {
smg_comms_c_wrappers 318 mpihelp_sub_n(np, np, dp, dsize);
smg_comms_c_wrappers 319 n0 = np[dsize - 1];
smg_comms_c_wrappers 320 most_significant_q_limb = 1;
smg_comms_c_wrappers 321 }
smg_comms_c_wrappers 322 }
smg_comms_c_wrappers 323
smg_comms_c_wrappers 324 for( i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
smg_comms_c_wrappers 325 mpi_limb_t q;
smg_comms_c_wrappers 326 mpi_limb_t n1, n2;
smg_comms_c_wrappers 327 mpi_limb_t cy_limb;
smg_comms_c_wrappers 328
smg_comms_c_wrappers 329 if( i >= qextra_limbs ) {
smg_comms_c_wrappers 330 np--;
smg_comms_c_wrappers 331 n2 = np[dsize];
smg_comms_c_wrappers 332 }
smg_comms_c_wrappers 333 else {
smg_comms_c_wrappers 334 n2 = np[dsize - 1];
smg_comms_c_wrappers 335 MPN_COPY_DECR (np + 1, np, dsize - 1);
smg_comms_c_wrappers 336 np[0] = 0;
smg_comms_c_wrappers 337 }
smg_comms_c_wrappers 338
smg_comms_c_wrappers 339 if( n0 == dX ) {
smg_comms_c_wrappers 340 /* This might over-estimate q, but it's probably not worth
smg_comms_c_wrappers 341 * the extra code here to find out. */
smg_comms_c_wrappers 342 q = ~(mpi_limb_t)0;
smg_comms_c_wrappers 343 }
smg_comms_c_wrappers 344 else {
smg_comms_c_wrappers 345 mpi_limb_t r;
smg_comms_c_wrappers 346
smg_comms_c_wrappers 347 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
smg_comms_c_wrappers 348 umul_ppmm(n1, n0, d1, q);
smg_comms_c_wrappers 349
smg_comms_c_wrappers 350 while( n1 > r || (n1 == r && n0 > np[dsize - 2])) {
smg_comms_c_wrappers 351 q--;
smg_comms_c_wrappers 352 r += dX;
smg_comms_c_wrappers 353 if( r < dX ) /* I.e. "carry in previous addition?" */
smg_comms_c_wrappers 354 break;
smg_comms_c_wrappers 355 n1 -= n0 < d1;
smg_comms_c_wrappers 356 n0 -= d1;
smg_comms_c_wrappers 357 }
smg_comms_c_wrappers 358 }
smg_comms_c_wrappers 359
smg_comms_c_wrappers 360 /* Possible optimization: We already have (q * n0) and (1 * n1)
smg_comms_c_wrappers 361 * after the calculation of q. Taking advantage of that, we
smg_comms_c_wrappers 362 * could make this loop make two iterations less. */
smg_comms_c_wrappers 363 cy_limb = mpihelp_submul_1(np, dp, dsize, q);
smg_comms_c_wrappers 364
smg_comms_c_wrappers 365 if( n2 != cy_limb ) {
smg_comms_c_wrappers 366 mpihelp_add_n(np, np, dp, dsize);
smg_comms_c_wrappers 367 q--;
smg_comms_c_wrappers 368 }
smg_comms_c_wrappers 369
smg_comms_c_wrappers 370 qp[i] = q;
smg_comms_c_wrappers 371 n0 = np[dsize - 1];
smg_comms_c_wrappers 372 }
smg_comms_c_wrappers 373 }
smg_comms_c_wrappers 374 }
smg_comms_c_wrappers 375
smg_comms_c_wrappers 376 return most_significant_q_limb;
smg_comms_c_wrappers 377 }
smg_comms_c_wrappers 378
smg_comms_c_wrappers 379
smg_comms_c_wrappers 380 /****************
smg_comms_c_wrappers 381 * Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
smg_comms_c_wrappers 382 * Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
smg_comms_c_wrappers 383 * Return the single-limb remainder.
smg_comms_c_wrappers 384 * There are no constraints on the value of the divisor.
smg_comms_c_wrappers 385 *
smg_comms_c_wrappers 386 * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
smg_comms_c_wrappers 387 */
smg_comms_c_wrappers 388
smg_comms_c_wrappers 389 mpi_limb_t
smg_comms_c_wrappers 390 mpihelp_divmod_1( mpi_ptr_t quot_ptr,
smg_comms_c_wrappers 391 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
smg_comms_c_wrappers 392 mpi_limb_t divisor_limb)
smg_comms_c_wrappers 393 {
smg_comms_c_wrappers 394 mpi_size_t i;
smg_comms_c_wrappers 395 mpi_limb_t n1, n0, r;
smg_comms_c_wrappers 396 int dummy;
smg_comms_c_wrappers 397
smg_comms_c_wrappers 398 if( !dividend_size )
smg_comms_c_wrappers 399 return 0;
smg_comms_c_wrappers 400
smg_comms_c_wrappers 401 /* If multiplication is much faster than division, and the
smg_comms_c_wrappers 402 * dividend is large, pre-invert the divisor, and use
smg_comms_c_wrappers 403 * only multiplications in the inner loop.
smg_comms_c_wrappers 404 *
smg_comms_c_wrappers 405 * This test should be read:
smg_comms_c_wrappers 406 * Does it ever help to use udiv_qrnnd_preinv?
smg_comms_c_wrappers 407 * && Does what we save compensate for the inversion overhead?
smg_comms_c_wrappers 408 */
smg_comms_c_wrappers 409 if( UDIV_TIME > (2 * UMUL_TIME + 6)
smg_comms_c_wrappers 410 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
smg_comms_c_wrappers 411 int normalization_steps;
smg_comms_c_wrappers 412
smg_comms_c_wrappers 413 count_leading_zeros( normalization_steps, divisor_limb );
smg_comms_c_wrappers 414 if( normalization_steps ) {
smg_comms_c_wrappers 415 mpi_limb_t divisor_limb_inverted;
smg_comms_c_wrappers 416
smg_comms_c_wrappers 417 divisor_limb <<= normalization_steps;
smg_comms_c_wrappers 418
smg_comms_c_wrappers 419 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
smg_comms_c_wrappers 420 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
smg_comms_c_wrappers 421 * most significant bit (with weight 2**N) implicit.
smg_comms_c_wrappers 422 */
smg_comms_c_wrappers 423 /* Special case for DIVISOR_LIMB == 100...000. */
smg_comms_c_wrappers 424 if( !(divisor_limb << 1) )
smg_comms_c_wrappers 425 divisor_limb_inverted = ~(mpi_limb_t)0;
smg_comms_c_wrappers 426 else
smg_comms_c_wrappers 427 udiv_qrnnd(divisor_limb_inverted, dummy,
smg_comms_c_wrappers 428 -divisor_limb, 0, divisor_limb);
smg_comms_c_wrappers 429
smg_comms_c_wrappers 430 n1 = dividend_ptr[dividend_size - 1];
smg_comms_c_wrappers 431 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
smg_comms_c_wrappers 432
smg_comms_c_wrappers 433 /* Possible optimization:
smg_comms_c_wrappers 434 * if (r == 0
smg_comms_c_wrappers 435 * && divisor_limb > ((n1 << normalization_steps)
smg_comms_c_wrappers 436 * | (dividend_ptr[dividend_size - 2] >> ...)))
smg_comms_c_wrappers 437 * ...one division less...
smg_comms_c_wrappers 438 */
smg_comms_c_wrappers 439 for( i = dividend_size - 2; i >= 0; i--) {
smg_comms_c_wrappers 440 n0 = dividend_ptr[i];
smg_comms_c_wrappers 441 UDIV_QRNND_PREINV( quot_ptr[i + 1], r, r,
smg_comms_c_wrappers 442 ((n1 << normalization_steps)
smg_comms_c_wrappers 443 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
smg_comms_c_wrappers 444 divisor_limb, divisor_limb_inverted);
smg_comms_c_wrappers 445 n1 = n0;
smg_comms_c_wrappers 446 }
smg_comms_c_wrappers 447 UDIV_QRNND_PREINV( quot_ptr[0], r, r,
smg_comms_c_wrappers 448 n1 << normalization_steps,
smg_comms_c_wrappers 449 divisor_limb, divisor_limb_inverted);
smg_comms_c_wrappers 450 return r >> normalization_steps;
smg_comms_c_wrappers 451 }
smg_comms_c_wrappers 452 else {
smg_comms_c_wrappers 453 mpi_limb_t divisor_limb_inverted;
smg_comms_c_wrappers 454
smg_comms_c_wrappers 455 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
smg_comms_c_wrappers 456 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
smg_comms_c_wrappers 457 * most significant bit (with weight 2**N) implicit.
smg_comms_c_wrappers 458 */
smg_comms_c_wrappers 459 /* Special case for DIVISOR_LIMB == 100...000. */
smg_comms_c_wrappers 460 if( !(divisor_limb << 1) )
smg_comms_c_wrappers 461 divisor_limb_inverted = ~(mpi_limb_t) 0;
smg_comms_c_wrappers 462 else
smg_comms_c_wrappers 463 udiv_qrnnd(divisor_limb_inverted, dummy,
smg_comms_c_wrappers 464 -divisor_limb, 0, divisor_limb);
smg_comms_c_wrappers 465
smg_comms_c_wrappers 466 i = dividend_size - 1;
smg_comms_c_wrappers 467 r = dividend_ptr[i];
smg_comms_c_wrappers 468
smg_comms_c_wrappers 469 if( r >= divisor_limb )
smg_comms_c_wrappers 470 r = 0;
smg_comms_c_wrappers 471 else
smg_comms_c_wrappers 472 quot_ptr[i--] = 0;
smg_comms_c_wrappers 473
smg_comms_c_wrappers 474 for( ; i >= 0; i-- ) {
smg_comms_c_wrappers 475 n0 = dividend_ptr[i];
smg_comms_c_wrappers 476 UDIV_QRNND_PREINV( quot_ptr[i], r, r,
smg_comms_c_wrappers 477 n0, divisor_limb, divisor_limb_inverted);
smg_comms_c_wrappers 478 }
smg_comms_c_wrappers 479 return r;
smg_comms_c_wrappers 480 }
smg_comms_c_wrappers 481 }
smg_comms_c_wrappers 482 else {
smg_comms_c_wrappers 483 if(UDIV_NEEDS_NORMALIZATION) {
smg_comms_c_wrappers 484 int normalization_steps;
smg_comms_c_wrappers 485
smg_comms_c_wrappers 486 count_leading_zeros (normalization_steps, divisor_limb);
smg_comms_c_wrappers 487 if( normalization_steps ) {
smg_comms_c_wrappers 488 divisor_limb <<= normalization_steps;
smg_comms_c_wrappers 489
smg_comms_c_wrappers 490 n1 = dividend_ptr[dividend_size - 1];
smg_comms_c_wrappers 491 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
smg_comms_c_wrappers 492
smg_comms_c_wrappers 493 /* Possible optimization:
smg_comms_c_wrappers 494 * if (r == 0
smg_comms_c_wrappers 495 * && divisor_limb > ((n1 << normalization_steps)
smg_comms_c_wrappers 496 * | (dividend_ptr[dividend_size - 2] >> ...)))
smg_comms_c_wrappers 497 * ...one division less...
smg_comms_c_wrappers 498 */
smg_comms_c_wrappers 499 for( i = dividend_size - 2; i >= 0; i--) {
smg_comms_c_wrappers 500 n0 = dividend_ptr[i];
smg_comms_c_wrappers 501 udiv_qrnnd (quot_ptr[i + 1], r, r,
smg_comms_c_wrappers 502 ((n1 << normalization_steps)
smg_comms_c_wrappers 503 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
smg_comms_c_wrappers 504 divisor_limb);
smg_comms_c_wrappers 505 n1 = n0;
smg_comms_c_wrappers 506 }
smg_comms_c_wrappers 507 udiv_qrnnd (quot_ptr[0], r, r,
smg_comms_c_wrappers 508 n1 << normalization_steps,
smg_comms_c_wrappers 509 divisor_limb);
smg_comms_c_wrappers 510 return r >> normalization_steps;
smg_comms_c_wrappers 511 }
smg_comms_c_wrappers 512 }
smg_comms_c_wrappers 513 /* No normalization needed, either because udiv_qrnnd doesn't require
smg_comms_c_wrappers 514 * it, or because DIVISOR_LIMB is already normalized. */
smg_comms_c_wrappers 515 i = dividend_size - 1;
smg_comms_c_wrappers 516 r = dividend_ptr[i];
smg_comms_c_wrappers 517
smg_comms_c_wrappers 518 if(r >= divisor_limb)
smg_comms_c_wrappers 519 r = 0;
smg_comms_c_wrappers 520 else
smg_comms_c_wrappers 521 quot_ptr[i--] = 0;
smg_comms_c_wrappers 522
smg_comms_c_wrappers 523 for(; i >= 0; i--) {
smg_comms_c_wrappers 524 n0 = dividend_ptr[i];
smg_comms_c_wrappers 525 udiv_qrnnd( quot_ptr[i], r, r, n0, divisor_limb );
smg_comms_c_wrappers 526 }
smg_comms_c_wrappers 527 return r;
smg_comms_c_wrappers 528 }
smg_comms_c_wrappers 529 }