-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib_random_intramodule_privatetable_elem.js
76 lines (61 loc) · 3.87 KB
/
fib_random_intramodule_privatetable_elem.js
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
/* Author: Lars T Hansen, Mozilla */
// INFO: Doubly-recursive fib(40) with indirect calls via a private table to a set of same-module functions, table initialized by elem
var ins = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(`
(module
;; Random indices into the function table, indexed by the fib argument
(memory 1)
(data (i32.const 0) (i8 4 1 5 6 7 4 2 2 1 2 6 7 0 5 3 5 5 4 6 2 0 6 0 3 2 5 3 7 0 4 6 5 2 2 5 0 2 2 5 7 0))
(data (i32.const 100) (i8 0 4 7 1 1 7 0 2 7 6 5 3 7 2 4 6 6 0 5 4 5 3 4 4 3 6 7 1 4 5 5 4 4 0 5 7 1 0 7 7 1))
(type $ty (func (param i32) (result i32)))
(table $t 8 funcref)
(elem $t (i32.const 0) $fib0 $fib1 $fib2 $fib3 $fib4 $fib5 $fib6 $fib7)
(func $fib0 (export "fib") (param $n i32) (result i32)
(if (result i32) (i32.lt_s (local.get $n) (i32.const 2))
(local.get $n)
(i32.add (call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 1)) (i32.load8_u (local.get $n)))
(call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 2)) (i32.load8_u offset=100 (local.get $n))))))
(func $fib1 (param $n i32) (result i32)
(if (result i32) (i32.lt_s (local.get $n) (i32.const 2))
(local.get $n)
(i32.add (call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 1)) (i32.load8_u (local.get $n)))
(call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 2)) (i32.load8_u offset=100 (local.get $n))))))
(func $fib2 (param $n i32) (result i32)
(if (result i32) (i32.lt_s (local.get $n) (i32.const 2))
(local.get $n)
(i32.add (call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 1)) (i32.load8_u (local.get $n)))
(call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 2)) (i32.load8_u offset=100 (local.get $n))))))
(func $fib3 (param $n i32) (result i32)
(if (result i32) (i32.lt_s (local.get $n) (i32.const 2))
(local.get $n)
(i32.add (call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 1)) (i32.load8_u (local.get $n)))
(call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 2)) (i32.load8_u offset=100 (local.get $n))))))
(func $fib4 (param $n i32) (result i32)
(if (result i32) (i32.lt_s (local.get $n) (i32.const 2))
(local.get $n)
(i32.add (call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 1)) (i32.load8_u (local.get $n)))
(call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 2)) (i32.load8_u offset=100 (local.get $n))))))
(func $fib5 (param $n i32) (result i32)
(if (result i32) (i32.lt_s (local.get $n) (i32.const 2))
(local.get $n)
(i32.add (call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 1)) (i32.load8_u (local.get $n)))
(call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 2)) (i32.load8_u offset=100 (local.get $n))))))
(func $fib6 (param $n i32) (result i32)
(if (result i32) (i32.lt_s (local.get $n) (i32.const 2))
(local.get $n)
(i32.add (call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 1)) (i32.load8_u (local.get $n)))
(call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 2)) (i32.load8_u offset=100 (local.get $n))))))
(func $fib7 (param $n i32) (result i32)
(if (result i32) (i32.lt_s (local.get $n) (i32.const 2))
(local.get $n)
(i32.add (call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 1)) (i32.load8_u (local.get $n)))
(call_indirect (type $ty) (i32.sub (local.get $n) (i32.const 2)) (i32.load8_u offset=100 (local.get $n))))))
)
`)));
assertEq(ins.exports.fib(10), 55);
var runs = (function () { let k = Number(os.getenv("RUNS")); return isNaN(k) || k < 0 ? 1 : k })();
while (runs-- > 0) {
var then = Date.now();
ins.exports.fib(40);
var now = Date.now();
print("fib_random_intramodule_privatetable_elem " + (now - then));
}