blob: 7d34ead204b04f9f82369020fc525b9eb8b918a3 (
plain) (
blame)
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
|
// four kernels with and without o3; bench.sh runs each one on each backend
int csum(int lim) { int s = 0; for (int n = 1; n < lim; n++) { int x = n; while (x != 1) { if (x & 1) x = 3 * x + 1; else x = x / 2; s++; } } return s; }
o3 int csum3(int lim) { int s = 0; for (int n = 1; n < lim; n++) { int x = n; while (x != 1) { if (x & 1) x = 3 * x + 1; else x = x / 2; s++; } } return s; }
int sieve(fixed []int f) { int c = 0, n = len(f); for (int i = 0; i < n; i++) f[i] = 1; for (int i = 2; i < n; i++) if (f[i]) { c++; for (int j = i + i; j < n; j += i) f[j] = 0; } return c; }
o3 int sieve3(fixed []int f) { int c = 0, n = len(f); for (int i = 0; i < n; i++) f[i] = 1; for (int i = 2; i < n; i++) if (f[i]) { c++; for (int j = i + i; j < n; j += i) f[j] = 0; } return c; }
int digits(int n) { int s = 0; for (int i = 0; i < n; i++) { int x = i; while (x) { s = s + x % 10; x = x / 10; } } return s; }
o3 int digits3(int n) { int s = 0; for (int i = 0; i < n; i++) { int x = i; while (x) { s = s + x % 10; x = x / 10; } } return s; }
int fib(int n) { if (n < 2) return n; return fib(n - 1) + fib(n - 2); }
o3 int fib3(int n) { if (n < 2) return n; return fib3(n - 1) + fib3(n - 2); }
// input: a letter (the kernel; z runs none) and how many times to run it
fn main() {
fixed []int f = new(fixed []int, 50000);
let which = get_c(), reps = 0, ch = get_c(), r = 0;
while (ch >= '0' && ch <= '9') { reps = reps * 10 + ch - '0'; ch = get_c(); }
while (reps > 0) {
if (which == 'a') r = csum(30000);
if (which == 'b') r = csum3(30000);
if (which == 'c') r = sieve(f);
if (which == 'd') r = sieve3(f);
if (which == 'e') r = digits(300000);
if (which == 'f') r = digits3(300000);
if (which == 'g') r = fib(22);
if (which == 'h') r = fib3(22);
reps--;
}
put_n(r); put_c(10);
free(d*f);
return 0;
}
|