2009-08-06 23:47:42 -04:00
|
|
|
/* Test mpn_mulmod_2expm1
|
|
|
|
|
|
|
|
Copyright 2009 Jason Moxham
|
|
|
|
|
|
|
|
This file is part of the MPIR Library.
|
|
|
|
|
|
|
|
The MPIR Library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
|
|
by the Free Software Foundation; either version 2.1 of the License, or (at
|
|
|
|
your option) any later version.
|
|
|
|
|
|
|
|
The MPIR Library is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with the MPIR Library; see the file COPYING.LIB. If not, write
|
|
|
|
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "mpir.h"
|
|
|
|
#include "gmp-impl.h"
|
|
|
|
#include "tests.h"
|
|
|
|
|
|
|
|
#define DISP(xp,xn) do{int __t;printf("%s=",#xp);for(__t=(xn)-1;__t>=0;__t--)printf("%lX ",(xp)[__t]);printf("\n");}while(0)
|
|
|
|
|
|
|
|
int
|
|
|
|
main (void)
|
2009-09-14 10:17:11 -04:00
|
|
|
{unsigned long bp,xn,n,b,zn,c;gmp_randstate_t rands;int qpn,j,k,i,l,i1,k1,j1,i2,k2,j2;
|
2009-08-10 10:50:25 -04:00
|
|
|
mp_limb_t xp[10000],dp[10000],qp[10000],yp[10000],rp[10000],zp[10000],tp[10000];
|
2009-08-06 23:47:42 -04:00
|
|
|
tests_start ();
|
2009-09-14 10:17:11 -04:00
|
|
|
gmp_randinit_default(rands);
|
|
|
|
|
2009-08-06 23:47:42 -04:00
|
|
|
for(b=1;b<600;b++)
|
|
|
|
{xn=BITS_TO_LIMBS(b);k=xn*GMP_NUMB_BITS-b;
|
|
|
|
MPN_ZERO(dp,xn);mpn_com_n(dp,dp,xn);dp[xn-1]&=GMP_NUMB_MASK>>k;// dp is 2^b-1
|
|
|
|
for(c=0;c<5*b;c++)
|
2009-09-14 07:51:49 -04:00
|
|
|
{mpn_rrandom(xp,rands,xn);mpn_rrandom(yp,rands,xn);
|
2009-08-06 23:47:42 -04:00
|
|
|
xp[xn-1]&=GMP_NUMB_MASK>>k;
|
|
|
|
yp[xn-1]&=GMP_NUMB_MASK>>k;
|
|
|
|
mpn_mul_n(zp,xp,yp,xn);zn=xn*2;MPN_NORMALIZE(zp,zn);
|
|
|
|
if(zn>=xn){mpn_tdiv_qr(qp,rp,0,zp,zn,dp,xn);}else{MPN_COPY(rp,zp,xn);}
|
|
|
|
mpn_mulmod_2expm1(tp,xp,yp,b,qp);
|
|
|
|
if(mpn_cmp(tp,dp,xn)==0)MPN_ZERO(tp,xn);//fully reduce
|
|
|
|
if(mpn_cmp(tp,rp,xn)!=0){printf("mpn_mulmod_2expm1 error %ld\n",b);abort();}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(b=MULMOD_2EXPM1_THRESHOLD*GMP_NUMB_BITS;b<2*MULMOD_2EXPM1_THRESHOLD*GMP_NUMB_BITS;b+=2)
|
|
|
|
{xn=BITS_TO_LIMBS(b);k=xn*GMP_NUMB_BITS-b;
|
|
|
|
MPN_ZERO(dp,xn);mpn_com_n(dp,dp,xn);dp[xn-1]&=GMP_NUMB_MASK>>k;// dp is 2^b-1
|
|
|
|
for(c=0;c<5;c++)
|
2009-09-14 07:51:49 -04:00
|
|
|
{mpn_rrandom(xp,rands,xn);mpn_rrandom(yp,rands,xn);
|
2009-08-06 23:47:42 -04:00
|
|
|
xp[xn-1]&=GMP_NUMB_MASK>>k;
|
|
|
|
yp[xn-1]&=GMP_NUMB_MASK>>k;
|
|
|
|
mpn_mul_n(zp,xp,yp,xn);zn=xn*2;MPN_NORMALIZE(zp,zn);
|
|
|
|
if(zn>=xn){mpn_tdiv_qr(qp,rp,0,zp,zn,dp,xn);}else{MPN_COPY(rp,zp,xn);}
|
|
|
|
mpn_mulmod_2expm1(tp,xp,yp,b,qp);
|
|
|
|
if(mpn_cmp(tp,dp,xn)==0)MPN_ZERO(tp,xn);//fully reduce
|
|
|
|
if(mpn_cmp(tp,rp,xn)!=0){printf("mpn_mulmod_2expm1 error %ld\n",b);abort();}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-14 10:17:11 -04:00
|
|
|
gmp_randclear(rands);
|
2009-08-06 23:47:42 -04:00
|
|
|
tests_end ();
|
|
|
|
exit (0);
|
|
|
|
}
|