Skip to content

Commit

Permalink
move Math.f16round and `DataView.prototype.{ getFloat16, setFloat16…
Browse files Browse the repository at this point in the history
… }` to stable ES
  • Loading branch information
zloirock committed Feb 18, 2025
1 parent 8eee102 commit 920033c
Show file tree
Hide file tree
Showing 29 changed files with 221 additions and 161 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Changelog
##### Unreleased
- [`Float16` proposal](https://github.com/tc39/proposal-regex-escaping):
- Built-ins:
- `Math.f16round`
- `DataView.prototype.getFloat16`
- `DataView.prototype.setFloat16`
- Moved to stable ES, February 2025 TC39 meeting
- Added `es.` namespace modules, `/es/` and `/stable/` namespaces entries
- Compat data improvements:
- [`DisposableStack`, `AsyncDisposableStack`, `SuppressedError` and `Iterator.prototype[@@dispose]`](https://github.com/tc39/proposal-explicit-resource-management) marked as [shipped from V8 ~ Chromium 134](https://issues.chromium.org/issues/42203506#comment24)
- [`Error.isError`](https://github.com/tc39/proposal-is-error) added and marked as supported from V8 ~ Chromium 134
Expand Down
56 changes: 25 additions & 31 deletions README.md

Large diffs are not rendered by default.

45 changes: 27 additions & 18 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,18 @@ export const data = {
rhino: '1.7.13',
safari: '7.1',
},
'es.data-view.get-float16': {
bun: '1.1.23',
deno: '1.43',
firefox: '129',
safari: '18.2',
},
'es.data-view.set-float16': {
bun: '1.1.23',
deno: '1.43',
firefox: '129',
safari: '18.2',
},
'es.array-buffer.detached': {
bun: '1.0.19',
chrome: '114',
Expand Down Expand Up @@ -793,6 +805,12 @@ export const data = {
rhino: '1.7.13',
safari: '7.1',
},
'es.math.f16round': {
bun: '1.1.23',
deno: '1.43',
firefox: '129',
safari: '18.2',
},
'es.math.hypot': {
// https://bugs.chromium.org/p/v8/issues/detail?id=9546
chrome: '78', // '38',
Expand Down Expand Up @@ -2290,20 +2308,12 @@ export const data = {
},
'esnext.composite-symbol': {
},
'esnext.data-view.get-float16': {
bun: '1.1.23',
deno: '1.43',
firefox: '129',
safari: '18.2',
},
// TODO: Remove from `core-js@4`
'esnext.data-view.get-float16': null,
'esnext.data-view.get-uint8-clamped': {
},
'esnext.data-view.set-float16': {
bun: '1.1.23',
deno: '1.43',
firefox: '129',
safari: '18.2',
},
// TODO: Remove from `core-js@4`
'esnext.data-view.set-float16': null,
'esnext.data-view.set-uint8-clamped': {
},
'esnext.disposable-stack.constructor': {
Expand Down Expand Up @@ -2444,12 +2454,8 @@ export const data = {
},
'esnext.math.fscale': {
},
'esnext.math.f16round': {
bun: '1.1.23',
deno: '1.43',
firefox: '129',
safari: '18.2',
},
// TODO: Remove from `core-js@4`
'esnext.math.f16round': null,
// TODO: Remove from `core-js@4`
'esnext.math.iaddh': {
},
Expand Down Expand Up @@ -2998,6 +3004,8 @@ export const renamed = new Map([
['esnext.array-buffer.transfer', 'es.array-buffer.transfer'],
['esnext.array-buffer.transfer-to-fixed-length', 'es.array-buffer.transfer-to-fixed-length'],
['esnext.aggregate-error', 'es.aggregate-error'],
['esnext.data-view.get-float16', 'es.data-view.get-float16'],
['esnext.data-view.set-float16', 'es.data-view.set-float16'],
['esnext.global-this', 'es.global-this'],
['esnext.iterator.constructor', 'es.iterator.constructor'],
['esnext.iterator.drop', 'es.iterator.drop'],
Expand All @@ -3013,6 +3021,7 @@ export const renamed = new Map([
['esnext.iterator.take', 'es.iterator.take'],
['esnext.iterator.to-array', 'es.iterator.to-array'],
['esnext.map.group-by', 'es.map.group-by'],
['esnext.math.f16round', 'es.math.f16round'],
['esnext.object.has-own', 'es.object.has-own'],
['esnext.object.group-by', 'es.object.group-by'],
['esnext.promise.all-settled', 'es.promise.all-settled'],
Expand Down
5 changes: 5 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,9 @@ export default {
'3.40': [
'esnext.error.is-error',
],
3.41: [
'es.data-view.get-float16',
'es.data-view.set-float16',
'es.math.f16round',
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
3 changes: 3 additions & 0 deletions packages/core-js/actual/data-view/get-float16.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
'use strict';
var parent = require('../../stable/data-view/get-float16');
require('../../modules/esnext.data-view.get-float16');

module.exports = parent;
3 changes: 3 additions & 0 deletions packages/core-js/actual/data-view/set-float16.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
'use strict';
var parent = require('../../stable/data-view/set-float16');
require('../../modules/esnext.data-view.set-float16');

module.exports = parent;
4 changes: 2 additions & 2 deletions packages/core-js/actual/math/f16round.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var parent = require('../../stable/math/f16round');
require('../../modules/esnext.math.f16round');
var path = require('../../internals/path');

module.exports = path.Math.f16round;
module.exports = parent;
2 changes: 2 additions & 0 deletions packages/core-js/es/data-view/get-float16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
require('../../modules/es.data-view.get-float16');
2 changes: 2 additions & 0 deletions packages/core-js/es/data-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require('../../modules/es.array-buffer.constructor');
require('../../modules/es.array-buffer.slice');
require('../../modules/es.data-view');
require('../../modules/es.data-view.get-float16');
require('../../modules/es.data-view.set-float16');
require('../../modules/es.object.to-string');
var path = require('../../internals/path');

Expand Down
2 changes: 2 additions & 0 deletions packages/core-js/es/data-view/set-float16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
require('../../modules/es.data-view.set-float16');
5 changes: 5 additions & 0 deletions packages/core-js/es/math/f16round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';
require('../../modules/es.math.f16round');
var path = require('../../internals/path');

module.exports = path.Math.f16round;
1 change: 1 addition & 0 deletions packages/core-js/es/math/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require('../../modules/es.math.clz32');
require('../../modules/es.math.cosh');
require('../../modules/es.math.expm1');
require('../../modules/es.math.fround');
require('../../modules/es.math.f16round');
require('../../modules/es.math.hypot');
require('../../modules/es.math.imul');
require('../../modules/es.math.log10');
Expand Down
31 changes: 31 additions & 0 deletions packages/core-js/modules/es.data-view.get-float16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');

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);

// `DataView.prototype.getFloat16` method
// https://github.com/tc39/proposal-float16array
$({ target: 'DataView', proto: true }, {
getFloat16: function getFloat16(byteOffset /* , littleEndian */) {
var uint16 = getUint16(this, byteOffset, arguments.length > 1 ? arguments[1] : false);
return unpackFloat16(uint16);
}
});
54 changes: 54 additions & 0 deletions packages/core-js/modules/es.data-view.set-float16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var aDataView = require('../internals/a-data-view');
var toIndex = require('../internals/to-index');
// TODO: Replace with module dependency in `core-js@4`
var log2 = require('../internals/math-log2');
var roundTiesToEven = require('../internals/math-round-ties-to-even');

var pow = Math.pow;

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 = log2(value) | 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);

// `DataView.prototype.setFloat16` method
// https://github.com/tc39/proposal-float16array
$({ target: 'DataView', proto: true }, {
setFloat16: function setFloat16(byteOffset, value /* , littleEndian */) {
aDataView(this);
var offset = toIndex(byteOffset);
var bytes = packFloat16(+value);
return setUint16(this, offset, bytes, arguments.length > 2 ? arguments[2] : false);
}
});
15 changes: 15 additions & 0 deletions packages/core-js/modules/es.math.f16round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';
var $ = require('../internals/export');
var floatRound = require('../internals/math-float-round');

var FLOAT16_EPSILON = 0.0009765625;
var FLOAT16_MAX_VALUE = 65504;
var FLOAT16_MIN_VALUE = 6.103515625e-05;

// `Math.f16round` method
// https://github.com/tc39/proposal-float16array
$({ target: 'Math', stat: true }, {
f16round: function f16round(x) {
return floatRound(x, FLOAT16_EPSILON, FLOAT16_MAX_VALUE, FLOAT16_MIN_VALUE);
}
});
32 changes: 2 additions & 30 deletions packages/core-js/modules/esnext.data-view.get-float16.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');

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);

// `DataView.prototype.getFloat16` method
// https://github.com/tc39/proposal-float16array
$({ target: 'DataView', proto: true }, {
getFloat16: function getFloat16(byteOffset /* , littleEndian */) {
var uint16 = getUint16(this, byteOffset, arguments.length > 1 ? arguments[1] : false);
return unpackFloat16(uint16);
}
});
// TODO: Remove from `core-js@4`
require('../modules/es.data-view.get-float16');
55 changes: 2 additions & 53 deletions packages/core-js/modules/esnext.data-view.set-float16.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,3 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var aDataView = require('../internals/a-data-view');
var toIndex = require('../internals/to-index');
// TODO: Replace with module dependency in `core-js@4`
var log2 = require('../internals/math-log2');
var roundTiesToEven = require('../internals/math-round-ties-to-even');

var pow = Math.pow;

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 = log2(value) | 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);

// `DataView.prototype.setFloat16` method
// https://github.com/tc39/proposal-float16array
$({ target: 'DataView', proto: true }, {
setFloat16: function setFloat16(byteOffset, value /* , littleEndian */) {
aDataView(this);
var offset = toIndex(byteOffset);
var bytes = packFloat16(+value);
return setUint16(this, offset, bytes, arguments.length > 2 ? arguments[2] : false);
}
});
// TODO: Remove from `core-js@4`
require('../modules/es.data-view.set-float16');
16 changes: 2 additions & 14 deletions packages/core-js/modules/esnext.math.f16round.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
'use strict';
var $ = require('../internals/export');
var floatRound = require('../internals/math-float-round');

var FLOAT16_EPSILON = 0.0009765625;
var FLOAT16_MAX_VALUE = 65504;
var FLOAT16_MIN_VALUE = 6.103515625e-05;

// `Math.f16round` method
// https://github.com/tc39/proposal-float16array
$({ target: 'Math', stat: true }, {
f16round: function f16round(x) {
return floatRound(x, FLOAT16_EPSILON, FLOAT16_MAX_VALUE, FLOAT16_MIN_VALUE);
}
});
// TODO: Remove from `core-js@4`
require('../modules/es.math.f16round');
4 changes: 4 additions & 0 deletions packages/core-js/stable/data-view/get-float16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
var parent = require('../../es/data-view/get-float16');

module.exports = parent;
4 changes: 4 additions & 0 deletions packages/core-js/stable/data-view/set-float16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
var parent = require('../../es/data-view/set-float16');

module.exports = parent;
4 changes: 4 additions & 0 deletions packages/core-js/stable/math/f16round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';
var parent = require('../../es/math/f16round');

module.exports = parent;
Loading

0 comments on commit 920033c

Please sign in to comment.