diff --git a/src/Makefile b/src/Makefile index 43b6619334def..edcd9b269f588 100644 --- a/src/Makefile +++ b/src/Makefile @@ -573,8 +573,11 @@ TEST_XFAILS_RUSTC := $(addprefix test/run-pass/, \ while-type-error.rs \ wrong-ret-type.rs \ ), \ - $(wildcard test/*fail/*.rs test/*fail/*.rc)) - + $(wildcard test/*fail/*.rs test/*fail/*.rc)) \ + test/bench/shootout/fannkuchredux.rs \ + test/bench/shootout/fasta.rs \ + test/bench/shootout/binarytrees.rs \ + $(wildcard test/bench/99-bottles/*rs) ifdef MINGW_CROSS TEST_XFAILS_BOOT += test/run-pass/native-mod.rc @@ -585,8 +588,10 @@ TEST_XFAILS_BOOT += test/run-pass/native-mod.rc TEST_XFAILS_RUSTC += test/run-pass/native-mod.rc endif +BENCH_RS := $(wildcard test/bench/shootout/*rs) \ + $(wildcard test/bench/99-bottles/*rs) RPASS_RC := $(wildcard test/run-pass/*.rc) -RPASS_RS := $(wildcard test/run-pass/*.rs) +RPASS_RS := $(wildcard test/run-pass/*.rs) $(BENCH_RS) RFAIL_RC := $(wildcard test/run-fail/*.rc) RFAIL_RS := $(wildcard test/run-fail/*.rs) CFAIL_RC := $(wildcard test/compile-fail/*.rc) @@ -730,6 +735,18 @@ test/run-pass/%.out.tmp: test/run-pass/%$(CFG_EXE_SUFFIX) $(CFG_RUNTIME) @$(call CFG_ECHO, run: $<) $(CFG_QUIET)$(call CFG_RUN_TARG, $<) > $@ +test/bench/shootout/%.out.tmp: test/bench/shootout/%$(CFG_EXE_SUFFIX) \ + $(CFG_RUNTIME) + $(CFG_QUIET)rm -f $<.tmp + @$(call CFG_ECHO, run: $<) + $(CFG_QUIET)$(call CFG_RUN_TARG, $<) > $@ + +test/bench/99-bottles/%.out.tmp: test/bench/99-bottles/%$(CFG_EXE_SUFFIX) \ + $(CFG_RUNTIME) + $(CFG_QUIET)rm -f $<.tmp + @$(call CFG_ECHO, run: $<) + $(CFG_QUIET)$(call CFG_RUN_TARG, $<) > $@ + test/run-fail/%.out.tmp: test/run-fail/%$(CFG_EXE_SUFFIX) \ $(CFG_RUNTIME) $(CFG_QUIET)rm -f $<.tmp @@ -758,14 +775,13 @@ test/compile-fail/%.rustc.out.tmp: test/compile-fail/%.rs $(SREQ) $(CFG_QUIET)grep --text --quiet \ "$$(grep error-pattern $< | cut -d : -f 2- | tr -d '\n\r')" $@ -test/run-pass/%.boot$(CFG_EXE_SUFFIX): test/run-pass/%.rc $(BREQ) - @$(call CFG_ECHO, compile [boot]: $<) - $(BOOT) -o $@ $< +%.bc: %.rc $(SREQ) + @$(call CFG_ECHO, compile [rustc]: $<) + $(RUSTC) -o $@ $< -test/bench/shootout/%.boot$(CFG_EXE_SUFFIX): \ - test/bench/shootout/%.rs $(BREQ) - @$(call CFG_ECHO, compile [boot]: $<) - $(BOOT) -o $@ $< +%.bc: %.rs $(SREQ) + @$(call CFG_ECHO, compile [rustc]: $<) + $(RUSTC) -o $@ $< %.ll: %.bc @$(call CFG_ECHO, dis [llvm]: $<) @@ -788,26 +804,14 @@ test/bench/shootout/%.boot$(CFG_EXE_SUFFIX): \ @# programs, I\'ll live with the noise. -$(CFG_QUIET)$(DSYMUTIL) $@ -test/run-pass/%.bc: test/run-pass/%.rc $(SREQ) - @$(call CFG_ECHO, compile [rustc]: $<) - $(RUSTC) -o $@ $< - -test/run-pass/%.boot$(CFG_EXE_SUFFIX): test/run-pass/%.rs $(BREQ) +%.boot$(CFG_EXE_SUFFIX): %.rs $(BREQ) @$(call CFG_ECHO, compile [boot]: $<) $(BOOT) -o $@ $< -test/run-pass/%.bc: test/run-pass/%.rs $(SREQ) - @$(call CFG_ECHO, compile [rustc]: $<) - $(RUSTC) -o $@ $< - -test/run-fail/%.boot$(CFG_EXE_SUFFIX): test/run-fail/%.rs $(BREQ) +%.boot$(CFG_EXE_SUFFIX): %.rc $(BREQ) @$(call CFG_ECHO, compile [boot]: $<) $(BOOT) -o $@ $< -test/run-fail/%.bc: test/run-fail/%.rs $(SREQ) - @$(call CFG_ECHO, compile [rustc]: $<) - $(RUSTC) -o $@ $< - ###################################################################### # Auto-dependency diff --git a/src/README b/src/README index 05d701bd380f6..f3ce45857360d 100644 --- a/src/README +++ b/src/README @@ -24,5 +24,6 @@ test/ Testsuite (for both bootstrap and self-hosted) test/compile-fail - Tests that should fail to compile test/run-fail - Tests that should compile, run and fail test/run-pass - Tests that should compile, run and succeed +test/bench - Benchmarks and miscellanea Please be gentle, it's a work in progress. diff --git a/src/lib/_int.rs b/src/lib/_int.rs index ee660f013b226..ef1b3b668dee0 100644 --- a/src/lib/_int.rs +++ b/src/lib/_int.rs @@ -34,6 +34,23 @@ fn to_str(mutable int n, uint radix) -> str } } +fn pow(int base, uint exponent) -> int { + + if (exponent == 0u) { + ret 1; + } else if (base == 0) { + ret 0; + } else { + auto accum = base; + auto count = exponent; + while (count > 1u) { + accum *= base; + count -= 1u; + } + ret accum; + } +} + // Local Variables: // mode: rust; // fill-column: 78; diff --git a/src/test/bench/99-bottles/Makefile b/src/test/bench/99-bottles/Makefile deleted file mode 100644 index 8d1d27f99f839..0000000000000 --- a/src/test/bench/99-bottles/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -RC:=../../../rustboot -RFLAGS:=-L ../../.. -TARGETS:= 99bob-simple 99bob-iter 99bob-tail 99bob-pattern -TARGET_X86:=$(addsuffix .x86,$(TARGETS)) -TARGET_LLVM:=$(addsuffix .llvm,$(TARGETS)) - -all : x86s llvms - -clean: - rm $(TARGET_X86) $(TARGET_LLVM) - -x86s : $(TARGET_X86) - -llvms: $(TARGET_LLVM) - -%.x86 : %.rs - $(RC) $(RFLAGS) $^ -o $@ - -%.llvm : %.rs - $(RC) $(RFLAGS) -llvm $^ -o $@ \ No newline at end of file diff --git a/src/test/bench/99-bottles/r.sh b/src/test/bench/99-bottles/r.sh deleted file mode 100755 index 9da274e41f905..0000000000000 --- a/src/test/bench/99-bottles/r.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -make -k $1.x86 -DYLD_LIBRARY_PATH=../../.. ./$1.x86 diff --git a/src/test/bench/shootout/binary-trees.rs b/src/test/bench/shootout/binary-trees.rs deleted file mode 100644 index 669cd809915df..0000000000000 --- a/src/test/bench/shootout/binary-trees.rs +++ /dev/null @@ -1,18 +0,0 @@ -tag tree { - nil(); - node(@tree, @tree, int); -} - -fn item_check(&tree t) -> int { - alt (t) { - case (nil()) { - ret 0; - } - case (node(@tree left, @tree right, int item)) { - ret item + item_check(left) - item_check(right); - } - } -} - -fn main() { -} \ No newline at end of file diff --git a/src/test/bench/shootout/binarytrees.rs b/src/test/bench/shootout/binarytrees.rs new file mode 100644 index 0000000000000..92bedb8b9e64d --- /dev/null +++ b/src/test/bench/shootout/binarytrees.rs @@ -0,0 +1,74 @@ +use std; + +import std._int; + +tag tree { + nil; + node(@tree, @tree, int); +} + +fn item_check(@tree t) -> int { + alt (*t) { + case (nil) { + ret 0; + } + case (node(?left, ?right, ?item)) { + ret item + item_check(left) - item_check(right); + } + } +} + +fn bottom_up_tree(int item, int depth) -> @tree{ + if (depth > 0) { + ret @node(bottom_up_tree(2 * item - 1, depth - 1), + bottom_up_tree(2 * item, depth - 1), + item); + } else { + ret @nil; + } +} + +fn main() { + + auto n = 8; + auto min_depth = 4; + auto max_depth; + if (min_depth + 2 > n) { + max_depth = min_depth + 2; + } else { + max_depth = n; + } + + auto stretch_depth = max_depth + 1; + + auto stretch_tree = bottom_up_tree(0, stretch_depth); + log #fmt("stretch tree of depth %d\t check: %d", + stretch_depth, item_check(stretch_tree)); + + auto long_lived_tree = bottom_up_tree(0, max_depth); + + auto depth = min_depth; + while (depth <= max_depth) { + auto iterations = _int.pow(2, (max_depth - depth + min_depth) as uint); + auto chk = 0; + + auto i = 1; + while (i <= iterations) { + auto temp_tree = bottom_up_tree(i, depth); + chk += item_check(temp_tree); + + temp_tree = bottom_up_tree(-i, depth); + chk += item_check(temp_tree); + + i += 1; + } + + log #fmt("%d\t trees of depth %d\t check: %d", + iterations * 2, depth, chk); + + depth += 2; + } + + log #fmt("long lived trees of depth %d\t check: %d", + max_depth, item_check(long_lived_tree)); +} \ No newline at end of file diff --git a/src/test/bench/shootout/fannkuchredux.rs b/src/test/bench/shootout/fannkuchredux.rs new file mode 100644 index 0000000000000..2d44067bec33c --- /dev/null +++ b/src/test/bench/shootout/fannkuchredux.rs @@ -0,0 +1,99 @@ +// Based on Isaac Gouy's fannkuchredux.csharp + +use std; + +import std._int; +import std._vec; + +impure fn fannkuch(int n) -> int { + + fn perm1init(uint i) -> mutable int { + ret i as int; + } + auto perm1init_ = perm1init; // Rustboot workaround + + auto perm = _vec.init_elt[mutable int](0, n as uint); + auto perm1 = _vec.init_fn[mutable int](perm1init_, n as uint); + auto count = _vec.init_elt[mutable int](0, n as uint); + + auto f = 0; + auto i = 0; + auto k = 0; + auto r = 0; + auto flips = 0; + auto nperm = 0; + auto checksum = 0; + + r = n; + while (r > 0) { + i = 0; + + while (r != 1) { + count.(r - 1) = r; + r -=1; + } + + while (i < n) { + perm.(i) = perm1.(i); + i += 1; + } + + // Count flips and update max and checksum + f = 0; + k = perm.(0); + while (k != 0) { + i = 0; + while (2 * i < k) { + auto t = perm.(i); + perm.(i) = perm.(k - i); + perm.(k - i) = t; + i += 1; + } + k = perm.(0); + f += 1; + } + + if (f > flips) { + flips = f; + } + + if ((nperm & 0x1) == 0) { + checksum += f; + } else { + checksum -= f; + } + + // Use incremental change to generate another permutation + auto go = true; + while (go) { + if (r == n) { + log checksum; + ret flips; + } + auto p0 = perm1.(0); + i = 0; + while (i < r) { + auto j = i + 1; + perm1.(i) = perm1.(j); + i = j; + } + perm1.(r) = p0; + + count.(r) -= 1; + if (count.(r) > 0) { + go = false; + } else { + r += 1; + } + } + + nperm += 1; + } + + ret flips; +} + +impure fn main(vec[str] args) { + auto n = 7; + log #fmt("Pfannkuchen(%d) = %d", n, fannkuch(n)); +} \ No newline at end of file diff --git a/src/test/run-pass/lib-int.rs b/src/test/run-pass/lib-int.rs index 153c368338134..2e85abf6509f2 100644 --- a/src/test/run-pass/lib-int.rs +++ b/src/test/run-pass/lib-int.rs @@ -11,6 +11,17 @@ fn test_to_str() { check (eq(_int.to_str(100, 10u), "100")); } +fn test_pow() { + check (_int.pow(0, 0u) == 1); + check (_int.pow(0, 1u) == 0); + check (_int.pow(0, 2u) == 0); + check (_int.pow(-1, 0u) == -1); + check (_int.pow(1, 0u) == 1); + check (_int.pow(-3, 2u) == 9); + check (_int.pow(-3, 3u) == -27); + check (_int.pow(4, 9u) == 262144); +} + fn main() { test_to_str(); }