|
| 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 | +// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#ifndef _CUDA_STD___RANDOM_NORMAL_DISTRIBUTION_H |
| 11 | +#define _CUDA_STD___RANDOM_NORMAL_DISTRIBUTION_H |
| 12 | + |
| 13 | +#include <cuda/std/detail/__config> |
| 14 | + |
| 15 | +#include <cuda/std/__cmath/logarithms.h> |
| 16 | +#include <cuda/std/__cmath/roots.h> |
| 17 | +#include <cuda/std/__random/is_valid.h> |
| 18 | +#include <cuda/std/__random/uniform_real_distribution.h> |
| 19 | +#include <cuda/std/limits> |
| 20 | + |
| 21 | +#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
| 22 | +# pragma GCC system_header |
| 23 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
| 24 | +# pragma clang system_header |
| 25 | +#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
| 26 | +# pragma system_header |
| 27 | +#endif // no system header |
| 28 | + |
| 29 | +#if !_CCCL_COMPILER(NVRTC) |
| 30 | +# include <ios> |
| 31 | +#endif // !_CCCL_COMPILER(NVRTC) |
| 32 | + |
| 33 | +#include <cuda/std/__cccl/prologue.h> |
| 34 | + |
| 35 | +_CCCL_BEGIN_NAMESPACE_CUDA_STD |
| 36 | + |
| 37 | +template <class _RealType = double> |
| 38 | +class normal_distribution |
| 39 | +{ |
| 40 | + static_assert(__libcpp_random_is_valid_realtype<_RealType>, "RealType must be a supported floating-point type"); |
| 41 | + |
| 42 | +public: |
| 43 | + // types |
| 44 | + using result_type = _RealType; |
| 45 | + |
| 46 | + class param_type |
| 47 | + { |
| 48 | + result_type __mean_; |
| 49 | + result_type __stddev_; |
| 50 | + |
| 51 | + public: |
| 52 | + using distribution_type = normal_distribution; |
| 53 | + |
| 54 | + _CCCL_API constexpr explicit param_type(result_type __mean = 0, result_type __stddev = 1) noexcept |
| 55 | + : __mean_{__mean} |
| 56 | + , __stddev_{__stddev} |
| 57 | + {} |
| 58 | + |
| 59 | + [[nodiscard]] _CCCL_API constexpr result_type mean() const noexcept |
| 60 | + { |
| 61 | + return __mean_; |
| 62 | + } |
| 63 | + [[nodiscard]] _CCCL_API constexpr result_type stddev() const noexcept |
| 64 | + { |
| 65 | + return __stddev_; |
| 66 | + } |
| 67 | + |
| 68 | + [[nodiscard]] _CCCL_API friend constexpr bool operator==(const param_type& __x, const param_type& __y) noexcept |
| 69 | + { |
| 70 | + return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_; |
| 71 | + } |
| 72 | + |
| 73 | +#if _CCCL_STD_VER <= 2017 |
| 74 | + [[nodiscard]] _CCCL_API friend constexpr bool operator!=(const param_type& __x, const param_type& __y) noexcept |
| 75 | + { |
| 76 | + return !(__x == __y); |
| 77 | + } |
| 78 | +#endif // _CCCL_STD_VER <= 2017 |
| 79 | + }; |
| 80 | + |
| 81 | +private: |
| 82 | + param_type __p_{}; |
| 83 | + result_type __v_{}; |
| 84 | + bool __v_hot_{false}; |
| 85 | + |
| 86 | +public: |
| 87 | + _CCCL_API constexpr normal_distribution() noexcept |
| 88 | + : normal_distribution{0} |
| 89 | + {} |
| 90 | + _CCCL_API constexpr explicit normal_distribution(result_type __mean, result_type __stddev = result_type{1}) noexcept |
| 91 | + : __p_{param_type(__mean, __stddev)} |
| 92 | + {} |
| 93 | + _CCCL_API constexpr explicit normal_distribution(const param_type& __p) noexcept |
| 94 | + : __p_{__p} |
| 95 | + {} |
| 96 | + _CCCL_API constexpr void reset() noexcept |
| 97 | + { |
| 98 | + __v_hot_ = false; |
| 99 | + } |
| 100 | + |
| 101 | + // generating functions |
| 102 | + template <class _URng> |
| 103 | + _CCCL_API constexpr result_type operator()(_URng& __g) |
| 104 | + { |
| 105 | + return (*this)(__g, __p_); |
| 106 | + } |
| 107 | + template <class _URng> |
| 108 | + _CCCL_API constexpr result_type operator()(_URng& __g, const param_type& __p) |
| 109 | + { |
| 110 | + static_assert(__cccl_random_is_valid_urng<_URng>, "URng must meet the UniformRandomBitGenerator requirements"); |
| 111 | + result_type __up = 0; |
| 112 | + if (__v_hot_) |
| 113 | + { |
| 114 | + __v_hot_ = false; |
| 115 | + __up = __v_; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + uniform_real_distribution<result_type> __uni(-1, 1); |
| 120 | + result_type __u = __uni(__g); |
| 121 | + result_type __v = __uni(__g); |
| 122 | + result_type __s = __u * __u + __v * __v; |
| 123 | + while (__s > 1 || __s == 0) |
| 124 | + { |
| 125 | + __u = __uni(__g); |
| 126 | + __v = __uni(__g); |
| 127 | + __s = __u * __u + __v * __v; |
| 128 | + } |
| 129 | + const result_type __fp = ::cuda::std::sqrt(-2 * ::cuda::std::log(__s) / __s); |
| 130 | + __v_ = __v * __fp; |
| 131 | + __v_hot_ = true; |
| 132 | + __up = __u * __fp; |
| 133 | + } |
| 134 | + return __up * __p.stddev() + __p.mean(); |
| 135 | + } |
| 136 | + |
| 137 | + // property functions |
| 138 | + [[nodiscard]] _CCCL_API constexpr result_type mean() const noexcept |
| 139 | + { |
| 140 | + return __p_.mean(); |
| 141 | + } |
| 142 | + [[nodiscard]] _CCCL_API constexpr result_type stddev() const noexcept |
| 143 | + { |
| 144 | + return __p_.stddev(); |
| 145 | + } |
| 146 | + |
| 147 | + [[nodiscard]] _CCCL_API constexpr param_type param() const noexcept |
| 148 | + { |
| 149 | + return __p_; |
| 150 | + } |
| 151 | + _CCCL_API constexpr void param(const param_type& __p) noexcept |
| 152 | + { |
| 153 | + __p_ = __p; |
| 154 | + } |
| 155 | + |
| 156 | + [[nodiscard]] _CCCL_API constexpr result_type min() const noexcept |
| 157 | + { |
| 158 | + return -numeric_limits<result_type>::infinity(); |
| 159 | + } |
| 160 | + [[nodiscard]] _CCCL_API constexpr result_type max() const noexcept |
| 161 | + { |
| 162 | + return numeric_limits<result_type>::infinity(); |
| 163 | + } |
| 164 | + |
| 165 | + [[nodiscard]] _CCCL_API friend constexpr bool |
| 166 | + operator==(const normal_distribution& __x, const normal_distribution& __y) noexcept |
| 167 | + { |
| 168 | + return __x.__p_ == __y.__p_ && __x.__v_hot_ == __y.__v_hot_ && (!__x.__v_hot_ || __x.__v_ == __y.__v_); |
| 169 | + } |
| 170 | +#if _CCCL_STD_VER <= 2017 |
| 171 | + [[nodiscard]] _CCCL_API friend constexpr bool |
| 172 | + operator!=(const normal_distribution& __x, const normal_distribution& __y) noexcept |
| 173 | + { |
| 174 | + return !(__x == __y); |
| 175 | + } |
| 176 | +#endif // _CCCL_STD_VER <= 2017 |
| 177 | + |
| 178 | +#if !_CCCL_COMPILER(NVRTC) |
| 179 | + template <class _CharT, class _Traits> |
| 180 | + friend ::std::basic_ostream<_CharT, _Traits>& |
| 181 | + operator<<(::std::basic_ostream<_CharT, _Traits>& __os, const normal_distribution& __x) |
| 182 | + { |
| 183 | + _CharT __sp = __os.widen(' '); |
| 184 | + ::std::ios_base::fmtflags __flags = __os.flags(); |
| 185 | + __os.flags(::std::ios_base::dec | ::std::ios_base::left | ::std::ios_base::fixed); |
| 186 | + _CharT __fill = __os.fill(__sp); |
| 187 | + ::std::streamsize __precision = __os.precision(17); // Max precision for double |
| 188 | + __os << __x.mean() << __sp << __x.stddev() << __sp << __x.__v_hot_; |
| 189 | + if (__x.__v_hot_) |
| 190 | + { |
| 191 | + __os << __sp << __x.__v_; |
| 192 | + } |
| 193 | + __os.precision(__precision); |
| 194 | + __os.fill(__fill); |
| 195 | + __os.flags(__flags); |
| 196 | + return __os; |
| 197 | + } |
| 198 | + |
| 199 | + template <class _CharT, class _Traits> |
| 200 | + friend ::std::basic_istream<_CharT, _Traits>& |
| 201 | + operator>>(::std::basic_istream<_CharT, _Traits>& __is, normal_distribution& __x) |
| 202 | + { |
| 203 | + using _Istream = ::std::basic_istream<_CharT, _Traits>; |
| 204 | + auto __flags = __is.flags(); |
| 205 | + __is.flags(_Istream::skipws); |
| 206 | + result_type __mean; |
| 207 | + result_type __stddev; |
| 208 | + result_type __vp = 0; |
| 209 | + int __v_hot_int = 0; |
| 210 | + __is >> __mean >> __stddev >> __v_hot_int; |
| 211 | + bool __v_hot = __v_hot_int != 0; |
| 212 | + if (__v_hot) |
| 213 | + { |
| 214 | + __is >> __vp; |
| 215 | + } |
| 216 | + if (!__is.fail()) |
| 217 | + { |
| 218 | + __x.param(param_type(__mean, __stddev)); |
| 219 | + __x.__v_hot_ = __v_hot; |
| 220 | + __x.__v_ = __vp; |
| 221 | + } |
| 222 | + __is.flags(__flags); |
| 223 | + return __is; |
| 224 | + } |
| 225 | +#endif // !_CCCL_COMPILER(NVRTC) |
| 226 | +}; |
| 227 | + |
| 228 | +_CCCL_END_NAMESPACE_CUDA_STD |
| 229 | + |
| 230 | +#include <cuda/std/__cccl/epilogue.h> |
| 231 | + |
| 232 | +#endif // _CUDA_STD___RANDOM_NORMAL_DISTRIBUTION_H |
0 commit comments