Skip to content

Commit

Permalink
✨ Make seed(SeedSeq) compatible with the original pcg
Browse files Browse the repository at this point in the history
  • Loading branch information
heavywatal committed Nov 22, 2024
1 parent b1e1c57 commit c6bfc47
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 11 additions & 4 deletions include/pcglite/pcglite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#ifndef PCGLITE_PCGLITE_HPP_
#define PCGLITE_PCGLITE_HPP_

#include <array>
#include <charconv>
#include <cstring>
#include <ios>
Expand Down Expand Up @@ -71,8 +70,15 @@ template <> inline constexpr __uint128_t default_increment<__uint128_t>
template <> inline constexpr __uint128_t default_state<__uint128_t>
= constexpr_uint128(0xb8dc10e158a92392ULL, 0x98046df007ec0a53ULL);

template <class state_type, int N = 2>
union SeedSeqData {
state_type as_state_type[N];
uint32_t as_uint32_t[N * sizeof(state_type) / sizeof(uint32_t)];
};

} // namespace detail


template <class UIntType>
class permuted_congruential_engine {
public:
Expand Down Expand Up @@ -106,9 +112,10 @@ class permuted_congruential_engine {
template <class SeedSeq>
std::enable_if_t<!std::is_convertible_v<SeedSeq, state_type>>
seed(SeedSeq& q) {
std::array<state_type, 2u> data;
q.generate(data.begin(), data.end());
seed(data[0], data[1]);
detail::SeedSeqData<state_type> data{};
constexpr int len = sizeof(data.as_uint32_t) / sizeof(uint32_t);
q.generate(data.as_uint32_t, data.as_uint32_t + len);
seed(data.as_state_type[1], data.as_state_type[0]);
}

result_type operator()() noexcept {
Expand Down
6 changes: 2 additions & 4 deletions test/identical.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,12 @@ int main() {
ret |= ctor2();
ret |= seed1();
ret |= seed2();
ret |= seedseq();
ret |= ctor0_64();
ret |= ctor1_64();
ret |= ctor2_64();
ret |= seed1_64();
ret |= seed2_64();
if (false) {// TODO
ret |= seedseq();
ret |= seedseq_64();
}
ret |= seedseq_64();
return ret;
}

0 comments on commit c6bfc47

Please sign in to comment.