Example #1
0
    NewFloatRepresentation(F f)
        : limbs{}, negative(f < 0.f), fpclass(std::fpclassify(f))
    {
      if (fpclass == FP_INFINITE || fpclass == FP_NAN) return;

      unsigned limbIndex = Length<F>::e0;

      float s = std::fabs(frexp(f, &exponent));

      s *= 1<<29; // pre-scale with 2^29 to extract as many bits as possible in the first iteration
      exponent -= 29; // store prescaling in exponent
      limbs[limbIndex] = static_cast<limb_t>(s);

      while (s) {
        s -= limbs[limbIndex];
        s *= 1<<29;
        mult2(29);

        limbIndex++;
        limbs[limbIndex] = static_cast<limb_t>(s);
      }

      // scale to exponent=0
      if (exponent < 0)
        div2(-exponent);
      else if (exponent > 0)
        mult2(exponent);

    }
Example #2
0
int
main(void)
{
  printf("%d\n", mult2(3));
  int (*fmult2) (int) = mult2;
  printf("%d\n", (*fmult2)(3));
}
Example #3
0
t_vect		unit(t_vect v)
{
	double is;

	if ((is = length(v)) > 1)
		return (mult2(v, (double)(1/is)));
	else
		return (v);
}
Example #4
0
int functions_test()
{
	int sum(int,int);
	int a,b,s;
	printf("Enter two nos.");
	scanf("%d%d", &a,&b);
	s=sum(a,b);
	printf("\nSum is = %d", s);
	mult2();
	return 0;
	
}
Example #5
0
void test() {
complex a, b;
a.x = 2.0;
a.y = 3.0;
b.x = 4;
b.y = 5;
printf("Enter a and b where a + ib is the first complex number.\n");
printf("a = ");
scanf("%f", &a.x);
printf("b = ");
scanf("%f", &a.y);
printf("Enter c and d where c + id is the second complex number.\n");
printf("c = ");
scanf("%f", &b.x);
printf("d = ");
scanf("%f", &b.y);
printf (" The complex numbers entered are :%.1f %+.1f and %.1f %+.1fi\n", a.x,a.y,b.x,b.y);
printf ("The multiplication of complex numbers are :%.1f %+.1fi\n", mult2(a,b).x, mult2(a,b).y);
printf ("The square of complex number is :%.1f %+.1fi\n", square(a).x, square(a).y);
printf ("The addition of complex numbers are :%.1f %+.1fi\n", add2(a,b).x, add2(a,b).y);
printf ("The juliamap of complex numbers are :%.1f %+.1fi\n", juliamap(a,b).x, juliamap(a,b).y);
complex_print(a);
complex_print(b);
}
Example #6
0
int main()
{
	float a=9;
	Parametro f1[2];
	f1[0].amarracao =  LIVRE;
	f1[0].tipo = FLOAT;
	f1[1].amarracao =  CONSTANTE;
	f1[1].tipo = FLOAT;
	f1[1].valor.vFloat = 2;

	mult mult2 = (mult)create(multiplicar,FLOAT,2,f1);
	printf("o dobro de %.2f e: %.2f\n",a, mult2(a));

	return 0;
}
Example #7
0
/* It returns the result of the desirable function among the functions defined above (mult2, square, add2, juliamap)*/
COMPLEX get_result(char *name, COMPLEX *a, COMPLEX *b){
  COMPLEX c;
  if(strcmp("mult2",name)==0){
    c=mult2(a,b);
  }
  else if(strcmp("square",name)==0){
    c=square(a);
  }
  else if(strcmp("add2",name)==0){
    c=add2(a,b);
  }
  else if(strcmp("juliamap",name)==0){
    c=juliamap(a,b);
  }
  return c;
}
Example #8
0
File: light.c Project: NegMozzie/42
double			light_spec(t_light *light, t_vect light_dir, t_vect norm, t_vect ray_dir, double spec)
{
	double		high;
	t_vect		v;
	t_vect		r;
	double		i;
	double		s;


	high = dot(norm, light_dir);
	v = negate(ray_dir);
	r = sub(light_dir, mult2(norm, high * 2.0));
	i = dot(v, r);
	if (i < 0)
		return (0);
	s = pow(i, spec) * KS_CONST * light->i;
	return (s);
}
Example #9
0
void test(){/*this function demonstrates that all of the functions defined above,work correctly.*/
        a.x=1.;
        a.y=3.;
        b.x=2.;
        b.y=1.;
        c=mult2(a,b);
        assert(c.x==-1.);
        assert(c.y==7.);
        printf("mult2 is correct.\n");
        c=square(a);
        assert(c.x==-8.);
        assert(c.y==6.);
        printf("square is correct.\n");
        c=add2(a,b);
        assert(c.x==3.);
        assert(c.y==4.);
        printf("add2 is correct.\n");
        c=juliamap(a,b);
        assert(c.x==-6.);
        assert(c.y==7.);
        printf("juliamap is correct.\n");
        complex_print(a);
        printf("If the previous sentence is 'z=1.000000+3.000000i', the function complex_print is working correctly.\n");
}
Example #10
0
/* Main computational kernel. The whole function will be timed,
   including the call and return. */
    static
