Skip to content

Commit

Permalink
Implement Math/Mathf.exp; Initial math test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 25, 2018
1 parent 70d2a0a commit e26734e
Show file tree
Hide file tree
Showing 12 changed files with 7,990 additions and 803 deletions.
2 changes: 1 addition & 1 deletion dist/asc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/asc.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/assemblyscript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/assemblyscript.js.map

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions scripts/hexfloat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<style>
html, body { margin: 0; }
body { font-family: sans-serif; border-top: 5px solid #0074C1; }
form { margin: 10px; }
label { cursor: pointer; }
</style>

<script src="hexfloat.js"></script>
<form onsubmit="convert(this); return false">
<h1>Hexadecimal float to decimal float converter</h1>
<p>
<label for="f64"><input id="f64" name="precision" value="f64" type="radio" checked /> f64</label>
<label for="f32"><input id="f32" name="precision" value="f32" type="radio" /> f32</label>
<input id="name" type="text" value="test(" />
<button>Convert</button>
</p>
<p><textarea cols="120" rows="20" id="input"></textarea></p>
<p><textarea cols="120" rows="20" id="output" readonly></textarea></p>
</form>

<script>
function convert(form) {
var isF64 = document.getElementById("f64").checked;
var name = document.getElementById("name").value;
var input = document.getElementById("input").value;
document.getElementById("output").value = input
.replace(/\b(\-?0x[0-9a-fA-F]*(?:\.[0-9a-fA-F]+)?[pP][+-]?[0-9]+\b)/g, ($0, $1) => {
var val = parse($1);
return val.toPrecision(isF64 ? 18 : 10);
})
.replace(/\bnan\b/g, "NaN")
.replace(/\inf\b/g, "Infinity")
.replace(/^T\(RN, */mg, name + "(")
.replace(/\)$/mg, ");")
.replace(/ +/g, " ");
}
</script>
82 changes: 82 additions & 0 deletions scripts/hexfloat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
MIT License
Copyright (c) 2017 Mauro Bringolf
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// see: https://github.com/maurobringolf/webassembly-floating-point-hex-parser
function parse(input) {
input = input.toUpperCase();
const splitIndex = input.indexOf('P');
let mantissa, exponent;

if (splitIndex !== -1) {
mantissa = input.substring(0, splitIndex);
exponent = parseInt(input.substring(splitIndex + 1));
} else {
mantissa = input;
exponent = 0;
}

const dotIndex = mantissa.indexOf('.');

if (dotIndex !== -1) {
let integerPart = parseInt(mantissa.substring(0,dotIndex), 16);
let sign = Math.sign(integerPart);
integerPart = sign * integerPart;
const fractionLength = mantissa.length - dotIndex - 1;
const fractionalPart = parseInt(mantissa.substring(dotIndex + 1), 16);
const fraction = fractionLength > 0 ? fractionalPart / Math.pow(16, fractionLength) : 0;
if (sign === 0) {
if (fraction === 0) {
mantissa = sign;
} else {
if (Object.is(sign, -0)) {
mantissa = - fraction;
} else {
mantissa = fraction;
}
}
} else {
mantissa = sign * (integerPart + fraction);
}
} else {
mantissa = parseInt(mantissa, 16);
}

return mantissa * (splitIndex !== -1 ? Math.pow(2, exponent) : 1);
}

if (typeof process !== "undefined") {
if (process.argv.length < 3) {
console.error("Usage: hexfloat 0x1p1023");
process.exit(1);
}

var output = parse(process.argv[2]);
var double = output.toPrecision(18); // 17
var single = output.toPrecision(10); // 9

console.log("<f64>" + double);
console.log("<f32>" + single);

if (!(parseFloat(double) === output)) throw Error("double precision error");
if (!(Math.fround(parseFloat(single)) === Math.fround(output))) throw Error("single precision error");
}
18 changes: 18 additions & 0 deletions src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,20 @@ export class Tokenizer extends DiagnosticEmitter {
}

readFloat(): f64 {
// var text = this.source.text;
// if (text.charCodeAt(this.pos) == CharCode._0 && this.pos + 2 < this.end) {
// switch (text.charCodeAt(this.pos + 1)) {
// case CharCode.X:
// case CharCode.x: {
// this.pos += 2;
// return this.readHexFloat();
// }
// }
// }
return this.readDecimalFloat();
}

readDecimalFloat(): f64 {
var start = this.pos;
var text = this.source.text;
while (this.pos < this.end && isDecimalDigit(text.charCodeAt(this.pos))) {
Expand Down Expand Up @@ -1300,6 +1314,10 @@ export class Tokenizer extends DiagnosticEmitter {
return parseFloat(text.substring(start, this.pos));
}

readHexFloat(): f64 {
throw new Error("not implemented"); // TBD
}

readUnicodeEscape(): string {
var remain = 4;
var value = 0;
Expand Down
2 changes: 2 additions & 0 deletions std/assembly.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ declare namespace Math {
export function abs(x: f64): f64;
export function ceil(x: f64): f64;
export function clz32(x: f64): i32;
export function exp(x: f64): f64;
export function floor(x: f64): f64;
export function fround(x: f64): f32;
export function imul(a: f64, b: f64): i32;
Expand All @@ -433,6 +434,7 @@ declare namespace Mathf {
export const SQRT1_2: f32;
export const SQRT2: f32;
export function abs(x: f32): f32;
export function exp(x: f32): f32;
export function ceil(x: f32): f32;
export function clz32(x: f32): i32;
export function floor(x: f32): f32;
Expand Down
Loading

0 comments on commit e26734e

Please sign in to comment.