/*
* Copyright (C) 2023 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see .
*/
#ifndef ANTKEEPER_MATH_HASH_PCG_HPP
#define ANTKEEPER_MATH_HASH_PCG_HPP
#include "math/vector.hpp"
#include "math/hash/make-uint.hpp"
#include
#include
namespace math {
namespace hash {
/// @private
template
constexpr T pcg_multiplier = 0;
template <>
constexpr std::uint8_t pcg_multiplier = 141U;
template <>
constexpr std::uint16_t pcg_multiplier = 12829U;
template <>
constexpr std::uint32_t pcg_multiplier = 747796405UL;
template <>
constexpr std::uint64_t pcg_multiplier = 6364136223846793005ULL;
/// @private
template
constexpr T pcg_increment = 0;
template <>
constexpr std::uint8_t pcg_increment = 77U;
template <>
constexpr std::uint16_t pcg_increment = 47989U;
template <>
constexpr std::uint32_t pcg_increment = 2891336453UL;
template <>
constexpr std::uint64_t pcg_increment = 1442695040888963407ULL;
/// @private
template
constexpr T mcg_multiplier = 0;
template <>
constexpr std::uint8_t mcg_multiplier = 217U;
template <>
constexpr std::uint16_t mcg_multiplier = 62169U;
template <>
constexpr std::uint32_t mcg_multiplier = 277803737UL;
template <>
constexpr std::uint64_t mcg_multiplier = 12605985483714917081ULL;
/// @private
template
[[nodiscard]] constexpr T pcg_uint(T x) noexcept
{
static_assert(std::is_integral::value && std::is_unsigned::value);
static_assert(sizeof(T) <= 8);
x = x * pcg_multiplier + pcg_increment;
x = (x ^ (x >> ((x >> ((sizeof(T) * 8) - (sizeof(T) + 1))) + (sizeof(T) + 1)))) * mcg_multiplier;
return x ^ (x >> ((sizeof(T) * 16 + 2) / 3));
}
/// @private
template
[[nodiscard]] inline constexpr vector pcg_uvec1(vector x) noexcept
{
static_assert(std::is_integral::value && std::is_unsigned::value);
static_assert(sizeof(T) <= 8);
x[0] = T(x[0]);
return x;
}
/// @private
template
[[nodiscard]] constexpr vector pcg_uvec2(vector x) noexcept
{
static_assert(std::is_integral::value && std::is_unsigned::value);
static_assert(sizeof(T) <= 8);
x = x * pcg_multiplier + pcg_increment;
x[0] += x[1] * pcg_multiplier;
x[1] += x[0] * pcg_multiplier;
x[0] ^= x[0] >> (sizeof(T) * 4);
x[1] ^= x[1] >> (sizeof(T) * 4);
x[0] += x[1] * pcg_multiplier;
x[1] += x[0] * pcg_multiplier;
x[0] ^= x[0] >> (sizeof(T) * 4);
x[1] ^= x[1] >> (sizeof(T) * 4);
return x;
}
/// @private
template
[[nodiscard]] constexpr vector pcg_uvec3(vector x) noexcept
{
static_assert(std::is_integral::value && std::is_unsigned::value);
static_assert(sizeof(T) <= 8);
x = x * pcg_multiplier + pcg_increment;
x[0] += x[1] * x[2];
x[1] += x[2] * x[0];
x[2] += x[0] * x[1];
x[0] ^= x[0] >> (sizeof(T) * 4);
x[1] ^= x[1] >> (sizeof(T) * 4);
x[2] ^= x[2] >> (sizeof(T) * 4);
x[0] += x[1] * x[2];
x[1] += x[2] * x[0];
x[2] += x[0] * x[1];
return x;
}
/// @private
template
[[nodiscard]] constexpr vector pcg_uvec4(vector x) noexcept
{
static_assert(std::is_integral::value && std::is_unsigned::value);
static_assert(sizeof(T) <= 8);
x = x * pcg_multiplier + pcg_increment;
x[0] += x[1] * x[3];
x[1] += x[2] * x[0];
x[2] += x[0] * x[1];
x[3] += x[1] * x[2];
x[0] ^= x[0] >> (sizeof(T) * 4);
x[1] ^= x[1] >> (sizeof(T) * 4);
x[2] ^= x[2] >> (sizeof(T) * 4);
x[3] ^= x[3] >> (sizeof(T) * 4);
x[0] += x[1] * x[3];
x[1] += x[2] * x[0];
x[2] += x[0] * x[1];
x[3] += x[1] * x[2];
return x;
}
/**
* PCG hash function.
*
* @param x Input value.
*
* @return Unsigned pseudorandom output value.
*
* @warning Floating point and signed input values will be converted to unsigned integers via `static_cast`.
* @warning Vectors with more than 4 elements are not supported.
*
* @see https://en.wikipedia.org/wiki/Permuted_congruential_generator
* @see O'Neill, M.E. (2014). PCG : A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation.
* @see Mark Jarzynski and Marc Olano, Hash Functions for GPU Rendering, Journal of Computer Graphics Techniques (JCGT), vol. 9, no. 3, 21-38, 2020.
*/
/// @{
[[nodiscard]] inline constexpr std::uint8_t pcg(std::uint8_t x) noexcept
{
return pcg_uint(x);
}
[[nodiscard]] inline constexpr std::uint16_t pcg(std::uint16_t x) noexcept
{
return pcg_uint(x);
}
[[nodiscard]] inline constexpr std::uint32_t pcg(std::uint32_t x) noexcept
{
return pcg_uint(x);
}
[[nodiscard]] inline constexpr std::uint64_t pcg(std::uint64_t x) noexcept
{
return pcg_uint(x);
}
[[nodiscard]] inline constexpr std::uint8_t pcg(std::int8_t x) noexcept
{
return pcg_uint(static_cast(x));
}
[[nodiscard]] inline constexpr std::uint16_t pcg(std::int16_t x) noexcept
{
return pcg_uint(static_cast(x));
}
[[nodiscard]] inline constexpr std::uint32_t pcg(std::int32_t x) noexcept
{
return pcg_uint(static_cast(x));
}
[[nodiscard]] inline constexpr std::uint64_t pcg(std::int64_t x) noexcept
{
return pcg_uint(static_cast(x));
}
[[nodiscard]] inline constexpr std::uint32_t pcg(float x) noexcept
{
return pcg_uint(static_cast(x));
}
[[nodiscard]] inline constexpr std::uint64_t pcg(double x) noexcept
{
return pcg_uint(static_cast(x));
}
template
[[nodiscard]] inline constexpr vector, N> pcg(const vector& x) noexcept
{
static_assert(N > 0 && N < 5, "PCG hash only supports vectors with 1-4 elements.");
if constexpr (N == 1)
return pcg_uvec1>(vector, N>(x));
else if constexpr (N == 2)
return pcg_uvec2>(vector, N>(x));
else if constexpr (N == 3)
return pcg_uvec3>(vector, N>(x));
else
return pcg_uvec4>(vector, N>(x));
}
/// @}
} // namespace hash
} // namespace math
#endif // ANTKEEPER_MATH_HASH_PCG_HPP