void kernel_floyd_warshall(int n,
        DATA_TYPE POLYBENCH_2D(path,N,N,n,n))
{
    int i1, i2, j3;

#pragma scop

    for(int k=0; k< _PB_N; k++){
        multifor(i1=0,i2=0,j3=0; i1<k+1, i2<k+1, j3<k+1; i1++,i2++,j3++; 1,1,1; 0,1,1){
0:{
      mult1(i1, path, k);
  }
1:{
      mult2(i2,path, n, k);
  }
2:{
      mult3(j3,path,n,k);
  }
        }
    for(int i=k+1; i<n;i++){
        for(int j=k+1;j<n;j++){
            path[i][j] = path[i][j] < path[i][k] + path[k][j] ? path[i][j] : path[i][k] + path[k][j];
        }
    }
    }
    //source code  for (k = 0; k < _PB_N; k++)
    //source code  {
    //source code      for(i = 0; i < _PB_N; i++)
    //source code          for (j = 0; j < _PB_N; j++)
    //source code              path[i][j] = path[i][j] < path[i][k] + path[k][j] ?
    //source code                  path[i][j] : path[i][k] + path[k][j];
    //source code  }
#pragma endscop

}
Example #11
0
// Point addition
void PointGFp::add(const PointGFp& rhs, std::vector<BigInt>& ws_bn)
   {
   if(is_zero())
      {
      m_coord_x = rhs.m_coord_x;
      m_coord_y = rhs.m_coord_y;
      m_coord_z = rhs.m_coord_z;
      return;
      }
   else if(rhs.is_zero())
      return;

   const BigInt& p = m_curve.get_p();

   BigInt& rhs_z2 = ws_bn[0];
   BigInt& U1 = ws_bn[1];
   BigInt& S1 = ws_bn[2];

   BigInt& lhs_z2 = ws_bn[3];
   BigInt& U2 = ws_bn[4];
   BigInt& S2 = ws_bn[5];

   BigInt& H = ws_bn[6];
   BigInt& r = ws_bn[7];

   /*
   http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-1998-cmo-2
   */

   curve_sqr(rhs_z2, rhs.m_coord_z);
   curve_mult(U1, m_coord_x, rhs_z2);
   curve_mult(S1, m_coord_y, curve_mult(rhs.m_coord_z, rhs_z2));

   curve_sqr(lhs_z2, m_coord_z);
   curve_mult(U2, rhs.m_coord_x, lhs_z2);
   curve_mult(S2, rhs.m_coord_y, curve_mult(m_coord_z, lhs_z2));

   H = U2;
   H -= U1;
   if(H.is_negative())
      H += p;

   r = S2;
   r -= S1;
   if(r.is_negative())
      r += p;

   if(H.is_zero())
      {
      if(r.is_zero())
         {
         mult2(ws_bn);
         return;
         }

      // setting to zero:
      m_coord_x = 0;
      m_coord_y = 1;
      m_coord_z = 0;
      return;
      }

   curve_sqr(U2, H);

   curve_mult(S2, U2, H);

   U2 = curve_mult(U1, U2);

   curve_sqr(m_coord_x, r);
   m_coord_x -= S2;
   m_coord_x -= (U2 << 1);
   while(m_coord_x.is_negative())
      m_coord_x += p;

   U2 -= m_coord_x;
   if(U2.is_negative())
      U2 += p;

   curve_mult(m_coord_y, r, U2);
   m_coord_y -= curve_mult(S1, S2);
   if(m_coord_y.is_negative())
      m_coord_y += p;

   curve_mult(m_coord_z, curve_mult(m_coord_z, rhs.m_coord_z), H);
   }
