Abseil Common Libraries (C++) (grcp 依赖) https://abseil.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
6.3 KiB

Export of internal Abseil changes -- f012012ef78234a6a4585321b67d7b7c92ebc266 by Laramie Leavitt <lar@google.com>: Slight restructuring of absl/random/internal randen implementation. Convert round-keys.inc into randen_round_keys.cc file. Consistently use a 128-bit pointer type for internal method parameters. This allows simpler pointer arithmetic in C++ & permits removal of some constants and casts. Remove some redundancy in comments & constexpr variables. Specifically, all references to Randen algorithm parameters use RandenTraits; duplication in RandenSlow removed. PiperOrigin-RevId: 312190313 -- dc8b42e054046741e9ed65335bfdface997c6063 by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 312167304 -- f13d248fafaf206492c1362c3574031aea3abaf7 by Matthew Brown <matthewbr@google.com>: Cleanup StrFormat extensions a little. PiperOrigin-RevId: 312166336 -- 9d9117589667afe2332bb7ad42bc967ca7c54502 by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 312105213 -- 9a12b9b3aa0e59b8ee6cf9408ed0029045543a9b by Abseil Team <absl-team@google.com>: Complete IGNORE_TYPE macro renaming. PiperOrigin-RevId: 311999699 -- 64756f20d61021d999bd0d4c15e9ad3857382f57 by Gennadiy Rozental <rogeeff@google.com>: Switch to fixed bytes specific default value. This fixes the Abseil Flags for big endian platforms. PiperOrigin-RevId: 311844448 -- bdbe6b5b29791dbc3816ada1828458b3010ff1e9 by Laramie Leavitt <lar@google.com>: Change many distribution tests to use pcg_engine as a deterministic source of entropy. It's reasonable to test that the BitGen itself has good entropy, however when testing the cross product of all random distributions x all the architecture variations x all submitted changes results in a large number of tests. In order to account for these failures while still using good entropy requires that our allowed sigma need to account for all of these independent tests. Our current sigma values are too restrictive, and we see a lot of failures, so we have to either relax the sigma values or convert some of the statistical tests to use deterministic values. This changelist does the latter. PiperOrigin-RevId: 311840096 GitOrigin-RevId: f012012ef78234a6a4585321b67d7b7c92ebc266 Change-Id: Ic84886f38ff30d7d72c126e9b63c9a61eb729a1a
5 years ago
// Copyright 2017 The Abseil Authors.
//
// 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
//
// https://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 "absl/random/internal/pool_urbg.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <iterator>
#include "gtest/gtest.h"
#include "absl/meta/type_traits.h"
#include "absl/types/span.h"
using absl::random_internal::PoolURBG;
using absl::random_internal::RandenPool;
namespace {
// is_randen_pool trait is true when parameterized by an RandenPool
template <typename T>
using is_randen_pool = typename absl::disjunction< //
std::is_same<T, RandenPool<uint8_t>>, //
std::is_same<T, RandenPool<uint16_t>>, //
std::is_same<T, RandenPool<uint32_t>>, //
std::is_same<T, RandenPool<uint64_t>>>; //
// MyFill either calls RandenPool::Fill() or std::generate(..., rng)
template <typename T, typename V>
typename absl::enable_if_t<absl::negation<is_randen_pool<T>>::value, void> //
MyFill(T& rng, absl::Span<V> data) { // NOLINT(runtime/references)
std::generate(std::begin(data), std::end(data), rng);
}
template <typename T, typename V>
typename absl::enable_if_t<is_randen_pool<T>::value, void> //
MyFill(T& rng, absl::Span<V> data) { // NOLINT(runtime/references)
rng.Fill(data);
}
template <typename EngineType>
class PoolURBGTypedTest : public ::testing::Test {};
using EngineTypes = ::testing::Types< //
RandenPool<uint8_t>, //
RandenPool<uint16_t>, //
RandenPool<uint32_t>, //
RandenPool<uint64_t>, //
PoolURBG<uint8_t, 2>, //
PoolURBG<uint16_t, 2>, //
PoolURBG<uint32_t, 2>, //
PoolURBG<uint64_t, 2>, //
PoolURBG<unsigned int, 8>, // NOLINT(runtime/int)
PoolURBG<unsigned long, 8>, // NOLINT(runtime/int)
PoolURBG<unsigned long int, 4>, // NOLINT(runtime/int)
PoolURBG<unsigned long long, 4>>; // NOLINT(runtime/int)
TYPED_TEST_SUITE(PoolURBGTypedTest, EngineTypes);
// This test is checks that the engines meet the URBG interface requirements
// defined in [rand.req.urbg].
TYPED_TEST(PoolURBGTypedTest, URBGInterface) {
using E = TypeParam;
using T = typename E::result_type;
static_assert(std::is_copy_constructible<E>::value,
"engine must be copy constructible");
static_assert(absl::is_copy_assignable<E>::value,
"engine must be copy assignable");
E e;
const E x;
e();
static_assert(std::is_same<decltype(e()), T>::value,
"return type of operator() must be result_type");
E u0(x);
u0();
E u1 = e;
u1();
}
// This validates that sequences are independent.
TYPED_TEST(PoolURBGTypedTest, VerifySequences) {
using E = TypeParam;
using result_type = typename E::result_type;
E rng;
(void)rng(); // Discard one value.
constexpr int kNumOutputs = 64;
result_type a[kNumOutputs];
result_type b[kNumOutputs];
std::fill(std::begin(b), std::end(b), 0);
// Fill a using Fill or generate, depending on the engine type.
{
E x = rng;
MyFill(x, absl::MakeSpan(a));
}
// Fill b using std::generate().
{
E x = rng;
std::generate(std::begin(b), std::end(b), x);
}
// Test that generated sequence changed as sequence of bits, i.e. if about
// half of the bites were flipped between two non-correlated values.
size_t changed_bits = 0;
size_t unchanged_bits = 0;
size_t total_set = 0;
size_t total_bits = 0;
size_t equal_count = 0;
for (size_t i = 0; i < kNumOutputs; ++i) {
equal_count += (a[i] == b[i]) ? 1 : 0;
std::bitset<sizeof(result_type) * 8> bitset(a[i] ^ b[i]);
changed_bits += bitset.count();
unchanged_bits += bitset.size() - bitset.count();
std::bitset<sizeof(result_type) * 8> a_set(a[i]);
std::bitset<sizeof(result_type) * 8> b_set(b[i]);
total_set += a_set.count() + b_set.count();
total_bits += 2 * 8 * sizeof(result_type);
}
// On average, half the bits are changed between two calls.
EXPECT_LE(changed_bits, 0.60 * (changed_bits + unchanged_bits));
EXPECT_GE(changed_bits, 0.40 * (changed_bits + unchanged_bits));
// verify using a quick normal-approximation to the binomial.
EXPECT_NEAR(total_set, total_bits * 0.5, 4 * std::sqrt(total_bits))
<< "@" << total_set / static_cast<double>(total_bits);
// Also, A[i] == B[i] with probability (1/range) * N.
// Give this a pretty wide latitude, though.
const double kExpected = kNumOutputs / (1.0 * sizeof(result_type) * 8);
EXPECT_LE(equal_count, 1.0 + kExpected);
}
} // namespace
/*
$ nanobenchmarks 1 RandenPool construct
$ nanobenchmarks 1 PoolURBG construct
RandenPool<uint32_t> | 1 | 1000 | 48482.00 ticks | 48.48 ticks | 13.9 ns
RandenPool<uint32_t> | 10 | 2000 | 1028795.00 ticks | 51.44 ticks | 14.7 ns
RandenPool<uint32_t> | 100 | 1000 | 5119968.00 ticks | 51.20 ticks | 14.6 ns
RandenPool<uint32_t> | 1000 | 500 | 25867936.00 ticks | 51.74 ticks | 14.8 ns
RandenPool<uint64_t> | 1 | 1000 | 49921.00 ticks | 49.92 ticks | 14.3 ns
RandenPool<uint64_t> | 10 | 2000 | 1208269.00 ticks | 60.41 ticks | 17.3 ns
RandenPool<uint64_t> | 100 | 1000 | 5844955.00 ticks | 58.45 ticks | 16.7 ns
RandenPool<uint64_t> | 1000 | 500 | 28767404.00 ticks | 57.53 ticks | 16.4 ns
PoolURBG<uint32_t,8> | 1 | 1000 | 86431.00 ticks | 86.43 ticks | 24.7 ns
PoolURBG<uint32_t,8> | 10 | 1000 | 206191.00 ticks | 20.62 ticks | 5.9 ns
PoolURBG<uint32_t,8> | 100 | 1000 | 1516049.00 ticks | 15.16 ticks | 4.3 ns
PoolURBG<uint32_t,8> | 1000 | 500 | 7613936.00 ticks | 15.23 ticks | 4.4 ns
PoolURBG<uint64_t,4> | 1 | 1000 | 96668.00 ticks | 96.67 ticks | 27.6 ns
PoolURBG<uint64_t,4> | 10 | 1000 | 282423.00 ticks | 28.24 ticks | 8.1 ns
PoolURBG<uint64_t,4> | 100 | 1000 | 2609587.00 ticks | 26.10 ticks | 7.5 ns
PoolURBG<uint64_t,4> | 1000 | 500 | 12408757.00 ticks | 24.82 ticks | 7.1 ns
*/