* Compute the integer square root. * @param num Radicand. * @return Rounded integer square root. * @note Algorithm taken from http://en.wikipedia.org/wiki/Methods_of_computing_square_roots */
| 40 | * @note Algorithm taken from http://en.wikipedia.org/wiki/Methods_of_computing_square_roots |
| 41 | */ |
| 42 | uint32_t IntSqrt(uint32_t num) |
| 43 | { |
| 44 | uint32_t res = 0; |
| 45 | uint32_t bit = 1UL << 30; // Second to top bit number. |
| 46 | |
| 47 | /* 'bit' starts at the highest power of four <= the argument. */ |
| 48 | while (bit > num) bit >>= 2; |
| 49 | |
| 50 | while (bit != 0) { |
| 51 | if (num >= res + bit) { |
| 52 | num -= res + bit; |
| 53 | res = (res >> 1) + bit; |
| 54 | } else { |
| 55 | res >>= 1; |
| 56 | } |
| 57 | bit >>= 2; |
| 58 | } |
| 59 | |
| 60 | /* Arithmetic rounding to nearest integer. */ |
| 61 | if (num > res) res++; |
| 62 | |
| 63 | return res; |
| 64 | } |
no outgoing calls
no test coverage detected