-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuwurandom_core.h
197 lines (160 loc) · 5.67 KB
/
uwurandom_core.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
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
189
190
191
192
193
194
195
196
197
#ifndef _UWURANDOM_CORE_H
#define _UWURANDOM_CORE_H
#include "uwurandom_platform.h"
#include "uwurandom_types.h"
#define CREATE_PRINT_STRING(printed_string) (uwu_op){\
.opcode = UWU_PRINT_STRING,\
.state = {\
.print_string = {\
.string = (printed_string),\
.remaining_chars = sizeof(printed_string) - 1\
}\
}\
}
#define CREATE_REPEAT_CHARACTER(repeated_character, num_repetitions) (uwu_op){\
.opcode = UWU_REPEAT_CHARACTER,\
.state = {\
.repeat_character = {\
.character = (repeated_character),\
.remaining_chars = (num_repetitions)\
}\
}\
}
#define CREATE_MARKOV(markov_data, starting_ngram, len) (uwu_op){\
.opcode = UWU_MARKOV,\
.state = {\
.markov = {\
.prev_ngram = (starting_ngram),\
.remaining_chars = (len),\
.ngrams = &(markov_data)\
}\
}\
}\
static inline int
uwu_push_op(uwu_state* state, uwu_op op) {
if (state->current_op == MAX_OPS - 1) {
return -1;
}
state->current_op++;
state->ops[state->current_op] = op;
return 0;
}
// Pick a random program from the list of programs and write it to the ops list
static void
generate_new_ops(uwu_state* state) {
uwu_random_number op_idx = uwu_random_int(state);
if (state->prev_op == -1) {
op_idx %= state->num_ops;
} else {
// don't repeat previous op
op_idx %= state->num_ops - 1;
if (op_idx >= (unsigned int)state->prev_op) {
op_idx += 1;
}
}
state->prev_op = op_idx;
// print a space after the op is done executing
uwu_push_op(state, CREATE_PRINT_STRING(" "));
state->ops_table[op_idx](state);
}
// Execute an operation once. Returns the number of characters written, or a negative value on error.
static int uwu_exec_op(uwu_state* state, char* buf, size_t len) {
uwu_op* op = &state->ops[state->current_op];
switch (op->opcode) {
case UWU_PRINT_STRING: {
char* string = op->state.print_string.string;
size_t remaining = op->state.print_string.remaining_chars;
if (remaining == 0) {
state->current_op--;
return 0;
}
size_t num_chars_to_copy = remaining > len ? len : remaining;
COPY_STR(buf, string, num_chars_to_copy);
// Advance state by number of characters copied;
op->state.print_string.string += num_chars_to_copy;
op->state.print_string.remaining_chars -= num_chars_to_copy;
return num_chars_to_copy;
}
case UWU_MARKOV: {
size_t ngram_index = op->state.markov.prev_ngram;
const uwu_markov_table* table = op->state.markov.ngrams;
const uwu_markov_ngram* ngrams = table->ngrams;
const uwu_markov_choice* choices = table->choices;
size_t remaining = op->state.markov.remaining_chars;
if (remaining == 0) {
state->current_op--;
return 0;
}
size_t num_chars_to_copy = remaining > len ? len : remaining;
size_t i;
for (i = 0; i < num_chars_to_copy; i++) {
uwu_markov_ngram ngram = ngrams[ngram_index];
uwu_random_number random = uwu_random_int(state);
random %= ngram.total_probability;
int j = 0;
while (true) {
uwu_markov_choice choice = choices[ngram.choices + j];
size_t cumulative_probability = choice.cumulative_probability;
if (random < cumulative_probability) {
ngram_index = choice.next_ngram;
break;
}
j++;
}
char ngram_char = ngram.character;
if (ngram_char < 0) {
size_t special_idx = -1 - ngram_char;
table->specials[special_idx](state);
break;
} else {
COPY_CHAR(ngram_char, buf + i);
}
}
op->state.markov.prev_ngram = ngram_index;
op->state.markov.remaining_chars -= i;
return i;
}
case UWU_REPEAT_CHARACTER: {
char c = op->state.repeat_character.character;
size_t i;
for (i = 0; i < len; i++) {
if (op->state.repeat_character.remaining_chars == 0) {
// Out of characters. Return the number of characters thus written.
state->current_op--;
return i;
}
COPY_CHAR(c, buf + i);
op->state.repeat_character.remaining_chars--;
}
return len;
}
default: return 0;
}
}
// Fill the given buffer with UwU
static int uwu_write_chars(uwu_state* state, char* buf, size_t n) {
size_t total_written = 0;
while (total_written < n) {
if (state->current_op == -1) {
// regenerate ops
generate_new_ops(state);
}
size_t chars_written = uwu_exec_op(state, buf + total_written, n - total_written);
if (chars_written < 0) return chars_written;
total_written += chars_written;
}
return 0;
}
static int uwu_init_state(uwu_state* state, uwu_op_factory** ops_table, size_t num_ops) {
int rng_err = uwu_init_rng(state);
if (rng_err) return rng_err;
state->ops_table = ops_table;
state->num_ops = num_ops;
state->current_op = -1;
state->prev_op = -1;
return 0;
}
static void uwu_destroy_state(uwu_state* state) {
uwu_destroy_rng(state);
}
#endif