Skip to content

Instantly share code, notes, and snippets.

@maximevince
Created October 15, 2019 16:25
Show Gist options
  • Save maximevince/5bc7d485f3fa58ae433473ac5c90cca8 to your computer and use it in GitHub Desktop.
Save maximevince/5bc7d485f3fa58ae433473ac5c90cca8 to your computer and use it in GitHub Desktop.
integer sqrt
/* Simple SQRT calculation
* NOTE: result fits within uint16_t (of course)
*/
uint32_t sqrt_int32(uint32_t x)
{
uint32_t res = 0;
uint32_t add = 0x8000;
uint32_t temp;
uint8_t i;
for(i=0; i<16; i++) {
temp = res | add;
if (x>= (temp*temp)) {
res = temp;
}
add>>=1;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment