-
Notifications
You must be signed in to change notification settings - Fork 0
/
8bitrand.c
188 lines (160 loc) · 3.33 KB
/
8bitrand.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdint.h>
#include "8bitrand.h"
#define MUL8x8(a, b) (uint16_t)((uint8_t)(a) * (uint8_t)(b))
static struct
{
uint8_t state[SEED_MAX]; /* would normally be smaller */
uint16_t c;
/* Could be folded into c if storage is tight */
uint8_t i;
uint8_t c1; /* only for *_8z8bit() case */
/* Would normally be compile-time constants */
uint8_t r;
uint8_t mul_lo;
uint8_t mul_hi;
} rand8;
void rand8_init(uint16_t a, uint8_t r, uint16_t c, uint8_t const *seed)
{
int i;
rand8.mul_lo = a & 0xff;
rand8.mul_hi = a >> 8;
rand8.r = r;
for (i = 0; i < r; i++)
rand8.state[i] = seed[i];
rand8.c = c;
rand8.i = 0;
}
/* Use this version for multipliers less than 256.
*/
uint8_t rand8_8bit(void)
{
uint8_t i = rand8.i;
uint8_t x = rand8.state[i];
uint16_t c = rand8.c;
uint16_t t;
t = MUL8x8(x, rand8.mul_lo) + c;
c = t >> 8;
x = t & 255;
rand8.state[i] = x;
rand8.c = c;
if (++i >= rand8.r)
i = 0;
rand8.i = i;
return x;
}
uint16_t rand16_8bit(void)
{
uint8_t x, y;
x = rand8_8bit();
y = rand8_8bit();
return ((uint16_t)y << 8) + x;
}
uint32_t rand32_8bit(void)
{
uint16_t x, y;
x = rand16_8bit();
y = rand16_8bit();
return ((uint32_t)y << 16) + x;
}
/* Use this version for multipliers between 256 and 511.
*/
uint8_t rand8_9bit(void)
{
int i = rand8.i;
uint8_t x = rand8.state[i];
uint16_t c = rand8.c;
uint16_t t;
t = MUL8x8(x, rand8.mul_lo) + c;
c = t >> 8;
c += x;
x = t & 255;
rand8.state[i] = x;
rand8.c = c;
if (++i >= rand8.r)
i = 0;
rand8.i = i;
return x;
}
uint16_t rand16_9bit(void)
{
uint8_t x, y;
x = rand8_9bit();
y = rand8_9bit();
return ((uint16_t)y << 8) + x;
}
uint32_t rand32_9bit(void)
{
uint16_t x, y;
x = rand16_9bit();
y = rand16_9bit();
return ((uint32_t)y << 16) + x;
}
/* Use this version for multipliers 512 and larger.
*/
uint8_t rand8_16bit(void)
{
int i = rand8.i;
uint8_t x = rand8.state[i];
uint16_t c = rand8.c;
uint16_t t;
t = MUL8x8(x, rand8.mul_lo) + c;
c = t >> 8;
c += MUL8x8(x, rand8.mul_hi);
x = t & 255;
rand8.state[i] = x;
rand8.c = c;
if (++i >= rand8.r)
i = 0;
rand8.i = i;
return x;
}
uint16_t rand16_16bit(void)
{
uint8_t x, y;
x = rand8_16bit();
y = rand8_16bit();
return ((uint16_t)y << 8) + x;
}
uint32_t rand32_16bit(void)
{
uint16_t x, y;
x = rand16_16bit();
y = rand16_16bit();
return ((uint32_t)y << 16) + x;
}
/* Use this version for multipliers in the form 0xXX00XX.
*/
uint8_t rand8_8z8bit(void)
{
int i = rand8.i;
uint8_t x = rand8.state[i];
uint16_t c0 = rand8.c;
uint8_t c1 = rand8.c1;
uint32_t t;
t = MUL8x8(x, rand8.mul_lo) + (uint32_t)c0;
t += (uint32_t)(MUL8x8(x, rand8.mul_hi) + c1) << 16;
c0 = t >> 8;
c1 = t >> 24;
x = t & 255;
rand8.state[i] = x;
rand8.c = c0;
rand8.c1 = c1;
if (++i >= rand8.r)
i = 0;
rand8.i = i;
return x;
}
uint16_t rand16_8z8bit(void)
{
uint8_t x, y;
x = rand8_8z8bit();
y = rand8_8z8bit();
return ((uint16_t)y << 8) + x;
}
uint32_t rand32_8z8bit(void)
{
uint16_t x, y;
x = rand16_8z8bit();
y = rand16_8z8bit();
return ((uint32_t)y << 16) + x;
}