Last active
March 7, 2025 18:56
-
-
Save assyrianic/a9e428638bd829a0b4b5e9986f53d055 to your computer and use it in GitHub Desktop.
fixed point integer division
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// for int32. | |
/// a * (1/b) == (a * [(2^16 / b) + 1]) >> 16 | |
#include <stdio.h> | |
int make_recip(int const b) { | |
return ((1 << 16) / b) + 1; | |
} | |
int fast_div(int const a, int const recip) { | |
return (a * recip) >> 16; | |
} | |
int main() { | |
printf("1: %i\n", fast_div(80, make_recip(40))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment