Skip to content

Commit

Permalink
Use ULP based comparison.
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceDai committed Nov 10, 2021
1 parent afee7d7 commit 98d76cb
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,26 @@ export function almostEqual(a, b, criteria) {
}
}

export function checkValue(
output, expected, criteria = opFp32AccuracyCriteria) {
function getBitwise(value) {
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
view.setFloat32(0, value);
return view.getInt32(0);
}

export function compareUlp(a, b, nulp = 1, format = "float32") {
assert(format === "float32", `Format ${format} is not supported.`);
const aBitwise = getBitwise(a);
const bBitwise = getBitwise(b);
let distance = aBitwise - bBitwise;
distance = distance > 0 ? distance : -distance;
return distance > nulp;
}

export function checkValue(output, expected) {
assert.isTrue(output.length === expected.length);
for (let i = 0; i < output.length; ++i) {
assert.isTrue(almostEqual(output[i], expected[i], criteria));
assert.isTrue(!compareUlp(output[i], expected[i]));
}
}

Expand Down

0 comments on commit 98d76cb

Please sign in to comment.