Branch data Line data Source code
1 : : // Copyright 2011-2023 David Robillard <d@drobilla.net> 2 : : // SPDX-License-Identifier: ISC 3 : : 4 : : #ifndef ZIX_TEST_DATA_H 5 : : #define ZIX_TEST_DATA_H 6 : : 7 : : #include <stddef.h> 8 : : #include <stdint.h> 9 : : 10 : : /// Linear Congruential Generator for making random 32-bit integers 11 : : static inline uint32_t 12 : 53940 : lcg32(const uint32_t i) 13 : : { 14 : : static const uint32_t a = 134775813U; 15 : : static const uint32_t c = 1U; 16 : : 17 : 53940 : return (a * i) + c; 18 : : } 19 : : 20 : : /// Linear Congruential Generator for making random 64-bit integers 21 : : static inline uint64_t 22 : 400048 : lcg64(const uint64_t i) 23 : : { 24 : : static const uint64_t a = 6364136223846793005ULL; 25 : : static const uint64_t c = 1ULL; 26 : : 27 : 400048 : return (a * i) + c; 28 : : } 29 : : 30 : : /// Linear Congruential Generator for making random pointer-sized integers 31 : : static inline uintptr_t 32 : 400048 : lcg(const uintptr_t i) 33 : : { 34 : : #if UINTPTR_MAX >= UINT64_MAX 35 : 400048 : return lcg64(i); 36 : : #else 37 : : return lcg32(i); 38 : : #endif 39 : : } 40 : : 41 : : /// Return a pseudo-pseudo-pseudo-random-ish integer with no duplicates 42 : : static inline size_t 43 : 682596 : unique_rand(size_t i) 44 : : { 45 : 682596 : i ^= 0x5CA1AB1EU; // Juggle bits to avoid linear clumps 46 : : 47 : : // Largest prime < 2^32 which satisfies (2^32 = 3 mod 4) 48 : : static const size_t prime = 4294967291U; 49 : : 50 : 682596 : const size_t residue = (size_t)(((uint64_t)i * i) % prime); 51 : 682596 : return (i >= prime) ? i // Values >= prime are mapped to themselves 52 [ + - ]: 1365192 : : (i <= prime / 2) ? residue 53 [ - + ]: 682596 : : prime - residue; 54 : : } 55 : : 56 : : #endif // ZIX_TEST_DATA_H