diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-01-26 13:42:39 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-01-26 13:42:39 -0800 |
commit | f5fe8742041429a6504ee6dc535aeb3d8550dd89 (patch) | |
tree | b3eda813075d28f44eb937b38aa2b78dbebc6a22 | |
parent | 498f9b6eb64b1bcc09ef24935172477bcd7d7b65 (diff) |
fix stack rewinding bug, and add life test and benchmark
-rw-r--r-- | src/analyzer.js | 3 | ||||
-rw-r--r-- | tests/life.c | 87 | ||||
-rwxr-xr-x | tests/runner.py | 60 |
3 files changed, 145 insertions, 5 deletions
diff --git a/src/analyzer.js b/src/analyzer.js index 60ef5ba8..1c53b76c 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -1517,9 +1517,8 @@ function analyzer(data, sidePass) { for (var i = 0; i < lines.length; i++) { var item = lines[i]; - if (!item.assignTo || item.intertype != 'alloca' || !isNumber(item.allocatedNum)) { + if (!finishedInitial && (!item.assignTo || item.intertype != 'alloca' || !isNumber(item.allocatedNum))) { finishedInitial = true; - continue; } if (item.intertype == 'alloca' && finishedInitial) { func.otherStackAllocations = true; diff --git a/tests/life.c b/tests/life.c new file mode 100644 index 00000000..08742f64 --- /dev/null +++ b/tests/life.c @@ -0,0 +1,87 @@ +// From http://rosettacode.org/wiki/Conway%27s_Game_of_Life#C + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#define for_x for (int x = 0; x < w; x++) +#define for_y for (int y = 0; y < h; y++) +#define for_xy for_x for_y + +void show(void *u, int w, int h) +{ + int (*univ)[w] = u; + for_x printf("-"); printf("\n"); + for_y { + for_x printf(univ[y][x] ? "[]" : " "); + printf("\n"); + } + for_x printf("-"); printf("\n"); + fflush(stdout); +} + +void evolve(void *u, int w, int h) +{ + unsigned (*univ)[w] = u; + unsigned new[h][w]; + + for_y for_x { + int n = 0; + for (int y1 = y - 1; y1 <= y + 1; y1++) + for (int x1 = x - 1; x1 <= x + 1; x1++) + if (univ[(y1 + h) % h][(x1 + w) % w]) + n++; + + if (univ[y][x]) n--; + new[y][x] = (n == 3 || (n == 2 && univ[y][x])); + } + for_y for_x univ[y][x] = new[y][x]; +} + +void nudge(void *u, int w, int h) +{ + unsigned (*univ)[w] = u; + int sum = 0; + for_xy sum += univ[y][x]; + while (sum < (w*h)/8) { + int x = sum & (w-1); + int y = (sum*sum) & (h-1); + univ[y][x] = 1; + sum++; + } +} + +void game(int w, int h, int i) +{ + unsigned univ[h][w]; + //for_xy univ[y][x] = rand() < RAND_MAX / 10 ? 1 : 0; + int acc = 0; // nonrandom generation, for benchmarking + for_xy { + acc += (x*17) % (y*3 + 1); + univ[y][x] = acc & 1; + } + while (i != 0) { + //show(univ, w, h); + evolve(univ, w, h); + if (i > 0) { + i--; + nudge(univ, w, h); // keep it interesting for benchmark + } else { + usleep(20000); + show(univ, w, h); + } + } + show(univ, w, h); +} + +int main(int c, char **v) +{ + int w = 0, h = 0, i = -1; // i = -1 means run forever, normal. otherwise, run in benchmark mode + if (c > 1) w = atoi(v[1]); + if (c > 2) h = atoi(v[2]); + if (c > 3) i = atoi(v[3]); + if (w <= 0) w = 32; + if (h <= 0) h = 32; + game(w, h, i); +} + diff --git a/tests/runner.py b/tests/runner.py index fd5b3358..1ed063b7 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -3047,6 +3047,22 @@ Exiting setjmp function, level: 0, prev_jmp: -1 ''' self.do_run(src, '.ok.\n') + def test_life(self): + if self.emcc_args is None: return self.skip('need c99') + self.emcc_args += ['-std=c99'] + src = open(path_from_root('tests', 'life.c'), 'r').read() + self.do_run(src, '''-------- + [][][] +[][] + [] + [] +[][] + + + +-------- +''', ['8', '8', '25000'], force_c=True) + def test_array2(self): src = ''' #include <stdio.h> @@ -10683,7 +10699,7 @@ elif 'benchmark' in str(sys.argv): print ' JavaScript: mean: %.3f (+-%.3f) secs median: %.3f range: %.3f-%.3f (noise: %3.3f%%) (%d runs)' % (mean, std, median, min(times), max(times), 100*std/mean, TEST_REPS) print ' Native : mean: %.3f (+-%.3f) secs median: %.3f range: %.3f-%.3f (noise: %3.3f%%) JS is %.2f X slower' % (mean_native, std_native, median_native, min(native_times), max(native_times), 100*std_native/mean_native, final) - def do_benchmark(self, name, src, args=[], expected_output='FAIL', emcc_args=[], native_args=[], force_c=False, reps=TEST_REPS): + def do_benchmark(self, name, src, args=[], expected_output='FAIL', emcc_args=[], native_args=[], shared_args=[], force_c=False, reps=TEST_REPS): dirname = self.get_dir() filename = os.path.join(dirname, name + '.c' + ('' if force_c else 'pp')) f = open(filename, 'w') @@ -10696,7 +10712,7 @@ elif 'benchmark' in str(sys.argv): '-O2', '-s', 'INLINING_LIMIT=0', '-s', 'DOUBLE_MODE=0', '-s', 'PRECISE_I64_MATH=0', '-s', 'ASM_JS=1',# '-s', 'USE_MATH_IMUL=1', '-s', 'TOTAL_MEMORY=128*1024*1024', '-s', 'FAST_MEMORY=10*1024*1024', - '-o', final_filename] + emcc_args, stdout=PIPE, stderr=self.stderr_redirect).communicate() + '-o', final_filename] + shared_args + emcc_args, stdout=PIPE, stderr=self.stderr_redirect).communicate() assert os.path.exists(final_filename), 'Failed to compile file: ' + output[0] # Run JS @@ -10715,7 +10731,7 @@ elif 'benchmark' in str(sys.argv): self.assertContained(expected_output, js_output) # Run natively - self.build_native(filename, native_args) + self.build_native(filename, shared_args + native_args) global total_native_times native_times = [] for i in range(reps): @@ -10910,6 +10926,44 @@ elif 'benchmark' in str(sys.argv): src = open(path_from_root('tests', 'skinning_test_no_simd.cpp'), 'r').read() self.do_benchmark('skinning', src, ['9500', '10000'], 'blah=0.000000') + def test_life(self): + src = open(path_from_root('tests', 'life.c'), 'r').read() + self.do_benchmark('life', src, ['32', '32', '7100'], '''-------------------------------- + + [][][][][] [] + [] + [] + [] + [][][][] [] + [][][][][] [] + [] [] [] + [][][] [] [] [][] +[][][] [][][] [] [][] [] +[][][] [][] [][] [][][] + [][][][][] [] + [][][] [] [] [] [] + [] [][] [] [][] [] [][] + [] [] [][][] [] [] + [][] [] [][] +[][] [] [] [][] [] +[][] [] [] +[][] [][] + [] [] + [][][] [][][] + [] [] [] + [][][][][] [][] + [][] [][] + [][] [] + [] [] + [] [] [][] [][] + [][] [][] + [] [] [] [] + [] [] + [] + [] [] +-------------------------------- +''', shared_args=['-std=c99'], force_c=True) + def test_dlmalloc(self): # XXX This seems to have regressed slightly with emcc. Are -g and the signs lines passed properly? src = open(path_from_root('system', 'lib', 'dlmalloc.c'), 'r').read() + '\n\n\n' + open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read() |