Skip to content

Instantly share code, notes, and snippets.

@travisdowns
Created November 6, 2016 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travisdowns/edb91e38dede70a6fa2b882a3790328e to your computer and use it in GitHub Desktop.
Save travisdowns/edb91e38dede70a6fa2b882a3790328e to your computer and use it in GitHub Desktop.
#include <inttypes.h>
#include <stdbool.h>
static inline uint64_t lg(uint64_t x) {
return 63U - (uint64_t)__builtin_clzl(x);
}
static inline uint32_t lg32(uint32_t x) {
return 31U - (uint32_t)__builtin_clz(x);
}
static inline uint64_t divide_chux(uint64_t p, uint64_t q) {
bool round_up = p & (q-1);
return (p >> lg(q)) + round_up;
}
static inline uint32_t divide_chux_32(uint32_t p, uint32_t q) {
bool round_up = p & (q-1);
return (p >> lg32(q)) + round_up;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment