Last active
July 1, 2016 15:32
-
-
Save yurydelendik/cdabbb03d58140b0139c6c01824ea8f2 to your computer and use it in GitHub Desktop.
sqrt of squares sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test | |
<script> | |
var bin = atob( | |
"AGFzbQsAAAAEdHlwZYqAgIAAAkABAQEBQAABBAZpbXBvcnSPgICAAAEAA2VudghjYWxsYmFjawhmdW5jdGlvboKAgIAAAQEGbWVtb3J5g4CAgAAICAEGZXhwb3J0jYCAgAABAApzcXJ0T2ZTcXJzBGNvZGW5gICAAAGzgICAAAEBBAESAAAAAAAAAAAVAAIUABAAGAEAEABNBwEBFAAQAC0DABAALQMAi4kVAAYAAA+WDwRuYW1lj4CAgAABDHNxcnRfb2Zfc3FycwA="); | |
var ar = new Uint8Array(Array.prototype.map.call(bin, (ch) => ch.charCodeAt(0))); | |
var input = [3, 4]; | |
var env = {env: { callback: (resultPtr) => { | |
resultPtr |= 0; | |
if (input.length === 0) return 0|0; | |
var data = +input.shift(); | |
memory.setFloat64(resultPtr, data, true); | |
return 1|0; | |
} } }; | |
var m = Wasm.instantiateModule(ar, env); | |
var memory = new DataView(m.exports.memory); | |
var result = m.exports.sqrtOfSqrs(); | |
console.log(result); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "callback" as $callback from "env" typeof function (i32) : (i32); | |
export $sqrt_of_sqrs as "sqrtOfSqrs"; | |
export memory as "memory"; | |
function $sqrt_of_sqrs () : (f64) { | |
var $sum: f64; | |
$sum = 0.0; | |
f64.sqrt(loop $l { | |
br_if (call_import $callback (0) == 0, $sum) $exit; | |
$sum = $sum + f64[0] * f64[0]; | |
br $l; | |
$exit: | |
}) | |
} | |
memory 8, 8 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(module | |
(import $callback "env" "callback" (param i32) (result i32)) | |
(memory 8 8) | |
(export "sqrtOfSqrs" $sqrt_of_sqrs) | |
(export "memory" memory) | |
(func $sqrt_of_sqrs (result f64) | |
(local $sum f64) | |
(set_local $sum (f64.const 0.0)) | |
(f64.sqrt | |
(loop $exit $l | |
(br_if $exit (get_local $sum) (i32.eq (call_import $callback (i32.const 0)) (i32.const 0))) | |
(set_local $sum (f64.add (get_local $sum) (f64.mul (f64.load (i32.const 0)) (f64.load (i32.const 0))))) | |
(br $l) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment