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.4.0 to 3.4.1

1920

browser/float16.js

@@ -1,1285 +0,1305 @@

/*! @petamoriken/float16 v3.4.0 | MIT License - https://git.io/float16 */
/*! @petamoriken/float16 v3.4.1 | MIT License - https://git.io/float16 */
var float16 = (function (exports) {
'use strict';
'use strict';
// algorithm: ftp://ftp.fox-toolkit.org/pub/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);
// algorithm: ftp://ftp.fox-toolkit.org/pub/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);
for (let i = 0; i < 256; ++i) {
const e = i - 127; // very small number (0, -0)
for (let i = 0; i < 256; ++i) {
const e = i - 127; // very small number (0, -0)
if (e < -27) {
baseTable[i] = 0x0000;
baseTable[i | 0x100] = 0x8000;
shiftTable[i] = 24;
shiftTable[i | 0x100] = 24; // small number (denorm)
} 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; // normal number
} else if (e <= 15) {
baseTable[i] = e + 15 << 10;
baseTable[i | 0x100] = e + 15 << 10 | 0x8000;
shiftTable[i] = 13;
shiftTable[i | 0x100] = 13; // large number (Infinity, -Infinity)
} else if (e < 128) {
baseTable[i] = 0x7c00;
baseTable[i | 0x100] = 0xfc00;
shiftTable[i] = 24;
shiftTable[i | 0x100] = 24; // stay (NaN, Infinity, -Infinity)
} else {
baseTable[i] = 0x7c00;
baseTable[i | 0x100] = 0xfc00;
shiftTable[i] = 13;
shiftTable[i | 0x100] = 13;
}
if (e < -27) {
baseTable[i] = 0x0000;
baseTable[i | 0x100] = 0x8000;
shiftTable[i] = 24;
shiftTable[i | 0x100] = 24; // small number (denorm)
} 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; // normal number
} else if (e <= 15) {
baseTable[i] = e + 15 << 10;
baseTable[i | 0x100] = e + 15 << 10 | 0x8000;
shiftTable[i] = 13;
shiftTable[i | 0x100] = 13; // large number (Infinity, -Infinity)
} else if (e < 128) {
baseTable[i] = 0x7c00;
baseTable[i | 0x100] = 0xfc00;
shiftTable[i] = 24;
shiftTable[i | 0x100] = 24; // stay (NaN, Infinity, -Infinity)
} else {
baseTable[i] = 0x7c00;
baseTable[i | 0x100] = 0xfc00;
shiftTable[i] = 13;
shiftTable[i | 0x100] = 13;
}
/**
* round a number to a half float number bits.
* @param {number} num - double float
* @returns {number} half float number bits
*/
}
/**
* round a number to a half float number bits.
* @param {number} num - double float
* @returns {number} half float number bits
*/
function roundToFloat16Bits(num) {
floatView[0] = num;
const f = uint32View[0];
const e = f >> 23 & 0x1ff;
return baseTable[e] + ((f & 0x007fffff) >> shiftTable[e]);
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);
mantissaTable[0] = 0;
for (let i = 1; i < 1024; ++i) {
let m = i << 13; // zero pad mantissa bits
let e = 0; // zero exponent
// normalized
while ((m & 0x00800000) === 0) {
e -= 0x00800000; // decrement exponent
m <<= 1;
}
const mantissaTable = new Uint32Array(2048);
const exponentTable = new Uint32Array(64);
const offsetTable = new Uint32Array(64);
mantissaTable[0] = 0;
for (let i = 1; i < 1024; ++i) {
let m = i << 13; // zero pad mantissa bits
m &= ~0x00800000; // clear leading 1 bit
let e = 0; // zero exponent
// normalized
e += 0x38800000; // adjust bias
while ((m & 0x00800000) === 0) {
e -= 0x00800000; // decrement exponent
mantissaTable[i] = m | e;
}
m <<= 1;
}
for (let i = 1024; i < 2048; ++i) {
mantissaTable[i] = 0x38000000 + (i - 1024 << 13);
}
m &= ~0x00800000; // clear leading 1 bit
exponentTable[0] = 0;
e += 0x38800000; // adjust bias
for (let i = 1; i < 31; ++i) {
exponentTable[i] = i << 23;
}
mantissaTable[i] = m | e;
}
exponentTable[31] = 0x47800000;
exponentTable[32] = 0x80000000;
for (let i = 1024; i < 2048; ++i) {
mantissaTable[i] = 0x38000000 + (i - 1024 << 13);
}
for (let i = 33; i < 63; ++i) {
exponentTable[i] = 0x80000000 + (i - 32 << 23);
}
exponentTable[0] = 0;
exponentTable[63] = 0xc7800000;
offsetTable[0] = 0;
for (let i = 1; i < 31; ++i) {
exponentTable[i] = i << 23;
for (let i = 1; i < 64; ++i) {
if (i === 32) {
offsetTable[i] = 0;
} else {
offsetTable[i] = 1024;
}
}
/**
* convert a half float number bits to a number.
* @param {number} float16bits - half float number bits
* @returns {number} double float
*/
exponentTable[31] = 0x47800000;
exponentTable[32] = 0x80000000;
for (let i = 33; i < 63; ++i) {
exponentTable[i] = 0x80000000 + (i - 32 << 23);
}
function convertToNumber(float16bits) {
const m = float16bits >> 10;
uint32View[0] = mantissaTable[offsetTable[m] + (float16bits & 0x3ff)] + exponentTable[m];
return floatView[0];
}
exponentTable[63] = 0xc7800000;
offsetTable[0] = 0;
/**
* returns the nearest half precision float representation of a number.
* @param {number} num
* @returns {number}
*/
for (let i = 1; i < 64; ++i) {
if (i === 32) {
offsetTable[i] = 0;
} else {
offsetTable[i] = 1024;
}
}
/**
* convert a half float number bits to a number.
* @param {number} float16bits - half float number bits
* @returns {number} double float
*/
function hfround(num) {
num = Number(num); // for optimization
function convertToNumber(float16bits) {
const m = float16bits >> 10;
uint32View[0] = mantissaTable[offsetTable[m] + (float16bits & 0x3ff)] + exponentTable[m];
return floatView[0];
if (!Number.isFinite(num) || num === 0) {
return num;
}
/**
* returns the nearest half precision float representation of a number.
* @param {number} num
* @returns {number}
*/
const x16 = roundToFloat16Bits(num);
return convertToNumber(x16);
}
function hfround(num) {
num = Number(num); // for optimization
/**
* @returns {(self:object) => object}
*/
function createPrivateStorage() {
const wm = new WeakMap();
return self => {
const storage = wm.get(self);
if (!Number.isFinite(num) || num === 0) {
return num;
if (storage !== undefined) {
return storage;
}
const x16 = roundToFloat16Bits(num);
return convertToNumber(x16);
const obj = Object.create(null);
wm.set(self, obj);
return obj;
};
}
const _$1 = createPrivateStorage();
const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
const ArrayIteratorPrototype = Object.create(IteratorPrototype, {
next: {
value: function next() {
return _$1(this).iterator.next();
},
writable: true,
configurable: true
},
[Symbol.toStringTag]: {
value: "Array Iterator",
configurable: true
}
});
/**
* @param {Iterator<number>} iterator
* @returns {IterableIterator<number>}
*/
/**
* @returns {(self:object) => object}
*/
function createPrivateStorage() {
const wm = new WeakMap();
return self => {
const storage = wm.get(self);
function wrapInArrayIterator(iterator) {
const arrayIterator = Object.create(ArrayIteratorPrototype);
_$1(arrayIterator).iterator = iterator;
return arrayIterator;
}
if (storage !== undefined) {
return storage;
}
/**
* @param {unknown} value
* @returns {value is object}
*/
function isObject(value) {
return value !== null && typeof value === "object" || typeof value === "function";
}
/**
* @param {unknown} value
* @returns {value is object}
*/
const obj = Object.create(null);
wm.set(self, obj);
return obj;
};
}
function isObjectLike(value) {
return value !== null && typeof value === "object";
} // Inspired by util.types implementation of Node.js
const _$1 = createPrivateStorage();
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array).prototype;
const getTypedArrayPrototypeSybolToStringTag = Object.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get;
/**
* @param {unknown} value
* @returns {value is Uint8Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array|Float32Array|Float64Array|BigUint64Array|BigInt64Array}
*/
const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
const ArrayIteratorPrototype = Object.create(IteratorPrototype, {
next: {
value: function next() {
return _$1(this).iterator.next();
},
writable: true,
configurable: true
},
[Symbol.toStringTag]: {
value: "Array Iterator",
configurable: true
}
});
/**
* @param {Iterator<number>} iterator
* @returns {IterableIterator<number>}
*/
function isTypedArray(value) {
return getTypedArrayPrototypeSybolToStringTag.call(value) !== undefined;
}
/**
* @param {unknown} value
* @returns {value is Uint16Array}
*/
function wrapInArrayIterator(iterator) {
const arrayIterator = Object.create(ArrayIteratorPrototype);
_$1(arrayIterator).iterator = iterator;
return arrayIterator;
function isUint16Array(value) {
return getTypedArrayPrototypeSybolToStringTag.call(value) === "Uint16Array";
}
const toString = Object.prototype.toString;
/**
* @param {unknown} value
* @returns {value is DataView}
*/
function isDataView(value) {
if (!ArrayBuffer.isView(value)) {
return false;
}
/**
* @param {unknown} value
* @returns {value is object}
*/
function isObject$1(value) {
return value !== null && typeof value === "object" || typeof value === "function";
if (isTypedArray(value)) {
return false;
}
/**
* @param {unknown} target
* @returns {number}
*/
if (toString.call(value) !== "[object DataView]") {
return false;
}
function ToIntegerOrInfinity(target) {
const number = Number(target);
return true;
}
/**
* @param {unknown} value
* @returns {value is ArrayBuffer}
*/
if (Number.isNaN(number) || number === 0) {
return 0;
}
function isArrayBuffer(value) {
return isObjectLike(value) && toString.call(value) === "[object ArrayBuffer]";
}
/**
* @param {unknown} value
* @returns {value is SharedArrayBuffer}
*/
if (number === Infinity) {
return Infinity;
}
function isSharedArrayBuffer(value) {
return isObjectLike(value) && toString.call(value) === "[object SharedArrayBuffer]";
}
/**
* @param {unknown} value
* @returns {value is Iterable}
*/
if (number === -Infinity) {
return -Infinity;
}
function isIterable(value) {
return isObject(value) && typeof value[Symbol.iterator] === "function";
}
/**
* @param {unknown} value
* @returns {value is string}
*/
return Math.trunc(number);
function isCanonicalIntegerIndexString(value) {
if (typeof value !== "string") {
return false;
}
/**
* @param {unknown} target
* @returns {number}
*/
function ToLength(target) {
const length = ToIntegerOrInfinity(target);
const number = Number(value);
if (length < 0) {
return 0;
}
if (value !== number + "") {
return false;
}
return length < Number.MAX_SAFE_INTEGER ? length : Number.MAX_SAFE_INTEGER;
if (!Number.isFinite(number)) {
return false;
}
/**
* @param {object} arrayLike
* @returns {number}
*/
function LengthOfArrayLike(arrayLike) {
if (!isObject$1(arrayLike)) {
throw TypeError("this is not a object");
}
return ToLength(arrayLike.length);
if (number !== Math.trunc(number)) {
return false;
}
/**
* @param {object} target
* @param {Function} defaultConstructor
* @returns {Function}
*/
function SpeciesConstructor(target, defaultConstructor) {
if (!isObject$1(target)) {
throw TypeError("this is not a object");
}
return true;
}
const constructor = target.constructor;
/**
* @param {unknown} target
* @returns {number}
*/
if (constructor === undefined) {
return defaultConstructor;
}
function ToIntegerOrInfinity(target) {
const number = Number(target);
if (!isObject$1(constructor)) {
throw TypeError("constructor is not a object");
}
if (Number.isNaN(number) || number === 0) {
return 0;
}
const species = constructor[Symbol.species];
if (number === Infinity) {
return Infinity;
}
if (species == null) {
return defaultConstructor;
}
return species;
if (number === -Infinity) {
return -Infinity;
}
/**
* @param {number} x
* @param {number} y
* @returns {-1 | 0 | 1}
*/
function defaultCompare(x, y) {
const [isNaN_x, isNaN_y] = [Number.isNaN(x), Number.isNaN(y)];
return Math.trunc(number);
}
/**
* @param {unknown} target
* @returns {number}
*/
if (isNaN_x && isNaN_y) {
return 0;
}
function ToLength(target) {
const length = ToIntegerOrInfinity(target);
if (isNaN_x) {
return 1;
}
if (length < 0) {
return 0;
}
if (isNaN_y) {
return -1;
}
return length < Number.MAX_SAFE_INTEGER ? length : Number.MAX_SAFE_INTEGER;
}
/**
* @param {object} arrayLike
* @returns {number}
*/
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
function LengthOfArrayLike(arrayLike) {
if (!isObject(arrayLike)) {
throw TypeError("this is not a object");
}
if (x === 0 && y === 0) {
const [isPlusZero_x, isPlusZero_y] = [Object.is(x, 0), Object.is(y, 0)];
return ToLength(arrayLike.length);
}
/**
* @param {object} target
* @param {Function} defaultConstructor
* @returns {Function}
*/
if (!isPlusZero_x && isPlusZero_y) {
return -1;
}
function SpeciesConstructor(target, defaultConstructor) {
if (!isObject(target)) {
throw TypeError("this is not a object");
}
if (isPlusZero_x && !isPlusZero_y) {
return 1;
}
}
const constructor = target.constructor;
return 0;
if (constructor === undefined) {
return defaultConstructor;
}
/**
* @param {unknown} value
* @returns {value is object}
*/
function isObject(value) {
return value !== null && typeof value === "object" || typeof value === "function";
if (!isObject(constructor)) {
throw TypeError("constructor is not a object");
}
/**
* @param {unknown} value
* @returns {value is object}
*/
function isObjectLike(value) {
return value !== null && typeof value === "object";
const species = constructor[Symbol.species];
if (species == null) {
return defaultConstructor;
}
const toString = Object.prototype.toString;
/**
* @param {unknown} value
* @returns {value is DataView}
*/
function isDataView(value) {
return ArrayBuffer.isView(value) && toString.call(value) === "[object DataView]";
} // Inspired by util.types implementation of Node.js
return species;
}
/**
* @param {number} x
* @param {number} y
* @returns {-1 | 0 | 1}
*/
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array).prototype;
const getTypedArrayPrototypeSybolToStringTag = Object.getOwnPropertyDescriptor(TypedArrayPrototype, Symbol.toStringTag).get;
/**
* @param {unknown} value
* @returns {value is Uint8Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array|Float32Array|Float64Array|BigUint64Array|BigInt64Array}
*/
function defaultCompare(x, y) {
const [isNaN_x, isNaN_y] = [Number.isNaN(x), Number.isNaN(y)];
function isTypedArray(value) {
return getTypedArrayPrototypeSybolToStringTag.call(value) !== undefined;
if (isNaN_x && isNaN_y) {
return 0;
}
/**
* @param {unknown} value
* @returns {value is Uint16Array}
*/
function isUint16Array(value) {
return getTypedArrayPrototypeSybolToStringTag.call(value) === "Uint16Array";
if (isNaN_x) {
return 1;
}
/**
* @param {unknown} value
* @returns {value is ArrayBuffer}
*/
function isArrayBuffer(value) {
return isObjectLike(value) && toString.call(value) === "[object ArrayBuffer]";
if (isNaN_y) {
return -1;
}
/**
* @param {unknown} value
* @returns {value is SharedArrayBuffer}
*/
function isSharedArrayBuffer(value) {
return isObjectLike(value) && toString.call(value) === "[object SharedArrayBuffer]";
if (x < y) {
return -1;
}
/**
* @param {unknown} value
* @returns {value is Iterable}
*/
function isIterable(value) {
return isObject(value) && typeof value[Symbol.iterator] === "function";
if (x > y) {
return 1;
}
/**
* @param {unknown} key
* @returns {value is string}
*/
function isCanonicalIntegerIndexString(key) {
return typeof key === "string" && key === ToIntegerOrInfinity(key) + "";
}
if (x === 0 && y === 0) {
const [isPlusZero_x, isPlusZero_y] = [Object.is(x, 0), Object.is(y, 0)];
const brand = Symbol.for("__Float16Array__");
if (!isPlusZero_x && isPlusZero_y) {
return -1;
}
const _ = createPrivateStorage();
/**
* @param {unknown} target
* @returns {boolean}
*/
if (isPlusZero_x && !isPlusZero_y) {
return 1;
}
}
return 0;
}
function hasFloat16ArrayBrand(target) {
if (!isObjectLike(target)) {
return false;
}
const brand = Symbol.for("__Float16Array__");
const constructor = target.constructor;
const _ = createPrivateStorage();
/**
* @param {unknown} target
* @returns {boolean}
*/
if (constructor === undefined) {
return false;
}
if (!isObject(constructor)) {
throw TypeError("constructor is not a object");
}
return Reflect.has(constructor, brand);
function hasFloat16ArrayBrand(target) {
if (!isObjectLike(target)) {
return false;
}
/**
* @param {unknown} target
* @returns {boolean}
*/
const constructor = target.constructor;
function isFloat16Array(target) {
return hasFloat16ArrayBrand(target) && !isTypedArray(target);
if (constructor === undefined) {
return false;
}
/**
* @param {unknown} target
* @returns {boolean}
*/
function isFloat16BitsArray(target) {
return hasFloat16ArrayBrand(target) && isUint16Array(target);
if (!isObject(constructor)) {
throw TypeError("constructor is not a object");
}
/**
* @param {unknown} target
* @throws {TypeError}
*/
return Reflect.has(constructor, brand);
}
/**
* @param {unknown} target
* @returns {boolean}
*/
function assertFloat16BitsArray(target) {
if (!isFloat16BitsArray(target)) {
throw new TypeError("This is not a Float16Array");
}
}
/**
* peel off Proxy
* @param {Float16Array} float16
* @return {Float16Array}
*/
function isFloat16Array(target) {
return hasFloat16ArrayBrand(target) && !isTypedArray(target);
}
/**
* @param {unknown} target
* @returns {boolean}
*/
function getFloat16BitsArrayFromFloat16Array(float16) {
let target = _(float16).target; // from another realm
function isFloat16BitsArray(target) {
return hasFloat16ArrayBrand(target) && isUint16Array(target);
}
/**
* @param {unknown} target
* @throws {TypeError}
*/
if (target === undefined) {
const clone = new Float16Array(float16.buffer, float16.byteOffset, float16.length);
target = _(clone).target;
}
return target;
function assertFloat16BitsArray(target) {
if (!isFloat16BitsArray(target)) {
throw new TypeError("This is not a Float16Array");
}
/**
* @param {unknown} target
* @returns {boolean}
*/
}
/**
* @param {Float16Array} float16
* @return {ArrayLike<number>}
*/
function isDefaultFloat16ArrayMethods(target) {
return typeof target === "function" && defaultFloat16ArrayMethods.has(target);
function getFloat16BitsArrayFromFloat16Array(float16) {
let target = _(float16).target; // from other realms
if (target === undefined) {
const clone = new Float16Array(float16.buffer, float16.byteOffset, float16.length);
target = _(clone).target;
}
/**
* @param {Float16Array} float16bitsArray
* @return {number[]}
*/
return target;
}
/**
* @param {ArrayLike<number>} float16bitsArray
* @return {number[]}
*/
function copyToArray(float16bitsArray) {
const length = float16bitsArray.length;
const array = [];
for (let i = 0; i < length; ++i) {
array.push(convertToNumber(float16bitsArray[i]));
}
function copyToArray(float16bitsArray) {
const length = float16bitsArray.length;
const array = [];
return array;
for (let i = 0; i < length; ++i) {
array[i] = convertToNumber(float16bitsArray[i]);
}
/** @type {ProxyHandler<Function>} */
return array;
}
/**
* @param {unknown} target
* @returns {boolean}
*/
const applyHandler = Object.freeze({
apply(func, thisArg, args) {
// peel off Proxy
if (isFloat16Array(thisArg)) {
const target = getFloat16BitsArrayFromFloat16Array(thisArg);
return Reflect.apply(func, target, args);
}
return Reflect.apply(func, thisArg, args);
function isDefaultFloat16ArrayMethods(target) {
return typeof target === "function" && defaultFloat16ArrayMethods.has(target);
}
/** @type {ProxyHandler<Function>} */
const applyHandler = Object.freeze({
apply(func, thisArg, args) {
// peel off Proxy
if (isFloat16Array(thisArg)) {
const target = getFloat16BitsArrayFromFloat16Array(thisArg);
return Reflect.apply(func, target, args);
}
});
/** @type {ProxyHandler<Float16Array>} */
return Reflect.apply(func, thisArg, args);
}
const handler = Object.freeze({
get(target, key) {
if (isCanonicalIntegerIndexString(key)) {
const raw = Reflect.get(target, key);
return raw !== undefined ? convertToNumber(raw) : undefined;
} else {
const ret = Reflect.get(target, key);
});
const hasOwnProperty = Object.prototype.hasOwnProperty;
/** @type {ProxyHandler<Float16Array>} */
if (!isDefaultFloat16ArrayMethods(ret)) {
return ret;
} // TypedArray methods can't be called by Proxy Object
const handler = Object.freeze({
get(target, key) {
if (isCanonicalIntegerIndexString(key) && hasOwnProperty.call(target, key)) {
return convertToNumber(Reflect.get(target, key));
}
const ret = Reflect.get(target, key);
let proxy = _(ret).proxy;
if (!isDefaultFloat16ArrayMethods(ret)) {
return ret;
} // TypedArray methods can't be called by Proxy Object
if (proxy === undefined) {
proxy = _(ret).proxy = new Proxy(ret, applyHandler);
}
return proxy;
}
},
let proxy = _(ret).proxy;
set(target, key, value) {
if (isCanonicalIntegerIndexString(key)) {
return Reflect.set(target, key, roundToFloat16Bits(value));
} else {
return Reflect.set(target, key, value);
}
if (proxy === undefined) {
proxy = _(ret).proxy = new Proxy(ret, applyHandler);
}
});
const hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* limitation: see README.md for details
*/
return proxy;
},
class Float16Array extends Uint16Array {
/**
* @see https://tc39.es/ecma262/#sec-typedarray
*/
constructor(input, byteOffset, length) {
// input Float16Array
if (isFloat16Array(input)) {
// peel off Proxy
super(getFloat16BitsArrayFromFloat16Array(input)); // object without ArrayBuffer
} else if (isObject(input) && !isArrayBuffer(input)) {
let list;
let length; // TypedArray
set(target, key, value) {
if (isCanonicalIntegerIndexString(key) && hasOwnProperty.call(target, key)) {
return Reflect.set(target, key, roundToFloat16Bits(value));
}
if (isTypedArray(input)) {
list = input;
length = input.length;
const buffer = input.buffer;
/** @type {ArrayBufferConstructor} */
return Reflect.set(target, key, value);
}
const BufferConstructor = !isSharedArrayBuffer(buffer) ? SpeciesConstructor(buffer, ArrayBuffer) : ArrayBuffer;
const data = new BufferConstructor(length * Float16Array.BYTES_PER_ELEMENT);
super(data); // Iterable (Array)
} else if (isIterable(input)) {
list = [...input];
length = list.length;
super(length); // ArrayLike
} else {
list = input;
length = LengthOfArrayLike(input);
super(length);
} // set values
});
/**
* limitation: see README.md for details
*/
class Float16Array extends Uint16Array {
/**
* @see https://tc39.es/ecma262/#sec-typedarray
*/
constructor(input, byteOffset, length) {
// input Float16Array
if (isFloat16Array(input)) {
// peel off Proxy
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(input);
super(float16bitsArray); // object without ArrayBuffer
} else if (isObject(input) && !isArrayBuffer(input)) {
let list;
let length; // TypedArray
for (let i = 0; i < length; ++i) {
// super (Uint16Array)
this[i] = roundToFloat16Bits(list[i]);
} // primitive, ArrayBuffer
if (isTypedArray(input)) {
list = input;
length = input.length;
const buffer = input.buffer;
/** @type {ArrayBufferConstructor} */
const BufferConstructor = !isSharedArrayBuffer(buffer) ? SpeciesConstructor(buffer, ArrayBuffer) : ArrayBuffer;
const data = new BufferConstructor(length * Float16Array.BYTES_PER_ELEMENT);
super(data); // Iterable (Array)
} else if (isIterable(input)) {
list = [...input];
length = list.length;
super(length); // ArrayLike
} else {
switch (arguments.length) {
case 0:
super();
break;
list = input;
length = LengthOfArrayLike(input);
super(length);
} // set values
case 1:
super(input);
break;
case 2:
super(input, byteOffset);
break;
for (let i = 0; i < length; ++i) {
// super (Uint16Array)
this[i] = roundToFloat16Bits(list[i]);
} // primitive, ArrayBuffer
case 3:
super(input, byteOffset, length);
break;
} else {
switch (arguments.length) {
case 0:
super();
break;
default:
super(...arguments);
}
}
case 1:
super(input);
break;
const proxy = new Proxy(this, handler); // proxy private storage
case 2:
super(input, byteOffset);
break;
_(proxy).target = this; // this private storage
case 3:
super(input, byteOffset, length);
break;
_(this).proxy = proxy;
return proxy;
default:
super(...arguments);
}
}
/**
* limitation: `Object.getOwnPropertyNames(Float16Array)` or `Reflect.ownKeys(Float16Array)` include this key
* @see https://tc39.es/ecma262/#sec-%typedarray%.from
*/
const proxy = new Proxy(this, handler); // proxy private storage
static from(src, ...opts) {
// for optimization
if (isFloat16Array(src) && opts.length === 0) {
const uint16 = new Uint16Array(src.buffer, src.byteOffset, src.length);
return new Float16Array(uint16.slice().buffer);
}
_(proxy).target = this; // this private storage
if (opts.length === 0) {
return new Float16Array(Uint16Array.from(src, roundToFloat16Bits).buffer);
}
_(this).proxy = proxy;
return proxy;
}
/**
* limitation: `Object.getOwnPropertyNames(Float16Array)` or `Reflect.ownKeys(Float16Array)` include this key
* @see https://tc39.es/ecma262/#sec-%typedarray%.from
*/
const mapFunc = opts[0];
const thisArg = opts[1];
return new Float16Array(Uint16Array.from(src, function (val, ...args) {
return roundToFloat16Bits(mapFunc.call(this, val, ...args));
}, thisArg).buffer);
static from(src, ...opts) {
// for optimization
if (isFloat16Array(src) && opts.length === 0) {
const uint16 = new Uint16Array(src.buffer, src.byteOffset, src.length);
return new Float16Array(uint16.slice().buffer);
}
/**
* limitation: `Object.getOwnPropertyNames(Float16Array)` or `Reflect.ownKeys(Float16Array)` include this key
* @see https://tc39.es/ecma262/#sec-%typedarray%.of
*/
if (opts.length === 0) {
return new Float16Array(Uint16Array.from(src, roundToFloat16Bits).buffer);
}
static of(...items) {
const length = items.length;
const proxy = new Float16Array(length);
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy);
const mapFunc = opts[0];
const thisArg = opts[1];
return new Float16Array(Uint16Array.from(src, function (val, ...args) {
return roundToFloat16Bits(mapFunc.call(this, val, ...args));
}, thisArg).buffer);
}
/**
* limitation: `Object.getOwnPropertyNames(Float16Array)` or `Reflect.ownKeys(Float16Array)` include this key
* @see https://tc39.es/ecma262/#sec-%typedarray%.of
*/
for (let i = 0; i < length; ++i) {
float16bitsArray[i] = roundToFloat16Bits(items[i]);
}
return proxy;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.keys
*/
static of(...items) {
const length = items.length;
const proxy = new Float16Array(length);
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy);
keys() {
assertFloat16BitsArray(this);
return super.keys();
for (let i = 0; i < length; ++i) {
float16bitsArray[i] = roundToFloat16Bits(items[i]);
}
/**
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%`
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.values
*/
return proxy;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.keys
*/
values() {
assertFloat16BitsArray(this);
const arrayIterator = super.values();
return wrapInArrayIterator(function* () {
for (const val of arrayIterator) {
yield convertToNumber(val);
}
}());
}
/**
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%`
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.entries
*/
keys() {
assertFloat16BitsArray(this);
return super.keys();
}
/**
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%`
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.values
*/
entries() {
assertFloat16BitsArray(this);
const arrayIterator = super.entries();
return wrapInArrayIterator(function* () {
for (const [i, val] of arrayIterator) {
yield [i, convertToNumber(val)];
}
}());
}
/**
* @see https://tc39.es/proposal-relative-indexing-method/#sec-%typedarray%.prototype.at
*/
values() {
assertFloat16BitsArray(this);
const arrayIterator = super.values();
return wrapInArrayIterator(function* () {
for (const val of arrayIterator) {
yield convertToNumber(val);
}
}());
}
/**
* limitation: returns a object whose prototype is not `%ArrayIteratorPrototype%`
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.entries
*/
at(index) {
assertFloat16BitsArray(this);
const length = this.length;
const relativeIndex = ToIntegerOrInfinity(index);
const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex;
if (k < 0 || k >= length) {
return;
entries() {
assertFloat16BitsArray(this);
const arrayIterator = super.entries();
return wrapInArrayIterator(function* () {
for (const [i, val] of arrayIterator) {
yield [i, convertToNumber(val)];
}
}());
}
/**
* @see https://tc39.es/proposal-relative-indexing-method/#sec-%typedarray%.prototype.at
*/
return convertToNumber(this[k]);
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.map
*/
at(index) {
assertFloat16BitsArray(this);
const length = this.length;
const relativeIndex = ToIntegerOrInfinity(index);
const k = relativeIndex >= 0 ? relativeIndex : length + relativeIndex;
map(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
const length = this.length;
const Constructor = SpeciesConstructor(this, Float16Array); // for optimization
if (k < 0 || k >= length) {
return;
}
if (Constructor === Float16Array) {
const proxy = new Float16Array(length);
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy);
return convertToNumber(this[k]);
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.map
*/
for (let i = 0; i < length; ++i) {
const val = convertToNumber(this[i]);
float16bitsArray[i] = roundToFloat16Bits(callback.call(thisArg, val, i, _(this).proxy));
}
return proxy;
}
map(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
const length = this.length;
const Constructor = SpeciesConstructor(this, Float16Array); // for optimization
const array = new Constructor(length);
if (Constructor === Float16Array) {
const proxy = new Float16Array(length);
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(proxy);
for (let i = 0; i < length; ++i) {
const val = convertToNumber(this[i]);
array[i] = callback.call(thisArg, val, i, _(this).proxy);
float16bitsArray[i] = roundToFloat16Bits(callback.call(thisArg, val, i, _(this).proxy));
}
return array;
return proxy;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.filter
*/
const array = new Constructor(length);
filter(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
const kept = [];
for (let i = 0; i < length; ++i) {
const val = convertToNumber(this[i]);
array[i] = callback.call(thisArg, val, i, _(this).proxy);
}
for (let i = 0, l = this.length; i < l; ++i) {
const val = convertToNumber(this[i]);
return array;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.filter
*/
if (callback.call(thisArg, val, i, _(this).proxy)) {
kept.push(val);
}
}
const Constructor = SpeciesConstructor(this, Float16Array);
const array = new Constructor(kept);
return array;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce
*/
filter(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
const kept = [];
for (let i = 0, l = this.length; i < l; ++i) {
const val = convertToNumber(this[i]);
reduce(callback, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
if (length === 0 && opts.length === 0) {
throw TypeError("Reduce of empty array with no initial value");
if (callback.call(thisArg, val, i, _(this).proxy)) {
kept.push(val);
}
}
let accumulator, start;
const Constructor = SpeciesConstructor(this, Float16Array);
const array = new Constructor(kept);
return array;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduce
*/
if (opts.length === 0) {
accumulator = convertToNumber(this[0]);
start = 1;
} else {
accumulator = opts[0];
start = 0;
}
for (let i = start; i < length; ++i) {
accumulator = callback(accumulator, convertToNumber(this[i]), i, _(this).proxy);
}
reduce(callback, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
return accumulator;
if (length === 0 && opts.length === 0) {
throw TypeError("Reduce of empty array with no initial value");
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduceright
*/
let accumulator, start;
reduceRight(callback, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
if (opts.length === 0) {
accumulator = convertToNumber(this[0]);
start = 1;
} else {
accumulator = opts[0];
start = 0;
}
if (length === 0 && opts.length === 0) {
throw TypeError("Reduce of empty array with no initial value");
}
for (let i = start; i < length; ++i) {
accumulator = callback(accumulator, convertToNumber(this[i]), i, _(this).proxy);
}
let accumulator, start;
return accumulator;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reduceright
*/
if (opts.length === 0) {
accumulator = convertToNumber(this[length - 1]);
start = length - 2;
} else {
accumulator = opts[0];
start = length - 1;
}
for (let i = start; i >= 0; --i) {
accumulator = callback(accumulator, convertToNumber(this[i]), i, _(this).proxy);
}
reduceRight(callback, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
return accumulator;
if (length === 0 && opts.length === 0) {
throw TypeError("Reduce of empty array with no initial value");
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.foreach
*/
let accumulator, start;
forEach(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
if (opts.length === 0) {
accumulator = convertToNumber(this[length - 1]);
start = length - 2;
} else {
accumulator = opts[0];
start = length - 1;
}
for (let i = 0, l = this.length; i < l; ++i) {
callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy);
}
for (let i = start; i >= 0; --i) {
accumulator = callback(accumulator, convertToNumber(this[i]), i, _(this).proxy);
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.find
*/
return accumulator;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.foreach
*/
find(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
const value = convertToNumber(this[i]);
forEach(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
if (callback.call(thisArg, value, i, _(this).proxy)) {
return value;
}
}
for (let i = 0, l = this.length; i < l; ++i) {
callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy);
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.findindex
*/
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.find
*/
findIndex(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
find(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
const value = convertToNumber(this[i]);
for (let i = 0, l = this.length; i < l; ++i) {
const value = convertToNumber(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return i;
}
if (callback.call(thisArg, value, i, _(this).proxy)) {
return value;
}
return -1;
}
/**
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlast
*/
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.findindex
*/
findLast(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
findIndex(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
for (let i = this.length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);
for (let i = 0, l = this.length; i < l; ++i) {
const value = convertToNumber(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return value;
}
if (callback.call(thisArg, value, i, _(this).proxy)) {
return i;
}
}
/**
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlastindex
*/
return -1;
}
/**
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlast
*/
findLastIndex(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
for (let i = this.length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);
findLast(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
if (callback.call(thisArg, value, i, _(this).proxy)) {
return i;
}
for (let i = this.length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return value;
}
return -1;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.every
*/
}
/**
* @see https://tc39.es/proposal-array-find-from-last/index.html#sec-%typedarray%.prototype.findlastindex
*/
every(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
findLastIndex(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
if (!callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy)) {
return false;
}
for (let i = this.length - 1; i >= 0; --i) {
const value = convertToNumber(this[i]);
if (callback.call(thisArg, value, i, _(this).proxy)) {
return i;
}
return true;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.some
*/
return -1;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.every
*/
some(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
if (callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy)) {
return true;
}
every(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
for (let i = 0, l = this.length; i < l; ++i) {
if (!callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy)) {
return false;
}
return false;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.set
*/
return true;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.some
*/
set(input, ...opts) {
assertFloat16BitsArray(this);
const targetOffset = ToIntegerOrInfinity(opts[0]);
if (targetOffset < 0) {
throw RangeError("offset is out of bounds");
} // for optimization
some(callback, ...opts) {
assertFloat16BitsArray(this);
const thisArg = opts[0];
if (isFloat16Array(input)) {
// peel off Proxy
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(input);
super.set(float16bitsArray, targetOffset);
return;
for (let i = 0, l = this.length; i < l; ++i) {
if (callback.call(thisArg, convertToNumber(this[i]), i, _(this).proxy)) {
return true;
}
}
const targetLength = this.length;
const src = Object(input);
const srcLength = LengthOfArrayLike(src);
return false;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.set
*/
if (targetOffset === Infinity || srcLength + targetOffset > targetLength) {
throw RangeError("offset is out of bounds");
}
for (let i = 0; i < srcLength; ++i) {
this[i + targetOffset] = roundToFloat16Bits(src[i]);
}
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reverse
*/
set(input, ...opts) {
assertFloat16BitsArray(this);
const targetOffset = ToIntegerOrInfinity(opts[0]);
if (targetOffset < 0) {
throw RangeError("offset is out of bounds");
} // for optimization
reverse() {
assertFloat16BitsArray(this);
super.reverse();
return _(this).proxy;
if (isFloat16Array(input)) {
// peel off Proxy
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(input);
super.set(float16bitsArray, targetOffset);
return;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill
*/
const targetLength = this.length;
const src = Object(input);
const srcLength = LengthOfArrayLike(src);
fill(value, ...opts) {
assertFloat16BitsArray(this);
super.fill(roundToFloat16Bits(value), ...opts);
return _(this).proxy;
if (targetOffset === Infinity || srcLength + targetOffset > targetLength) {
throw RangeError("offset is out of bounds");
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.copywithin
*/
copyWithin(target, start, ...opts) {
assertFloat16BitsArray(this);
super.copyWithin(target, start, ...opts);
return _(this).proxy;
for (let i = 0; i < srcLength; ++i) {
this[i + targetOffset] = roundToFloat16Bits(src[i]);
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort
*/
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.reverse
*/
sort(...opts) {
assertFloat16BitsArray(this);
const compare = opts[0] !== undefined ? opts[0] : defaultCompare;
super.sort((x, y) => {
return compare(convertToNumber(x), convertToNumber(y));
});
return _(this).proxy;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.slice
*/
reverse() {
assertFloat16BitsArray(this);
super.reverse();
return _(this).proxy;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill
*/
slice(...opts) {
assertFloat16BitsArray(this);
const Constructor = SpeciesConstructor(this, Float16Array); // for optimization
fill(value, ...opts) {
assertFloat16BitsArray(this);
super.fill(roundToFloat16Bits(value), ...opts);
return _(this).proxy;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.copywithin
*/
if (Constructor === Float16Array) {
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length);
const float16bitsArray = uint16.slice(...opts);
return new Float16Array(float16bitsArray.buffer);
}
const length = this.length;
const start = ToIntegerOrInfinity(opts[0]);
const end = opts[1] === undefined ? length : ToIntegerOrInfinity(opts[1]);
let k;
copyWithin(target, start, ...opts) {
assertFloat16BitsArray(this);
super.copyWithin(target, start, ...opts);
return _(this).proxy;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort
*/
if (start === -Infinity) {
k = 0;
} else if (start < 0) {
k = length + start > 0 ? length + start : 0;
} else {
k = length < start ? length : start;
}
let final;
sort(...opts) {
assertFloat16BitsArray(this);
const compare = opts[0] !== undefined ? opts[0] : defaultCompare;
super.sort((x, y) => {
return compare(convertToNumber(x), convertToNumber(y));
});
return _(this).proxy;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.slice
*/
if (end === -Infinity) {
final = 0;
} else if (end < 0) {
final = length + end > 0 ? length + end : 0;
} else {
final = length < end ? length : end;
}
const count = final - k > 0 ? final - k : 0;
const array = new Constructor(count);
slice(...opts) {
assertFloat16BitsArray(this);
const Constructor = SpeciesConstructor(this, Float16Array); // for optimization
if (count === 0) {
return array;
}
if (Constructor === Float16Array) {
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length);
const float16bitsArray = uint16.slice(...opts);
return new Float16Array(float16bitsArray.buffer);
}
let n = 0;
const length = this.length;
const start = ToIntegerOrInfinity(opts[0]);
const end = opts[1] === undefined ? length : ToIntegerOrInfinity(opts[1]);
let k;
while (k < final) {
array[n] = convertToNumber(this[k]);
++k;
++n;
}
if (start === -Infinity) {
k = 0;
} else if (start < 0) {
k = length + start > 0 ? length + start : 0;
} else {
k = length < start ? length : start;
}
return array;
let final;
if (end === -Infinity) {
final = 0;
} else if (end < 0) {
final = length + end > 0 ? length + end : 0;
} else {
final = length < end ? length : end;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.subarray
*/
const count = final - k > 0 ? final - k : 0;
const array = new Constructor(count);
subarray(...opts) {
assertFloat16BitsArray(this);
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length);
const float16bitsArray = uint16.subarray(...opts);
const Constructor = SpeciesConstructor(this, Float16Array);
const array = new Constructor(float16bitsArray.buffer, float16bitsArray.byteOffset, float16bitsArray.length);
if (count === 0) {
return array;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.indexof
*/
let n = 0;
indexOf(element, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
let from = ToIntegerOrInfinity(opts[0]);
while (k < final) {
array[n] = convertToNumber(this[k]);
++k;
++n;
}
if (from === Infinity) {
return -1;
}
return array;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.subarray
*/
if (from < 0) {
from += length;
if (from < 0) {
from = 0;
}
}
subarray(...opts) {
assertFloat16BitsArray(this);
const uint16 = new Uint16Array(this.buffer, this.byteOffset, this.length);
const float16bitsArray = uint16.subarray(...opts);
const Constructor = SpeciesConstructor(this, Float16Array);
const array = new Constructor(float16bitsArray.buffer, float16bitsArray.byteOffset, float16bitsArray.length);
return array;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.indexof
*/
for (let i = from, l = length; i < l; ++i) {
if (hasOwnProperty.call(this, i) && convertToNumber(this[i]) === element) {
return i;
}
}
indexOf(element, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
let from = ToIntegerOrInfinity(opts[0]);
if (from === Infinity) {
return -1;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.lastindexof
*/
if (from < 0) {
from += length;
lastIndexOf(element, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
let from = opts.length >= 1 ? ToIntegerOrInfinity(opts[0]) : length - 1;
if (from === -Infinity) {
return -1;
if (from < 0) {
from = 0;
}
}
if (from >= 0) {
from = from < length - 1 ? from : length - 1;
} else {
from += length;
for (let i = from, l = length; i < l; ++i) {
if (hasOwnProperty.call(this, i) && convertToNumber(this[i]) === element) {
return i;
}
}
for (let i = from; i >= 0; --i) {
if (hasOwnProperty.call(this, i) && convertToNumber(this[i]) === element) {
return i;
}
}
return -1;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.lastindexof
*/
lastIndexOf(element, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
let from = opts.length >= 1 ? ToIntegerOrInfinity(opts[0]) : length - 1;
if (from === -Infinity) {
return -1;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes
*/
if (from >= 0) {
from = from < length - 1 ? from : length - 1;
} else {
from += length;
}
includes(element, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
let from = ToIntegerOrInfinity(opts[0]);
if (from === Infinity) {
return false;
for (let i = from; i >= 0; --i) {
if (hasOwnProperty.call(this, i) && convertToNumber(this[i]) === element) {
return i;
}
}
if (from < 0) {
from += length;
return -1;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes
*/
if (from < 0) {
from = 0;
}
}
const isNaN = Number.isNaN(element);
includes(element, ...opts) {
assertFloat16BitsArray(this);
const length = this.length;
let from = ToIntegerOrInfinity(opts[0]);
for (let i = from, l = length; i < l; ++i) {
const value = convertToNumber(this[i]);
if (isNaN && Number.isNaN(value)) {
return true;
}
if (value === element) {
return true;
}
}
if (from === Infinity) {
return false;
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.join
*/
if (from < 0) {
from += length;
join(...opts) {
assertFloat16BitsArray(this);
const array = copyToArray(this);
return array.join(...opts);
if (from < 0) {
from = 0;
}
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.tolocalestring
*/
const isNaN = Number.isNaN(element);
toLocaleString(...opts) {
assertFloat16BitsArray(this);
const array = copyToArray(this);
return array.toLocaleString(...opts);
}
/**
* @see https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag
*/
for (let i = from, l = length; i < l; ++i) {
const value = convertToNumber(this[i]);
if (isNaN && Number.isNaN(value)) {
return true;
}
get [Symbol.toStringTag]() {
if (isFloat16BitsArray(this)) {
return "Float16Array";
if (value === element) {
return true;
}
}
return false;
}
/**
* @see https://tc39.es/ecma262/#sec-typedarray.bytes_per_element
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.join
*/
Object.defineProperty(Float16Array, "BYTES_PER_ELEMENT", {
value: Uint16Array.BYTES_PER_ELEMENT
});
join(...opts) {
assertFloat16BitsArray(this);
const array = copyToArray(this);
return array.join(...opts);
}
/**
* limitation: It is peaked by `Object.getOwnPropertySymbols(Float16Array)` and `Reflect.ownKeys(Float16Array)`
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype.tolocalestring
*/
Object.defineProperty(Float16Array, brand, {});
const Float16ArrayPrototype = Float16Array.prototype;
toLocaleString(...opts) {
assertFloat16BitsArray(this);
const array = copyToArray(this);
return array.toLocaleString(...opts);
}
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype-@@iterator
* @see https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag
*/
Object.defineProperty(Float16ArrayPrototype, Symbol.iterator, {
value: Float16ArrayPrototype.values,
writable: true,
configurable: true
});
const defaultFloat16ArrayMethods = new WeakSet();
for (const key of Reflect.ownKeys(Float16ArrayPrototype)) {
// constructor is not callable
if (key === "constructor") {
continue;
get [Symbol.toStringTag]() {
if (isFloat16BitsArray(this)) {
return "Float16Array";
}
}
const val = Float16ArrayPrototype[key];
}
/**
* @see https://tc39.es/ecma262/#sec-typedarray.bytes_per_element
*/
if (typeof val === "function") {
defaultFloat16ArrayMethods.add(val);
}
Object.defineProperty(Float16Array, "BYTES_PER_ELEMENT", {
value: Uint16Array.BYTES_PER_ELEMENT
});
/**
* limitation: It is peaked by `Object.getOwnPropertySymbols(Float16Array)` and `Reflect.ownKeys(Float16Array)`
*/
Object.defineProperty(Float16Array, brand, {});
const Float16ArrayPrototype = Float16Array.prototype;
/**
* @see https://tc39.es/ecma262/#sec-%typedarray%.prototype-@@iterator
*/
Object.defineProperty(Float16ArrayPrototype, Symbol.iterator, {
value: Float16ArrayPrototype.values,
writable: true,
configurable: true
});
const defaultFloat16ArrayMethods = new WeakSet();
for (const key of Reflect.ownKeys(Float16ArrayPrototype)) {
// constructor is not callable
if (key === "constructor") {
continue;
}
/**
* returns an unsigned 16-bit float at the specified byte offset from the start of the DataView.
* @param {DataView} dataView
* @param {number} byteOffset
* @param {[boolean]} opts
* @returns {number}
*/
const val = Float16ArrayPrototype[key];
function getFloat16(dataView, byteOffset, ...opts) {
if (!isDataView(dataView)) {
throw new TypeError("First argument to getFloat16 function must be a DataView");
}
if (typeof val === "function") {
defaultFloat16ArrayMethods.add(val);
}
}
return convertToNumber(dataView.getUint16(byteOffset, ...opts));
/**
* returns an unsigned 16-bit float at the specified byte offset from the start of the DataView.
* @param {DataView} dataView
* @param {number} byteOffset
* @param {[boolean]} opts
* @returns {number}
*/
function getFloat16(dataView, byteOffset, ...opts) {
if (!isDataView(dataView)) {
throw new TypeError("First argument to getFloat16 function must be a DataView");
}
/**
* stores an unsigned 16-bit float value at the specified byte offset from the start of the DataView.
* @param {DataView} dataView
* @param {number} byteOffset
* @param {number} value
* @param {[boolean]} opts
*/
function setFloat16(dataView, byteOffset, value, ...opts) {
if (!isDataView(dataView)) {
throw new TypeError("First argument to setFloat16 function must be a DataView");
}
return convertToNumber(dataView.getUint16(byteOffset, ...opts));
}
/**
* stores an unsigned 16-bit float value at the specified byte offset from the start of the DataView.
* @param {DataView} dataView
* @param {number} byteOffset
* @param {number} value
* @param {[boolean]} opts
*/
dataView.setUint16(byteOffset, roundToFloat16Bits(value), ...opts);
function setFloat16(dataView, byteOffset, value, ...opts) {
if (!isDataView(dataView)) {
throw new TypeError("First argument to setFloat16 function must be a DataView");
}
exports.Float16Array = Float16Array;
exports.getFloat16 = getFloat16;
exports.hfround = hfround;
exports.isFloat16Array = isFloat16Array;
exports.setFloat16 = setFloat16;
dataView.setUint16(byteOffset, roundToFloat16Bits(value), ...opts);
}
Object.defineProperty(exports, '__esModule', { value: true });
exports.Float16Array = Float16Array;
exports.getFloat16 = getFloat16;
exports.hfround = hfround;
exports.isFloat16Array = isFloat16Array;
exports.setFloat16 = setFloat16;
return exports;
Object.defineProperty(exports, '__esModule', { value: true });
return exports;
}({}));

@@ -335,3 +335,3 @@ /**

* Returns `true` if the value is a Float16Array instance.
* @since v3.3.4
* @since v3.4.0
*/

@@ -338,0 +338,0 @@ export declare function isFloat16Array(value: unknown): value is Float16Array;

@@ -75,5 +75,4 @@ "use strict";

/**
* peel off Proxy
* @param {Float16Array} float16
* @return {Float16Array}
* @return {ArrayLike<number>}
*/

@@ -83,3 +82,3 @@

function getFloat16BitsArrayFromFloat16Array(float16) {
let target = _(float16).target; // from another realm
let target = _(float16).target; // from other realms

@@ -95,12 +94,3 @@

/**
* @param {unknown} target
* @returns {boolean}
*/
function isDefaultFloat16ArrayMethods(target) {
return typeof target === "function" && defaultFloat16ArrayMethods.has(target);
}
/**
* @param {Float16Array} float16bitsArray
* @param {ArrayLike<number>} float16bitsArray
* @return {number[]}

@@ -115,3 +105,3 @@ */

for (let i = 0; i < length; ++i) {
array.push((0, _lib.convertToNumber)(float16bitsArray[i]));
array[i] = (0, _lib.convertToNumber)(float16bitsArray[i]);
}

@@ -121,2 +111,11 @@

}
/**
* @param {unknown} target
* @returns {boolean}
*/
function isDefaultFloat16ArrayMethods(target) {
return typeof target === "function" && defaultFloat16ArrayMethods.has(target);
}
/** @type {ProxyHandler<Function>} */

@@ -137,2 +136,3 @@

});
const hasOwnProperty = Object.prototype.hasOwnProperty;
/** @type {ProxyHandler<Float16Array>} */

@@ -142,33 +142,31 @@

get(target, key) {
if ((0, _is.isCanonicalIntegerIndexString)(key)) {
const raw = Reflect.get(target, key);
return raw !== undefined ? (0, _lib.convertToNumber)(raw) : undefined;
} else {
const ret = Reflect.get(target, key);
if ((0, _is.isCanonicalIntegerIndexString)(key) && hasOwnProperty.call(target, key)) {
return (0, _lib.convertToNumber)(Reflect.get(target, key));
}
if (!isDefaultFloat16ArrayMethods(ret)) {
return ret;
} // TypedArray methods can't be called by Proxy Object
const ret = Reflect.get(target, key);
if (!isDefaultFloat16ArrayMethods(ret)) {
return ret;
} // TypedArray methods can't be called by Proxy Object
let proxy = _(ret).proxy;
if (proxy === undefined) {
proxy = _(ret).proxy = new Proxy(ret, applyHandler);
}
let proxy = _(ret).proxy;
return proxy;
if (proxy === undefined) {
proxy = _(ret).proxy = new Proxy(ret, applyHandler);
}
return proxy;
},
set(target, key, value) {
if ((0, _is.isCanonicalIntegerIndexString)(key)) {
if ((0, _is.isCanonicalIntegerIndexString)(key) && hasOwnProperty.call(target, key)) {
return Reflect.set(target, key, (0, _lib.roundToFloat16Bits)(value));
} else {
return Reflect.set(target, key, value);
}
return Reflect.set(target, key, value);
}
});
const hasOwnProperty = Object.prototype.hasOwnProperty;
/**

@@ -186,3 +184,4 @@ * limitation: see README.md for details

// peel off Proxy
super(getFloat16BitsArrayFromFloat16Array(input)); // object without ArrayBuffer
const float16bitsArray = getFloat16BitsArrayFromFloat16Array(input);
super(float16bitsArray); // object without ArrayBuffer
} else if ((0, _is.isObject)(input) && !(0, _is.isArrayBuffer)(input)) {

@@ -189,0 +188,0 @@ let list;

@@ -8,5 +8,5 @@ "use strict";

exports.isObjectLike = isObjectLike;
exports.isDataView = isDataView;
exports.isTypedArray = isTypedArray;
exports.isUint16Array = isUint16Array;
exports.isDataView = isDataView;
exports.isArrayBuffer = isArrayBuffer;

@@ -17,4 +17,2 @@ exports.isSharedArrayBuffer = isSharedArrayBuffer;

var _spec = require("./spec.js");
/**

@@ -35,12 +33,2 @@ * @param {unknown} value

return value !== null && typeof value === "object";
}
const toString = Object.prototype.toString;
/**
* @param {unknown} value
* @returns {value is DataView}
*/
function isDataView(value) {
return ArrayBuffer.isView(value) && toString.call(value) === "[object DataView]";
} // Inspired by util.types implementation of Node.js

@@ -68,4 +56,26 @@

}
const toString = Object.prototype.toString;
/**
* @param {unknown} value
* @returns {value is DataView}
*/
function isDataView(value) {
if (!ArrayBuffer.isView(value)) {
return false;
}
if (isTypedArray(value)) {
return false;
}
if (toString.call(value) !== "[object DataView]") {
return false;
}
return true;
}
/**
* @param {unknown} value
* @returns {value is ArrayBuffer}

@@ -97,3 +107,3 @@ */

/**
* @param {unknown} key
* @param {unknown} value
* @returns {value is string}

@@ -103,4 +113,22 @@ */

function isCanonicalIntegerIndexString(key) {
return typeof key === "string" && key === (0, _spec.ToIntegerOrInfinity)(key) + "";
function isCanonicalIntegerIndexString(value) {
if (typeof value !== "string") {
return false;
}
const number = Number(value);
if (value !== number + "") {
return false;
}
if (!Number.isFinite(number)) {
return false;
}
if (number !== Math.trunc(number)) {
return false;
}
return true;
}

@@ -11,15 +11,8 @@ "use strict";

var _is = require("./is.js");
/**
* @param {unknown} value
* @returns {value is object}
*/
function isObject(value) {
return value !== null && typeof value === "object" || typeof value === "function";
}
/**
* @param {unknown} target
* @returns {number}
*/
function ToIntegerOrInfinity(target) {

@@ -64,3 +57,3 @@ const number = Number(target);

function LengthOfArrayLike(arrayLike) {
if (!isObject(arrayLike)) {
if (!(0, _is.isObject)(arrayLike)) {
throw TypeError("this is not a object");

@@ -79,3 +72,3 @@ }

function SpeciesConstructor(target, defaultConstructor) {
if (!isObject(target)) {
if (!(0, _is.isObject)(target)) {
throw TypeError("this is not a object");

@@ -90,3 +83,3 @@ }

if (!isObject(constructor)) {
if (!(0, _is.isObject)(constructor)) {
throw TypeError("constructor is not a object");

@@ -93,0 +86,0 @@ }

{
"name": "@petamoriken/float16",
"description": "half precision floating point for JavaScript",
"version": "3.4.0",
"version": "3.4.1",
"main": "./index.js",

@@ -52,3 +52,3 @@ "module": "./index.mjs",

"lint": "eslint *.js src/**/*.mjs test/**/*.js test/**/*.mjs",
"test": "nyc --reporter=lcov mocha",
"test": "nyc --reporter=lcov mocha test/*.js",
"test-browser": "nightwatch -e chrome,chrome_old,firefox,firefox_old,firefox_esr,edge,edge_old,safari,safari_old",

@@ -71,5 +71,5 @@ "setup-test-browser": "http-server docs/test -p 8000 > /dev/null 2>&1 &",

"espower-cli": "^1.1.0",
"espower-loader": "^1.2.2",
"exorcist": "^2.0.0",
"http-server": "^13.0.0",
"intelli-espower-loader": "^1.0.1",
"mocha": "^9.0.2",

@@ -76,0 +76,0 @@ "nightwatch": "^1.6.4",

@@ -13,4 +13,10 @@ # <a href="https://git.io/float16">@petamoriken/float16</a>

<a href="https://www.npmjs.com/package/@petamoriken/float16">
<img src="https://img.shields.io/npm/v/@petamoriken/float16.svg?label=version&amp;style=flat-square" alt="npm">
<img src="https://img.shields.io/npm/v/@petamoriken/float16.svg?label=version&amp;logo=npm&amp;style=flat-square" alt="npm version">
</a>
<a href="https://deno.land/x/float16">
<img src="https://img.shields.io/github/v/tag/petamoriken/float16?label=version&amp;logo=deno&amp;style=flat-square" alt="deno version">
</a>
<a href="https://github.com/petamoriken/float16/blob/master/package.json">
<img src="https://img.shields.io/david/petamoriken/float16?style=flat-square" alt="dependencies">
</a>
<a href="https://github.com/petamoriken/float16/blob/master/LICENSE">

@@ -37,3 +43,3 @@ <img src="https://img.shields.io/npm/l/@petamoriken/float16.svg?style=flat-square" alt="license">

```console
npm install @petamoriken/float16 --save
npm install @petamoriken/float16
```

@@ -57,3 +63,3 @@

Serve `browser/float16.mjs` / `browser/float16.js` files from your Web server as the JavaScript `Content-Type`.
Deliver a `browser/float16.mjs` or `browser/float16.js` file from your Web server with the JavaScript `Content-Type` HTTP header.

@@ -75,3 +81,3 @@ ```html

Or use [jsDelivr](https://www.jsdelivr.com/) CDN.
Or use [jsDelivr](https://cdn.jsdelivr.net/npm/@petamoriken/float16/) CDN.

@@ -93,3 +99,3 @@ ```html

ES modules are also available on the [Skypack](https://www.skypack.dev/) CDN.
ES modules are also available on the [Skypack](https://www.skypack.dev/view/@petamoriken/float16) CDN.

@@ -105,2 +111,4 @@ ```html

You can get modules from [deno.land/x](https://deno.land/x/float16) hosting service.
```ts

@@ -129,3 +137,3 @@ import { Float16Array, isFloat16Array, getFloat16, setFloat16, hfround } from "https://deno.land/x/float16/index.mjs";

This API is similar to `TypedArray` such as `Float32Array` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array)).
`Float16Array` is similar to `TypedArray` such as `Float32Array` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array)).

@@ -143,2 +151,4 @@ ```js

`isFloat16Array` is a utility function to check whether the value given as an argument is an instance of `Float16Array` or not.
```ts

@@ -149,5 +159,5 @@ declare function isFloat16Array(value: unknown): value is Float16Array;

```js
isFloat16Array(new Float16Array()); // true
isFloat16Array(new Float32Array()); // false
isFloat16Array(new Uint16Array()); // false
isFloat16Array(new Float16Array(10)); // true
isFloat16Array(new Float32Array(10)); // false
isFloat16Array(new Uint16Array(10)); // false
```

@@ -157,2 +167,4 @@

`getFloat16` and `setFloat16` are similar to `DataView` methods such as `DataView#getFloat32` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat32)) and `DataView#setFloat32` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat32)).
```ts

@@ -163,4 +175,2 @@ declare function getFloat16(view: DataView, byteOffset: number, littleEndian?: boolean): number;

These APIs are similar to `DataView` methods such as `DataView#getFloat32` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat32)) and `DataView#setFloat32` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat32)).
```js

@@ -185,2 +195,5 @@ const buffer = new ArrayBuffer(10);

`hfround` is similar to `Math.fround` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround)).
This function returns nearest half precision float representation of a number.
```ts

@@ -190,5 +203,2 @@ declare function hfround(x: number): number;

This API is similar to `Math.fround` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround)).
This function returns nearest half precision float representation of a number.
```js

@@ -231,3 +241,3 @@ Math.fround(1.337); // 1.3370000123977661

E.g. `ArrayBuffer.isView` is the butlt-in method that checks if it has the `[[ViewedArrayBuffer]]` internal slot. It returns `false` for `Proxy` object such as `Float16Array`.
E.g. `ArrayBuffer.isView` is the butlt-in method that checks if it has the `[[ViewedArrayBuffer]]` internal slot. It returns `false` for `Proxy` object such as `Float16Array` instance.

@@ -239,2 +249,13 @@ ```js

### The structured clone algorithm (Web Workers, IndexedDB, etc)
The structured clone algorithm copies complex JavaScript objects. It is used internally when invoking `structuredClone()`, to transfer data between Web Workers via `postMessage()`, storing objects with IndexedDB, or copying objects for other APIs ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)).
It can't clone `Proxy` object such as `Float16Array` instance, you need to convert it to `Uint16Array` or deal with `ArrayBuffer` directly.
```js
const array = new Float16Array([1.0, 1.1, 1.2]);
const cloned = structuredClone({ buffer: array.buffer });
```
### WebGL

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

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