Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fibonacci.wast #20

Closed
larshp opened this issue Oct 27, 2020 · 3 comments
Closed

fibonacci.wast #20

larshp opened this issue Oct 27, 2020 · 3 comments

Comments

@larshp
Copy link
Member

larshp commented Oct 27, 2020

(module
  (func $fibonacci (param $i i32) (result i32)
    (local $a i32)
    (local $b i32)
    (local $tmp i32)
    (set_local $a (i32.const 1))
    (set_local $b (i32.const 0))
    (loop $done $loop
      (if (i32.le_s (get_local $i)(i32.const 1))(br $done)
        (block
          (set_local $tmp (get_local $a))
          (set_local $a (i32.add (get_local $a)(get_local $b)))
          (set_local $b (get_local $tmp))
          (set_local $i (i32.sub (get_local $i)(i32.const 1)))
        )
      )
      (br $loop)
    )
    (get_local $a)
  )
  (export "fib" $fibonacci)
)
@larshp
Copy link
Member Author

larshp commented Oct 27, 2020

(module
 (export "fib" (func $fib))
 (func $fib (param $n i32) (result i32)
  (if
   (i32.lt_s
    (get_local $n)
    (i32.const 2)
   )
   (return
    (i32.const 1)
   )
  )
  (return
   (i32.add
    (call $fib
     (i32.sub
      (get_local $n)
      (i32.const 2)
     )
    )
    (call $fib
     (i32.sub
      (get_local $n)
      (i32.const 1)
     )
    )
   )
  )
 )
)

@larshp
Copy link
Member Author

larshp commented Oct 27, 2020

(module
  (type $t0 (func (param i32) (result i32)))
  (func $fib (export "fib") (type $t0) (param $n i32) (result i32)
    get_local $n
    i32.const 2
    i32.lt_s
    if $I0
      i32.const 1
      return
    end
    get_local $n
    i32.const 2
    i32.sub
    call $fib
    get_local $n
    i32.const 1
    i32.sub
    call $fib
    i32.add
    return))

@larshp
Copy link
Member Author

larshp commented Oct 29, 2020

code added in e3949e1, closing

@larshp larshp closed this as completed Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant