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/InitializeRngState.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "RanluxppRngEngine.hh"
0010 #include "SplitMix64.hh"
0011 #include "XorwowRngEngine.hh"
0012 
0013 namespace celeritas
0014 {
0015 //---------------------------------------------------------------------------//
0016 /*!
0017  * Fill a XorwowRngEngine state initializer given a seed, event id, and primary
0018  * id.
0019  */
0020 CELER_FUNCTION inline void initialize_rng_state(
0021     XorwowSeed seed,
0022     unsigned int event_id,
0023     unsigned int primary_id,
0024     XorwowRngEngine::RngStateInitializer_t& rng_init)
0025 {
0026     // Seed and setup SplitMix64 with the seed, event_id, and primary_id
0027     // incorporated into the state
0028     SplitMix64 rng(seed[0]);
0029     for (unsigned int v : {event_id, primary_id})
0030     {
0031         rng.advance();
0032         rng.xor_state(v);
0033     }
0034 
0035     // Fill initial two states
0036     std::uint64_t val = rng();
0037     rng_init.xorstate[0] = static_cast<XorwowUInt>(val);
0038     rng_init.xorstate[1] = static_cast<XorwowUInt>(val >> 32);
0039 
0040     // Update SplitMix64 state by xor-ing in event_id and draw another number
0041     // for the next two state values
0042     rng.xor_state(event_id);
0043     val = rng();
0044     rng_init.xorstate[2] = static_cast<XorwowUInt>(val);
0045     rng_init.xorstate[3] = static_cast<XorwowUInt>(val >> 32);
0046 
0047     // Update SplitMix64 state by xor-ing in primary id and update the final
0048     // two numbers
0049     rng.xor_state(primary_id);
0050     val = rng();
0051     rng_init.xorstate[4] = static_cast<XorwowUInt>(val);
0052     rng_init.weylstate = static_cast<XorwowUInt>(val >> 32);
0053 }
0054 
0055 //---------------------------------------------------------------------------//
0056 /*!
0057  * Fill a Ranluxpp state initializer given a seed, event id, and primary id
0058  */
0059 CELER_FUNCTION inline void initialize_rng_state(
0060     RanluxppUInt seed,
0061     unsigned int event_id,
0062     unsigned int primary_id,
0063     RanluxppRngEngine::RngStateInitializer_t& rng_init)
0064 {
0065     // Seed and setup SplitMix64 with the seed, event_id, and primary_id
0066     // incorporated into the state
0067     SplitMix64 rng(seed);
0068     for (unsigned int v : {event_id, primary_id})
0069     {
0070         rng.advance();
0071         rng.xor_state(v);
0072     }
0073 
0074     // Loop and fill the values in the Ranluxpp state
0075     for (unsigned int i : celeritas::range(9))
0076     {
0077         rng_init.value.number[i] = rng();
0078         rng.xor_state(i % 2 == 0 ? event_id : primary_id);
0079     }
0080 }
0081 
0082 //---------------------------------------------------------------------------//
0083 }  // namespace celeritas