forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.h
51 lines (42 loc) · 1.03 KB
/
random.h
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
#ifndef LAB0_RANDOM_H
#define LAB0_RANDOM_H
#include <stddef.h>
#include <stdint.h>
extern int randombytes(uint8_t *buf, size_t len);
static inline uint8_t randombit(void)
{
uint8_t ret = 0;
randombytes(&ret, 1);
return ret & 1;
}
#if INTPTR_MAX == INT64_MAX
#define M_INTPTR_SHIFT (3)
#elif INTPTR_MAX == INT32_MAX
#define M_INTPTR_SHIFT (2)
#else
#error Platform pointers must be 32 or 64 bits
#endif
#define M_INTPTR_SIZE (1 << M_INTPTR_SHIFT)
static inline uintptr_t random_shuffle(uintptr_t x)
{
/* Ensure we do not get stuck in generating zeros */
if (x == 0)
x = 17;
#if M_INTPTR_SIZE == 8
/* by Sebastiano Vigna, see: <http://xoshiro.di.unimi.it/splitmix64.c> */
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9UL;
x ^= x >> 27;
x *= 0x94d049bb133111ebUL;
x ^= x >> 31;
#elif M_INTPTR_SIZE == 4
/* by Chris Wellons, see: <https://nullprogram.com/blog/2018/07/31/> */
x ^= x >> 16;
x *= 0x7feb352dUL;
x ^= x >> 15;
x *= 0x846ca68bUL;
x ^= x >> 16;
#endif
return x;
}
#endif