-
Notifications
You must be signed in to change notification settings - Fork 203
/
benchmark_fannkuchredux.t
154 lines (127 loc) · 2.77 KB
/
benchmark_fannkuchredux.t
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
--[[
The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
contributed by Ledrug Katz
]]
C = terralib.includecstring [[
#include<stdio.h>
#include<stdlib.h>
]]
-- this depends highly on the platform. It might be faster to use
-- char type on 32-bit systems; it might be faster to use unsigned.
elem = int
s = global(elem[16])
t = global(elem[16])
maxflips = global(int)
max_n = global(int)
odd = global(bool)
checksum = global(int)
terra flip()
var i = max_n
var x : &elem = t
var y : &elem = s
var c : elem
while i > 0 do
i = i - 1
@x = @y
x = x + 1
y = y + 1
end
i = 1
repeat
x = t
y = t + t[0]
while x < y do
c = @x
@x = @y
x = x + 1
@y = c
y = y - 1
end
i = i + 1
until t[t[0]] == 0
--C.printf("flip %d\n",i);
return i
end
terra rotate(n : int)
var c = s[0]
for i = 0,n do
s[i] = s[i+1]
end
s[n] = c
--C.printf("rotate(%d) %d\n",n,c);
end
terra tk(n : int)
var i = 0
var f : int
var c : elem[16]
for i = 0,16 do
c[i] = 0
end
while i < n do
rotate(i)
if c[i] >= i then
c[i] = 0
i = i + 1
goto continue
end
c[i] = c[i] + 1
i = 1
odd = not odd
if s[0] ~= 0 then
if s[s[0]] ~= 0 then
f = flip()
else
f = 1
end
if f > maxflips then
maxflips = f
end
--C.printf("f = %d\n",f)
if odd then
checksum = checksum - f
else
checksum = checksum + f
end
end
::continue::
end
end
local terra doit(N : int)
maxflips = 0
odd = false
checksum = 0
max_n = N
if max_n < 3 or max_n > 15 then
C.printf("range: must be 3 <= n <= 12\n")
C.exit(1)
end
for i = 0,max_n do
s[i] = i
end
tk(max_n)
C.printf("%d\nPfannkuchen(%d) = %d\n", checksum, max_n, maxflips)
return checksum
end
local terra main(argc : int, v : &&int8)
if argc < 2 then
C.printf("usage: %s number\n", v[0])
C.exit(1);
end
doit(C.atoi(v[1]))
return 0
end
local test = require("test")
doit:compile()
print(test.time(function()
test.eq(doit(10),73196)
end))
local N = assert(tonumber((...) or 10))
doit(N)
if jit.os == "Windows" then
terralib.saveobj("benchmark_fannkuchredux.exe", { main = main })
os.execute("benchmark_fannkuchredux.exe "..N)
else
terralib.saveobj("benchmark_fannkuchredux", { main = main })
os.execute("./benchmark_fannkuchredux "..N)
end