|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <catch2/catch.hpp> |
| 21 | +#include <cmath> |
| 22 | +#include <cstring> |
| 23 | +#include <limits> |
| 24 | + |
| 25 | +#include "hll.hpp" |
| 26 | +#include "fdlibm_log.hpp" |
| 27 | + |
| 28 | +namespace datasketches { |
| 29 | + |
| 30 | +// offsets into the HLL updatable image |
| 31 | +static const size_t CUR_MIN_BYTE = 6; |
| 32 | +static const size_t NUM_AT_CUR_MIN_INT = 32; |
| 33 | +static const size_t HLL_BYTE_ARR_START = 40; |
| 34 | + |
| 35 | +static hll_sketch make(uint8_t lg_k, target_hll_type type, uint64_t lo, uint64_t hi) { |
| 36 | + hll_sketch sk(lg_k, type); |
| 37 | + for (uint64_t i = lo; i < hi; ++i) sk.update(i); |
| 38 | + return sk; |
| 39 | +} |
| 40 | +static uint32_t num_at_cur_min_of(const hll_sketch::vector_bytes& img) { |
| 41 | + uint32_t v; std::memcpy(&v, img.data() + NUM_AT_CUR_MIN_INT, sizeof(v)); return v; |
| 42 | +} |
| 43 | + |
| 44 | +TEST_CASE("hll kxq rebuild: union result is merge-order independent", "[hll_kxq]") { |
| 45 | + // C stays in SET mode, so this exercises the coupon-update path into a gadget with a |
| 46 | + // deferred rebuild pending, which is where the stored curMin/numAtCurMin used to drift |
| 47 | + const hll_sketch a = make(12, HLL_4, 20000, 30364); |
| 48 | + const hll_sketch b = make(10, HLL_8, 5000, 14699); |
| 49 | + const hll_sketch c = make(17, HLL_4, 70000, 72598); |
| 50 | + const hll_sketch* in[3] = {&a, &b, &c}; |
| 51 | + |
| 52 | + auto run = [&](int i, int j, int k) { |
| 53 | + hll_union u(7); |
| 54 | + u.update(*in[i]); u.update(*in[j]); u.update(*in[k]); |
| 55 | + return u.get_result(HLL_8).serialize_updatable(); |
| 56 | + }; |
| 57 | + const auto ref = run(0, 1, 2); |
| 58 | + REQUIRE(run(0, 2, 1) == ref); |
| 59 | + REQUIRE(run(1, 0, 2) == ref); |
| 60 | + REQUIRE(run(1, 2, 0) == ref); |
| 61 | + REQUIRE(run(2, 0, 1) == ref); |
| 62 | + REQUIRE(run(2, 1, 0) == ref); |
| 63 | +} |
| 64 | + |
| 65 | +TEST_CASE("hll kxq rebuild: reading an estimate does not change a later result", |
| 66 | + "[hll_kxq]") { |
| 67 | + // the rebuild is lazy; when it fires must not be observable in the serialized image |
| 68 | + const hll_sketch p = make(13, HLL_8, 0, 50000); |
| 69 | + const hll_sketch q = make(13, HLL_8, 50000, 100000); |
| 70 | + |
| 71 | + for (uint8_t lg_max_k: {uint8_t(7), uint8_t(8), uint8_t(9)}) { |
| 72 | + hll_union peeked(lg_max_k); |
| 73 | + peeked.update(p); peeked.update(q); |
| 74 | + (void) peeked.get_estimate(); // forces the rebuild here |
| 75 | + for (uint64_t v = 9000000; v < 9400000; ++v) peeked.update(v); |
| 76 | + |
| 77 | + hll_union plain(lg_max_k); |
| 78 | + plain.update(p); plain.update(q); |
| 79 | + for (uint64_t v = 9000000; v < 9400000; ++v) plain.update(v); |
| 80 | + |
| 81 | + REQUIRE(peeked.get_result(HLL_8).serialize_updatable() |
| 82 | + == plain.get_result(HLL_8).serialize_updatable()); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +TEST_CASE("hll kxq rebuild: stored curMin and numAtCurMin agree with the registers", |
| 87 | + "[hll_kxq]") { |
| 88 | + // HLL_8 convention: curMin is always 0 and numAtCurMin counts the zero registers |
| 89 | + const hll_sketch a = make(15, HLL_8, 0, 100000); |
| 90 | + const hll_sketch b = make(8, HLL_8, 100000, 200000); |
| 91 | + hll_union u(8); |
| 92 | + u.update(a); u.update(b); |
| 93 | + const auto img = u.get_result(HLL_8).serialize_updatable(); |
| 94 | + |
| 95 | + uint32_t zeros = 0; |
| 96 | + for (size_t i = HLL_BYTE_ARR_START; i < img.size(); ++i) if (img[i] == 0) ++zeros; |
| 97 | + |
| 98 | + REQUIRE(img[CUR_MIN_BYTE] == 0); |
| 99 | + REQUIRE(num_at_cur_min_of(img) == zeros); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_CASE("hll kxq rebuild: relative error constants are full precision", "[hll_kxq]") { |
| 103 | + // lg_k > 12 uses the closed form rather than the interpolation table, so a constant |
| 104 | + // truncated to seven digits is directly observable in the bounds |
| 105 | + const double hip = std::sqrt(std::log(2.0)); // sqrt(ln 2) |
| 106 | + const double non_hip = std::sqrt((3.0 * std::log(2.0)) - 1.0); // sqrt(3 ln 2 - 1) |
| 107 | + |
| 108 | + for (uint8_t lg_k: {uint8_t(13), uint8_t(16), uint8_t(21)}) { |
| 109 | + const double k = static_cast<double>(1 << lg_k); |
| 110 | + for (uint8_t sd = 1; sd <= 3; ++sd) { |
| 111 | + const double got_hip = hll_union::get_rel_err(false, false, lg_k, sd); |
| 112 | + const double got_non = hll_union::get_rel_err(false, true, lg_k, sd); |
| 113 | + REQUIRE(got_hip == Approx(sd * hip / std::sqrt(k)).epsilon(1e-15)); |
| 114 | + REQUIRE(got_non == Approx(sd * non_hip / std::sqrt(k)).epsilon(1e-15)); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +TEST_CASE("hll kxq rebuild: log matches the fdlibm reference", "[hll_kxq]") { |
| 120 | + // the linear counting estimator subtracts two nearby harmonic numbers, which amplifies a |
| 121 | + // 1 ULP difference in log() by more than an order of magnitude. Pin log() to fdlibm, the |
| 122 | + // function datasketches-java's StrictMath.log is specified to be. These expected values |
| 123 | + // were taken from Java; the platform libm differs from every one of them. |
| 124 | + struct { int x; uint64_t bits; } expected[] = { |
| 125 | + { 48, 0x400ef8383c50bb74ULL}, |
| 126 | + { 74, 0x4011375cd6fcab1cULL}, |
| 127 | + { 185, 0x4014e1a4f518c72cULL}, |
| 128 | + { 196, 0x40151cca16d7bba8ULL}, |
| 129 | + { 299, 0x4016cd411481a020ULL}, |
| 130 | + { 308, 0x4016eb9f470ac0b8ULL}, |
| 131 | + { 334, 0x40173e9bbe951e9cULL}, |
| 132 | + { 343, 0x401759d602a5c3c2ULL}, |
| 133 | + { 1261, 0x401c8f031e7e1220ULL}, |
| 134 | + }; |
| 135 | + for (const auto& e: expected) { |
| 136 | + const double got = fdlibm::log(static_cast<double>(e.x)); |
| 137 | + uint64_t bits; std::memcpy(&bits, &got, sizeof(bits)); |
| 138 | + REQUIRE(bits == e.bits); |
| 139 | + } |
| 140 | + |
| 141 | + // edge cases: fdlibm produces these by dividing by a zero constant, which MSVC rejects at |
| 142 | + // compile time, so they are returned directly. Pin the values. |
| 143 | + REQUIRE(fdlibm::log(0.0) == -std::numeric_limits<double>::infinity()); |
| 144 | + REQUIRE(fdlibm::log(-0.0) == -std::numeric_limits<double>::infinity()); |
| 145 | + REQUIRE(std::isnan(fdlibm::log(-1.0))); |
| 146 | + REQUIRE(fdlibm::log(1.0) == 0.0); |
| 147 | + REQUIRE(fdlibm::log(5e-320) == Approx(-735.2178).epsilon(1e-6)); // subnormal scaling path |
| 148 | +} |
| 149 | + |
| 150 | +} /* namespace datasketches */ |
0 commit comments