Recursive Fibonacci in text WebAssembly











up vote
0
down vote

favorite












I've been playing around with text WebAssembly and wanted to write a recursive Fibonacci number calculator.



I got a version that works but it uses a single branch if statement to check for the base case:



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


I tested this in wabt here: https://webassembly.github.io/wabt/demo/wat2wasm/



I tried to rewrite this using select conditional:



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


This compiles to .wasm, but it does not run as expected, just returning the base case. I tried similar versions with if-then-else, but to no avail. Why would the result of a single-branch if be any different from a two-branch conditional?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I've been playing around with text WebAssembly and wanted to write a recursive Fibonacci number calculator.



    I got a version that works but it uses a single branch if statement to check for the base case:



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


    I tested this in wabt here: https://webassembly.github.io/wabt/demo/wat2wasm/



    I tried to rewrite this using select conditional:



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


    This compiles to .wasm, but it does not run as expected, just returning the base case. I tried similar versions with if-then-else, but to no avail. Why would the result of a single-branch if be any different from a two-branch conditional?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I've been playing around with text WebAssembly and wanted to write a recursive Fibonacci number calculator.



      I got a version that works but it uses a single branch if statement to check for the base case:



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


      I tested this in wabt here: https://webassembly.github.io/wabt/demo/wat2wasm/



      I tried to rewrite this using select conditional:



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


      This compiles to .wasm, but it does not run as expected, just returning the base case. I tried similar versions with if-then-else, but to no avail. Why would the result of a single-branch if be any different from a two-branch conditional?










      share|improve this question













      I've been playing around with text WebAssembly and wanted to write a recursive Fibonacci number calculator.



      I got a version that works but it uses a single branch if statement to check for the base case:



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


      I tested this in wabt here: https://webassembly.github.io/wabt/demo/wat2wasm/



      I tried to rewrite this using select conditional:



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


      This compiles to .wasm, but it does not run as expected, just returning the base case. I tried similar versions with if-then-else, but to no avail. Why would the result of a single-branch if be any different from a two-branch conditional?







      recursion webassembly






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 6:34









      Andrew Bolyachevets

      1215




      1215
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You just gave me a reason to learn wasm. I can't yet answer your question about the single-branch if, but I can show you a working fib function.



          (module
          (func $fib2 (param $n i32) (param $a i32) (param $b i32) (result i32)
          (if (result i32)
          (i32.eqz (get_local $n))
          (then (get_local $a))
          (else (call $fib2 (i32.sub (get_local $n)
          (i32.const 1))
          (get_local $b)
          (i32.add (get_local $a)
          (get_local $b))))))

          (func $fib (param i32) (result i32)
          (call $fib2 (get_local 0)
          (i32.const 0) ;; seed value $a
          (i32.const 1))) ;; seed value $b

          (export "fib" (func $fib)))


          Copy/paste to demo it here



          const wasmInstance =
          new WebAssembly.Instance (wasmModule, {})

          const { fib } =
          wasmInstance.exports

          for (let x = 0; x < 10; x = x + 1)
          console.log (fib (x))


          Output



          0
          1
          1
          2
          3
          5
          8
          13
          21
          34


          For what it's worth, the implementation here is quite different than yours. Your program requires exponential computational time and space whereas the above program's requirements are linear.



          This is evident by examining your use of (call $fib ...). In your program, a single call to $fib has the possibility to spawn two additional calls to $fib, which each have the possibility to spawn two more calls to $fib, and on and on. Above $fib2 only has potential to call itself once, at most.





          Although it has a lesser performance, of course it is still possible to implement the exponential process



          (module
          (func $fib (param $n i32) (result i32)
          (if (result i32)
          (i32.lt_s (get_local $n)
          (i32.const 2))
          (then (get_local $n))
          ;; recursive branch spawns _two_ calls to $fib; not ideal
          (else (i32.add (call $fib (i32.sub (get_local $n)
          (i32.const 1)))
          (call $fib (i32.sub (get_local $n)
          (i32.const 2)))))))

          (export "fib" (func $fib)))





          share|improve this answer























          • Thank you for the working example. I wonder if the reason for failure to compile text WASM to bytecode WASM is some sort of compiler optimization that does not like the inefficient recursion. The reason for insisting on the brute force recursion was trying to make a toy transpiler to WASM. What confused me was the many ways in which conditionals can be written and how parenthesis could be applied to AST nodes.
            – Andrew Bolyachevets
            Nov 22 at 0:47












          • I found Writing WebAssembly By Hand to be very helpful. I also made an update to my answer. This is a neat little language. Thanks for sharing your question :D
            – user633183
            Nov 22 at 19:48













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406439%2frecursive-fibonacci-in-text-webassembly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You just gave me a reason to learn wasm. I can't yet answer your question about the single-branch if, but I can show you a working fib function.



          (module
          (func $fib2 (param $n i32) (param $a i32) (param $b i32) (result i32)
          (if (result i32)
          (i32.eqz (get_local $n))
          (then (get_local $a))
          (else (call $fib2 (i32.sub (get_local $n)
          (i32.const 1))
          (get_local $b)
          (i32.add (get_local $a)
          (get_local $b))))))

          (func $fib (param i32) (result i32)
          (call $fib2 (get_local 0)
          (i32.const 0) ;; seed value $a
          (i32.const 1))) ;; seed value $b

          (export "fib" (func $fib)))


          Copy/paste to demo it here



          const wasmInstance =
          new WebAssembly.Instance (wasmModule, {})

          const { fib } =
          wasmInstance.exports

          for (let x = 0; x < 10; x = x + 1)
          console.log (fib (x))


          Output



          0
          1
          1
          2
          3
          5
          8
          13
          21
          34


          For what it's worth, the implementation here is quite different than yours. Your program requires exponential computational time and space whereas the above program's requirements are linear.



          This is evident by examining your use of (call $fib ...). In your program, a single call to $fib has the possibility to spawn two additional calls to $fib, which each have the possibility to spawn two more calls to $fib, and on and on. Above $fib2 only has potential to call itself once, at most.





          Although it has a lesser performance, of course it is still possible to implement the exponential process



          (module
          (func $fib (param $n i32) (result i32)
          (if (result i32)
          (i32.lt_s (get_local $n)
          (i32.const 2))
          (then (get_local $n))
          ;; recursive branch spawns _two_ calls to $fib; not ideal
          (else (i32.add (call $fib (i32.sub (get_local $n)
          (i32.const 1)))
          (call $fib (i32.sub (get_local $n)
          (i32.const 2)))))))

          (export "fib" (func $fib)))





          share|improve this answer























          • Thank you for the working example. I wonder if the reason for failure to compile text WASM to bytecode WASM is some sort of compiler optimization that does not like the inefficient recursion. The reason for insisting on the brute force recursion was trying to make a toy transpiler to WASM. What confused me was the many ways in which conditionals can be written and how parenthesis could be applied to AST nodes.
            – Andrew Bolyachevets
            Nov 22 at 0:47












          • I found Writing WebAssembly By Hand to be very helpful. I also made an update to my answer. This is a neat little language. Thanks for sharing your question :D
            – user633183
            Nov 22 at 19:48

















          up vote
          0
          down vote



          accepted










          You just gave me a reason to learn wasm. I can't yet answer your question about the single-branch if, but I can show you a working fib function.



          (module
          (func $fib2 (param $n i32) (param $a i32) (param $b i32) (result i32)
          (if (result i32)
          (i32.eqz (get_local $n))
          (then (get_local $a))
          (else (call $fib2 (i32.sub (get_local $n)
          (i32.const 1))
          (get_local $b)
          (i32.add (get_local $a)
          (get_local $b))))))

          (func $fib (param i32) (result i32)
          (call $fib2 (get_local 0)
          (i32.const 0) ;; seed value $a
          (i32.const 1))) ;; seed value $b

          (export "fib" (func $fib)))


          Copy/paste to demo it here



          const wasmInstance =
          new WebAssembly.Instance (wasmModule, {})

          const { fib } =
          wasmInstance.exports

          for (let x = 0; x < 10; x = x + 1)
          console.log (fib (x))


          Output



          0
          1
          1
          2
          3
          5
          8
          13
          21
          34


          For what it's worth, the implementation here is quite different than yours. Your program requires exponential computational time and space whereas the above program's requirements are linear.



          This is evident by examining your use of (call $fib ...). In your program, a single call to $fib has the possibility to spawn two additional calls to $fib, which each have the possibility to spawn two more calls to $fib, and on and on. Above $fib2 only has potential to call itself once, at most.





          Although it has a lesser performance, of course it is still possible to implement the exponential process



          (module
          (func $fib (param $n i32) (result i32)
          (if (result i32)
          (i32.lt_s (get_local $n)
          (i32.const 2))
          (then (get_local $n))
          ;; recursive branch spawns _two_ calls to $fib; not ideal
          (else (i32.add (call $fib (i32.sub (get_local $n)
          (i32.const 1)))
          (call $fib (i32.sub (get_local $n)
          (i32.const 2)))))))

          (export "fib" (func $fib)))





          share|improve this answer























          • Thank you for the working example. I wonder if the reason for failure to compile text WASM to bytecode WASM is some sort of compiler optimization that does not like the inefficient recursion. The reason for insisting on the brute force recursion was trying to make a toy transpiler to WASM. What confused me was the many ways in which conditionals can be written and how parenthesis could be applied to AST nodes.
            – Andrew Bolyachevets
            Nov 22 at 0:47












          • I found Writing WebAssembly By Hand to be very helpful. I also made an update to my answer. This is a neat little language. Thanks for sharing your question :D
            – user633183
            Nov 22 at 19:48















          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You just gave me a reason to learn wasm. I can't yet answer your question about the single-branch if, but I can show you a working fib function.



          (module
          (func $fib2 (param $n i32) (param $a i32) (param $b i32) (result i32)
          (if (result i32)
          (i32.eqz (get_local $n))
          (then (get_local $a))
          (else (call $fib2 (i32.sub (get_local $n)
          (i32.const 1))
          (get_local $b)
          (i32.add (get_local $a)
          (get_local $b))))))

          (func $fib (param i32) (result i32)
          (call $fib2 (get_local 0)
          (i32.const 0) ;; seed value $a
          (i32.const 1))) ;; seed value $b

          (export "fib" (func $fib)))


          Copy/paste to demo it here



          const wasmInstance =
          new WebAssembly.Instance (wasmModule, {})

          const { fib } =
          wasmInstance.exports

          for (let x = 0; x < 10; x = x + 1)
          console.log (fib (x))


          Output



          0
          1
          1
          2
          3
          5
          8
          13
          21
          34


          For what it's worth, the implementation here is quite different than yours. Your program requires exponential computational time and space whereas the above program's requirements are linear.



          This is evident by examining your use of (call $fib ...). In your program, a single call to $fib has the possibility to spawn two additional calls to $fib, which each have the possibility to spawn two more calls to $fib, and on and on. Above $fib2 only has potential to call itself once, at most.





          Although it has a lesser performance, of course it is still possible to implement the exponential process



          (module
          (func $fib (param $n i32) (result i32)
          (if (result i32)
          (i32.lt_s (get_local $n)
          (i32.const 2))
          (then (get_local $n))
          ;; recursive branch spawns _two_ calls to $fib; not ideal
          (else (i32.add (call $fib (i32.sub (get_local $n)
          (i32.const 1)))
          (call $fib (i32.sub (get_local $n)
          (i32.const 2)))))))

          (export "fib" (func $fib)))





          share|improve this answer














          You just gave me a reason to learn wasm. I can't yet answer your question about the single-branch if, but I can show you a working fib function.



          (module
          (func $fib2 (param $n i32) (param $a i32) (param $b i32) (result i32)
          (if (result i32)
          (i32.eqz (get_local $n))
          (then (get_local $a))
          (else (call $fib2 (i32.sub (get_local $n)
          (i32.const 1))
          (get_local $b)
          (i32.add (get_local $a)
          (get_local $b))))))

          (func $fib (param i32) (result i32)
          (call $fib2 (get_local 0)
          (i32.const 0) ;; seed value $a
          (i32.const 1))) ;; seed value $b

          (export "fib" (func $fib)))


          Copy/paste to demo it here



          const wasmInstance =
          new WebAssembly.Instance (wasmModule, {})

          const { fib } =
          wasmInstance.exports

          for (let x = 0; x < 10; x = x + 1)
          console.log (fib (x))


          Output



          0
          1
          1
          2
          3
          5
          8
          13
          21
          34


          For what it's worth, the implementation here is quite different than yours. Your program requires exponential computational time and space whereas the above program's requirements are linear.



          This is evident by examining your use of (call $fib ...). In your program, a single call to $fib has the possibility to spawn two additional calls to $fib, which each have the possibility to spawn two more calls to $fib, and on and on. Above $fib2 only has potential to call itself once, at most.





          Although it has a lesser performance, of course it is still possible to implement the exponential process



          (module
          (func $fib (param $n i32) (result i32)
          (if (result i32)
          (i32.lt_s (get_local $n)
          (i32.const 2))
          (then (get_local $n))
          ;; recursive branch spawns _two_ calls to $fib; not ideal
          (else (i32.add (call $fib (i32.sub (get_local $n)
          (i32.const 1)))
          (call $fib (i32.sub (get_local $n)
          (i32.const 2)))))))

          (export "fib" (func $fib)))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 at 19:43

























          answered Nov 21 at 16:38









          user633183

          67.1k21133172




          67.1k21133172












          • Thank you for the working example. I wonder if the reason for failure to compile text WASM to bytecode WASM is some sort of compiler optimization that does not like the inefficient recursion. The reason for insisting on the brute force recursion was trying to make a toy transpiler to WASM. What confused me was the many ways in which conditionals can be written and how parenthesis could be applied to AST nodes.
            – Andrew Bolyachevets
            Nov 22 at 0:47












          • I found Writing WebAssembly By Hand to be very helpful. I also made an update to my answer. This is a neat little language. Thanks for sharing your question :D
            – user633183
            Nov 22 at 19:48




















          • Thank you for the working example. I wonder if the reason for failure to compile text WASM to bytecode WASM is some sort of compiler optimization that does not like the inefficient recursion. The reason for insisting on the brute force recursion was trying to make a toy transpiler to WASM. What confused me was the many ways in which conditionals can be written and how parenthesis could be applied to AST nodes.
            – Andrew Bolyachevets
            Nov 22 at 0:47












          • I found Writing WebAssembly By Hand to be very helpful. I also made an update to my answer. This is a neat little language. Thanks for sharing your question :D
            – user633183
            Nov 22 at 19:48


















          Thank you for the working example. I wonder if the reason for failure to compile text WASM to bytecode WASM is some sort of compiler optimization that does not like the inefficient recursion. The reason for insisting on the brute force recursion was trying to make a toy transpiler to WASM. What confused me was the many ways in which conditionals can be written and how parenthesis could be applied to AST nodes.
          – Andrew Bolyachevets
          Nov 22 at 0:47






          Thank you for the working example. I wonder if the reason for failure to compile text WASM to bytecode WASM is some sort of compiler optimization that does not like the inefficient recursion. The reason for insisting on the brute force recursion was trying to make a toy transpiler to WASM. What confused me was the many ways in which conditionals can be written and how parenthesis could be applied to AST nodes.
          – Andrew Bolyachevets
          Nov 22 at 0:47














          I found Writing WebAssembly By Hand to be very helpful. I also made an update to my answer. This is a neat little language. Thanks for sharing your question :D
          – user633183
          Nov 22 at 19:48






          I found Writing WebAssembly By Hand to be very helpful. I also made an update to my answer. This is a neat little language. Thanks for sharing your question :D
          – user633183
          Nov 22 at 19:48




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406439%2frecursive-fibonacci-in-text-webassembly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Fiat S.p.A.

          Type 'String' is not a subtype of type 'int' of 'index'