Fixed the 64-bit hash function and added unit test (#129)
The 64-bit hash function was not well tested and had a bad optimization. Switched to using rapidhash: https://github.com/Nicoshev/rapidhash
This commit is contained in:
parent
f42be210a2
commit
30c67b5e6d
11 changed files with 1239 additions and 50 deletions
|
|
@ -1968,7 +1968,7 @@ typedef struct b3HullFace
|
|||
} b3HullFace;
|
||||
|
||||
/// 64-bit hull version. Useful for validating serialized data.
|
||||
#define B3_HULL_VERSION 0xE3BD07365000417Cull
|
||||
#define B3_HULL_VERSION 0x4A4C9587DE57485Cull
|
||||
|
||||
/// A convex hull.
|
||||
/// @note This data structure has data hanging off the end and cannot be directly copied.
|
||||
|
|
@ -2097,7 +2097,7 @@ typedef struct b3MeshDef
|
|||
} b3MeshDef;
|
||||
|
||||
/// 64-bit mesh version. Useful for validating serialized data.
|
||||
#define B3_MESH_VERSION 0xA661B13E6655156Full
|
||||
#define B3_MESH_VERSION 0xAAAB9A00F1A8AAF7ull
|
||||
|
||||
/// Triangle mesh edge flags.
|
||||
typedef enum b3MeshEdgeFlags
|
||||
|
|
@ -2278,7 +2278,7 @@ typedef struct b3HeightFieldDef
|
|||
#define B3_HEIGHT_FIELD_HOLE 0xFF
|
||||
|
||||
/// 64-bit height-field version. Useful for validating serialized data.
|
||||
#define B3_HEIGHT_FIELD_VERSION 0x9FEE70DB5A860835ull
|
||||
#define B3_HEIGHT_FIELD_VERSION 0x8E41E5FB084848F8ull
|
||||
|
||||
/// A height field with compressed storage.
|
||||
/// @note This data structure has data hanging off the end and cannot be directly copied.
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
// Ref-counted GPU buffer store for triangle geometries (convex hulls,
|
||||
// triangle meshes, heightfields). Owned by the renderer
|
||||
// Keyed by uint32_t content hash for Box3D shapes we
|
||||
// Keyed by uint64_t content hash for Box3D shapes we
|
||||
// pass through b3HullData.hash / b3MeshData.hash / b3HeightFieldData.hash
|
||||
//
|
||||
// Lifecycle:
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ set(BOX3D_SOURCE_FILES
|
|||
platform.h
|
||||
prismatic_joint.c
|
||||
qsort.h
|
||||
rapidhash.h
|
||||
recording.c
|
||||
recording.h
|
||||
recording_ops.inl
|
||||
|
|
|
|||
32
src/core.c
32
src/core.c
|
|
@ -11,6 +11,7 @@
|
|||
#endif
|
||||
|
||||
#include "core.h"
|
||||
#include "rapidhash.h"
|
||||
|
||||
#include "box3d/constants.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
|
@ -254,38 +255,13 @@ void b3StrCpy( char* dst, int size, const char* src )
|
|||
}
|
||||
}
|
||||
|
||||
// Fowler/Noll/Vo FNV-1a salted by length, then the splitmix64 finalizer so tiny inputs
|
||||
// still spread across all bits.
|
||||
uint64_t b3Hash64NonZero( const uint8_t* bytes, int n )
|
||||
{
|
||||
uint64_t h = 0xcbf29ce484222325ull ^ (uint64_t)(uint32_t)n;
|
||||
const uint64_t prime = 0x100000001b3ull;
|
||||
int i = 0;
|
||||
|
||||
while ( i + 8 <= n )
|
||||
if ( n <= 0 )
|
||||
{
|
||||
uint64_t word;
|
||||
memcpy( &word, bytes + i, sizeof( word ) );
|
||||
#if defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
||||
word = ( ( word & 0x00000000000000FFULL ) << 56 ) | ( ( word & 0x000000000000FF00ULL ) << 40 ) |
|
||||
( ( word & 0x0000000000FF0000ULL ) << 24 ) | ( ( word & 0x00000000FF000000ULL ) << 8 ) |
|
||||
( ( word & 0x000000FF00000000ULL ) >> 8 ) | ( ( word & 0x0000FF0000000000ULL ) >> 24 ) |
|
||||
( ( word & 0x00FF000000000000ULL ) >> 40 ) | ( ( word & 0xFF00000000000000ULL ) >> 56 );
|
||||
#endif
|
||||
h = ( h ^ word ) * prime;
|
||||
i += 8;
|
||||
return 1;
|
||||
}
|
||||
|
||||
while ( i < n )
|
||||
{
|
||||
h = ( h ^ (uint64_t)bytes[i] ) * prime;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
h ^= h >> 30;
|
||||
h *= 0xbf58476d1ce4e5b9ull;
|
||||
h ^= h >> 27;
|
||||
h *= 0x94d049bb133111ebull;
|
||||
h ^= h >> 31;
|
||||
uint64_t h = rapidhash( bytes, n );
|
||||
return h == 0 ? 1 : h;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -160,5 +160,4 @@ void b3JoinThread( b3Thread* t );
|
|||
|
||||
void b3StrCpy( char* dst, int size, const char* src );
|
||||
|
||||
// Good quality 64-bit hash that never returns 0.
|
||||
uint64_t b3Hash64NonZero( const uint8_t* bytes, int n );
|
||||
|
|
|
|||
568
src/rapidhash.h
Normal file
568
src/rapidhash.h
Normal file
|
|
@ -0,0 +1,568 @@
|
|||
/*
|
||||
* rapidhash V3 - Very fast, high quality, platform-independent hashing algorithm.
|
||||
*
|
||||
* Based on 'wyhash', by Wang Yi <godspeed_china@yeah.net>
|
||||
*
|
||||
* Copyright (C) 2025 Nicolas De Carli
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You can contact the author at:
|
||||
* - rapidhash source repository: https://github.com/Nicoshev/rapidhash
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
* Includes.
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#if defined(_MSC_VER)
|
||||
# include <intrin.h>
|
||||
# if defined(_M_X64) && !defined(_M_ARM64EC)
|
||||
# pragma intrinsic(_umul128)
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* C/C++ macros.
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define RAPIDHASH_ALWAYS_INLINE __forceinline
|
||||
#elif defined(__GNUC__)
|
||||
# define RAPIDHASH_ALWAYS_INLINE inline __attribute__((__always_inline__))
|
||||
#else
|
||||
# define RAPIDHASH_ALWAYS_INLINE inline
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
# define RAPIDHASH_NOEXCEPT noexcept
|
||||
# define RAPIDHASH_CONSTEXPR constexpr
|
||||
# ifndef RAPIDHASH_INLINE
|
||||
# define RAPIDHASH_INLINE RAPIDHASH_ALWAYS_INLINE
|
||||
# endif
|
||||
# if __cplusplus >= 201402L && !defined(_MSC_VER)
|
||||
# define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_ALWAYS_INLINE constexpr
|
||||
# else
|
||||
# define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_ALWAYS_INLINE
|
||||
# endif
|
||||
#else
|
||||
# define RAPIDHASH_NOEXCEPT
|
||||
# define RAPIDHASH_CONSTEXPR static const
|
||||
# ifndef RAPIDHASH_INLINE
|
||||
# define RAPIDHASH_INLINE static RAPIDHASH_ALWAYS_INLINE
|
||||
# endif
|
||||
# define RAPIDHASH_INLINE_CONSTEXPR RAPIDHASH_INLINE
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Unrolled macro.
|
||||
* Improves large input speed, but increases code size and worsens small input speed.
|
||||
*
|
||||
* RAPIDHASH_COMPACT: Normal behavior.
|
||||
* RAPIDHASH_UNROLLED:
|
||||
*
|
||||
*/
|
||||
#ifndef RAPIDHASH_UNROLLED
|
||||
# define RAPIDHASH_COMPACT
|
||||
#elif defined(RAPIDHASH_COMPACT)
|
||||
# error "cannot define RAPIDHASH_COMPACT and RAPIDHASH_UNROLLED simultaneously."
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Protection macro, alters behaviour of rapid_mum multiplication function.
|
||||
*
|
||||
* RAPIDHASH_FAST: Normal behavior, max speed.
|
||||
* RAPIDHASH_PROTECTED: Extra protection against entropy loss.
|
||||
*/
|
||||
#ifndef RAPIDHASH_PROTECTED
|
||||
# define RAPIDHASH_FAST
|
||||
#elif defined(RAPIDHASH_FAST)
|
||||
# error "cannot define RAPIDHASH_PROTECTED and RAPIDHASH_FAST simultaneously."
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Likely and unlikely macros.
|
||||
*/
|
||||
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
|
||||
# define _likely_(x) __builtin_expect(x,1)
|
||||
# define _unlikely_(x) __builtin_expect(x,0)
|
||||
#else
|
||||
# define _likely_(x) (x)
|
||||
# define _unlikely_(x) (x)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Endianness macros.
|
||||
*/
|
||||
#ifndef RAPIDHASH_LITTLE_ENDIAN
|
||||
# if defined(_WIN32) || defined(__LITTLE_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
|
||||
# define RAPIDHASH_LITTLE_ENDIAN
|
||||
# elif defined(__BIG_ENDIAN__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
|
||||
# define RAPIDHASH_BIG_ENDIAN
|
||||
# else
|
||||
# warning "could not determine endianness! Falling back to little endian."
|
||||
# define RAPIDHASH_LITTLE_ENDIAN
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Default secret parameters.
|
||||
*/
|
||||
RAPIDHASH_CONSTEXPR uint64_t rapid_secret[8] = {
|
||||
0x2d358dccaa6c78a5ull,
|
||||
0x8bb84b93962eacc9ull,
|
||||
0x4b33a62ed433d4a3ull,
|
||||
0x4d5a2da51de1aa47ull,
|
||||
0xa0761d6478bd642full,
|
||||
0xe7037ed1a0b428dbull,
|
||||
0x90ed1765281c388cull,
|
||||
0xaaaaaaaaaaaaaaaaull};
|
||||
|
||||
/*
|
||||
* 64*64 -> 128bit multiply function.
|
||||
*
|
||||
* @param A Address of 64-bit number.
|
||||
* @param B Address of 64-bit number.
|
||||
*
|
||||
* Calculates 128-bit C = *A * *B.
|
||||
*
|
||||
* When RAPIDHASH_FAST is defined:
|
||||
* Overwrites A contents with C's low 64 bits.
|
||||
* Overwrites B contents with C's high 64 bits.
|
||||
*
|
||||
* When RAPIDHASH_PROTECTED is defined:
|
||||
* Xors and overwrites A contents with C's low 64 bits.
|
||||
* Xors and overwrites B contents with C's high 64 bits.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR void rapid_mum(uint64_t *A, uint64_t *B) RAPIDHASH_NOEXCEPT {
|
||||
#if defined(__SIZEOF_INT128__)
|
||||
__uint128_t r=*A; r*=*B;
|
||||
#ifdef RAPIDHASH_PROTECTED
|
||||
*A^=(uint64_t)r; *B^=(uint64_t)(r>>64);
|
||||
#else
|
||||
*A=(uint64_t)r; *B=(uint64_t)(r>>64);
|
||||
#endif
|
||||
#elif defined(_MSC_VER) && (defined(_WIN64) || defined(_M_HYBRID_CHPE_ARM64))
|
||||
#if defined(_M_X64)
|
||||
#ifdef RAPIDHASH_PROTECTED
|
||||
uint64_t a, b;
|
||||
a=_umul128(*A,*B,&b);
|
||||
*A^=a; *B^=b;
|
||||
#else
|
||||
*A=_umul128(*A,*B,B);
|
||||
#endif
|
||||
#else
|
||||
#ifdef RAPIDHASH_PROTECTED
|
||||
uint64_t a, b;
|
||||
b = __umulh(*A, *B);
|
||||
a = *A * *B;
|
||||
*A^=a; *B^=b;
|
||||
#else
|
||||
uint64_t c = __umulh(*A, *B);
|
||||
*A = *A * *B;
|
||||
*B = c;
|
||||
#endif
|
||||
#endif
|
||||
#else
|
||||
uint64_t ha=*A>>32, hb=*B>>32, la=(uint32_t)*A, lb=(uint32_t)*B;
|
||||
uint64_t rh=ha*hb, rm0=ha*lb, rm1=hb*la, rl=la*lb, t=rl+(rm0<<32), c=t<rl;
|
||||
uint64_t lo=t+(rm1<<32);
|
||||
c+=lo<t;
|
||||
uint64_t hi=rh+(rm0>>32)+(rm1>>32)+c;
|
||||
#ifdef RAPIDHASH_PROTECTED
|
||||
*A^=lo; *B^=hi;
|
||||
#else
|
||||
*A=lo; *B=hi;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Multiply and xor mix function.
|
||||
*
|
||||
* @param A 64-bit number.
|
||||
* @param B 64-bit number.
|
||||
*
|
||||
* Calculates 128-bit C = A * B.
|
||||
* Returns 64-bit xor between high and low 64 bits of C.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapid_mix(uint64_t A, uint64_t B) RAPIDHASH_NOEXCEPT { rapid_mum(&A,&B); return A^B; }
|
||||
|
||||
/*
|
||||
* Read functions.
|
||||
*/
|
||||
#ifdef RAPIDHASH_LITTLE_ENDIAN
|
||||
RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return v;}
|
||||
RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return v;}
|
||||
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
|
||||
RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return __builtin_bswap64(v);}
|
||||
RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return __builtin_bswap32(v);}
|
||||
#elif defined(_MSC_VER)
|
||||
RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint64_t v; memcpy(&v, p, sizeof(uint64_t)); return _byteswap_uint64(v);}
|
||||
RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT { uint32_t v; memcpy(&v, p, sizeof(uint32_t)); return _byteswap_ulong(v);}
|
||||
#else
|
||||
RAPIDHASH_INLINE uint64_t rapid_read64(const uint8_t *p) RAPIDHASH_NOEXCEPT {
|
||||
uint64_t v; memcpy(&v, p, 8);
|
||||
return (((v >> 56) & 0xff)| ((v >> 40) & 0xff00)| ((v >> 24) & 0xff0000)| ((v >> 8) & 0xff000000)| ((v << 8) & 0xff00000000)| ((v << 24) & 0xff0000000000)| ((v << 40) & 0xff000000000000)| ((v << 56) & 0xff00000000000000));
|
||||
}
|
||||
RAPIDHASH_INLINE uint64_t rapid_read32(const uint8_t *p) RAPIDHASH_NOEXCEPT {
|
||||
uint32_t v; memcpy(&v, p, 4);
|
||||
return (((v >> 24) & 0xff)| ((v >> 8) & 0xff00)| ((v << 8) & 0xff0000)| ((v << 24) & 0xff000000));
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* rapidhash main function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
* @param seed 64-bit seed used to alter the hash result predictably.
|
||||
* @param secret Triplet of 64-bit secrets used to alter hash result predictably.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhash_internal(const void *key, size_t len, uint64_t seed, const uint64_t* secret) RAPIDHASH_NOEXCEPT {
|
||||
const uint8_t *p=(const uint8_t *)key;
|
||||
seed ^= rapid_mix(seed ^ secret[2], secret[1]);
|
||||
uint64_t a=0, b=0;
|
||||
size_t i = len;
|
||||
if (_likely_(len <= 16)) {
|
||||
if (len >= 4) {
|
||||
seed ^= len;
|
||||
if (len >= 8) {
|
||||
const uint8_t* plast = p + len - 8;
|
||||
a = rapid_read64(p);
|
||||
b = rapid_read64(plast);
|
||||
} else {
|
||||
const uint8_t* plast = p + len - 4;
|
||||
a = rapid_read32(p);
|
||||
b = rapid_read32(plast);
|
||||
}
|
||||
} else if (len > 0) {
|
||||
a = (((uint64_t)p[0])<<45)|p[len-1];
|
||||
b = p[len>>1];
|
||||
} else
|
||||
a = b = 0;
|
||||
} else {
|
||||
if (len > 112) {
|
||||
uint64_t see1 = seed, see2 = seed;
|
||||
uint64_t see3 = seed, see4 = seed;
|
||||
uint64_t see5 = seed, see6 = seed;
|
||||
#ifdef RAPIDHASH_COMPACT
|
||||
do {
|
||||
seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed);
|
||||
see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1);
|
||||
see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2);
|
||||
see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], rapid_read64(p + 56) ^ see3);
|
||||
see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], rapid_read64(p + 72) ^ see4);
|
||||
see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], rapid_read64(p + 88) ^ see5);
|
||||
see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], rapid_read64(p + 104) ^ see6);
|
||||
p += 112;
|
||||
i -= 112;
|
||||
} while(i > 112);
|
||||
#else
|
||||
while (i > 224) {
|
||||
seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed);
|
||||
see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1);
|
||||
see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2);
|
||||
see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], rapid_read64(p + 56) ^ see3);
|
||||
see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], rapid_read64(p + 72) ^ see4);
|
||||
see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], rapid_read64(p + 88) ^ see5);
|
||||
see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], rapid_read64(p + 104) ^ see6);
|
||||
seed = rapid_mix(rapid_read64(p + 112) ^ secret[0], rapid_read64(p + 120) ^ seed);
|
||||
see1 = rapid_mix(rapid_read64(p + 128) ^ secret[1], rapid_read64(p + 136) ^ see1);
|
||||
see2 = rapid_mix(rapid_read64(p + 144) ^ secret[2], rapid_read64(p + 152) ^ see2);
|
||||
see3 = rapid_mix(rapid_read64(p + 160) ^ secret[3], rapid_read64(p + 168) ^ see3);
|
||||
see4 = rapid_mix(rapid_read64(p + 176) ^ secret[4], rapid_read64(p + 184) ^ see4);
|
||||
see5 = rapid_mix(rapid_read64(p + 192) ^ secret[5], rapid_read64(p + 200) ^ see5);
|
||||
see6 = rapid_mix(rapid_read64(p + 208) ^ secret[6], rapid_read64(p + 216) ^ see6);
|
||||
p += 224;
|
||||
i -= 224;
|
||||
}
|
||||
if (i > 112) {
|
||||
seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed);
|
||||
see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1);
|
||||
see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2);
|
||||
see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], rapid_read64(p + 56) ^ see3);
|
||||
see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], rapid_read64(p + 72) ^ see4);
|
||||
see5 = rapid_mix(rapid_read64(p + 80) ^ secret[5], rapid_read64(p + 88) ^ see5);
|
||||
see6 = rapid_mix(rapid_read64(p + 96) ^ secret[6], rapid_read64(p + 104) ^ see6);
|
||||
p += 112;
|
||||
i -= 112;
|
||||
}
|
||||
#endif
|
||||
seed ^= see1;
|
||||
see2 ^= see3;
|
||||
see4 ^= see5;
|
||||
seed ^= see6;
|
||||
see2 ^= see4;
|
||||
seed ^= see2;
|
||||
}
|
||||
if (i > 16) {
|
||||
seed = rapid_mix(rapid_read64(p) ^ secret[2], rapid_read64(p + 8) ^ seed);
|
||||
if (i > 32) {
|
||||
seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], rapid_read64(p + 24) ^ seed);
|
||||
if (i > 48) {
|
||||
seed = rapid_mix(rapid_read64(p + 32) ^ secret[1], rapid_read64(p + 40) ^ seed);
|
||||
if (i > 64) {
|
||||
seed = rapid_mix(rapid_read64(p + 48) ^ secret[1], rapid_read64(p + 56) ^ seed);
|
||||
if (i > 80) {
|
||||
seed = rapid_mix(rapid_read64(p + 64) ^ secret[2], rapid_read64(p + 72) ^ seed);
|
||||
if (i > 96) {
|
||||
seed = rapid_mix(rapid_read64(p + 80) ^ secret[1], rapid_read64(p + 88) ^ seed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
a=rapid_read64(p+i-16) ^ i; b=rapid_read64(p+i-8);
|
||||
}
|
||||
a ^= secret[1];
|
||||
b ^= seed;
|
||||
rapid_mum(&a, &b);
|
||||
return rapid_mix(a ^ secret[7], b ^ secret[1] ^ i);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhashMicro main function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
* @param seed 64-bit seed used to alter the hash result predictably.
|
||||
* @param secret Triplet of 64-bit secrets used to alter hash result predictably.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashMicro_internal(const void *key, size_t len, uint64_t seed, const uint64_t* secret) RAPIDHASH_NOEXCEPT {
|
||||
const uint8_t *p=(const uint8_t *)key;
|
||||
seed ^= rapid_mix(seed ^ secret[2], secret[1]);
|
||||
uint64_t a=0, b=0;
|
||||
size_t i = len;
|
||||
if (_likely_(len <= 16)) {
|
||||
if (len >= 4) {
|
||||
seed ^= len;
|
||||
if (len >= 8) {
|
||||
const uint8_t* plast = p + len - 8;
|
||||
a = rapid_read64(p);
|
||||
b = rapid_read64(plast);
|
||||
} else {
|
||||
const uint8_t* plast = p + len - 4;
|
||||
a = rapid_read32(p);
|
||||
b = rapid_read32(plast);
|
||||
}
|
||||
} else if (len > 0) {
|
||||
a = (((uint64_t)p[0])<<45)|p[len-1];
|
||||
b = p[len>>1];
|
||||
} else
|
||||
a = b = 0;
|
||||
} else {
|
||||
if (i > 80) {
|
||||
uint64_t see1 = seed, see2 = seed;
|
||||
uint64_t see3 = seed, see4 = seed;
|
||||
do {
|
||||
seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed);
|
||||
see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1);
|
||||
see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2);
|
||||
see3 = rapid_mix(rapid_read64(p + 48) ^ secret[3], rapid_read64(p + 56) ^ see3);
|
||||
see4 = rapid_mix(rapid_read64(p + 64) ^ secret[4], rapid_read64(p + 72) ^ see4);
|
||||
p += 80;
|
||||
i -= 80;
|
||||
} while(i > 80);
|
||||
seed ^= see1;
|
||||
see2 ^= see3;
|
||||
seed ^= see4;
|
||||
seed ^= see2;
|
||||
}
|
||||
if (i > 16) {
|
||||
seed = rapid_mix(rapid_read64(p) ^ secret[2], rapid_read64(p + 8) ^ seed);
|
||||
if (i > 32) {
|
||||
seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], rapid_read64(p + 24) ^ seed);
|
||||
if (i > 48) {
|
||||
seed = rapid_mix(rapid_read64(p + 32) ^ secret[1], rapid_read64(p + 40) ^ seed);
|
||||
if (i > 64) {
|
||||
seed = rapid_mix(rapid_read64(p + 48) ^ secret[1], rapid_read64(p + 56) ^ seed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
a=rapid_read64(p+i-16) ^ i; b=rapid_read64(p+i-8);
|
||||
}
|
||||
a ^= secret[1];
|
||||
b ^= seed;
|
||||
rapid_mum(&a, &b);
|
||||
return rapid_mix(a ^ secret[7], b ^ secret[1] ^ i);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhashNano main function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
* @param seed 64-bit seed used to alter the hash result predictably.
|
||||
* @param secret Triplet of 64-bit secrets used to alter hash result predictably.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashNano_internal(const void *key, size_t len, uint64_t seed, const uint64_t* secret) RAPIDHASH_NOEXCEPT {
|
||||
const uint8_t *p=(const uint8_t *)key;
|
||||
seed ^= rapid_mix(seed ^ secret[2], secret[1]);
|
||||
uint64_t a=0, b=0;
|
||||
size_t i = len;
|
||||
if (_likely_(len <= 16)) {
|
||||
if (len >= 4) {
|
||||
seed ^= len;
|
||||
if (len >= 8) {
|
||||
const uint8_t* plast = p + len - 8;
|
||||
a = rapid_read64(p);
|
||||
b = rapid_read64(plast);
|
||||
} else {
|
||||
const uint8_t* plast = p + len - 4;
|
||||
a = rapid_read32(p);
|
||||
b = rapid_read32(plast);
|
||||
}
|
||||
} else if (len > 0) {
|
||||
a = (((uint64_t)p[0])<<45)|p[len-1];
|
||||
b = p[len>>1];
|
||||
} else
|
||||
a = b = 0;
|
||||
} else {
|
||||
if (i > 48) {
|
||||
uint64_t see1 = seed, see2 = seed;
|
||||
do {
|
||||
seed = rapid_mix(rapid_read64(p) ^ secret[0], rapid_read64(p + 8) ^ seed);
|
||||
see1 = rapid_mix(rapid_read64(p + 16) ^ secret[1], rapid_read64(p + 24) ^ see1);
|
||||
see2 = rapid_mix(rapid_read64(p + 32) ^ secret[2], rapid_read64(p + 40) ^ see2);
|
||||
p += 48;
|
||||
i -= 48;
|
||||
} while(i > 48);
|
||||
seed ^= see1;
|
||||
seed ^= see2;
|
||||
}
|
||||
if (i > 16) {
|
||||
seed = rapid_mix(rapid_read64(p) ^ secret[2], rapid_read64(p + 8) ^ seed);
|
||||
if (i > 32) {
|
||||
seed = rapid_mix(rapid_read64(p + 16) ^ secret[2], rapid_read64(p + 24) ^ seed);
|
||||
}
|
||||
}
|
||||
a=rapid_read64(p+i-16) ^ i; b=rapid_read64(p+i-8);
|
||||
}
|
||||
a ^= secret[1];
|
||||
b ^= seed;
|
||||
rapid_mum(&a, &b);
|
||||
return rapid_mix(a ^ secret[7], b ^ secret[1] ^ i);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhash seeded hash function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
* @param seed 64-bit seed used to alter the hash result predictably.
|
||||
*
|
||||
* Calls rapidhash_internal using provided parameters and default secrets.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhash_withSeed(const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT {
|
||||
return rapidhash_internal(key, len, seed, rapid_secret);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhash general purpose hash function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
*
|
||||
* Calls rapidhash_withSeed using provided parameters and the default seed.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhash(const void *key, size_t len) RAPIDHASH_NOEXCEPT {
|
||||
return rapidhash_withSeed(key, len, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhashMicro seeded hash function.
|
||||
*
|
||||
* Designed for HPC and server applications, where cache misses make a noticeable performance detriment.
|
||||
* Clang-18+ compiles it to ~140 instructions without stack usage, both on x86-64 and aarch64.
|
||||
* Faster for sizes up to 512 bytes, just 15%-20% slower for inputs above 1kb.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
* @param seed 64-bit seed used to alter the hash result predictably.
|
||||
*
|
||||
* Calls rapidhash_internal using provided parameters and default secrets.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashMicro_withSeed(const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT {
|
||||
return rapidhashMicro_internal(key, len, seed, rapid_secret);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhashMicro hash function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
*
|
||||
* Calls rapidhash_withSeed using provided parameters and the default seed.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashMicro(const void *key, size_t len) RAPIDHASH_NOEXCEPT {
|
||||
return rapidhashMicro_withSeed(key, len, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhashNano seeded hash function.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
* @param seed 64-bit seed used to alter the hash result predictably.
|
||||
*
|
||||
* Calls rapidhash_internal using provided parameters and default secrets.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashNano_withSeed(const void *key, size_t len, uint64_t seed) RAPIDHASH_NOEXCEPT {
|
||||
return rapidhashNano_internal(key, len, seed, rapid_secret);
|
||||
}
|
||||
|
||||
/*
|
||||
* rapidhashNano hash function.
|
||||
*
|
||||
* Designed for Mobile and embedded applications, where keeping a small code size is a top priority.
|
||||
* Clang-18+ compiles it to less than 100 instructions without stack usage, both on x86-64 and aarch64.
|
||||
* The fastest for sizes up to 48 bytes, but may be considerably slower for larger inputs.
|
||||
*
|
||||
* @param key Buffer to be hashed.
|
||||
* @param len @key length, in bytes.
|
||||
*
|
||||
* Calls rapidhash_withSeed using provided parameters and the default seed.
|
||||
*
|
||||
* Returns a 64-bit hash.
|
||||
*/
|
||||
RAPIDHASH_INLINE_CONSTEXPR uint64_t rapidhashNano(const void *key, size_t len) RAPIDHASH_NOEXCEPT {
|
||||
return rapidhashNano_withSeed(key, len, 0);
|
||||
}
|
||||
42
src/shape.c
42
src/shape.c
|
|
@ -352,7 +352,14 @@ b3ShapeId b3CreateCapsuleShape( b3BodyId bodyId, const b3ShapeDef* def, const b3
|
|||
b3ShapeId b3CreateHullShape( b3BodyId bodyId, const b3ShapeDef* def, const b3HullData* hull )
|
||||
{
|
||||
B3_VALIDATE( b3IsValidHull( hull ) );
|
||||
B3_VALIDATE( hull->hash != 0 );
|
||||
B3_ASSERT( hull->version == B3_HULL_VERSION );
|
||||
B3_ASSERT( hull->hash != 0 );
|
||||
|
||||
if ( hull->version != B3_HULL_VERSION )
|
||||
{
|
||||
return b3_nullShapeId;
|
||||
}
|
||||
|
||||
b3ShapeId shapeId = b3CreateShape( bodyId, def, hull, b3_hullShape, b3Transform_identity, b3Vec3_one, false );
|
||||
if ( shapeId.index1 != 0 )
|
||||
{
|
||||
|
|
@ -371,6 +378,14 @@ b3ShapeId b3CreateTransformedHullShape( b3BodyId bodyId, const b3ShapeDef* def,
|
|||
b3Vec3 scale )
|
||||
{
|
||||
B3_VALIDATE( b3IsValidHull( hull ) );
|
||||
B3_ASSERT( hull->version == B3_HULL_VERSION );
|
||||
B3_ASSERT( hull->hash != 0 );
|
||||
|
||||
if ( hull->version != B3_HULL_VERSION )
|
||||
{
|
||||
return b3_nullShapeId;
|
||||
}
|
||||
|
||||
b3ShapeId shapeId = b3CreateShape( bodyId, def, hull, b3_hullShape, transform, scale, true );
|
||||
if ( shapeId.index1 != 0 )
|
||||
{
|
||||
|
|
@ -392,7 +407,14 @@ b3ShapeId b3CreateTransformedHullShape( b3BodyId bodyId, const b3ShapeDef* def,
|
|||
b3ShapeId b3CreateMeshShape( b3BodyId bodyId, const b3ShapeDef* def, const b3MeshData* mesh, b3Vec3 scale )
|
||||
{
|
||||
B3_VALIDATE( b3IsValidMesh( mesh ) );
|
||||
B3_VALIDATE( mesh->hash != 0 );
|
||||
B3_ASSERT( mesh->version == B3_MESH_VERSION );
|
||||
B3_ASSERT( mesh->hash != 0 );
|
||||
|
||||
if ( mesh->version != B3_MESH_VERSION )
|
||||
{
|
||||
return b3_nullShapeId;
|
||||
}
|
||||
|
||||
b3ShapeId shapeId = b3CreateShape( bodyId, def, mesh, b3_meshShape, b3Transform_identity, scale, true );
|
||||
if ( shapeId.index1 != 0 )
|
||||
{
|
||||
|
|
@ -409,7 +431,14 @@ b3ShapeId b3CreateMeshShape( b3BodyId bodyId, const b3ShapeDef* def, const b3Mes
|
|||
|
||||
b3ShapeId b3CreateHeightFieldShape( b3BodyId bodyId, const b3ShapeDef* def, const b3HeightFieldData* heightField )
|
||||
{
|
||||
B3_VALIDATE( heightField->hash != 0 );
|
||||
B3_ASSERT( heightField->version == B3_HEIGHT_FIELD_VERSION );
|
||||
B3_ASSERT( heightField->hash != 0 );
|
||||
|
||||
if ( heightField->version != B3_HEIGHT_FIELD_VERSION )
|
||||
{
|
||||
return b3_nullShapeId;
|
||||
}
|
||||
|
||||
b3ShapeId shapeId = b3CreateShape( bodyId, def, heightField, b3_heightShape, b3Transform_identity, b3Vec3_one, false );
|
||||
if ( shapeId.index1 != 0 )
|
||||
{
|
||||
|
|
@ -426,6 +455,13 @@ b3ShapeId b3CreateHeightFieldShape( b3BodyId bodyId, const b3ShapeDef* def, cons
|
|||
|
||||
b3ShapeId b3CreateBakedCompoundShape( b3BodyId bodyId, b3ShapeDef* def, const b3CompoundData* compound )
|
||||
{
|
||||
B3_ASSERT( compound->version == B3_COMPOUND_VERSION );
|
||||
|
||||
if ( compound->version != B3_COMPOUND_VERSION )
|
||||
{
|
||||
return b3_nullShapeId;
|
||||
}
|
||||
|
||||
b3ShapeId shapeId = b3CreateShape( bodyId, def, compound, b3_compoundShape, b3Transform_identity, b3Vec3_one, false );
|
||||
if ( shapeId.index1 != 0 )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ set(BOX3D_TEST_FILES
|
|||
test_container.c
|
||||
test_determinism.c
|
||||
test_distance.c
|
||||
test_hash.c
|
||||
test_height_field.c
|
||||
test_hull.c
|
||||
test_id.c
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ extern int CompoundTest( void );
|
|||
extern int ContainerTest( void );
|
||||
extern int DeterminismTest( void );
|
||||
extern int DistanceTest( void );
|
||||
extern int HashTest( void );
|
||||
extern int HeightFieldTest( void );
|
||||
extern int HullTest( void );
|
||||
extern int IdTest( void );
|
||||
|
|
@ -107,6 +108,7 @@ int main( int argc, char** argv )
|
|||
MAYBE_RUN_TEST( ContainerTest );
|
||||
MAYBE_RUN_TEST( DeterminismTest );
|
||||
MAYBE_RUN_TEST( DistanceTest );
|
||||
MAYBE_RUN_TEST( HashTest );
|
||||
MAYBE_RUN_TEST( HeightFieldTest );
|
||||
MAYBE_RUN_TEST( HullTest );
|
||||
MAYBE_RUN_TEST( IdTest );
|
||||
|
|
|
|||
620
test/test_hash.c
Normal file
620
test/test_hash.c
Normal file
|
|
@ -0,0 +1,620 @@
|
|||
// SPDX-FileCopyrightText: 2026 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "core.h"
|
||||
#include "hull.h"
|
||||
#include "physics_world.h"
|
||||
#include "table.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
#include "box3d/box3d.h"
|
||||
#include "box3d/collision.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
// Separates the two things a repeated hash can mean. Identical bytes repeating is normal, since
|
||||
// distinct generator parameters can still bake to the same geometry. Distinct bytes sharing a hash
|
||||
// is the failure this file exists to catch, so the invariant under test is that different content
|
||||
// gets different digests, never that different parameters do.
|
||||
//
|
||||
// Zero doubles as "no collision", since the hash reserves that value. Results are read back only
|
||||
// after teardown so a failing test does not also dump a leak and bury which condition broke.
|
||||
typedef struct HashEntry
|
||||
{
|
||||
uint64_t hash;
|
||||
int byteCount;
|
||||
uint8_t* bytes;
|
||||
} HashEntry;
|
||||
|
||||
typedef struct HashProbe
|
||||
{
|
||||
b3HashSet set;
|
||||
HashEntry* entries;
|
||||
int count;
|
||||
int capacity;
|
||||
uint64_t collision;
|
||||
int duplicates;
|
||||
bool sawZero;
|
||||
bool overflow;
|
||||
} HashProbe;
|
||||
|
||||
typedef struct HashResult
|
||||
{
|
||||
uint64_t collision;
|
||||
int count;
|
||||
int duplicates;
|
||||
bool sawZero;
|
||||
bool overflow;
|
||||
} HashResult;
|
||||
|
||||
static HashProbe ProbeBegin( int capacity )
|
||||
{
|
||||
HashProbe probe = { 0 };
|
||||
probe.set = b3CreateSet( capacity );
|
||||
probe.entries = B3_ALLOC( HashEntry, capacity );
|
||||
probe.capacity = capacity;
|
||||
return probe;
|
||||
}
|
||||
|
||||
// Pass NULL bytes when the caller builds provably distinct inputs, so any repeat is a collision by
|
||||
// construction and there is nothing to compare against.
|
||||
static void ProbeAdd( HashProbe* probe, uint64_t hash, const void* bytes, int byteCount )
|
||||
{
|
||||
if ( hash == 0 )
|
||||
{
|
||||
probe->sawZero = true;
|
||||
}
|
||||
|
||||
if ( probe->count == probe->capacity )
|
||||
{
|
||||
probe->overflow = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( b3AddKey( &probe->set, hash ) )
|
||||
{
|
||||
bool sameContent = false;
|
||||
if ( bytes != NULL )
|
||||
{
|
||||
for ( int i = 0; i < probe->count; ++i )
|
||||
{
|
||||
if ( probe->entries[i].hash != hash )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
sameContent = probe->entries[i].byteCount == byteCount &&
|
||||
memcmp( probe->entries[i].bytes, bytes, (size_t)byteCount ) == 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( sameContent )
|
||||
{
|
||||
probe->duplicates += 1;
|
||||
}
|
||||
else if ( probe->collision == 0 )
|
||||
{
|
||||
probe->collision = hash;
|
||||
}
|
||||
}
|
||||
|
||||
HashEntry* entry = probe->entries + probe->count;
|
||||
entry->hash = hash;
|
||||
entry->byteCount = 0;
|
||||
entry->bytes = NULL;
|
||||
|
||||
if ( bytes != NULL && byteCount > 0 )
|
||||
{
|
||||
entry->byteCount = byteCount;
|
||||
entry->bytes = B3_ALLOC( uint8_t, byteCount );
|
||||
memcpy( entry->bytes, bytes, (size_t)byteCount );
|
||||
}
|
||||
|
||||
probe->count += 1;
|
||||
}
|
||||
|
||||
static HashResult ProbeEnd( HashProbe* probe )
|
||||
{
|
||||
HashResult result = { probe->collision, probe->count, probe->duplicates, probe->sawZero, probe->overflow };
|
||||
|
||||
for ( int i = 0; i < probe->count; ++i )
|
||||
{
|
||||
if ( probe->entries[i].bytes != NULL )
|
||||
{
|
||||
B3_FREE( probe->entries[i].bytes, uint8_t, probe->entries[i].byteCount );
|
||||
}
|
||||
}
|
||||
|
||||
B3_FREE( probe->entries, HashEntry, probe->capacity );
|
||||
b3DestroySet( &probe->set );
|
||||
return result;
|
||||
}
|
||||
|
||||
// Pins that the upper bytes of each 8 byte word reach the low bits of the hash. Multiply carries
|
||||
// bits upward only, so a hash that keeps just the low half of its product leaves the low bits blind
|
||||
// to those bytes, and blobs differing only up there collapse onto a tiny range no finalizer can
|
||||
// spread back out. Varying the top byte of two words is the sharp case. A pairwise avalanche check
|
||||
// cannot catch this, since a finalizer scatters any single difference across all bits no matter how
|
||||
// weak the mixing behind it.
|
||||
static int HashWordFamily( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
blobSize = 16,
|
||||
familySize = 256 * 256
|
||||
};
|
||||
|
||||
HashProbe probe = ProbeBegin( familySize );
|
||||
|
||||
uint8_t blob[blobSize];
|
||||
for ( int i = 0; i < blobSize; ++i )
|
||||
{
|
||||
blob[i] = (uint8_t)( 0x5A + i );
|
||||
}
|
||||
|
||||
for ( int a = 0; a < 256; ++a )
|
||||
{
|
||||
for ( int b = 0; b < 256; ++b )
|
||||
{
|
||||
blob[7] = (uint8_t)a;
|
||||
blob[15] = (uint8_t)b;
|
||||
ProbeAdd( &probe, b3Hash64NonZero( blob, blobSize ), NULL, 0 );
|
||||
}
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count == familySize );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Every input bit must move the digest, at every offset. Flipping one byte in one position is a
|
||||
// weak check: a mixer can pass it while still being blind to whole regions of a longer blob.
|
||||
static int HashBitSweep( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
blobSize = 32,
|
||||
bitCount = 8 * blobSize
|
||||
};
|
||||
|
||||
HashProbe probe = ProbeBegin( bitCount + 1 );
|
||||
|
||||
uint8_t blob[blobSize];
|
||||
for ( int i = 0; i < blobSize; ++i )
|
||||
{
|
||||
blob[i] = (uint8_t)( 0xA5 ^ ( i * 17 ) );
|
||||
}
|
||||
|
||||
ProbeAdd( &probe, b3Hash64NonZero( blob, blobSize ), NULL, 0 );
|
||||
|
||||
for ( int bit = 0; bit < bitCount; ++bit )
|
||||
{
|
||||
uint8_t mask = (uint8_t)( 1u << ( bit & 7 ) );
|
||||
blob[bit >> 3] ^= mask;
|
||||
ProbeAdd( &probe, b3Hash64NonZero( blob, blobSize ), NULL, 0 );
|
||||
blob[bit >> 3] ^= mask;
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count == bitCount + 1 );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Baked blobs carry explicit padding and unused array slots, so long runs of zeros are common and
|
||||
// content alone cannot separate them. Length has to reach the digest.
|
||||
static int HashZeroLengths( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
maxLength = 4096
|
||||
};
|
||||
|
||||
uint8_t* zeros = B3_ALLOC( uint8_t, maxLength );
|
||||
memset( zeros, 0, maxLength );
|
||||
|
||||
HashProbe probe = ProbeBegin( maxLength );
|
||||
for ( int n = 0; n < maxLength; ++n )
|
||||
{
|
||||
ProbeAdd( &probe, b3Hash64NonZero( zeros, n ), NULL, 0 );
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
B3_FREE( zeros, uint8_t, maxLength );
|
||||
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count == maxLength );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Mirrored geometry differs from its original only in sign bits, which sit at the top of every
|
||||
// float. A mixer that cannot carry high bits downward maps the whole family onto a few digests.
|
||||
static int HashFloatSigns( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
floatCount = 12,
|
||||
comboCount = 1 << floatCount
|
||||
};
|
||||
|
||||
HashProbe probe = ProbeBegin( comboCount );
|
||||
|
||||
float values[floatCount];
|
||||
for ( int i = 0; i < floatCount; ++i )
|
||||
{
|
||||
values[i] = 1.0f + 0.25f * (float)i;
|
||||
}
|
||||
|
||||
for ( int mask = 0; mask < comboCount; ++mask )
|
||||
{
|
||||
float flipped[floatCount];
|
||||
for ( int i = 0; i < floatCount; ++i )
|
||||
{
|
||||
flipped[i] = ( mask & ( 1 << i ) ) != 0 ? -values[i] : values[i];
|
||||
}
|
||||
|
||||
ProbeAdd( &probe, b3Hash64NonZero( (const uint8_t*)flipped, (int)sizeof( flipped ) ), NULL, 0 );
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count == comboCount );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Procedurally placed vertices land one ulp apart. Those blobs differ in a single low mantissa bit
|
||||
// buried in a long run of identical bytes.
|
||||
static int HashFloatUlp( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
floatCount = 64
|
||||
};
|
||||
|
||||
HashProbe probe = ProbeBegin( floatCount + 1 );
|
||||
|
||||
float values[floatCount];
|
||||
for ( int i = 0; i < floatCount; ++i )
|
||||
{
|
||||
values[i] = 100.0f + (float)i;
|
||||
}
|
||||
|
||||
ProbeAdd( &probe, b3Hash64NonZero( (const uint8_t*)values, (int)sizeof( values ) ), NULL, 0 );
|
||||
|
||||
for ( int i = 0; i < floatCount; ++i )
|
||||
{
|
||||
float saved = values[i];
|
||||
uint32_t bits;
|
||||
memcpy( &bits, &saved, sizeof( bits ) );
|
||||
bits += 1;
|
||||
memcpy( values + i, &bits, sizeof( bits ) );
|
||||
|
||||
ProbeAdd( &probe, b3Hash64NonZero( (const uint8_t*)values, (int)sizeof( values ) ), NULL, 0 );
|
||||
values[i] = saved;
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count == floatCount + 1 );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Real baked hulls. Every box shares its entire topology section and differs in a handful of floats,
|
||||
// which is the closest thing the engine produces to a worst case for a content hash.
|
||||
static int HashBoxHulls( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
steps = 16
|
||||
};
|
||||
|
||||
HashProbe probe = ProbeBegin( steps * steps * steps );
|
||||
|
||||
for ( int i = 0; i < steps; ++i )
|
||||
{
|
||||
for ( int j = 0; j < steps; ++j )
|
||||
{
|
||||
for ( int k = 0; k < steps; ++k )
|
||||
{
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f + 0.25f * (float)i, 0.5f + 0.25f * (float)j, 0.5f + 0.25f * (float)k );
|
||||
ProbeAdd( &probe, box.base.hash, &box, box.base.byteCount );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count == steps * steps * steps );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Same box, moved and turned. The extent bytes are identical across the family so the hash has to
|
||||
// separate these on transform alone.
|
||||
static int HashTransformedBoxHulls( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
steps = 12
|
||||
};
|
||||
|
||||
HashProbe probe = ProbeBegin( steps * steps );
|
||||
|
||||
for ( int i = 0; i < steps; ++i )
|
||||
{
|
||||
for ( int j = 0; j < steps; ++j )
|
||||
{
|
||||
b3Vec3 axis = b3Normalize( (b3Vec3){ 1.0f, 0.5f + 0.1f * (float)j, 0.25f } );
|
||||
b3Transform transform = {
|
||||
.p = { 0.125f * (float)i, -0.25f * (float)j, 0.5f * (float)( i + j ) },
|
||||
.q = b3MakeQuatFromAxisAngle( axis, 0.05f * (float)( i * steps + j ) ),
|
||||
};
|
||||
|
||||
b3BoxHull box = b3MakeTransformedBoxHull( 1.0f, 2.0f, 3.0f, transform );
|
||||
ProbeAdd( &probe, box.base.hash, &box, box.base.byteCount );
|
||||
}
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count == steps * steps );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Tessellated hulls across a parameter sweep. Neighboring parameters produce blobs that agree
|
||||
// almost everywhere, including identical vertex counts and topology.
|
||||
static int HashProceduralHulls( void )
|
||||
{
|
||||
HashProbe probe = ProbeBegin( 1024 );
|
||||
|
||||
for ( int sides = 3; sides <= 18; ++sides )
|
||||
{
|
||||
for ( int r = 1; r <= 5; ++r )
|
||||
{
|
||||
for ( int h = 1; h <= 4; ++h )
|
||||
{
|
||||
b3HullData* cylinder = b3CreateCylinder( 0.5f * (float)h, 0.25f * (float)r, 0.0f, sides );
|
||||
if ( cylinder != NULL )
|
||||
{
|
||||
ProbeAdd( &probe, cylinder->hash, cylinder, cylinder->byteCount );
|
||||
b3DestroyHull( cylinder );
|
||||
}
|
||||
|
||||
// Cones need at least four slices
|
||||
if ( sides >= 4 )
|
||||
{
|
||||
b3HullData* cone = b3CreateCone( 0.5f * (float)h, 0.25f * (float)r, 0.1f * (float)r, sides );
|
||||
if ( cone != NULL )
|
||||
{
|
||||
ProbeAdd( &probe, cone->hash, cone, cone->byteCount );
|
||||
b3DestroyHull( cone );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count > 0 );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Height fields are mostly a flat array of heights, so grids of nearby dimensions differ in very
|
||||
// little beyond their counts. Small wave grids flatten to the plain grid, which is why the probe
|
||||
// has to tell a duplicate blob apart from a collision.
|
||||
static int HashHeightFields( void )
|
||||
{
|
||||
HashProbe probe = ProbeBegin( 512 );
|
||||
|
||||
for ( int rows = 2; rows <= 14; ++rows )
|
||||
{
|
||||
for ( int cols = 2; cols <= 14; ++cols )
|
||||
{
|
||||
b3Vec3 scale = { 1.0f, 1.0f, 1.0f };
|
||||
|
||||
b3HeightFieldData* grid = b3CreateGrid( rows, cols, scale, false );
|
||||
ProbeAdd( &probe, grid->hash, grid, grid->byteCount );
|
||||
b3DestroyHeightField( grid );
|
||||
|
||||
b3HeightFieldData* wave = b3CreateWave( rows, cols, scale, 0.25f * (float)rows, 0.125f * (float)cols, false );
|
||||
ProbeAdd( &probe, wave->hash, wave, wave->byteCount );
|
||||
b3DestroyHeightField( wave );
|
||||
}
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count > 0 );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Meshes carry a baked BVH, so most of the blob is derived data that moves in lockstep with small
|
||||
// parameter changes.
|
||||
static int HashMeshes( void )
|
||||
{
|
||||
HashProbe probe = ProbeBegin( 256 );
|
||||
|
||||
for ( int x = 2; x <= 10; ++x )
|
||||
{
|
||||
for ( int z = 2; z <= 10; ++z )
|
||||
{
|
||||
b3MeshData* grid = b3CreateGridMesh( x, z, 1.0f, 1, false );
|
||||
ProbeAdd( &probe, grid->hash, grid, grid->byteCount );
|
||||
b3DestroyMesh( grid );
|
||||
}
|
||||
}
|
||||
|
||||
for ( int radial = 3; radial <= 12; ++radial )
|
||||
{
|
||||
for ( int tubular = 3; tubular <= 12; ++tubular )
|
||||
{
|
||||
b3MeshData* torus = b3CreateTorusMesh( radial, tubular, 1.0f, 0.25f );
|
||||
ProbeAdd( &probe, torus->hash, torus, torus->byteCount );
|
||||
b3DestroyMesh( torus );
|
||||
}
|
||||
}
|
||||
|
||||
HashResult result = ProbeEnd( &probe );
|
||||
ENSURE( result.overflow == false );
|
||||
ENSURE( result.count > 0 );
|
||||
ENSURE( result.sawZero == false );
|
||||
ENSURE( result.collision == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Voxel colliders sit on a regular grid, so their coordinate floats vary only in high bits. The
|
||||
// hull database takes its home bucket from the low bits of the hash, so a mixer that cannot carry
|
||||
// high bits downward funnels every hull into one bucket. The digests stay distinct throughout,
|
||||
// which is exactly why the collision tests above cannot see it. From issue 120.
|
||||
static int HashVoxelDispersion( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
hullCount = 3000,
|
||||
lowBits = 13,
|
||||
lowCount = 1 << lowBits
|
||||
};
|
||||
|
||||
bool* seen = B3_ALLOC( bool, lowCount );
|
||||
memset( seen, 0, lowCount * sizeof( bool ) );
|
||||
|
||||
int distinctLow = 0;
|
||||
for ( int i = 0; i < hullCount; ++i )
|
||||
{
|
||||
const float cell = 0.25f;
|
||||
b3Vec3 offset = {
|
||||
(float)( i % 15 ) * cell,
|
||||
(float)( ( i / 15 ) % 20 ) * cell,
|
||||
(float)( i / 300 ) * cell,
|
||||
};
|
||||
|
||||
b3BoxHull hull = b3MakeOffsetBoxHull( 0.5f * cell, 0.5f * cell, 0.5f * cell, offset );
|
||||
|
||||
int low = (int)( hull.base.hash & ( lowCount - 1 ) );
|
||||
if ( seen[low] == false )
|
||||
{
|
||||
seen[low] = true;
|
||||
distinctLow += 1;
|
||||
}
|
||||
}
|
||||
|
||||
B3_FREE( seen, bool, lowCount );
|
||||
|
||||
// Filling 8192 slots with 3000 draws tops out near 2500. The 32 bit hash this replaced reached 1.
|
||||
ENSURE( distinctLow > hullCount / 2 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The consequence of the above, measured where it hurt. These hulls drove the world hull database
|
||||
// to two million buckets and a hundred and fifty milliseconds, since every insert walked one chain.
|
||||
static int HashVoxelHullDatabase( void )
|
||||
{
|
||||
enum
|
||||
{
|
||||
hullCount = 3000
|
||||
};
|
||||
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
||||
b3World* world = b3GetWorldFromId( worldId );
|
||||
b3HullMap* map = (b3HullMap*)world->hullDatabase;
|
||||
|
||||
bool created = true;
|
||||
for ( int i = 0; i < hullCount; ++i )
|
||||
{
|
||||
const float cell = 0.25f;
|
||||
b3Vec3 offset = {
|
||||
(float)( i % 15 ) * cell,
|
||||
(float)( ( i / 15 ) % 20 ) * cell,
|
||||
(float)( i / 300 ) * cell,
|
||||
};
|
||||
|
||||
b3BoxHull hull = b3MakeOffsetBoxHull( 0.5f * cell, 0.5f * cell, 0.5f * cell, offset );
|
||||
b3ShapeId shapeId = b3CreateHullShape( bodyId, &shapeDef, &hull.base );
|
||||
if ( shapeId.index1 == 0 )
|
||||
{
|
||||
created = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
size_t entryCount = b3HullMap_size( map );
|
||||
size_t bucketCount = b3HullMap_bucket_count( map );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
|
||||
ENSURE( created );
|
||||
ENSURE( entryCount == hullCount );
|
||||
|
||||
// Healthy growth settles at 4096 for this many distinct hulls
|
||||
ENSURE( bucketCount <= 4 * hullCount );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Identical input must bake to an identical hash, or dedup silently stops working.
|
||||
static int HashStability( void )
|
||||
{
|
||||
b3BoxHull box1 = b3MakeBoxHull( 1.0f, 2.0f, 3.0f );
|
||||
b3BoxHull box2 = b3MakeBoxHull( 1.0f, 2.0f, 3.0f );
|
||||
ENSURE( box1.base.hash == box2.base.hash );
|
||||
|
||||
b3HullData* cylinder1 = b3CreateCylinder( 1.0f, 0.5f, 0.0f, 12 );
|
||||
b3HullData* cylinder2 = b3CreateCylinder( 1.0f, 0.5f, 0.0f, 12 );
|
||||
bool sameHash = cylinder1->hash == cylinder2->hash;
|
||||
bool sameSize = cylinder1->byteCount == cylinder2->byteCount;
|
||||
bool sameBytes = sameSize && memcmp( cylinder1, cylinder2, (size_t)cylinder1->byteCount ) == 0;
|
||||
b3DestroyHull( cylinder1 );
|
||||
b3DestroyHull( cylinder2 );
|
||||
|
||||
ENSURE( sameHash );
|
||||
ENSURE( sameBytes );
|
||||
|
||||
// An empty blob has no content to mix, so it takes the reserved value
|
||||
ENSURE( b3Hash64NonZero( NULL, 0 ) == 1 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int HashTest( void )
|
||||
{
|
||||
RUN_SUBTEST( HashWordFamily );
|
||||
RUN_SUBTEST( HashBitSweep );
|
||||
RUN_SUBTEST( HashZeroLengths );
|
||||
RUN_SUBTEST( HashFloatSigns );
|
||||
RUN_SUBTEST( HashFloatUlp );
|
||||
RUN_SUBTEST( HashBoxHulls );
|
||||
RUN_SUBTEST( HashTransformedBoxHulls );
|
||||
RUN_SUBTEST( HashProceduralHulls );
|
||||
RUN_SUBTEST( HashHeightFields );
|
||||
RUN_SUBTEST( HashMeshes );
|
||||
RUN_SUBTEST( HashVoxelDispersion );
|
||||
RUN_SUBTEST( HashVoxelHullDatabase );
|
||||
RUN_SUBTEST( HashStability );
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1666,20 +1666,6 @@ static int GeometryHashCollision( void )
|
|||
const int n = 16;
|
||||
const uint64_t sharedHash = 0xABCD1234ull;
|
||||
|
||||
// The content hash must use its full width: a one-byte change in a same-length blob has to perturb
|
||||
// the high word too, not just the low one, which is the trap a reseeded 32-bit djb2 fell into.
|
||||
{
|
||||
uint8_t p[16];
|
||||
uint8_t q[16];
|
||||
memset( p, 0x11, sizeof( p ) );
|
||||
memset( q, 0x11, sizeof( q ) );
|
||||
q[7] = 0x12;
|
||||
uint64_t hp = b3Hash64NonZero( p, (int)sizeof( p ) );
|
||||
uint64_t hq = b3Hash64NonZero( q, (int)sizeof( q ) );
|
||||
ENSURE( hp != hq );
|
||||
ENSURE( (uint32_t)( hp >> 32 ) != (uint32_t)( hq >> 32 ) );
|
||||
}
|
||||
|
||||
b3GeometryRegistry reg = { 0 };
|
||||
|
||||
uint8_t* blobA = (uint8_t*)b3Alloc( (size_t)n );
|
||||
|
|
|
|||
Loading…
Reference in a new issue