1 /* mpihelp-div.c - MPI helper functions
2 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
3 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 * Note: This code is heavily based on the GNU MP Library.
22 * Actually it's the same code with only minor changes in the
23 * way the data is stored; this is to support the abstraction
24 * of an optional secure memory allocation which may be used
25 * to avoid revealing of sensitive data due to paging etc.
26 * The GNU MP Library itself is published under the LGPL;
27 * however I decided to publish this code under the plain GPL.
30 #include "mpi-internal.h"
37 #define UDIV_TIME UMUL_TIME
40 /* FIXME: We should be using invert_limb (or invert_normalized_limb)
41 * here (not udiv_qrnnd).
45 mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
46 mpi_limb_t divisor_limb)
52 /* Botch: Should this be handled at all? Rely on callers? */
56 /* If multiplication is much faster than division, and the
57 * dividend is large, pre-invert the divisor, and use
58 * only multiplications in the inner loop.
60 * This test should be read:
61 * Does it ever help to use udiv_qrnnd_preinv?
62 * && Does what we save compensate for the inversion overhead?
64 if( UDIV_TIME > (2 * UMUL_TIME + 6)
65 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
66 int normalization_steps;
68 count_leading_zeros( normalization_steps, divisor_limb );
69 if( normalization_steps ) {
70 mpi_limb_t divisor_limb_inverted;
72 divisor_limb <<= normalization_steps;
74 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
75 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
76 * most significant bit (with weight 2**N) implicit.
78 * Special case for DIVISOR_LIMB == 100...000.
80 if( !(divisor_limb << 1) )
81 divisor_limb_inverted = ~(mpi_limb_t)0;
83 udiv_qrnnd(divisor_limb_inverted, dummy,
84 -divisor_limb, 0, divisor_limb);
86 n1 = dividend_ptr[dividend_size - 1];
87 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
89 /* Possible optimization:
91 * && divisor_limb > ((n1 << normalization_steps)
92 * | (dividend_ptr[dividend_size - 2] >> ...)))
93 * ...one division less...
95 for( i = dividend_size - 2; i >= 0; i--) {
97 UDIV_QRNND_PREINV(dummy, r, r,
98 ((n1 << normalization_steps)
99 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
100 divisor_limb, divisor_limb_inverted);
103 UDIV_QRNND_PREINV(dummy, r, r,
104 n1 << normalization_steps,
105 divisor_limb, divisor_limb_inverted);
106 return r >> normalization_steps;
109 mpi_limb_t divisor_limb_inverted;
111 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
112 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
113 * most significant bit (with weight 2**N) implicit.
115 * Special case for DIVISOR_LIMB == 100...000.
117 if( !(divisor_limb << 1) )
118 divisor_limb_inverted = ~(mpi_limb_t)0;
120 udiv_qrnnd(divisor_limb_inverted, dummy,
121 -divisor_limb, 0, divisor_limb);
123 i = dividend_size - 1;
126 if( r >= divisor_limb )
131 for( ; i >= 0; i--) {
132 n0 = dividend_ptr[i];
133 UDIV_QRNND_PREINV(dummy, r, r,
134 n0, divisor_limb, divisor_limb_inverted);
140 if( UDIV_NEEDS_NORMALIZATION ) {
141 int normalization_steps;
143 count_leading_zeros(normalization_steps, divisor_limb);
144 if( normalization_steps ) {
145 divisor_limb <<= normalization_steps;
147 n1 = dividend_ptr[dividend_size - 1];
148 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
150 /* Possible optimization:
152 * && divisor_limb > ((n1 << normalization_steps)
153 * | (dividend_ptr[dividend_size - 2] >> ...)))
154 * ...one division less...
156 for(i = dividend_size - 2; i >= 0; i--) {
157 n0 = dividend_ptr[i];
158 udiv_qrnnd (dummy, r, r,
159 ((n1 << normalization_steps)
160 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
164 udiv_qrnnd (dummy, r, r,
165 n1 << normalization_steps,
167 return r >> normalization_steps;
170 /* No normalization needed, either because udiv_qrnnd doesn't require
171 * it, or because DIVISOR_LIMB is already normalized. */
172 i = dividend_size - 1;
175 if(r >= divisor_limb)
181 n0 = dividend_ptr[i];
182 udiv_qrnnd (dummy, r, r, n0, divisor_limb);
188 /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
189 * the NSIZE-DSIZE least significant quotient limbs at QP
190 * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
191 * non-zero, generate that many fraction bits and append them after the
192 * other quotient limbs.
193 * Return the most significant limb of the quotient, this is always 0 or 1.
197 * 1. The most significant bit of the divisor must be set.
198 * 2. QP must either not overlap with the input operands at all, or
199 * QP + DSIZE >= NP must hold true. (This means that it's
200 * possible to put the quotient in the high part of NUM, right after the
202 * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
206 mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
207 mpi_ptr_t np, mpi_size_t nsize,
208 mpi_ptr_t dp, mpi_size_t dsize)
210 mpi_limb_t most_significant_q_limb = 0;
214 /* We are asked to divide by zero, so go ahead and do it! (To make
215 the compiler not remove this statement, return the value.) */
229 most_significant_q_limb = 1;
233 for( i = nsize - 2; i >= 0; i--)
234 udiv_qrnnd( qp[i], n1, n1, np[i], d );
237 for( i = qextra_limbs - 1; i >= 0; i-- )
238 udiv_qrnnd (qp[i], n1, n1, 0, d);
247 mpi_limb_t n1, n0, n2;
256 if( n1 >= d1 && (n1 > d1 || n0 >= d0) ) {
257 sub_ddmmss (n1, n0, n1, n0, d1, d0);
258 most_significant_q_limb = 1;
261 for( i = qextra_limbs + nsize - 2 - 1; i >= 0; i-- ) {
265 if( i >= qextra_limbs )
271 /* Q should be either 111..111 or 111..110. Need special
272 * treatment of this rare case as normal division would
277 if( r < d1 ) { /* Carry in the addition? */
278 add_ssaaaa( n1, n0, r - d0, np[0], 0, d0 );
282 n1 = d0 - (d0 != 0?1:0);
286 udiv_qrnnd (q, r, n1, n0, d1);
287 umul_ppmm (n1, n0, d0, q);
292 if( n1 > r || (n1 == r && n0 > n2) ) {
293 /* The estimated Q was too large. */
295 sub_ddmmss (n1, n0, n1, n0, 0, d0);
297 if( r >= d1 ) /* If not carry, test Q again. */
302 sub_ddmmss (n1, n0, r, n2, n1, n0);
312 mpi_limb_t dX, d1, n0;
320 if(n0 > dX || mpihelp_cmp(np, dp, dsize - 1) >= 0 ) {
321 mpihelp_sub_n(np, np, dp, dsize);
323 most_significant_q_limb = 1;
327 for( i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
332 if( i >= qextra_limbs ) {
338 MPN_COPY_DECR (np + 1, np, dsize - 1);
343 /* This might over-estimate q, but it's probably not worth
344 * the extra code here to find out. */
350 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
351 umul_ppmm(n1, n0, d1, q);
353 while( n1 > r || (n1 == r && n0 > np[dsize - 2])) {
356 if( r < dX ) /* I.e. "carry in previous addition?" */
363 /* Possible optimization: We already have (q * n0) and (1 * n1)
364 * after the calculation of q. Taking advantage of that, we
365 * could make this loop make two iterations less. */
366 cy_limb = mpihelp_submul_1(np, dp, dsize, q);
368 if( n2 != cy_limb ) {
369 mpihelp_add_n(np, np, dp, dsize);
379 return most_significant_q_limb;
384 * Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
385 * Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
386 * Return the single-limb remainder.
387 * There are no constraints on the value of the divisor.
389 * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
393 mpihelp_divmod_1( mpi_ptr_t quot_ptr,
394 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
395 mpi_limb_t divisor_limb)
398 mpi_limb_t n1, n0, r;
404 /* If multiplication is much faster than division, and the
405 * dividend is large, pre-invert the divisor, and use
406 * only multiplications in the inner loop.
408 * This test should be read:
409 * Does it ever help to use udiv_qrnnd_preinv?
410 * && Does what we save compensate for the inversion overhead?
412 if( UDIV_TIME > (2 * UMUL_TIME + 6)
413 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
414 int normalization_steps;
416 count_leading_zeros( normalization_steps, divisor_limb );
417 if( normalization_steps ) {
418 mpi_limb_t divisor_limb_inverted;
420 divisor_limb <<= normalization_steps;
422 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
423 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
424 * most significant bit (with weight 2**N) implicit.
426 /* Special case for DIVISOR_LIMB == 100...000. */
427 if( !(divisor_limb << 1) )
428 divisor_limb_inverted = ~(mpi_limb_t)0;
430 udiv_qrnnd(divisor_limb_inverted, dummy,
431 -divisor_limb, 0, divisor_limb);
433 n1 = dividend_ptr[dividend_size - 1];
434 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
436 /* Possible optimization:
438 * && divisor_limb > ((n1 << normalization_steps)
439 * | (dividend_ptr[dividend_size - 2] >> ...)))
440 * ...one division less...
442 for( i = dividend_size - 2; i >= 0; i--) {
443 n0 = dividend_ptr[i];
444 UDIV_QRNND_PREINV( quot_ptr[i + 1], r, r,
445 ((n1 << normalization_steps)
446 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
447 divisor_limb, divisor_limb_inverted);
450 UDIV_QRNND_PREINV( quot_ptr[0], r, r,
451 n1 << normalization_steps,
452 divisor_limb, divisor_limb_inverted);
453 return r >> normalization_steps;
456 mpi_limb_t divisor_limb_inverted;
458 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
459 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
460 * most significant bit (with weight 2**N) implicit.
462 /* Special case for DIVISOR_LIMB == 100...000. */
463 if( !(divisor_limb << 1) )
464 divisor_limb_inverted = ~(mpi_limb_t) 0;
466 udiv_qrnnd(divisor_limb_inverted, dummy,
467 -divisor_limb, 0, divisor_limb);
469 i = dividend_size - 1;
472 if( r >= divisor_limb )
477 for( ; i >= 0; i-- ) {
478 n0 = dividend_ptr[i];
479 UDIV_QRNND_PREINV( quot_ptr[i], r, r,
480 n0, divisor_limb, divisor_limb_inverted);
486 if(UDIV_NEEDS_NORMALIZATION) {
487 int normalization_steps;
489 count_leading_zeros (normalization_steps, divisor_limb);
490 if( normalization_steps ) {
491 divisor_limb <<= normalization_steps;
493 n1 = dividend_ptr[dividend_size - 1];
494 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
496 /* Possible optimization:
498 * && divisor_limb > ((n1 << normalization_steps)
499 * | (dividend_ptr[dividend_size - 2] >> ...)))
500 * ...one division less...
502 for( i = dividend_size - 2; i >= 0; i--) {
503 n0 = dividend_ptr[i];
504 udiv_qrnnd (quot_ptr[i + 1], r, r,
505 ((n1 << normalization_steps)
506 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
510 udiv_qrnnd (quot_ptr[0], r, r,
511 n1 << normalization_steps,
513 return r >> normalization_steps;
516 /* No normalization needed, either because udiv_qrnnd doesn't require
517 * it, or because DIVISOR_LIMB is already normalized. */
518 i = dividend_size - 1;
521 if(r >= divisor_limb)
527 n0 = dividend_ptr[i];
528 udiv_qrnnd( quot_ptr[i], r, r, n0, divisor_limb );