[DevTip] Integer Square Root

정수로 Square Root 계산할 일이 있어 조사한 링크들.

  • http://www.codecodex.com/wiki/Calculate_an_integer_square_root
  • http://stackoverflow.com/questions/1100090/looking-for-an-efficient-integer-square-root-algorithm-for-arm-thumb2
  • http://www.azillionmonkeys.com/qed/sqroot.html

실제 사용한 함수는 아래.
(16-bit RISC MCU용 코드. 즉, int가 2 바이트, long이 4 바이트)

unsigned int sqrt32(unsigned long n)
{
  unsigned int c = 0x8000;
  unsigned int g = 0x8000;

  for(;;) {
    if(g*g > n)
      g ^= c;
    c >>= 1;
    if(c == 0)
      return g;
    g |= c;
  }
}

댓글

이 블로그의 인기 게시물

환경개선부담금

[DevTip] Windows에서 tail 쓰기...