-
+ AA4E2EFA1E53416AEDF9CBF7A73A00FBDE7A8266F532002DC046B0B995CF49D060496C45C74944EC3F41CF0DD441884FF6230260978FCBF227903EB7213B074A
mpi/mpih-div.c
(0 . 0)(1 . 534)
8706 /* mpihelp-div.c - MPI helper functions
8707 * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
8708 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
8709 *
8710 * This file is part of GnuPG.
8711 *
8712 * GnuPG is free software; you can redistribute it and/or modify
8713 * it under the terms of the GNU General Public License as published by
8714 * the Free Software Foundation; either version 3 of the License, or
8715 * (at your option) any later version.
8716 *
8717 * GnuPG is distributed in the hope that it will be useful,
8718 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8719 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8720 * GNU General Public License for more details.
8721 *
8722 * You should have received a copy of the GNU General Public License
8723 * along with this program; if not, see <http://www.gnu.org/licenses/>.
8724 *
8725 * Note: This code is heavily based on the GNU MP Library.
8726 * Actually it's the same code with only minor changes in the
8727 * way the data is stored; this is to support the abstraction
8728 * of an optional secure memory allocation which may be used
8729 * to avoid revealing of sensitive data due to paging etc.
8730 * The GNU MP Library itself is published under the LGPL;
8731 * however I decided to publish this code under the plain GPL.
8732 */
8733
8734 #include <config.h>
8735 #include <stdio.h>
8736 #include <stdlib.h>
8737 #include "mpi-internal.h"
8738 #include "longlong.h"
8739
8740 #ifndef UMUL_TIME
8741 #define UMUL_TIME 1
8742 #endif
8743 #ifndef UDIV_TIME
8744 #define UDIV_TIME UMUL_TIME
8745 #endif
8746
8747 /* FIXME: We should be using invert_limb (or invert_normalized_limb)
8748 * here (not udiv_qrnnd).
8749 */
8750
8751 mpi_limb_t
8752 mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
8753 mpi_limb_t divisor_limb)
8754 {
8755 mpi_size_t i;
8756 mpi_limb_t n1, n0, r;
8757 int dummy;
8758
8759 /* Botch: Should this be handled at all? Rely on callers? */
8760 if( !dividend_size )
8761 return 0;
8762
8763 /* If multiplication is much faster than division, and the
8764 * dividend is large, pre-invert the divisor, and use
8765 * only multiplications in the inner loop.
8766 *
8767 * This test should be read:
8768 * Does it ever help to use udiv_qrnnd_preinv?
8769 * && Does what we save compensate for the inversion overhead?
8770 */
8771 if( UDIV_TIME > (2 * UMUL_TIME + 6)
8772 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
8773 int normalization_steps;
8774
8775 count_leading_zeros( normalization_steps, divisor_limb );
8776 if( normalization_steps ) {
8777 mpi_limb_t divisor_limb_inverted;
8778
8779 divisor_limb <<= normalization_steps;
8780
8781 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
8782 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
8783 * most significant bit (with weight 2**N) implicit.
8784 *
8785 * Special case for DIVISOR_LIMB == 100...000.
8786 */
8787 if( !(divisor_limb << 1) )
8788 divisor_limb_inverted = ~(mpi_limb_t)0;
8789 else
8790 udiv_qrnnd(divisor_limb_inverted, dummy,
8791 -divisor_limb, 0, divisor_limb);
8792
8793 n1 = dividend_ptr[dividend_size - 1];
8794 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
8795
8796 /* Possible optimization:
8797 * if (r == 0
8798 * && divisor_limb > ((n1 << normalization_steps)
8799 * | (dividend_ptr[dividend_size - 2] >> ...)))
8800 * ...one division less...
8801 */
8802 for( i = dividend_size - 2; i >= 0; i--) {
8803 n0 = dividend_ptr[i];
8804 UDIV_QRNND_PREINV(dummy, r, r,
8805 ((n1 << normalization_steps)
8806 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
8807 divisor_limb, divisor_limb_inverted);
8808 n1 = n0;
8809 }
8810 UDIV_QRNND_PREINV(dummy, r, r,
8811 n1 << normalization_steps,
8812 divisor_limb, divisor_limb_inverted);
8813 return r >> normalization_steps;
8814 }
8815 else {
8816 mpi_limb_t divisor_limb_inverted;
8817
8818 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
8819 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
8820 * most significant bit (with weight 2**N) implicit.
8821 *
8822 * Special case for DIVISOR_LIMB == 100...000.
8823 */
8824 if( !(divisor_limb << 1) )
8825 divisor_limb_inverted = ~(mpi_limb_t)0;
8826 else
8827 udiv_qrnnd(divisor_limb_inverted, dummy,
8828 -divisor_limb, 0, divisor_limb);
8829
8830 i = dividend_size - 1;
8831 r = dividend_ptr[i];
8832
8833 if( r >= divisor_limb )
8834 r = 0;
8835 else
8836 i--;
8837
8838 for( ; i >= 0; i--) {
8839 n0 = dividend_ptr[i];
8840 UDIV_QRNND_PREINV(dummy, r, r,
8841 n0, divisor_limb, divisor_limb_inverted);
8842 }
8843 return r;
8844 }
8845 }
8846 else {
8847 if( UDIV_NEEDS_NORMALIZATION ) {
8848 int normalization_steps;
8849
8850 count_leading_zeros(normalization_steps, divisor_limb);
8851 if( normalization_steps ) {
8852 divisor_limb <<= normalization_steps;
8853
8854 n1 = dividend_ptr[dividend_size - 1];
8855 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
8856
8857 /* Possible optimization:
8858 * if (r == 0
8859 * && divisor_limb > ((n1 << normalization_steps)
8860 * | (dividend_ptr[dividend_size - 2] >> ...)))
8861 * ...one division less...
8862 */
8863 for(i = dividend_size - 2; i >= 0; i--) {
8864 n0 = dividend_ptr[i];
8865 udiv_qrnnd (dummy, r, r,
8866 ((n1 << normalization_steps)
8867 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
8868 divisor_limb);
8869 n1 = n0;
8870 }
8871 udiv_qrnnd (dummy, r, r,
8872 n1 << normalization_steps,
8873 divisor_limb);
8874 return r >> normalization_steps;
8875 }
8876 }
8877 /* No normalization needed, either because udiv_qrnnd doesn't require
8878 * it, or because DIVISOR_LIMB is already normalized. */
8879 i = dividend_size - 1;
8880 r = dividend_ptr[i];
8881
8882 if(r >= divisor_limb)
8883 r = 0;
8884 else
8885 i--;
8886
8887 for(; i >= 0; i--) {
8888 n0 = dividend_ptr[i];
8889 udiv_qrnnd (dummy, r, r, n0, divisor_limb);
8890 }
8891 return r;
8892 }
8893 }
8894
8895 /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
8896 * the NSIZE-DSIZE least significant quotient limbs at QP
8897 * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
8898 * non-zero, generate that many fraction bits and append them after the
8899 * other quotient limbs.
8900 * Return the most significant limb of the quotient, this is always 0 or 1.
8901 *
8902 * Preconditions:
8903 * 0. NSIZE >= DSIZE.
8904 * 1. The most significant bit of the divisor must be set.
8905 * 2. QP must either not overlap with the input operands at all, or
8906 * QP + DSIZE >= NP must hold true. (This means that it's
8907 * possible to put the quotient in the high part of NUM, right after the
8908 * remainder in NUM.
8909 * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
8910 */
8911
8912 mpi_limb_t
8913 mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
8914 mpi_ptr_t np, mpi_size_t nsize,
8915 mpi_ptr_t dp, mpi_size_t dsize)
8916 {
8917 mpi_limb_t most_significant_q_limb = 0;
8918
8919 switch(dsize) {
8920 case 0:
8921 /* We are asked to divide by zero, so go ahead and do it! (To make
8922 the compiler not remove this statement, return the value.) */
8923 return 1 / dsize;
8924
8925 case 1:
8926 {
8927 mpi_size_t i;
8928 mpi_limb_t n1;
8929 mpi_limb_t d;
8930
8931 d = dp[0];
8932 n1 = np[nsize - 1];
8933
8934 if( n1 >= d ) {
8935 n1 -= d;
8936 most_significant_q_limb = 1;
8937 }
8938
8939 qp += qextra_limbs;
8940 for( i = nsize - 2; i >= 0; i--)
8941 udiv_qrnnd( qp[i], n1, n1, np[i], d );
8942 qp -= qextra_limbs;
8943
8944 for( i = qextra_limbs - 1; i >= 0; i-- )
8945 udiv_qrnnd (qp[i], n1, n1, 0, d);
8946
8947 np[0] = n1;
8948 }
8949 break;
8950
8951 case 2:
8952 {
8953 mpi_size_t i;
8954 mpi_limb_t n1, n0, n2;
8955 mpi_limb_t d1, d0;
8956
8957 np += nsize - 2;
8958 d1 = dp[1];
8959 d0 = dp[0];
8960 n1 = np[1];
8961 n0 = np[0];
8962
8963 if( n1 >= d1 && (n1 > d1 || n0 >= d0) ) {
8964 sub_ddmmss (n1, n0, n1, n0, d1, d0);
8965 most_significant_q_limb = 1;
8966 }
8967
8968 for( i = qextra_limbs + nsize - 2 - 1; i >= 0; i-- ) {
8969 mpi_limb_t q;
8970 mpi_limb_t r;
8971
8972 if( i >= qextra_limbs )
8973 np--;
8974 else
8975 np[0] = 0;
8976
8977 if( n1 == d1 ) {
8978 /* Q should be either 111..111 or 111..110. Need special
8979 * treatment of this rare case as normal division would
8980 * give overflow. */
8981 q = ~(mpi_limb_t)0;
8982
8983 r = n0 + d1;
8984 if( r < d1 ) { /* Carry in the addition? */
8985 add_ssaaaa( n1, n0, r - d0, np[0], 0, d0 );
8986 qp[i] = q;
8987 continue;
8988 }
8989 n1 = d0 - (d0 != 0?1:0);
8990 n0 = -d0;
8991 }
8992 else {
8993 udiv_qrnnd (q, r, n1, n0, d1);
8994 umul_ppmm (n1, n0, d0, q);
8995 }
8996
8997 n2 = np[0];
8998 q_test:
8999 if( n1 > r || (n1 == r && n0 > n2) ) {
9000 /* The estimated Q was too large. */
9001 q--;
9002 sub_ddmmss (n1, n0, n1, n0, 0, d0);
9003 r += d1;
9004 if( r >= d1 ) /* If not carry, test Q again. */
9005 goto q_test;
9006 }
9007
9008 qp[i] = q;
9009 sub_ddmmss (n1, n0, r, n2, n1, n0);
9010 }
9011 np[1] = n1;
9012 np[0] = n0;
9013 }
9014 break;
9015
9016 default:
9017 {
9018 mpi_size_t i;
9019 mpi_limb_t dX, d1, n0;
9020
9021 np += nsize - dsize;
9022 dX = dp[dsize - 1];
9023 d1 = dp[dsize - 2];
9024 n0 = np[dsize - 1];
9025
9026 if( n0 >= dX ) {
9027 if(n0 > dX || mpihelp_cmp(np, dp, dsize - 1) >= 0 ) {
9028 mpihelp_sub_n(np, np, dp, dsize);
9029 n0 = np[dsize - 1];
9030 most_significant_q_limb = 1;
9031 }
9032 }
9033
9034 for( i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
9035 mpi_limb_t q;
9036 mpi_limb_t n1, n2;
9037 mpi_limb_t cy_limb;
9038
9039 if( i >= qextra_limbs ) {
9040 np--;
9041 n2 = np[dsize];
9042 }
9043 else {
9044 n2 = np[dsize - 1];
9045 MPN_COPY_DECR (np + 1, np, dsize - 1);
9046 np[0] = 0;
9047 }
9048
9049 if( n0 == dX ) {
9050 /* This might over-estimate q, but it's probably not worth
9051 * the extra code here to find out. */
9052 q = ~(mpi_limb_t)0;
9053 }
9054 else {
9055 mpi_limb_t r;
9056
9057 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
9058 umul_ppmm(n1, n0, d1, q);
9059
9060 while( n1 > r || (n1 == r && n0 > np[dsize - 2])) {
9061 q--;
9062 r += dX;
9063 if( r < dX ) /* I.e. "carry in previous addition?" */
9064 break;
9065 n1 -= n0 < d1;
9066 n0 -= d1;
9067 }
9068 }
9069
9070 /* Possible optimization: We already have (q * n0) and (1 * n1)
9071 * after the calculation of q. Taking advantage of that, we
9072 * could make this loop make two iterations less. */
9073 cy_limb = mpihelp_submul_1(np, dp, dsize, q);
9074
9075 if( n2 != cy_limb ) {
9076 mpihelp_add_n(np, np, dp, dsize);
9077 q--;
9078 }
9079
9080 qp[i] = q;
9081 n0 = np[dsize - 1];
9082 }
9083 }
9084 }
9085
9086 return most_significant_q_limb;
9087 }
9088
9089
9090 /****************
9091 * Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
9092 * Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
9093 * Return the single-limb remainder.
9094 * There are no constraints on the value of the divisor.
9095 *
9096 * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
9097 */
9098
9099 mpi_limb_t
9100 mpihelp_divmod_1( mpi_ptr_t quot_ptr,
9101 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
9102 mpi_limb_t divisor_limb)
9103 {
9104 mpi_size_t i;
9105 mpi_limb_t n1, n0, r;
9106 int dummy;
9107
9108 if( !dividend_size )
9109 return 0;
9110
9111 /* If multiplication is much faster than division, and the
9112 * dividend is large, pre-invert the divisor, and use
9113 * only multiplications in the inner loop.
9114 *
9115 * This test should be read:
9116 * Does it ever help to use udiv_qrnnd_preinv?
9117 * && Does what we save compensate for the inversion overhead?
9118 */
9119 if( UDIV_TIME > (2 * UMUL_TIME + 6)
9120 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
9121 int normalization_steps;
9122
9123 count_leading_zeros( normalization_steps, divisor_limb );
9124 if( normalization_steps ) {
9125 mpi_limb_t divisor_limb_inverted;
9126
9127 divisor_limb <<= normalization_steps;
9128
9129 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
9130 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
9131 * most significant bit (with weight 2**N) implicit.
9132 */
9133 /* Special case for DIVISOR_LIMB == 100...000. */
9134 if( !(divisor_limb << 1) )
9135 divisor_limb_inverted = ~(mpi_limb_t)0;
9136 else
9137 udiv_qrnnd(divisor_limb_inverted, dummy,
9138 -divisor_limb, 0, divisor_limb);
9139
9140 n1 = dividend_ptr[dividend_size - 1];
9141 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
9142
9143 /* Possible optimization:
9144 * if (r == 0
9145 * && divisor_limb > ((n1 << normalization_steps)
9146 * | (dividend_ptr[dividend_size - 2] >> ...)))
9147 * ...one division less...
9148 */
9149 for( i = dividend_size - 2; i >= 0; i--) {
9150 n0 = dividend_ptr[i];
9151 UDIV_QRNND_PREINV( quot_ptr[i + 1], r, r,
9152 ((n1 << normalization_steps)
9153 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
9154 divisor_limb, divisor_limb_inverted);
9155 n1 = n0;
9156 }
9157 UDIV_QRNND_PREINV( quot_ptr[0], r, r,
9158 n1 << normalization_steps,
9159 divisor_limb, divisor_limb_inverted);
9160 return r >> normalization_steps;
9161 }
9162 else {
9163 mpi_limb_t divisor_limb_inverted;
9164
9165 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
9166 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
9167 * most significant bit (with weight 2**N) implicit.
9168 */
9169 /* Special case for DIVISOR_LIMB == 100...000. */
9170 if( !(divisor_limb << 1) )
9171 divisor_limb_inverted = ~(mpi_limb_t) 0;
9172 else
9173 udiv_qrnnd(divisor_limb_inverted, dummy,
9174 -divisor_limb, 0, divisor_limb);
9175
9176 i = dividend_size - 1;
9177 r = dividend_ptr[i];
9178
9179 if( r >= divisor_limb )
9180 r = 0;
9181 else
9182 quot_ptr[i--] = 0;
9183
9184 for( ; i >= 0; i-- ) {
9185 n0 = dividend_ptr[i];
9186 UDIV_QRNND_PREINV( quot_ptr[i], r, r,
9187 n0, divisor_limb, divisor_limb_inverted);
9188 }
9189 return r;
9190 }
9191 }
9192 else {
9193 if(UDIV_NEEDS_NORMALIZATION) {
9194 int normalization_steps;
9195
9196 count_leading_zeros (normalization_steps, divisor_limb);
9197 if( normalization_steps ) {
9198 divisor_limb <<= normalization_steps;
9199
9200 n1 = dividend_ptr[dividend_size - 1];
9201 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
9202
9203 /* Possible optimization:
9204 * if (r == 0
9205 * && divisor_limb > ((n1 << normalization_steps)
9206 * | (dividend_ptr[dividend_size - 2] >> ...)))
9207 * ...one division less...
9208 */
9209 for( i = dividend_size - 2; i >= 0; i--) {
9210 n0 = dividend_ptr[i];
9211 udiv_qrnnd (quot_ptr[i + 1], r, r,
9212 ((n1 << normalization_steps)
9213 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
9214 divisor_limb);
9215 n1 = n0;
9216 }
9217 udiv_qrnnd (quot_ptr[0], r, r,
9218 n1 << normalization_steps,
9219 divisor_limb);
9220 return r >> normalization_steps;
9221 }
9222 }
9223 /* No normalization needed, either because udiv_qrnnd doesn't require
9224 * it, or because DIVISOR_LIMB is already normalized. */
9225 i = dividend_size - 1;
9226 r = dividend_ptr[i];
9227
9228 if(r >= divisor_limb)
9229 r = 0;
9230 else
9231 quot_ptr[i--] = 0;
9232
9233 for(; i >= 0; i--) {
9234 n0 = dividend_ptr[i];
9235 udiv_qrnnd( quot_ptr[i], r, r, n0, divisor_limb );
9236 }
9237 return r;
9238 }
9239 }