/*****************************************************************************/
/*									     */
/*  FACTOR (a**p-b**p)/(a-b) (when [(a**p+b**p)/(a+b)] is a pth power)	     */
/*  03/15/16 (dkc)							     */
/*									     */
/*  The output is "a, b, (code<<16)|number of prime factors, code0|code1,    */
/*  code2|code3, code4|code5, code6, "and" of code0,code1,...,code6".        */
/*  "code" values are 0, 1, 2, or 3 if p divides a, b, a-b, or a+b           */
/*  respectively.  code0, code1,...,code6 are codes for the different prime  */
/*  factors of [(a**p-b**p)/(a-b)].  A corresponding bit is set in the code  */
/*  if p/2, 2*p, 2, p, f1*a+f2*b, f2*a+f1*b, p(f1*a+f2*b), p(f2*a+f1*b),     */
/*  p**2*(f1*a+f2*b), p**2(f2*a+f1*b) is a pth power modulo a prime factor of*/
/*  [(a**p-b**p)/(a-b)].						     */
/*									     */
/*****************************************************************************/
#include <math.h>
#include <stdio.h>
#include "table.h"
#include "table9.h"
extern char *malloc();
unsigned int primed(unsigned int *out, unsigned int tsize,
		    unsigned int *table,unsigned int limit);
void dummy(unsigned int a, unsigned int b, unsigned int c, unsigned int d);
void bigprod(unsigned int a, unsigned int b, unsigned int c, unsigned int *p);
void quotient(unsigned int *a, unsigned int *b, unsigned int);
void bigresx(unsigned int a, unsigned int b, unsigned int c, unsigned int d,
	     unsigned int *e, unsigned int f);
void bigbigs(unsigned int *a, unsigned int *b);
void bigbigd(unsigned int *a, unsigned int *b);
void bigbigq(unsigned int a0, unsigned int a1, unsigned int a2,
	     unsigned int a3, unsigned int *quotient, unsigned int d2,
	     unsigned int d3);
unsigned int lmbd(unsigned int mode, unsigned int a);
void differ(unsigned int *a, unsigned int *b);

