Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions common/include/inv_pow2.hpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <cstring>

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<uint64_t>(1023 - exp) << 52;
double value;
std::memcpy(&value, &bits, sizeof(value));
return value;
}

} /* namespace datasketches */

#endif
107 changes: 0 additions & 107 deletions common/include/inv_pow2_table.hpp

This file was deleted.

1 change: 1 addition & 0 deletions common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions common/test/inv_pow2_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cmath>
#include <cstdint>
#include <cstring>

#include <catch2/catch.hpp>

#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<int>(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<double>(exp))));
}
}

} /* namespace datasketches */
6 changes: 3 additions & 3 deletions cpc/include/cpc_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -268,7 +268,7 @@ void cpc_sketch_alloc<A>::update_hip(uint32_t row_col) {
const uint8_t col = row_col & 63;
const double one_over_p = static_cast<double>(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
Expand Down Expand Up @@ -372,7 +372,7 @@ void cpc_sketch_alloc<A>::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];
}

Expand Down
2 changes: 1 addition & 1 deletion cpc/include/kxp_byte_lookup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions hll/include/HllArray-internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "CubicInterpolation.hpp"
#include "CompositeInterpolationXTable.hpp"
#include "CouponList.hpp"
#include "inv_pow2_table.hpp"
#include "inv_pow2.hpp"
#include <cstring>
#include <cmath>
#include <stdexcept>
Expand Down Expand Up @@ -550,10 +550,10 @@ void HllArray<A>::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); }
}

/**
Expand Down Expand Up @@ -618,8 +618,8 @@ void HllArray<A>::check_rebuild_kxq_cur_min() {
while (it != end) {
uint8_t v = HllUtil<A>::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;
}
Expand Down
Loading