Less prose; Update i64 example; More options for asc

This commit is contained in:
dcodeIO
2017-12-25 12:08:51 +01:00
parent 4baff99125
commit 5c4bf1af76
14 changed files with 322 additions and 82 deletions

View File

@ -1,15 +1,126 @@
i64 polyfill
============
@assemblyscript/i64
===================
An [AssemblyScript](http://assemblyscript.org) example. Exposes i64 operations to JS using 32-bit integers (low and high bits).
Exposes WebAssembly's i64 operations to JavaScript using 32-bit integers (low and high bits).
Instructions
------------
Usage
-----
```
$> npm install @assemblyscript/i64
```
```ts
import * as i64 from "@assemblyscript/i64";
i64.div(10, 0, 2, 0);
console.log("result: lo=" + i64.getLo() + ", hi=" + i64.getHi());
```
API
---
**Note** that `u32` is just an alias of `number` in JavaScript with values in 32-bit integer range.
* **getLo**(): `u32`<br />
Gets the low 32 bits of the computed 64-bit value.
* **getHi**(): `u32`<br />
Gets the high 32 bits of the computed 64-bit value.
* **clz**(loLeft: `u32`, hiLeft: `u32`): `void`<br />
Performs the sign-agnostic count leading zero bits operation. All zero bits are considered leading if the value is zero.
* **ctz**(loLeft: `u32`, hiLeft: `u32`): `void`<br />
Performs the sign-agnostic count tailing zero bits operation. All zero bits are considered trailing if the value is zero.
* **popcnt**(loLeft: `u32`, hiLeft: `u32`): `void`<br />
Performs the sign-agnostic count number of one bits operation.
* **eqz**(loLeft: `u32`, hiLeft: `u32`): `void`<br />
Performs the sign-agnostic equals-zero operation.
* **add**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic addition operation.
* **sub**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic subtraction operation.
* **mul**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic multiplication operation.
* **div_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed division operation.
* **div_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned division operation.
* **rem_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed remainder operation.
* **rem_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned remainder operation.
* **and**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic bitwise and operation.
* **or**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic bitwise or operation.
* **xor**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic bitwise xor operation.
* **shl**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic bitwise shift left operation.
* **shr_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed bitwise shift right operation.
* **shr_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned bitwise shift right operation.
* **rotl**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic rotate left operation.
* **rotr**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic rotate right operation.
* **eq**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic compare equal operation.
* **ne**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the sign-agnostic compare unequal operation.
* **lt_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed less than operation.
* **lt_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned less than operation.
* **le_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed less than or equal operation.
* **le_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned less than or equal operation.
* **gt_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed greater than operation.
* **gt_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned greater than operation.
* **ge_s**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the signed greater than or equal operation.
* **ge_u**(loLeft: `u32`, hiLeft: `u32`, loRight: `u32`, hiRight: `u32`): `void`<br />
Performs the unsigned greater than or equal operation.
Building
--------
To build [assembly/i64.ts](./assembly/i64.ts) to an untouched and an optimized `.wasm` including their respective `.wast` representations, run:
```
$> npm run build
```
Afterwards, `require` the node module as usual (CommonJS entry point is [index.js](./index.js)).

63
examples/i64-polyfill/index.d.ts vendored Normal file
View File

@ -0,0 +1,63 @@
type u32 = number;
/** Gets the low 32 bits of the computed 64-bit value. */
export function getLo(): u32;
/** Gets the high 32 bits of the computed 64-bit value. */
export function getHi(): u32;
/** Performs the sign-agnostic count leading zero bits operation. All zero bits are considered leading if the value is zero. */
export function clz(loLeft: u32, hiLeft: u32): void;
/** Performs the sign-agnostic count tailing zero bits operation. All zero bits are considered trailing if the value is zero. */
export function ctz(loLeft: u32, hiLeft: u32): void;
/** Performs the sign-agnostic count number of one bits operation. */
export function popcnt(loLeft: u32, hiLeft: u32): void;
/** Performs the sign-agnostic equals-zero operation. */
export function eqz(loLeft: u32, hiLeft: u32): void;
/** Performs the sign-agnostic addition operation. */
export function add(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic subtraction operation. */
export function sub(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic multiplication operation. */
export function mul(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed division operation. */
export function div_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned division operation. */
export function div_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed remainder operation. */
export function rem_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned remainder operation. */
export function rem_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic bitwise and operation. */
export function and(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic bitwise or operation. */
export function or(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic bitwise xor operation. */
export function xor(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic bitwise shift left operation. */
export function shl(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed bitwise shift right operation. */
export function shr_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned bitwise shift right operation. */
export function shr_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic rotate left operation. */
export function rotl(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic rotate right operation. */
export function rotr(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic compare equal operation. */
export function eq(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the sign-agnostic compare unequal operation. */
export function ne(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed less than operation. */
export function lt_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned less than operation. */
export function lt_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed less than or equal operation. */
export function le_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned less than or equal operation. */
export function le_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed greater than operation.*/
export function gt_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned greater than operation.*/
export function gt_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the signed greater than or equal operation. */
export function ge_s(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;
/** Performs the unsigned greater than or equal operation. */
export function ge_u(loLeft: u32, hiLeft: u32, loRight: u32, hiRight: u32): void;

View File

@ -1,10 +1,28 @@
{
"name": "@assemblyscript/i64-polyfill-example",
"name": "@assemblyscript/i64",
"version": "1.0.0",
"private": true,
"author": "Daniel Wirtz <dcode+assemblyscript@dcode.io>",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/AssemblyScript/assemblyscript.git"
},
"bugs": {
"url": "https://github.com/AssemblyScript/assemblyscript/issues"
},
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"build": "npm run build:untouched && npm run build:optimized",
"build:untouched": "asc assembly/i64.ts -t i64.untouched.wast -b i64.untouched.wasm --validate",
"build:optimized": "asc -O assembly/i64.ts -b i64.optimized.wasm -t i64.optimized.wast --validate"
}
"build:untouched": "asc assembly/i64.ts -t i64.untouched.wast -b i64.untouched.wasm --noMemory --validate",
"build:optimized": "asc -O assembly/i64.ts -b i64.optimized.wasm -t i64.optimized.wast --noMemory --validate",
"test": "node tests"
},
"files": [
"assembly/",
"i64.optimized.wasm",
"index.d.ts",
"index.js",
"README.md"
]
}

View File

@ -0,0 +1,40 @@
var assert = require("assert");
function assertUnary(op, loLeft, hiLeft, loResult, hiResult) {
op(loLeft, hiLeft);
assert.strictEqual(i64.getLo(), loResult);
assert.strictEqual(i64.getHi(), hiResult);
}
function assertBinary(op, loLeft, hiLeft, loRight, hiRight, loResult, hiResult) {
op(loLeft, hiLeft, loLeft, loRight);
assert.strictEqual(i64.getLo(), loResult);
assert.strictEqual(i64.getHi(), hiResult);
}
var i64 = require("..");
assertUnary(i64.clz, 1, 0, 63, 0);
assertUnary(i64.clz, 0, 1, 31, 0);
assertUnary(i64.clz, 1, 1, 31, 0);
assertUnary(i64.clz, 0, 0, 64, 0);
assertUnary(i64.ctz, 0, 0x80000000, 63, 0);
assertUnary(i64.ctz, 0x80000000, 0x80000000, 31, 0);
assertUnary(i64.ctz, 0, 1, 32, 0);
assertUnary(i64.ctz, 1, 0, 0, 0);
assertUnary(i64.ctz, 0, 0, 64, 0);
assertUnary(i64.popcnt, 0x55555555, 0x55555555, 32, 0);
assertUnary(i64.popcnt, -1, -1, 64, 0);
assertUnary(i64.popcnt, 0, 0, 0, 0);
assertUnary(i64.popcnt, 0x55, 0, 4, 0);
assertUnary(i64.popcnt, 0, 0x55, 4, 0);
assertUnary(i64.popcnt, 0x55, 0x55, 8, 0);
assertUnary(i64.eqz, 0, 0, 1, 0);
assertUnary(i64.eqz, 0, 1, 0, 0);
assertUnary(i64.eqz, 1, 0, 0, 0);
assertUnary(i64.eqz, 1, 1, 0, 0);
// TODO...