Turi Create
4.0
|
#include <core/util/fast_integer_power.hpp>
Public Member Functions | |
fast_integer_power (double a=1.0) | |
void | set_base (double a) |
double | pow (size_t b) const GL_HOT_INLINE_FLATTEN |
A class that gives highly optimized version of the power function for when a^b needs to be computed for many different values of b. b must be a non-negative integer (type size_t).
Usage:
fast_integer_power fip(a); // Slower than std::pow by a factor of 10 or so.
fip.pow(b); // Very fast, returns std::pow(a, b); Internals: Let the size_t value x be laid out in 4 bit blocks on disk. | b0 b1 b2 b3 | b4 b5 b6 b7 | ... | +-------------+-------------+ ... block 1 block 2 First note that a^b can be computed for by looking at all the bits of b that are 1, e.g. if b0, b4, and b5, are 1 (so b = 2^0 + 2^4 + 2^5), then a^b = a^(2^0) * a^(2^4) * a^(2^5). Thus caching the values for each of these powers of 2 gives us the final value of a. The method used internally employs this technique, but treats each block as a unit. Thus we actually compute a^b = a^(lookup1[block1 bits]) * a^(lookup2[block2 bits]) * ... with each lookup having 8 bits and thus 256 possible values.
Definition at line 53 of file fast_integer_power.hpp.
|
inline |
Constructs lookup tables to return std::pow(a, b).
Definition at line 58 of file fast_integer_power.hpp.
|
inline |
Returns a^b, where a is given in the constructor.
Calculate out a^n.
Definition at line 93 of file fast_integer_power.hpp.
|
inline |
Sets the base of the power function.
Definition at line 64 of file fast_integer_power.hpp.