Skip to content

Commit 1208456

Browse files
jwakelyjakubjelinek
andcommitted
libstdc++: Implement std::philox_engine for targets without __int128
This moves the __detail::_Select_uint_least_t<N>::type class to namespace scope and extends it with more 128-bit arithmetic operations, implemented in terms of uint64_t. Now std::philox_engine can use _Select_uint_least_t<w*2>::type instead of __uint128_t, so that it works on targets without 128-bit integers. This also means that targets that do support __uint128_t only use it when actually necessary, so that we use uint64_t when generating a 32-bit result (e.g. with std::philox4x32). libstdc++-v3/ChangeLog: * include/bits/random.h [!__SIZEOF_INT128__] (__rand_uint128): Refactor and rename _Select_uint_least_t<128>::type to a new class. Make all members constexpr. Add new member functions for additional arithmetic and bitwise operations, and comparisons. (__detail::_Select_uint_least_t<>::type): Define as an alias of __rand_uint128. * include/bits/random.tcc (philox_engine::_M_mulhi): Use _Select_uint_least_t<w*2>::type instead of __uint128_t. (philox_engine::_M_transition): Likewise. * include/bits/version.def (philox_engine): Remove extra_cond. * include/bits/version.h: Regenerate. * testsuite/26_numerics/random/philox4x32.cc: Remove dg-require-cpp-feature-test directive. * testsuite/26_numerics/random/philox4x64.cc: Likewise. * testsuite/26_numerics/random/philox_engine/cons/copy.cc: Likewise. * testsuite/26_numerics/random/philox_engine/cons/default.cc: Likewise. * testsuite/26_numerics/random/philox_engine/cons/seed.cc: Likewise. * testsuite/26_numerics/random/philox_engine/operators/equal.cc: Likewise. * testsuite/26_numerics/random/philox_engine/operators/serialize.cc: Likewise. * testsuite/26_numerics/random/philox_engine/requirements/constants.cc: Likewise. * testsuite/26_numerics/random/philox_engine/requirements/typedefs.cc: Likewise. Co-authored-by: Jakub Jelinek <[email protected]> Reviewed-by: Tomasz Kamiński <[email protected]>
1 parent a9eb2f2 commit 1208456

File tree

13 files changed

+440
-127
lines changed

13 files changed

+440
-127
lines changed

libstdc++-v3/include/bits/random.h

Lines changed: 419 additions & 97 deletions
Large diffs are not rendered by default.

libstdc++-v3/include/bits/random.tcc

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -915,9 +915,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
915915
philox_engine<_UIntType, __w, __n, __r, __consts...>::
916916
_S_mulhi(_UIntType __a, _UIntType __b)
917917
{
918-
const __uint128_t __num =
919-
static_cast<__uint128_t>(__a) * static_cast<__uint128_t>(__b);
920-
return static_cast<_UIntType>((__num >> __w) & max());
918+
using __type = typename __detail::_Select_uint_least_t<__w * 2>::type;
919+
const __type __num = static_cast<__type>(__a) * __b;
920+
return static_cast<_UIntType>(__num >> __w) & max();
921921
}
922922

923923
template<typename _UIntType, size_t __w, size_t __n, size_t __r,
@@ -938,33 +938,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
938938
if (_M_i != __n)
939939
return;
940940

941+
using __type = typename __detail::_Select_uint_least_t<__w * 2>::type;
942+
941943
_M_philox();
942944
if constexpr (__n == 4)
943945
{
944-
__uint128_t __uh =
945-
(static_cast<__uint128_t>(_M_x[1]) << __w)
946-
| (static_cast<__uint128_t>(_M_x[0]) + 1);
947-
__uint128_t __lh =
948-
((static_cast<__uint128_t>(_M_x[3]) << __w)
949-
| (_M_x[2]));
950-
__uint128_t __bigMask =
951-
(static_cast<__uint128_t>(1) << ((2 * __w) - 1))
952-
| ((static_cast<__uint128_t>(1) << ((2 * __w) - 1)) - 1);
946+
__type __uh
947+
= (static_cast<__type>(_M_x[1]) << __w)
948+
| (static_cast<__type>(_M_x[0]) + 1);
949+
__type __lh
950+
= (static_cast<__type>(_M_x[3]) << __w)
951+
| static_cast<__type>(_M_x[2]);
952+
__type __bigMask
953+
= ~__type(0) >> ((sizeof(__type) * __CHAR_BIT__) - (__w * 2));
953954
if ((__uh & __bigMask) == 0)
954955
{
955956
++__lh;
956957
__uh = 0;
957958
}
958-
_M_x[0] = __uh & max();
959-
_M_x[1] = (__uh >> (__w)) & max();
960-
_M_x[2] = __lh & max();
961-
_M_x[3] = (__lh >> (__w)) & max();
959+
_M_x[0] = static_cast<_UIntType>(__uh & max());
960+
_M_x[1] = static_cast<_UIntType>((__uh >> (__w)) & max());
961+
_M_x[2] = static_cast<_UIntType>(__lh & max());
962+
_M_x[3] = static_cast<_UIntType>((__lh >> (__w)) & max());
962963
}
963964
else
964965
{
965-
__uint128_t __num =
966-
(static_cast<__uint128_t>(_M_x[1]) << __w)
967-
| (static_cast<__uint128_t>(_M_x[0]) + 1);
966+
__type __num =
967+
(static_cast<__type>(_M_x[1]) << __w)
968+
| (static_cast<__type>(_M_x[0]) + 1);
968969
_M_x[0] = __num & max();
969970
_M_x[1] = (__num >> __w) & max();
970971
}

libstdc++-v3/include/bits/version.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2241,7 +2241,6 @@ ftms = {
22412241
values = {
22422242
v = 202406;
22432243
cxxmin = 26;
2244-
extra_cond = "__SIZEOF_INT128__";
22452244
};
22462245
};
22472246

libstdc++-v3/include/bits/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2507,7 +2507,7 @@
25072507
#undef __glibcxx_want_constexpr_exceptions
25082508

25092509
#if !defined(__cpp_lib_philox_engine)
2510-
# if (__cplusplus > 202302L) && (__SIZEOF_INT128__)
2510+
# if (__cplusplus > 202302L)
25112511
# define __glibcxx_philox_engine 202406L
25122512
# if defined(__glibcxx_want_all) || defined(__glibcxx_want_philox_engine)
25132513
# define __cpp_lib_philox_engine 202406L

libstdc++-v3/testsuite/26_numerics/random/philox4x32.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// { dg-do run { target c++26 } }
2-
// { dg-require-cpp-feature-test __cpp_lib_philox_engine }
32

43
// N5014 29.5.6 Engines and engine adaptors with predefined parameters
54

libstdc++-v3/testsuite/26_numerics/random/philox4x64.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// { dg-do run { target c++26 } }
2-
// { dg-require-cpp-feature-test __cpp_lib_philox_engine }
32

43
// N5014 29.5.6 Engines and engine adaptors with predefined parameters
54

libstdc++-v3/testsuite/26_numerics/random/philox_engine/cons/copy.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// { dg-do run { target c++26 } }
2-
// { dg-require-cpp-feature-test __cpp_lib_philox_engine }
32

43
// N5014 29.5.4 Random Number Engine Class Templates
54
// N5014 29.5.4.5 Class Template philox_engine

libstdc++-v3/testsuite/26_numerics/random/philox_engine/cons/default.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// { dg-do run { target c++26 } }
2-
// { dg-require-cpp-feature-test __cpp_lib_philox_engine }
32

43
// N5014 29.5.4.5 Class Template philox_engine
54

libstdc++-v3/testsuite/26_numerics/random/philox_engine/cons/seed.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// { dg-do run { target c++26 } }
2-
// { dg-require-cpp-feature-test __cpp_lib_philox_engine }
32

43
#include <random>
54
#include <testsuite_hooks.h>

libstdc++-v3/testsuite/26_numerics/random/philox_engine/operators/equal.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// { dg-do run { target c++26 } }
2-
// { dg-require-cpp-feature-test __cpp_lib_philox_engine }
32

43
// N5014 29.5.4.5 Class Template philox_engine
54

0 commit comments

Comments
 (0)