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.

497 lines
19 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/generate_real.h"
#include <cfloat>
#include <cstddef>
#include <cstdint>
#include <string>
#include "gtest/gtest.h"
#include "absl/flags/flag.h"
Export of internal Abseil changes -- dab5caab05d89d03066ef92584660688595a3aaf by Mark Barolak <mbar@google.com>: Add absl::Status and absl::StatusOr to absl/README.md Import of https://github.com/abseil/abseil-cpp/pull/863 PiperOrigin-RevId: 347857368 -- 1ca3c7a96417cd6e6d62f4dc36fd5ddaa61cfa20 by Chris Kennelly <ckennelly@google.com>: Leverage integer power-of-2 functions and bit counting library in Abseil. PiperOrigin-RevId: 347816486 -- e5cbe05879fd65dce7875e2e0105331a1615d89b by Chris Kennelly <ckennelly@google.com>: Mitigate narrowing warning on MSVC. If sizeof(x) <= sizeof(uint32_t), no truncation occurs when casting to uint32_t, but the compiler cannot always determine this. PiperOrigin-RevId: 347696526 -- 079dff64cb175d282d9e22dfb4a522199ffdae2e by Benjamin Barenblat <bbaren@google.com>: Avoid libgcc -NaN narrowing bug When testing -NaN parsing, avoid narrowing -NaN from double to float. This avoids a bug in libgcc (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98251). PiperOrigin-RevId: 347654751 -- 2e78a7634865aeef6765e1f447e96cf8d9985059 by Chris Kennelly <ckennelly@google.com>: Mark popcount helpers as inline. These are conditionally constexpr, so we need to add inline to cover the non-constexpr builds to avoid ODR violations. PiperOrigin-RevId: 347620138 -- 437fbb363aea1654179f102dcdd607ec33c1af1e by Chris Kennelly <ckennelly@google.com>: Use explicit narrowing cast. This is never invoked in practice, but compilers with -Wimplicit-int-conversion may trigger when sizeof(T) > sizeof(uint16_t) prior to determining this never runs. PiperOrigin-RevId: 347609857 GitOrigin-RevId: dab5caab05d89d03066ef92584660688595a3aaf Change-Id: I6296ddffe7ec646f8ce121138f21e1e85a2cff4b
4 years ago
#include "absl/numeric/bits.h"
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
ABSL_FLAG(int64_t, absl_random_test_trials, 50000,
"Number of trials for the probability tests.");
using absl::random_internal::GenerateNegativeTag;
using absl::random_internal::GeneratePositiveTag;
using absl::random_internal::GenerateRealFromBits;
using absl::random_internal::GenerateSignedTag;
namespace {
TEST(GenerateRealTest, U64ToFloat_Positive_NoZero_Test) {
auto ToFloat = [](uint64_t a) {
return GenerateRealFromBits<float, GeneratePositiveTag, false>(a);
};
EXPECT_EQ(ToFloat(0x0000000000000000), 2.710505431e-20f);
EXPECT_EQ(ToFloat(0x0000000000000001), 5.421010862e-20f);
EXPECT_EQ(ToFloat(0x8000000000000000), 0.5);
EXPECT_EQ(ToFloat(0x8000000000000001), 0.5);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), 0.9999999404f);
}
TEST(GenerateRealTest, U64ToFloat_Positive_Zero_Test) {
auto ToFloat = [](uint64_t a) {
return GenerateRealFromBits<float, GeneratePositiveTag, true>(a);
};
EXPECT_EQ(ToFloat(0x0000000000000000), 0.0);
EXPECT_EQ(ToFloat(0x0000000000000001), 5.421010862e-20f);
EXPECT_EQ(ToFloat(0x8000000000000000), 0.5);
EXPECT_EQ(ToFloat(0x8000000000000001), 0.5);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), 0.9999999404f);
}
TEST(GenerateRealTest, U64ToFloat_Negative_NoZero_Test) {
auto ToFloat = [](uint64_t a) {
return GenerateRealFromBits<float, GenerateNegativeTag, false>(a);
};
EXPECT_EQ(ToFloat(0x0000000000000000), -2.710505431e-20f);
EXPECT_EQ(ToFloat(0x0000000000000001), -5.421010862e-20f);
EXPECT_EQ(ToFloat(0x8000000000000000), -0.5);
EXPECT_EQ(ToFloat(0x8000000000000001), -0.5);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), -0.9999999404f);
}
TEST(GenerateRealTest, U64ToFloat_Negative_Zero_Test) {
auto ToFloat = [](uint64_t a) {
return GenerateRealFromBits<float, GenerateNegativeTag, true>(a);
};
EXPECT_EQ(ToFloat(0x0000000000000000), 0.0);
EXPECT_EQ(ToFloat(0x0000000000000001), -5.421010862e-20f);
EXPECT_EQ(ToFloat(0x8000000000000000), -0.5);
EXPECT_EQ(ToFloat(0x8000000000000001), -0.5);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), -0.9999999404f);
}
TEST(GenerateRealTest, U64ToFloat_Signed_NoZero_Test) {
auto ToFloat = [](uint64_t a) {
return GenerateRealFromBits<float, GenerateSignedTag, false>(a);
};
EXPECT_EQ(ToFloat(0x0000000000000000), 5.421010862e-20f);
EXPECT_EQ(ToFloat(0x0000000000000001), 1.084202172e-19f);
EXPECT_EQ(ToFloat(0x7FFFFFFFFFFFFFFF), 0.9999999404f);
EXPECT_EQ(ToFloat(0x8000000000000000), -5.421010862e-20f);
EXPECT_EQ(ToFloat(0x8000000000000001), -1.084202172e-19f);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), -0.9999999404f);
}
TEST(GenerateRealTest, U64ToFloat_Signed_Zero_Test) {
auto ToFloat = [](uint64_t a) {
return GenerateRealFromBits<float, GenerateSignedTag, true>(a);
};
EXPECT_EQ(ToFloat(0x0000000000000000), 0);
EXPECT_EQ(ToFloat(0x0000000000000001), 1.084202172e-19f);
EXPECT_EQ(ToFloat(0x7FFFFFFFFFFFFFFF), 0.9999999404f);
EXPECT_EQ(ToFloat(0x8000000000000000), 0);
EXPECT_EQ(ToFloat(0x8000000000000001), -1.084202172e-19f);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), -0.9999999404f);
}
TEST(GenerateRealTest, U64ToFloat_Signed_Bias_Test) {
auto ToFloat = [](uint64_t a) {
return GenerateRealFromBits<float, GenerateSignedTag, true>(a, 1);
};
EXPECT_EQ(ToFloat(0x0000000000000000), 0);
EXPECT_EQ(ToFloat(0x0000000000000001), 2 * 1.084202172e-19f);
EXPECT_EQ(ToFloat(0x7FFFFFFFFFFFFFFF), 2 * 0.9999999404f);
EXPECT_EQ(ToFloat(0x8000000000000000), 0);
EXPECT_EQ(ToFloat(0x8000000000000001), 2 * -1.084202172e-19f);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), 2 * -0.9999999404f);
}
TEST(GenerateRealTest, U64ToFloatTest) {
auto ToFloat = [](uint64_t a) -> float {
return GenerateRealFromBits<float, GeneratePositiveTag, true>(a);
};
EXPECT_EQ(ToFloat(0x0000000000000000), 0.0f);
EXPECT_EQ(ToFloat(0x8000000000000000), 0.5f);
EXPECT_EQ(ToFloat(0x8000000000000001), 0.5f);
EXPECT_EQ(ToFloat(0x800000FFFFFFFFFF), 0.5f);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), 0.9999999404f);
EXPECT_GT(ToFloat(0x0000000000000001), 0.0f);
EXPECT_NE(ToFloat(0x7FFFFF0000000000), ToFloat(0x7FFFFEFFFFFFFFFF));
EXPECT_LT(ToFloat(0xFFFFFFFFFFFFFFFF), 1.0f);
int32_t two_to_24 = 1 << 24;
EXPECT_EQ(static_cast<int32_t>(ToFloat(0xFFFFFFFFFFFFFFFF) * two_to_24),
two_to_24 - 1);
EXPECT_NE(static_cast<int32_t>(ToFloat(0xFFFFFFFFFFFFFFFF) * two_to_24 * 2),
two_to_24 * 2 - 1);
EXPECT_EQ(ToFloat(0xFFFFFFFFFFFFFFFF), ToFloat(0xFFFFFF0000000000));
EXPECT_NE(ToFloat(0xFFFFFFFFFFFFFFFF), ToFloat(0xFFFFFEFFFFFFFFFF));
EXPECT_EQ(ToFloat(0x7FFFFFFFFFFFFFFF), ToFloat(0x7FFFFF8000000000));
EXPECT_NE(ToFloat(0x7FFFFFFFFFFFFFFF), ToFloat(0x7FFFFF7FFFFFFFFF));
EXPECT_EQ(ToFloat(0x3FFFFFFFFFFFFFFF), ToFloat(0x3FFFFFC000000000));
EXPECT_NE(ToFloat(0x3FFFFFFFFFFFFFFF), ToFloat(0x3FFFFFBFFFFFFFFF));
// For values where every bit counts, the values scale as multiples of the
// input.
for (int i = 0; i < 100; ++i) {
EXPECT_EQ(i * ToFloat(0x0000000000000001), ToFloat(i));
}
// For each i: value generated from (1 << i).
float exp_values[64];
exp_values[63] = 0.5f;
for (int i = 62; i >= 0; --i) exp_values[i] = 0.5f * exp_values[i + 1];
constexpr uint64_t one = 1;
for (int i = 0; i < 64; ++i) {
EXPECT_EQ(ToFloat(one << i), exp_values[i]);
for (int j = 1; j < FLT_MANT_DIG && i - j >= 0; ++j) {
EXPECT_NE(exp_values[i] + exp_values[i - j], exp_values[i]);
EXPECT_EQ(ToFloat((one << i) + (one << (i - j))),
exp_values[i] + exp_values[i - j]);
}
for (int j = FLT_MANT_DIG; i - j >= 0; ++j) {
EXPECT_EQ(exp_values[i] + exp_values[i - j], exp_values[i]);
EXPECT_EQ(ToFloat((one << i) + (one << (i - j))), exp_values[i]);
}
}
}
TEST(GenerateRealTest, U64ToDouble_Positive_NoZero_Test) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GeneratePositiveTag, false>(a);
};
EXPECT_EQ(ToDouble(0x0000000000000000), 2.710505431213761085e-20);
EXPECT_EQ(ToDouble(0x0000000000000001), 5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x0000000000000002), 1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0x8000000000000000), 0.5);
EXPECT_EQ(ToDouble(0x8000000000000001), 0.5);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), 0.999999999999999888978);
}
TEST(GenerateRealTest, U64ToDouble_Positive_Zero_Test) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GeneratePositiveTag, true>(a);
};
EXPECT_EQ(ToDouble(0x0000000000000000), 0.0);
EXPECT_EQ(ToDouble(0x0000000000000001), 5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x8000000000000000), 0.5);
EXPECT_EQ(ToDouble(0x8000000000000001), 0.5);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), 0.999999999999999888978);
}
TEST(GenerateRealTest, U64ToDouble_Negative_NoZero_Test) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GenerateNegativeTag, false>(a);
};
EXPECT_EQ(ToDouble(0x0000000000000000), -2.710505431213761085e-20);
EXPECT_EQ(ToDouble(0x0000000000000001), -5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x0000000000000002), -1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0x8000000000000000), -0.5);
EXPECT_EQ(ToDouble(0x8000000000000001), -0.5);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978);
}
TEST(GenerateRealTest, U64ToDouble_Negative_Zero_Test) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GenerateNegativeTag, true>(a);
};
EXPECT_EQ(ToDouble(0x0000000000000000), 0.0);
EXPECT_EQ(ToDouble(0x0000000000000001), -5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x0000000000000002), -1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0x8000000000000000), -0.5);
EXPECT_EQ(ToDouble(0x8000000000000001), -0.5);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978);
}
TEST(GenerateRealTest, U64ToDouble_Signed_NoZero_Test) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GenerateSignedTag, false>(a);
};
EXPECT_EQ(ToDouble(0x0000000000000000), 5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x0000000000000001), 1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), 0.999999999999999888978);
EXPECT_EQ(ToDouble(0x8000000000000000), -5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x8000000000000001), -1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978);
}
TEST(GenerateRealTest, U64ToDouble_Signed_Zero_Test) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GenerateSignedTag, true>(a);
};
EXPECT_EQ(ToDouble(0x0000000000000000), 0);
EXPECT_EQ(ToDouble(0x0000000000000001), 1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), 0.999999999999999888978);
EXPECT_EQ(ToDouble(0x8000000000000000), 0);
EXPECT_EQ(ToDouble(0x8000000000000001), -1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978);
}
TEST(GenerateRealTest, U64ToDouble_GenerateSignedTag_Bias_Test) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GenerateSignedTag, true>(a, -1);
};
EXPECT_EQ(ToDouble(0x0000000000000000), 0);
EXPECT_EQ(ToDouble(0x0000000000000001), 1.084202172485504434e-19 / 2);
EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), 0.999999999999999888978 / 2);
EXPECT_EQ(ToDouble(0x8000000000000000), 0);
EXPECT_EQ(ToDouble(0x8000000000000001), -1.084202172485504434e-19 / 2);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), -0.999999999999999888978 / 2);
}
TEST(GenerateRealTest, U64ToDoubleTest) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GeneratePositiveTag, true>(a);
};
EXPECT_EQ(ToDouble(0x0000000000000000), 0.0);
EXPECT_EQ(ToDouble(0x0000000000000000), 0.0);
EXPECT_EQ(ToDouble(0x0000000000000001), 5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x7fffffffffffffef), 0.499999999999999944489);
EXPECT_EQ(ToDouble(0x8000000000000000), 0.5);
// For values > 0.5, RandU64ToDouble discards up to 11 bits. (64-53).
EXPECT_EQ(ToDouble(0x8000000000000001), 0.5);
EXPECT_EQ(ToDouble(0x80000000000007FF), 0.5);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), 0.999999999999999888978);
EXPECT_NE(ToDouble(0x7FFFFFFFFFFFF800), ToDouble(0x7FFFFFFFFFFFF7FF));
EXPECT_LT(ToDouble(0xFFFFFFFFFFFFFFFF), 1.0);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFF), ToDouble(0xFFFFFFFFFFFFF800));
EXPECT_NE(ToDouble(0xFFFFFFFFFFFFFFFF), ToDouble(0xFFFFFFFFFFFFF7FF));
EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), ToDouble(0x7FFFFFFFFFFFFC00));
EXPECT_NE(ToDouble(0x7FFFFFFFFFFFFFFF), ToDouble(0x7FFFFFFFFFFFFBFF));
EXPECT_EQ(ToDouble(0x3FFFFFFFFFFFFFFF), ToDouble(0x3FFFFFFFFFFFFE00));
EXPECT_NE(ToDouble(0x3FFFFFFFFFFFFFFF), ToDouble(0x3FFFFFFFFFFFFDFF));
EXPECT_EQ(ToDouble(0x1000000000000001), 0.0625);
EXPECT_EQ(ToDouble(0x2000000000000001), 0.125);
EXPECT_EQ(ToDouble(0x3000000000000001), 0.1875);
EXPECT_EQ(ToDouble(0x4000000000000001), 0.25);
EXPECT_EQ(ToDouble(0x5000000000000001), 0.3125);
EXPECT_EQ(ToDouble(0x6000000000000001), 0.375);
EXPECT_EQ(ToDouble(0x7000000000000001), 0.4375);
EXPECT_EQ(ToDouble(0x8000000000000001), 0.5);
EXPECT_EQ(ToDouble(0x9000000000000001), 0.5625);
EXPECT_EQ(ToDouble(0xa000000000000001), 0.625);
EXPECT_EQ(ToDouble(0xb000000000000001), 0.6875);
EXPECT_EQ(ToDouble(0xc000000000000001), 0.75);
EXPECT_EQ(ToDouble(0xd000000000000001), 0.8125);
EXPECT_EQ(ToDouble(0xe000000000000001), 0.875);
EXPECT_EQ(ToDouble(0xf000000000000001), 0.9375);
// Large powers of 2.
int64_t two_to_53 = int64_t{1} << 53;
EXPECT_EQ(static_cast<int64_t>(ToDouble(0xFFFFFFFFFFFFFFFF) * two_to_53),
two_to_53 - 1);
EXPECT_NE(static_cast<int64_t>(ToDouble(0xFFFFFFFFFFFFFFFF) * two_to_53 * 2),
two_to_53 * 2 - 1);
// For values where every bit counts, the values scale as multiples of the
// input.
for (int i = 0; i < 100; ++i) {
EXPECT_EQ(i * ToDouble(0x0000000000000001), ToDouble(i));
}
// For each i: value generated from (1 << i).
double exp_values[64];
exp_values[63] = 0.5;
for (int i = 62; i >= 0; --i) exp_values[i] = 0.5 * exp_values[i + 1];
constexpr uint64_t one = 1;
for (int i = 0; i < 64; ++i) {
EXPECT_EQ(ToDouble(one << i), exp_values[i]);
for (int j = 1; j < DBL_MANT_DIG && i - j >= 0; ++j) {
EXPECT_NE(exp_values[i] + exp_values[i - j], exp_values[i]);
EXPECT_EQ(ToDouble((one << i) + (one << (i - j))),
exp_values[i] + exp_values[i - j]);
}
for (int j = DBL_MANT_DIG; i - j >= 0; ++j) {
EXPECT_EQ(exp_values[i] + exp_values[i - j], exp_values[i]);
EXPECT_EQ(ToDouble((one << i) + (one << (i - j))), exp_values[i]);
}
}
}
TEST(GenerateRealTest, U64ToDoubleSignedTest) {
auto ToDouble = [](uint64_t a) {
return GenerateRealFromBits<double, GenerateSignedTag, false>(a);
};
EXPECT_EQ(ToDouble(0x0000000000000000), 5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x0000000000000001), 1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0x8000000000000000), -5.42101086242752217004e-20);
EXPECT_EQ(ToDouble(0x8000000000000001), -1.084202172485504434e-19);
const double e_plus = ToDouble(0x0000000000000001);
const double e_minus = ToDouble(0x8000000000000001);
EXPECT_EQ(e_plus, 1.084202172485504434e-19);
EXPECT_EQ(e_minus, -1.084202172485504434e-19);
EXPECT_EQ(ToDouble(0x3fffffffffffffef), 0.499999999999999944489);
EXPECT_EQ(ToDouble(0xbfffffffffffffef), -0.499999999999999944489);
// For values > 0.5, RandU64ToDouble discards up to 10 bits. (63-53).
EXPECT_EQ(ToDouble(0x4000000000000000), 0.5);
EXPECT_EQ(ToDouble(0x4000000000000001), 0.5);
EXPECT_EQ(ToDouble(0x40000000000003FF), 0.5);
EXPECT_EQ(ToDouble(0xC000000000000000), -0.5);
EXPECT_EQ(ToDouble(0xC000000000000001), -0.5);
EXPECT_EQ(ToDouble(0xC0000000000003FF), -0.5);
EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFe), 0.999999999999999888978);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFe), -0.999999999999999888978);
EXPECT_NE(ToDouble(0x7FFFFFFFFFFFF800), ToDouble(0x7FFFFFFFFFFFF7FF));
EXPECT_LT(ToDouble(0x7FFFFFFFFFFFFFFF), 1.0);
EXPECT_GT(ToDouble(0x7FFFFFFFFFFFFFFF), 0.9999999999);
EXPECT_GT(ToDouble(0xFFFFFFFFFFFFFFFe), -1.0);
EXPECT_LT(ToDouble(0xFFFFFFFFFFFFFFFe), -0.999999999);
EXPECT_EQ(ToDouble(0xFFFFFFFFFFFFFFFe), ToDouble(0xFFFFFFFFFFFFFC00));
EXPECT_EQ(ToDouble(0x7FFFFFFFFFFFFFFF), ToDouble(0x7FFFFFFFFFFFFC00));
EXPECT_NE(ToDouble(0xFFFFFFFFFFFFFFFe), ToDouble(0xFFFFFFFFFFFFF3FF));
EXPECT_NE(ToDouble(0x7FFFFFFFFFFFFFFF), ToDouble(0x7FFFFFFFFFFFF3FF));
EXPECT_EQ(ToDouble(0x1000000000000001), 0.125);
EXPECT_EQ(ToDouble(0x2000000000000001), 0.25);
EXPECT_EQ(ToDouble(0x3000000000000001), 0.375);
EXPECT_EQ(ToDouble(0x4000000000000001), 0.5);
EXPECT_EQ(ToDouble(0x5000000000000001), 0.625);
EXPECT_EQ(ToDouble(0x6000000000000001), 0.75);
EXPECT_EQ(ToDouble(0x7000000000000001), 0.875);
EXPECT_EQ(ToDouble(0x7800000000000001), 0.9375);
EXPECT_EQ(ToDouble(0x7c00000000000001), 0.96875);
EXPECT_EQ(ToDouble(0x7e00000000000001), 0.984375);
EXPECT_EQ(ToDouble(0x7f00000000000001), 0.9921875);
// 0x8000000000000000 ~= 0
EXPECT_EQ(ToDouble(0x9000000000000001), -0.125);
EXPECT_EQ(ToDouble(0xa000000000000001), -0.25);
EXPECT_EQ(ToDouble(0xb000000000000001), -0.375);
EXPECT_EQ(ToDouble(0xc000000000000001), -0.5);
EXPECT_EQ(ToDouble(0xd000000000000001), -0.625);
EXPECT_EQ(ToDouble(0xe000000000000001), -0.75);
EXPECT_EQ(ToDouble(0xf000000000000001), -0.875);
// Large powers of 2.
int64_t two_to_53 = int64_t{1} << 53;
EXPECT_EQ(static_cast<int64_t>(ToDouble(0x7FFFFFFFFFFFFFFF) * two_to_53),
two_to_53 - 1);
EXPECT_EQ(static_cast<int64_t>(ToDouble(0xFFFFFFFFFFFFFFFF) * two_to_53),
-(two_to_53 - 1));
EXPECT_NE(static_cast<int64_t>(ToDouble(0x7FFFFFFFFFFFFFFF) * two_to_53 * 2),
two_to_53 * 2 - 1);
// For values where every bit counts, the values scale as multiples of the
// input.
for (int i = 1; i < 100; ++i) {
EXPECT_EQ(i * e_plus, ToDouble(i)) << i;
EXPECT_EQ(i * e_minus, ToDouble(0x8000000000000000 | i)) << i;
}
}
TEST(GenerateRealTest, ExhaustiveFloat) {
auto ToFloat = [](uint64_t a) {
return GenerateRealFromBits<float, GeneratePositiveTag, true>(a);
};
// Rely on RandU64ToFloat generating values from greatest to least when
Export of internal Abseil changes -- a5af5874c1c5cc02bd2a748d455321f82b6f2a93 by Andy Getzendanner <durandal@google.com>: fix compile fails with asan and -Wredundant-decls Import of https://github.com/abseil/abseil-cpp/pull/801 PiperOrigin-RevId: 336693223 -- ed9df42ab2b742386c6692c2bed015374c919d9c by Derek Mauro <dmauro@google.com>: Fix integer conversion warning Fixes #814 PiperOrigin-RevId: 336651814 -- 0ab4c23884e72dce17b67c1eb520f9dbb802565d by Derek Mauro <dmauro@google.com>: Internal change PiperOrigin-RevId: 336585378 -- eba0e3dccd52a6e91bcff84075bef0affc650b74 by Matt Kulukundis <kfm@google.com>: Add bitset operations to Futex helper. PiperOrigin-RevId: 336409368 -- 8b0709a8b4500bf5f0af4b602d76a298d81645e8 by Abseil Team <absl-team@google.com>: Fix code indentation in a comment. PiperOrigin-RevId: 336368167 -- bc3961c87a7e7760c10319a5b0349c279f7ae3ad by Samuel Benzaquen <sbenza@google.com>: Improve performance of the registry: - Reduce contention - Reduce memory usage for each flag by `6*sizeof(void*)`. - Replace one immortal allocation per-flag with a single one for all the flags - Slightly improve single-threaded performance by avoiding the std::map indirections. PiperOrigin-RevId: 336365904 -- 264ad9f28f935aad8b6b1437f8bf804fa9104346 by Abseil Team <absl-team@google.com>: Fix typo in comment on absl::Condition. PiperOrigin-RevId: 336311680 -- b5b808a8c75ca0df7b09eff9a423ec171d80f771 by Derek Mauro <dmauro@google.com>: Add missing Apache license headers PiperOrigin-RevId: 336294980 -- 89446c3a4793df8b95060385cf3e219357c3db1d by Andy Soffer <asoffer@google.com>: Internal changes PiperOrigin-RevId: 336287465 -- 57c8be4e294881bc79a6a44b8e4bf7ecbb19b9b9 by Matt Kulukundis <kfm@google.com>: Extract Futex from an implementation detail of Wait to a private interface. PiperOrigin-RevId: 336123209 GitOrigin-RevId: a5af5874c1c5cc02bd2a748d455321f82b6f2a93 Change-Id: Ie5a0ebe28e571814e3e11d4c05ca308523ccf311
4 years ago
// supplied with uint64_t values from greatest (0xfff...) to least (0x0).
// Thus, this algorithm stores the previous value, and if the new value is at
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
// greater than or equal to the previous value, then there is a collision in
// the generation algorithm.
//
// Use the computation below to convert the random value into a result:
// double res = a() * (1.0f - sample) + b() * sample;
float last_f = 1.0, last_g = 2.0;
uint64_t f_collisions = 0, g_collisions = 0;
uint64_t f_unique = 0, g_unique = 0;
uint64_t total = 0;
auto count = [&](const float r) {
total++;
// `f` is mapped to the range [0, 1) (default)
const float f = 0.0f * (1.0f - r) + 1.0f * r;
if (f >= last_f) {
f_collisions++;
} else {
f_unique++;
last_f = f;
}
// `g` is mapped to the range [1, 2)
const float g = 1.0f * (1.0f - r) + 2.0f * r;
if (g >= last_g) {
g_collisions++;
} else {
g_unique++;
last_g = g;
}
};
size_t limit = absl::GetFlag(FLAGS_absl_random_test_trials);
// Generate all uint64_t which have unique floating point values.
// Counting down from 0xFFFFFFFFFFFFFFFFu ... 0x0u
uint64_t x = ~uint64_t(0);
for (; x != 0 && limit > 0;) {
constexpr int kDig = (64 - FLT_MANT_DIG);
// Set a decrement value & the next point at which to change
// the decrement value. By default these are 1, 0.
uint64_t dec = 1;
uint64_t chk = 0;
// Adjust decrement and check value based on how many leading 0
// bits are set in the current value.
Export of internal Abseil changes -- dab5caab05d89d03066ef92584660688595a3aaf by Mark Barolak <mbar@google.com>: Add absl::Status and absl::StatusOr to absl/README.md Import of https://github.com/abseil/abseil-cpp/pull/863 PiperOrigin-RevId: 347857368 -- 1ca3c7a96417cd6e6d62f4dc36fd5ddaa61cfa20 by Chris Kennelly <ckennelly@google.com>: Leverage integer power-of-2 functions and bit counting library in Abseil. PiperOrigin-RevId: 347816486 -- e5cbe05879fd65dce7875e2e0105331a1615d89b by Chris Kennelly <ckennelly@google.com>: Mitigate narrowing warning on MSVC. If sizeof(x) <= sizeof(uint32_t), no truncation occurs when casting to uint32_t, but the compiler cannot always determine this. PiperOrigin-RevId: 347696526 -- 079dff64cb175d282d9e22dfb4a522199ffdae2e by Benjamin Barenblat <bbaren@google.com>: Avoid libgcc -NaN narrowing bug When testing -NaN parsing, avoid narrowing -NaN from double to float. This avoids a bug in libgcc (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98251). PiperOrigin-RevId: 347654751 -- 2e78a7634865aeef6765e1f447e96cf8d9985059 by Chris Kennelly <ckennelly@google.com>: Mark popcount helpers as inline. These are conditionally constexpr, so we need to add inline to cover the non-constexpr builds to avoid ODR violations. PiperOrigin-RevId: 347620138 -- 437fbb363aea1654179f102dcdd607ec33c1af1e by Chris Kennelly <ckennelly@google.com>: Use explicit narrowing cast. This is never invoked in practice, but compilers with -Wimplicit-int-conversion may trigger when sizeof(T) > sizeof(uint16_t) prior to determining this never runs. PiperOrigin-RevId: 347609857 GitOrigin-RevId: dab5caab05d89d03066ef92584660688595a3aaf Change-Id: I6296ddffe7ec646f8ce121138f21e1e85a2cff4b
4 years ago
const int clz = absl::countl_zero(x);
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
if (clz < kDig) {
dec <<= (kDig - clz);
chk = (~uint64_t(0)) >> (clz + 1);
}
for (; x > chk && limit > 0; x -= dec) {
count(ToFloat(x));
--limit;
}
}
static_assert(FLT_MANT_DIG == 24,
"The float type is expected to have a 24 bit mantissa.");
if (limit != 0) {
// There are between 2^28 and 2^29 unique values in the range [0, 1). For
// the low values of x, there are 2^24 -1 unique values. Once x > 2^24,
// there are 40 * 2^24 unique values. Thus:
// (2 + 4 + 8 ... + 2^23) + 40 * 2^23
EXPECT_LT(1 << 28, f_unique);
EXPECT_EQ((1 << 24) + 40 * (1 << 23) - 1, f_unique);
EXPECT_EQ(total, f_unique);
EXPECT_EQ(0, f_collisions);
// Expect at least 2^23 unique values for the range [1, 2)
EXPECT_LE(1 << 23, g_unique);
EXPECT_EQ(total - g_unique, g_collisions);
}
}
} // namespace