Socket
Socket
Sign inDemoInstall

@petamoriken/float16

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@petamoriken/float16 - npm Package Compare versions

Comparing version 3.5.11 to 3.6.0

lib/_util/brand.cjs

310

browser/float16.js

@@ -1,2 +0,2 @@

/*! @petamoriken/float16 v3.5.11 | MIT License - https://git.io/float16 */
/*! @petamoriken/float16 v3.6.0 | MIT License - https://git.io/float16 */

@@ -141,119 +141,2 @@ const float16 = (function (exports) {

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 roundToFloat16Bits(num) {
floatView[0] = (num);
const f = uint32View[0];
const e = (f >> 23) & 0x1ff;
return baseTable[e] + ((f & 0x007fffff) >> shiftTable[e]);
}
const mantissaTable = new NativeUint32Array(2048);
const exponentTable = new NativeUint32Array(64);
const offsetTable = new NativeUint32Array(64);
mantissaTable[0] = 0;
for (let i = 1; i < 1024; ++i) {
let m = i << 13;
let e = 0;
while((m & 0x00800000) === 0) {
e -= 0x00800000;
m <<= 1;
}
m &= ~0x00800000;
e += 0x38800000;
mantissaTable[i] = m | e;
}
for (let i = 1024; i < 2048; ++i) {
mantissaTable[i] = 0x38000000 + ((i - 1024) << 13);
}
exponentTable[0] = 0;
for (let i = 1; i < 31; ++i) {
exponentTable[i] = i << 23;
}
exponentTable[31] = 0x47800000;
exponentTable[32] = 0x80000000;
for (let i = 33; i < 63; ++i) {
exponentTable[i] = 0x80000000 + ((i - 32) << 23);
}
exponentTable[63] = 0xc7800000;
offsetTable[0] = 0;
for (let i = 1; i < 64; ++i) {
if (i === 32) {
offsetTable[i] = 0;
} else {
offsetTable[i] = 1024;
}
}
function convertToNumber(float16bits) {
const m = float16bits >> 10;
uint32View[0] = mantissaTable[offsetTable[m] + (float16bits & 0x3ff)] + exponentTable[m];
return floatView[0];
}
const THIS_IS_NOT_AN_OBJECT = "This is not an object";
const THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT = "This is not a Float16Array object";
const THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY =
"This constructor is not a subclass of Float16Array";
const THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT =
"The constructor property value is not an object";
const SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT =
"Species constructor didn't return TypedArray object";
const DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH =
"Derived constructor created TypedArray object which was too small length";
const ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER =
"Attempting to access detached ArrayBuffer";
const CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT =
"Cannot convert undefined or null to object";
const CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER =
"Cannot convert a BigInt value to a number";
const CANNOT_MIX_BIGINT_AND_OTHER_TYPES =
"Cannot mix BigInt and other types, use explicit conversions";
const ITERATOR_PROPERTY_IS_NOT_CALLABLE = "@@iterator property is not callable";
const REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE =
"Reduce of empty array with no initial value";
const OFFSET_IS_OUT_OF_BOUNDS = "Offset is out of bounds";
function hfround(num) {
if (typeof num === "bigint") {
throw NativeTypeError(CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER);
}
num = NativeNumber(num);
if (!NumberIsFinite(num) || num === 0) {
return num;
}
const x16 = roundToFloat16Bits(num);
return convertToNumber(x16);
}
function toSafe(array) {

@@ -305,6 +188,6 @@ if (array[SymbolIterator] === NativeArrayPrototypeSymbolIterator) {

}
function isTypedArray(value) {
function isNativeTypedArray(value) {
return TypedArrayPrototypeGetSymbolToStringTag(value) !== undefined;
}
function isBigIntTypedArray(value) {
function isNativeBigIntTypedArray(value) {
const typedArrayName = TypedArrayPrototypeGetSymbolToStringTag(value);

@@ -343,4 +226,4 @@ return typedArrayName === "BigInt64Array" ||

}
function isOrdinaryTypedArray(value) {
if (!isTypedArray(value)) {
function isOrdinaryNativeTypedArray(value) {
if (!isNativeTypedArray(value)) {
return false;

@@ -368,2 +251,126 @@ }

const THIS_IS_NOT_AN_OBJECT = "This is not an object";
const THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT = "This is not a Float16Array object";
const THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY =
"This constructor is not a subclass of Float16Array";
const THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT =
"The constructor property value is not an object";
const SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT =
"Species constructor didn't return TypedArray object";
const DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH =
"Derived constructor created TypedArray object which was too small length";
const ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER =
"Attempting to access detached ArrayBuffer";
const CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT =
"Cannot convert undefined or null to object";
const CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER =
"Cannot convert a BigInt value to a number";
const CANNOT_MIX_BIGINT_AND_OTHER_TYPES =
"Cannot mix BigInt and other types, use explicit conversions";
const ITERATOR_PROPERTY_IS_NOT_CALLABLE = "@@iterator property is not callable";
const REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE =
"Reduce of empty array with no initial value";
const OFFSET_IS_OUT_OF_BOUNDS = "Offset is out of bounds";
const brand = SymbolFor("__Float16Array__");
function hasFloat16ArrayBrand(target) {
if (!isObjectLike(target)) {
return false;
}
const prototype = ReflectGetPrototypeOf(target);
if (!isObjectLike(prototype)) {
return false;
}
const constructor = prototype.constructor;
if (constructor === undefined) {
return false;
}
if (!isObject(constructor)) {
throw NativeTypeError(THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT);
}
return ReflectHas(constructor, brand);
}
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 roundToFloat16Bits(num) {
floatView[0] = (num);
const f = uint32View[0];
const e = (f >> 23) & 0x1ff;
return baseTable[e] + ((f & 0x007fffff) >> shiftTable[e]);
}
const mantissaTable = new NativeUint32Array(2048);
const exponentTable = new NativeUint32Array(64);
const offsetTable = new NativeUint32Array(64);
mantissaTable[0] = 0;
for (let i = 1; i < 1024; ++i) {
let m = i << 13;
let e = 0;
while((m & 0x00800000) === 0) {
e -= 0x00800000;
m <<= 1;
}
m &= ~0x00800000;
e += 0x38800000;
mantissaTable[i] = m | e;
}
for (let i = 1024; i < 2048; ++i) {
mantissaTable[i] = 0x38000000 + ((i - 1024) << 13);
}
exponentTable[0] = 0;
for (let i = 1; i < 31; ++i) {
exponentTable[i] = i << 23;
}
exponentTable[31] = 0x47800000;
exponentTable[32] = 0x80000000;
for (let i = 33; i < 63; ++i) {
exponentTable[i] = 0x80000000 + ((i - 32) << 23);
}
exponentTable[63] = 0xc7800000;
offsetTable[0] = 0;
for (let i = 1; i < 64; ++i) {
if (i === 32) {
offsetTable[i] = 0;
} else {
offsetTable[i] = 1024;
}
}
function convertToNumber(float16bits) {
const m = float16bits >> 10;
uint32View[0] = mantissaTable[offsetTable[m] + (float16bits & 0x3ff)] + exponentTable[m];
return floatView[0];
}
const MAX_SAFE_INTEGER = NativeNumber.MAX_SAFE_INTEGER;

@@ -448,21 +455,3 @@ function ToIntegerOrInfinity(target) {

const BYTES_PER_ELEMENT = 2;
const brand = SymbolFor("__Float16Array__");
const float16bitsArrays = new NativeWeakMap();
function hasFloat16ArrayBrand(target) {
if (!isObjectLike(target)) {
return false;
}
const prototype = ReflectGetPrototypeOf(target);
if (!isObjectLike(prototype)) {
return false;
}
const constructor = prototype.constructor;
if (constructor === undefined) {
return false;
}
if (!isObject(constructor)) {
throw NativeTypeError(THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT);
}
return ReflectHas(constructor, brand);
}
function isFloat16Array(target) {

@@ -479,3 +468,3 @@ return WeakMapPrototypeHas(float16bitsArrays, target) ||

const isTargetFloat16Array = isFloat16Array(target);
const isTargetTypedArray = isTypedArray(target);
const isTargetTypedArray = isNativeTypedArray(target);
if (!isTargetFloat16Array && !isTargetTypedArray) {

@@ -498,3 +487,3 @@ throw NativeTypeError(SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT);

}
if (isBigIntTypedArray(target)) {
if (isNativeBigIntTypedArray(target)) {
throw NativeTypeError(CANNOT_MIX_BIGINT_AND_OTHER_TYPES);

@@ -512,3 +501,3 @@ }

}
const buffer = float16.buffer;
const buffer = (float16).buffer;
if (IsDetachedBuffer(buffer)) {

@@ -519,4 +508,4 @@ throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);

buffer,
float16.byteOffset,
float16.length,
(float16).byteOffset,
(float16).length,
], float16.constructor);

@@ -590,3 +579,3 @@ return WeakMapPrototypeGet(float16bitsArrays, cloned);

let length;
if (isTypedArray(input)) {
if (isNativeTypedArray(input)) {
list = input;

@@ -604,3 +593,3 @@ length = TypedArrayPrototypeGetLength(input);

}
if (isBigIntTypedArray(input)) {
if (isNativeBigIntTypedArray(input)) {
throw NativeTypeError(CANNOT_MIX_BIGINT_AND_OTHER_TYPES);

@@ -622,3 +611,3 @@ }

} else {
list = [...input];
list = [... (input)];
length = list.length;

@@ -638,3 +627,3 @@ }

}
const proxy = new NativeProxy( (float16bitsArray), handler);
const proxy = (new NativeProxy(float16bitsArray, handler));
WeakMapPrototypeSet(float16bitsArrays, proxy, float16bitsArray);

@@ -691,3 +680,3 @@ return proxy;

length = src.length;
} else if (isOrdinaryTypedArray(src)) {
} else if (isOrdinaryNativeTypedArray(src)) {
list = src;

@@ -981,3 +970,3 @@ length = TypedArrayPrototypeGetLength(src);

}
if (isBigIntTypedArray(input)) {
if (isNativeBigIntTypedArray(input)) {
throw NativeTypeError(

@@ -994,3 +983,3 @@ CANNOT_MIX_BIGINT_AND_OTHER_TYPES

}
if (isTypedArray(input)) {
if (isNativeTypedArray(input)) {
const buffer = TypedArrayPrototypeGetBuffer(input);

@@ -1220,2 +1209,6 @@ if (IsDetachedBuffer(buffer)) {

function isTypedArray(target) {
return isNativeTypedArray(target) || isFloat16Array(target);
}
function getFloat16(dataView, byteOffset, ...opts) {

@@ -1235,2 +1228,14 @@ return convertToNumber(

function hfround(num) {
if (typeof num === "bigint") {
throw NativeTypeError(CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER);
}
num = NativeNumber(num);
if (!NumberIsFinite(num) || num === 0) {
return num;
}
const x16 = roundToFloat16Bits(num);
return convertToNumber(x16);
}
exports.Float16Array = Float16Array;

@@ -1240,2 +1245,3 @@ exports.getFloat16 = getFloat16;

exports.isFloat16Array = isFloat16Array;
exports.isTypedArray = isTypedArray;
exports.setFloat16 = setFloat16;

@@ -1242,0 +1248,0 @@

@@ -421,2 +421,22 @@ /**

/**
* Returns `true` if the value is a type of TypedArray instance that contains Float16Array.
* @since v3.6.0
*/
export declare function isTypedArray(
value: unknown,
): value is
| Uint8Array
| Uint8ClampedArray
| Uint16Array
| Uint32Array
| Int8Array
| Int16Array
| Int32Array
| Float16Array
| Float32Array
| Float64Array
| BigUint64Array
| BigInt64Array;
/**
* Gets the Float16 value at the specified byte offset from the start of the view. There is

@@ -423,0 +443,0 @@ * no alignment constraint; multi-byte values may be fetched from any offset.

{
"name": "@petamoriken/float16",
"version": "3.5.11",
"version": "3.6.0",
"description": "IEEE 754 half-precision floating-point for JavaScript",

@@ -75,11 +75,11 @@ "keywords": [

"@babel/cli": "^7.15.7",
"@babel/core": "^7.15.8",
"@babel/plugin-transform-modules-commonjs": "^7.15.4",
"@babel/core": "^7.16.5",
"@babel/plugin-transform-modules-commonjs": "^7.16.5",
"@types/nightwatch": "^1.3.4",
"babel-plugin-replace-import-extension": "^1.1.1",
"browserslist": "^4.17.6",
"concurrently": "^6.3.0",
"eslint": "^8.2.0",
"babel-plugin-replace-import-extension": "^1.1.2",
"browserslist": "^4.19.1",
"concurrently": "^6.5.1",
"eslint": "^8.5.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jsdoc": "^37.0.3",
"eslint-plugin-jsdoc": "^37.3.0",
"espower-cli": "^1.1.0",

@@ -91,11 +91,11 @@ "espower-loader": "^1.2.2",

"mocha": "^9.1.3",
"nightwatch": "^1.7.11",
"nightwatch": "^1.7.13",
"nightwatch-saucelabs-endsauce": "^1.0.5",
"nyc": "^15.1.0",
"power-assert": "^1.4.2",
"rollup": "^2.58.3",
"rollup": "^2.61.1",
"rollup-plugin-cleanup": "^3.2.1",
"source-map-support": "^0.5.20"
"source-map-support": "^0.5.21"
},
"packageManager": "yarn@1.22.15"
}

@@ -56,3 +56,3 @@ # <a href="https://git.io/float16">float16</a>

import {
Float16Array, isFloat16Array,
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,

@@ -66,3 +66,3 @@ hfround,

const {
Float16Array, isFloat16Array,
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,

@@ -80,3 +80,3 @@ hfround,

import {
Float16Array, isFloat16Array,
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,

@@ -96,3 +96,3 @@ hfround,

import {
Float16Array, isFloat16Array,
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,

@@ -109,3 +109,3 @@ hfround,

const {
Float16Array, isFloat16Array,
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,

@@ -124,3 +124,3 @@ hfround,

import {
Float16Array, isFloat16Array,
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,

@@ -137,3 +137,3 @@ hfround,

const {
Float16Array, isFloat16Array,
Float16Array, isFloat16Array, isTypedArray,
getFloat16, setFloat16,

@@ -177,8 +177,10 @@ hfround,

```js
const array = new Float16Array([1.0, 1.1, 1.2]);
for (const val of array) {
console.log(val); // => 1, 1.099609375, 1.19921875
const array = new Float16Array([1.0, 1.1, 1.2, 1.3]);
for (const value of array) {
// 1, 1.099609375, 1.19921875, 1.2998046875
console.log(value);
}
array.reduce((prev, current) => prev + current); // 3.298828125
// Float16Array(4) [ 2, 2.19921875, 2.3984375, 2.599609375 ]
array.map((value) => value * 2);
```

@@ -191,10 +193,30 @@

```ts
declare function isFloat16Array(value: unknown): value is Float16Array;
```js
const buffer = new ArrayBuffer(256);
// true
isFloat16Array(new Float16Array(buffer));
// false
isFloat16Array(new Float32Array(buffer));
isFloat16Array(new Uint16Array(buffer));
isFloat16Array(new DataView(buffer));
```
### `isTypedArray`
`isTypedArray` is a utility function to check whether the value given as an
argument is an instance of a type of `TypedArray` or not. Unlike
`util.types.isTypedArray` in Node.js, this returns `true` for `Float16Array`.
```js
isFloat16Array(new Float16Array(10)); // true
isFloat16Array(new Float32Array(10)); // false
isFloat16Array(new Uint16Array(10)); // false
const buffer = new ArrayBuffer(256);
// true
isTypedArray(new Float16Array(buffer));
isTypedArray(new Float32Array(buffer));
isTypedArray(new Uint16Array(buffer));
// false
isTypedArray(new DataView(buffer));
```

@@ -216,3 +238,3 @@

```js
const buffer = new ArrayBuffer(10);
const buffer = new ArrayBuffer(256);
const view = new DataView(buffer);

@@ -219,0 +241,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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc