AMDGPU: Fix verifier assert with out of bounds subregister indexes (#119799)
[llvm-project.git] / libcxx / include / __cxx03 / __random / bernoulli_distribution.h
blob3c35e8c39e237fa86c8080a05a6c2b8e9b1c17ce
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H
12 #include <__cxx03/__config>
13 #include <__cxx03/__random/is_valid.h>
14 #include <__cxx03/__random/uniform_real_distribution.h>
15 #include <__cxx03/iosfwd>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 # pragma GCC system_header
19 #endif
21 _LIBCPP_PUSH_MACROS
22 #include <__cxx03/__undef_macros>
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 class _LIBCPP_TEMPLATE_VIS bernoulli_distribution {
27 public:
28 // types
29 typedef bool result_type;
31 class _LIBCPP_TEMPLATE_VIS param_type {
32 double __p_;
34 public:
35 typedef bernoulli_distribution distribution_type;
37 _LIBCPP_HIDE_FROM_ABI explicit param_type(double __p = 0.5) : __p_(__p) {}
39 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_; }
41 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
42 return __x.__p_ == __y.__p_;
44 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
47 private:
48 param_type __p_;
50 public:
51 // constructors and reset functions
52 #ifndef _LIBCPP_CXX03_LANG
53 _LIBCPP_HIDE_FROM_ABI bernoulli_distribution() : bernoulli_distribution(0.5) {}
54 _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p) : __p_(param_type(__p)) {}
55 #else
56 _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(double __p = 0.5) : __p_(param_type(__p)) {}
57 #endif
58 _LIBCPP_HIDE_FROM_ABI explicit bernoulli_distribution(const param_type& __p) : __p_(__p) {}
59 _LIBCPP_HIDE_FROM_ABI void reset() {}
61 // generating functions
62 template <class _URNG>
63 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
64 return (*this)(__g, __p_);
66 template <class _URNG>
67 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
69 // property functions
70 _LIBCPP_HIDE_FROM_ABI double p() const { return __p_.p(); }
72 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
73 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
75 _LIBCPP_HIDE_FROM_ABI result_type min() const { return false; }
76 _LIBCPP_HIDE_FROM_ABI result_type max() const { return true; }
78 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
79 return __x.__p_ == __y.__p_;
81 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const bernoulli_distribution& __x, const bernoulli_distribution& __y) {
82 return !(__x == __y);
86 template <class _URNG>
87 inline bernoulli_distribution::result_type bernoulli_distribution::operator()(_URNG& __g, const param_type& __p) {
88 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
89 uniform_real_distribution<double> __gen;
90 return __gen(__g) < __p.p();
93 template <class _CharT, class _Traits>
94 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
95 operator<<(basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x) {
96 __save_flags<_CharT, _Traits> __lx(__os);
97 typedef basic_ostream<_CharT, _Traits> _OStream;
98 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
99 _CharT __sp = __os.widen(' ');
100 __os.fill(__sp);
101 return __os << __x.p();
104 template <class _CharT, class _Traits>
105 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
106 operator>>(basic_istream<_CharT, _Traits>& __is, bernoulli_distribution& __x) {
107 typedef bernoulli_distribution _Eng;
108 typedef typename _Eng::param_type param_type;
109 __save_flags<_CharT, _Traits> __lx(__is);
110 typedef basic_istream<_CharT, _Traits> _Istream;
111 __is.flags(_Istream::dec | _Istream::skipws);
112 double __p;
113 __is >> __p;
114 if (!__is.fail())
115 __x.param(param_type(__p));
116 return __is;
119 _LIBCPP_END_NAMESPACE_STD
121 _LIBCPP_POP_MACROS
123 #endif // _LIBCPP___CXX03___RANDOM_BERNOULLI_DISTRIBUTION_H