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