void FractalTexture::updateCorners() {
#ifdef ROE_FRACTAL_GMP_USE_C
			mpf_div_ui(xPos, m_width , 2);
			mpf_div_ui(yPos, m_height, 2);
			mpf_sub(m_xUpLt, m_xCenter, xPos);
			mpf_add(m_yUpLt, m_yCenter, yPos);
#else
			m_xUpLt = m_xCenter-0.5*m_width;
			m_yUpLt = m_yCenter+0.5*m_height;
#endif
		}
Example #2
0
/* merit (rop, t, v, m) -- calculate merit for spectral test result in
   dimension T, see Knuth p. 105.  BUGS: Only valid for 2 <= T <=
   6. */
void
merit (mpf_t rop, unsigned int t, mpf_t v, mpz_t m)
{
  int f;
  mpf_t f_m, f_const, f_pi;

  mpf_init (f_m);
  mpf_set_z (f_m, m);
  mpf_init_set_d (f_const, M_PI);
  mpf_init_set_d (f_pi, M_PI);

  switch (t)
    {
    case 2:			/* PI */
      break;
    case 3:			/* PI * 4/3 */
      mpf_mul_ui (f_const, f_const, 4);
      mpf_div_ui (f_const, f_const, 3);
      break;
    case 4:			/* PI^2 * 1/2 */
      mpf_mul (f_const, f_const, f_pi);
      mpf_div_ui (f_const, f_const, 2);
      break;
    case 5:			/* PI^2 * 8/15 */
      mpf_mul (f_const, f_const, f_pi);
      mpf_mul_ui (f_const, f_const, 8);
      mpf_div_ui (f_const, f_const, 15);
      break;
    case 6:			/* PI^3 * 1/6 */
      mpf_mul (f_const, f_const, f_pi);
      mpf_mul (f_const, f_const, f_pi);
      mpf_div_ui (f_const, f_const, 6);
      break;
    default:
      fprintf (stderr,
	       "spect (merit): can't calculate merit for dimensions > 6\n");
      mpf_set_ui (f_const, 0);
      break;
    }

  /* rop = v^t */
  mpf_set (rop, v);
  for (f = 1; f < t; f++)
    mpf_mul (rop, rop, v);
  mpf_mul (rop, rop, f_const);
  mpf_div (rop, rop, f_m);

  mpf_clear (f_m);
  mpf_clear (f_const);
  mpf_clear (f_pi);
}
Example #3
0
File: hilbert.c Project: blynn/pbc
static void mpc_cis(mpc_t res, mpf_t theta) {
  mpf_t a;

  mpf_init(a); mpf_set(a, theta);
  //res = exp(i a)
  //  = cos a + i sin a
  //converges quickly near the origin
  mpf_t f0;
  mpf_ptr rx = mpc_re(res), ry = mpc_im(res);
  int i;
  int toggle = 1;

  mpf_init(f0);

  mpf_set(f0, a);
  mpf_set_ui(rx, 1);
  mpf_set(ry, f0);
  i = 1;
  for(;;) {
    toggle = !toggle;
    i++;
    mpf_div_ui(f0, f0, i);
    mpf_mul(f0, f0, a);
    if (toggle) {
      mpf_add(rx, rx, f0);
    } else {
      mpf_sub(rx, rx, f0);
    }

    i++;
    mpf_div_ui(f0, f0, i);
    mpf_mul(f0, f0, a);

    if (toggle) {
      mpf_add(ry, ry, f0);
    } else {
      mpf_sub(ry, ry, f0);
    }

    if (mpf_sgn(f0) > 0) {
      if (mpf_cmp(f0, epsilon) < 0) break;
    } else {
      if (mpf_cmp(f0, negepsilon) > 0) break;
    }
  }

  mpf_clear(f0);
  mpf_clear(a);
}
Example #4
0
File: hilbert.c Project: blynn/pbc
static void mpf_exp(mpf_t res, mpf_t pwr) {
  mpf_t a;
  mpf_t f0;
  int i;

  mpf_init(a); mpf_set(a, pwr);

  mpf_init(f0);

  mpf_set(f0, a);
  mpf_add_ui(res, a, 1);

  for (i=2;;i++) {
    mpf_mul(f0, f0, a);
    mpf_div_ui(f0, f0, i);
    if (mpf_sgn(f0) > 0) {
      if (mpf_cmp(f0, epsilon) < 0) break;
    } else {
      if (mpf_cmp(f0, negepsilon) > 0) break;
    }
    mpf_add(res, res, f0);
  }

  mpf_clear(f0);
  mpf_clear(a);
}
Example #5
0
/* 
 * Evaluate the series 1/0! + 1/1! + 1/2! + 1/3! + 1/4! ...
 * On input, 'bits' is the desired precision in bits.
 * On output, e' contains the calculated value.
 */
static void calculateE(
	unsigned bits,
	mpf_t e)
{
	mpf_t lastE;
	mpf_t invFact;					/* 1/2!, 1/3!, 1/4!, etc. */
	unsigned term;					/* 2, 3, 4... */
	
	/* initial conditions, including the first two terms */
	mpf_init_set_ui(lastE, 0);
	mpf_set_ui(e, 2);				
	mpf_init_set_ui(invFact, 1);	/* 1/1! */
	term = 2;
	
	for(;;) {
		/* invFact /= (term) */
		mpf_div_ui(invFact, invFact, term);
		/* e += 1 / (term!) */
		mpf_add(e, e, invFact);
		
		/* if e == lastE, within the requested precision, we're done */
		if(mpf_eq(e, lastE, bits)) {
			break;
		}
		mpf_set(lastE, e);
		term++;
	}
	/* free memory associated with mpf_t's we allocated */
	mpf_clear(lastE);
	mpf_clear(invFact);
}
Example #6
0
File: hilbert.c Project: blynn/pbc
static void precision_init(int prec) {
  int i;
  mpf_t f0;

  mpf_set_default_prec(prec);
  mpf_init2(epsilon, 2);
  mpf_init2(negepsilon, 2);
  mpf_init(recipeulere);
  mpf_init(pi);
  mpf_init(eulere);

  mpf_set_ui(epsilon, 1);
  mpf_div_2exp(epsilon, epsilon, prec);
  mpf_neg(negepsilon, epsilon);

  mpf_init(f0);
  mpf_set_ui(eulere, 1);
  mpf_set_ui(f0, 1);
  for (i=1;; i++) {
    mpf_div_ui(f0, f0, i);
    if (mpf_cmp(f0, epsilon) < 0) {
      break;
    }
    mpf_add(eulere, eulere, f0);
  }
  mpf_clear(f0);

  mpf_ui_div(recipeulere, 1, eulere);

  compute_pi(prec);
}
/**
 * void calculate_a()
 * 
 * Descricao:
 * 	Calcula o valor do "pi" na n-esima iteracao.
 * 
 * Parametros de entrada:
 * 	-
 * 
 * Parametros de retorno:
 * 	-
 */
