mirror of
https://github.com/remko/waforth
synced 2025-01-13 08:01:32 +01:00
Improve sieve-raw-wasm
This commit is contained in:
parent
64357b336f
commit
85efb6262e
1 changed files with 12 additions and 19 deletions
|
@ -1,5 +1,4 @@
|
|||
(module
|
||||
(import "js" "print" (func $print (param i32)))
|
||||
(module $sieve-raw
|
||||
(memory 8192)
|
||||
(func $sieve (export "sieve") (param $n i32) (result i32)
|
||||
(local $i i32)
|
||||
|
@ -17,29 +16,23 @@
|
|||
(local.set $i (i32.const 2))
|
||||
(block $endLoop
|
||||
(loop $loop
|
||||
(br_if $endLoop (i32.ge_s (i32.mul (local.get $i) (local.get $i))
|
||||
(local.get $n)))
|
||||
(br_if $endLoop (i32.ge_s (i32.mul (local.get $i) (local.get $i)) (local.get $n)))
|
||||
(if (i32.eq (i32.load8_s (local.get $i)) (i32.const 1))
|
||||
(then
|
||||
(local.set $j (i32.mul (local.get $i) (local.get $i)))
|
||||
(block $endInnerLoop
|
||||
(loop $innerLoop
|
||||
(i32.store8 (local.get $j) (i32.const 0))
|
||||
(local.set $j (i32.add (local.get $j) (local.get $i)))
|
||||
(br_if $endInnerLoop (i32.ge_s (local.get $j) (local.get $n)))
|
||||
(br $innerLoop)))))
|
||||
(loop $innerLoop
|
||||
(i32.store8 (local.get $j) (i32.const 0))
|
||||
(local.set $j (i32.add (local.get $j) (local.get $i)))
|
||||
(br_if $innerLoop (i32.lt_s (local.get $j) (local.get $n))))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br $loop)))
|
||||
|
||||
(local.set $i (i32.const 2))
|
||||
(block $endLoop
|
||||
(loop $loop
|
||||
(if (i32.eq (i32.load8_s (local.get $i)) (i32.const 1))
|
||||
(then
|
||||
;; (call $print (local.get $i))
|
||||
(local.set $last (local.get $i))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br_if $endLoop (i32.ge_s (local.get $i) (local.get $n)))
|
||||
(br $loop)))
|
||||
(loop $loop
|
||||
(if (i32.eq (i32.load8_s (local.get $i)) (i32.const 1))
|
||||
(then
|
||||
(local.set $last (local.get $i))))
|
||||
(local.set $i (i32.add (local.get $i) (i32.const 1)))
|
||||
(br_if $loop (i32.lt_s (local.get $i) (local.get $n))))
|
||||
|
||||
(return (local.get $last))))
|
||||
|
|
Loading…
Reference in a new issue