Recursive Fibonacci in text WebAssembly
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
add a comment |
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
add a comment |
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
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
recursion webassembly
asked Nov 21 '18 at 6:34
Andrew BolyachevetsAndrew Bolyachevets
1215
1215
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)))
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 '18 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 '18 at 19:48
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)))
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 '18 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 '18 at 19:48
add a comment |
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)))
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 '18 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 '18 at 19:48
add a comment |
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)))
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)))
edited Nov 22 '18 at 19:43
answered Nov 21 '18 at 16:38
user633183user633183
68.2k21137175
68.2k21137175
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 '18 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 '18 at 19:48
add a comment |
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 19:48
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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