folly/folly/Fingerprint.cpp

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * 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/Fingerprint.h>

#include <folly/Portability.h>
#include <folly/Utility.h>
#include <folly/detail/FingerprintPolynomial.h>

#include <utility>

namespace folly {
namespace detail {

namespace {

// The polynomials below were generated by a separate program that requires the
// NTL (Number Theory Library) from http://www.shoup.net/ntl/
//
// Briefly: randomly generate a polynomial of degree D, test for
// irreducibility, repeat until you find an irreducible polynomial
// (roughly 1/D of all polynomials of degree D are irreducible, so
// this will succeed in D/2 tries on average; D is small (64..128) so
// this simple method works well)
//
// DO NOT REPLACE THE POLYNOMIALS USED, EVER, as that would change the value
// of every single fingerprint in existence.
template <size_t Deg>
struct FingerprintTablePoly;
template <>
struct FingerprintTablePoly<63> {};
template <>
struct FingerprintTablePoly<95> {};
template <>
struct FingerprintTablePoly<127> {};

template <typename D, size_t S0, size_t... I0>
constexpr auto copy_table(D const (&table)[S0], std::index_sequence<I0...>) {}
template <typename D, size_t S0>
constexpr auto copy_table(D const (&table)[S0]) {}

template <typename D, size_t S0, size_t S1, size_t... I0>
constexpr auto copy_table(
    D const (&table)[S0][S1], std::index_sequence<I0...>) {}
template <typename D, size_t S0, size_t S1>
constexpr auto copy_table(D const (&table)[S0][S1]) {}

template <typename D, size_t S0, size_t S1, size_t S2, size_t... I0>
constexpr auto copy_table(
    D const (&table)[S0][S1][S2], std::index_sequence<I0...>) {}
template <typename D, size_t S0, size_t S1, size_t S2>
constexpr auto copy_table(D const (&table)[S0][S1][S2]) {}

template <size_t Deg>
constexpr poly_table<Deg> make_poly_table() {}

// private global variables marked constexpr to enforce that make_poly_table is
// really invoked at constexpr time, which would not otherwise be guaranteed
FOLLY_STORAGE_CONSTEXPR auto const poly_table_63 =;
FOLLY_STORAGE_CONSTEXPR auto const poly_table_95 =;
FOLLY_STORAGE_CONSTEXPR auto const poly_table_127 =;

} // namespace

template <>
const uint64_t FingerprintTable<64>::poly[poly_size(64)] =;
template <>
const uint64_t FingerprintTable<96>::poly[poly_size(96)] =;
template <>
const uint64_t FingerprintTable<128>::poly[poly_size(128)] =;

template <>
const poly_table<64> FingerprintTable<64>::table =;
template <>
const poly_table<96> FingerprintTable<96>::table =;
template <>
const poly_table<128> FingerprintTable<128>::table =;

} // namespace detail
} // namespace folly