This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / crypto / mpi / mpih-div.c
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.
4  *
5  * This file is part of GnuPG.
6  *
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.
11  *
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.
16  *
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
20  *
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.
28  */
29
30 #include "mpi-internal.h"
31 #include "longlong.h"
32
33 #ifndef UMUL_TIME
34   #define UMUL_TIME 1
35 #endif
36 #ifndef UDIV_TIME
37   #define UDIV_TIME UMUL_TIME
38 #endif
39
40 /* FIXME: We should be using invert_limb (or invert_normalized_limb)
41  * here (not udiv_qrnnd).
42  */
43
44 mpi_limb_t
45 mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
46                                       mpi_limb_t divisor_limb)
47 {
48     mpi_size_t i;
49     mpi_limb_t n1, n0, r;
50     int dummy;
51
52     /* Botch: Should this be handled at all?  Rely on callers?  */
53     if( !dividend_size )
54         return 0;
55
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.
59      *
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?
63      */
64     if( UDIV_TIME > (2 * UMUL_TIME + 6)
65         && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
66         int normalization_steps;
67
68         count_leading_zeros( normalization_steps, divisor_limb );
69         if( normalization_steps ) {
70             mpi_limb_t divisor_limb_inverted;
71
72             divisor_limb <<= normalization_steps;
73
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.
77              *
78              * Special case for DIVISOR_LIMB == 100...000.
79              */
80             if( !(divisor_limb << 1) )
81                 divisor_limb_inverted = ~(mpi_limb_t)0;
82             else
83                 udiv_qrnnd(divisor_limb_inverted, dummy,
84                            -divisor_limb, 0, divisor_limb);
85
86             n1 = dividend_ptr[dividend_size - 1];
87             r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
88
89             /* Possible optimization:
90              * if (r == 0
91              * && divisor_limb > ((n1 << normalization_steps)
92              *                 | (dividend_ptr[dividend_size - 2] >> ...)))
93              * ...one division less...
94              */
95             for( i = dividend_size - 2; i >= 0; i--) {
96                 n0 = dividend_ptr[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);
101                 n1 = n0;
102             }
103             UDIV_QRNND_PREINV(dummy, r, r,
104                               n1 << normalization_steps,
105                               divisor_limb, divisor_limb_inverted);
106             return r >> normalization_steps;
107         }
108         else {
109             mpi_limb_t divisor_limb_inverted;
110
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.
114              *
115              * Special case for DIVISOR_LIMB == 100...000.
116              */
117             if( !(divisor_limb << 1) )
118                 divisor_limb_inverted = ~(mpi_limb_t)0;
119             else
120                 udiv_qrnnd(divisor_limb_inverted, dummy,
121                             -divisor_limb, 0, divisor_limb);
122
123             i = dividend_size - 1;
124             r = dividend_ptr[i];
125
126             if( r >= divisor_limb )
127                 r = 0;
128             else
129                 i--;
130
131             for( ; i >= 0; i--) {
132                 n0 = dividend_ptr[i];
133                 UDIV_QRNND_PREINV(dummy, r, r,
134                                   n0, divisor_limb, divisor_limb_inverted);
135             }
136             return r;
137         }
138     }
139     else {
140         if( UDIV_NEEDS_NORMALIZATION ) {
141             int normalization_steps;
142
143             count_leading_zeros(normalization_steps, divisor_limb);
144             if( normalization_steps ) {
145                 divisor_limb <<= normalization_steps;
146
147                 n1 = dividend_ptr[dividend_size - 1];
148                 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
149
150                 /* Possible optimization:
151                  * if (r == 0
152                  * && divisor_limb > ((n1 << normalization_steps)
153                  *                 | (dividend_ptr[dividend_size - 2] >> ...)))
154                  * ...one division less...
155                  */
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))),
161                          divisor_limb);
162                     n1 = n0;
163                 }
164                 udiv_qrnnd (dummy, r, r,
165                             n1 << normalization_steps,
166                             divisor_limb);
167                 return r >> normalization_steps;
168             }
169         }
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;
173         r = dividend_ptr[i];
174
175         if(r >= divisor_limb)
176             r = 0;
177         else
178             i--;
179
180         for(; i >= 0; i--) {
181             n0 = dividend_ptr[i];
182             udiv_qrnnd (dummy, r, r, n0, divisor_limb);
183         }
184         return r;
185     }
186 }
187
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.
194  *
195  * Preconditions:
196  * 0. NSIZE >= DSIZE.
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
201  *    remainder in NUM.
202  * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
203  */
204
205 mpi_limb_t
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)
209 {
210     mpi_limb_t most_significant_q_limb = 0;
211
212     switch(dsize) {
213       case 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.)  */
216         return 1 / dsize;
217
218       case 1:
219         {
220             mpi_size_t i;
221             mpi_limb_t n1;
222             mpi_limb_t d;
223
224             d = dp[0];
225             n1 = np[nsize - 1];
226
227             if( n1 >= d ) {
228                 n1 -= d;
229                 most_significant_q_limb = 1;
230             }
231
232             qp += qextra_limbs;
233             for( i = nsize - 2; i >= 0; i--)
234                 udiv_qrnnd( qp[i], n1, n1, np[i], d );
235             qp -= qextra_limbs;
236
237             for( i = qextra_limbs - 1; i >= 0; i-- )
238                 udiv_qrnnd (qp[i], n1, n1, 0, d);
239
240             np[0] = n1;
241         }
242         break;
243
244       case 2:
245         {
246             mpi_size_t i;
247             mpi_limb_t n1, n0, n2;
248             mpi_limb_t d1, d0;
249
250             np += nsize - 2;
251             d1 = dp[1];
252             d0 = dp[0];
253             n1 = np[1];
254             n0 = np[0];
255
256             if( n1 >= d1 && (n1 > d1 || n0 >= d0) ) {
257                 sub_ddmmss (n1, n0, n1, n0, d1, d0);
258                 most_significant_q_limb = 1;
259             }
260
261             for( i = qextra_limbs + nsize - 2 - 1; i >= 0; i-- ) {
262                 mpi_limb_t q;
263                 mpi_limb_t r;
264
265                 if( i >= qextra_limbs )
266                     np--;
267                 else
268                     np[0] = 0;
269
270                 if( n1 == d1 ) {
271                     /* Q should be either 111..111 or 111..110.  Need special
272                      * treatment of this rare case as normal division would
273                      * give overflow.  */
274                     q = ~(mpi_limb_t)0;
275
276                     r = n0 + d1;
277                     if( r < d1 ) {   /* Carry in the addition? */
278                         add_ssaaaa( n1, n0, r - d0, np[0], 0, d0 );
279                         qp[i] = q;
280                         continue;
281                     }
282                     n1 = d0 - (d0 != 0?1:0);
283                     n0 = -d0;
284                 }
285                 else {
286                     udiv_qrnnd (q, r, n1, n0, d1);
287                     umul_ppmm (n1, n0, d0, q);
288                 }
289
290                 n2 = np[0];
291               q_test:
292                 if( n1 > r || (n1 == r && n0 > n2) ) {
293                     /* The estimated Q was too large.  */
294                     q--;
295                     sub_ddmmss (n1, n0, n1, n0, 0, d0);
296                     r += d1;
297                     if( r >= d1 )    /* If not carry, test Q again.  */
298                         goto q_test;
299                 }
300
301                 qp[i] = q;
302                 sub_ddmmss (n1, n0, r, n2, n1, n0);
303             }
304             np[1] = n1;
305             np[0] = n0;
306         }
307         break;
308
309       default:
310         {
311             mpi_size_t i;
312             mpi_limb_t dX, d1, n0;
313
314             np += nsize - dsize;
315             dX = dp[dsize - 1];
316             d1 = dp[dsize - 2];
317             n0 = np[dsize - 1];
318
319             if( n0 >= dX ) {
320                 if(n0 > dX || mpihelp_cmp(np, dp, dsize - 1) >= 0 ) {
321                     mpihelp_sub_n(np, np, dp, dsize);
322                     n0 = np[dsize - 1];
323                     most_significant_q_limb = 1;
324                 }
325             }
326
327             for( i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
328                 mpi_limb_t q;
329                 mpi_limb_t n1, n2;
330                 mpi_limb_t cy_limb;
331
332                 if( i >= qextra_limbs ) {
333                     np--;
334                     n2 = np[dsize];
335                 }
336                 else {
337                     n2 = np[dsize - 1];
338                     MPN_COPY_DECR (np + 1, np, dsize - 1);
339                     np[0] = 0;
340                 }
341
342                 if( n0 == dX ) {
343                     /* This might over-estimate q, but it's probably not worth
344                      * the extra code here to find out.  */
345                     q = ~(mpi_limb_t)0;
346                 }
347                 else {
348                     mpi_limb_t r;
349
350                     udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
351                     umul_ppmm(n1, n0, d1, q);
352
353                     while( n1 > r || (n1 == r && n0 > np[dsize - 2])) {
354                         q--;
355                         r += dX;
356                         if( r < dX ) /* I.e. "carry in previous addition?" */
357                             break;
358                         n1 -= n0 < d1;
359                         n0 -= d1;
360                     }
361                 }
362
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);
367
368                 if( n2 != cy_limb ) {
369                     mpihelp_add_n(np, np, dp, dsize);
370                     q--;
371                 }
372
373                 qp[i] = q;
374                 n0 = np[dsize - 1];
375             }
376         }
377     }
378
379     return most_significant_q_limb;
380 }
381
382
383 /****************
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.
388  *
389  * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
390  */
391
392 mpi_limb_t
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)
396 {
397     mpi_size_t i;
398     mpi_limb_t n1, n0, r;
399     int dummy;
400
401     if( !dividend_size )
402         return 0;
403
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.
407      *
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?
411      */
412     if( UDIV_TIME > (2 * UMUL_TIME + 6)
413         && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
414         int normalization_steps;
415
416         count_leading_zeros( normalization_steps, divisor_limb );
417         if( normalization_steps ) {
418             mpi_limb_t divisor_limb_inverted;
419
420             divisor_limb <<= normalization_steps;
421
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.
425              */
426             /* Special case for DIVISOR_LIMB == 100...000.  */
427             if( !(divisor_limb << 1) )
428                 divisor_limb_inverted = ~(mpi_limb_t)0;
429             else
430                 udiv_qrnnd(divisor_limb_inverted, dummy,
431                            -divisor_limb, 0, divisor_limb);
432
433             n1 = dividend_ptr[dividend_size - 1];
434             r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
435
436             /* Possible optimization:
437              * if (r == 0
438              * && divisor_limb > ((n1 << normalization_steps)
439              *                 | (dividend_ptr[dividend_size - 2] >> ...)))
440              * ...one division less...
441              */
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);
448                 n1 = n0;
449             }
450             UDIV_QRNND_PREINV( quot_ptr[0], r, r,
451                                n1 << normalization_steps,
452                                divisor_limb, divisor_limb_inverted);
453             return r >> normalization_steps;
454         }
455         else {
456             mpi_limb_t divisor_limb_inverted;
457
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.
461              */
462             /* Special case for DIVISOR_LIMB == 100...000.  */
463             if( !(divisor_limb << 1) )
464                 divisor_limb_inverted = ~(mpi_limb_t) 0;
465             else
466                 udiv_qrnnd(divisor_limb_inverted, dummy,
467                            -divisor_limb, 0, divisor_limb);
468
469             i = dividend_size - 1;
470             r = dividend_ptr[i];
471
472             if( r >= divisor_limb )
473                 r = 0;
474             else
475                 quot_ptr[i--] = 0;
476
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);
481             }
482             return r;
483         }
484     }
485     else {
486         if(UDIV_NEEDS_NORMALIZATION) {
487             int normalization_steps;
488
489             count_leading_zeros (normalization_steps, divisor_limb);
490             if( normalization_steps ) {
491                 divisor_limb <<= normalization_steps;
492
493                 n1 = dividend_ptr[dividend_size - 1];
494                 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
495
496                 /* Possible optimization:
497                  * if (r == 0
498                  * && divisor_limb > ((n1 << normalization_steps)
499                  *                 | (dividend_ptr[dividend_size - 2] >> ...)))
500                  * ...one division less...
501                  */
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))),
507                                 divisor_limb);
508                     n1 = n0;
509                 }
510                 udiv_qrnnd (quot_ptr[0], r, r,
511                             n1 << normalization_steps,
512                             divisor_limb);
513                 return r >> normalization_steps;
514             }
515         }
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;
519         r = dividend_ptr[i];
520
521         if(r >= divisor_limb)
522             r = 0;
523         else
524             quot_ptr[i--] = 0;
525
526         for(; i >= 0; i--) {
527             n0 = dividend_ptr[i];
528             udiv_qrnnd( quot_ptr[i], r, r, n0, divisor_limb );
529         }
530         return r;
531     }
532 }
533
534