void calculate_pi(){
  
    mpf_add(pi[n_count], a_n[n_count+1], b_n[n_count+1]);
    mpf_pow_ui(pi[n_count], pi[n_count], 2);    
    mpf_div_ui(pi[n_count], pi[n_count], 4);
    mpf_div(pi[n_count], pi[n_count], t_n[n_count+1]); 
    
}
void *thread2(void *param)
{
	mpf_add(x, sqrtx, invsqrtx);
	mpf_div_ui(x, x, 2);
	mpf_add_ui(aux1, x, 1);
	mpf_mul(aux1 ,p, aux1);
	pthread_exit(0);
}
Example #9
0
static int
mixed (void)
{
    int n1;
    int n2;
    int i = 121;
#ifndef NPRINTF_L
    long double d = 1. / 31.;
#endif
    mpf_t mpf;
    mpq_t mpq;
    mpz_t mpz;
    mpfr_t x;
    mpfr_rnd_t rnd;

    mpf_init (mpf);
    mpf_set_ui (mpf, 40);
    mpf_div_ui (mpf, mpf, 31); /* mpf = 40.0 / 31.0 */
    mpq_init (mpq);
    mpq_set_ui (mpq, 123456, 4567890);
    mpz_init (mpz);
    mpz_fib_ui (mpz, 64);
    mpfr_init (x);
    mpfr_set_str (x, "-12345678.875", 10, MPFR_RNDN);
    rnd = MPFR_RNDD;

    check_vsprintf ("121%", "%i%%", i);
    check_vsprintf ("121% -1.2345678875E+07", "%i%% %RNE", i, x);
    check_vsprintf ("121, -12345679", "%i, %.0Rf", i, x);
    check_vsprintf ("10610209857723, -1.2345678875e+07", "%Zi, %R*e", mpz, rnd,
                    x);
    check_vsprintf ("-12345678.9, 121", "%.1Rf, %i", x, i);
    check_vsprintf ("-12345678, 1e240/45b352", "%.0R*f, %Qx", MPFR_RNDZ, x, mpq);
    n1 = check_vsprintf ("121, -12345678.875000000000, 1.290323", "%i, %.*Rf, %Ff%n",
                         i, 12, x, mpf, &n2);
    if (n1 != n2)
    {
        printf ("error in number of characters written by mpfr_vsprintf\n");
        printf ("expected: %d\n", n2);
        printf ("     got: %d\n", n1);
        exit (1);
    }

#ifndef NPRINTF_L
    check_vsprintf ("00000010610209857723, -1.2345678875e+07, 0.032258",
                    "%.*Zi, %R*e, %Lf", 20, mpz, rnd, x, d);
#endif

    mpf_clear (mpf);
    mpq_clear (mpq);
    mpz_clear (mpz);
    mpfr_clear (x);
    return 0;
}
/* New calculate function. */
ordinal_number_t cache_calculator(render_t* handle,const view_position_t render_position)
{
	/* Volatile data. */
	ordinal_number_t* help;
	complex_number_t  complex_position;
	view_position_t   shift;
	real_number_t     scaling_factor;
	mpf_t             help_mpf;
	mpf_t             help_two;

	mpf_set_default_prec(sizeof(char)*handle->prec);
	mpf_init(help_mpf);
	mpf_init(Re(complex_position));
	mpf_init(Im(complex_position));
	mpf_init(scaling_factor);
	mpf_init(help_two);

	/* Check if the point has been calculated already. */
	help=handle->points+render_position.y*handle->geometry.width+render_position.x;
	if(*help==0)
	{
		/* Has not been calculated till now, calculate the iteration. */

		/* Precalculate scaling factor and center shift for speed reasons. */
		mpf_div_ui(scaling_factor,handle->scale,handle->geometry.width);
		shift.x=handle->geometry.width/2;
		shift.y=handle->geometry.height/2;

		/* Calculate the iteration. */
		mpf_set_si(help_two,(render_position.x-shift.x));
		mpf_mul(help_mpf,scaling_factor,help_two);
		mpf_add(complex_position.real_part,help_mpf,handle->center.real_part);

		mpf_set_si(help_two,(render_position.y-shift.y));
		mpf_mul(help_mpf,scaling_factor,help_two);
		mpf_sub(Im(complex_position),Im(handle->center),help_mpf);

		*help=(*handle->fractal_facility->facility.fractal.calculate_function)(handle->fractal,&complex_position);
	}

	mpf_clear(help_mpf);
	mpf_clear(Re(complex_position));
	mpf_clear(Im(complex_position));
	mpf_clear(scaling_factor);
	mpf_clear(help_two);
	

	/* Return the iteration. */
	return(*help);
	//return(0);
}
Example #11
0
void
mpc_mod (mpc_t *rop, mpc_t op1, mpc_t op2)
{
  /* I am 90% sure that this doesn't work. However, it works for
   * integers, so I'll put off fixing it for a little while.
   */
  mpf_t hold_op1;
  mpf_t hold_op2;
  mpf_t hold_res;
  unsigned int prec;

  mpf_init (hold_op1);
  mpf_init (hold_op2);
  mpf_init (hold_res);
  mpf_set_z (hold_op1, op1.object);
  mpf_set_z (hold_op2, op2.object);

  // Get the largest precision.
  prec = (op1.precision > op2.precision) ? op1.precision : op2.precision;

  // Set the scalar values
  while (op1.precision-- > 0)
    mpf_div_ui (hold_op1, hold_op1, 10);
  while (op2.precision-- > 0)
    mpf_div_ui (hold_op2, hold_op2, 10);

  // Get the value
  mpf_div (hold_res, hold_op1, hold_op2);
  mpf_floor (hold_res, hold_res);
  mpf_mul (hold_res, hold_res, hold_op2);
  mpf_sub (hold_res, hold_op1, hold_res);

  for (rop->precision = prec; prec > 0; prec--)
    mpf_mul_ui (hold_res, hold_res, 10);
  
  mpz_set_f (rop->object, hold_res);
}
int
main () {

  int i, ix, r, max = 500;

  mpf_t n;
  mpf_init2(n, PREC);

  for (i = LIMIT; i > 0; i--) {
    mpf_set_ui(n, 1);
    mpf_div_ui(n,n,i);
    r = cycle(n);
    if ( r>max) {
      ix = i;
      max = r;
      break;
    }
    //printf("%d for i %d\n", cycle(n), i);
  }

  mpf_set_ui(n, 1);
  mpf_div_ui(n,n,ix);
  printf("Solution is %d with  %d cycles\n", ix, cycle(n));
}
Example #13
0
// The Brent-Salamin algorithm
int main(int argc, char* argv[]) {
  if (argc < 2) return -1;
  int n = (int)strtol(argv[1], NULL, 10);
  mpf_set_default_prec(1000);
  mpf_t a, b, t, c, sum;
  // a=1
  mpf_init_set_ui(a, 1);
  mpf_init_set_ui(sum, 0);
  mpf_init(b);
  mpf_init(t);
  mpf_init(c);
  mpf_init(sum);

  // b=1/sqrt(2)
  mpf_sqrt_ui(b, 2);
  mpf_ui_div(b, 1, b);

  // n次迭代的误差小于\frac{2^{n+9}}{20^{2n+1}}
  for (int i = 1; i <= n; ++i) {
    // t=(a+b)/2
    mpf_add(t, a, b);
    mpf_div_ui(t, t, 2);
    // b=sqrt(a*b);
    mpf_mul(b, a, b);
    mpf_sqrt(b, b);
    // a=t
    mpf_swap(t, a);
    mpf_mul(t, a, a);
    mpf_mul(c, b, b);
    mpf_sub(c, t, c);
    mpf_mul_2exp(c, c, i + 1);
    mpf_add(sum, sum, c);
  }
  mpf_mul(t, a, a);
  mpf_mul_ui(t, t, 4);
  mpf_ui_sub(sum, 1, sum);
  mpf_div(t, t, sum);
  mpf_out_str(stdout, 10, 0, t);
  printf("\n");
  mpf_clear(a);
  mpf_clear(b);
  mpf_clear(t);
  mpf_clear(c);
  mpf_clear(sum);
  return 0;
}
Example #14
0
int main(int argc,char *argv[])
{
    mpf_t mpfVal,mpf1;
    unsigned long i;

    mpf_set_default_prec(9000);
    mpf_init_set_ui(mpf1,1);
    printf ("%ld\n",mpf_get_default_prec());

    for (i=997; i>=997; i--) {
        mpf_init(mpfVal);
        mpf_div_ui(mpfVal,mpf1,i);
        printf("%ld: ",i);
        (void) mpf_out_str(stdout,10,2000,mpfVal);

        mpf_clear(mpfVal);
    }
}
Example #15
0
void correction_factor_Q(mpf_t Q, int N,mpf_t rho,int k){
  unsigned long int f;
  mpf_t tmp,tmp1,tmp2;
  /*
  if (PNK_N_1_rho_1_PN == 0) 
    cout << "Algun factor es 0 N = " << N 
	 << " rho = " << rho 
	 << " k = " << k 
	 << " P__0 = " << P__0
	 << " P__N = " << P__N << endl;
  */
  mpf_init(tmp);
  mpf_init_set_d(tmp1,pow(N,k-1)); // N^(k-1)
  f = 1;
  mpf_set_ui(Q,0);
  for (unsigned long int j = k;j < N;j++) {
    /*Q += (N - j) * pow(N,j) * pow(rho,j - k) * P__0 * N_k_1 / (factorial(j - k) * PNK_N_1_rho_1_PN); */
    /* Q += [(N - j) * N^j * rho^(j - k) / (j - k)!] * P__0 * N_k_1 / PNK_N_1_rho_1_PN; */
    mpf_mul_ui(tmp1,tmp1,N); // * N
    if (j - k > 1) mpf_div_ui(tmp1,tmp1,j - k); // / (j - k)
    mpf_mul_ui(tmp,tmp1,N - j); // * (N - j)
    mpf_add(Q,Q,tmp); // Q += (N - j) * N^j * rho^(j - k) / (j - k)!
    mpf_mul(tmp1,tmp1,rho); // * rho
  }
  mpf_init(tmp2);
  mpf_ui_sub(tmp2,1,P__N); // 1 - P__N
  mpf_pow_ui(tmp1,tmp2,k); // (1 - P__N)^k
  mpf_mul(tmp2,tmp2,rho); // rho * (1 - P__N)
  mpf_ui_sub(tmp2,1,tmp2); // 1 - rho * (1 - P__N)
  
  mpf_mul(Q,Q,P__0); // * P__0
  mpf_mul_ui(Q,Q,factorial(N-k-1)); // * (N - k - 1)!

  /* pow(1 - P__N,k) * factorial(N) * (1 - rho * (1 - P__N)) */
  mpf_mul(tmp2,tmp1,tmp2); // (1 - P__N)^k * (1 - rho * (1 - P__N))
  mpf_mul_ui(tmp2,tmp2,factorial(N)); // (1 - P__N)^k * (1 - rho * (1 - P__N)) * N!
  mpf_div(Q,Q,tmp2); // / [(1 - P__N)^k * (1 - rho * (1 - P__N)) * N!]

  mpf_clear(tmp);
  mpf_clear(tmp1);
  mpf_clear(tmp2);
}
Example #16
0
void
x2 (mpf_t V,			/* result */
    unsigned long int X[],	/* data */
    unsigned int k,		/* #of categories */
    void (P) (mpf_t, unsigned long int, void *), /* probability func */
    void *x,			/* extra user data passed to P() */
    unsigned long int n)	/* #of samples */
{
  unsigned int f;
  mpf_t f_t, f_t2;		/* temp floats */

  mpf_init (f_t); mpf_init (f_t2);


  mpf_set_ui (V, 0);
  for (f = 0; f < k; f++)
    {
      if (g_debug > DEBUG_2)
	fprintf (stderr, "%u: P()=", f);
      mpf_set_ui (f_t, X[f]);
      mpf_mul (f_t, f_t, f_t);	/* f_t = X[f]^2 */
      P (f_t2, f, x);		/* f_t2 = Pr(f) */
      if (g_debug > DEBUG_2)
	mpf_out_str (stderr, 10, 2, f_t2);
      mpf_div (f_t, f_t, f_t2);
      mpf_add (V, V, f_t);
      if (g_debug > DEBUG_2)
	{
	  fprintf (stderr, "\tV=");
	  mpf_out_str (stderr, 10, 2, V);
	  fprintf (stderr, "\t");
	}
    }
  if (g_debug > DEBUG_2)
    fprintf (stderr, "\n");
  mpf_div_ui (V, V, n);
  mpf_sub_ui (V, V, n);

  mpf_clear (f_t); mpf_clear (f_t2);
}
Example #17
0
void
ks_table (mpf_t p, mpf_t val, const unsigned int n)
{
  /* We use Eq. (27), Knuth p.58, skipping O(1/n) for simplicity.
     This shortcut will result in too high probabilities, especially
     when n is small.

     Pr(Kp(n) <= s) = 1 - pow(e, -2*s^2) * (1 - 2/3*s/sqrt(n) + O(1/n)) */

  /* We have 's' in variable VAL and store the result in P. */

  mpf_t t1, t2;

  mpf_init (t1); mpf_init (t2);

  /* t1 = 1 - 2/3 * s/sqrt(n) */
  mpf_sqrt_ui (t1, n);
  mpf_div (t1, val, t1);
  mpf_mul_ui (t1, t1, 2);
  mpf_div_ui (t1, t1, 3);
  mpf_ui_sub (t1, 1, t1);

  /* t2 = pow(e, -2*s^2) */
#ifndef OLDGMP
  mpf_pow_ui (t2, val, 2);	/* t2 = s^2 */
  mpf_set_d (t2, exp (-(2.0 * mpf_get_d (t2))));
#else
  /* hmmm, gmp doesn't have pow() for floats.  use doubles. */
  mpf_set_d (t2, pow (M_E, -(2 * pow (mpf_get_d (val), 2))));
#endif

  /* p = 1 - t1 * t2 */
  mpf_mul (t1, t1, t2);
  mpf_ui_sub (p, 1, t1);

  mpf_clear (t1); mpf_clear (t2);
}
Example #18
0
int
main (void)
{
  mpf_t  f;

  tests_start ();
  mpf_init2 (f, 200L);

  mpf_set_ui (f, 0L);
  one (f, 1);

  mpf_set_ui (f, 1L);
  all (f, 1);

  mpf_set_ui (f, 1L);
  mpf_div_2exp (f, f, 1L);
  all (f, 0);

  mpf_set_ui (f, 1L);
  mpf_div_2exp (f, f, 5000L);
  all (f, 0);

  mpf_set_ui (f, 1L);
  mpf_mul_2exp (f, f, 5000L);
  all (f, 1);

  mpf_set_str (f, "0.5", 10);
  all (f, 0);

  mpf_set_ui (f, 1L);
  mpf_div_ui (f, f, 3L);
  all (f, 0);

  mpf_clear (f);
  tests_end ();
  exit (0);
}
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
	mpf_add (out1, in1, in2);
	mpf_div_ui (out1, out1, 2);
	mpf_mul (out2, in1, in2);
	mpf_sqrt (out2, out2);
}
Example #20
0
/* Pzf(p, s, x) -- Probability for category S in mpz_freqt().  It's
   1/d for all S.  X is a pointer to an unsigned int holding 'd'. */
