bridge/class-master/depend/pari/src/basemath/ifactor1.c
/* k>2 assumed prime, XAUX = scratchpad */
k>2 assumed prime, XAUX = scratchpad
k > 2 assumed prime XAUX = scratchpad
static int
ellmult(GEN N, GEN *gl, long nbc, ulong k, GEN *X1, GEN *X2, GEN *XAUX)
{
  ulong r, d, e, e1;
  int res;
  GEN *A = X2, *B = XAUX, *T = XAUX + 2*nbc;
  ZV_aff(2*nbc,X1,XAUX);
  if ((res = elldouble(N, gl, nbc, X1, X2)) != 0) return res;
  r = (ulong)(k*0.61803398875 + .5);
  d = k - r;
  e = r - d; 
  while (d != e)
  { 
    switch(get_rule(d,e))
    {
    case 0: 
      if ( (res = ecm_elladd(N, gl, nbc, A, B, T)) ) return res;
      if ( (res = ecm_elladd2(N, gl, nbc, T, A, A, T, B, B)) != 0) return res;
      e1 = d - e; d = (d + e1)/3; e = (e - e1)/3; break;
    case 1: 
      if ( (res = ecm_elladd(N, gl, nbc, A, B, B)) ) return res;
      if ( (res = elldouble(N, gl, nbc, A, A)) ) return res;
      d = (d-e)>>1; break;
    case 3: 
      if ( (res = elldouble(N, gl, nbc, A, A)) ) return res;
      d >>= 1; break;
    case 4: 
      if ( (res = elldouble(N, gl, nbc, A, T)) ) return res;
      if ( (res = ecm_elladd(N, gl, nbc, T, A, A)) ) return res;
      if ( (res = ecm_elladd(N, gl, nbc, A, B, B)) ) return res;
      d = d/3 - e; break;
    case 2: 
      if ( (res = ecm_elladd(N, gl, nbc, A, B, B)) ) return res;
      d -= e; break;
    case 5: 
      if ( (res = elldouble(N, gl, nbc, A, T)) ) return res;
      if ( (res = ecm_elladd2(N, gl, nbc, T, A, A, T, B, B)) != 0) return res;
      d = (d - 2*e)/3; break;
    case 6: 
      if ( (res = ecm_elladd(N, gl, nbc, A, B, B)) ) return res;
      if ( (res = elldouble(N, gl, nbc, A, T)) ) return res;
      if ( (res = ecm_elladd(N, gl, nbc, T, A, A)) ) return res;
      d = (d - e)/3; break;
    case 7: 
      if ( (res = elldouble(N, gl, nbc, B, B)) ) return res;
      e >>= 1; break;
    }
    if (d < e) { lswap(d,e); pswap(A,B); }
  }
  return ecm_elladd(N, gl, nbc, XAUX, X2, X2);
}
static int ellmult ( GEN N , GEN * gl , long nbc , ulong k , GEN * X1 , GEN * X2 , GEN * XAUX ) { ulong r , d , e , e1 ; int res ; GEN * A = X2 , * B = XAUX , * T = XAUX + 2 * nbc ; ZV_aff ( 2 * nbc , X1 , XAUX ) ; if ( ( res = elldouble ( N , gl , nbc , X1 , X2 ) ) != 0 ) return res ; r = ( ulong ) ( k * 0.61803398875 + .5 ) ; d = k - r ; e = r - d ; while ( d != e ) { switch ( get_rule ( d , e ) ) { case 0 : if ( ( res = ecm_elladd ( N , gl , nbc , A , B , T ) ) ) return res ; if ( ( res = ecm_elladd2 ( N , gl , nbc , T , A , A , T , B , B ) ) != 0 ) return res ; e1 = d - e ; d = ( d + e1 ) / 3 ; e = ( e - e1 ) / 3 ; break ; case 1 : if ( ( res = ecm_elladd ( N , gl , nbc , A , B , B ) ) ) return res ; if ( ( res = elldouble ( N , gl , nbc , A , A ) ) ) return res ; d = ( d - e ) >> 1 ; break ; case 3 : if ( ( res = elldouble ( N , gl , nbc , A , A ) ) ) return res ; d >>= 1 ; break ; case 4 : if ( ( res = elldouble ( N , gl , nbc , A , T ) ) ) return res ; if ( ( res = ecm_elladd ( N , gl , nbc , T , A , A ) ) ) return res ; if ( ( res = ecm_elladd ( N , gl , nbc , A , B , B ) ) ) return res ; d = d / 3 - e ; break ; case 2 : if ( ( res = ecm_elladd ( N , gl , nbc , A , B , B ) ) ) return res ; d -= e ; break ; case 5 : if ( ( res = elldouble ( N , gl , nbc , A , T ) ) ) return res ; if ( ( res = ecm_elladd2 ( N , gl , nbc , T , A , A , T , B , B ) ) != 0 ) return res ; d = ( d - 2 * e ) / 3 ; break ; case 6 : if ( ( res = ecm_elladd ( N , gl , nbc , A , B , B ) ) ) return res ; if ( ( res = elldouble ( N , gl , nbc , A , T ) ) ) return res ; if ( ( res = ecm_elladd ( N , gl , nbc , T , A , A ) ) ) return res ; d = ( d - e ) / 3 ; break ; case 7 : if ( ( res = elldouble ( N , gl , nbc , B , B ) ) ) return res ; e >>= 1 ; break ; } if ( d < e ) { lswap ( d , e ) ; pswap ( A , B ) ; } } return ecm_elladd ( N , gl , nbc , XAUX , X2 , X2 ) ; }
k>2 assumed prime, XAUX = scratchpad
k > 2 assumed prime XAUX = scratchpad
/* first doubling picks up X1;  after this we'll be working in XAUX and
   * X2 only, mostly via A and B and T */ /* split the work at the golden ratio */ /* d+e == r, so no danger of ofl below */ /* apply one of the nine transformations from PM's Table 4. */ /* rule 1 */ /* rules 2 and 4 */ /* rule 5 */ /* rule 6 */ /* rule 3 */ /* rule 7 */ /* rule 8 */ /* rule 9 */ /* swap d <-> e and A <-> B if necessary */