fast.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "fast.h"
  2. #include <util/stream/input.h>
  3. static inline ui32 FixSeq(ui32 seq1, ui32 seq2) noexcept {
  4. const ui32 mask = (~(ui32)(0)) >> 1;
  5. if ((seq1 & mask) == (seq2 & mask)) {
  6. return ~seq2;
  7. }
  8. return seq2;
  9. }
  10. TFastRng64::TFastRng64(ui64 seed1, ui32 seq1, ui64 seed2, ui32 seq2) noexcept
  11. : R1_(seed1, seq1)
  12. , R2_(seed2, FixSeq(seq1, seq2))
  13. {
  14. }
  15. TFastRng64::TArgs::TArgs(ui64 seed) noexcept {
  16. TReallyFastRng32 rng(seed);
  17. Seed1 = rng.GenRand64();
  18. Seq1 = rng.GenRand();
  19. Seed2 = rng.GenRand64();
  20. Seq2 = rng.GenRand();
  21. }
  22. TFastRng64::TArgs::TArgs(IInputStream& entropy) {
  23. static_assert(sizeof(*this) == 3 * sizeof(ui64), "please, fix me");
  24. entropy.LoadOrFail(this, sizeof(*this));
  25. }
  26. template <class T>
  27. static inline T Read(IInputStream& in) noexcept {
  28. T t = T();
  29. in.LoadOrFail(&t, sizeof(t));
  30. return t;
  31. }
  32. TFastRng32::TFastRng32(IInputStream& entropy)
  33. : TFastRng32(Read<ui64>(entropy), Read<ui32>(entropy))
  34. {
  35. }
  36. TReallyFastRng32::TReallyFastRng32(IInputStream& entropy)
  37. : TReallyFastRng32(Read<ui64>(entropy))
  38. {
  39. }