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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
#define _GNU_SOURCE
#define inline __attribute__((always_inline))
//
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#define auto __auto_type
#define let auto
// -- disable use of compiler intrinsics
// #define PU3_NO_INTRINSICS
// -- inaccurate clocks
// #define PU3_BAD_CLOCK
#ifndef PU3_BAD_CLOCK
uint64_t ntime() {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC_RAW, &t);
return t.tv_nsec + t.tv_sec * 1e9;
}
#else
uint64_t ntime() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
}
#endif
// it is time
// the thing you've none been waiting for
// pu3
typedef char *string;
typedef unsigned char byte;
typedef byte bool;
typedef uint64_t pu3_inst_t;
#ifndef PU3_NO_INTRINSICS
#define unlikely(JJ) __builtin_expect((JJ), 0)
#define likely(JJ) __builtin_expect((JJ), 1)
#else
#define unlikely(JJ) (JJ)
#define likely(JJ) (JJ)
#endif
#define get_spare_nosh(__x) ((__x) & 0xFF00000000000000ULL)
#define get_scroll_byte_nosh(__x) ((__x) & 0x00FF000000000000ULL)
#define get_data_u16_nosh(__x) ((__x) & 0x0000FFFF00000000ULL)
#define get_dest_u32_nosh(__x) ((__x) & 0x00000000FFFFFFFFULL)
#define get_spare(__x) (get_spare_nosh(__x) >> 56)
#define get_scroll_byte(__x) (get_scroll_byte_nosh(__x) >> 48)
#define get_data_u16(__x) (get_data_u16_nosh(__x) >> 32)
#define get_dest_u32(__x) (get_dest_u32_nosh(__x) >> 0)
#define prep_spare(__x) (((__x) & 0x000000FFULL) << 56)
#define prep_scroll_byte(__x) (((__x) & 0x000000FFULL) << 48)
#define prep_data_u16(__x) (((__x) & 0x0000FFFFULL) << 32)
#define prep_dest_u32(__x) (((__x) & 0xFFFFFFFFULL) << 00)
#define set_spare(__v, __x) __v = (((__v) & 0x00FFFFFFFFFFFFFFULL) | prep_spare(__x))
#define set_scroll_byte(__v, __x) __v = (((__v) & 0xFF00FFFFFFFFFFFFULL) | prep_scroll_byte(__x))
#define set_data_u16(__v, __x) __v = (((__v) & 0xFFFF0000FFFFFFFFULL) | prep_data_u16(__x))
#define set_dest_u32(__v, __x) __v = (((__v) & 0xFFFFFFFF00000000ULL) | prep_dest_u32(__x))
// clang-format off
struct {
uint32_t nop, hlt, mov, add, sub, inc, dec, mul, lsh, rsh, and, not, jmp, jnz, dmv, ddm;
} opcode = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
};
typedef uint32_t pu3_opcode;
// clang-format on
inline pu3_inst_t build_inst(bool swap, bool deref, pu3_opcode opcode, uint32_t lhs, uint16_t rhs) {
pu3_inst_t output = 0;
byte spare = ((swap & 1) << 7) | ((deref & 1) << 6);
byte op = opcode & 0b1111;
set_spare(output, spare);
set_scroll_byte(output, op);
set_data_u16(output, rhs);
set_dest_u32(output, lhs);
return output;
}
typedef struct {
pu3_inst_t *memory_u64;
uint32_t *memory_u32;
} pu3_mem;
typedef struct {
pu3_mem memory;
uint32_t mem_size;
uint32_t *pc;
uint32_t time_slice;
} pu3_ctx;
// instruct packing:
// typedef struct {
// byte __spare[ 1 ];
// byte op;
// uint16_t data;
// uint32_t dest;
// } pu3_inst_t;
// ------------------------------------------------------------------------------------------
// instructions list
// 0: nop ________, ___ (( advances program counter by lhs ))
// 1: hlt ________, ___ (( stops the cpu ))
// 2: mov [target], src (( moves rhs to lhs ))
// 3: add [target], src (( adds rhs to lhs ))
// 4: sub [target], src (( subtracts rhs from lhs ))
// 5: inc [target], ___ (( increments lhs ))
// 6: dec [target], ___ (( decrements lhs ))
// 7: mul [target], src (( multiplies lhs by rhs ))
// 8: lsh [target], src (( left shifts lhs by rhs ))
// 9: rsh [target], src (( right shifts lhs by rhs ))
// 10: and [target], src (( logical ANDs lhs with rhs ))
// 11: not [target], src (( stores !*lhs in lhs ))
// 12: jmp [target], src (( jumps to lhs ))
// 13: jnz [target], src (( jumps to lhs if rhs is non-zero ))
// 14: dmv [target], ___ (( sets lhs to *lhs ))
// 15: ddm [target], src (( set m[m[lhs]] to m[rhs] ))
// ------------------------------------------------------------------------------------------
// explanation
// we see lhs and rhs. these are, in order, the u32 dest and the u16 data.
// the following are executed, in order, in each cycle (if required):
// 1. lhs and rhs are swapped (to allow access to all of the u32 memory range)-
// -if the leftmost bit of spare is set. (spare & 0b10000000 != 0)
// 2. lhs is converted to memory[ lhs ] unconditionally
// 3. rhs is converted to memory[ rhs ] only if the second leftmmost bit of spare is set.
// (spare & 0b01000000 != 0)
// ------------------------------------------------------------------------------------------
// WARNING: the fields left blank in the instruction list are NOT PROCESSED,
// this means they're ignored even if there's a dereference or a swap.
// ------------------------------------------------------------------------------------------
#define prep_raw() \
({ \
rhs = (uint32_t) get_data_u16(cur); \
if (get_spare(cur) & 0x40) rhs = mem32[ rhs ]; \
})
#define prep_vals() \
({ \
spare = get_spare(cur); \
if (unlikely(spare & 0x80)) { \
rhs = get_dest_u32(cur); \
lhs = (uint32_t) get_data_u16(cur); \
} else { \
lhs = get_dest_u32(cur); \
rhs = (uint32_t) get_data_u16(cur); \
} \
if (spare & 0x40) rhs = mem32[ rhs ]; \
})
#define prep_only_left() ({ lhs = get_dest_u32(cur); })
// spare = lhs;
// spare = *spare;
//
void disassemble_pu3_inst(pu3_inst_t inst);
pu3(pu3_ctx ctx) {
uint64_t i = 0;
pu3_inst_t cur;
uint32_t *mem32 = ctx.memory.memory_u32;
pu3_inst_t *mem64 = ctx.memory.memory_u64;
pu3_inst_t *pc = mem64 + *ctx.pc;
byte op;
byte spare;
// uint32_t lhs;
// uint32_t rhs;
uint32_t lhs;
uint32_t rhs;
uint32_t out;
const __attribute__((aligned(64))) void *goto_table[]
= { &&pu3_inst_0, &&pu3_inst_1, &&pu3_inst_2, &&pu3_inst_3, &&pu3_inst_4, &&pu3_inst_5,
&&pu3_inst_6, &&pu3_inst_7, &&pu3_inst_8, &&pu3_inst_9, &&pu3_inst_10, &&pu3_inst_11,
&&pu3_inst_12, &&pu3_inst_13, &&pu3_inst_14, &&pu3_inst_15 };
pu3_start:
i++;
if (unlikely(ctx.time_slice == i)) goto pu3_inst_1;
cur = *pc++;
// printf("%li: ", pc - mem64 - 1);
// disassemble_pu3_inst(cur);
#ifndef PU3_NO_INTRINSICS
__builtin_prefetch(pc + 1, 0, 3);
#endif
op = get_scroll_byte(cur);
goto *goto_table[ op & 0b1111 ];
pu3_inst_0: // nop
prep_raw();
pc += rhs;
goto pu3_start;
pu3_inst_2: // mov
prep_vals();
mem32[ lhs ] = rhs;
goto pu3_start;
pu3_inst_3: // add
prep_vals();
mem32[ lhs ] += rhs;
goto pu3_start;
pu3_inst_4: // sub
prep_vals();
mem32[ lhs ] -= rhs;
goto pu3_start;
pu3_inst_5: // inc
prep_only_left();
mem32[ lhs ]++;
goto pu3_start;
pu3_inst_6: // dec
prep_only_left();
mem32[ lhs ]--;
goto pu3_start;
pu3_inst_7: // mul
prep_vals();
mem32[ lhs ] *= rhs;
goto pu3_start;
pu3_inst_8: // lsh
prep_vals();
mem32[ lhs ] <<= rhs;
goto pu3_start;
pu3_inst_9: // rsh
prep_vals();
mem32[ lhs ] >>= rhs;
goto pu3_start;
pu3_inst_10: // and
prep_vals();
mem32[ lhs ] &= rhs;
goto pu3_start;
pu3_inst_11: // not
prep_only_left();
mem32[ lhs ] = !mem32[ lhs ];
goto pu3_start;
pu3_inst_12: // jmp
prep_only_left();
pc = mem64 + lhs;
goto pu3_start;
pu3_inst_13: // jnz
prep_vals();
if (rhs) pc = mem64 + lhs;
goto pu3_start;
pu3_inst_14: // dmv
prep_vals();
mem32[ lhs ] = mem32[ mem32[ lhs ] ];
goto pu3_start;
pu3_inst_15: // ddm
prep_vals();
mem32[ mem32[ lhs ] ] = mem32[ rhs ];
goto pu3_start;
pu3_inst_1: // hlt
*ctx.pc = pc - mem64;
return ctx.time_slice - i;
}
const string op_names[] = { "nop", "hlt", "mov", "add", "sub", "inc", "dec", "mul",
"lsh", "rsh", "and", "not", "jmp", "jnz", "dmv", "ddm" };
// bench: measure using clock()
double bench_cycle(byte instr, pu3_ctx ctx) {
int i;
for (i = 16; i < ctx.mem_size - 16; i++) {
set_scroll_byte(ctx.memory.memory_u64[ i ], instr & 0b1111);
set_data_u16(ctx.memory.memory_u64[ i ], 4);
set_dest_u32(ctx.memory.memory_u64[ i ], i);
if (i == ctx.mem_size - 20) {
let j = 0;
for (j = 0; j < 20; j++) {
set_scroll_byte(ctx.memory.memory_u64[ ctx.mem_size - j ], 1);
}
}
}
set_scroll_byte(ctx.memory.memory_u64[ i ], 0);
*ctx.pc = 8;
uint64_t start = ntime();
pu3(ctx);
uint64_t end = ntime();
double nanoseconds = (double) (end - start);
double inst_count = *ctx.pc - 8;
double insts_per_ns = inst_count / nanoseconds;
double insts_per_s = insts_per_ns * 1000000000ULL;
printf(
"%-8s | time: %.3fms | insts: %.0f (%llu real) | ips: %.3fM inst/s\n",
op_names[ instr ],
nanoseconds / 1000000,
inst_count,
(unsigned long long) (end - start),
insts_per_ns * 1e3);
return insts_per_s / 1e6; // return M inst/s
}
#define S 1 << 23
pu3_ctx *init_pu3() {
const uint32_t SIZE = S;
let mem_arr = calloc(SIZE, sizeof(pu3_inst_t));
if (!mem_arr) {
perror("memory allocation failed");
return NULL;
}
pu3_ctx *ctx = calloc(1, sizeof(pu3_ctx));
*ctx = (pu3_ctx) { .memory = { .memory_u64 = mem_arr, .memory_u32 = mem_arr },
.mem_size = SIZE,
.pc = calloc(1, sizeof(uint32_t)),
.time_slice = 0 };
return ctx;
}
pu3_benchmark() {
pu3_ctx *__ctx = init_pu3();
pu3_ctx ctx = *__ctx;
int i;
int total = 0;
double benchs = 0;
for (i = 0; i < 16; i++) {
if (i == 1 || i == 12 || i == 13) continue;
total++;
benchs += bench_cycle(i, ctx);
}
printf(
"-------------------------------\n"
"TOTAL: %fMHz\n",
benchs / total);
free(ctx.memory.memory_u32);
free(ctx.pc);
free(__ctx);
return 0;
}
|