libfoedus-core
FOEDUS Core Library
|
The pre-calculated p-m pair for optimized integer division by constant. More...
The pre-calculated p-m pair for optimized integer division by constant.
This object implements the optimized integer division described in [HACKER] Chap 10. We assume the divisor is within 32bit and dividee is either 32bit or 64bit. This is a bit different assumption from existing library; libdivide.
This is used in places where integer division by a constant integer is the bottleneck. So far the only such place is partitioning and array storage (Array Storage). In read-only experiments on array storage, we did observe more than 20% overhead in std::lldiv(). It's a division by fan-out, so of course we can afford to pre-calculate p and m. Hence, we added this object.
For example, use it as follows.
This object is totally header-only. This object is totally immutable, so . This object is POD and can trivially copy/move.
For more details, read the following:
Definition at line 67 of file const_div.hpp.
#include <const_div.hpp>
Public Types | |
enum | Constants { kFlagPowerOfTwo = 0x01, kFlagAdd32 = 0x02, kFlagAdd64 = 0x04 } |
Public Member Functions | |
ConstDiv (uint32_t d) | |
Pre-calculate the p-m pair for the given divisor. More... | |
ConstDiv () | |
void | init (uint32_t d) |
uint32_t | div32 (uint32_t n) const |
32-bit integer division that outputs both quotient and remainder. More... | |
uint32_t | rem32 (uint32_t n, uint32_t d, uint32_t q) const |
Calculate remainder. More... | |
uint64_t | div64 (uint64_t n) const |
64-bit integer division that outputs both quotient and remainder. More... | |
uint32_t | rem64 (uint64_t n, uint32_t d, uint64_t q) const |
Calculate remainder. More... | |
Public Attributes | |
uint8_t | d_highest_bits_ |
Highest bits to represent d. More... | |
uint8_t | shift32_ |
"s" for 32 bit division. More... | |
uint8_t | shift64_ |
"s" for 64 bit division. More... | |
uint8_t | flags_ |
misc flags. More... | |
uint32_t | magic32_ |
magic number for 32 bit division. More... | |
uint64_t | magic64_ |
magic number for 64 bit division. More... | |
uint32_t | d_ |
Oridinal divisor. More... | |
uint32_t | dummy_ |
Enumerator | |
---|---|
kFlagPowerOfTwo |
Whether the divisor is a power of 2. When this flag is on, we just shift bits. |
kFlagAdd32 |
Add inidicator for 32bit division. |
kFlagAdd64 |
Add inidicator for 64bit division. |
Definition at line 68 of file const_div.hpp.
|
inlineexplicit |
Pre-calculate the p-m pair for the given divisor.
[in] | d | divisor |
Definition at line 81 of file const_div.hpp.
References init().
|
inline |
Definition at line 85 of file const_div.hpp.
References init().
|
inline |
32-bit integer division that outputs both quotient and remainder.
[in] | n | dividee |
Calculation of remainder is separated below. If the caller calls the two functions in a row, I believe compiler is smart enough to get rid of extra multiplication.
Definition at line 228 of file const_div.hpp.
References d_highest_bits_, flags_, kFlagAdd32, kFlagPowerOfTwo, magic32_, and shift32_.
Referenced by div64(), and foedus::cache::HashFunc::get_bucket_number().
|
inline |
64-bit integer division that outputs both quotient and remainder.
[in] | n | dividee |
Definition at line 241 of file const_div.hpp.
References ASSERT_ND, d_highest_bits_, div32(), flags_, kFlagAdd64, kFlagPowerOfTwo, magic64_, and shift64_.
Referenced by foedus::storage::array::LookupRouteFinder::find_route(), foedus::storage::array::LookupRouteFinder::find_route_and_switch(), and foedus::storage::array::ArrayPartitioner::partition_batch().
|
inline |
Definition at line 137 of file const_div.hpp.
References ASSERT_ND, d_, d_highest_bits_, dummy_, flags_, kFlagAdd32, kFlagAdd64, kFlagPowerOfTwo, magic32_, magic64_, shift32_, and shift64_.
Referenced by ConstDiv().
|
inline |
Calculate remainder.
Definition at line 205 of file const_div.hpp.
References ASSERT_ND, d_, d_highest_bits_, flags_, and kFlagPowerOfTwo.
Referenced by foedus::cache::HashFunc::get_bucket_number().
|
inline |
Calculate remainder.
Definition at line 216 of file const_div.hpp.
References ASSERT_ND, d_, d_highest_bits_, flags_, and kFlagPowerOfTwo.
uint32_t foedus::assorted::ConstDiv::d_ |
uint8_t foedus::assorted::ConstDiv::d_highest_bits_ |
uint32_t foedus::assorted::ConstDiv::dummy_ |
Definition at line 133 of file const_div.hpp.
Referenced by init().
uint8_t foedus::assorted::ConstDiv::flags_ |
uint32_t foedus::assorted::ConstDiv::magic32_ |
magic number for 32 bit division.
Definition at line 125 of file const_div.hpp.
uint64_t foedus::assorted::ConstDiv::magic64_ |
magic number for 64 bit division.
Definition at line 128 of file const_div.hpp.
uint8_t foedus::assorted::ConstDiv::shift32_ |
"s" for 32 bit division.
Definition at line 116 of file const_div.hpp.
uint8_t foedus::assorted::ConstDiv::shift64_ |
"s" for 64 bit division.
Definition at line 119 of file const_div.hpp.