Upgrade FXdiv to 561254d968e5679460e6a0a743206410284d9f46 am: 74c9d9ef6a

Change-Id: If463523d54ae4582aa10026d81929ea2b0848f23
tree: e14feefc15be0a7f4621370bb1bce1db9b8c50fa
  1. .gitignore
  2. .travis.yml
  3. Android.bp
  4. BUILD.bazel
  5. CMakeLists.txt
  6. LICENSE
  7. METADATA
  8. MODULE_LICENSE_MIT
  9. OWNERS
  10. README.md
  11. TEST_MAPPING
  12. WORKSPACE
  13. bench/
  14. cmake/
  15. configure.py
  16. confu.yaml
  17. include/
  18. test/
README.md

FXdiv

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.

Features

  • 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

Example

#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 =
    fxdiv_init_uint32_t(divisor);
  for (size_t i = 0; i < length; i++) {
    array[i] = fxdiv_quotient_uint32_t(array[i], precomputed_divisor);
  }
}

Status

Currently working features:

Platformuint32_tuint64_tsize_t
x86-64 gccWorksWorksWorks
x86-64 clangWorksWorksWorks
x86-64 MSVCWorksWorksWorks
x86 gccWorksWorksWorks
x86 clangWorksWorksWorks
x86 MSVCWorksWorksWorks
ARMv7 gccWorksWorksWorks
ARMv7 clangWorksWorksWorks
ARMv7 MSVC*CompilesCompilesCompiles
ARM64 gccWorksWorksWorks
ARM64 clangWorksWorksWorks
ARM64 MSVC*CompilesCompilesCompiles
PPC64 gccWorksWorksWorks
WAsm clangWorksWorksWorks
Asm.js clangWorksWorksWorks
PNaCl clangWorksWorksWorks
CUDAUntestedUntestedUntested
OpenCLUntestedUntestedUntested

*ARMv7 and ARM64 builds with MSVC are presumed to work, but were only verified to compile successfully

References

  • 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