From acb53767009183b2ecd44a9615a7beb20d90f4c7 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Sun, 22 Nov 2026 12:25:09 -0800 Subject: [PATCH] Compute inverse powers of 2 instead of looking them up Replace the 256-entry INVERSE_POWERS_OF_2 table with inv_pow2(), which builds 2^-exp from the exponent bits of a double, as Java's Util.invPow2 does. The values are bit-for-bit the same as the table's for every index; the new test checks 0 to 1022 against ldexp and pow. Co-Authored-By: Claude Opus 5.5 --- common/CMakeLists.txt | 2 +- common/include/inv_pow2.hpp | 43 ++++++++++++ common/include/inv_pow2_table.hpp | 107 ------------------------------ common/test/CMakeLists.txt | 1 + common/test/inv_pow2_test.cpp | 50 ++++++++++++++ cpc/include/cpc_sketch_impl.hpp | 6 +- cpc/include/kxp_byte_lookup.hpp | 2 +- hll/include/HllArray-internal.hpp | 14 ++-- 8 files changed, 106 insertions(+), 119 deletions(-) create mode 100644 common/include/inv_pow2.hpp delete mode 100644 common/include/inv_pow2_table.hpp create mode 100644 common/test/inv_pow2_test.cpp diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index bb316d096..6c789f625 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -41,7 +41,7 @@ install(FILES include/conditional_forward.hpp include/count_zeros.hpp include/fdlibm_log.hpp - include/inv_pow2_table.hpp + include/inv_pow2.hpp include/kolmogorov_smirnov_impl.hpp include/kolmogorov_smirnov.hpp include/memory_operations.hpp diff --git a/common/include/inv_pow2.hpp b/common/include/inv_pow2.hpp new file mode 100644 index 000000000..f9907872d --- /dev/null +++ b/common/include/inv_pow2.hpp @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef INV_POW2_HPP_ +#define INV_POW2_HPP_ + +#include +#include + +namespace datasketches { + +/** + * Computes 2^-exp exactly by building the double from its exponent bits, as Java's Util.invPow2 does. + * Exact for exp from 0 to 1022. + * @param exp the exponent, from 0 to 1022 + * @return 2^-exp + */ +static inline double inv_pow2(uint32_t exp) { + const uint64_t bits = static_cast(1023 - exp) << 52; + double value; + std::memcpy(&value, &bits, sizeof(value)); + return value; +} + +} /* namespace datasketches */ + +#endif diff --git a/common/include/inv_pow2_table.hpp b/common/include/inv_pow2_table.hpp deleted file mode 100644 index c904bfed7..000000000 --- a/common/include/inv_pow2_table.hpp +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef INV_POW_2_TAB_HPP_ -#define INV_POW_2_TAB_HPP_ - -#include - -namespace datasketches { - -// the table was created by the following procedure: - -//void fill_inverse_power_of_2_table() { -// for (int i = 0; i < 256; i++) { -// inverse_powers_of_2_table[i] = pow(2.0, (-1.0 * ((double) i))); -// printf("%.17g", inverse_powers_of_2_table[i]); -// if (i != 255) printf(", "); -// if ((i + 1) % 4 == 0) printf("\n"); -// } -//} - -static const double INVERSE_POWERS_OF_2[256] = { - 1, 0.5, 0.25, 0.125, - 0.0625, 0.03125, 0.015625, 0.0078125, - 0.00390625, 0.001953125, 0.0009765625, 0.00048828125, - 0.000244140625, 0.0001220703125, 6.103515625e-05, 3.0517578125e-05, - 1.52587890625e-05, 7.62939453125e-06, 3.814697265625e-06, 1.9073486328125e-06, - 9.5367431640625e-07, 4.76837158203125e-07, 2.384185791015625e-07, 1.1920928955078125e-07, - 5.9604644775390625e-08, 2.9802322387695312e-08, 1.4901161193847656e-08, 7.4505805969238281e-09, - 3.7252902984619141e-09, 1.862645149230957e-09, 9.3132257461547852e-10, 4.6566128730773926e-10, - 2.3283064365386963e-10, 1.1641532182693481e-10, 5.8207660913467407e-11, 2.9103830456733704e-11, - 1.4551915228366852e-11, 7.2759576141834259e-12, 3.637978807091713e-12, 1.8189894035458565e-12, - 9.0949470177292824e-13, 4.5474735088646412e-13, 2.2737367544323206e-13, 1.1368683772161603e-13, - 5.6843418860808015e-14, 2.8421709430404007e-14, 1.4210854715202004e-14, 7.1054273576010019e-15, - 3.5527136788005009e-15, 1.7763568394002505e-15, 8.8817841970012523e-16, 4.4408920985006262e-16, - 2.2204460492503131e-16, 1.1102230246251565e-16, 5.5511151231257827e-17, 2.7755575615628914e-17, - 1.3877787807814457e-17, 6.9388939039072284e-18, 3.4694469519536142e-18, 1.7347234759768071e-18, - 8.6736173798840355e-19, 4.3368086899420177e-19, 2.1684043449710089e-19, 1.0842021724855044e-19, - 5.4210108624275222e-20, 2.7105054312137611e-20, 1.3552527156068805e-20, 6.7762635780344027e-21, - 3.3881317890172014e-21, 1.6940658945086007e-21, 8.4703294725430034e-22, 4.2351647362715017e-22, - 2.1175823681357508e-22, 1.0587911840678754e-22, 5.2939559203393771e-23, 2.6469779601696886e-23, - 1.3234889800848443e-23, 6.6174449004242214e-24, 3.3087224502121107e-24, 1.6543612251060553e-24, - 8.2718061255302767e-25, 4.1359030627651384e-25, 2.0679515313825692e-25, 1.0339757656912846e-25, - 5.169878828456423e-26, 2.5849394142282115e-26, 1.2924697071141057e-26, 6.4623485355705287e-27, - 3.2311742677852644e-27, 1.6155871338926322e-27, 8.0779356694631609e-28, 4.0389678347315804e-28, - 2.0194839173657902e-28, 1.0097419586828951e-28, 5.0487097934144756e-29, 2.5243548967072378e-29, - 1.2621774483536189e-29, 6.3108872417680944e-30, 3.1554436208840472e-30, 1.5777218104420236e-30, - 7.8886090522101181e-31, 3.944304526105059e-31, 1.9721522630525295e-31, 9.8607613152626476e-32, - 4.9303806576313238e-32, 2.4651903288156619e-32, 1.2325951644078309e-32, 6.1629758220391547e-33, - 3.0814879110195774e-33, 1.5407439555097887e-33, 7.7037197775489434e-34, 3.8518598887744717e-34, - 1.9259299443872359e-34, 9.6296497219361793e-35, 4.8148248609680896e-35, 2.4074124304840448e-35, - 1.2037062152420224e-35, 6.018531076210112e-36, 3.009265538105056e-36, 1.504632769052528e-36, - 7.5231638452626401e-37, 3.76158192263132e-37, 1.88079096131566e-37, 9.4039548065783001e-38, - 4.70197740328915e-38, 2.350988701644575e-38, 1.1754943508222875e-38, 5.8774717541114375e-39, - 2.9387358770557188e-39, 1.4693679385278594e-39, 7.3468396926392969e-40, 3.6734198463196485e-40, - 1.8367099231598242e-40, 9.1835496157991212e-41, 4.5917748078995606e-41, 2.2958874039497803e-41, - 1.1479437019748901e-41, 5.7397185098744507e-42, 2.8698592549372254e-42, 1.4349296274686127e-42, - 7.1746481373430634e-43, 3.5873240686715317e-43, 1.7936620343357659e-43, 8.9683101716788293e-44, - 4.4841550858394146e-44, 2.2420775429197073e-44, 1.1210387714598537e-44, 5.6051938572992683e-45, - 2.8025969286496341e-45, 1.4012984643248171e-45, 7.0064923216240854e-46, 3.5032461608120427e-46, - 1.7516230804060213e-46, 8.7581154020301067e-47, 4.3790577010150533e-47, 2.1895288505075267e-47, - 1.0947644252537633e-47, 5.4738221262688167e-48, 2.7369110631344083e-48, 1.3684555315672042e-48, - 6.8422776578360209e-49, 3.4211388289180104e-49, 1.7105694144590052e-49, 8.5528470722950261e-50, - 4.276423536147513e-50, 2.1382117680737565e-50, 1.0691058840368783e-50, 5.3455294201843913e-51, - 2.6727647100921956e-51, 1.3363823550460978e-51, 6.6819117752304891e-52, 3.3409558876152446e-52, - 1.6704779438076223e-52, 8.3523897190381114e-53, 4.1761948595190557e-53, 2.0880974297595278e-53, - 1.0440487148797639e-53, 5.2202435743988196e-54, 2.6101217871994098e-54, 1.3050608935997049e-54, - 6.5253044679985245e-55, 3.2626522339992623e-55, 1.6313261169996311e-55, 8.1566305849981557e-56, - 4.0783152924990778e-56, 2.0391576462495389e-56, 1.0195788231247695e-56, 5.0978941156238473e-57, - 2.5489470578119236e-57, 1.2744735289059618e-57, 6.3723676445298091e-58, 3.1861838222649046e-58, - 1.5930919111324523e-58, 7.9654595556622614e-59, 3.9827297778311307e-59, 1.9913648889155653e-59, - 9.9568244445778267e-60, 4.9784122222889134e-60, 2.4892061111444567e-60, 1.2446030555722283e-60, - 6.2230152778611417e-61, 3.1115076389305709e-61, 1.5557538194652854e-61, 7.7787690973264271e-62, - 3.8893845486632136e-62, 1.9446922743316068e-62, 9.7234613716580339e-63, 4.861730685829017e-63, - 2.4308653429145085e-63, 1.2154326714572542e-63, 6.0771633572862712e-64, 3.0385816786431356e-64, - 1.5192908393215678e-64, 7.596454196607839e-65, 3.7982270983039195e-65, 1.8991135491519597e-65, - 9.4955677457597987e-66, 4.7477838728798994e-66, 2.3738919364399497e-66, 1.1869459682199748e-66, - 5.9347298410998742e-67, 2.9673649205499371e-67, 1.4836824602749686e-67, 7.4184123013748428e-68, - 3.7092061506874214e-68, 1.8546030753437107e-68, 9.2730153767185535e-69, 4.6365076883592767e-69, - 2.3182538441796384e-69, 1.1591269220898192e-69, 5.7956346104490959e-70, 2.897817305224548e-70, - 1.448908652612274e-70, 7.2445432630613699e-71, 3.6222716315306849e-71, 1.8111358157653425e-71, - 9.0556790788267124e-72, 4.5278395394133562e-72, 2.2639197697066781e-72, 1.131959884853339e-72, - 5.6597994242666952e-73, 2.8298997121333476e-73, 1.4149498560666738e-73, 7.074749280333369e-74, - 3.5373746401666845e-74, 1.7686873200833423e-74, 8.8434366004167113e-75, 4.4217183002083556e-75, - 2.2108591501041778e-75, 1.1054295750520889e-75, 5.5271478752604446e-76, 2.7635739376302223e-76, - 1.3817869688151111e-76, 6.9089348440755557e-77, 3.4544674220377779e-77, 1.7272337110188889e-77 -}; - -} /* namespace datasketches */ - -#endif diff --git a/common/test/CMakeLists.txt b/common/test/CMakeLists.txt index 6db1fc3b8..a4dfb623d 100644 --- a/common/test/CMakeLists.txt +++ b/common/test/CMakeLists.txt @@ -73,6 +73,7 @@ target_sources(common_test binomial_bounds_test.cpp hash_test.cpp serde_test.cpp + inv_pow2_test.cpp ) # now the integration test part diff --git a/common/test/inv_pow2_test.cpp b/common/test/inv_pow2_test.cpp new file mode 100644 index 000000000..cad748f64 --- /dev/null +++ b/common/test/inv_pow2_test.cpp @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include + +#include + +#include "inv_pow2.hpp" + +namespace datasketches { + +static uint64_t bits_of(double value) { + uint64_t bits; + std::memcpy(&bits, &value, sizeof(bits)); + return bits; +} + +TEST_CASE("inv_pow2: exact powers of 2", "[common]") { + REQUIRE(inv_pow2(0) == 1.0); + REQUIRE(inv_pow2(1) == 0.5); + REQUIRE(inv_pow2(64) == 1.0 / 18446744073709551616.0); + // the smallest normal double + REQUIRE(inv_pow2(1022) == std::ldexp(1.0, -1022)); + for (uint32_t exp = 0; exp <= 1022; ++exp) { + CAPTURE(exp); + REQUIRE(bits_of(inv_pow2(exp)) == bits_of(std::ldexp(1.0, -static_cast(exp)))); + // the table this replaced was filled with pow(2.0, -exp) + REQUIRE(bits_of(inv_pow2(exp)) == bits_of(std::pow(2.0, -static_cast(exp)))); + } +} + +} /* namespace datasketches */ diff --git a/cpc/include/cpc_sketch_impl.hpp b/cpc/include/cpc_sketch_impl.hpp index 7fe44d6e6..5bed42f78 100644 --- a/cpc/include/cpc_sketch_impl.hpp +++ b/cpc/include/cpc_sketch_impl.hpp @@ -27,7 +27,7 @@ #include "cpc_confidence.hpp" #include "kxp_byte_lookup.hpp" -#include "inv_pow2_table.hpp" +#include "inv_pow2.hpp" #include "cpc_util.hpp" #include "icon_estimator.hpp" #include "serde.hpp" @@ -268,7 +268,7 @@ void cpc_sketch_alloc::update_hip(uint32_t row_col) { const uint8_t col = row_col & 63; const double one_over_p = static_cast(k) / kxp; hip_est_accum += one_over_p; - kxp -= INVERSE_POWERS_OF_2[col + 1]; // notice the "+1" + kxp -= inv_pow2(col + 1); // notice the "+1" } // In terms of flavor, this promotes SPARSE to HYBRID @@ -372,7 +372,7 @@ void cpc_sketch_alloc::refresh_kxp(const uint64_t* bit_matrix) { double total = 0.0; for (int j = 7; j >= 0; j--) { // the reverse order is important - const double factor = INVERSE_POWERS_OF_2[8 * j]; // pow (256.0, (-1.0 * ((double) j))); + const double factor = inv_pow2(8 * j); // pow (256.0, (-1.0 * ((double) j))); total += factor * byte_sums[j]; } diff --git a/cpc/include/kxp_byte_lookup.hpp b/cpc/include/kxp_byte_lookup.hpp index 4df785d48..64a31de0d 100644 --- a/cpc/include/kxp_byte_lookup.hpp +++ b/cpc/include/kxp_byte_lookup.hpp @@ -30,7 +30,7 @@ namespace datasketches { // for (int col = 0; col < 8; col++) { // const uint8_t bit = (byte >> col) & 1; // if (bit == 0) { // note the inverted logic -// sum += INVERSE_POWERS_OF_2[col + 1]; // note the "+1" +// sum += inv_pow2(col + 1); // note the "+1" // } // } // kxp_byte_table[byte] = sum; diff --git a/hll/include/HllArray-internal.hpp b/hll/include/HllArray-internal.hpp index 1da49caba..ab1679e6d 100644 --- a/hll/include/HllArray-internal.hpp +++ b/hll/include/HllArray-internal.hpp @@ -27,7 +27,7 @@ #include "CubicInterpolation.hpp" #include "CompositeInterpolationXTable.hpp" #include "CouponList.hpp" -#include "inv_pow2_table.hpp" +#include "inv_pow2.hpp" #include #include #include @@ -550,10 +550,10 @@ void HllArray::hipAndKxQIncrementalUpdate(uint8_t oldValue, uint8_t newValue) // update hip BEFORE updating kxq if (!oooFlag_) { hipAccum_ += configK / (kxq0_ + kxq1_); } // update kxq0 and kxq1; subtract first, then add - if (oldValue < 32) { kxq0_ -= INVERSE_POWERS_OF_2[oldValue]; } - else { kxq1_ -= INVERSE_POWERS_OF_2[oldValue]; } - if (newValue < 32) { kxq0_ += INVERSE_POWERS_OF_2[newValue]; } - else { kxq1_ += INVERSE_POWERS_OF_2[newValue]; } + if (oldValue < 32) { kxq0_ -= inv_pow2(oldValue); } + else { kxq1_ -= inv_pow2(oldValue); } + if (newValue < 32) { kxq0_ += inv_pow2(newValue); } + else { kxq1_ += inv_pow2(newValue); } } /** @@ -618,8 +618,8 @@ void HllArray::check_rebuild_kxq_cur_min() { while (it != end) { uint8_t v = HllUtil::getValue(*it); if (v > 0) { - if (v < 32) { kxq0 += INVERSE_POWERS_OF_2[v] - 1.0; } - else { kxq1 += INVERSE_POWERS_OF_2[v] - 1.0; } + if (v < 32) { kxq0 += inv_pow2(v) - 1.0; } + else { kxq1 += inv_pow2(v) - 1.0; } } else { ++num_zeros; }