Example #12
0
// Square a complex number
COMPLEX square(COMPLEX *a){
  return mult2(a,a);
}
Example #13
0
// Point addition
void PointGFp::add(const PointGFp& rhs, std::vector<BigInt>& ws_bn)
   {
   if(is_zero())
      {
      coord_x = rhs.coord_x;
      coord_y = rhs.coord_y;
      coord_z = rhs.coord_z;
      return;
      }
   else if(rhs.is_zero())
      return;

   const BigInt& p = curve.get_p();

   BigInt& rhs_z2 = ws_bn[0];
   BigInt& U1 = ws_bn[1];
   BigInt& S1 = ws_bn[2];

   BigInt& lhs_z2 = ws_bn[3];
   BigInt& U2 = ws_bn[4];
   BigInt& S2 = ws_bn[5];

   BigInt& H = ws_bn[6];
   BigInt& r = ws_bn[7];

   monty_sqr(rhs_z2, rhs.coord_z);
   monty_mult(U1, coord_x, rhs_z2);
   monty_mult(S1, coord_y, monty_mult(rhs.coord_z, rhs_z2));

   monty_sqr(lhs_z2, coord_z);
   monty_mult(U2, rhs.coord_x, lhs_z2);
   monty_mult(S2, rhs.coord_y, monty_mult(coord_z, lhs_z2));

   H = U2;
   H -= U1;
   if(H.is_negative())
      H += p;

   r = S2;
   r -= S1;
   if(r.is_negative())
      r += p;

   if(H.is_zero())
      {
      if(r.is_zero())
         {
         mult2(ws_bn);
         return;
         }

      *this = PointGFp(curve); // setting myself to zero
      return;
      }

   monty_sqr(U2, H);

   monty_mult(S2, U2, H);

   U2 = monty_mult(U1, U2);

   monty_sqr(coord_x, r);
   coord_x -= S2;
   coord_x -= (U2 << 1);
   while(coord_x.is_negative())
      coord_x += p;

   U2 -= coord_x;
   if(U2.is_negative())
      U2 += p;

   monty_mult(coord_y, r, U2);
   coord_y -= monty_mult(S1, S2);
   if(coord_y.is_negative())
      coord_y += p;

   monty_mult(coord_z, monty_mult(coord_z, rhs.coord_z), H);
   }
