From da591fe6677a17070b8e123f7d450e75e435c788 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sun, 30 Jul 2017 17:58:29 -0700 Subject: [PATCH] Simplify the StateSize helper in Random Summary: [Folly] Simplify the `StateSize` helper in `Random`. * Using member type aliases rather than class constants means we can remove definitions. * Partially specializing over all RNG types with `state_size` class constants means we can remove the `mersenne_twister` specializations, which have many template parameters and are a pain. Reviewed By: Orvid Differential Revision: D5525144 fbshipit-source-id: bc27f112ed0d9b55befe9dabe08c4d345a402435 --- folly/Random-inl.h | 75 +++++++-------------------------------- folly/Random.h | 2 ++ folly/test/RandomTest.cpp | 10 +++--- 3 files changed, 20 insertions(+), 67 deletions(-) diff --git a/folly/Random-inl.h b/folly/Random-inl.h index 405ef1ff..f2f3b4ba 100644 --- a/folly/Random-inl.h +++ b/folly/Random-inl.h @@ -18,8 +18,6 @@ #error This file may only be included from folly/Random.h #endif -#include - namespace folly { namespace detail { @@ -29,82 +27,35 @@ namespace detail { // For some (mersenne_twister_engine), this is exported as a state_size static // data member; for others, the standard shows formulas. -template struct StateSize { +template +struct StateSize { // A sane default. - static constexpr size_t value = 512; + using type = std::integral_constant; }; template -constexpr size_t StateSize::value; +struct StateSize> { + using type = std::integral_constant; +}; template struct StateSize> { // From the standard [rand.eng.lcong], this is ceil(log2(m) / 32) + 3, // which is the same as ceil(ceil(log2(m) / 32) + 3, and // ceil(log2(m)) <= std::numeric_limits::digits - static constexpr size_t value = - (std::numeric_limits::digits + 31) / 32 + 3; -}; - -template -constexpr size_t -StateSize>::value; - -template -struct StateSize> { - static constexpr size_t value = - std::mersenne_twister_engine::state_size; -}; - -template -constexpr size_t -StateSize>::value; - -#if FOLLY_HAVE_EXTRANDOM_SFMT19937 - -template -struct StateSize<__gnu_cxx::simd_fast_mersenne_twister_engine< - UIntType, m, pos1, sl1, sl2, sr1, sr2, msk1, msk2, msk3, msk4, - parity1, parity2, parity3, parity4>> { - static constexpr size_t value = - __gnu_cxx::simd_fast_mersenne_twister_engine< - UIntType, m, pos1, sl1, sl2, sr1, sr2, - msk1, msk2, msk3, msk4, - parity1, parity2, parity3, parity4>::state_size; + using type = std::integral_constant< + size_t, + (std::numeric_limits::digits + 31) / 32 + 3>; }; -template -constexpr size_t -StateSize<__gnu_cxx::simd_fast_mersenne_twister_engine< - UIntType, m, pos1, sl1, sl2, sr1, sr2, msk1, msk2, msk3, msk4, - parity1, parity2, parity3, parity4>>::value; - -#endif - template struct StateSize> { // [rand.eng.sub]: r * ceil(w / 32) - static constexpr size_t value = r * ((w + 31) / 32); + using type = std::integral_constant; }; -template -constexpr size_t -StateSize>::value; +template +using StateSizeT = _t>; template struct SeedData { @@ -112,7 +63,7 @@ struct SeedData { Random::secureRandom(seedData.data(), seedData.size() * sizeof(uint32_t)); } - static constexpr size_t stateSize = StateSize::value; + static constexpr size_t stateSize = StateSizeT::value; std::array seedData; }; diff --git a/folly/Random.h b/folly/Random.h index 0042176b..dbb16bf5 100644 --- a/folly/Random.h +++ b/folly/Random.h @@ -17,11 +17,13 @@ #pragma once #define FOLLY_RANDOM_H_ +#include #include #include #include #include +#include #if FOLLY_HAVE_EXTRANDOM_SFMT19937 #include diff --git a/folly/test/RandomTest.cpp b/folly/test/RandomTest.cpp index 30f99251..d3627575 100644 --- a/folly/test/RandomTest.cpp +++ b/folly/test/RandomTest.cpp @@ -32,13 +32,13 @@ TEST(Random, StateSize) { using namespace folly::detail; // uint_fast32_t is uint64_t on x86_64, w00t - EXPECT_EQ(sizeof(uint_fast32_t) / 4 + 3, - StateSize::value); - EXPECT_EQ(624, StateSize::value); + EXPECT_EQ( + sizeof(uint_fast32_t) / 4 + 3, StateSizeT::value); + EXPECT_EQ(624, StateSizeT::value); #if FOLLY_HAVE_EXTRANDOM_SFMT19937 - EXPECT_EQ(624, StateSize<__gnu_cxx::sfmt19937>::value); + EXPECT_EQ(624, StateSizeT<__gnu_cxx::sfmt19937>::value); #endif - EXPECT_EQ(24, StateSize::value); + EXPECT_EQ(24, StateSizeT::value); } TEST(Random, Simple) { -- 2.34.1