Skip to content

Commit 5bc70f6

Browse files
committed
add some float16 methods
1 parent 5fae871 commit 5bc70f6

File tree

16 files changed

+176
-0
lines changed

16 files changed

+176
-0
lines changed

packages/core-js-compat/src/data.mjs

+6
Original file line numberDiff line numberDiff line change
@@ -1973,8 +1973,12 @@ export const data = {
19731973
},
19741974
'esnext.composite-symbol': {
19751975
},
1976+
'esnext.data-view.get-float16': {
1977+
},
19761978
'esnext.data-view.get-uint8-clamped': {
19771979
},
1980+
'esnext.data-view.set-float16': {
1981+
},
19781982
'esnext.data-view.set-uint8-clamped': {
19791983
},
19801984
'esnext.disposable-stack.constructor': {
@@ -2088,6 +2092,8 @@ export const data = {
20882092
},
20892093
'esnext.math.fscale': {
20902094
},
2095+
'esnext.math.f16round': {
2096+
},
20912097
// TODO: Remove from `core-js@4`
20922098
'esnext.math.iaddh': {
20932099
},

packages/core-js-compat/src/modules-by-versions.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ export default {
214214
'web.url-search-params.has',
215215
],
216216
3.32: [
217+
'esnext.data-view.get-float16',
217218
'esnext.data-view.get-uint8-clamped',
219+
'esnext.data-view.set-float16',
218220
'esnext.data-view.set-uint8-clamped',
221+
'esnext.math.f16round',
219222
],
220223
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22
var parent = require('../../actual/data-view');
3+
require('../../modules/esnext.data-view.get-float16');
34
require('../../modules/esnext.data-view.get-uint8-clamped');
5+
require('../../modules/esnext.data-view.set-float16');
46
require('../../modules/esnext.data-view.set-uint8-clamped');
57

68
module.exports = parent;
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
require('../../modules/esnext.math.f16round');
3+
var path = require('../../internals/path');
4+
5+
module.exports = path.Math.f16round;

packages/core-js/full/math/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ require('../../modules/esnext.math.clamp');
44
require('../../modules/esnext.math.deg-per-rad');
55
require('../../modules/esnext.math.degrees');
66
require('../../modules/esnext.math.fscale');
7+
require('../../modules/esnext.math.f16round');
78
require('../../modules/esnext.math.rad-per-deg');
89
require('../../modules/esnext.math.radians');
910
require('../../modules/esnext.math.scale');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
var $ = require('../internals/export');
3+
var uncurryThis = require('../internals/function-uncurry-this');
4+
var unpackIEEE754 = require('../internals/ieee754').unpack;
5+
6+
// eslint-disable-next-line es/no-typed-arrays -- safe
7+
var getUint16 = uncurryThis(DataView.prototype.getUint16);
8+
9+
// `DataView.prototype.getFloat16` method
10+
// https://github.com/tc39/proposal-float16array
11+
$({ target: 'DataView', proto: true, forced: true }, {
12+
getFloat16: function getFloat16(byteOffset /* , littleEndian */) {
13+
var uint16 = getUint16(this, byteOffset, arguments.length > 1 ? arguments[1] : false);
14+
return unpackIEEE754([uint16 & 0xFF, uint16 >> 8 & 0xFF], 10);
15+
}
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
var $ = require('../internals/export');
3+
var uncurryThis = require('../internals/function-uncurry-this');
4+
var classof = require('../internals/classof');
5+
var toIndex = require('../internals/to-index');
6+
var packIEEE754 = require('../internals/ieee754').pack;
7+
8+
var $TypeError = TypeError;
9+
// eslint-disable-next-line es/no-typed-arrays -- safe
10+
var setUint16 = uncurryThis(DataView.prototype.setUint16);
11+
12+
// `DataView.prototype.setFloat16` method
13+
// https://github.com/tc39/proposal-float16array
14+
$({ target: 'DataView', proto: true, forced: true }, {
15+
setFloat16: function setFloat16(byteOffset, value /* , littleEndian */) {
16+
if (classof(this) !== 'DataView') throw $TypeError('Incorrect receiver');
17+
var offset = toIndex(byteOffset);
18+
var bytes = packIEEE754(+value, 10, 2);
19+
return setUint16(this, offset, bytes[1] << 8 | bytes[0], arguments.length > 2 ? arguments[2] : false);
20+
}
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
var $ = require('../internals/export');
3+
var IEEE754 = require('../internals/ieee754');
4+
5+
var packIEEE754 = IEEE754.pack;
6+
var unpackIEEE754 = IEEE754.unpack;
7+
var $isFinite = isFinite;
8+
9+
// `Math.f16round` method
10+
// https://github.com/tc39/proposal-float16array
11+
$({ target: 'Math', stat: true }, {
12+
f16round: function f16round(x) {
13+
var n = +x;
14+
return $isFinite(n) && n !== 0 ? unpackIEEE754(packIEEE754(n, 10, 2), 10) : n;
15+
}
16+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
// https://github.com/tc39/proposal-float16array
3+
require('../modules/esnext.data-view.get-float16');
4+
require('../modules/esnext.data-view.set-float16');
5+
require('../modules/esnext.math.f16round');

packages/core-js/stage/3.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require('../proposals/array-from-async-stage-2');
66
require('../proposals/array-grouping-v2');
77
require('../proposals/decorator-metadata-v2');
88
require('../proposals/explicit-resource-management');
9+
require('../proposals/float16array');
910
require('../proposals/iterator-helpers-stage-3-2');
1011
require('../proposals/json-parse-with-source');
1112
require('../proposals/promise-with-resolvers');

tests/compat/tests.js

+9
Original file line numberDiff line numberDiff line change
@@ -1532,9 +1532,15 @@ GLOBAL.tests = {
15321532
'esnext.composite-symbol': function () {
15331533
return compositeSymbol;
15341534
},
1535+
'esnext.data-view.get-float16': [ARRAY_BUFFER_SUPPORT, function () {
1536+
return DataView.prototype.getFloat16;
1537+
}],
15351538
'esnext.data-view.get-uint8-clamped': [ARRAY_BUFFER_SUPPORT, function () {
15361539
return DataView.prototype.getUint8Clamped;
15371540
}],
1541+
'esnext.data-view.set-float16': [ARRAY_BUFFER_SUPPORT, function () {
1542+
return DataView.prototype.setFloat16;
1543+
}],
15381544
'esnext.data-view.set-uint8-clamped': [ARRAY_BUFFER_SUPPORT, function () {
15391545
return DataView.prototype.setUint8Clamped;
15401546
}],
@@ -1682,6 +1688,9 @@ GLOBAL.tests = {
16821688
'esnext.math.fscale': function () {
16831689
return Math.fscale;
16841690
},
1691+
'esnext.math.f16round': function () {
1692+
return Math.f16round;
1693+
},
16851694
'esnext.math.rad-per-deg': function () {
16861695
return Math.RAD_PER_DEG;
16871696
},

tests/entries/unit.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
787787
ok(load(NS, 'math/deg-per-rad') === Math.PI / 180);
788788
ok(load(NS, 'math/degrees')(Math.PI) === 180);
789789
ok(load(NS, 'math/fscale')(3, 1, 2, 1, 2) === 3);
790+
ok(load(NS, 'math/f16round')(1.337) === 1.3369140625);
790791
ok(load(NS, 'math/iaddh')(3, 2, 0xFFFFFFFF, 4) === 7);
791792
ok(load(NS, 'math/isubh')(3, 4, 0xFFFFFFFF, 2) === 1);
792793
ok(load(NS, 'math/imulh')(0xFFFFFFFF, 7) === -1);
@@ -919,6 +920,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
919920
load('proposals/efficient-64-bit-arithmetic');
920921
load('proposals/error-cause');
921922
load('proposals/explicit-resource-management');
923+
load('proposals/float16array');
922924
load('proposals/function-demethodize');
923925
load('proposals/function-is-callable-is-constructor');
924926
load('proposals/function-un-this');
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// some asserts based on https://github.com/petamoriken/float16/blob/master/test/f16round.js
2+
import { createConversionChecker } from '../helpers/helpers';
3+
4+
const { MAX_VALUE, MIN_VALUE } = Number;
5+
6+
QUnit.test('Math.f16round', assert => {
7+
const { f16round } = Math;
8+
assert.isFunction(f16round);
9+
assert.name(f16round, 'f16round');
10+
assert.arity(f16round, 1);
11+
assert.looksNative(f16round);
12+
assert.nonEnumerable(Math, 'f16round');
13+
assert.same(f16round(), NaN);
14+
assert.same(f16round(undefined), NaN);
15+
assert.same(f16round(NaN), NaN);
16+
assert.same(f16round(null), 0);
17+
assert.same(f16round(0), 0);
18+
assert.same(f16round(-0), -0);
19+
assert.same(f16round(MIN_VALUE), 0);
20+
assert.same(f16round(-MIN_VALUE), -0);
21+
assert.same(f16round(Infinity), Infinity);
22+
assert.same(f16round(-Infinity), -Infinity);
23+
assert.same(f16round(MAX_VALUE), Infinity);
24+
assert.same(f16round(-MAX_VALUE), -Infinity);
25+
26+
const maxFloat16 = 65504;
27+
const minFloat16 = 2 ** -24;
28+
29+
assert.same(f16round(maxFloat16), maxFloat16);
30+
assert.same(f16round(-maxFloat16), -maxFloat16);
31+
assert.same(f16round(minFloat16), minFloat16);
32+
assert.same(f16round(-minFloat16), -minFloat16);
33+
assert.same(f16round(minFloat16 / 2), 0);
34+
assert.same(f16round(-minFloat16 / 2), -0);
35+
assert.same(f16round(minFloat16 / 2 + 2 ** -25), minFloat16);
36+
assert.same(f16round(-minFloat16 / 2 - 2 ** -25), -minFloat16);
37+
38+
assert.same(f16round(1.337), 1.3369140625);
39+
40+
const checker = createConversionChecker(1.1);
41+
assert.same(f16round(checker), 1.099609375, 'object wrapper');
42+
assert.same(checker.$valueOf, 1, 'valueOf calls');
43+
assert.same(checker.$toString, 0, 'toString calls');
44+
});
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// some asserts based on https://github.com/petamoriken/float16/blob/master/test/f16round.js
2+
import { createConversionChecker } from '../helpers/helpers';
3+
4+
import f16round from 'core-js-pure/full/math/f16round';
5+
6+
const { MAX_VALUE, MIN_VALUE } = Number;
7+
8+
QUnit.test('Math.f16round', assert => {
9+
assert.isFunction(f16round);
10+
assert.name(f16round, 'f16round');
11+
assert.arity(f16round, 1);
12+
assert.same(f16round(), NaN);
13+
assert.same(f16round(undefined), NaN);
14+
assert.same(f16round(NaN), NaN);
15+
assert.same(f16round(null), 0);
16+
assert.same(f16round(0), 0);
17+
assert.same(f16round(-0), -0);
18+
assert.same(f16round(MIN_VALUE), 0);
19+
assert.same(f16round(-MIN_VALUE), -0);
20+
assert.same(f16round(Infinity), Infinity);
21+
assert.same(f16round(-Infinity), -Infinity);
22+
assert.same(f16round(MAX_VALUE), Infinity);
23+
assert.same(f16round(-MAX_VALUE), -Infinity);
24+
25+
const maxFloat16 = 65504;
26+
const minFloat16 = 2 ** -24;
27+
28+
assert.same(f16round(maxFloat16), maxFloat16);
29+
assert.same(f16round(-maxFloat16), -maxFloat16);
30+
assert.same(f16round(minFloat16), minFloat16);
31+
assert.same(f16round(-minFloat16), -minFloat16);
32+
assert.same(f16round(minFloat16 / 2), 0);
33+
assert.same(f16round(-minFloat16 / 2), -0);
34+
assert.same(f16round(minFloat16 / 2 + 2 ** -25), minFloat16);
35+
assert.same(f16round(-minFloat16 / 2 - 2 ** -25), -minFloat16);
36+
37+
assert.same(f16round(1.337), 1.3369140625);
38+
39+
const checker = createConversionChecker(1.1);
40+
assert.same(f16round(checker), 1.099609375, 'object wrapper');
41+
assert.same(checker.$valueOf, 1, 'valueOf calls');
42+
assert.same(checker.$toString, 0, 'toString calls');
43+
});

0 commit comments

Comments
 (0)