static void
Pzf (mpf_t p, unsigned long int s, void *x)
{
  mpf_set_ui (p, 1);
  mpf_div_ui (p, p, *((unsigned int *) x));
}
Example #21
0
int pi_worker( unsigned long start_x , unsigned long end_x )
{
	mpf_t		_4 ;
	unsigned long	x ;
	int		flag ;
	mpf_t		pi_incr ;
	mpf_t		pi ;
	
	char		output[ 1024 + 1 ] ;
	
	DC4CSetAppLogFile( "pi_worker" );
	SetLogLevel( LOGLEVEL_INFO );
	
	InfoLog( __FILE__ , __LINE__ , "pi_worker" );
	
	if( start_x % 2 == 0 )
		start_x++;
	if( end_x % 2 == 0 )
		end_x++;
	
	if( start_x < 1 )
		start_x = 1 ;
	if( end_x < start_x )
		end_x = start_x ;
	
	mpf_init_set_d( _4 , 4.00 );
	mpf_init( pi_incr );
	mpf_init( pi );
	
	/*
	                                               0        1         2         3         4
	                                               1234567890123456789012345678901234567890
		           1   1   1   1   1
		PI = 4 * ( _ - _ + _ - _ + _ ... ) = 3.1415926535897932384626433832795
		           1   3   5   7   9
		                        4 1000000000 3.14159265158640477943 14.962s
		                          1000000000 3.14159265557624002834 56.315s
		                         4 100000000 3.14159263358945602263  1.460s
		                           100000000 3.14159267358843756024  5.888s
		                            10000000 3.14159285358961767296  0.621s
		                             1000000 3.14159465358577968703  0.091s
		                              100000 3.14161265318979787768  0.011s
		                               10000 3.14179261359579270235  0.003s
	*/
	mpf_set_d( pi , 0.00 );
	flag = ((start_x/2)%2)?'-':' ' ;
	for( x = start_x ; x <= end_x ; x += 2 )
	{
		mpf_div_ui( pi_incr , _4 , x );
		if( flag == '-' )
			mpf_neg( pi_incr , pi_incr );
		mpf_add( pi , pi , pi_incr );
		
		flag = '-' + ' ' - flag ;
	}
	
	memset( output , 0x00 , sizeof(output) );
	gmp_snprintf( output , sizeof(output)-1 , "%.Ff" , pi );
	InfoLog( __FILE__ , __LINE__ , "pi_worker() - start_x[%lu] end_x[%lu] - PI[%s]" , start_x , end_x , output );
	
	DC4CSetReplyInfo( output );
	
	mpf_clear( _4 );
	mpf_clear( pi_incr );
	mpf_clear( pi );
	
	return 0;
}
Example #22
0
int
main (int argc, char **argv)
{
  mp_size_t size;
  mp_exp_t exp;
  int reps = 10000;
  int i;
  mpf_t u, v, w, x;
  mp_size_t bprec = SIZE * GMP_LIMB_BITS;
  mpf_t rerr, limit_rerr;
  unsigned long ulimb, vlimb;
  int single_flag;

  tests_start ();

  if (argc > 1)
    {
      reps = strtol (argv[1], 0, 0);
      if (argc > 2)
	bprec = strtol (argv[2], 0, 0);
    }

  mpf_set_default_prec (bprec);

  mpf_init (rerr);
  mpf_init (limit_rerr);

  mpf_init (u);
  mpf_init (v);
  mpf_init (w);
  mpf_init (x);

  for (i = 0; i < reps; i++)
    {
      mp_size_t res_prec;

      res_prec = urandom () % bprec + 1;
      mpf_set_prec (w, res_prec);
      mpf_set_prec (x, res_prec);

      mpf_set_ui (limit_rerr, 1);
      mpf_div_2exp (limit_rerr, limit_rerr, res_prec - 1);

      single_flag = 0;

      if ((urandom () & 1) != 0)
	{
	  size = urandom () % (2 * SIZE) - SIZE;
	  exp = urandom () % SIZE;
	  mpf_random2 (u, size, exp);
	}
      else
	{
	  ulimb = urandom ();
	  mpf_set_ui (u, ulimb);
	  single_flag = 1;
	}

      if ((urandom () & 1) != 0)
	{
	  size = urandom () % (2 * SIZE) - SIZE;
	  exp = urandom () % SIZE;
	  mpf_random2 (v, size, exp);
	}
      else
	{
	  vlimb = urandom ();
	  mpf_set_ui (v, vlimb);
	  single_flag = 2;
	}

      if (mpf_sgn (v) == 0)
	continue;

      mpf_div (w, u, v);
      mpf_mul (x, w, v);
      mpf_reldiff (rerr, u, x);
      if (mpf_cmp (rerr, limit_rerr) > 0)
	{
	  printf ("ERROR in mpf_mul or mpf_div after %d tests\n", i);
	  printf ("   u = "); mpf_dump (u);
	  printf ("   v = "); mpf_dump (v);
	  printf ("   x = "); mpf_dump (x);
	  printf ("   w = "); mpf_dump (w);
	  abort ();
	}

      if (single_flag == 2)
	{
	  mpf_div_ui (x, u, vlimb);
	  mpf_reldiff (rerr, w, x);
	  if (mpf_cmp (rerr, limit_rerr) > 0)
	    {
	      printf ("ERROR in mpf_div or mpf_div_ui after %d tests\n", i);
	      printf ("   u = "); mpf_dump (u);
	      printf ("   v = "); mpf_dump (v);
	      printf ("   x = "); mpf_dump (x);
	      printf ("   w = "); mpf_dump (w);
	      abort ();
	    }
	}

      if (single_flag == 1)
	{
	  mpf_ui_div (x, ulimb, v);
	  mpf_reldiff (rerr, w, x);
	  if (mpf_cmp (rerr, limit_rerr) > 0)
	    {
	      printf ("ERROR in mpf_div or mpf_ui_div after %d tests\n", i);
	      printf ("   u = "); mpf_dump (u);
	      printf ("   v = "); mpf_dump (v);
	      printf ("   x = "); mpf_dump (x);
	      printf ("   w = "); mpf_dump (w);
	      abort ();
	    }
	}
    }

  mpf_clear (rerr);
  mpf_clear (limit_rerr);

  mpf_clear (u);
  mpf_clear (v);
  mpf_clear (w);
  mpf_clear (x);

  tests_end ();
  exit (0);
}
Example #23
0
long int julia(const mpf_t x, const mpf_t xr, long int xres, const mpf_t y, const mpf_t yr, long int yres, mpf_t *c, int flag, long int max_iteration,
	  float *iterations, int my_rank, int p, MPI_Comm comm)
{
	double t0 = MPI_Wtime();
	int i,j;

	//------------julia gmp
	const double maxRadius = 4.0;

	// double xi, yi, savex, savex2, savey, radius;
	mpf_t xi, yi, x_min, x_max, y_min, y_max, savex, savex2, savey, radius, xgap, ygap, savex_a, savex_b, savey_a, savey_b, tmp, tmp1;
	mpf_init(xi);
	mpf_init(yi);
	mpf_init(x_min);
	mpf_init(x_max);
	mpf_init(y_min);
	mpf_init(y_max);
	mpf_init(savex);
	mpf_init(savex2);
	mpf_init(savey);
	mpf_init(radius);
	mpf_init(xgap);
	mpf_init(ygap);
	mpf_init(savex_a);
	mpf_init(savex_b);
	mpf_init(savey_a);
	mpf_init(savey_b);

	mpf_init(tmp);
	mpf_init(tmp1);
	//double x_min = x - xr;
	mpf_sub(x_min, x, xr);
	//double x_max = x + xr;
	mpf_add(x_max, x, xr);
	//double y_min = y - yr;
	mpf_sub(y_min, y, yr);
	//double y_max = y + yr;
	mpf_add(y_max, y, yr);

	// spaceing between x and y points
	//double xgap = (x_max - x_min) / xres;
	mpf_sub(xgap, x_max, x_min);
	mpf_div_ui(xgap, xgap, xres);

	//double ygap = (y_max - y_min) / yres;
	mpf_sub(ygap, y_max, y_min);
	mpf_div_ui(ygap, ygap, yres);



	//----------------------------
	long long int iteration;
	long long int total_number_iterations = 0;	
	int k = 0;

	
	for (j = 0; j < yres; j++){
		for (i = 0; i < xres; i++){
			//xi = x_min + i * xgap;
			mpf_mul_ui(tmp, xgap, i);
			mpf_add(xi, x_min, tmp);

			//yi = y_min + j * ygap;
			mpf_mul_ui(tmp, ygap, j);
			mpf_add(yi, y_min, tmp);

			//flag betwee[n julia or mandelbrot
			//savex = flag * c[0] + (1 - flag) * xi;
			mpf_mul_ui(savex_a, c[0], flag);
			mpf_mul_ui(savex_b, xi, (1-flag));
			mpf_add(savex, savex_a, savex_b);

			//savey = flag * c[1] + (1 - flag) * yi;
			mpf_mul_ui(savey_a, c[1], flag);
			mpf_mul_ui(savey_b, yi, (1-flag));
			mpf_add(savey, savey_a, savey_b);

			//radius = 0;
			mpf_set_ui(radius, 0);

			iteration = 0;
			
			//while ((radius <= maxRadius) && (iteration < max_iteration)){
			while ((mpf_cmp_d(radius, maxRadius)<=0) && (iteration < max_iteration)){
				//savex2 = xi;
				mpf_add_ui(savex2, xi, 0);

				//xi = xi * xi - yi * yi + savex;
				mpf_mul(xi, xi, xi);
				mpf_mul(tmp, yi, yi);
				mpf_sub(xi, xi, tmp);
				mpf_add(xi, xi, savex);

				//yi = 2.0f * savex2 * yi + savey;
				mpf_mul_ui(tmp, savex2, 2);
				mpf_mul(yi, yi, tmp);
				mpf_add(yi, yi, savey);

				//radius = xi * xi + yi * yi;
				mpf_mul(tmp, xi, xi);
				mpf_mul(tmp1, yi, yi);
				mpf_add(radius, tmp, tmp1);

				iteration++;
			}

			total_number_iterations += iteration;

			float *p = iterations + k*xres + i;

			//if (radius > maxRadius){
			if (mpf_cmp_d(radius, maxRadius)>0){
				//float zn = sqrt(xi*xi + yi*yi);
				mpf_t zn;
				mpf_init(zn);
				mpf_mul(tmp, xi, xi);
				mpf_mul(tmp1, yi, yi);
				mpf_add(zn, tmp, tmp1);
				mpf_sqrt(zn, zn);
				double n = mpf_get_d(zn);
				//float nu = log(log(zn) / log(2))/log(2);
				double nu = log(log(n) / log(2))/log(2);


				//the point has escaped at iteration at any of the iterations 0,1,2,3...
				*p = iteration + 1 - nu;
			}
			else
			// zij stays within the region up to max_iteration
			{
				assert(iteration==max_iteration);
				*p = -1;
			}
		}
		k++;
	}
	
	//reduce max iteration count
	long long int total_reduced_iterations = -1;
	//printf("rank: %i, total_reduced_iterations: %i\n", my_rank, total_number_iterations);
	MPI_Reduce(&total_number_iterations, &total_reduced_iterations, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, comm);

	double t4 = MPI_Wtime();
	
	double max_reduced_time = -1;
	double total_time = t4 - t0;
	MPI_Reduce(&total_time, &max_reduced_time, 1, MPI_DOUBLE, MPI_MAX, 0, comm);
	
	printf("np: %i, time: %f , iterations: %lld\n",p, max_reduced_time, total_reduced_iterations);
	//clear

	//printf("proc: %i, total time: %lf sec, init: %lf sec, calc: %lf sec, collect: %lf\n", my_rank, t4-t0, t1-t0, t2-t1, t3-t2);
  	return total_reduced_iterations;
}
Example #24
0
void jarvis_hypercube_approximation
(int C, /* Number of types of customers*/
 int N, /* Number of servers */
 mpf_t *lambda, /* arrive rate according to a Poisson process per type */
 mpf_t **Tao, /* expected service time for service i and customer of node m */
 int **a, /* for customers of type m, the list of preferred servers */
 mpf_t **f) {

  mpf_t Lambda;
  mpf_t Rho;
  mpf_t tao;
  mpf_t *rho;
  mpf_t *new_rho;
  mpf_t *Q_N_rho;

  mpf_t max_change;
  mpf_t tmp;

  mpf_init(Lambda);
  rho = new mpf_t [N];
  for (int i = 0; i < N;i++)
    mpf_init(rho[i]);
  mpf_init(tao);
  mpf_init_set_ui(P__0,1);
  mpf_init_set_ui(P__N,1);
  Q_N_rho = new mpf_t [N];
  for (int i = 0;i < N;i++)
    mpf_init(Q_N_rho[i]);
  new_rho = new mpf_t [N];
  for (int i = 0;i < N;i++)
    mpf_init(new_rho[i]);
  mpf_init(Rho);
  mpf_init(max_change);
  mpf_init(tmp);

  /* INITIALIZE: */
  for (int m = 0;m < C;m++) 
    mpf_add(Lambda,Lambda,lambda[m]);

  /* Busy probability rho_i */
  for (int i = 0; i < N;i++) {
    for (int m = 0; m < C;m++) {
      if (a[m][0] == i) {
	mpf_mul(tmp,lambda[m],Tao[i][m]);
	mpf_add(rho[i],rho[i],tmp);
      }
    }
  }

  /* mean service time 'tao' */
  for (int m = 0;m < C;m++) {
    mpf_mul(tmp,lambda[m],Tao[a[m][0]][m]);
    mpf_add(tao,tao,tmp);
  }
  mpf_div(tao,tao,Lambda);

  /* Define intial values for P__0 and P__N */
  for (int i = 0;i < N;i++) {
    mpf_ui_sub(tmp,1,rho[i]);
    mpf_mul(P__0,P__0,tmp);
  }

  for (int i = 0;i < N;i++)
    mpf_mul(P__N,P__N,rho[i]);

  /* ITERATION: */
  do {

    /*
    cout << "'rho_i':";
    for (int i = 0;i < N;i++) cout << " " << mpf_get_d(rho[i]);
    cout << endl;
    */

    /* traffic intensity */
    mpf_mul(Rho,Lambda,tao);
    mpf_div_ui(Rho,Rho,N);

    /* Compute Q(N,'rho',k) */
    for (int k = 0;k < N;k++)
      correction_factor_Q(Q_N_rho[k],N,Rho,k);
  
    /* Compute f_{im} */
    mpf_t rho_a_ml;
    mpf_init(rho_a_ml);
    for (int i = 0;i < N;i++) {
      for (int m = 0;m < C;m++) {
	mpf_set_ui(rho_a_ml,1);
	for (int k = 0;k < N;k++) {
	  if (a[m][k] == i) {
	    mpf_ui_sub(tmp,1,rho[i]);
	    mpf_mul(tmp,tmp,rho_a_ml);
	    mpf_mul(f[i][m],tmp,Q_N_rho[k]);
	    break;
	  }
	  mpf_mul(rho_a_ml,rho_a_ml,rho[a[m][k]]);
	}
      }
    }
    mpf_clear(rho_a_ml);

    /* Aproximation of 'rho'_i */
    mpf_t Vi;
    mpf_init(Vi);
    mpf_init(rho_a_ml);
    for (int i = 0;i < N;i++) {
      mpf_set_ui(Vi,0);
      for (int m = 0;m < C;m++) {
	mpf_set_ui(rho_a_ml,1);
	for (int k = 0;k < N;k++) {
	  if (a[m][k] == i) {
	    mpf_mul(tmp,lambda[m],Tao[i][m]);
	    mpf_mul(tmp,tmp,Q_N_rho[k]);
	    mpf_mul(tmp,tmp,rho_a_ml);
	    mpf_add(Vi,Vi,tmp);
	    /* Vi += lambda[m] * Tao[i][m] * Q_N_rho[k] * rho_a_ml */
	    break;
	  }
	  mpf_mul(rho_a_ml,rho_a_ml,rho[a[m][k]]);
	}
      }
      mpf_add_ui(tmp,Vi,1);
      mpf_div(new_rho[i],Vi,tmp);
    }
    mpf_clear(rho_a_ml);
    mpf_clear(Vi);

    /* Convergence criterion */
    mpf_set_ui(max_change,0);
    for (int i = 0;i < N;i++) {
      mpf_sub(tmp,rho[i],new_rho[i]);
      mpf_abs(tmp,tmp);
      if (mpf_cmp(tmp,max_change) > 0)
	mpf_set(max_change,tmp);
    }
    
    /*
    cout << "max change = " << mpf_get_d(max_change);
    if (mpf_cmp_d(max_change,JARVIS_EPSILON) < 0)
      cout << " **STOP**" << endl;
    else 
      cout << "\r";
    */

    if (mpf_cmp_d(max_change,JARVIS_EPSILON) < 0) break; /* STOP */

    for (int i = 0;i < N;i++)
      mpf_set(rho[i],new_rho[i]);

    /* Compute P__0 */
    mpf_set_ui(P__0,1);
    for (int i = 0;i < N;i++) {
      mpf_ui_sub(tmp,1,rho[i]);
      mpf_mul(P__0,P__0,tmp);
    }

    /* Compute P__N */
    mpf_t s_rho;
    mpf_init(s_rho);
    for (int i = 0;i < N;i++) 
      mpf_add(s_rho,s_rho,rho[i]);
    mpf_div_ui(tmp,s_rho,N);
    mpf_div(tmp,tmp,Rho);
    mpf_ui_sub(P__N,1,tmp);

    /* Compute mean service time 'tao' */
    mpf_t aux;
    mpf_set_ui(tao,0);
    mpf_init(aux);
    for (int m = 0;m < C;m++) {
      mpf_set_ui(tmp,0);
      for (int i = 0;i < N;i++) {
	mpf_mul(aux,Tao[i][m],f[i][m]);
	mpf_add(tmp,tmp,aux);
      }
      mpf_mul(tmp,lambda[m],tmp);
      mpf_add(tao,tao,tmp);
    }
    mpf_div(tao,tao,Lambda); // / Lambda
    mpf_ui_sub(aux,1,P__N); // 1 - P__N
    mpf_div(tao,tao,aux); // / (1 - P__N)
    mpf_clear(aux);
 
  } while (1);

  /*
  cout << "finsh jarvis" << endl;
  for (int i = 0;i < N;i++) {
    for (int m = 0;m < C;m++) {
      cout << mpf_get_d(f[i][m]) << " ";
    }
    cout << endl;
  }
  */

  mpf_clear(tmp);
  mpf_clear(max_change);
  mpf_clear(Rho);
  for (int i = 0;i < N;i++)
    mpf_clear(new_rho[i]);
  delete [] new_rho;
  for (int i = 0;i < N;i++)
    mpf_clear(Q_N_rho[i]);
  delete [] Q_N_rho;
  mpf_clear(P__N);
  mpf_clear(P__0);
  mpf_clear(tao);
  for (int i = 0;i < N;i++)
    mpf_clear(rho[i]);
  delete [] rho;
  mpf_clear(Lambda);
}
Example #25
0
int scanhash_m7m_hash(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
    uint64_t max_nonce, unsigned long *hashes_done)
{
    uint32_t data[32] __attribute__((aligned(128)));
    uint32_t *data_p64 = data + (M7_MIDSTATE_LEN / sizeof(data[0]));
    uint32_t hash[8] __attribute__((aligned(32)));
    uint8_t bhash[7][64] __attribute__((aligned(32)));
    uint32_t n = pdata[19] - 1;
    const uint32_t first_nonce = pdata[19];
    char data_str[161], hash_str[65], target_str[65];
    uint8_t *bdata = 0;
    mpz_t bns[8];
    int rc = 0;
    int bytes, nnNonce2;

    mpz_t product;
    mpz_init(product);

    for(int i=0; i < 8; i++){
        mpz_init(bns[i]);
    }

    memcpy(data, pdata, 80);

    sph_sha256_context       ctx_final_sha256;

    sph_sha256_context       ctx_sha256;
    sph_sha512_context       ctx_sha512;
    sph_keccak512_context    ctx_keccak;
    sph_whirlpool_context    ctx_whirlpool;
    sph_haval256_5_context   ctx_haval;
    sph_tiger_context        ctx_tiger;
    sph_ripemd160_context    ctx_ripemd;

    sph_sha256_init(&ctx_sha256);
    sph_sha256 (&ctx_sha256, data, M7_MIDSTATE_LEN);
    
    sph_sha512_init(&ctx_sha512);
    sph_sha512 (&ctx_sha512, data, M7_MIDSTATE_LEN);
    
    sph_keccak512_init(&ctx_keccak);
    sph_keccak512 (&ctx_keccak, data, M7_MIDSTATE_LEN);

    sph_whirlpool_init(&ctx_whirlpool);
    sph_whirlpool (&ctx_whirlpool, data, M7_MIDSTATE_LEN);
    
    sph_haval256_5_init(&ctx_haval);
    sph_haval256_5 (&ctx_haval, data, M7_MIDSTATE_LEN);

    sph_tiger_init(&ctx_tiger);
    sph_tiger (&ctx_tiger, data, M7_MIDSTATE_LEN);

    sph_ripemd160_init(&ctx_ripemd);
    sph_ripemd160 (&ctx_ripemd, data, M7_MIDSTATE_LEN);

    sph_sha256_context       ctx2_sha256;
    sph_sha512_context       ctx2_sha512;
    sph_keccak512_context    ctx2_keccak;
    sph_whirlpool_context    ctx2_whirlpool;
    sph_haval256_5_context   ctx2_haval;
    sph_tiger_context        ctx2_tiger;
    sph_ripemd160_context    ctx2_ripemd;

    do {
        data[19] = ++n;
        nnNonce2 = (int)(data[19]/2);
        memset(bhash, 0, 7 * 64);

        ctx2_sha256 = ctx_sha256;
        sph_sha256 (&ctx2_sha256, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_sha256_close(&ctx2_sha256, (void*)(bhash[0]));

        ctx2_sha512 = ctx_sha512;
        sph_sha512 (&ctx2_sha512, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_sha512_close(&ctx2_sha512, (void*)(bhash[1]));
        
        ctx2_keccak = ctx_keccak;
        sph_keccak512 (&ctx2_keccak, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_keccak512_close(&ctx2_keccak, (void*)(bhash[2]));

        ctx2_whirlpool = ctx_whirlpool;
        sph_whirlpool (&ctx2_whirlpool, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_whirlpool_close(&ctx2_whirlpool, (void*)(bhash[3]));
        
        ctx2_haval = ctx_haval;
        sph_haval256_5 (&ctx2_haval, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_haval256_5_close(&ctx2_haval, (void*)(bhash[4]));

        ctx2_tiger = ctx_tiger;
        sph_tiger (&ctx2_tiger, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_tiger_close(&ctx2_tiger, (void*)(bhash[5]));

        ctx2_ripemd = ctx_ripemd;
        sph_ripemd160 (&ctx2_ripemd, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_ripemd160_close(&ctx2_ripemd, (void*)(bhash[6]));

        for(int i=0; i < 7; i++){
            set_one_if_zero(bhash[i]);
            mpz_set_uint512(bns[i],bhash[i]);
        }

        mpz_set_ui(bns[7],0);

        for(int i=0; i < 7; i++){
	        mpz_add(bns[7], bns[7], bns[i]);
        }

        mpz_set_ui(product,1);

        for(int i=0; i < 8; i++){
            mpz_mul(product,product,bns[i]);
        }

        mpz_pow_ui(product, product, 2); 

        bytes = mpz_sizeinbase(product, 256);
        bdata = (uint8_t *)realloc(bdata, bytes);
        mpz_export((void *)bdata, NULL, -1, 1, 0, 0, product);

        sph_sha256_init(&ctx_final_sha256);
        sph_sha256 (&ctx_final_sha256, bdata, bytes);
        sph_sha256_close(&ctx_final_sha256, (void*)(hash));

        int digits=(int)((sqrt((double)(nnNonce2))*(1.+EPS))/9000+75);
        int iterations=20;
        mpf_set_default_prec((long int)(digits*BITS_PER_DIGIT+16));

        mpz_t magipi;
        mpz_t magisw;
        mpf_t magifpi;
        mpf_t mpa1, mpb1, mpt1, mpp1;
        mpf_t mpa2, mpb2, mpt2, mpp2;
        mpf_t mpsft;

        mpz_init(magipi);
        mpz_init(magisw);
        mpf_init(magifpi);
        mpf_init(mpsft);
        mpf_init(mpa1);
        mpf_init(mpb1);
        mpf_init(mpt1);
        mpf_init(mpp1);

        mpf_init(mpa2);
        mpf_init(mpb2);
        mpf_init(mpt2);
        mpf_init(mpp2);

        uint32_t usw_;
        usw_ = sw_(nnNonce2, SW_DIVS);
        if (usw_ < 1) usw_ = 1;
        mpz_set_ui(magisw, usw_);
        uint32_t mpzscale=mpz_size(magisw);

        for(int i=0; i < NM7M; i++){
            if (mpzscale > 1000) {
                mpzscale = 1000;
            }
            else if (mpzscale < 1) {
                mpzscale = 1;
            }

            mpf_set_ui(mpa1, 1);
            mpf_set_ui(mpb1, 2);
            mpf_set_d(mpt1, 0.25*mpzscale);
            mpf_set_ui(mpp1, 1);
            mpf_sqrt(mpb1, mpb1);
            mpf_ui_div(mpb1, 1, mpb1);
            mpf_set_ui(mpsft, 10);

            for(int j=0; j <= iterations; j++){
	            mpf_add(mpa2, mpa1,  mpb1);
            	mpf_div_ui(mpa2, mpa2, 2);
            	mpf_mul(mpb2, mpa1, mpb1);
            	mpf_abs(mpb2, mpb2);
            	mpf_sqrt(mpb2, mpb2);
            	mpf_sub(mpt2, mpa1, mpa2);
            	mpf_abs(mpt2, mpt2);
            	mpf_sqrt(mpt2, mpt2);
            	mpf_mul(mpt2, mpt2, mpp1);
            	mpf_sub(mpt2, mpt1, mpt2);
            	mpf_mul_ui(mpp2, mpp1, 2);
            	mpf_swap(mpa1, mpa2);
            	mpf_swap(mpb1, mpb2);
            	mpf_swap(mpt1, mpt2);
            	mpf_swap(mpp1, mpp2);
            }

            mpf_add(magifpi, mpa1, mpb1);
            mpf_pow_ui(magifpi, magifpi, 2);
            mpf_div_ui(magifpi, magifpi, 4);
            mpf_abs(mpt1, mpt1);
            mpf_div(magifpi, magifpi, mpt1);

            mpf_pow_ui(mpsft, mpsft, digits/2);
            mpf_mul(magifpi, magifpi, mpsft);

            mpz_set_f(magipi, magifpi);

            mpz_add(product,product,magipi);
            mpz_add(product,product,magisw);

            mpz_set_uint256(bns[0], (void*)(hash));
            mpz_add(bns[7], bns[7], bns[0]);

            mpz_mul(product,product,bns[7]);
            mpz_cdiv_q (product, product, bns[0]);
            if (mpz_sgn(product) <= 0) mpz_set_ui(product,1);

            bytes = mpz_sizeinbase(product, 256);
            mpzscale=bytes;
            bdata = (uint8_t *)realloc(bdata, bytes);
            mpz_export(bdata, NULL, -1, 1, 0, 0, product);

            sph_sha256_init(&ctx_final_sha256);
            sph_sha256 (&ctx_final_sha256, bdata, bytes);
            sph_sha256_close(&ctx_final_sha256, (void*)(hash));

        }

        mpz_clear(magipi);
        mpz_clear(magisw);
        mpf_clear(magifpi);
        mpf_clear(mpsft);
        mpf_clear(mpa1);
        mpf_clear(mpb1);
        mpf_clear(mpt1);
        mpf_clear(mpp1);

        mpf_clear(mpa2);
        mpf_clear(mpb2);
        mpf_clear(mpt2);
        mpf_clear(mpp2);

        rc = fulltest_m7hash(hash, ptarget);
        if (rc) {
            if (opt_debug) {
                bin2hex(hash_str, (unsigned char *)hash, 32);
                bin2hex(target_str, (unsigned char *)ptarget, 32);
                bin2hex(data_str, (unsigned char *)data, 80);
                applog(LOG_DEBUG, "DEBUG: [%d thread] Found share!\ndata   %s\nhash   %s\ntarget %s", thr_id, 
                    data_str,
                    hash_str,
                    target_str);
            }

            pdata[19] = data[19];

            goto out;
        }
    } while (n < max_nonce && !work_restart[thr_id].restart);

    pdata[19] = n;

out:
    for(int i=0; i < 8; i++){
        mpz_clear(bns[i]);
    }
    mpz_clear(product);
    free(bdata);

    *hashes_done = n - first_nonce + 1;
    return rc;
}
Example #26
0
long int julia(const mpf_t x, const mpf_t xr, long int xres, const mpf_t y, const mpf_t yr, long int yres, mpf_t *c, int flag, long int max_iteration,
	  float *iterations, int my_rank, int p, MPI_Comm comm)
{
	double t0 = MPI_Wtime();
	int i,j;

	// Find how many rows per process.
	int *rows;
	rows = (int*)malloc(sizeof(int)*p);
	for (i=0; i < p; i++)
		rows[i] = yres/p;
	for (i=0; i < yres % p; i++)
		rows[i]++;

	//allocate memory for each processor
	if(my_rank > 0){
		iterations = (float*)malloc( sizeof(float) * xres * rows[my_rank]);
		assert(iterations);
	}

	//------------julia gmp
	const double maxRadius = 4.0;

	mpf_t xi, yi, x_min, x_max, y_min, y_max, savex, savex2, savey, radius, xgap, ygap, savex_a, savex_b, savey_a, savey_b, tmp, tmp1;
	mpf_init(xi);
	mpf_init(yi);
	mpf_init(x_min);
	mpf_init(x_max);
	mpf_init(y_min);
	mpf_init(y_max);
	mpf_init(savex);
	mpf_init(savex2);
	mpf_init(savey);
	mpf_init(radius);
	mpf_init(xgap);
	mpf_init(ygap);
	mpf_init(savex_a);
	mpf_init(savex_b);
	mpf_init(savey_a);
	mpf_init(savey_b);

	mpf_init(tmp);
	mpf_init(tmp1);
	//double x_min = x - xr;
	mpf_sub(x_min, x, xr);
	//double x_max = x + xr;
	mpf_add(x_max, x, xr);
	//double y_min = y - yr;
	mpf_sub(y_min, y, yr);
	//double y_max = y + yr;
	mpf_add(y_max, y, yr);

	// spaceing between x and y points
	//double xgap = (x_max - x_min) / xres;
	mpf_sub(xgap, x_max, x_min);
	mpf_div_ui(xgap, xgap, xres);

	//double ygap = (y_max - y_min) / yres;
	mpf_sub(ygap, y_max, y_min);
	mpf_div_ui(ygap, ygap, yres);



	//----------------------------
	long long int iteration;
	long long int total_number_iterations = 0;	
	int k = 0;

	
	for (j = my_rank; j < yres; j+=p){
		if(my_rank==0)
			k = j;	//needed for root

		for (i = 0; i < xres; i++){
			//xi = x_min + i * xgap;
			mpf_mul_ui(tmp, xgap, i);
			mpf_add(xi, x_min, tmp);

			//yi = y_min + j * ygap;
			mpf_mul_ui(tmp, ygap, j);
			mpf_add(yi, y_min, tmp);

			//flag betwee[n julia or mandelbrot
			//savex = flag * c[0] + (1 - flag) * xi;
			mpf_mul_ui(savex_a, c[0], flag);
			mpf_mul_ui(savex_b, xi, (1-flag));
			mpf_add(savex, savex_a, savex_b);

			//savey = flag * c[1] + (1 - flag) * yi;
			mpf_mul_ui(savey_a, c[1], flag);
			mpf_mul_ui(savey_b, yi, (1-flag));
			mpf_add(savey, savey_a, savey_b);

			//radius = 0;
			mpf_set_ui(radius, 0);

			iteration = 0;
			
			//while ((radius <= maxRadius) && (iteration < max_iteration)){
			while ((mpf_cmp_d(radius, maxRadius)<=0) && (iteration < max_iteration)){
				//savex2 = xi;
				mpf_add_ui(savex2, xi, 0);

				//xi = xi * xi - yi * yi + savex;
				mpf_mul(xi, xi, xi);
				mpf_mul(tmp, yi, yi);
				mpf_sub(xi, xi, tmp);
				mpf_add(xi, xi, savex);

				//yi = 2.0f * savex2 * yi + savey;
				mpf_mul_ui(tmp, savex2, 2);
				mpf_mul(yi, yi, tmp);
				mpf_add(yi, yi, savey);

				//radius = xi * xi + yi * yi;
				mpf_mul(tmp, xi, xi);
				mpf_mul(tmp1, yi, yi);
				mpf_add(radius, tmp, tmp1);

				iteration++;
			}

			total_number_iterations += iteration;

			float *p = iterations + k*xres + i;

			//if (radius > maxRadius){
			if (mpf_cmp_d(radius, maxRadius)>0){
				//float zn = sqrt(xi*xi + yi*yi);
				mpf_t zn;
				mpf_init(zn);
				mpf_mul(tmp, xi, xi);
				mpf_mul(tmp1, yi, yi);
				mpf_add(zn, tmp, tmp1);
				mpf_sqrt(zn, zn);
				double n = mpf_get_d(zn);
				//float nu = log(log(zn) / log(2))/log(2);
				double nu = log(log(n) / log(2))/log(2);


				//the point has escaped at iteration at any of the iterations 0,1,2,3...
				*p = iteration + 1 - nu;
			}
			else
			// zij stays within the region up to max_iteration
			{
				assert(iteration==max_iteration);
				*p = -1;
			}
		}
		k++;
	}
	
	//collect various data
	MPI_Status status;
	if(my_rank == 0){
		int i,j;
		for(i = 1; i < p; i++){
			for(j = 0; j < rows[i]; j++){
				MPI_Recv((iterations + (i + j * p) * xres), xres, MPI_FLOAT, i, 0, comm, &status);
				//MPI_Irecv((iterations + (i + j * p) * xres), xres, MPI_FLOAT, i, 0, comm, NULL);
			}
		}
	}
	else{
		int i;
		for(i = 0; i < rows[my_rank]; i++)
			MPI_Send((iterations + i *xres), xres, MPI_FLOAT, 0, 0, comm);
	}
	

	//reduce max iteration count
	long long int total_reduced_iterations = -1;
	//printf("rank: %i, total_reduced_iterations: %i\n", my_rank, total_number_iterations);
	MPI_Reduce(&total_number_iterations, &total_reduced_iterations, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, comm);

	double t4 = MPI_Wtime();
	
	double max_reduced_time = -1;
	double total_time = t4 - t0;
	MPI_Reduce(&total_time, &max_reduced_time, 1, MPI_DOUBLE, MPI_MAX, 0, comm);
	
	if(my_rank == 0){
		printf("np: %i, time: %f , iterations: %lld\n",p, max_reduced_time, total_reduced_iterations);
		//printf("%i\t%.2e\n", p, max_reduced_time);
	}
	//clear

	//printf("proc: %i, total time: %lf sec, init: %lf sec, calc: %lf sec, collect: %lf\n", my_rank, t4-t0, t1-t0, t2-t1, t3-t2);
  	return total_reduced_iterations;
}
Example #27
0
void
ks (mpf_t Kp,
    mpf_t Km,
    mpf_t X[],
    void (P) (mpf_t, mpf_t),
    unsigned long int n)
{
  mpf_t Kt;			/* temp */
  mpf_t f_x;
  mpf_t f_j;			/* j */
  mpf_t f_jnq;			/* j/n or (j-1)/n */
  unsigned long int j;

  /* Sort the vector in ascending order. */
  qsort (X, n, sizeof (__mpf_struct), mpf_cmp);

  /* K-S test. */
  /*	Kp = sqr(n) * max(j/n - F(Xj))		for all 1<=j<=n
	Km = sqr(n) * max(F(Xj) - (j-1)/n))	for all 1<=j<=n
  */

  mpf_init (Kt); mpf_init (f_x); mpf_init (f_j); mpf_init (f_jnq);
  mpf_set_ui (Kp, 0);  mpf_set_ui (Km, 0);
  for (j = 1; j <= n; j++)
    {
      P (f_x, X[j-1]);
      mpf_set_ui (f_j, j);

      mpf_div_ui (f_jnq, f_j, n);
      mpf_sub (Kt, f_jnq, f_x);
      if (mpf_cmp (Kt, Kp) > 0)
	mpf_set (Kp, Kt);
      if (g_debug > DEBUG_2)
	{
	  printf ("j=%lu ", j);
	  printf ("P()="); mpf_out_str (stdout, 10, 2, f_x); printf ("\t");

	  printf ("jnq="); mpf_out_str (stdout, 10, 2, f_jnq); printf (" ");
	  printf ("diff="); mpf_out_str (stdout, 10, 2, Kt); printf (" ");
	  printf ("Kp="); mpf_out_str (stdout, 10, 2, Kp); printf ("\t");
	}
      mpf_sub_ui (f_j, f_j, 1);
      mpf_div_ui (f_jnq, f_j, n);
      mpf_sub (Kt, f_x, f_jnq);
      if (mpf_cmp (Kt, Km) > 0)
	mpf_set (Km, Kt);

      if (g_debug > DEBUG_2)
	{
	  printf ("jnq="); mpf_out_str (stdout, 10, 2, f_jnq); printf (" ");
	  printf ("diff="); mpf_out_str (stdout, 10, 2, Kt); printf (" ");
	  printf ("Km="); mpf_out_str (stdout, 10, 2, Km); printf (" ");
	  printf ("\n");
	}
    }
  mpf_sqrt_ui (Kt, n);
  mpf_mul (Kp, Kp, Kt);
  mpf_mul (Km, Km, Kt);

  mpf_clear (Kt); mpf_clear (f_x); mpf_clear (f_j); mpf_clear (f_jnq);
}
Example #28
0
void
mpc_div (mpc_t *rop, mpc_t op1, mpc_t op2) 
{
  /* This function needs to be fixed. At the current moment I am tired
   * of having to mess around with the precision values, so I'm just
   * going to convert the values to 'mpf's and call it quits for the
   * day. I'll fix it later.
   * Division by zero is not checked for in this function.
   */
  mpf_t hold_op1;
  mpf_t hold_op2;
  mpf_t hold_res;
  unsigned int prec;

  mpf_init (hold_op1);
  mpf_init (hold_op2);
  mpf_init (hold_res);
  mpf_set_z (hold_op1, op1.object);
  mpf_set_z (hold_op2, op2.object);

  // Get the largest precision
  prec = (op1.precision > op2.precision) ? op1.precision : op2.precision;
  if (prec == 0)
    prec = default_prec;

  // Set the scalar values
  while (op1.precision-- > 0)
    mpf_div_ui (hold_op1, hold_op1, 10);
  while (op2.precision-- > 0)
    mpf_div_ui (hold_op2, hold_op2, 10);

  // Get the value
  mpf_div (hold_res, hold_op1, hold_op2);

  for (rop->precision = prec; prec > 0; prec--)
    mpf_mul_ui (hold_res, hold_res, 10);
  
  mpz_set_f (rop->object, hold_res);
  /*
  temp.precision = (op1.precision < op2.precision) ? op2.precision : op1.precision;

  if (!op1.precision && !op2.precision)
    {
      temp.precision = default_prec;
      mpz_set (temp.object, op1.object);
      power_of_ten (temp.object, default_prec);
      mpz_tdiv_q (temp_res, temp.object, op2.object);
    }
  else if (op1.precision < op2.precision)
    {
      temp.precision = op2.precision;
      mpz_set (temp.object, op1.object);
      power_of_ten (temp.object, op2.precision - op1.precision);
      mpz_tdiv_q (temp_res, temp.object, op2.object);
    }
  else if (op1.precision > op2.precision)
    {
      temp.precision = op1.precision;
      mpz_set (temp.object, op2.object);
      power_of_ten (temp.object, op1.precision - op2.precision);
      mpz_tdiv_q (temp_res, op1.object, temp.object);
    }
  else
    {
      temp.precision = op1.precision;
      mpz_set (temp.object, op1.object);
      mpz_tdiv_q (temp_res, temp.object, op2.object);
    }

  rop->precision = temp.precision;
  mpz_set (rop->object, temp_res);
  */
}
Example #29
0
File: omega.c Project: ramos/omega
void 
omega(long int n, long int m, double tau, long int q, 
			  long int k1, long int k2, mpf_t *factoriales, mpf_t pi) {

  mpf_t aux, aux2, aux3, sqrf, acum, Ltau, z, zelev;
  int j;
  unsigned long int qsqr;

  /* 
	* Set n = min(n,m), and m = max(n,m)
	*/
  j = n;
  n = min(n,m);
  m = max(j,m);
	 
  /*
	* The sqrt(n!m!) pre-factor
	*/
  mpf_init(aux);
  mpf_init(sqrf);
  mpf_mul(aux, factoriales[n], factoriales[m]);
  mpf_sqrt(sqrf, aux);
  
  /*
	* Calculus of tau
	*/
  mpf_init(aux2);
  mpf_init_set_d(Ltau, tau);
  mpf_init(z);
  mpf_init(zelev);
  qsqr = (unsigned long int) q;
  
  
  mpf_set_d(aux, (double) pow((double) k1, (double) 2));
  mpf_mul(aux, aux, Ltau);
  
  mpf_set_d(aux2, (double) pow((double) k2, (double) 2));
  mpf_div(aux, aux, Ltau);

  mpf_add(aux, aux, aux2);
  mpf_div_ui(aux, aux, qsqr);

  mpf_mul(z, aux, pi);

  

  if ( ((m-n)%2) == 0)
	 mpf_pow_ui(zelev, z, (unsigned long int) (m-n)/2);
  else {
	 mpf_pow_ui(zelev, z, m-n);
	 mpf_sqrt(zelev, zelev);
  } 
	 

  /*  mpf_pow_ui(zelev, z, m-n);
	*mpf_pow_ui(z, z, 2);
	*/
  /*  mpf_out_str(stdout, 10, 20, z);
		printf("\n");*/
  /* 
	* The loop
	*/
  mpf_init(acum);
  mpf_init(aux3);
  mpf_set_ui(acum, (unsigned long int) 0); 

  for (j=0; j <= n; j++) {
	 mpf_mul(aux, factoriales[j], factoriales[n-j]);
	 mpf_mul(aux2, aux, factoriales[j+m-n]);

	 mpf_pow_ui(aux3, z, j);
	 mpf_div(aux, aux3, aux2);
	 if ((j%2) == 0) 
		mpf_set(aux2, aux);
	 else
		mpf_neg(aux2, aux);
	 
	 mpf_add(acum, acum, aux2);

  }
  


  mpf_mul(aux, acum, sqrf);
  mpf_mul(aux, aux, zelev);

  gmp_printf("%4d %4d %4d %4d %20.20Fe\n", n, m, k1, k2, aux);
  /*mpf_out_str(stdout, 10, 20, aux);*/

}
/**
 * void calculate_a()
 * 
 * Descricao:
 * 	Calcula o valor da variavel a na n-esima iteracao.
 * 
 * Parametros de entrada:
 * 	-
 * 
 * Parametros de retorno:
 * 	-
 */
void calculate_a(){
  
    mpf_add(a_n[n_count+1], a_n[n_count], b_n[n_count]);
    mpf_div_ui(a_n[n_count+1], a_n[n_count+1], 2);       
    
}