OLD | NEW |
1 /* crypto/bn/bn_div.c */ | 1 /* crypto/bn/bn_div.c */ |
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
8 * | 8 * |
9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 { | 331 { |
332 if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2
])) | 332 if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2
])) |
333 break; | 333 break; |
334 q--; | 334 q--; |
335 rem += d0; | 335 rem += d0; |
336 if (rem < d0) break; /* don't let rem overflow *
/ | 336 if (rem < d0) break; /* don't let rem overflow *
/ |
337 t2 -= d1; | 337 t2 -= d1; |
338 } | 338 } |
339 #else /* !BN_LLONG */ | 339 #else /* !BN_LLONG */ |
340 BN_ULONG t2l,t2h; | 340 BN_ULONG t2l,t2h; |
341 #if !defined(BN_UMULT_LOHI) && !defined(BN_UMULT_HIGH) | |
342 BN_ULONG ql,qh; | |
343 #endif | |
344 | 341 |
345 q=bn_div_words(n0,n1,d0); | 342 q=bn_div_words(n0,n1,d0); |
346 #ifdef BN_DEBUG_LEVITTE | 343 #ifdef BN_DEBUG_LEVITTE |
347 fprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\ | 344 fprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\ |
348 X) -> 0x%08X\n", | 345 X) -> 0x%08X\n", |
349 n0, n1, d0, q); | 346 n0, n1, d0, q); |
350 #endif | 347 #endif |
351 #ifndef REMAINDER_IS_ALREADY_CALCULATED | 348 #ifndef REMAINDER_IS_ALREADY_CALCULATED |
352 rem=(n1-q*d0)&BN_MASK2; | 349 rem=(n1-q*d0)&BN_MASK2; |
353 #endif | 350 #endif |
354 | 351 |
355 #if defined(BN_UMULT_LOHI) | 352 #if defined(BN_UMULT_LOHI) |
356 BN_UMULT_LOHI(t2l,t2h,d1,q); | 353 BN_UMULT_LOHI(t2l,t2h,d1,q); |
357 #elif defined(BN_UMULT_HIGH) | 354 #elif defined(BN_UMULT_HIGH) |
358 t2l = d1 * q; | 355 t2l = d1 * q; |
359 t2h = BN_UMULT_HIGH(d1,q); | 356 t2h = BN_UMULT_HIGH(d1,q); |
360 #else | 357 #else |
| 358 { |
| 359 BN_ULONG ql, qh; |
361 t2l=LBITS(d1); t2h=HBITS(d1); | 360 t2l=LBITS(d1); t2h=HBITS(d1); |
362 ql =LBITS(q); qh =HBITS(q); | 361 ql =LBITS(q); qh =HBITS(q); |
363 mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */ | 362 mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */ |
| 363 } |
364 #endif | 364 #endif |
365 | 365 |
366 for (;;) | 366 for (;;) |
367 { | 367 { |
368 if ((t2h < rem) || | 368 if ((t2h < rem) || |
369 ((t2h == rem) && (t2l <= wnump[-2]))) | 369 ((t2h == rem) && (t2l <= wnump[-2]))) |
370 break; | 370 break; |
371 q--; | 371 q--; |
372 rem += d0; | 372 rem += d0; |
373 if (rem < d0) break; /* don't let rem overflow *
/ | 373 if (rem < d0) break; /* don't let rem overflow *
/ |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 { | 558 { |
559 if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2
])) | 559 if (t2 <= ((((BN_ULLONG)rem)<<BN_BITS2)|wnump[-2
])) |
560 break; | 560 break; |
561 q--; | 561 q--; |
562 rem += d0; | 562 rem += d0; |
563 if (rem < d0) break; /* don't let rem overflow *
/ | 563 if (rem < d0) break; /* don't let rem overflow *
/ |
564 t2 -= d1; | 564 t2 -= d1; |
565 } | 565 } |
566 #else /* !BN_LLONG */ | 566 #else /* !BN_LLONG */ |
567 BN_ULONG t2l,t2h; | 567 BN_ULONG t2l,t2h; |
568 #if !defined(BN_UMULT_LOHI) && !defined(BN_UMULT_HIGH) | |
569 BN_ULONG ql,qh; | |
570 #endif | |
571 | 568 |
572 q=bn_div_words(n0,n1,d0); | 569 q=bn_div_words(n0,n1,d0); |
573 #ifdef BN_DEBUG_LEVITTE | 570 #ifdef BN_DEBUG_LEVITTE |
574 fprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\ | 571 fprintf(stderr,"DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\ |
575 X) -> 0x%08X\n", | 572 X) -> 0x%08X\n", |
576 n0, n1, d0, q); | 573 n0, n1, d0, q); |
577 #endif | 574 #endif |
578 #ifndef REMAINDER_IS_ALREADY_CALCULATED | 575 #ifndef REMAINDER_IS_ALREADY_CALCULATED |
579 rem=(n1-q*d0)&BN_MASK2; | 576 rem=(n1-q*d0)&BN_MASK2; |
580 #endif | 577 #endif |
581 | 578 |
582 #if defined(BN_UMULT_LOHI) | 579 #if defined(BN_UMULT_LOHI) |
583 BN_UMULT_LOHI(t2l,t2h,d1,q); | 580 BN_UMULT_LOHI(t2l,t2h,d1,q); |
584 #elif defined(BN_UMULT_HIGH) | 581 #elif defined(BN_UMULT_HIGH) |
585 t2l = d1 * q; | 582 t2l = d1 * q; |
586 t2h = BN_UMULT_HIGH(d1,q); | 583 t2h = BN_UMULT_HIGH(d1,q); |
587 #else | 584 #else |
| 585 { |
| 586 BN_ULONG ql, qh; |
588 t2l=LBITS(d1); t2h=HBITS(d1); | 587 t2l=LBITS(d1); t2h=HBITS(d1); |
589 ql =LBITS(q); qh =HBITS(q); | 588 ql =LBITS(q); qh =HBITS(q); |
590 mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */ | 589 mul64(t2l,t2h,ql,qh); /* t2=(BN_ULLONG)d1*q; */ |
| 590 } |
591 #endif | 591 #endif |
592 | 592 |
593 for (;;) | 593 for (;;) |
594 { | 594 { |
595 if ((t2h < rem) || | 595 if ((t2h < rem) || |
596 ((t2h == rem) && (t2l <= wnump[-2]))) | 596 ((t2h == rem) && (t2l <= wnump[-2]))) |
597 break; | 597 break; |
598 q--; | 598 q--; |
599 rem += d0; | 599 rem += d0; |
600 if (rem < d0) break; /* don't let rem overflow *
/ | 600 if (rem < d0) break; /* don't let rem overflow *
/ |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 bn_correct_top(res); | 641 bn_correct_top(res); |
642 BN_CTX_end(ctx); | 642 BN_CTX_end(ctx); |
643 return(1); | 643 return(1); |
644 err: | 644 err: |
645 bn_check_top(rm); | 645 bn_check_top(rm); |
646 BN_CTX_end(ctx); | 646 BN_CTX_end(ctx); |
647 return(0); | 647 return(0); |
648 } | 648 } |
649 | 649 |
650 #endif | 650 #endif |
OLD | NEW |