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
|
namespace;
include rt;
// custom opcodes: recipes, lifted vm code, several outputs, and a fallback
enhance clampadd(a, b) micro "a = add a b; a = min a 100; a = max a 0";
enhance cube(x) vm { 0 $ 0 $ * * };
enhance 2 divmod(a, b) micro "r2 = sdiv a b" "\n r1 = srem a b\n r0 = r2";
enhance div7(a) micro "a = sdiv a 7";
enhance urem10(a) micro "a = urem a 10";
enhance hi(a, b) micro "a = mulhu a b";
enhance noisy(x) vm { 0 $ . } else soft_noisy; // i/o can't be lifted, so calls use the fallback
fn soft_noisy(x) { return x + 1; }
let qq, rr, seed = 7;
fn rnd() { seed = seed * 1103515245 + 12345; return seed ^ (seed >> 11); }
fn main() {
let i = 0, a, bad = 0;
rt.put_n(clampadd(70, 50)); rt.put_c(' '); rt.put_n(clampadd(-50, 2)); rt.put_c(' '); rt.put_n(cube(7)); rt.put_c(' ');
asm { 17 5 divmod rr ! qq ! }
rt.put_n(qq); rt.put_c(','); rt.put_n(rr); rt.put_c(' ');
rt.put_n(noisy(4)); rt.put_c(' ');
rt.put_n(hi(-1, -1)); rt.put_c(' ');
while (i < 5000) {
a = rnd();
if (div7(a) != rt.zdiv(a, 7)) bad++;
if (urem10(a) < 0 || urem10(a) > 9) bad++;
if (a / (i + 1) != rt.zdiv(a, i + 1)) bad++;
i++;
}
rt.put_n(bad); rt.put_c(10);
return 0;
}
|