Commit ef7558cd authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

A unit-test for the kSelectInByte table

Summary:
[Folly] A unit-test for the `kSelectInByte` table.

The table essentially contains an encoding of every byte value. So we add a unit-test which decodes from the table, confirming that each encoded byte decodes back to the original.

Also add helpers to the test file to ease viewing disassembly of select64 variants.

Reviewed By: ot

Differential Revision: D9628409

fbshipit-source-id: 61cebaff4e9aff223a06194f7440eb3580a9273d
parent b05020b2
......@@ -476,6 +476,7 @@ if (BUILD_TESTS)
# Depends on liburcu
#TEST read_mostly_shared_ptr_test SOURCES ReadMostlySharedPtrTest.cpp
#TEST ref_count_test SOURCES RefCountTest.cpp
TEST select64_test SOURCES Select64Test.cpp
TEST stringkeyed_test SOURCES StringKeyedTest.cpp
TEST test_util_test SOURCES TestUtilTest.cpp
TEST tuple_ops_test SOURCES TupleOpsTest.cpp
......
......@@ -24,7 +24,17 @@
namespace folly {
namespace detail {
// kSelectInByte
//
// Described in:
// http://dsiutils.di.unimi.it/docs/it/unimi/dsi/bits/Fast.html#selectInByte
//
// A precomputed table containing in position 256j + i, for integers i in
// [0, 256) and j in [0, 8), the position of the j-th set bit in the binary
// representation of i, or 8 if it has fewer than j set bits.
extern const uint8_t kSelectInByte[2048];
} // namespace detail
/**
......
......@@ -133,19 +133,6 @@ TEST_F(EliasFanoCodingTest, SkipForwardPointers) {
doTestAll<128, 128, size_t>();
}
TEST_F(EliasFanoCodingTest, Select64) {
typedef instructions::EF_TEST_ARCH instr;
constexpr uint64_t kPrime = uint64_t(-59);
for (uint64_t x = kPrime, i = 0; i < (1 << 20); x *= kPrime, i += 1) {
size_t w = instr::popcount(x);
for (size_t k = 0; k < w; ++k) {
auto pos = folly::select64<instr>(x, k);
CHECK_EQ((x >> pos) & 1, 1);
CHECK_EQ(instr::popcount(x & ((uint64_t(1) << pos) - 1)), k);
}
}
}
TEST_F(EliasFanoCodingTest, BugLargeGapInUpperBits) { // t16274876
typedef EliasFanoEncoderV2<uint32_t, uint32_t, 2, 2> Encoder;
typedef EliasFanoReader<Encoder, instructions::EF_TEST_ARCH> Reader;
......
/*
* Copyright 2018-present Facebook, Inc.
*
* Licensed 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 <folly/experimental/Select64.h>
#include <cstddef>
#include <cstdint>
#include <folly/experimental/Instructions.h>
#include <folly/portability/GTest.h>
// for disassembling
extern "C" uint64_t check_select64_default(uint64_t x, uint64_t k) {
return folly::select64<folly::compression::instructions::Default>(x, k);
}
extern "C" uint64_t check_select64_haswell(uint64_t x, uint64_t k) {
return folly::select64<folly::compression::instructions::Haswell>(x, k);
}
class Select64Test : public testing::Test {};
using TestArch = folly::compression::instructions::Default;
TEST_F(Select64Test, SelectInByteTable) {
for (size_t i = 0u; i < 256u; ++i) {
uint8_t decoded = 0;
for (size_t j = 0u; j < 8u; ++j) {
auto const entry = folly::detail::kSelectInByte[256 * j + i];
decoded |= uint8_t(entry != 8) << entry;
}
EXPECT_EQ(i, decoded);
}
}
TEST_F(Select64Test, Select64) {
using instr = TestArch;
constexpr uint64_t kPrime = uint64_t(-59);
for (uint64_t x = kPrime, i = 0; i < (1 << 20); x *= kPrime, i += 1) {
auto const w = instr::popcount(x);
for (size_t k = 0; k < w; ++k) {
auto const pos = folly::select64<instr>(x, k);
CHECK_EQ((x >> pos) & 1, 1);
CHECK_EQ(instr::popcount(x & ((uint64_t(1) << pos) - 1)), k);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment