Index: libcxx/include/__bits =================================================================== --- libcxx/include/__bits +++ libcxx/include/__bits @@ -43,6 +43,17 @@ inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); } +#ifndef _LIBCPP_HAS_NO_INT128 + +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR +int __libcpp_clz(__uint128_t __x) _NOEXCEPT { + return ((__x >> 64) == 0) + ? (64 + __libcpp_clz((unsigned long long)(__x & ((unsigned long long)~0)))) + : __libcpp_clz((unsigned long long)(__x >> 64)); +} + +#endif // _LIBCPP_HAS_NO_INT128 + inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); } Index: libcxx/include/__random/log2.h =================================================================== --- libcxx/include/__random/log2.h +++ libcxx/include/__random/log2.h @@ -11,6 +11,7 @@ #include <__config> #include +#include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #pragma GCC system_header @@ -18,30 +19,55 @@ _LIBCPP_BEGIN_NAMESPACE_STD +template +struct __log2_imp; + template -struct __log2_imp +struct __log2_imp { static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp - : __log2_imp<_Xp, _Rp - 1>::value; + : __log2_imp::value; }; template -struct __log2_imp<_Xp, 0> +struct __log2_imp { static const size_t value = 0; }; template -struct __log2_imp<0, _Rp> +struct __log2_imp { static const size_t value = _Rp + 1; }; +#ifndef _LIBCPP_HAS_NO_INT128 + +template <__uint128_t _Xp, size_t _Rp> +struct __log2_imp<__uint128_t, _Xp, _Rp> +{ + static const size_t value = (_Xp >> 64) + ? (64 + __log2_imp> 64), 63>::value) + : __log2_imp::value; +}; + +#endif // _LIBCPP_HAS_NO_INT128 + template struct __log2 { - static const size_t value = __log2_imp<_Xp, - sizeof(_UIntType) * __CHAR_BIT__ - 1>::value; + static const size_t value = __log2_imp< +#ifndef _LIBCPP_HAS_NO_INT128 + typename conditional + < + sizeof(_UIntType) <= sizeof(unsigned long long), + unsigned long long, + __uint128_t + >::type, +#else + unsigned long long, +#endif // _LIBCPP_HAS_NO_INT128 + _Xp, sizeof(_UIntType) * __CHAR_BIT__ - 1>::value; }; _LIBCPP_END_NAMESPACE_STD Index: libcxx/include/__random/uniform_int_distribution.h =================================================================== --- libcxx/include/__random/uniform_int_distribution.h +++ libcxx/include/__random/uniform_int_distribution.h @@ -229,8 +229,8 @@ uniform_int_distribution<_IntType>::operator()(_URNG& __g, const param_type& __p) _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { - typedef typename conditional::type _UIntType; + typedef typename conditional::type>::type _UIntType; const _UIntType _Rp = _UIntType(__p.b()) - _UIntType(__p.a()) + _UIntType(1); if (_Rp == 1) return __p.a(); Index: libcxx/test/std/numerics/rand/rand.dis/rand.dist.uni/rand.dist.uni.int/int128.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/numerics/rand/rand.dis/rand.dist.uni/rand.dist.uni.int/int128.pass.cpp @@ -0,0 +1,86 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: c++03 + +// + +// template +// class uniform_int_distribution + +// template result_type operator()(_URNG& g); + +#include +#include + +#include "test_macros.h" + +int main(int, char**) { + +#ifndef _LIBCPP_HAS_NO_INT128 + + // Test that values outside of the 64-bit range can be produced. + { + auto e = std::default_random_engine{}; + auto d = std::uniform_int_distribution<__int128_t>{}; + assert(d.min() == 0 && d.max() == std::numeric_limits<__int128_t>::max()); + bool all_in_range = true; + for (int i = 0; i < 100; ++i) { + const __int128_t n = d(e); + all_in_range = all_in_range && (n <= UINT64_MAX); + } + assert(!all_in_range); + } + + // Same test as above with min/max set and outside the 64-bit range. + { + const __int128_t a = ((__int128_t)INT64_MIN) * 10; + const __int128_t b = ((__int128_t)INT64_MAX) * 10; + auto e = std::default_random_engine{}; + auto d = std::uniform_int_distribution<__int128_t>{a, b}; + assert(d.min() == a && d.max() == b); + bool all_in_range = true; + for (int i = 0; i < 100; ++i) { + const __int128_t n = d(e); + assert(n >= a && n <= b); + all_in_range = all_in_range && (n >= INT64_MIN) && (n <= (INT64_MAX)); + } + assert(!all_in_range); + } + + // Same test as above with __uint128_t. + { + const __uint128_t a = UINT64_MAX / 3; + const __uint128_t b = ((__uint128_t)UINT64_MAX) * 10; + auto e = std::default_random_engine{}; + auto d = std::uniform_int_distribution<__uint128_t>{a, b}; + assert(d.min() == a && d.max() == b); + bool all_in_range = true; + for (int i = 0; i < 100; ++i) { + const __uint128_t n = d(e); + assert(n >= a && n <= b); + all_in_range = all_in_range && (n <= (UINT64_MAX)); + } + assert(!all_in_range); + } + + // Regression test for PR#51520: + { + auto e = std::default_random_engine{}; + auto d = std::uniform_int_distribution<__int128_t>{INT64_MIN, INT64_MAX}; + assert(d.min() == INT64_MIN && d.max() == INT64_MAX); + for (int i = 0; i < 100; ++i) { + const __int128_t n = d(e); + assert((n >= INT64_MIN) && (n <= INT64_MAX)); + } + } + +#endif // _LIBCPP_HAS_NO_INT128 + + return 0; +}