-
+ 49EF731356E9C2E54D04CB0F5C4A68285AD1CA1C27015AACEFDA5F0CB5AC676A0DC47B1656AB2057E1CFFCC58E837B1010E85391A8F570CCDDA24630B28620F7
smg_comms/mpi/mpih-div.c
(0 . 0)(1 . 529)
5892 /* mpihelp-div.c - MPI helper functions
5893 * Modified by No Such Labs. (C) 2015. See README.
5894 *
5895 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
5896 * SHA256(gnupg-1.4.10.tar.gz):
5897 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
5898 * (C) 1994-2005 Free Software Foundation, Inc.
5899 *
5900 * This program is free software: you can redistribute it and/or modify
5901 * it under the terms of the GNU General Public License as published by
5902 * the Free Software Foundation, either version 3 of the License, or
5903 * (at your option) any later version.
5904 *
5905 * This program is distributed in the hope that it will be useful,
5906 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5907 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5908 * GNU General Public License for more details.
5909 *
5910 * You should have received a copy of the GNU General Public License
5911 * along with this program. If not, see <http://www.gnu.org/licenses/>.
5912 */
5913
5914 #include <stdio.h>
5915 #include <stdlib.h>
5916
5917 #include "knobs.h"
5918 #include "mpi-internal.h"
5919 #include "longlong.h"
5920
5921 #ifndef UMUL_TIME
5922 #define UMUL_TIME 1
5923 #endif
5924 #ifndef UDIV_TIME
5925 #define UDIV_TIME UMUL_TIME
5926 #endif
5927
5928 /* FIXME: We should be using invert_limb (or invert_normalized_limb)
5929 * here (not udiv_qrnnd).
5930 */
5931
5932 mpi_limb_t
5933 mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
5934 mpi_limb_t divisor_limb)
5935 {
5936 mpi_size_t i;
5937 mpi_limb_t n1, n0, r;
5938 int dummy;
5939
5940 /* Botch: Should this be handled at all? Rely on callers? */
5941 if( !dividend_size )
5942 return 0;
5943
5944 /* If multiplication is much faster than division, and the
5945 * dividend is large, pre-invert the divisor, and use
5946 * only multiplications in the inner loop.
5947 *
5948 * This test should be read:
5949 * Does it ever help to use udiv_qrnnd_preinv?
5950 * && Does what we save compensate for the inversion overhead?
5951 */
5952 if( UDIV_TIME > (2 * UMUL_TIME + 6)
5953 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
5954 int normalization_steps;
5955
5956 count_leading_zeros( normalization_steps, divisor_limb );
5957 if( normalization_steps ) {
5958 mpi_limb_t divisor_limb_inverted;
5959
5960 divisor_limb <<= normalization_steps;
5961
5962 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
5963 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
5964 * most significant bit (with weight 2**N) implicit.
5965 *
5966 * Special case for DIVISOR_LIMB == 100...000.
5967 */
5968 if( !(divisor_limb << 1) )
5969 divisor_limb_inverted = ~(mpi_limb_t)0;
5970 else
5971 udiv_qrnnd(divisor_limb_inverted, dummy,
5972 -divisor_limb, 0, divisor_limb);
5973
5974 n1 = dividend_ptr[dividend_size - 1];
5975 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
5976
5977 /* Possible optimization:
5978 * if (r == 0
5979 * && divisor_limb > ((n1 << normalization_steps)
5980 * | (dividend_ptr[dividend_size - 2] >> ...)))
5981 * ...one division less...
5982 */
5983 for( i = dividend_size - 2; i >= 0; i--) {
5984 n0 = dividend_ptr[i];
5985 UDIV_QRNND_PREINV(dummy, r, r,
5986 ((n1 << normalization_steps)
5987 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
5988 divisor_limb, divisor_limb_inverted);
5989 n1 = n0;
5990 }
5991 UDIV_QRNND_PREINV(dummy, r, r,
5992 n1 << normalization_steps,
5993 divisor_limb, divisor_limb_inverted);
5994 return r >> normalization_steps;
5995 }
5996 else {
5997 mpi_limb_t divisor_limb_inverted;
5998
5999 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
6000 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
6001 * most significant bit (with weight 2**N) implicit.
6002 *
6003 * Special case for DIVISOR_LIMB == 100...000.
6004 */
6005 if( !(divisor_limb << 1) )
6006 divisor_limb_inverted = ~(mpi_limb_t)0;
6007 else
6008 udiv_qrnnd(divisor_limb_inverted, dummy,
6009 -divisor_limb, 0, divisor_limb);
6010
6011 i = dividend_size - 1;
6012 r = dividend_ptr[i];
6013
6014 if( r >= divisor_limb )
6015 r = 0;
6016 else
6017 i--;
6018
6019 for( ; i >= 0; i--) {
6020 n0 = dividend_ptr[i];
6021 UDIV_QRNND_PREINV(dummy, r, r,
6022 n0, divisor_limb, divisor_limb_inverted);
6023 }
6024 return r;
6025 }
6026 }
6027 else {
6028 if( UDIV_NEEDS_NORMALIZATION ) {
6029 int normalization_steps;
6030
6031 count_leading_zeros(normalization_steps, divisor_limb);
6032 if( normalization_steps ) {
6033 divisor_limb <<= normalization_steps;
6034
6035 n1 = dividend_ptr[dividend_size - 1];
6036 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
6037
6038 /* Possible optimization:
6039 * if (r == 0
6040 * && divisor_limb > ((n1 << normalization_steps)
6041 * | (dividend_ptr[dividend_size - 2] >> ...)))
6042 * ...one division less...
6043 */
6044 for(i = dividend_size - 2; i >= 0; i--) {
6045 n0 = dividend_ptr[i];
6046 udiv_qrnnd (dummy, r, r,
6047 ((n1 << normalization_steps)
6048 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
6049 divisor_limb);
6050 n1 = n0;
6051 }
6052 udiv_qrnnd (dummy, r, r,
6053 n1 << normalization_steps,
6054 divisor_limb);
6055 return r >> normalization_steps;
6056 }
6057 }
6058 /* No normalization needed, either because udiv_qrnnd doesn't require
6059 * it, or because DIVISOR_LIMB is already normalized. */
6060 i = dividend_size - 1;
6061 r = dividend_ptr[i];
6062
6063 if(r >= divisor_limb)
6064 r = 0;
6065 else
6066 i--;
6067
6068 for(; i >= 0; i--) {
6069 n0 = dividend_ptr[i];
6070 udiv_qrnnd (dummy, r, r, n0, divisor_limb);
6071 }
6072 return r;
6073 }
6074 }
6075
6076 /* Divide num (NP/NSIZE) by den (DP/DSIZE) and write
6077 * the NSIZE-DSIZE least significant quotient limbs at QP
6078 * and the DSIZE long remainder at NP. If QEXTRA_LIMBS is
6079 * non-zero, generate that many fraction bits and append them after the
6080 * other quotient limbs.
6081 * Return the most significant limb of the quotient, this is always 0 or 1.
6082 *
6083 * Preconditions:
6084 * 0. NSIZE >= DSIZE.
6085 * 1. The most significant bit of the divisor must be set.
6086 * 2. QP must either not overlap with the input operands at all, or
6087 * QP + DSIZE >= NP must hold true. (This means that it's
6088 * possible to put the quotient in the high part of NUM, right after the
6089 * remainder in NUM.
6090 * 3. NSIZE >= DSIZE, even if QEXTRA_LIMBS is non-zero.
6091 */
6092
6093 mpi_limb_t
6094 mpihelp_divrem( mpi_ptr_t qp, mpi_size_t qextra_limbs,
6095 mpi_ptr_t np, mpi_size_t nsize,
6096 mpi_ptr_t dp, mpi_size_t dsize)
6097 {
6098 mpi_limb_t most_significant_q_limb = 0;
6099
6100 switch(dsize) {
6101 case 0:
6102 /* We are asked to divide by zero, so go ahead and do it! (To make
6103 the compiler not remove this statement, return the value.) */
6104 return 1 / dsize;
6105
6106 case 1:
6107 {
6108 mpi_size_t i;
6109 mpi_limb_t n1;
6110 mpi_limb_t d;
6111
6112 d = dp[0];
6113 n1 = np[nsize - 1];
6114
6115 if( n1 >= d ) {
6116 n1 -= d;
6117 most_significant_q_limb = 1;
6118 }
6119
6120 qp += qextra_limbs;
6121 for( i = nsize - 2; i >= 0; i--)
6122 udiv_qrnnd( qp[i], n1, n1, np[i], d );
6123 qp -= qextra_limbs;
6124
6125 for( i = qextra_limbs - 1; i >= 0; i-- )
6126 udiv_qrnnd (qp[i], n1, n1, 0, d);
6127
6128 np[0] = n1;
6129 }
6130 break;
6131
6132 case 2:
6133 {
6134 mpi_size_t i;
6135 mpi_limb_t n1, n0, n2;
6136 mpi_limb_t d1, d0;
6137
6138 np += nsize - 2;
6139 d1 = dp[1];
6140 d0 = dp[0];
6141 n1 = np[1];
6142 n0 = np[0];
6143
6144 if( n1 >= d1 && (n1 > d1 || n0 >= d0) ) {
6145 sub_ddmmss (n1, n0, n1, n0, d1, d0);
6146 most_significant_q_limb = 1;
6147 }
6148
6149 for( i = qextra_limbs + nsize - 2 - 1; i >= 0; i-- ) {
6150 mpi_limb_t q;
6151 mpi_limb_t r;
6152
6153 if( i >= qextra_limbs )
6154 np--;
6155 else
6156 np[0] = 0;
6157
6158 if( n1 == d1 ) {
6159 /* Q should be either 111..111 or 111..110. Need special
6160 * treatment of this rare case as normal division would
6161 * give overflow. */
6162 q = ~(mpi_limb_t)0;
6163
6164 r = n0 + d1;
6165 if( r < d1 ) { /* Carry in the addition? */
6166 add_ssaaaa( n1, n0, r - d0, np[0], 0, d0 );
6167 qp[i] = q;
6168 continue;
6169 }
6170 n1 = d0 - (d0 != 0?1:0);
6171 n0 = -d0;
6172 }
6173 else {
6174 udiv_qrnnd (q, r, n1, n0, d1);
6175 umul_ppmm (n1, n0, d0, q);
6176 }
6177
6178 n2 = np[0];
6179 q_test:
6180 if( n1 > r || (n1 == r && n0 > n2) ) {
6181 /* The estimated Q was too large. */
6182 q--;
6183 sub_ddmmss (n1, n0, n1, n0, 0, d0);
6184 r += d1;
6185 if( r >= d1 ) /* If not carry, test Q again. */
6186 goto q_test;
6187 }
6188
6189 qp[i] = q;
6190 sub_ddmmss (n1, n0, r, n2, n1, n0);
6191 }
6192 np[1] = n1;
6193 np[0] = n0;
6194 }
6195 break;
6196
6197 default:
6198 {
6199 mpi_size_t i;
6200 mpi_limb_t dX, d1, n0;
6201
6202 np += nsize - dsize;
6203 dX = dp[dsize - 1];
6204 d1 = dp[dsize - 2];
6205 n0 = np[dsize - 1];
6206
6207 if( n0 >= dX ) {
6208 if(n0 > dX || mpihelp_cmp(np, dp, dsize - 1) >= 0 ) {
6209 mpihelp_sub_n(np, np, dp, dsize);
6210 n0 = np[dsize - 1];
6211 most_significant_q_limb = 1;
6212 }
6213 }
6214
6215 for( i = qextra_limbs + nsize - dsize - 1; i >= 0; i--) {
6216 mpi_limb_t q;
6217 mpi_limb_t n1, n2;
6218 mpi_limb_t cy_limb;
6219
6220 if( i >= qextra_limbs ) {
6221 np--;
6222 n2 = np[dsize];
6223 }
6224 else {
6225 n2 = np[dsize - 1];
6226 MPN_COPY_DECR (np + 1, np, dsize - 1);
6227 np[0] = 0;
6228 }
6229
6230 if( n0 == dX ) {
6231 /* This might over-estimate q, but it's probably not worth
6232 * the extra code here to find out. */
6233 q = ~(mpi_limb_t)0;
6234 }
6235 else {
6236 mpi_limb_t r;
6237
6238 udiv_qrnnd(q, r, n0, np[dsize - 1], dX);
6239 umul_ppmm(n1, n0, d1, q);
6240
6241 while( n1 > r || (n1 == r && n0 > np[dsize - 2])) {
6242 q--;
6243 r += dX;
6244 if( r < dX ) /* I.e. "carry in previous addition?" */
6245 break;
6246 n1 -= n0 < d1;
6247 n0 -= d1;
6248 }
6249 }
6250
6251 /* Possible optimization: We already have (q * n0) and (1 * n1)
6252 * after the calculation of q. Taking advantage of that, we
6253 * could make this loop make two iterations less. */
6254 cy_limb = mpihelp_submul_1(np, dp, dsize, q);
6255
6256 if( n2 != cy_limb ) {
6257 mpihelp_add_n(np, np, dp, dsize);
6258 q--;
6259 }
6260
6261 qp[i] = q;
6262 n0 = np[dsize - 1];
6263 }
6264 }
6265 }
6266
6267 return most_significant_q_limb;
6268 }
6269
6270
6271 /****************
6272 * Divide (DIVIDEND_PTR,,DIVIDEND_SIZE) by DIVISOR_LIMB.
6273 * Write DIVIDEND_SIZE limbs of quotient at QUOT_PTR.
6274 * Return the single-limb remainder.
6275 * There are no constraints on the value of the divisor.
6276 *
6277 * QUOT_PTR and DIVIDEND_PTR might point to the same limb.
6278 */
6279
6280 mpi_limb_t
6281 mpihelp_divmod_1( mpi_ptr_t quot_ptr,
6282 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
6283 mpi_limb_t divisor_limb)
6284 {
6285 mpi_size_t i;
6286 mpi_limb_t n1, n0, r;
6287 int dummy;
6288
6289 if( !dividend_size )
6290 return 0;
6291
6292 /* If multiplication is much faster than division, and the
6293 * dividend is large, pre-invert the divisor, and use
6294 * only multiplications in the inner loop.
6295 *
6296 * This test should be read:
6297 * Does it ever help to use udiv_qrnnd_preinv?
6298 * && Does what we save compensate for the inversion overhead?
6299 */
6300 if( UDIV_TIME > (2 * UMUL_TIME + 6)
6301 && (UDIV_TIME - (2 * UMUL_TIME + 6)) * dividend_size > UDIV_TIME ) {
6302 int normalization_steps;
6303
6304 count_leading_zeros( normalization_steps, divisor_limb );
6305 if( normalization_steps ) {
6306 mpi_limb_t divisor_limb_inverted;
6307
6308 divisor_limb <<= normalization_steps;
6309
6310 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
6311 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
6312 * most significant bit (with weight 2**N) implicit.
6313 */
6314 /* Special case for DIVISOR_LIMB == 100...000. */
6315 if( !(divisor_limb << 1) )
6316 divisor_limb_inverted = ~(mpi_limb_t)0;
6317 else
6318 udiv_qrnnd(divisor_limb_inverted, dummy,
6319 -divisor_limb, 0, divisor_limb);
6320
6321 n1 = dividend_ptr[dividend_size - 1];
6322 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
6323
6324 /* Possible optimization:
6325 * if (r == 0
6326 * && divisor_limb > ((n1 << normalization_steps)
6327 * | (dividend_ptr[dividend_size - 2] >> ...)))
6328 * ...one division less...
6329 */
6330 for( i = dividend_size - 2; i >= 0; i--) {
6331 n0 = dividend_ptr[i];
6332 UDIV_QRNND_PREINV( quot_ptr[i + 1], r, r,
6333 ((n1 << normalization_steps)
6334 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
6335 divisor_limb, divisor_limb_inverted);
6336 n1 = n0;
6337 }
6338 UDIV_QRNND_PREINV( quot_ptr[0], r, r,
6339 n1 << normalization_steps,
6340 divisor_limb, divisor_limb_inverted);
6341 return r >> normalization_steps;
6342 }
6343 else {
6344 mpi_limb_t divisor_limb_inverted;
6345
6346 /* Compute (2**2N - 2**N * DIVISOR_LIMB) / DIVISOR_LIMB. The
6347 * result is a (N+1)-bit approximation to 1/DIVISOR_LIMB, with the
6348 * most significant bit (with weight 2**N) implicit.
6349 */
6350 /* Special case for DIVISOR_LIMB == 100...000. */
6351 if( !(divisor_limb << 1) )
6352 divisor_limb_inverted = ~(mpi_limb_t) 0;
6353 else
6354 udiv_qrnnd(divisor_limb_inverted, dummy,
6355 -divisor_limb, 0, divisor_limb);
6356
6357 i = dividend_size - 1;
6358 r = dividend_ptr[i];
6359
6360 if( r >= divisor_limb )
6361 r = 0;
6362 else
6363 quot_ptr[i--] = 0;
6364
6365 for( ; i >= 0; i-- ) {
6366 n0 = dividend_ptr[i];
6367 UDIV_QRNND_PREINV( quot_ptr[i], r, r,
6368 n0, divisor_limb, divisor_limb_inverted);
6369 }
6370 return r;
6371 }
6372 }
6373 else {
6374 if(UDIV_NEEDS_NORMALIZATION) {
6375 int normalization_steps;
6376
6377 count_leading_zeros (normalization_steps, divisor_limb);
6378 if( normalization_steps ) {
6379 divisor_limb <<= normalization_steps;
6380
6381 n1 = dividend_ptr[dividend_size - 1];
6382 r = n1 >> (BITS_PER_MPI_LIMB - normalization_steps);
6383
6384 /* Possible optimization:
6385 * if (r == 0
6386 * && divisor_limb > ((n1 << normalization_steps)
6387 * | (dividend_ptr[dividend_size - 2] >> ...)))
6388 * ...one division less...
6389 */
6390 for( i = dividend_size - 2; i >= 0; i--) {
6391 n0 = dividend_ptr[i];
6392 udiv_qrnnd (quot_ptr[i + 1], r, r,
6393 ((n1 << normalization_steps)
6394 | (n0 >> (BITS_PER_MPI_LIMB - normalization_steps))),
6395 divisor_limb);
6396 n1 = n0;
6397 }
6398 udiv_qrnnd (quot_ptr[0], r, r,
6399 n1 << normalization_steps,
6400 divisor_limb);
6401 return r >> normalization_steps;
6402 }
6403 }
6404 /* No normalization needed, either because udiv_qrnnd doesn't require
6405 * it, or because DIVISOR_LIMB is already normalized. */
6406 i = dividend_size - 1;
6407 r = dividend_ptr[i];
6408
6409 if(r >= divisor_limb)
6410 r = 0;
6411 else
6412 quot_ptr[i--] = 0;
6413
6414 for(; i >= 0; i--) {
6415 n0 = dividend_ptr[i];
6416 udiv_qrnnd( quot_ptr[i], r, r, n0, divisor_limb );
6417 }
6418 return r;
6419 }
6420 }