Programming/MFC-C++

임베디드 최적화 관련 함수

빠릿베짱이 2013. 1. 28. 14:01
반응형

float MathFunction::fast_sqrt(float x)
  {
   float xhalf = 0.5f * x;

   //initial guess of 1/sqrt(x);

   int i = 0x5f3759df - ((*(int*)&x)>>1);

   float y = *(float*)&i;

   //iterate
   y = y * (1.5f-y*y*xhalf);
   y = y * (1.5f-y*y*xhalf);

   //end of calculation of 1/sqrt(x);
   //return sqrt(x) = x * 1/sqrt(x);
   return (x*y);

  }

 

float MathFunction::fast_arctan(float y, float x)
  {
   Uni32SUF _x, _y;
   int ix, iy, ygx, idx;
   float z;

   _x.f = x;
   _y.f = y;
   ix = _x.i;
   iy = _y.i;
   idx = (ix<0)*2+(iy<0)*4;
   ix &= 0x7fffffff;
   iy &= 0x7fffffff;
   ygx = (iy<=ix)-1;
   idx -= ygx;
   idx &= ((ix==0)-1) | ((iy==0)-1);
   /* swap ix and iy if ix < iy */
   ix ^= iy&ygx;
   iy ^= ix&ygx;
   ix ^= iy&ygx;
   _y.i = iy^gAtanSign[idx];
   /* ix = ix != 0 ? ix : 1.f */
   _x.i = ((ix^0x3f800000)&((ix==0)-1))^0x3f800000;
   z = _y.f/_x.f;
   return (ATAN_CF0*(float)ZABS(z)+ATAN_CF1)*z+gAtanTab[idx];

}

반응형