@petamoriken/float16
Advanced tools
Comparing version 3.8.1 to 3.8.2
@@ -1,2 +0,2 @@ | ||
/*! @petamoriken/float16 v3.8.1 | MIT License - https://github.com/petamoriken/float16 */ | ||
/*! @petamoriken/float16 v3.8.2 | MIT License - https://github.com/petamoriken/float16 */ | ||
@@ -105,3 +105,10 @@ var float16 = (function (exports) { | ||
const ArrayPrototypeSymbolIterator = uncurryThis(NativeArrayPrototypeSymbolIterator); | ||
const MathTrunc = Math.trunc; | ||
const { | ||
abs: MathAbs, | ||
floor: MathFloor, | ||
log2: MathLog2, | ||
pow: MathPow, | ||
sign: MathSign, | ||
trunc: MathTrunc, | ||
} = Math; | ||
const NativeArrayBuffer = ArrayBuffer; | ||
@@ -321,49 +328,54 @@ const ArrayBufferIsView = NativeArrayBuffer.isView; | ||
const buffer = new NativeArrayBuffer(4); | ||
const floatView = new NativeFloat32Array(buffer); | ||
const uint32View = new NativeUint32Array(buffer); | ||
const baseTable = new NativeUint32Array(512); | ||
const shiftTable = new NativeUint32Array(512); | ||
for (let i = 0; i < 256; ++i) { | ||
const e = i - 127; | ||
if (e < -27) { | ||
baseTable[i] = 0x0000; | ||
baseTable[i | 0x100] = 0x8000; | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; | ||
} else if (e < -14) { | ||
baseTable[i] = 0x0400 >> (-e - 14); | ||
baseTable[i | 0x100] = (0x0400 >> (-e - 14)) | 0x8000; | ||
shiftTable[i] = -e - 1; | ||
shiftTable[i | 0x100] = -e - 1; | ||
} else if (e <= 15) { | ||
baseTable[i] = (e + 15) << 10; | ||
baseTable[i | 0x100] = ((e + 15) << 10) | 0x8000; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; | ||
} else if (e < 128) { | ||
baseTable[i] = 0x7c00; | ||
baseTable[i | 0x100] = 0xfc00; | ||
shiftTable[i] = 24; | ||
shiftTable[i | 0x100] = 24; | ||
} else { | ||
baseTable[i] = 0x7c00; | ||
baseTable[i | 0x100] = 0xfc00; | ||
shiftTable[i] = 13; | ||
shiftTable[i | 0x100] = 13; | ||
function roundTiesToEven(num) { | ||
const truncated = MathTrunc(num); | ||
const isOdd = truncated % 2 !== 0; | ||
const delta = MathAbs(num - truncated); | ||
if (delta > 0.5 || delta === 0.5 && isOdd) { | ||
return truncated + MathSign(num); | ||
} | ||
return truncated; | ||
} | ||
const f16EMax = 31; | ||
const f16EBias = 15; | ||
const f16MLen = 10; | ||
const f16MMask = 0x3ff; | ||
function roundToFloat16Bits(num) { | ||
floatView[0] = (num); | ||
const f = uint32View[0]; | ||
const e = (f >> 23) & 0x1ff; | ||
return baseTable[e] + ((f & 0x007fffff) >> shiftTable[e]); | ||
const absNum = MathAbs( (num)); | ||
const s = (num) < 0 || ObjectIs(num, -0) ? 1 : 0; | ||
let m, e; | ||
if (!NumberIsFinite(absNum)) { | ||
m = NumberIsNaN(absNum) ? 0x200 : 0; | ||
e = f16EMax; | ||
} else { | ||
let rawE = MathFloor(MathLog2(absNum)); | ||
let c = MathPow(2, -rawE); | ||
if (absNum * c < 1) { | ||
--rawE; | ||
c *= 2; | ||
} | ||
if (absNum * c >= 2) { | ||
++rawE; | ||
c /= 2; | ||
} | ||
if (rawE + f16EBias >= f16EMax) { | ||
m = 0; | ||
e = f16EMax; | ||
} else if (rawE + f16EBias >= 1) { | ||
m = roundTiesToEven(((absNum * c) - 1) * 0x400) & f16MMask; | ||
e = rawE + f16EBias; | ||
} else { | ||
m = roundTiesToEven(absNum * 0x1000000) & f16MMask; | ||
e = 0; | ||
} | ||
} | ||
return s << 15 | e << f16MLen | m; | ||
} | ||
const buffer = new NativeArrayBuffer(4); | ||
const floatView = new NativeFloat32Array(buffer); | ||
const uint32View = new NativeUint32Array(buffer); | ||
const mantissaTable = new NativeUint32Array(2048); | ||
const exponentTable = new NativeUint32Array(64); | ||
const offsetTable = new NativeUint32Array(64); | ||
for (let i = 1; i < 1024; ++i) { | ||
let m = i << 13; | ||
let e = 0; | ||
while((m & 0x00800000) === 0) { | ||
while ((m & 0x00800000) === 0) { | ||
m <<= 1; | ||
@@ -379,2 +391,3 @@ e -= 0x00800000; | ||
} | ||
const exponentTable = new NativeUint32Array(64); | ||
for (let i = 1; i < 31; ++i) { | ||
@@ -389,2 +402,3 @@ exponentTable[i] = i << 23; | ||
exponentTable[63] = 0xc7800000; | ||
const offsetTable = new NativeUint16Array(64); | ||
for (let i = 1; i < 64; ++i) { | ||
@@ -396,4 +410,4 @@ if (i !== 32) { | ||
function convertToNumber(float16bits) { | ||
const m = float16bits >> 10; | ||
uint32View[0] = mantissaTable[offsetTable[m] + (float16bits & 0x3ff)] + exponentTable[m]; | ||
const i = float16bits >> 10; | ||
uint32View[0] = mantissaTable[offsetTable[i] + (float16bits & 0x3ff)] + exponentTable[i]; | ||
return floatView[0]; | ||
@@ -400,0 +414,0 @@ } |
{ | ||
"name": "@petamoriken/float16", | ||
"version": "3.8.1", | ||
"version": "3.8.2", | ||
"description": "IEEE 754 half-precision floating-point for JavaScript", | ||
@@ -76,12 +76,12 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/cli": "^7.21.5", | ||
"@babel/core": "^7.21.8", | ||
"@babel/plugin-transform-modules-commonjs": "^7.21.5", | ||
"@types/nightwatch": "^2.3.23", | ||
"@babel/cli": "^7.22.9", | ||
"@babel/core": "^7.22.9", | ||
"@babel/plugin-transform-modules-commonjs": "^7.22.5", | ||
"@types/nightwatch": "^2.3.24", | ||
"babel-plugin-replace-import-extension": "^1.1.3", | ||
"browserslist": "^4.21.5", | ||
"concurrently": "^8.0.1", | ||
"eslint": "^8.41.0", | ||
"browserslist": "^4.21.9", | ||
"concurrently": "^8.2.0", | ||
"eslint": "^8.45.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-jsdoc": "^44.2.5", | ||
"eslint-plugin-jsdoc": "^46.4.5", | ||
"espower-cli": "^1.1.0", | ||
@@ -93,7 +93,7 @@ "espower-loader": "^1.2.2", | ||
"mocha": "^10.2.0", | ||
"nightwatch": "^2.6.21", | ||
"nightwatch-saucelabs-endsauce": "^2.1.4", | ||
"nightwatch": "^3.1.1", | ||
"nightwatch-saucelabs-endsauce": "^2.3.1", | ||
"nyc": "^15.1.0", | ||
"power-assert": "^1.4.2", | ||
"rollup": "^3.23.0", | ||
"rollup": "^3.26.3", | ||
"rollup-plugin-cleanup": "^3.2.1", | ||
@@ -100,0 +100,0 @@ "source-map-support": "^0.5.21" |
@@ -35,3 +35,3 @@ # <a href="https://github.com/petamoriken/float16">float16</a> | ||
<a href="https://saucelabs.com/u/petamoriken"> | ||
<img src="https://saucelabs.com/browser-matrix/petamoriken.svg" alt="Sauce Labs browser matrix"> | ||
<img src="https://app.saucelabs.com/browser-matrix/petamoriken.svg" alt="Sauce Labs browser matrix"> | ||
</a> | ||
@@ -181,3 +181,3 @@ </p> | ||
for (const value of array) { | ||
// 1, 1.099609375, 1.19921875, 1.2998046875 | ||
// 1, 1.099609375, 1.2001953125, 1.2998046875 | ||
console.log(value); | ||
@@ -184,0 +184,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
236112
6127