Skip to content

Instantly share code, notes, and snippets.

@assyrianic
Last active December 8, 2023 05:27
Show Gist options
  • Save assyrianic/a9e428638bd829a0b4b5e9986f53d055 to your computer and use it in GitHub Desktop.
Save assyrianic/a9e428638bd829a0b4b5e9986f53d055 to your computer and use it in GitHub Desktop.
fixed point integer division
/// for int32.
/// a * (1/b) == (a * [(2^16 / b) + 1]) >> 16
#include <stdio.h>
int make_recip(int b) {
return ((1 << 16) / b) + 1;
}
int fast_div(int a, int 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