Skip to content

Commit

Permalink
#13385: Find 10^x with LUT
Browse files Browse the repository at this point in the history
  • Loading branch information
jdh8 committed Oct 27, 2024
1 parent b4a4d6e commit 20bc7ec
Showing 1 changed file with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,30 @@ inline vFloat round_even(vFloat v) {
return v;
}

static float pow_unsigned(float x, uint n) {
if (n == 0)
return 1.0f;

if ((n & 1) == 0)
return pow_unsigned(x * x, n >> 1);

return x * pow_unsigned(x * x, n >> 1);
}

template <bool APPROXIMATE, int ITERATIONS = 8>
void calculate_round(const int decimals) {
const auto exp10i = [](int n) {
if (n < 0)
return pow_unsigned(0.1f, -n);
return pow_unsigned(10.0f, n);
const float TABLE[] = {
1e-45F, 1e-44F, 1e-43F, 1e-42F, 1e-41F, 1e-40F, 1e-39F, 1e-38F,
1e-37F, 1e-36F, 1e-35F, 1e-34F, 1e-33F, 1e-32F, 1e-31F, 1e-30F,
1e-29F, 1e-28F, 1e-27F, 1e-26F, 1e-25F, 1e-24F, 1e-23F, 1e-22F,
1e-21F, 1e-20F, 1e-19F, 1e-18F, 1e-17F, 1e-16F, 1e-15F, 1e-14F,
1e-13F, 1e-12F, 1e-11F, 1e-10F, 1e-9F, 1e-8F, 1e-7F, 1e-6F,
1e-5F, 1e-4F, 1e-3F, 1e-2F, 1e-1F, 1e0F, 1e1F, 1e2F,
1e3F, 1e4F, 1e5F, 1e6F, 1e7F, 1e8F, 1e9F, 1e10F,
1e11F, 1e12F, 1e13F, 1e14F, 1e15F, 1e16F, 1e17F, 1e18F,
1e19F, 1e20F, 1e21F, 1e22F, 1e23F, 1e24F, 1e25F, 1e26F,
1e27F, 1e28F, 1e29F, 1e30F, 1e31F, 1e32F, 1e33F, 1e34F,
1e35F, 1e36F, 1e37F, 1e38F,
};

if (n > 38)
return 1.0F / 0.0F;

if (n < -45)
return 0.0F;

return TABLE[n + 45];
};

const vFloat coeff = exp10i(decimals);
Expand Down

0 comments on commit 20bc7ec

Please sign in to comment.