File: test_data.h

package info (click to toggle)
zix 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 828 kB
  • sloc: ansic: 9,083; cpp: 479; python: 127; makefile: 6
file content (56 lines) | stat: -rw-r--r-- 1,362 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2011-2023 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC

#ifndef ZIX_TEST_DATA_H
#define ZIX_TEST_DATA_H

#include <stddef.h>
#include <stdint.h>

/// Linear Congruential Generator for making random 32-bit integers
static inline uint32_t
lcg32(const uint32_t i)
{
  static const uint32_t a = 134775813U;
  static const uint32_t c = 1U;

  return (a * i) + c;
}

/// Linear Congruential Generator for making random 64-bit integers
static inline uint64_t
lcg64(const uint64_t i)
{
  static const uint64_t a = 6364136223846793005ULL;
  static const uint64_t c = 1ULL;

  return (a * i) + c;
}

/// Linear Congruential Generator for making random pointer-sized integers
static inline uintptr_t
lcg(const uintptr_t i)
{
#if UINTPTR_MAX >= UINT64_MAX
  return lcg64(i);
#else
  return lcg32(i);
#endif
}

/// Return a pseudo-pseudo-pseudo-random-ish integer with no duplicates
static inline size_t
unique_rand(size_t i)
{
  i ^= 0x5CA1AB1EU; // Juggle bits to avoid linear clumps

  // Largest prime < 2^32 which satisfies (2^32 = 3 mod 4)
  static const size_t prime = 4294967291U;

  const size_t residue = (size_t)(((uint64_t)i * i) % prime);
  return (i >= prime)       ? i // Values >= prime are mapped to themselves
         : (i <= prime / 2) ? residue
                            : prime - residue;
}

#endif // ZIX_TEST_DATA_H