Example #14
0
void test()
{
 	/* From now on, we will be using . right after an integer in order to be in full agreement with the definition of 
 *  	 my x_realpart and y_imagpart since they were defined as long double */

	COMPLEX compl1;
	compl1.x_realpart = 1.;
	compl1.y_imagpart = 2.;

	COMPLEX compl2;
	compl2.x_realpart = 3.;
	compl2.y_imagpart = 4.;

	/* let's test */
       /* let's compute the multipliction*/

	COMPLEX expected1;
	expected1.x_realpart = -5.; /* this is the real part of the complex number obtained by computing 1+2i and 3+4i*/
	expected1.y_imagpart = 10.; /* this is the imaginary part of the complex number obtained by computing 1+2i and 3+4i */
	
	COMPLEX result1;
	COMPLEX *cptr11, *cptr12;
	cptr11 = &compl1;
	cptr12 = &compl2;
	result1 = mult2(cptr11,cptr12);
	/*complex_print(result1)*/; /* print the result of the multiplication of two complex numbers*/
	assert (expected1.x_realpart == result1.x_realpart);
	assert (expected1.y_imagpart == result1.y_imagpart);

       /* let's compute the square of the first  complex given above */
	COMPLEX expected2;
	expected2.x_realpart = -3.; /* this is the real part of the complex number obtained by squaring 1+2i*/
	expected2.y_imagpart = 4.;  /* this is the imaginary part of the complex number obtained by squaring 1+2i*/

	COMPLEX result2;
	COMPLEX *cptr2;
	cptr2 = &compl1;
	result2 = square(cptr2);
	/*complex_print(result2)*/; /* print the result of the square of compl1*/
	assert (expected2.x_realpart == result2.x_realpart);
	assert (expected2.y_imagpart == result2.y_imagpart);

       /* let's compute the addition of two complex numbers */
	COMPLEX expected3; 
	expected3.x_realpart = 4.; /* this is the real part of the complex number obtained by adding 1+2i and 3+4i*/
	expected3.y_imagpart = 6.; /* this ia the imag part of the complex number obtained by adding 1+2i and 3+4i*/

	COMPLEX result3;
	COMPLEX *cptr31, *cptr32;
	cptr31 = &compl1;
	cptr32 = &compl2;
	result3 = add2(cptr31,cptr32);
	/*complex_print(result3)*/; /* print the result of the addition of compl1 and compl2*/
	assert (expected3.x_realpart == result3.x_realpart);
	assert (expected3.y_imagpart == result3.y_imagpart);

       /* let's compute the juliamap where z= compl1 is the first variable and c= compl2 is the second variable*/
	COMPLEX expected4;
	expected4.x_realpart = 0.; /* this is the real part of the complex number obtained by adding the square of compl1 to compl2*/
	expected4.y_imagpart = 8.; /* this is the imag part of the complex number obtained by adding the square of compl1 to compl2*/

	COMPLEX result4;
	COMPLEX *cptr41, *cptr42;
	cptr41 = &compl1;
	cptr42 = &compl2;
	result4 = juliamap(cptr41,cptr42); 
	/*complex_print(result4)*/; /* print the result of the juliamap*/
	assert (expected4.x_realpart == result4.x_realpart);
	assert (expected4.y_imagpart == result4.y_imagpart);

       /* let's compute the complex_return function with compl1 as the argument*/
	COMPLEX expected5;
	expected5.x_realpart = 1.;
	expected5.y_imagpart = 2.;

        COMPLEX *cptr5;
	cptr5 = &expected5;
        complex_print(cptr5);
	printf(" if the previous sentence is z = 1.000000+2.000000i, then the complex return is  working as expected \n" );

}
Example #15
0
struct complex square(struct complex a){/*we define a function square that takes a complex structure and returns the square of that structure*/
        z=mult2(a,a);
        return z;
}
Example #16
0
// Projection functions
inline void projectedOnto2(vec2 * r, vec2 a, vec2 b) {
	mult2( r, a, dot2(a, b) );
	invScale2( r, length2(a) * length2(b) );
}
Example #17
0
inline void reflect2(vec2 * r, vec2 n) {
	vec2 i = *r;
	mult2( r, n, dot2( n, i ) * ((scalar)2) );
	subFrom2( r, i );
}
Example #18
0
inline void projectOnto2(vec2 a, vec2 * r) {
	scalar l = length2(a) * length2(*r);
	mult2( r, a, dot2(a, *r) );
	invScale2( r, l );
}
Example #19
0
// Point addition
void PointGFp::add(const PointGFp& rhs, std::vector<BigInt>& ws_bn)
   {
   if(is_zero())
      {
      m_coord_x = rhs.m_coord_x;
      m_coord_y = rhs.m_coord_y;
      m_coord_z = rhs.m_coord_z;
      return;
      }
   else if(rhs.is_zero())
      return;

   const BigInt& p = m_curve.get_p();

   const size_t cap_size = 2*m_curve.get_p_words() + 2;
   for(size_t i = 0; i != ws_bn.size(); ++i)
      ws_bn[i].ensure_capacity(cap_size);

   BigInt& rhs_z2 = ws_bn[0];
   BigInt& U1 = ws_bn[1];
   BigInt& S1 = ws_bn[2];

   BigInt& lhs_z2 = ws_bn[3];
   BigInt& U2 = ws_bn[4];
   BigInt& S2 = ws_bn[5];

   BigInt& H = ws_bn[6];
   BigInt& r = ws_bn[7];

   BigInt& tmp = ws_bn[9];

   secure_vector<word>& monty_ws = ws_bn[8].get_word_vector();

   /*
   https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-1998-cmo-2
   */

   m_curve.sqr(rhs_z2, rhs.m_coord_z, monty_ws);
   m_curve.mul(U1, m_coord_x, rhs_z2, monty_ws);

   m_curve.mul(tmp, rhs.m_coord_z, rhs_z2, monty_ws); // z^3
   m_curve.mul(S1, m_coord_y, tmp, monty_ws);

   m_curve.sqr(lhs_z2, m_coord_z, monty_ws);
   m_curve.mul(U2, rhs.m_coord_x, lhs_z2, monty_ws);

   m_curve.mul(tmp, m_coord_z, lhs_z2, monty_ws);
   m_curve.mul(S2, rhs.m_coord_y, tmp, monty_ws);

   H = U2;
   H -= U1;
   if(H.is_negative())
      H += p;

   r = S2;
   r -= S1;
   if(r.is_negative())
      r += p;

   if(H.is_zero())
      {
      if(r.is_zero())
         {
         mult2(ws_bn);
         return;
         }

      // setting to zero:
      m_coord_x = 0;
      m_coord_y = 1;
      m_coord_z = 0;
      return;
      }

   m_curve.sqr(U2, H, monty_ws);

   m_curve.mul(S2, U2, H, monty_ws);

   m_curve.mul(tmp, U1, U2, monty_ws);
   U2 = tmp;

   m_curve.sqr(m_coord_x, r, monty_ws);
   m_coord_x -= S2;
   m_coord_x -= (U2 << 1);
   while(m_coord_x.is_negative())
      m_coord_x += p;

   U2 -= m_coord_x;
   if(U2.is_negative())
      U2 += p;

   m_curve.mul(m_coord_y, r, U2, monty_ws);
   m_curve.mul(tmp, S1, S2, monty_ws);
   m_coord_y -= tmp;
   if(m_coord_y.is_negative())
      m_coord_y += p;

   m_curve.mul(tmp, m_coord_z, rhs.m_coord_z, monty_ws);
   m_curve.mul(m_coord_z, tmp, H, monty_ws);
   }
Example #20
0
void multstore(long x, long y, long* dest){
    long t = mult2(x, y);
    *dest = t;
}