JPL Spatial
Sound spatialization and propagation library
Loading...
Searching...
No Matches
Random.h
Go to the documentation of this file.
1//
2// ██╗██████╗ ██╗ ██╗██████╗ ███████╗
3// ██║██╔══██╗ ██║ ██║██╔══██╗██╔════╝ ** JPL Spatial **
4// ██║██████╔╝ ██║ ██║██████╔╝███████╗
5// ██ ██║██╔═══╝ ██║ ██║██╔══██╗╚════██║ https://github.com/Jaytheway/JPLSpatial
6// ╚█████╔╝██║ ███████╗██║██████╔╝███████║
7// ╚════╝ ╚═╝ ╚══════╝╚═╝╚═════╝ ╚══════╝
8//
9// Copyright 2026 Jaroslav Pevno, JPL Spatial is offered under the terms of the ISC license:
10//
11// Permission to use, copy, modify, and/or distribute this software for any purpose with or
12// without fee is hereby granted, provided that the above copyright notice and this permission
13// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
14// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#pragma once
21
22#include <JPLSpatial/Core.h>
23
24#include <limits>
25
26#if defined(JPL_COMPILER_MSVC) and (JPL_CPU_ADDRESS_BITS == 64)
27#include <intrin.h> // _umul128
28#endif
29
30namespace JPL
31{
32 struct WyRand
33 {
35
37
39 : State(seed)
40 {}
41
42 static constexpr result_type min() { return 0; }
43 static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
44
46 {
47 State += 0xa0761d6478bd642full;
48 return wymum(State, State ^ 0xe7037ed1a0b428dbull);
49 }
50
51 private:
52 static uint64 wymum(uint64 a, uint64 b) noexcept
53 {
54#if defined(JPL_COMPILER_MSVC) and (JPL_CPU_ADDRESS_BITS == 64)
55
56#if defined(JPL_CPU_X86)
57
58 uint64 high = 0;
59 const uint64 low = _umul128(a, b, &high);
60 return high ^ low;
61
62#elif defined(JPL_CPU_ARM)
63
64 const uint64 high = __umulh(a, b);
65 const uint64 low = a * b;
66 return high ^ low;
67
68#else
69#Error "Unsupported CPU architecture."
70#endif
71
72#elif defined(__SIZEOF_INT128__)
73 const __uint128_t p = static_cast<__uint128_t>(a) * b;
74 return static_cast<uint64>(p >> 64) ^ static_cast<uint64>(p);
75
76#else
77 // Portable fallback: 64x64 -> 128 multiply using 32-bit limbs.
78 const uint64 a0 = static_cast<uint32>(a);
79 const uint64 a1 = a >> 32;
80 const uint64 b0 = static_cast<uint32>(b);
81 const uint64 b1 = b >> 32;
82
83 const uint64 p00 = a0 * b0;
84 const uint64 p01 = a0 * b1;
85 const uint64 p10 = a1 * b0;
86 const uint64 p11 = a1 * b1;
87
88 const uint64 middle = (p00 >> 32) + static_cast<uint32>(p01) + static_cast<uint32>(p10);
89
90 const uint64 low =
91 (p00 & 0xffffffffull) | (middle << 32);
92
93 const uint64 high =
94 p11 + (p01 >> 32) + (p10 >> 32) + (middle >> 32);
95
96 return high ^ low;
97#endif
98 }
99 };
100
102 {
104
106
108
109 static constexpr result_type min() { return 0; }
110 static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
111
113 {
114 uint64 z = (State += 0x9e3779b97f4a7c15ull);
115 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull;
116 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull;
117 return z ^ (z >> 31);
118 }
119 };
120
121 using Rand = WyRand;
122} // namespace JPL
Definition AcousticMaterial.h:36
std::uint64_t uint64
Definition Core.h:312
std::uint32_t uint32
Definition Core.h:311
Definition Random.h:102
result_type operator()() noexcept
Definition Random.h:112
uint64 result_type
Definition Random.h:103
uint64 State
Definition Random.h:105
static constexpr result_type max()
Definition Random.h:110
static constexpr result_type min()
Definition Random.h:109
SplitMix64(uint64 seed)
Definition Random.h:107
Definition Random.h:33
static constexpr result_type max()
Definition Random.h:43
static constexpr result_type min()
Definition Random.h:42
WyRand(uint64 seed) noexcept
Definition Random.h:38
result_type operator()() noexcept
Definition Random.h:45
uint64 result_type
Definition Random.h:34
uint64 State
Definition Random.h:36