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 { + unsigned long long _Upper = __x >> 64; + unsigned long long _Lower = __x & ((unsigned long long)~0); + return (_Upper == 0) ? (64 + __libcpp_clz(_Lower)) : __libcpp_clz(_Upper); +} + +#endif + inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); } Index: libcxx/include/__random/uniform_int_distribution.h =================================================================== --- libcxx/include/__random/uniform_int_distribution.h +++ libcxx/include/__random/uniform_int_distribution.h @@ -28,30 +28,55 @@ // __independent_bits_engine +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 + 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 + _Xp, sizeof(_UIntType) * __CHAR_BIT__ - 1>::value; }; template @@ -257,7 +282,18 @@ _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK { typedef typename conditional::type _UIntType; + uint32_t, +#ifndef _LIBCPP_HAS_NO_INT128 + typename conditional + < + sizeof(result_type) == sizeof(uint64_t), + uint64_t, + __uint128_t + >::type +#else + uint64_t +#endif + >::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,50 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +// 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>{}; + bool all_in_range = true; + for (auto i = 0; i < 100; ++i) { + const auto n = d(e); + 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}; + for (auto i = 0; i < 100; ++i) { + const auto n = d(e); + assert((n >= INT64_MIN) && (n <= INT64_MAX)); + } + } + +#endif + + return 0; +}