aboutsummaryrefslogtreecommitdiff
path: root/test/collatz.ssa
diff options
context:
space:
mode:
authorQuentin Carbonneaux <[email protected]>2016-03-27 15:00:45 -0400
committerQuentin Carbonneaux <[email protected]>2016-03-27 15:00:45 -0400
commit36635660b40573f6a0c19d50dfdd9277589030de (patch)
treea1b4463f7260462c314bd8d7cb35b3d199143f33 /test/collatz.ssa
parentaad52241c88ad5327a8488c66dc906c8393c9c92 (diff)
extract tests out of src
Diffstat (limited to 'test/collatz.ssa')
-rw-r--r--test/collatz.ssa61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/collatz.ssa b/test/collatz.ssa
new file mode 100644
index 0000000..373ecac
--- /dev/null
+++ b/test/collatz.ssa
@@ -0,0 +1,61 @@
+# a solution for N=1000 to
+# https://projecteuler.net/problem=14
+# we use a fast local array to
+# memoize small collatz numbers
+
+function $test() {
+@start
+ %mem =l alloc4 4000
+@loop
+ %n =w phi @start 1, @newm %n9, @oldm %n9
+ %cmax =w phi @start 0, @newm %c, @oldm %cmax
+ %fin =w csltw %n, 1000
+ jnz %fin, @cloop, @end
+@cloop
+ %n0 =w phi @loop %n, @odd %n2, @even %n3
+ %c0 =w phi @loop 0, @odd %c1, @even %c1
+ %no1 =w cnew %n0, 1
+ jnz %no1, @iter0, @endcl
+@iter0
+ %ism =w csltw %n0, %n
+ jnz %ism, @getmemo, @iter1
+@iter1
+ %c1 =w add %c0, 1
+ %p =w and %n0, 1
+ jnz %p, @odd, @even
+@odd
+ %n1 =w mul 3, %n0
+ %n2 =w add %n1, 1
+ jmp @cloop
+@even
+ %n3 =w div %n0, 2
+ jmp @cloop
+@getmemo # get the count for n0 in mem
+ %n0l =l extsw %n0
+ %idx0 =l mul %n0l, 4
+ %loc0 =l add %idx0, %mem
+ %cn0 =w loadw %loc0
+ %c2 =w add %c0, %cn0
+@endcl # store the count for n in mem
+ %c =w phi @getmemo %c2, @cloop %c0
+ %nl =l extsw %n
+ %idx1 =l mul %nl, 4
+ %loc1 =l add %idx1, %mem
+ storew %c, %loc1
+ %n9 =w add 1, %n
+ %big =w cslew %cmax, %c
+ jnz %big, @newm, @oldm
+@newm
+ jmp @loop
+@oldm
+ jmp @loop
+@end
+ storew %cmax, $a
+ ret
+}
+
+# >>> driver
+# extern void test(void);
+# int a;
+# int main() { test(); return !(a == 178); }
+# <<<