Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-26 09:34:38

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/random/engine/SplitMix64.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cstdint>
0010 
0011 #include "corecel/Macros.hh"
0012 
0013 namespace celeritas
0014 {
0015 
0016 //---------------------------------------------------------------------------//
0017 /*!
0018  * \brief RNG for initializing the state for other RNGs
0019  *
0020  * See https://prng.di.unimi.it for a details of the SplitMix64 engine
0021  */
0022 class SplitMix64
0023 {
0024   public:
0025     // Construct the SplitMix64 class with the given seed
0026     inline CELER_FUNCTION explicit SplitMix64(std::uint64_t seed);
0027 
0028     // Produce a random number
0029     inline CELER_FUNCTION std::uint64_t operator()();
0030 
0031     // Advance the state forward one sample
0032     inline CELER_FUNCTION void advance();
0033 
0034     // XOR this state with another
0035     inline CELER_FUNCTION void xor_state(std::uint64_t state);
0036 
0037   private:
0038     // SplitMix64 State
0039     std::uint64_t state_;
0040 };
0041 
0042 //---------------------------------------------------------------------------//
0043 // INLINE DEFINITIONS
0044 //---------------------------------------------------------------------------//
0045 /*!
0046  * Construct the SplitMix64 engine with the given seed.
0047  */
0048 CELER_FUNCTION SplitMix64::SplitMix64(std::uint64_t seed) : state_(seed)
0049 {
0050     /* * */
0051 }
0052 
0053 //---------------------------------------------------------------------------//
0054 /*!
0055  * Generate a 64-bit pseudorandom number using the SplitMix64 engine.
0056  *
0057  * See https://prng.di.unimi.it for a description of the method.
0058  */
0059 CELER_FUNCTION std::uint64_t SplitMix64::operator()()
0060 {
0061     this->advance();
0062     std::uint64_t z = state_;
0063     z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;
0064     z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;
0065     return z ^ (z >> 31);
0066 }
0067 
0068 //---------------------------------------------------------------------------//
0069 /*!
0070  * \brief Advance the state forward by one sample
0071  */
0072 CELER_FUNCTION void SplitMix64::advance()
0073 {
0074     state_ += 0x9e3779b97f4a7c15ull;
0075 }
0076 
0077 //---------------------------------------------------------------------------//
0078 /*!
0079  * Perform a XOR operation of this state with another state.
0080  */
0081 CELER_FUNCTION void SplitMix64::xor_state(std::uint64_t state)
0082 {
0083     state_ ^= state;
0084 }
0085 
0086 //---------------------------------------------------------------------------//
0087 }  // namespace celeritas