File indexing completed on 2025-02-22 10:31:30
0001
0002
0003
0004
0005
0006
0007
0008 #pragma once
0009
0010 #include "corecel/Types.hh"
0011
0012 namespace celeritas
0013 {
0014 namespace detail
0015 {
0016
0017
0018
0019
0020 template<class RealType = ::celeritas::real_type>
0021 class GenerateCanonical32;
0022
0023 template<>
0024 class GenerateCanonical32<float>
0025 {
0026 public:
0027
0028
0029 using result_type = float;
0030
0031
0032 public:
0033
0034 template<class Generator>
0035 inline CELER_FUNCTION result_type operator()(Generator& rng);
0036 };
0037
0038 template<>
0039 class GenerateCanonical32<double>
0040 {
0041 public:
0042
0043
0044 using result_type = double;
0045
0046
0047 public:
0048
0049 template<class Generator>
0050 inline CELER_FUNCTION result_type operator()(Generator& rng);
0051 };
0052
0053
0054
0055
0056
0057
0058
0059 template<class Generator>
0060 CELER_FUNCTION float GenerateCanonical32<float>::operator()(Generator& rng)
0061 {
0062 static_assert(Generator::max() == 0xffffffffu,
0063 "Generator must return 32-bit sample");
0064
0065 constexpr float norm = 2.32830643654e-10f;
0066 return norm * rng();
0067 }
0068
0069
0070
0071
0072
0073
0074
0075 template<class Generator>
0076 CELER_FUNCTION double GenerateCanonical32<double>::operator()(Generator& rng)
0077 {
0078 static_assert(Generator::max() == 0xffffffffu,
0079 "Generator must return 32-bit sample");
0080 static_assert(sizeof(ull_int) == 8, "Expected 64-bit UL");
0081
0082 unsigned int upper = rng();
0083 unsigned int lower = rng();
0084
0085
0086
0087
0088 constexpr double norm = 1.1102230246251565e-16;
0089 return norm
0090 * static_cast<double>((static_cast<ull_int>(upper) << (53ul - 32ul))
0091 ^ static_cast<ull_int>(lower));
0092 }
0093
0094
0095 }
0096 }