Antkeeper  0.0.1
pcg.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_MATH_HASH_PCG_HPP
21 #define ANTKEEPER_MATH_HASH_PCG_HPP
22 
23 #include <engine/math/vector.hpp>
25 #include <cstdint>
26 #include <type_traits>
27 
28 namespace math {
29 namespace hash {
30 
32 template <class T>
33 constexpr T pcg_multiplier = 0;
34 template <>
35 constexpr std::uint8_t pcg_multiplier<std::uint8_t> = 141U;
36 template <>
37 constexpr std::uint16_t pcg_multiplier<std::uint16_t> = 12829U;
38 template <>
39 constexpr std::uint32_t pcg_multiplier<std::uint32_t> = 747796405UL;
40 template <>
41 constexpr std::uint64_t pcg_multiplier<std::uint64_t> = 6364136223846793005ULL;
42 
44 template <class T>
45 constexpr T pcg_increment = 0;
46 template <>
47 constexpr std::uint8_t pcg_increment<std::uint8_t> = 77U;
48 template <>
49 constexpr std::uint16_t pcg_increment<std::uint16_t> = 47989U;
50 template <>
51 constexpr std::uint32_t pcg_increment<std::uint32_t> = 2891336453UL;
52 template <>
53 constexpr std::uint64_t pcg_increment<std::uint64_t> = 1442695040888963407ULL;
54 
56 template <class T>
57 constexpr T mcg_multiplier = 0;
58 template <>
59 constexpr std::uint8_t mcg_multiplier<std::uint8_t> = 217U;
60 template <>
61 constexpr std::uint16_t mcg_multiplier<std::uint16_t> = 62169U;
62 template <>
63 constexpr std::uint32_t mcg_multiplier<std::uint32_t> = 277803737UL;
64 template <>
65 constexpr std::uint64_t mcg_multiplier<std::uint64_t> = 12605985483714917081ULL;
66 
68 template <class T>
69 [[nodiscard]] constexpr T pcg_uint(T x) noexcept
70 {
71  static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value);
72  static_assert(sizeof(T) <= 8);
73 
74  x = x * pcg_multiplier<T> + pcg_increment<T>;
75  x = (x ^ (x >> ((x >> ((sizeof(T) * 8) - (sizeof(T) + 1))) + (sizeof(T) + 1)))) * mcg_multiplier<T>;
76  return x ^ (x >> ((sizeof(T) * 16 + 2) / 3));
77 }
78 
80 template <class T>
81 [[nodiscard]] inline constexpr vector<T, 1> pcg_uvec1(vector<T, 1> x) noexcept
82 {
83  static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value);
84  static_assert(sizeof(T) <= 8);
85 
86  x[0] = T(x[0]);
87 
88  return x;
89 }
90 
92 template <class T>
93 [[nodiscard]] constexpr vector<T, 2> pcg_uvec2(vector<T, 2> x) noexcept
94 {
95  static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value);
96  static_assert(sizeof(T) <= 8);
97 
98  x = x * pcg_multiplier<T> + pcg_increment<T>;
99 
100  x[0] += x[1] * pcg_multiplier<T>;
101  x[1] += x[0] * pcg_multiplier<T>;
102 
103  x[0] ^= x[0] >> (sizeof(T) * 4);
104  x[1] ^= x[1] >> (sizeof(T) * 4);
105 
106  x[0] += x[1] * pcg_multiplier<T>;
107  x[1] += x[0] * pcg_multiplier<T>;
108 
109  x[0] ^= x[0] >> (sizeof(T) * 4);
110  x[1] ^= x[1] >> (sizeof(T) * 4);
111 
112  return x;
113 }
114 
116 template <class T>
117 [[nodiscard]] constexpr vector<T, 3> pcg_uvec3(vector<T, 3> x) noexcept
118 {
119  static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value);
120  static_assert(sizeof(T) <= 8);
121 
122  x = x * pcg_multiplier<T> + pcg_increment<T>;
123 
124  x[0] += x[1] * x[2];
125  x[1] += x[2] * x[0];
126  x[2] += x[0] * x[1];
127 
128  x[0] ^= x[0] >> (sizeof(T) * 4);
129  x[1] ^= x[1] >> (sizeof(T) * 4);
130  x[2] ^= x[2] >> (sizeof(T) * 4);
131 
132  x[0] += x[1] * x[2];
133  x[1] += x[2] * x[0];
134  x[2] += x[0] * x[1];
135 
136  return x;
137 }
138 
140 template <class T>
141 [[nodiscard]] constexpr vector<T, 4> pcg_uvec4(vector<T, 4> x) noexcept
142 {
143  static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value);
144  static_assert(sizeof(T) <= 8);
145 
146  x = x * pcg_multiplier<T> + pcg_increment<T>;
147 
148  x[0] += x[1] * x[3];
149  x[1] += x[2] * x[0];
150  x[2] += x[0] * x[1];
151  x[3] += x[1] * x[2];
152 
153  x[0] ^= x[0] >> (sizeof(T) * 4);
154  x[1] ^= x[1] >> (sizeof(T) * 4);
155  x[2] ^= x[2] >> (sizeof(T) * 4);
156  x[3] ^= x[3] >> (sizeof(T) * 4);
157 
158  x[0] += x[1] * x[3];
159  x[1] += x[2] * x[0];
160  x[2] += x[0] * x[1];
161  x[3] += x[1] * x[2];
162 
163  return x;
164 }
165 
181 [[nodiscard]] inline constexpr std::uint8_t pcg(std::uint8_t x) noexcept
182 {
183  return pcg_uint<std::uint8_t>(x);
184 }
185 
186 [[nodiscard]] inline constexpr std::uint16_t pcg(std::uint16_t x) noexcept
187 {
188  return pcg_uint<std::uint16_t>(x);
189 }
190 
191 [[nodiscard]] inline constexpr std::uint32_t pcg(std::uint32_t x) noexcept
192 {
193  return pcg_uint<std::uint32_t>(x);
194 }
195 
196 [[nodiscard]] inline constexpr std::uint64_t pcg(std::uint64_t x) noexcept
197 {
198  return pcg_uint<std::uint64_t>(x);
199 }
200 
201 [[nodiscard]] inline constexpr std::uint8_t pcg(std::int8_t x) noexcept
202 {
203  return pcg_uint<std::uint8_t>(static_cast<std::uint8_t>(x));
204 }
205 
206 [[nodiscard]] inline constexpr std::uint16_t pcg(std::int16_t x) noexcept
207 {
208  return pcg_uint<std::uint16_t>(static_cast<std::uint16_t>(x));
209 }
210 
211 [[nodiscard]] inline constexpr std::uint32_t pcg(std::int32_t x) noexcept
212 {
213  return pcg_uint<std::uint32_t>(static_cast<std::uint32_t>(x));
214 }
215 
216 [[nodiscard]] inline constexpr std::uint64_t pcg(std::int64_t x) noexcept
217 {
218  return pcg_uint<std::uint64_t>(static_cast<std::uint64_t>(x));
219 }
220 
221 [[nodiscard]] inline constexpr std::uint32_t pcg(float x) noexcept
222 {
223  return pcg_uint<std::uint32_t>(static_cast<std::uint32_t>(x));
224 }
225 
226 [[nodiscard]] inline constexpr std::uint64_t pcg(double x) noexcept
227 {
228  return pcg_uint<std::uint64_t>(static_cast<std::uint64_t>(x));
229 }
230 
231 template <class T, std::size_t N>
232 [[nodiscard]] inline constexpr vector<make_uint_t<T>, N> pcg(const vector<T, N>& x) noexcept
233 {
234  static_assert(N > 0 && N < 5, "PCG hash only supports vectors with 1-4 elements.");
235 
236  if constexpr (N == 1)
237  return pcg_uvec1<make_uint_t<T>>(vector<make_uint_t<T>, N>(x));
238  else if constexpr (N == 2)
239  return pcg_uvec2<make_uint_t<T>>(vector<make_uint_t<T>, N>(x));
240  else if constexpr (N == 3)
241  return pcg_uvec3<make_uint_t<T>>(vector<make_uint_t<T>, N>(x));
242  else
243  return pcg_uvec4<make_uint_t<T>>(vector<make_uint_t<T>, N>(x));
244 }
246 
247 } // namespace hash
248 } // namespace math
249 
250 #endif // ANTKEEPER_MATH_HASH_PCG_HPP
Hash functions.
Definition: fnv1a.hpp:29
constexpr std::uint8_t pcg(std::uint8_t x) noexcept
PCG hash function.
Definition: pcg.hpp:181
Mathematical functions and data types.
Definition: angles.hpp:26
n-dimensional vector.
Definition: vector.hpp:44