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