Skip to content

Commit

Permalink
Tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Sep 5, 2021
1 parent 0727b02 commit 2eb4786
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
half precision floating point for JavaScript<br>
See <a href="https://esdiscuss.org/topic/float16array">ES Discuss Float16Array topic</a>
See <a href="https://esdiscuss.org/topic/float16array">the archive of the ES Discuss Float16Array topic</a> for details
</p>

<p align="center">
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
"binary16",
"FP16",
"half-precision",
"ieee754",
"Float16Array",
"TypedArray",
"DataView",
"getFloat16",
"setFloat16",
"DataView",
"hfround"
"hfround",
"ponyfill",
"shim"
],
"scripts": {
"build": "yarn run build:lib; yarn run build:browser",
Expand Down
7 changes: 2 additions & 5 deletions src/helper/lib.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// algorithm: ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf
// algorithm: http://fox-toolkit.org/ftp/fasthalffloatconversion.pdf

const buffer = new ArrayBuffer(4);
const floatView = new Float32Array(buffer);
const uint32View = new Uint32Array(buffer);


const baseTable = new Uint32Array(512);
const shiftTable = new Uint32Array(512);

Expand Down Expand Up @@ -55,13 +54,11 @@ for (let i = 0; i < 256; ++i) {
*/
export function roundToFloat16Bits(num) {
floatView[0] = num;

const f = uint32View[0];
const e = (f >> 23) & 0x1ff;
return baseTable[e] + ((f & 0x007fffff) >> shiftTable[e]);
}


const mantissaTable = new Uint32Array(2048);
const exponentTable = new Uint32Array(64);
const offsetTable = new Uint32Array(64);
Expand All @@ -73,7 +70,7 @@ for (let i = 1; i < 1024; ++i) {

// normalized
while((m & 0x00800000) === 0) {
e -= 0x00800000; // decrement exponent
e -= 0x00800000; // decrement exponent
m <<= 1;
}

Expand Down
12 changes: 10 additions & 2 deletions src/helper/spec.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isObject } from "./is.mjs";

/**
* @see https://tc39.es/ecma262/#sec-tointegerorinfinity
* @param {unknown} target
* @returns {number}
*/
Expand All @@ -23,6 +24,7 @@ export function ToIntegerOrInfinity(target) {
}

/**
* @see https://tc39.es/ecma262/#sec-tolength
* @param {unknown} target
* @returns {number}
*/
Expand All @@ -36,6 +38,7 @@ function ToLength(target) {
}

/**
* @see https://tc39.es/ecma262/#sec-lengthofarraylike
* @param {object} arrayLike
* @returns {number}
*/
Expand All @@ -48,6 +51,7 @@ export function LengthOfArrayLike(arrayLike) {
}

/**
* @see https://tc39.es/ecma262/#sec-speciesconstructor
* @param {object} target
* @param {Function} defaultConstructor
* @returns {Function}
Expand All @@ -74,12 +78,15 @@ export function SpeciesConstructor(target, defaultConstructor) {
}

/**
* bigint comparisons are not supported
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort
* @param {number} x
* @param {number} y
* @returns {-1 | 0 | 1}
*/
export function defaultCompare(x, y) {
const [isNaN_x, isNaN_y] = [Number.isNaN(x), Number.isNaN(y)];
const isNaN_x = Number.isNaN(x);
const isNaN_y = Number.isNaN(y);

if (isNaN_x && isNaN_y) {
return 0;
Expand All @@ -102,7 +109,8 @@ export function defaultCompare(x, y) {
}

if (x === 0 && y === 0) {
const [isPlusZero_x, isPlusZero_y] = [Object.is(x, 0), Object.is(y, 0)];
const isPlusZero_x = Object.is(x, 0);
const isPlusZero_y = Object.is(y, 0);

if (!isPlusZero_x && isPlusZero_y) {
return -1;
Expand Down

0 comments on commit 2eb4786

Please sign in to comment.