-
Notifications
You must be signed in to change notification settings - Fork 3
[WIP] feat(examples): AssemblyScript implementation of median algorithm #4
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
Draft
jdvlio
wants to merge
18
commits into
main
Choose a base branch
from
assemblyscript-median
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
48fb763
feat(examples): AssemblyScript implementation of median algorithm
jdvlio d7e90a5
fix(examples): properly treat input of even length
jdvlio 9d242ca
fix(examples): output should be in a memory buffer
jdvlio d90fb12
style(examples): semicolon usage now consistent
jdvlio eb5ca58
chore(examples): remove tests directory; add make files for convenience
jdvlio 07a3389
chore(examples/as-median): lift .gitignore up, include node_modules
PiDelport 8db3ba9
chore(examples): upgrade npm lockfile to version 2 format
jdvlio ac4dadb
chore(examples): separate median calculation into a new function
jdvlio e68cc07
chore(examples): remove redundant function, add comments
jdvlio a2d1668
fix(examples): more explicit use of types; added relevant casting
jdvlio f42badc
fix(examples): avoid overflow by leveraging distributivity
jdvlio 440d158
fix(examples): make casting explicit, as advised by compiler warning
jdvlio 4fd3d4e
chore(examples): add `assemblyscript-json` as dependency
jdvlio 518519f
chore(examples): move array sorting to `calc_median` function
jdvlio 1e3cbec
feat(examples): marshal data in/out in JSON format
jdvlio fe8f8dc
fix(examples): pass in memory buffer as pointer and length
jdvlio 1ac4812
fix(examples): use JSONEncoder; return mutable pointers
jdvlio f25ac6b
fix(examples): width of `out_buf_len` needs to be fixed
jdvlio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
|
|
||
| .PHONY: all clean | ||
|
|
||
| all: | ||
| make -C as-median build | ||
|
|
||
| clean: | ||
| make -C as-median $@ |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
|
|
||
| .PHONY: build clean | ||
|
|
||
| build: | ||
| npm run asbuild | ||
|
|
||
| clean: | ||
| rm -f build/*.wasm | ||
| rm -f build/*.wasm.map | ||
| rm -f build/*.wat |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "targets": { | ||
| "debug": { | ||
| "binaryFile": "build/untouched.wasm", | ||
| "textFile": "build/untouched.wat", | ||
| "sourceMap": true, | ||
| "debug": true | ||
| }, | ||
| "release": { | ||
| "binaryFile": "build/optimized.wasm", | ||
| "textFile": "build/optimized.wat", | ||
| "sourceMap": true, | ||
| "optimizeLevel": 3, | ||
| "shrinkLevel": 0, | ||
| "converge": false, | ||
| "noAssert": false | ||
| } | ||
| }, | ||
| "options": {} | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
|
|
||
| export function alloc(count: i32): usize { | ||
| /* allocate memory buffer */ | ||
| let buf = new Array<i32>(count); | ||
| let buf_ptr = memory.data(32); | ||
|
|
||
| /* | ||
| * dereference a pointer and mutate | ||
| * its underlying value | ||
| */ | ||
| store<Array<i32>>(buf_ptr, buf); | ||
|
|
||
| return buf_ptr | ||
| } | ||
|
|
||
| // note that a median can have a fractional part | ||
| export function exec(msg: usize, msg_len: usize, out_ptr: usize, out_size: usize): i32 { | ||
| /* error if msg size is not a multiple of 32 in bits */ | ||
| if (msg_len % 4 != 0) { return -1 } | ||
| let count = msg_len / 4; | ||
| let val = new Array<i32>(count as i32); | ||
|
|
||
| /* convert length to bits from octets */ | ||
| let len = (msg_len * 8) as i32; | ||
|
|
||
| for(let i = 0; i < len; i++) { | ||
| val[i] = load<i32>(msg + (i * 32)); | ||
| } | ||
| val.sort(); | ||
|
|
||
| /* | ||
| * in the following, `lmid` and `rmid` are the same | ||
| * whenever `val` has odd length. Note also that the | ||
| * average of 2 identical values is simply that value. | ||
| */ | ||
| let lmid = calc_mid(msg_len, 0); | ||
| let rmid = calc_mid(msg_len, 1); | ||
|
|
||
| let sum = (val[lmid as i32] + val[rmid as i32]) as f64; | ||
| let median = sum / 2.0; | ||
|
|
||
| /* create and populate the output buffer */ | ||
| out_size = 64 / 8; | ||
| out_ptr = memory.data(64); | ||
| store<f64>(out_ptr, median); | ||
jdvlio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return 0 | ||
| } | ||
|
|
||
| function calc_mid(len: usize, rhs: bool): usize { | ||
| if (len % 2 == 0) { | ||
| if (rhs) { | ||
| let rmid = len / 2 + 1; | ||
| return rmid; | ||
| } else { | ||
| let lmid = len / 2; | ||
| return lmid; | ||
| } | ||
| } else { | ||
| let mid = (len + 1) / 2; | ||
| return mid; | ||
| } | ||
| } | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "extends": "assemblyscript/std/assembly.json", | ||
| "include": [ | ||
| "./**/*.ts" | ||
| ] | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| *.wasm | ||
| *.wasm.map | ||
| *.asm.js |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const fs = require("fs"); | ||
| const loader = require("@assemblyscript/loader/umd"); | ||
| const imports = { /* imports go here */ }; | ||
| const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"), imports); | ||
| module.exports = wasmModule.exports; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "name": "as-median", | ||
| "version": "0.0.0", | ||
| "description": "Calculate median of a dataset", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "node tests", | ||
| "asbuild:untouched": "asc assembly/index.ts --target debug", | ||
| "asbuild:optimized": "asc assembly/index.ts --target release", | ||
| "asbuild": "npm run asbuild:untouched && npm run asbuild:optimized" | ||
| }, | ||
| "author": "ntls.io", | ||
| "license": "Apache-2.0", | ||
| "dependencies": { | ||
| "@assemblyscript/loader": "^0.19.22" | ||
| }, | ||
| "devDependencies": { | ||
| "assemblyscript": "^0.19.22" | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.