Commit 2c09f8f
uint_divisor, lifted from thrift Fast64BitRemainderCalculator
Summary:
Lift `apache::thrift::frozen::detail::Fast64BitRemainderCalculator` into a new public header `folly/math/Division.h` as `folly::uint_divisor<Word>`, generalized to all unsigned integer types.
Uses Lemire's constant-divisor technique (credited in doc-comments). Key changes vs. the thrift original:
- Support all unsigned integer types, possibly falling back to integer division.
- Support zero divisors since the optimized algorithm does not perform division.
- Calculates composite division and remainder, division, or remainder.
- `constexpr` construction and invocation.
- Invocable-object usage with `operator()` in addition to `divrem`. Mathematical operator usage with `operator/` and `operator%` in addition to `div` and `rem`.
- Thrift uses Lemire’s direct fast-remainder algorithm: multiply by a precomputed reciprocal, then perform another widened multiply to recover the remainder. uint_divisor uses the same reciprocal to compute the exact quotient and derives the remainder as dividend - quotient * divisor. For 64-bit words this reduces the hot path from roughly four multiplies to three while remaining branchless. Narrow types retain Lemire’s direct remainder path where it benchmarks faster.
- Hold the original divisor in addition to the computed multiplier to support mathematical operator usage. This increases in-situ object size.
- Add `uint_divisor<Word>::calc` that does not hold the original divisor; let both `uint_divisor` and `Fast64BitRemainderCalculator` delegate to it.
Reviewed By: iahs
Differential Revision: D115907214
fbshipit-source-id: de6d55b8dac71489f4f36b382eeb9e065380cad11 parent 1dea159 commit 2c09f8f
7 files changed
Lines changed: 2242 additions & 65 deletions
File tree
- third-party
- folly/src
- folly/math
- test
- thrift/src/thrift/lib/cpp2/frozen
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
802 | 802 | | |
803 | 803 | | |
804 | 804 | | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
805 | 809 | | |
806 | 810 | | |
807 | 811 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
25 | 35 | | |
26 | 36 | | |
27 | 37 | | |
| |||
0 commit comments