int main ()
{
unsigned int p=3; // input prime
unsigned int numfac=2; // number of distinct prime factors of (a**p-b**p)/(a-b)
unsigned int split=0;  // if set to 0, don't allow 2 and p to "split"
		       // if set to 1, only allow "split" 2 and p
		       // otherwise, allow both
unsigned int psflag=2; // if set to 1, factors must be of the form p**2*k+1
		       // if set to 0, factors must not be of the form p**2*k+1
		       // otherwise, factors can be of mixed types
unsigned int offset=9000;  // offset into the input look-up table (must be
			   // even and less than "insize").  Used to reduce
			   // execution time.
unsigned int maskout=1; // if set, mask output 
unsigned int zokay=0;	// if set, output code of 0 (normally set to 0)
unsigned int jump=1;	// if set, take jump (normally set to 1)
unsigned int mask2p=8;	// select between p/2, 2p, 2, and p (8, 4, 2, or 1)
unsigned int base2=0;	// if set, use a base of 2, otherwise use a base of p
unsigned int bypass=0;	// bypass mask2p selection (normally set to 0);
unsigned int bigcode=1; // output selected values
unsigned int selectd=0x18e5;  // output if bigcode==2
unsigned int selecte=0xab0;
unsigned int c3=1;	// select linear combination
unsigned int c4=3;	// select linear combination
unsigned int pflag,pcount,qcount,outcnt,fail,pass;
int s3,s4;
unsigned int lout;
unsigned int tsize=1230;
unsigned int t2size=4000000;
unsigned int *tmptab,tmpsiz;
extern unsigned int input[];
extern unsigned int output[];
extern unsigned int error[];
extern unsigned int count;
unsigned int outsiz=15000*7;
unsigned int save[20];  // solutions array
unsigned int savsiz=19;  // size of solutions array minus one
unsigned int d,e,c;
unsigned int h,i,j,k,l,m,iters;
unsigned int flag,temp,sumdif,ps;
unsigned int R[2],S[2],T[2],U[2],V[2],W[2],X[3],Y[4],Z[4];
int yflag,zflag;
unsigned int rflag,sflag,tflag,uflag,vflag,wflag,xflag;
unsigned int n=0;
FILE *Outfp;
Outfp = fopen("out32e.dat","w");
tmptab=(unsigned int*) malloc((t2size+1)*4);
if (tmptab==NULL) {
   printf("not enough memory \n");
   return(0);
   }
tmpsiz=primed(tmptab, tsize, table, t2size*4);
printf("prime look-up table size=%d, prime=%d \n",tmpsiz,tmptab[tmpsiz-1]);
/***********************************/
/*  factor (d**p + e**p)/(d + e)   */
/***********************************/
error[0]=0;	// clear error array
error[1]=0;
error[2]=0;
ps=p*p;
zflag=0;
count=0;
qcount=0;
fail=0;
pass=0;
for (h=offset; h<insize; h++) {
   if (bigcode==0)
      printf(" h=%d %#06x %#06x %d %d %d \n",h,input[3*h],input[3*h+1],input[3*h+2],error[0],qcount);
zloop:
   if (zflag<2) {
      d=input[3*h];
      e=input[3*h+1];
      c=input[3*h+2];
      sumdif=0;
      }
   else {
      d=input[3*(h-1)+1];
      e=input[3*h+1];
      c=input[3*h+2];
      sumdif=1;
      }
   if ((bigcode==1)&&(split==0)&&(mask2p==4)&&(numfac==2)) {
      if ((d==0x4e0e5)&&(e==0x38c94)) goto qskip;
      if ((d==0x404ac)&&(e==0x1497d)) goto qskip;
      if ((d==0x3472e)&&(e==0x1f021)) goto qskip;
      if ((d==0x2e105)&&(e==0x26934)) goto qskip;
      if ((d==0x1b60d)&&(e==0xbd7e)) goto qskip;
      if ((d==0x1bcb9)&&(e==0x9b0a)) goto qskip;
      if ((d==0x22787)&&(e==0x19398)) goto qskip;
      if ((d==0x21e64)&&(e==0x11d79)) goto qskip;
      if ((d==0x208bb)&&(e==0xbff4)) goto qskip;
      if ((d==0x1e744)&&(e==0xe657)) goto qskip;
      if ((d==0x1c2a1)&&(e==0x675c)) goto qskip;
      if ((d==0x15985)&&(e==0xfff0)) goto qskip;
      if ((d==0x15888)&&(e==0xb6d1)) goto qskip;
      if ((d==0xd12e)&&(e==0xa65b)) goto qskip;
      if ((d==0x7b0d)&&(e==0x6c48)) goto qskip;
      if ((d==0x6cc6)&&(e==0x5ec7)) goto qskip;
      if ((d==0x10bc)&&(e==0xfc1)) goto qskip;
      if ((d==0xfc)&&(e==0xb5)) goto qskip;
      goto askip;
      }
   if ((bigcode==1)&&(split==1)&&(mask2p==4)&&(numfac==2)) {
      if ((d==0x22da5)&&(e==0x1d510)) goto qskip;
      if ((d==0x3ef18)&&(e==0x20a1)) goto qskip;
      goto askip;
      }
   if ((bigcode==1)&&(split==0)&&(mask2p==8)&&(numfac==2)) {
      if ((d==0x3ff41)&&(e==0x2f40)) goto qskip;
      if ((d==0x3f5e7)&&(e==0x17fe8)) goto qskip;
      if ((d==0x3f56a)&&(e==0xdbcb)) goto qskip;
      if ((d==0x3186c)&&(e==0xc6a1)) goto qskip;
      if ((d==0x3cdd3)&&(e==0x5a12)) goto qskip;
      if ((d==0x3c52f)&&(e==0xae3c)) goto qskip;
      if ((d==0x3c23a)&&(e==0x1f609)) goto qskip;
      if ((d==0x38ad3)&&(e==0x1874a)) goto qskip;
      if ((d==0x1d651)&&(e==0x1a64c)) goto qskip;
      if ((d==0x2fbdf)&&(e==0x17afc)) goto qskip;
      if ((d==0x2dc60)&&(e==0x17fa1)) goto qskip;
      if ((d==0x1b396)&&(e==0xf1e1)) goto qskip;
      if ((d==0x28f61)&&(e==0x23a72)) goto qskip;
      if ((d==0x25878)&&(e==0x71b)) goto qskip;
      if ((d==0x2496d)&&(e==0xd032)) goto qskip;
      if ((d==0x11b02)&&(e==0xb2ab)) goto qskip;
      if ((d==0xb5d5)&&(e==0xec4)) goto qskip;
      if ((d==0x3ffc)&&(e==0x191b)) goto qskip;
      goto askip;
      }
   if ((bigcode==1)&&(split==0)&&(mask2p==2)&&(numfac==2)) {
      if ((d==0x40ecb)&&(e==0x2bf8d)) goto qskip;
      if ((d==0x3f5e7)&&(e==0x275ff)) goto qskip;
      if ((d==0x3f58f)&&(e==0x3d4db)) goto qskip;
      if ((d==0x3ee99)&&(e==0x133f7)) goto qskip;
      if ((d==0x3dc97)&&(e==0x2b157)) goto qskip;
      if ((d==0x3af3f)&&(e==0xfabd)) goto qskip;
      goto askip;
      }
   if ((bigcode==1)&&(split==0)&&(mask2p==1)&&(base2==1)&&(numfac==2)) {
      if ((d==0xc4c08)&&(e==0x21587)) goto qskip;
      if ((d==0xc45a3)&&(e==0x4ee76)) goto qskip;
      if ((d==0xb7b57)&&(e==0xc65a)) goto qskip;
      if ((d==0xc2c89)&&(e==0x7bd0e)) goto qskip;
      if ((d==0x71634)&&(e==0x505cf)) goto qskip;
      if ((d==0xba6a8)&&(e==0x3e6dd)) goto qskip;
      if ((d==0xae9a8)&&(e==0x345fb)) goto qskip;
      if ((d==0xab306)&&(e==0x4081f)) goto qskip;
      if ((d==0x65641)&&(e==0x41fbe)) goto qskip;
      if ((d==0xa0643)&&(e==0x99f66)) goto qskip;
      if ((d==0x812b2)&&(e==0x74867)) goto qskip;
      if ((d==0x8333)&&(e==0x3624)) goto qskip;
      if ((d==0x6e9e8)&&(e==0x6ace1)) goto qskip;
      if ((d==0x48990)&&(e==0x1cd2d)) goto qskip;
      if ((d==0x4fc3f)&&(e==0x1c25a)) goto qskip;
      if ((d==0x4dcf5)&&(e==0x15eee)) goto qskip;
      if ((d==0x327f5)&&(e==0x149c4)) goto qskip;
      if ((d==0x3b862)&&(e==0x85a9)) goto qskip;
      goto askip;
      }
qskip:
   if ((d/p)*p==d) {
      if (split==0) {
	 if ((d/2)*2!=d)
            goto askip;
	 }
      if (split==1) {
	 if ((d/2)*2==d)
            goto askip;
	 }
      yflag=0;
      }
   if ((e/p)*p==e) {
      if (split==0) {
	 if ((e/2)*2!=e)
            goto askip;
	 }
      if (split==1) {
	 if ((e/2)*2==e)
            goto askip;
	 }
      yflag=1;
      }
   if (((d+e)/p)*p==(d+e)) {
      if (split==0) {
	 if (((d+e)/2)*2!=(d+e))
            goto askip;
	 }
      if (split==1) {
	 if (((d+e)/2)*2==(d+e))
            goto askip;
	 }
      if (sumdif==0)
	 yflag=3;
      else
	 yflag=2;
      }
   if (((d-e)/p)*p==(d-e)) {
      if (split==0) {
	 if (((d-e)/2)*2!=(d-e))
            goto askip;
	 }
      if (split==1) {
	 if (((d-e)/2)*2==(d-e))
            goto askip;
	 }
      if (sumdif==0)
	 yflag=2;
      else
	 yflag=3;
      }
/************************************/
/*  compute (d**p + e**p)/(d + e)   */
/************************************/
      Y[0] = 0;
      Y[1] = 0;
      Y[2] = 0;
      Y[3] = d;
      for (i=0; i<p-1; i++) {
	 bigprod(Y[2], Y[3], d, X);
	 Y[1]=X[0];
	 Y[2]=X[1];
	 Y[3]=X[2];
	 }
      Z[0] = 0;
      Z[1] = 0;
      Z[2] = 0;
      Z[3] = e;
      for (i=0; i<p-1; i++) {
	 bigprod(Z[2], Z[3], e, X);
	 Z[1]=X[0];
	 Z[2]=X[1];
	 Z[3]=X[2];
	 }
      if (sumdif==1) {
	 bigbigs(Y, Z);
	 temp=d+e;
	 if (((d+e)/p)*p==(d+e))
	    temp=temp*p;
	 bigbigq(Z[0], Z[1], Z[2], Z[3], Y, 0, temp);
	 }
      else {
	 bigbigd(Y, Z);
	 temp=d-e;
	 if (((d-e)/p)*p==(d-e))
	    temp=temp*p;
	 bigbigq(Z[0], Z[1], Z[2], Z[3], Y, 0, temp);
	 }
      S[0]=Y[2];
      S[1]=Y[3];
      W[0]=S[0];
      W[1]=S[1];
/************************************************/
/*  look for prime factors using look-up table	*/
/************************************************/
      if (S[0]==0) {
	 l = (33 - lmbd(1, S[1]))/2;
	 l = 1 << l;
	 }
      else {
	 l = (65 - lmbd(1, S[0]))/2;
	 l = 1 << l;
	 }
      k=0;
      if (l>tmptab[tmpsiz-1]) {
	 flag=1;
	 k=tmpsiz-1;
	 }
      else {
	 flag=0;
	 for (i=0; i<tmpsiz; i++) {
	    if (tmptab[i] < l) k=i;
	    else break;
	    }
	 }
      l=k;
      iters=0;
      rflag=0xffffffff;
      sflag=0xffffffff;
      tflag=0xffffffff;
      uflag=0xffffffff;
      vflag=0xffffffff;
      wflag=0xffffffff;
      xflag=0xffffffff;
      m=0;
      for (i=0; i<=l; i++) {
	 k = tmptab[i];
	 quotient(S, T, k);
	 V[0]=T[0];
	 V[1]=T[1];
	 bigprod(T[0], T[1], k, X);
	 if ((S[0]!=X[1]) || (S[1]!=X[2])) continue;
	 if (psflag==1) {
            if (((k-1)/ps)*ps!=(k-1))
               goto askip;
            }
	 if (psflag==0) {
	    if (((k-1)/ps)*ps==(k-1))
               goto askip;
            }
	 if (iters<numfac) {
	    rflag=0;
	    if (zflag!=2) {
	       s3=c3*d+c4*e;
	       s4=c4*d+c3*e;
	       }
	    else {
	       s3=c3*d-c4*e;
	       s4=c4*d-c3*e;
	       if (s3<0)
		  s3=-s3;
	       if (s4<0)
		  s4=-s4;
	       }
	    if (bigcode==1)
	       printf("k=%d \n",k);
	    else {
	       if ((d==selectd)&&(e==selecte)&&(bigcode==2))
		  printf("k=%d, sign=%d \n",k,sumdif);
	       }
	    bigresx(0, (k-1)/p, 0, k, U, (unsigned int)s3);
	    if ((U[0]==0)&&(U[1]==1))
	       rflag=rflag+32;
	    bigresx(0, (k-1)/p, 0, k, U, (unsigned int)s4);
	    if ((U[0]==0)&&(U[1]==1))
	       rflag=rflag+16;
	    if (base2==0)
	       bigresx(0, (k-1)/p, 0, k, U, p*(unsigned int)s3);
	    else
	       bigresx(0, (k-1)/p, 0, k, U, 2*(unsigned int)s3);
	    if ((U[0]==0)&&(U[1]==1))
	       rflag=rflag+8;
	    if (base2==0)
	       bigresx(0, (k-1)/p, 0, k, U, p*(unsigned int)s4);
	    else
	       bigresx(0, (k-1)/p, 0, k, U, 2*(unsigned int)s4);
	    if ((U[0]==0)&&(U[1]==1))
	       rflag=rflag+4;
	    if (base2==0)
	       bigresx(0, (k-1)/p, 0, k, U, p*p*(unsigned int)s3);
	    else
	       bigresx(0, (k-1)/p, 0, k, U, 2*2*(unsigned int)s3);
	    if ((U[0]==0)&&(U[1]==1))
	       rflag=rflag+2;
	    if (base2==0)
	       bigresx(0, (k-1)/p, 0, k, U, p*p*(unsigned int)s4);
	    else
	       bigresx(0, (k-1)/p, 0, k, U, 2*2*(unsigned int)s4);
	    if ((U[0]==0)&&(U[1]==1))
	       rflag=rflag+1;
	    bigresx(0, (k-1)/p, 0, k, U, p);
	    if ((U[0]==0)&&(U[1]==1))
	       rflag=rflag+4096;
	    R[0]=U[0];
	    R[1]=U[1];
	    bigresx(0, (k-1)/p, 0, k, U, 2);
	    if ((U[0]==0)&&(U[1]==1)) {
	       rflag=rflag+8192;
	       if (split==1) {
		  if (((k-1)/ps)*ps!=(k-1))
		     error[0]=8;
		  }
	       }
	    if ((R[0]==U[0])&&(R[1]==U[1]))
	       rflag=rflag+32768;
	    bigresx(0, (k-1)/p, 0, k, U, 2*p);
	    if ((U[0]==0)&&(U[1]==1)) {
	       rflag=rflag+16384;
	       }
	    }
aloop:	 S[0]=V[0];
	 S[1]=V[1];
	 save[m]=k;
	 if (m < savsiz) m=m+1;
	 else {
	    error[0]=3;
	    goto bskip;
	    }
	 quotient(S, T, k);
	 V[0]=T[0];
	 V[1]=T[1];
	 bigprod(T[0], T[1], k, X);
	 if ((S[0]==X[1]) && (S[1]==X[2])) goto aloop;
	 iters=iters+1;
	 if (iters<numfac) {
	     xflag=wflag;
	     wflag=vflag;
	     vflag=uflag;
	     uflag=tflag;
	     tflag=sflag;
	     sflag=rflag;
	     }
	 else {
	    if ((S[0]!=0)||(S[1]!=1))
	       goto askip;
	    }
	 }
/***********************************************/
/*  output prime factors satisfying criterion  */
/***********************************************/
      if ((S[0]!=0) || (S[1]!=1)) {
	 if (flag==1) {
	    if (S[0]==0) {
	       j = (33 - lmbd(1, S[1]))/2;
	       j = 1 << j;
	       }
	    else {
	       j = (65 - lmbd(1, S[0]))/2;
	       j = 1 << j;
	       }
	    for (i=tmptab[tmpsiz-1]; i<j; i+=2) {
	       quotient(S, T, i);
	       bigprod(T[0], T[1], i, X);
	       if ((X[1]==S[0]) && (X[2]==S[1])) {
		  if (psflag==1) {
                     if (((i-1)/ps)*ps!=(i-1))
                        goto askip;
                     }
		  if (psflag==0) {
		     if (((i-1)/ps)*ps==(i-1))
                        goto askip;
                     }
		  if (iters<numfac) {
		     rflag=0;
		     if (zflag!=2) {
			s3=c3*d+c4*e;
			s4=c4*d+c3*e;
			}
		     else {
			s3=c3*d-c4*e;
			s4=c4*d-c3*e;
			if (s3<0)
			   s3=-s3;
			if (s4<0)
			   s4=-s4;
			}
		     if (bigcode==1)
			printf("i=%d \n",i);
		     else {
			if ((d==selectd)&&(e==selecte)&&(bigcode==2))
			   printf("i=%d, sign=%d \n",i,sumdif);
			}
		     bigresx(0, (i-1)/p, 0, i, U, (unsigned int)s3);
		     if ((U[0]==0)&&(U[1]==1))
			rflag=rflag+32;
		     bigresx(0, (i-1)/p, 0, i, U, (unsigned int)s4);
		     if ((U[0]==0)&&(U[1]==1))
			rflag=rflag+16;
		     if (base2==0)
			bigresx(0, (i-1)/p, 0, i, U, p*(unsigned int)s3);
		     else
			bigresx(0, (i-1)/p, 0, i, U, 2*(unsigned int)s3);
		     if ((U[0]==0)&&(U[1]==1))
			rflag=rflag+8;
		     if (base2==0)
			bigresx(0, (i-1)/p, 0, i, U, p*(unsigned int)s4);
		     else
			bigresx(0, (i-1)/p, 0, i, U, 2*(unsigned int)s4);
		     if ((U[0]==0)&&(U[1]==1))
			rflag=rflag+4;
		     if (base2==0)
			bigresx(0, (i-1)/p, 0, i, U, p*p*(unsigned int)s3);
		     else
			bigresx(0, (i-1)/p, 0, i, U, 2*2*(unsigned int)s3);
		     if ((U[0]==0)&&(U[1]==1))
			rflag=rflag+2;
		     if (base2==0)
			bigresx(0, (i-1)/p, 0, i, U, p*p*(unsigned int)s4);
		     else
			bigresx(0, (i-1)/p, 0, i, U, 2*2*(unsigned int)s4);
		     if ((U[0]==0)&&(U[1]==1))
			rflag=rflag+1;
		     bigresx(0, (i-1)/p, 0, i, U, p);
		     if ((U[0]==0)&&(U[1]==1))
			rflag=rflag+4096;
		     R[0]=U[0];
		     R[1]=U[1];
		     bigresx(0, (i-1)/p, 0, i, U, 2);
		     if ((U[0]==0)&&(U[1]==1)) {
			rflag=rflag+8192;
			if (split==1) {
			   if (((i-1)/ps)*ps!=(i-1))
			      error[0]=8;
			   }
			}
		     if ((R[0]==U[0])&&(R[1]==U[1]))
			rflag=rflag+32768;
		     bigresx(0, (i-1)/p, 0, i, U, 2*p);
		     if ((U[0]==0)&&(U[1]==1)) {
			rflag=rflag+16384;
			}
		     }
		  iters=iters+1;
		  if (iters<numfac) {
		     xflag=wflag;
		     wflag=vflag;
		     vflag=uflag;
		     uflag=tflag;
		     tflag=sflag;
		     sflag=rflag;
		     }
		  if (T[0]<=4) {     // largest prime in table is 0x270eb
		     S[0]=T[0];
		     S[1]=T[1];
		     save[m]=i;
		     if (m < savsiz) m=m+1;
		     else {
			error[0]=3;
			goto bskip;
			}
		     goto cskip;
		     }
		  else {
		     error[0]=4;
		     goto bskip;
		     }
		  }
	       }
	    }
cskip:	 if ((S[0]==0)&&(S[1]==1)) {
	    if (iters!=numfac)
	       goto askip;
	    else
	       goto dskip;
	    }
	 if ((S[0]==0)&&(S[1]==save[m])) {
	    if (iters==numfac)
	       goto dskip;
	    else
	       goto askip;
	    }
	 if (iters!=(numfac-1))
	    goto askip;
	 if (psflag==1) {
            T[0]=0;
            T[1]=1;
            differ(S,T);
            quotient(T,T,ps);
            bigprod(T[0],T[1],ps,X);
            T[0]=0;
            T[1]=1;
            differ(S,T);
            if ((X[1]!=T[0])||(X[2]!=T[1]))
               goto askip;
            }
	 if (psflag==0) {
            T[0]=0;
            T[1]=1;
            differ(S,T);
            quotient(T,T,ps);
            bigprod(T[0],T[1],ps,X);
            T[0]=0;
            T[1]=1;
            differ(S,T);
	    if ((X[1]==T[0])&&(X[2]==T[1]))
               goto askip;
            }
         T[0]=0;
	 T[1]=1;
	 differ(S, T);
	 quotient(T, T, p);
	 rflag=0;
	 if (zflag!=2) {
	    s3=c3*d+c4*e;
	    s4=c4*d+c3*e;
	    }
	 else {
	    s3=c3*d-c4*e;
	    s4=c4*d-c3*e;
	    if (s3<0)
	       s3=-s3;
	    if (s4<0)
	       s4=-s4;
	    }
	 if (bigcode==1)
	    printf("S=%#010x %#010x \n",S[0],S[1]);
	 else {
	    if ((d==selectd)&&(e==selecte)&&(bigcode==2))
	       printf("S=%#010x %#010x, sign=%d \n",S[0],S[1],sumdif);
	    }
	 bigresx(T[0], T[1], S[0], S[1], U, (unsigned int)s3);
	 if ((U[0]==0)&&(U[1]==1))
	    rflag=rflag+32;
	 bigresx(T[0], T[1], S[0], S[1], U, (unsigned int)s4);
	 if ((U[0]==0)&&(U[1]==1))
	    rflag=rflag+16;
	 if (base2==0)
	    bigresx(T[0], T[1], S[0], S[1], U, p*(unsigned int)s3);
	 else
	    bigresx(T[0], T[1], S[0], S[1], U, 2*(unsigned int)s3);
	 if ((U[0]==0)&&(U[1]==1))
	    rflag=rflag+8;
	 if (base2==0)
	    bigresx(T[0], T[1], S[0], S[1], U, p*(unsigned int)s4);
	 else
	    bigresx(T[0], T[1], S[0], S[1], U, 2*(unsigned int)s4);
	 if ((U[0]==0)&&(U[1]==1))
	    rflag=rflag+4;
	 if (base2==0)
	    bigresx(T[0], T[1], S[0], S[1], U, p*p*(unsigned int)s3);
	 else
	    bigresx(T[0], T[1], S[0], S[1], U, 2*2*(unsigned int)s3);
	 if ((U[0]==0)&&(U[1]==1))
	    rflag=rflag+2;
	 if (base2==0)
	    bigresx(T[0], T[1], S[0], S[1], U, p*p*(unsigned int)s4);
	 else
	    bigresx(T[0], T[1], S[0], S[1], U, 2*2*(unsigned int)s4);
	 if ((U[0]==0)&&(U[1]==1))
	    rflag=rflag+1;
	 bigresx(T[0], T[1], S[0], S[1], U, p);
	 if ((U[0]==0)&&(U[1]==1))
	    rflag=rflag+4096;
	 R[0]=U[0];
	 R[1]=U[1];
	 bigresx(T[0], T[1], S[0], S[1], U, 2);
	 if ((U[0]==0)&&(U[1]==1)) {
	    rflag=rflag+8192;
	    if (split==1) {
	       V[0]=0;
	       V[1]=1;
	       differ(S,V);
	       quotient(V,V,ps);
	       bigprod(V[0],V[1],ps,X);
	       V[0]=0;
	       V[1]=1;
	       differ(S,V);
	       if ((X[1]!=V[0])||(X[2]!=V[1]))
		  error[0]=8;
	       }
	    }
	 if ((R[0]==U[0])&&(R[1]==U[1]))
	    rflag=rflag+32768;
	 bigresx(T[0], T[1], S[0], S[1], U, 2*p);
	 if ((U[0]==0)&&(U[1]==1)) {
	    rflag=rflag+16384;
	    }
dskip:	 if (n+6>outsiz) {
	    error[0]=6;
	    goto bskip;
	    }
	 output[n]=d;
	 output[n+1]=e;
	 output[n+2]=(yflag<<16)|(m+1);
	 output[n+3]=(xflag<<16)|wflag;
	 output[n+4]=(vflag<<16)|uflag;
	 output[n+5]=(tflag<<16)|sflag;
	 xflag=rflag&sflag&tflag&uflag&vflag&wflag&wflag;
	 lout=xflag>>12;
	 if ((lout==mask2p)||(bypass!=0)) {
	    pcount=0;
	    pflag=xflag&0x03f;
	    while (pflag!=0) {
	       if ((pflag&1)==1)
		  pcount=pcount+1;
	       pflag=pflag>>1;
	       }
	    if ((pcount!=2)&&(pcount!=0)) {
	       fail=fail+1;
	       printf("error: pcount=%d, flag=%d \n",pcount,lout);
	       if (jump!=0)
		  goto bskip;
	       }
	    if (pcount!=0)
	       qcount=qcount+1;
	    }
	 output[n+6]=(rflag<<16)|xflag;
	 for (i=0; i<m; i++) {
	    bigprod(S[0], S[1], save[i], X);
	    S[0] = X[1];
	    S[1] = X[2];
	    }
	 if ((S[0]!=W[0]) || (S[1]!=W[1])) {
	    error[0]=7;
	    goto bskip;
	    }
	 T[0]=0;
	 T[1]=1;
	 differ(S,T);
	 quotient(T,T,ps);
	 bigprod(T[0],T[1],ps,X);
	 T[0]=0;
	 T[1]=1;
	 differ(S,T);
	 if ((X[1]!=T[0])||(X[2]!=T[1]))
	    error[1]+=1;
	 n=n+7;
	 count=count+1;
	 if ((numfac==2)&&(split==0)&&(psflag==0)) {
	    if ((yflag==0)||(yflag==1)) {
	       if (rflag!=sflag)
		  error[0]=8;
	       T[0]=0;
	       T[1]=1;
	       differ(W,T);
	       quotient(T,T,ps);
	       bigprod(T[0],T[1],ps,X);
	       T[0]=0;
	       T[1]=1;
	       differ(W,T);
	       if ((X[1]!=T[0])||(X[2]!=T[1]))
		  error[0]=8;
	       }
	    }
	 }
//
// S[0]=0, S[1]=1
//
      else {
	 if (iters!=numfac)
	   goto askip;
	 if (n+6>outsiz) {
	    error[0]=6;
	    goto bskip;
	    }
	 output[n]=d;
	 output[n+1]=e;
	 output[n+2]=(yflag<<16)|m;
	 output[n+3]=(xflag<<16)|wflag;
	 output[n+4]=(vflag<<16)|uflag;
	 output[n+5]=(tflag<<16)|sflag;
	 xflag=rflag&sflag&tflag&uflag&vflag&wflag&wflag;
	 lout=xflag>>12;
	 if ((lout==mask2p)||(bypass!=0)) {
	    pcount=0;
	    pflag=xflag&0x03f;
	    while (pflag!=0) {
	       if ((pflag&1)==1)
		  pcount=pcount+1;
	       pflag=pflag>>1;
	       }
	    if ((pcount!=2)&&(pcount!=0)) {
	       fail=fail+1;
	       printf("error: pcount=%d, flag=%d \n",pcount,lout);
	       if (jump!=0)
		  goto bskip;
	       }
	    if (pcount!=0)
	       qcount=qcount+1;
	    }
	 output[n+6]=(rflag<<16)|xflag;
         S[0]=0;
	 S[1]=1;
	 for (i=0; i<m; i++) {
	    bigprod(S[0], S[1], save[i], X);
	    S[0] = X[1];
	    S[1] = X[2];
	    }
	 if ((S[0]!=W[0]) || (S[1]!=W[1])) {
	    error[0]=7;
	    goto bskip;
	    }
	 T[0]=0;
	 T[1]=1;
	 differ(S,T);
	 quotient(T,T,ps);
	 bigprod(T[0],T[1],ps,X);
	 T[0]=0;
	 T[1]=1;
	 differ(S,T);
	 if ((X[1]!=T[0])||(X[2]!=T[1]))
	    error[1]+=1;
	 n=n+7;
	 count=count+1;
	 if ((numfac==2)&&(split==0)&&(psflag==0)) {
	    if ((yflag==0)||(yflag==1)) {
	       if (rflag!=sflag)
		  error[0]=8;
	       T[0]=0;
	       T[1]=1;
	       differ(W,T);
	       quotient(T,T,ps);
	       bigprod(T[0],T[1],ps,X);
	       T[0]=0;
	       T[1]=1;
	       differ(W,T);
	       if ((X[1]!=T[0])||(X[2]!=T[1]))
		  error[0]=8;
	       }
	    }
	 }
askip: if (zflag==2)
          zflag=-1;
       zflag+=1;
       if (zflag==2)
          goto zloop;
}
bskip:
outcnt=0;
printf("fail=%d \n",fail);
printf("pass=%d \n",pass);
printf("count=%d \n",qcount);
output[n]=0xffffffff;
printf("error=%d \n",error[0]);
fprintf(Outfp," count=%d \n",(n+1)/7);
for (i=0; i<(n+1)/7; i++) {
   if (maskout!=0) {
      lout=(output[7*i+6]&0xf000)>>12;
      if (zokay==0) {
	 if ((lout==mask2p)&&((output[7*i+6]&0x003f)!=0)) {
	    fprintf(Outfp," %#9x %#9x %#10x %#10x %#10x %#10x %#6x %#6x \n",
	      output[7*i],output[7*i+1],output[7*i+2],output[7*i+3],output[7*i+4],
	      output[7*i+5],(unsigned short)(output[7*i+6]>>16),
	      (unsigned short)(output[7*i+6]&0x003f));
	    outcnt=outcnt+1;
	    }
	 }
      else {
	 if (lout==mask2p) {
	    fprintf(Outfp," %#9x %#9x %#10x %#10x %#10x %#10x %#6x %#6x \n",
	      output[7*i],output[7*i+1],output[7*i+2],output[7*i+3],output[7*i+4],
	      output[7*i+5],(unsigned short)(output[7*i+6]>>16),
	      (unsigned short)(output[7*i+6]&0x003f));
	    outcnt=outcnt+1;
	    }
	 }
      }
   else
      fprintf(Outfp," %#9x %#9x %#10x %#10x %#10x %#10x %#6x %#6x \n",
	output[7*i],output[7*i+1],output[7*i+2],output[7*i+3],output[7*i+4],
	output[7*i+5],(unsigned short)(output[7*i+6]>>16),
	(unsigned short)(output[7*i+6]&0xffff));
   }
printf("outcnt=%d \n",outcnt);
fclose(Outfp);
return(0);
}