-
+ AA4E2EFA1E53416AEDF9CBF7A73A00FBDE7A8266F532002DC046B0B995CF49D060496C45C74944EC3F41CF0DD441884FF6230260978FCBF227903EB7213B074A
mpi/mpih-div.c
(0 . 0)(1 . 534)
7562 /* mpihelp-div.c - MPI helper functions
7563 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
7564 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
7565 *
7566 * This file is part of GnuPG.
7567 *
7568 * GnuPG is free software; you can redistribute it and/or modify
7569 * it under the terms of the GNU General Public License as published by
7570 * the Free Software Foundation; either version 3 of the License, or
7571 * (at your option) any later version.
7572 *
7573 * GnuPG is distributed in the hope that it will be useful,
7574 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7575 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7576 * GNU General Public License for more details.
7577 *
7578 * You should have received a copy of the GNU General Public License
7579 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7580 *
7581 * Note: This code is heavily based on the GNU MP Library.
7582 * Actually it's the same code with only minor changes in the
7583 * way the data is stored; this is to support the abstraction
7584 * of an optional secure memory allocation which may be used
7585 * to avoid revealing of sensitive data due to paging etc.
7586 * The GNU MP Library itself is published under the LGPL;
7587 * however I decided to publish this code under the plain GPL.
7588 */
7589
7590 #include <config.h>
7591 #include <stdio.h>
7592 #include <stdlib.h>
7593 #include "mpi-internal.h"
7594 #include "longlong.h"
7595
7596 #ifndef UMUL_TIME
7597 #define UMUL_TIME 1
7598 #endif
7599 #ifndef UDIV_TIME
7600 #define UDIV_TIME UMUL_TIME
7601 #endif
7602
7603 /* FIXME: We should be using invert_limb (or invert_normalized_limb)
7604 * here (not udiv_qrnnd).
7605 */
7606
7607 mpi_limb_t
7608 mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
7609 mpi_limb_t divisor_limb)
7610 {
7611 mpi_size_t i;
7612 mpi_limb_t n1, n0, r;
7613 int dummy;
7614
7615 /* Botch: Should this be handled at all? Rely on callers? */
7616 if( !dividend_size )
7617 return 0;
7618
7619 /* If multiplication is much faster than division, and the
7620 * dividend is large, pre-invert the divisor, and use
7621 * only multiplications in the inner loop.
7622 *
7623 * This test should be read:
7624 * Does it ever help to use udiv_qrnnd_preinv?
7625 * && Does what we save compensate for the inversion overhead?
7626 */
7627 if( UDIV_TIME > (2 * UMUL_TIME + 6)
7628 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
7629 int normalization_steps;
7630
7631 count_leading_zeros( normalization_steps, divisor_limb );
7632 if( normalization_steps ) {
7633 mpi_limb_t divisor_limb_inverted;
7634
7635 divisor_limb <<= normalization_steps;
7636
7637 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
7638 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
7639 * most significant bit (with weight 2**N) implicit.
7640 *
7641 * Special case for DIVISOR_LIMB == 100...000.
7642 */
7643 if( !(divisor_limb << 1) )
7644 divisor_limb_inverted = ~(mpi_limb_t)0;
7645 else
7646 udiv_qrnnd(divisor_limb_inverted, dummy,
7647 -divisor_limb, 0, divisor_limb);
7648
7649 n1 = dividend_ptr[dividend_size - 1];
7650 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
7651
7652 /* Possible optimization:
7653 * if (r == 0
7654 * && divisor_limb > ((n1 << normalization_steps)
7655 * | (dividend_ptr[dividend_size - 2] >> ...)))
7656 * ...one division less...
7657 */
7658 for( i = dividend_size - 2; i >= 0; i--) {
7659 n0 = dividend_ptr[i];
7660 UDIV_QRNND_PREINV(dummy, r, r,
7661 ((n1 << normalization_steps)
7662 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
7663 divisor_limb, divisor_limb_inverted);
7664 n1 = n0;
7665 }
7666 UDIV_QRNND_PREINV(dummy, r, r,
7667 n1 << normalization_steps,
7668 divisor_limb, divisor_limb_inverted);
7669 return r >> normalization_steps;
7670 }
7671 else {
7672 mpi_limb_t divisor_limb_inverted;
7673
7674 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
7675 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
7676 * most significant bit (with weight 2**N) implicit.
7677 *
7678 * Special case for DIVISOR_LIMB == 100...000.
7679 */
7680 if( !(divisor_limb << 1) )
7681 divisor_limb_inverted = ~(mpi_limb_t)0;
7682 else
7683 udiv_qrnnd(divisor_limb_inverted, dummy,
7684 -divisor_limb, 0, divisor_limb);
7685
7686 i = dividend_size - 1;
7687 r = dividend_ptr[i];
7688
7689 if( r >= divisor_limb )
7690 r = 0;
7691 else
7692 i--;
7693
7694 for( ; i >= 0; i--) {
7695 n0 = dividend_ptr[i];
7696 UDIV_QRNND_PREINV(dummy, r, r,
7697 n0, divisor_limb, divisor_limb_inverted);
7698 }
7699 return r;
7700 }
7701 }
7702 else {
7703 if( UDIV_NEEDS_NORMALIZATION ) {
7704 int normalization_steps;
7705
7706 count_leading_zeros(normalization_steps, divisor_limb);
7707 if( normalization_steps ) {
7708 divisor_limb <<= normalization_steps;
7709
7710 n1 = dividend_ptr[dividend_size - 1];
7711 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
7712
7713 /* Possible optimization:
7714 * if (r == 0
7715 * && divisor_limb > ((n1 << normalization_steps)
7716 * | (dividend_ptr[dividend_size - 2] >> ...)))
7717 * ...one division less...
7718 */
7719 for(i = dividend_size - 2; i >= 0; i--) {
7720 n0 = dividend_ptr[i];
7721 udiv_qrnnd (dummy, r, r,
7722 ((n1 << normalization_steps)
7723 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
7724 divisor_limb);
7725 n1 = n0;
7726 }
7727 udiv_qrnnd (dummy, r, r,
7728 n1 << normalization_steps,
7729 divisor_limb);
7730 return r >> normalization_steps;
7731 }
7732 }
7733 /* No normalization needed, either because udiv_qrnnd doesn't require
7734 * it, or because DIVISOR_LIMB is already normalized. */
7735 i = dividend_size - 1;
7736 r = dividend_ptr[i];
7737
7738 if(r >= divisor_limb)
7739 r = 0;
7740 else
7741 i--;
7742
7743 for(; i >= 0; i--) {
7744 n0 = dividend_ptr[i];
7745 udiv_qrnnd (dummy, r, r, n0, divisor_limb);
7746 }
7747 return r;
7748 }
7749 }
7750
7751 /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
7752 * the NSIZE-DSIZE least significant quotient limbs at QP
7753 * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
7754 * non-zero, generate that many fraction bits and append them after the
7755 * other quotient limbs.
7756 * Return the most significant limb of the quotient, this is always 0 or 1.
7757 *
7758 * Preconditions:
7759 * 0. NSIZE >= DSIZE.
7760 * 1. The most significant bit of the divisor must be set.
7761 * 2. QP must either not overlap with the input operands at all, or
7762 * QP + DSIZE >= NP must hold true. (This means that it's
7763 * possible to put the quotient in the high part of NUM, right after the
7764 * remainder in NUM.
7765 * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
7766 */
7767
7768 mpi_limb_t
7769 mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
7770 mpi_ptr_t np, mpi_size_t nsize,
7771 mpi_ptr_t dp, mpi_size_t dsize)
7772 {
7773 mpi_limb_t most_significant_q_limb = 0;
7774
7775 switch(dsize) {
7776 case 0:
7777 /* We are asked to divide by zero, so go ahead and do it! (To make
7778 the compiler not remove this statement, return the value.) */
7779 return 1 / dsize;
7780
7781 case 1:
7782 {
7783 mpi_size_t i;
7784 mpi_limb_t n1;
7785 mpi_limb_t d;
7786
7787 d = dp[0];
7788 n1 = np[nsize - 1];
7789
7790 if( n1 >= d ) {
7791 n1 -= d;
7792 most_significant_q_limb = 1;
7793 }
7794
7795 qp += qextra_limbs;
7796 for( i = nsize - 2; i >= 0; i--)
7797 udiv_qrnnd( qp[i], n1, n1, np[i], d );
7798 qp -= qextra_limbs;
7799
7800 for( i = qextra_limbs - 1; i >= 0; i-- )
7801 udiv_qrnnd (qp[i], n1, n1, 0, d);
7802
7803 np[0] = n1;
7804 }
7805 break;
7806
7807 case 2:
7808 {
7809 mpi_size_t i;
7810 mpi_limb_t n1, n0, n2;
7811 mpi_limb_t d1, d0;
7812
7813 np += nsize - 2;
7814 d1 = dp[1];
7815 d0 = dp[0];
7816 n1 = np[1];
7817 n0 = np[0];
7818
7819 if( n1 >= d1 && (n1 > d1 || n0 >= d0) ) {
7820 sub_ddmmss (n1, n0, n1, n0, d1, d0);
7821 most_significant_q_limb = 1;
7822 }
7823
7824 for( i = qextra_limbs + nsize - 2 - 1; i >= 0; i-- ) {
7825 mpi_limb_t q;
7826 mpi_limb_t r;
7827
7828 if( i >= qextra_limbs )
7829 np--;
7830 else
7831 np[0] = 0;
7832
7833 if( n1 == d1 ) {
7834 /* Q should be either 111..111 or 111..110. Need special
7835 * treatment of this rare case as normal division would
7836 * give overflow. */
7837 q = ~(mpi_limb_t)0;
7838
7839 r = n0 + d1;
7840 if( r < d1 ) { /* Carry in the addition? */
7841 add_ssaaaa( n1, n0, r - d0, np[0], 0, d0 );
7842 qp[i] = q;
7843 continue;
7844 }
7845 n1 = d0 - (d0 != 0?1:0);
7846 n0 = -d0;
7847 }
7848 else {
7849 udiv_qrnnd (q, r, n1, n0, d1);
7850 umul_ppmm (n1, n0, d0, q);
7851 }
7852
7853 n2 = np[0];
7854 q_test:
7855 if( n1 > r || (n1 == r && n0 > n2) ) {
7856 /* The estimated Q was too large. */
7857 q--;
7858 sub_ddmmss (n1, n0, n1, n0, 0, d0);
7859 r += d1;
7860 if( r >= d1 ) /* If not carry, test Q again. */
7861 goto q_test;
7862 }
7863
7864 qp[i] = q;
7865 sub_ddmmss (n1, n0, r, n2, n1, n0);
7866 }
7867 np[1] = n1;
7868 np[0] = n0;
7869 }
7870 break;
7871
7872 default:
7873 {
7874 mpi_size_t i;
7875 mpi_limb_t dX, d1, n0;
7876
7877 np += nsize - dsize;
7878 dX = dp[dsize - 1];
7879 d1 = dp[dsize - 2];
7880 n0 = np[dsize - 1];
7881
7882 if( n0 >= dX ) {
7883 if(n0 > dX || mpihelp_cmp(np, dp, dsize - 1) >= 0 ) {
7884 mpihelp_sub_n(np, np, dp, dsize);
7885 n0 = np[dsize - 1];
7886 most_significant_q_limb = 1;
7887 }
7888 }
7889
7890 for( i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
7891 mpi_limb_t q;
7892 mpi_limb_t n1, n2;
7893 mpi_limb_t cy_limb;
7894
7895 if( i >= qextra_limbs ) {
7896 np--;
7897 n2 = np[dsize];
7898 }
7899 else {
7900 n2 = np[dsize - 1];
7901 MPN_COPY_DECR (np + 1, np, dsize - 1);
7902 np[0] = 0;
7903 }
7904
7905 if( n0 == dX ) {
7906 /* This might over-estimate q, but it's probably not worth
7907 * the extra code here to find out. */
7908 q = ~(mpi_limb_t)0;
7909 }
7910 else {
7911 mpi_limb_t r;
7912
7913 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
7914 umul_ppmm(n1, n0, d1, q);
7915
7916 while( n1 > r || (n1 == r && n0 > np[dsize - 2])) {
7917 q--;
7918 r += dX;
7919 if( r < dX ) /* I.e. "carry in previous addition?" */
7920 break;
7921 n1 -= n0 < d1;
7922 n0 -= d1;
7923 }
7924 }
7925
7926 /* Possible optimization: We already have (q * n0) and (1 * n1)
7927 * after the calculation of q. Taking advantage of that, we
7928 * could make this loop make two iterations less. */
7929 cy_limb = mpihelp_submul_1(np, dp, dsize, q);
7930
7931 if( n2 != cy_limb ) {
7932 mpihelp_add_n(np, np, dp, dsize);
7933 q--;
7934 }
7935
7936 qp[i] = q;
7937 n0 = np[dsize - 1];
7938 }
7939 }
7940 }
7941
7942 return most_significant_q_limb;
7943 }
7944
7945
7946 /****************
7947 * Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
7948 * Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
7949 * Return the single-limb remainder.
7950 * There are no constraints on the value of the divisor.
7951 *
7952 * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
7953 */
7954
7955 mpi_limb_t
7956 mpihelp_divmod_1( mpi_ptr_t quot_ptr,
7957 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
7958 mpi_limb_t divisor_limb)
7959 {
7960 mpi_size_t i;
7961 mpi_limb_t n1, n0, r;
7962 int dummy;
7963
7964 if( !dividend_size )
7965 return 0;
7966
7967 /* If multiplication is much faster than division, and the
7968 * dividend is large, pre-invert the divisor, and use
7969 * only multiplications in the inner loop.
7970 *
7971 * This test should be read:
7972 * Does it ever help to use udiv_qrnnd_preinv?
7973 * && Does what we save compensate for the inversion overhead?
7974 */
7975 if( UDIV_TIME > (2 * UMUL_TIME + 6)
7976 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
7977 int normalization_steps;
7978
7979 count_leading_zeros( normalization_steps, divisor_limb );
7980 if( normalization_steps ) {
7981 mpi_limb_t divisor_limb_inverted;
7982
7983 divisor_limb <<= normalization_steps;
7984
7985 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
7986 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
7987 * most significant bit (with weight 2**N) implicit.
7988 */
7989 /* Special case for DIVISOR_LIMB == 100...000. */
7990 if( !(divisor_limb << 1) )
7991 divisor_limb_inverted = ~(mpi_limb_t)0;
7992 else
7993 udiv_qrnnd(divisor_limb_inverted, dummy,
7994 -divisor_limb, 0, divisor_limb);
7995
7996 n1 = dividend_ptr[dividend_size - 1];
7997 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
7998
7999 /* Possible optimization:
8000 * if (r == 0
8001 * && divisor_limb > ((n1 << normalization_steps)
8002 * | (dividend_ptr[dividend_size - 2] >> ...)))
8003 * ...one division less...
8004 */
8005 for( i = dividend_size - 2; i >= 0; i--) {
8006 n0 = dividend_ptr[i];
8007 UDIV_QRNND_PREINV( quot_ptr[i + 1], r, r,
8008 ((n1 << normalization_steps)
8009 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
8010 divisor_limb, divisor_limb_inverted);
8011 n1 = n0;
8012 }
8013 UDIV_QRNND_PREINV( quot_ptr[0], r, r,
8014 n1 << normalization_steps,
8015 divisor_limb, divisor_limb_inverted);
8016 return r >> normalization_steps;
8017 }
8018 else {
8019 mpi_limb_t divisor_limb_inverted;
8020
8021 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
8022 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
8023 * most significant bit (with weight 2**N) implicit.
8024 */
8025 /* Special case for DIVISOR_LIMB == 100...000. */
8026 if( !(divisor_limb << 1) )
8027 divisor_limb_inverted = ~(mpi_limb_t) 0;
8028 else
8029 udiv_qrnnd(divisor_limb_inverted, dummy,
8030 -divisor_limb, 0, divisor_limb);
8031
8032 i = dividend_size - 1;
8033 r = dividend_ptr[i];
8034
8035 if( r >= divisor_limb )
8036 r = 0;
8037 else
8038 quot_ptr[i--] = 0;
8039
8040 for( ; i >= 0; i-- ) {
8041 n0 = dividend_ptr[i];
8042 UDIV_QRNND_PREINV( quot_ptr[i], r, r,
8043 n0, divisor_limb, divisor_limb_inverted);
8044 }
8045 return r;
8046 }
8047 }
8048 else {
8049 if(UDIV_NEEDS_NORMALIZATION) {
8050 int normalization_steps;
8051
8052 count_leading_zeros (normalization_steps, divisor_limb);
8053 if( normalization_steps ) {
8054 divisor_limb <<= normalization_steps;
8055
8056 n1 = dividend_ptr[dividend_size - 1];
8057 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
8058
8059 /* Possible optimization:
8060 * if (r == 0
8061 * && divisor_limb > ((n1 << normalization_steps)
8062 * | (dividend_ptr[dividend_size - 2] >> ...)))
8063 * ...one division less...
8064 */
8065 for( i = dividend_size - 2; i >= 0; i--) {
8066 n0 = dividend_ptr[i];
8067 udiv_qrnnd (quot_ptr[i + 1], r, r,
8068 ((n1 << normalization_steps)
8069 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
8070 divisor_limb);
8071 n1 = n0;
8072 }
8073 udiv_qrnnd (quot_ptr[0], r, r,
8074 n1 << normalization_steps,
8075 divisor_limb);
8076 return r >> normalization_steps;
8077 }
8078 }
8079 /* No normalization needed, either because udiv_qrnnd doesn't require
8080 * it, or because DIVISOR_LIMB is already normalized. */
8081 i = dividend_size - 1;
8082 r = dividend_ptr[i];
8083
8084 if(r >= divisor_limb)
8085 r = 0;
8086 else
8087 quot_ptr[i--] = 0;
8088
8089 for(; i >= 0; i--) {
8090 n0 = dividend_ptr[i];
8091 udiv_qrnnd( quot_ptr[i], r, r, n0, divisor_limb );
8092 }
8093 return r;
8094 }
8095 }