-
+ 49EF731356E9C2E54D04CB0F5C4A68285AD1CA1C27015AACEFDA5F0CB5AC676A0DC47B1656AB2057E1CFFCC58E837B1010E85391A8F570CCDDA24630B28620F7
eucrypt/mpi/mpih-div.c
(0 . 0)(1 . 529)
5136 /* mpihelp-div.c - MPI helper functions
5137 * Modified by No Such Labs. (C) 2015. See README.
5138 *
5139 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
5140 * SHA256(gnupg-1.4.10.tar.gz):
5141 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
5142 * (C) 1994-2005 Free Software Foundation, Inc.
5143 *
5144 * This program is free software: you can redistribute it and/or modify
5145 * it under the terms of the GNU General Public License as published by
5146 * the Free Software Foundation, either version 3 of the License, or
5147 * (at your option) any later version.
5148 *
5149 * This program is distributed in the hope that it will be useful,
5150 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5151 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5152 * GNU General Public License for more details.
5153 *
5154 * You should have received a copy of the GNU General Public License
5155 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5156 */
5157
5158 #include <stdio.h>
5159 #include <stdlib.h>
5160
5161 #include "knobs.h"
5162 #include "mpi-internal.h"
5163 #include "longlong.h"
5164
5165 #ifndef UMUL_TIME
5166 #define UMUL_TIME 1
5167 #endif
5168 #ifndef UDIV_TIME
5169 #define UDIV_TIME UMUL_TIME
5170 #endif
5171
5172 /* FIXME: We should be using invert_limb (or invert_normalized_limb)
5173 * here (not udiv_qrnnd).
5174 */
5175
5176 mpi_limb_t
5177 mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
5178 mpi_limb_t divisor_limb)
5179 {
5180 mpi_size_t i;
5181 mpi_limb_t n1, n0, r;
5182 int dummy;
5183
5184 /* Botch: Should this be handled at all? Rely on callers? */
5185 if( !dividend_size )
5186 return 0;
5187
5188 /* If multiplication is much faster than division, and the
5189 * dividend is large, pre-invert the divisor, and use
5190 * only multiplications in the inner loop.
5191 *
5192 * This test should be read:
5193 * Does it ever help to use udiv_qrnnd_preinv?
5194 * && Does what we save compensate for the inversion overhead?
5195 */
5196 if( UDIV_TIME > (2 * UMUL_TIME + 6)
5197 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
5198 int normalization_steps;
5199
5200 count_leading_zeros( normalization_steps, divisor_limb );
5201 if( normalization_steps ) {
5202 mpi_limb_t divisor_limb_inverted;
5203
5204 divisor_limb <<= normalization_steps;
5205
5206 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
5207 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
5208 * most significant bit (with weight 2**N) implicit.
5209 *
5210 * Special case for DIVISOR_LIMB == 100...000.
5211 */
5212 if( !(divisor_limb << 1) )
5213 divisor_limb_inverted = ~(mpi_limb_t)0;
5214 else
5215 udiv_qrnnd(divisor_limb_inverted, dummy,
5216 -divisor_limb, 0, divisor_limb);
5217
5218 n1 = dividend_ptr[dividend_size - 1];
5219 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
5220
5221 /* Possible optimization:
5222 * if (r == 0
5223 * && divisor_limb > ((n1 << normalization_steps)
5224 * | (dividend_ptr[dividend_size - 2] >> ...)))
5225 * ...one division less...
5226 */
5227 for( i = dividend_size - 2; i >= 0; i--) {
5228 n0 = dividend_ptr[i];
5229 UDIV_QRNND_PREINV(dummy, r, r,
5230 ((n1 << normalization_steps)
5231 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
5232 divisor_limb, divisor_limb_inverted);
5233 n1 = n0;
5234 }
5235 UDIV_QRNND_PREINV(dummy, r, r,
5236 n1 << normalization_steps,
5237 divisor_limb, divisor_limb_inverted);
5238 return r >> normalization_steps;
5239 }
5240 else {
5241 mpi_limb_t divisor_limb_inverted;
5242
5243 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
5244 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
5245 * most significant bit (with weight 2**N) implicit.
5246 *
5247 * Special case for DIVISOR_LIMB == 100...000.
5248 */
5249 if( !(divisor_limb << 1) )
5250 divisor_limb_inverted = ~(mpi_limb_t)0;
5251 else
5252 udiv_qrnnd(divisor_limb_inverted, dummy,
5253 -divisor_limb, 0, divisor_limb);
5254
5255 i = dividend_size - 1;
5256 r = dividend_ptr[i];
5257
5258 if( r >= divisor_limb )
5259 r = 0;
5260 else
5261 i--;
5262
5263 for( ; i >= 0; i--) {
5264 n0 = dividend_ptr[i];
5265 UDIV_QRNND_PREINV(dummy, r, r,
5266 n0, divisor_limb, divisor_limb_inverted);
5267 }
5268 return r;
5269 }
5270 }
5271 else {
5272 if( UDIV_NEEDS_NORMALIZATION ) {
5273 int normalization_steps;
5274
5275 count_leading_zeros(normalization_steps, divisor_limb);
5276 if( normalization_steps ) {
5277 divisor_limb <<= normalization_steps;
5278
5279 n1 = dividend_ptr[dividend_size - 1];
5280 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
5281
5282 /* Possible optimization:
5283 * if (r == 0
5284 * && divisor_limb > ((n1 << normalization_steps)
5285 * | (dividend_ptr[dividend_size - 2] >> ...)))
5286 * ...one division less...
5287 */
5288 for(i = dividend_size - 2; i >= 0; i--) {
5289 n0 = dividend_ptr[i];
5290 udiv_qrnnd (dummy, r, r,
5291 ((n1 << normalization_steps)
5292 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
5293 divisor_limb);
5294 n1 = n0;
5295 }
5296 udiv_qrnnd (dummy, r, r,
5297 n1 << normalization_steps,
5298 divisor_limb);
5299 return r >> normalization_steps;
5300 }
5301 }
5302 /* No normalization needed, either because udiv_qrnnd doesn't require
5303 * it, or because DIVISOR_LIMB is already normalized. */
5304 i = dividend_size - 1;
5305 r = dividend_ptr[i];
5306
5307 if(r >= divisor_limb)
5308 r = 0;
5309 else
5310 i--;
5311
5312 for(; i >= 0; i--) {
5313 n0 = dividend_ptr[i];
5314 udiv_qrnnd (dummy, r, r, n0, divisor_limb);
5315 }
5316 return r;
5317 }
5318 }
5319
5320 /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
5321 * the NSIZE-DSIZE least significant quotient limbs at QP
5322 * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
5323 * non-zero, generate that many fraction bits and append them after the
5324 * other quotient limbs.
5325 * Return the most significant limb of the quotient, this is always 0 or 1.
5326 *
5327 * Preconditions:
5328 * 0. NSIZE >= DSIZE.
5329 * 1. The most significant bit of the divisor must be set.
5330 * 2. QP must either not overlap with the input operands at all, or
5331 * QP + DSIZE >= NP must hold true. (This means that it's
5332 * possible to put the quotient in the high part of NUM, right after the
5333 * remainder in NUM.
5334 * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
5335 */
5336
5337 mpi_limb_t
5338 mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
5339 mpi_ptr_t np, mpi_size_t nsize,
5340 mpi_ptr_t dp, mpi_size_t dsize)
5341 {
5342 mpi_limb_t most_significant_q_limb = 0;
5343
5344 switch(dsize) {
5345 case 0:
5346 /* We are asked to divide by zero, so go ahead and do it! (To make
5347 the compiler not remove this statement, return the value.) */
5348 return 1 / dsize;
5349
5350 case 1:
5351 {
5352 mpi_size_t i;
5353 mpi_limb_t n1;
5354 mpi_limb_t d;
5355
5356 d = dp[0];
5357 n1 = np[nsize - 1];
5358
5359 if( n1 >= d ) {
5360 n1 -= d;
5361 most_significant_q_limb = 1;
5362 }
5363
5364 qp += qextra_limbs;
5365 for( i = nsize - 2; i >= 0; i--)
5366 udiv_qrnnd( qp[i], n1, n1, np[i], d );
5367 qp -= qextra_limbs;
5368
5369 for( i = qextra_limbs - 1; i >= 0; i-- )
5370 udiv_qrnnd (qp[i], n1, n1, 0, d);
5371
5372 np[0] = n1;
5373 }
5374 break;
5375
5376 case 2:
5377 {
5378 mpi_size_t i;
5379 mpi_limb_t n1, n0, n2;
5380 mpi_limb_t d1, d0;
5381
5382 np += nsize - 2;
5383 d1 = dp[1];
5384 d0 = dp[0];
5385 n1 = np[1];
5386 n0 = np[0];
5387
5388 if( n1 >= d1 && (n1 > d1 || n0 >= d0) ) {
5389 sub_ddmmss (n1, n0, n1, n0, d1, d0);
5390 most_significant_q_limb = 1;
5391 }
5392
5393 for( i = qextra_limbs + nsize - 2 - 1; i >= 0; i-- ) {
5394 mpi_limb_t q;
5395 mpi_limb_t r;
5396
5397 if( i >= qextra_limbs )
5398 np--;
5399 else
5400 np[0] = 0;
5401
5402 if( n1 == d1 ) {
5403 /* Q should be either 111..111 or 111..110. Need special
5404 * treatment of this rare case as normal division would
5405 * give overflow. */
5406 q = ~(mpi_limb_t)0;
5407
5408 r = n0 + d1;
5409 if( r < d1 ) { /* Carry in the addition? */
5410 add_ssaaaa( n1, n0, r - d0, np[0], 0, d0 );
5411 qp[i] = q;
5412 continue;
5413 }
5414 n1 = d0 - (d0 != 0?1:0);
5415 n0 = -d0;
5416 }
5417 else {
5418 udiv_qrnnd (q, r, n1, n0, d1);
5419 umul_ppmm (n1, n0, d0, q);
5420 }
5421
5422 n2 = np[0];
5423 q_test:
5424 if( n1 > r || (n1 == r && n0 > n2) ) {
5425 /* The estimated Q was too large. */
5426 q--;
5427 sub_ddmmss (n1, n0, n1, n0, 0, d0);
5428 r += d1;
5429 if( r >= d1 ) /* If not carry, test Q again. */
5430 goto q_test;
5431 }
5432
5433 qp[i] = q;
5434 sub_ddmmss (n1, n0, r, n2, n1, n0);
5435 }
5436 np[1] = n1;
5437 np[0] = n0;
5438 }
5439 break;
5440
5441 default:
5442 {
5443 mpi_size_t i;
5444 mpi_limb_t dX, d1, n0;
5445
5446 np += nsize - dsize;
5447 dX = dp[dsize - 1];
5448 d1 = dp[dsize - 2];
5449 n0 = np[dsize - 1];
5450
5451 if( n0 >= dX ) {
5452 if(n0 > dX || mpihelp_cmp(np, dp, dsize - 1) >= 0 ) {
5453 mpihelp_sub_n(np, np, dp, dsize);
5454 n0 = np[dsize - 1];
5455 most_significant_q_limb = 1;
5456 }
5457 }
5458
5459 for( i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
5460 mpi_limb_t q;
5461 mpi_limb_t n1, n2;
5462 mpi_limb_t cy_limb;
5463
5464 if( i >= qextra_limbs ) {
5465 np--;
5466 n2 = np[dsize];
5467 }
5468 else {
5469 n2 = np[dsize - 1];
5470 MPN_COPY_DECR (np + 1, np, dsize - 1);
5471 np[0] = 0;
5472 }
5473
5474 if( n0 == dX ) {
5475 /* This might over-estimate q, but it's probably not worth
5476 * the extra code here to find out. */
5477 q = ~(mpi_limb_t)0;
5478 }
5479 else {
5480 mpi_limb_t r;
5481
5482 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
5483 umul_ppmm(n1, n0, d1, q);
5484
5485 while( n1 > r || (n1 == r && n0 > np[dsize - 2])) {
5486 q--;
5487 r += dX;
5488 if( r < dX ) /* I.e. "carry in previous addition?" */
5489 break;
5490 n1 -= n0 < d1;
5491 n0 -= d1;
5492 }
5493 }
5494
5495 /* Possible optimization: We already have (q * n0) and (1 * n1)
5496 * after the calculation of q. Taking advantage of that, we
5497 * could make this loop make two iterations less. */
5498 cy_limb = mpihelp_submul_1(np, dp, dsize, q);
5499
5500 if( n2 != cy_limb ) {
5501 mpihelp_add_n(np, np, dp, dsize);
5502 q--;
5503 }
5504
5505 qp[i] = q;
5506 n0 = np[dsize - 1];
5507 }
5508 }
5509 }
5510
5511 return most_significant_q_limb;
5512 }
5513
5514
5515 /****************
5516 * Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
5517 * Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
5518 * Return the single-limb remainder.
5519 * There are no constraints on the value of the divisor.
5520 *
5521 * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
5522 */
5523
5524 mpi_limb_t
5525 mpihelp_divmod_1( mpi_ptr_t quot_ptr,
5526 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
5527 mpi_limb_t divisor_limb)
5528 {
5529 mpi_size_t i;
5530 mpi_limb_t n1, n0, r;
5531 int dummy;
5532
5533 if( !dividend_size )
5534 return 0;
5535
5536 /* If multiplication is much faster than division, and the
5537 * dividend is large, pre-invert the divisor, and use
5538 * only multiplications in the inner loop.
5539 *
5540 * This test should be read:
5541 * Does it ever help to use udiv_qrnnd_preinv?
5542 * && Does what we save compensate for the inversion overhead?
5543 */
5544 if( UDIV_TIME > (2 * UMUL_TIME + 6)
5545 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
5546 int normalization_steps;
5547
5548 count_leading_zeros( normalization_steps, divisor_limb );
5549 if( normalization_steps ) {
5550 mpi_limb_t divisor_limb_inverted;
5551
5552 divisor_limb <<= normalization_steps;
5553
5554 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
5555 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
5556 * most significant bit (with weight 2**N) implicit.
5557 */
5558 /* Special case for DIVISOR_LIMB == 100...000. */
5559 if( !(divisor_limb << 1) )
5560 divisor_limb_inverted = ~(mpi_limb_t)0;
5561 else
5562 udiv_qrnnd(divisor_limb_inverted, dummy,
5563 -divisor_limb, 0, divisor_limb);
5564
5565 n1 = dividend_ptr[dividend_size - 1];
5566 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
5567
5568 /* Possible optimization:
5569 * if (r == 0
5570 * && divisor_limb > ((n1 << normalization_steps)
5571 * | (dividend_ptr[dividend_size - 2] >> ...)))
5572 * ...one division less...
5573 */
5574 for( i = dividend_size - 2; i >= 0; i--) {
5575 n0 = dividend_ptr[i];
5576 UDIV_QRNND_PREINV( quot_ptr[i + 1], r, r,
5577 ((n1 << normalization_steps)
5578 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
5579 divisor_limb, divisor_limb_inverted);
5580 n1 = n0;
5581 }
5582 UDIV_QRNND_PREINV( quot_ptr[0], r, r,
5583 n1 << normalization_steps,
5584 divisor_limb, divisor_limb_inverted);
5585 return r >> normalization_steps;
5586 }
5587 else {
5588 mpi_limb_t divisor_limb_inverted;
5589
5590 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
5591 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
5592 * most significant bit (with weight 2**N) implicit.
5593 */
5594 /* Special case for DIVISOR_LIMB == 100...000. */
5595 if( !(divisor_limb << 1) )
5596 divisor_limb_inverted = ~(mpi_limb_t) 0;
5597 else
5598 udiv_qrnnd(divisor_limb_inverted, dummy,
5599 -divisor_limb, 0, divisor_limb);
5600
5601 i = dividend_size - 1;
5602 r = dividend_ptr[i];
5603
5604 if( r >= divisor_limb )
5605 r = 0;
5606 else
5607 quot_ptr[i--] = 0;
5608
5609 for( ; i >= 0; i-- ) {
5610 n0 = dividend_ptr[i];
5611 UDIV_QRNND_PREINV( quot_ptr[i], r, r,
5612 n0, divisor_limb, divisor_limb_inverted);
5613 }
5614 return r;
5615 }
5616 }
5617 else {
5618 if(UDIV_NEEDS_NORMALIZATION) {
5619 int normalization_steps;
5620
5621 count_leading_zeros (normalization_steps, divisor_limb);
5622 if( normalization_steps ) {
5623 divisor_limb <<= normalization_steps;
5624
5625 n1 = dividend_ptr[dividend_size - 1];
5626 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
5627
5628 /* Possible optimization:
5629 * if (r == 0
5630 * && divisor_limb > ((n1 << normalization_steps)
5631 * | (dividend_ptr[dividend_size - 2] >> ...)))
5632 * ...one division less...
5633 */
5634 for( i = dividend_size - 2; i >= 0; i--) {
5635 n0 = dividend_ptr[i];
5636 udiv_qrnnd (quot_ptr[i + 1], r, r,
5637 ((n1 << normalization_steps)
5638 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
5639 divisor_limb);
5640 n1 = n0;
5641 }
5642 udiv_qrnnd (quot_ptr[0], r, r,
5643 n1 << normalization_steps,
5644 divisor_limb);
5645 return r >> normalization_steps;
5646 }
5647 }
5648 /* No normalization needed, either because udiv_qrnnd doesn't require
5649 * it, or because DIVISOR_LIMB is already normalized. */
5650 i = dividend_size - 1;
5651 r = dividend_ptr[i];
5652
5653 if(r >= divisor_limb)
5654 r = 0;
5655 else
5656 quot_ptr[i--] = 0;
5657
5658 for(; i >= 0; i--) {
5659 n0 = dividend_ptr[i];
5660 udiv_qrnnd( quot_ptr[i], r, r, n0, divisor_limb );
5661 }
5662 return r;
5663 }
5664 }