Ponde _usqrt(u32)

sqrt(x)
http://www.codecodex.com/wiki/Calculate_an_integer_square_root

word
_usqrt(op)
{
    word res, one;

    res = 0;

    /* "one" starts at the highest power of four <= than the argument. */
    one = 1 << 30; /* second-to-top bit set */
//    while (one > op) one >>= 2;  ** original **
    while(1) {
    // unsigned comparison i.e. checking 'C' flag
     __push__(op);
     __push__(one);
asm {
    sub
    exc
}
     if(!__pop__()) break;
     one >>= 2;
    }

    while (one != 0) {
//      if (op >= res + one) {   ** original **
    // unsigned comparison i.e. checking 'C' flag
     __push__(op);
     __push__(res + one);
asm {
    sub
    exc
}
     if(!__pop__()) {
        op -= res + one;
        res += one << 1; // <-- faster than 2 * one
      }
      res >>= 1;
      one >>= 2;
    }
    return res;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License