aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/examples/sieve.frt
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-19 12:15:28 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-19 12:15:28 +0200
commit67d25d837ac55f28a366c0a3b262e439a6e75fc3 (patch)
treedf7715c7724c5935ab87c807f3b8b4ef529315e3 /amforth-6.5/examples/sieve.frt
parente0d6784e89dba33226c0edb815bb974486fa7c48 (diff)
Add AmForth
Diffstat (limited to 'amforth-6.5/examples/sieve.frt')
-rw-r--r--amforth-6.5/examples/sieve.frt58
1 files changed, 58 insertions, 0 deletions
diff --git a/amforth-6.5/examples/sieve.frt b/amforth-6.5/examples/sieve.frt
new file mode 100644
index 0000000..13c45f2
--- /dev/null
+++ b/amforth-6.5/examples/sieve.frt
@@ -0,0 +1,58 @@
+\ sieve benchmark, modified version of
+\ marcel hendrix' sources. Uses single bits
+\ insted of whole bytes to store the is-prime
+\ marker cuts memory footprint to 1/8th.
+
+\ runtime: ATMega644 @ 16MHz 2,3 seconds per DO-PRIME
+
+marker _sieve_
+
+decimal
+
+1000 constant #times
+8192 constant size \ needs 1KB
+
+variable flags size 8 / allot
+
+\ highly un-optimized words
+: bit-addr ( addr bit -- eff-addr )
+ \ every byte has 8 bits. addr = addr + (bit >> 3)
+ 3 rshift ( -- addr off)
+ + ( -- eff-addr)
+;
+
+: bit? ( addr bit -- f )
+ swap over bit-addr swap ( -- eff-addr bit )
+ 7 and 1 swap lshift ( -- eff-addr bitmask)
+ swap c@ and ( -- f)
+;
+
+: bit-reset ( addr bit -- )
+ swap over bit-addr swap ( -- eff-addr bit )
+ 7 and 1 swap lshift ( -- eff-addr bitmask)
+ invert over c@ and swap c!
+;
+
+: 2drop drop drop ;
+
+: DO-PRIME flags [ size 8 / ] literal -1 fill
+ 0 size 0 do
+ flags i
+ bit? if
+ i 2* 3 +
+ dup i +
+ begin
+ dup
+ size u<
+ while
+ flags over bit-reset
+ over +
+ repeat
+ 2drop 1+
+ then
+ loop ;
+
+: primes cr #times u. ." iterations."
+ 0 #times 0 do drop DO-PRIME loop
+ cr . ." primes found, " ;
+