1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___RANDOM_IS_VALID_H
10 #define _LIBCPP___RANDOM_IS_VALID_H
11 
12 #include <__config>
13 #include <cstdint>
14 #include <type_traits>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #  pragma GCC system_header
18 #endif
19 
20 _LIBCPP_BEGIN_NAMESPACE_STD
21 
22 // [rand.req.genl]/1.5:
23 // The effect of instantiating a template that has a template type parameter
24 // named IntType is undefined unless the corresponding template argument is
25 // cv-unqualified and is one of short, int, long, long long, unsigned short,
26 // unsigned int, unsigned long, or unsigned long long.
27 
28 template<class> struct __libcpp_random_is_valid_inttype : false_type {};
29 template<> struct __libcpp_random_is_valid_inttype<int8_t> : true_type {}; // extension
30 template<> struct __libcpp_random_is_valid_inttype<short> : true_type {};
31 template<> struct __libcpp_random_is_valid_inttype<int> : true_type {};
32 template<> struct __libcpp_random_is_valid_inttype<long> : true_type {};
33 template<> struct __libcpp_random_is_valid_inttype<long long> : true_type {};
34 template<> struct __libcpp_random_is_valid_inttype<uint8_t> : true_type {}; // extension
35 template<> struct __libcpp_random_is_valid_inttype<unsigned short> : true_type {};
36 template<> struct __libcpp_random_is_valid_inttype<unsigned int> : true_type {};
37 template<> struct __libcpp_random_is_valid_inttype<unsigned long> : true_type {};
38 template<> struct __libcpp_random_is_valid_inttype<unsigned long long> : true_type {};
39 
40 #ifndef _LIBCPP_HAS_NO_INT128
41 template<> struct __libcpp_random_is_valid_inttype<__int128_t> : true_type {}; // extension
42 template<> struct __libcpp_random_is_valid_inttype<__uint128_t> : true_type {}; // extension
43 #endif // _LIBCPP_HAS_NO_INT128
44 
45 // [rand.req.urng]/3:
46 // A class G meets the uniform random bit generator requirements if G models
47 // uniform_random_bit_generator, invoke_result_t<G&> is an unsigned integer type,
48 // and G provides a nested typedef-name result_type that denotes the same type
49 // as invoke_result_t<G&>.
50 // (In particular, reject URNGs with signed result_types; our distributions cannot
51 // handle such generator types.)
52 
53 template<class, class = void> struct __libcpp_random_is_valid_urng : false_type {};
54 template<class _Gp> struct __libcpp_random_is_valid_urng<_Gp, __enable_if_t<
55     is_unsigned<typename _Gp::result_type>::value &&
56     _IsSame<decltype(std::declval<_Gp&>()()), typename _Gp::result_type>::value
57 > > : true_type {};
58 
59 _LIBCPP_END_NAMESPACE_STD
60 
61 #endif // _LIBCPP___RANDOM_IS_VALID_H
62