Skip to content

Commit

Permalink
Merge pull request zloirock#1379 from LeviPesin/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock authored Nov 15, 2024
2 parents 743f9e6 + ff1c855 commit a1298dc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
19 changes: 17 additions & 2 deletions packages/core-js/modules/esnext.data-view.get-float16.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var unpackIEEE754 = require('../internals/ieee754').unpack;

var pow = Math.pow;

var EXP_MASK16 = 31; // 2 ** 5 - 1
var SIGNIFICAND_MASK16 = 1023; // 2 ** 10 - 1
var MIN_SUBNORMAL16 = pow(2, -24); // 2 ** -10 * 2 ** -14
var SIGNIFICAND_DENOM16 = 0.0009765625; // 2 ** -10

var unpackFloat16 = function (bytes) {
var sign = bytes >>> 15;
var exponent = bytes >>> 10 & EXP_MASK16;
var significand = bytes & SIGNIFICAND_MASK16;
if (exponent === EXP_MASK16) return significand === 0 ? (sign === 0 ? Infinity : -Infinity) : NaN;
if (exponent === 0) return significand * (sign === 0 ? MIN_SUBNORMAL16 : -MIN_SUBNORMAL16);
return pow(2, exponent - 15) * (sign === 0 ? 1 + significand * SIGNIFICAND_DENOM16 : -1 - significand * SIGNIFICAND_DENOM16);
};

// eslint-disable-next-line es/no-typed-arrays -- safe
var getUint16 = uncurryThis(DataView.prototype.getUint16);
Expand All @@ -11,6 +26,6 @@ var getUint16 = uncurryThis(DataView.prototype.getUint16);
$({ target: 'DataView', proto: true }, {
getFloat16: function getFloat16(byteOffset /* , littleEndian */) {
var uint16 = getUint16(this, byteOffset, arguments.length > 1 ? arguments[1] : false);
return unpackIEEE754([uint16 & 0xFF, uint16 >> 8 & 0xFF], 10);
return unpackFloat16(uint16);
}
});
46 changes: 43 additions & 3 deletions packages/core-js/modules/esnext.data-view.set-float16.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,49 @@ var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var aDataView = require('../internals/a-data-view');
var toIndex = require('../internals/to-index');
var packIEEE754 = require('../internals/ieee754').pack;
var f16round = require('../internals/math-f16round');

var pow = Math.pow;
var log = Math.log;
var LN2 = Math.LN2;

var EPSILON = 2.220446049250313e-16; // Number.EPSILON
var INVERSE_EPSILON = 1 / EPSILON;

var roundTiesToEven = function (n) {
return n + INVERSE_EPSILON - INVERSE_EPSILON;
};

var MIN_INFINITY16 = 65520; // (2 - 2 ** -11) * 2 ** 15
var MIN_NORMAL16 = 0.000061005353927612305; // (1 - 2 ** -11) * 2 ** -14
var REC_MIN_SUBNORMAL16 = 16777216; // 2 ** 10 * 2 ** 14
var REC_SIGNIFICAND_DENOM16 = 1024; // 2 ** 10;

var packFloat16 = function (value) {
// eslint-disable-next-line no-self-compare -- NaN check
if (value !== value) return 0x7E00; // NaN
if (value === 0) return (1 / value === -Infinity) << 15; // +0 or -0

var neg = value < 0;
if (neg) value = -value;
if (value >= MIN_INFINITY16) return neg << 15 | 0x7C00; // Infinity
if (value < MIN_NORMAL16) return neg << 15 | roundTiesToEven(value * REC_MIN_SUBNORMAL16); // subnormal

// normal
var exponent = log(value) / LN2 | 0;
if (exponent === -15) {
// we round from a value between 2 ** -15 * (1 + 1022/1024) (the largest subnormal) and 2 ** -14 * (1 + 0/1024) (the smallest normal)
// to the latter (former impossible because of the subnormal check above)
return neg << 15 | REC_SIGNIFICAND_DENOM16;
}
var significand = roundTiesToEven((value * pow(2, -exponent) - 1) * REC_SIGNIFICAND_DENOM16);
if (significand === REC_SIGNIFICAND_DENOM16) {
// we round from a value between 2 ** n * (1 + 1023/1024) and 2 ** (n + 1) * (1 + 0/1024) to the latter
return neg << 15 | exponent + 16 << 10;
}
return neg << 15 | exponent + 15 << 10 | significand;
};

// eslint-disable-next-line es/no-typed-arrays -- safe
var setUint16 = uncurryThis(DataView.prototype.setUint16);

Expand All @@ -15,7 +55,7 @@ $({ target: 'DataView', proto: true }, {
setFloat16: function setFloat16(byteOffset, value /* , littleEndian */) {
aDataView(this);
var offset = toIndex(byteOffset);
var bytes = packIEEE754(f16round(value), 10, 2);
return setUint16(this, offset, bytes[1] << 8 | bytes[0], arguments.length > 2 ? arguments[2] : false);
var bytes = packFloat16(f16round(value));
return setUint16(this, offset, bytes, arguments.length > 2 ? arguments[2] : false);
}
});

0 comments on commit a1298dc

Please sign in to comment.