From 099507726958390adcefb13f563938917b59af94 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Mon, 8 Aug 2022 12:02:26 +0800 Subject: [PATCH 1/5] Improve static constexpr members in --- stl/inc/random | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/stl/inc/random b/stl/inc/random index 5c87135054d..90ffbd0dc6e 100644 --- a/stl/inc/random +++ b/stl/inc/random @@ -790,11 +790,13 @@ protected: typename _Swc_Traits::_Cy_t _Carry; }; +#if !_HAS_CXX17 template const size_t _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>::short_lag; template const size_t _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>::long_lag; +#endif // !_HAS_CXX17 template struct _Swc_traits { // traits for subtract_with_carry generator @@ -1005,6 +1007,18 @@ public: }; #if _HAS_TR1_NAMESPACE +_CONSTEVAL double _Cx_ldexp_one(const int _Exp) noexcept { + double _Ret = 1.0; + for (int _Count = _Exp; _Count > 0; --_Count) { + _Ret *= 2.0; + } + for (int _Count = _Exp; _Count < 0; ++_Count) { + _Ret *= 0.5; + } + + return _Ret; +} + template struct _Swc_01_traits { // traits for subtract_with_carry_01 generator using _Cy_t = _Ty; @@ -1012,9 +1026,10 @@ struct _Swc_01_traits { // traits for subtract_with_carry_01 generator using _Mod_t = _Ty; using _Seed_t = unsigned int; - static const _Cy_t _Cy; - static const _Mod_t _Mod; - static const _Ty _Max; + static constexpr _Cy_t _Cy = static_cast<_Cy_t>( + _Cx_ldexp_one(static_cast(-static_cast(_Wx)))); + static constexpr _Mod_t _Mod = 1; + static constexpr _Ty _Max = 1; static constexpr int _Nwords = (_Wx + 31) / 32; template @@ -1058,23 +1073,23 @@ struct _Swc_01_traits { // traits for subtract_with_carry_01 generator } private: - static const _Ty _Scale1; + static constexpr _Ty _Scale1 = static_cast<_Ty>(_Cx_ldexp_one(_Wx % 32)); static constexpr unsigned long _Mask = ~((~0UL) << (_Wx % 32)); }; +#if _HAS_CXX17 template -const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy_t - _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy = static_cast::_Cy_t>( - _CSTD ldexp(1.0, static_cast(-static_cast(_Wx)))); +const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy_t _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy; template -const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod_t _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod = 1; +const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod_t _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod; template -const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Max = 1; +const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Max; template -const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Scale1 = static_cast<_Ty>(_CSTD ldexp(1.0, _Wx % 32)); +const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Scale1; +#endif // !_HAS_CXX17 template class _DEPRECATE_TR1_NAMESPACE subtract_with_carry_01 @@ -1092,10 +1107,12 @@ public: subtract_with_carry_01(_Gen& _Gx) : _Mybase(_Gx) {} }; +#if !_HAS_CXX17 _STL_DISABLE_DEPRECATED_WARNING template const size_t subtract_with_carry_01<_Ty, _Wx, _Sx, _Rx>::word_size; _STL_RESTORE_DEPRECATED_WARNING +#endif // !_HAS_CXX17 #endif // _HAS_TR1_NAMESPACE @@ -1443,11 +1460,13 @@ private: int _Nx; }; +#if !_HAS_CXX17 template const int discard_block<_Engine, _Px, _Rx>::block_size; template const int discard_block<_Engine, _Px, _Rx>::used_block; +#endif // !_HAS_CXX17 template class discard_block_engine : public discard_block<_Engine, _Px, _Rx> { // discard_block_engine compound engine From b99b48386549e12ed9139088459bddbf211301e0 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Mon, 8 Aug 2022 12:29:20 +0800 Subject: [PATCH 2/5] Why didn't this get clang-formatted... --- stl/inc/random | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stl/inc/random b/stl/inc/random index 90ffbd0dc6e..d06bfae24dd 100644 --- a/stl/inc/random +++ b/stl/inc/random @@ -1026,8 +1026,7 @@ struct _Swc_01_traits { // traits for subtract_with_carry_01 generator using _Mod_t = _Ty; using _Seed_t = unsigned int; - static constexpr _Cy_t _Cy = static_cast<_Cy_t>( - _Cx_ldexp_one(static_cast(-static_cast(_Wx)))); + static constexpr _Cy_t _Cy = static_cast<_Cy_t>(_Cx_ldexp_one(static_cast(-static_cast(_Wx)))); static constexpr _Mod_t _Mod = 1; static constexpr _Ty _Max = 1; static constexpr int _Nwords = (_Wx + 31) / 32; From 546bed78f952b185cec0e40a5064fdaa6d1e239f Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Mon, 8 Aug 2022 13:28:44 +0800 Subject: [PATCH 3/5] A severe typo --- stl/inc/random | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/random b/stl/inc/random index d06bfae24dd..5e6d52725c4 100644 --- a/stl/inc/random +++ b/stl/inc/random @@ -1076,7 +1076,7 @@ private: static constexpr unsigned long _Mask = ~((~0UL) << (_Wx % 32)); }; -#if _HAS_CXX17 +#if !_HAS_CXX17 template const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy_t _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy; From 8be848cea7fc8fe47328fc4fefcf75cd983b0519 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Tue, 9 Aug 2022 09:52:10 +0800 Subject: [PATCH 4/5] Drop the out-of-class definition of static constexpr members even in C++14 mode. --- stl/inc/random | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/stl/inc/random b/stl/inc/random index 5e6d52725c4..dc05e7652fe 100644 --- a/stl/inc/random +++ b/stl/inc/random @@ -790,14 +790,6 @@ protected: typename _Swc_Traits::_Cy_t _Carry; }; -#if !_HAS_CXX17 -template -const size_t _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>::short_lag; - -template -const size_t _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>::long_lag; -#endif // !_HAS_CXX17 - template struct _Swc_traits { // traits for subtract_with_carry generator using _Cy_t = int; @@ -1076,20 +1068,6 @@ private: static constexpr unsigned long _Mask = ~((~0UL) << (_Wx % 32)); }; -#if !_HAS_CXX17 -template -const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy_t _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy; - -template -const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod_t _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod; - -template -const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Max; - -template -const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Scale1; -#endif // !_HAS_CXX17 - template class _DEPRECATE_TR1_NAMESPACE subtract_with_carry_01 : public _Swc_base<_Ty, _Sx, _Rx, _Swc_01_traits<_Ty, _Wx, _Rx>> { // subtract_with_carry_01 generator @@ -1105,14 +1083,6 @@ public: template = 0> subtract_with_carry_01(_Gen& _Gx) : _Mybase(_Gx) {} }; - -#if !_HAS_CXX17 -_STL_DISABLE_DEPRECATED_WARNING -template -const size_t subtract_with_carry_01<_Ty, _Wx, _Sx, _Rx>::word_size; -_STL_RESTORE_DEPRECATED_WARNING -#endif // !_HAS_CXX17 - #endif // _HAS_TR1_NAMESPACE template @@ -1459,14 +1429,6 @@ private: int _Nx; }; -#if !_HAS_CXX17 -template -const int discard_block<_Engine, _Px, _Rx>::block_size; - -template -const int discard_block<_Engine, _Px, _Rx>::used_block; -#endif // !_HAS_CXX17 - template class discard_block_engine : public discard_block<_Engine, _Px, _Rx> { // discard_block_engine compound engine public: From 048d49bea5681ca0bcd392ee0d7c369f9e06bb1f Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Tue, 9 Aug 2022 23:38:23 +0800 Subject: [PATCH 5/5] Change _Cx_ldexp_one to _Cx_exp2 --- stl/inc/random | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stl/inc/random b/stl/inc/random index dc05e7652fe..d31d5fb7a2f 100644 --- a/stl/inc/random +++ b/stl/inc/random @@ -999,7 +999,7 @@ public: }; #if _HAS_TR1_NAMESPACE -_CONSTEVAL double _Cx_ldexp_one(const int _Exp) noexcept { +_CONSTEVAL double _Cx_exp2(const int _Exp) noexcept { double _Ret = 1.0; for (int _Count = _Exp; _Count > 0; --_Count) { _Ret *= 2.0; @@ -1018,7 +1018,7 @@ struct _Swc_01_traits { // traits for subtract_with_carry_01 generator using _Mod_t = _Ty; using _Seed_t = unsigned int; - static constexpr _Cy_t _Cy = static_cast<_Cy_t>(_Cx_ldexp_one(static_cast(-static_cast(_Wx)))); + static constexpr _Cy_t _Cy = static_cast<_Cy_t>(_Cx_exp2(static_cast(-static_cast(_Wx)))); static constexpr _Mod_t _Mod = 1; static constexpr _Ty _Max = 1; static constexpr int _Nwords = (_Wx + 31) / 32; @@ -1064,7 +1064,7 @@ struct _Swc_01_traits { // traits for subtract_with_carry_01 generator } private: - static constexpr _Ty _Scale1 = static_cast<_Ty>(_Cx_ldexp_one(_Wx % 32)); + static constexpr _Ty _Scale1 = static_cast<_Ty>(_Cx_exp2(_Wx % 32)); static constexpr unsigned long _Mask = ~((~0UL) << (_Wx % 32)); };