File size: 3,000 Bytes
8b7c501 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# FXdiv
[![MIT License](https://img.shields.io/badge/License-MIT%20License-blue.svg)](https://github.com/Maratyszcza/FXdiv/blob/master/LICENSE)
[![Build Status](https://img.shields.io/travis/Maratyszcza/FXdiv.svg)](https://travis-ci.org/Maratyszcza/FXdiv)
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
```c
#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:
| Platform | uint32_t | uint64_t | size_t |
| --------------- |:--------:|:--------:|:--------:|
| x86-64 gcc | Works | Works | Works |
| x86-64 clang | Works | Works | Works |
| x86-64 MSVC | Works | Works | Works |
| x86 gcc | Works | Works | Works |
| x86 clang | Works | Works | Works |
| x86 MSVC | Works | Works | Works |
| ARMv7 gcc | Works | Works | Works |
| ARMv7 clang | Works | Works | Works |
| ARMv7 MSVC* | Compiles | Compiles | Compiles |
| ARM64 gcc | Works | Works | Works |
| ARM64 clang | Works | Works | Works |
| ARM64 MSVC* | Compiles | Compiles | Compiles |
| PPC64 gcc | Works | Works | Works |
| WAsm clang | Works | Works | Works |
| Asm.js clang | Works | Works | Works |
| PNaCl clang | Works | Works | Works |
| CUDA | Untested | Untested | Untested |
| OpenCL | Untested | Untested | Untested |
*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](https://gmplib.org/~tege/divcnst-pldi94.pdf)
|