Digest-prvhash64
view release on metacpan or search on metacpan
prvhash_core.h view on Meta::CPAN
/**
* prvhash_core.h version 4.3.4
*
* The inclusion file for the "prvhash_core*" PRVHASH core functions for
* various state variable sizes. Also includes several auxiliary functions and
* macros for endianness-correction.
*
* Description is available at https://github.com/avaneev/prvhash
* E-mail: aleksey.vaneev@gmail.com or info@voxengo.com
*
* License
*
* Copyright (c) 2020-2025 Aleksey Vaneev
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef PRVHASH_CORE_INCLUDED
#define PRVHASH_CORE_INCLUDED
#include <stdint.h>
#include <string.h>
/**
* @def PRVHASH_INIT_COUNT
* @brief Common number of PRVHASH initialization rounds.
*/
#define PRVHASH_INIT_COUNT 5
/**
* @def PRVHASH_GCC_BUILTINS
* @brief Macro that denotes availability of GCC-style built-in functions.
*/
#if defined( __GNUC__ ) || defined( __clang__ ) || \
defined( __IBMC__ ) || defined( __IBMCPP__ ) || defined( __COMPCERT__ )
#define PRVHASH_GCC_BUILTINS
#endif // GCC built-ins check
/**
* @def PRVHASH_INLINE
* @brief Macro to force code inlining.
*/
#if defined( PRVHASH_GCC_BUILTINS )
#define PRVHASH_INLINE inline __attribute__((always_inline))
#elif defined( _MSC_VER )
#define PRVHASH_INLINE inline __forceinline
#else // defined( _MSC_VER )
#define PRVHASH_INLINE inline
#endif // defined( _MSC_VER )
/**
* This function runs a single PRVHASH random number generation round. This
* function can be used both as a hash generator and as a general-purpose
* random number generator. In either case, it is advisable to initially run
* this function 5 times (independent of state variable's size), before using
* its random output, to neutralize any possible oddities of state variables'
* initial values (including zero values). Note that after such
* initialization, any further "strange" or zero values in the hashword array
* do not have any influence over the quality of the output (since they get
* mixed with the Seed that already became uniformly-random).
*
* To generate hashes, the "Seed" and "lcg" variables should be simultaneously
* XORed with the same entropy input, prior to calling this function.
* Additionally, the "Seed" can be XORed with a good-quality uniformly-random
* entropy (including output of another PRVHASH system): this is called
* "daisy-chaining", it does not interfere with hashing.
*
* @param[in,out] Seed0 The current "Seed" value. Can be initialized to any
* value.
* @param[in,out] lcg0 The current "lcg" value. Can be initialized to any
* value.
* @param[in,out] Hash0 Current hash word in a hash word array.
* @return Current random value.
*/
static PRVHASH_INLINE uint64_t prvhash_core64( uint64_t* const Seed0,
uint64_t* const lcg0, uint64_t* const Hash0 )
{
uint64_t Seed = *Seed0; uint64_t lcg = *lcg0; uint64_t Hash = *Hash0;
Seed *= lcg * 2 + 1;
const uint64_t rs = Seed >> 32 | Seed << 32;
Hash += rs + 0xAAAAAAAAAAAAAAAA;
lcg += Seed + 0x5555555555555555;
Seed ^= Hash;
const uint64_t out = lcg ^ rs;
*Seed0 = Seed; *lcg0 = lcg; *Hash0 = Hash;
return( out );
}
static PRVHASH_INLINE uint32_t prvhash_core32( uint32_t* const Seed0,
uint32_t* const lcg0, uint32_t* const Hash0 )
( run in 1.591 second using v1.01-cache-2.11-cpan-f56aa216473 )