Bug: 148898578

Clone this repo:
  1. c3ea0c5 Add OWNERS by Miao Wang · 6 days ago master
  2. 320f701 Add build files and meta data files by Miao Wang · 7 days ago
  3. 7f095bd Merge remote-tracking branch 'aosp/upstream-master' into import by Miao Wang · 7 days ago
  4. 33a7041 Initial empty repository by Inna Palant · 13 days ago sdk-release
  5. fd804a9 Travis-CI: configure with CMake by Marat Dukhan · 4 months ago upstream-master


MIT License Build Status

Header-only library for division via fixed-point multiplication by inverse

On modern CPUs and GPUs integer division is several times slower than multiplication. FXdiv implements an algorithm to replace an integer division with a multiplication and two shifts. This algorithm improves performance when an application performs repeated divisions by the same divisor.


  • Integer division for uint32_t, uint64_t, and size_t
  • Header-only library, no installation or build required
  • Compatible with C99, C++, OpenCL, and CUDA
  • Uses platform-specific compiler intrinsics for optimal performance
  • Covered with unit tests and microbenchmarks


#include <fxdiv.h>

/* Division of array by a constant: reference implementation */
void divide_array_c(size_t length, uint32_t array[], uint32_t divisor) {
  for (size_t i = 0; i < length; i++) {
    array[i] /= divisor;

/* Division of array by a constant: implementation with FXdiv */
void divide_array_fxdiv(size_t length, uint32_t array[], uint32_t divisor) {
  const struct fxdiv_divisor_uint32_t precomputed_divisor =
  for (size_t i = 0; i < length; i++) {
    array[i] = fxdiv_quotient_uint32_t(array[i], precomputed_divisor);


Project is in alpha stage. API is unstable. Currently working features:

x86-64 gccWorksWorksWorks
x86-64 MSVCWorksWorksWorks
x86 gccWorksWorksWorks
x86 MSVCWorksWorksWorks
ARMv7 gccWorksWorksWorks
PPC64 gccWorksWorksWorks
PNaCl clangWorksWorksWorks
Asm.js clangWorksWorksWorks


  • Granlund, Torbjörn, and Peter L. Montgomery. “Division by invariant integers using multiplication.” In ACM SIGPLAN Notices, vol. 29, no. 6, pp. 61-72. ACM, 1994. Available: gmplib.org/~tege/divcnst-pldi94.pdf