Skip to content

Commit

Permalink
add some float16 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jul 22, 2023
1 parent 5fae871 commit 5bc70f6
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/core-js-compat/src/data.mjs
Expand Up @@ -1973,8 +1973,12 @@ export const data = {
},
'esnext.composite-symbol': {
},
'esnext.data-view.get-float16': {
},
'esnext.data-view.get-uint8-clamped': {
},
'esnext.data-view.set-float16': {
},
'esnext.data-view.set-uint8-clamped': {
},
'esnext.disposable-stack.constructor': {
Expand Down Expand Up @@ -2088,6 +2092,8 @@ export const data = {
},
'esnext.math.fscale': {
},
'esnext.math.f16round': {
},
// TODO: Remove from `core-js@4`
'esnext.math.iaddh': {
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Expand Up @@ -214,7 +214,10 @@ export default {
'web.url-search-params.has',
],
3.32: [
'esnext.data-view.get-float16',
'esnext.data-view.get-uint8-clamped',
'esnext.data-view.set-float16',
'esnext.data-view.set-uint8-clamped',
'esnext.math.f16round',
],
};
@@ -0,0 +1 @@
// empty
@@ -0,0 +1 @@
// empty
2 changes: 2 additions & 0 deletions packages/core-js/full/data-view/index.js
@@ -1,6 +1,8 @@
'use strict';
var parent = require('../../actual/data-view');
require('../../modules/esnext.data-view.get-float16');
require('../../modules/esnext.data-view.get-uint8-clamped');
require('../../modules/esnext.data-view.set-float16');
require('../../modules/esnext.data-view.set-uint8-clamped');

module.exports = parent;
5 changes: 5 additions & 0 deletions packages/core-js/full/math/f16round.js
@@ -0,0 +1,5 @@
'use strict';
require('../../modules/esnext.math.f16round');
var path = require('../../internals/path');

module.exports = path.Math.f16round;
1 change: 1 addition & 0 deletions packages/core-js/full/math/index.js
Expand Up @@ -4,6 +4,7 @@ require('../../modules/esnext.math.clamp');
require('../../modules/esnext.math.deg-per-rad');
require('../../modules/esnext.math.degrees');
require('../../modules/esnext.math.fscale');
require('../../modules/esnext.math.f16round');
require('../../modules/esnext.math.rad-per-deg');
require('../../modules/esnext.math.radians');
require('../../modules/esnext.math.scale');
Expand Down
16 changes: 16 additions & 0 deletions packages/core-js/modules/esnext.data-view.get-float16.js
@@ -0,0 +1,16 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var unpackIEEE754 = require('../internals/ieee754').unpack;

// 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, forced: 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);
}
});
21 changes: 21 additions & 0 deletions packages/core-js/modules/esnext.data-view.set-float16.js
@@ -0,0 +1,21 @@
'use strict';
var $ = require('../internals/export');
var uncurryThis = require('../internals/function-uncurry-this');
var classof = require('../internals/classof');
var toIndex = require('../internals/to-index');
var packIEEE754 = require('../internals/ieee754').pack;

var $TypeError = TypeError;
// 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, forced: true }, {
setFloat16: function setFloat16(byteOffset, value /* , littleEndian */) {
if (classof(this) !== 'DataView') throw $TypeError('Incorrect receiver');
var offset = toIndex(byteOffset);
var bytes = packIEEE754(+value, 10, 2);
return setUint16(this, offset, bytes[1] << 8 | bytes[0], arguments.length > 2 ? arguments[2] : false);
}
});
16 changes: 16 additions & 0 deletions packages/core-js/modules/esnext.math.f16round.js
@@ -0,0 +1,16 @@
'use strict';
var $ = require('../internals/export');
var IEEE754 = require('../internals/ieee754');

var packIEEE754 = IEEE754.pack;
var unpackIEEE754 = IEEE754.unpack;
var $isFinite = isFinite;

// `Math.f16round` method
// https://github.com/tc39/proposal-float16array
$({ target: 'Math', stat: true }, {
f16round: function f16round(x) {
var n = +x;
return $isFinite(n) && n !== 0 ? unpackIEEE754(packIEEE754(n, 10, 2), 10) : n;
}
});
5 changes: 5 additions & 0 deletions packages/core-js/proposals/float16array.js
@@ -0,0 +1,5 @@
'use strict';
// https://github.com/tc39/proposal-float16array
require('../modules/esnext.data-view.get-float16');
require('../modules/esnext.data-view.set-float16');
require('../modules/esnext.math.f16round');
1 change: 1 addition & 0 deletions packages/core-js/stage/3.js
Expand Up @@ -6,6 +6,7 @@ require('../proposals/array-from-async-stage-2');
require('../proposals/array-grouping-v2');
require('../proposals/decorator-metadata-v2');
require('../proposals/explicit-resource-management');
require('../proposals/float16array');
require('../proposals/iterator-helpers-stage-3-2');
require('../proposals/json-parse-with-source');
require('../proposals/promise-with-resolvers');
Expand Down
9 changes: 9 additions & 0 deletions tests/compat/tests.js
Expand Up @@ -1532,9 +1532,15 @@ GLOBAL.tests = {
'esnext.composite-symbol': function () {
return compositeSymbol;
},
'esnext.data-view.get-float16': [ARRAY_BUFFER_SUPPORT, function () {
return DataView.prototype.getFloat16;
}],
'esnext.data-view.get-uint8-clamped': [ARRAY_BUFFER_SUPPORT, function () {
return DataView.prototype.getUint8Clamped;
}],
'esnext.data-view.set-float16': [ARRAY_BUFFER_SUPPORT, function () {
return DataView.prototype.setFloat16;
}],
'esnext.data-view.set-uint8-clamped': [ARRAY_BUFFER_SUPPORT, function () {
return DataView.prototype.setUint8Clamped;
}],
Expand Down Expand Up @@ -1682,6 +1688,9 @@ GLOBAL.tests = {
'esnext.math.fscale': function () {
return Math.fscale;
},
'esnext.math.f16round': function () {
return Math.f16round;
},
'esnext.math.rad-per-deg': function () {
return Math.RAD_PER_DEG;
},
Expand Down
2 changes: 2 additions & 0 deletions tests/entries/unit.mjs
Expand Up @@ -787,6 +787,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(load(NS, 'math/deg-per-rad') === Math.PI / 180);
ok(load(NS, 'math/degrees')(Math.PI) === 180);
ok(load(NS, 'math/fscale')(3, 1, 2, 1, 2) === 3);
ok(load(NS, 'math/f16round')(1.337) === 1.3369140625);
ok(load(NS, 'math/iaddh')(3, 2, 0xFFFFFFFF, 4) === 7);
ok(load(NS, 'math/isubh')(3, 4, 0xFFFFFFFF, 2) === 1);
ok(load(NS, 'math/imulh')(0xFFFFFFFF, 7) === -1);
Expand Down Expand Up @@ -919,6 +920,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
load('proposals/efficient-64-bit-arithmetic');
load('proposals/error-cause');
load('proposals/explicit-resource-management');
load('proposals/float16array');
load('proposals/function-demethodize');
load('proposals/function-is-callable-is-constructor');
load('proposals/function-un-this');
Expand Down
44 changes: 44 additions & 0 deletions tests/unit-global/esnext.math.f16round.js
@@ -0,0 +1,44 @@
// some asserts based on https://github.com/petamoriken/float16/blob/master/test/f16round.js
import { createConversionChecker } from '../helpers/helpers';

const { MAX_VALUE, MIN_VALUE } = Number;

QUnit.test('Math.f16round', assert => {
const { f16round } = Math;
assert.isFunction(f16round);
assert.name(f16round, 'f16round');
assert.arity(f16round, 1);
assert.looksNative(f16round);
assert.nonEnumerable(Math, 'f16round');
assert.same(f16round(), NaN);
assert.same(f16round(undefined), NaN);
assert.same(f16round(NaN), NaN);
assert.same(f16round(null), 0);
assert.same(f16round(0), 0);
assert.same(f16round(-0), -0);
assert.same(f16round(MIN_VALUE), 0);
assert.same(f16round(-MIN_VALUE), -0);
assert.same(f16round(Infinity), Infinity);
assert.same(f16round(-Infinity), -Infinity);
assert.same(f16round(MAX_VALUE), Infinity);
assert.same(f16round(-MAX_VALUE), -Infinity);

const maxFloat16 = 65504;
const minFloat16 = 2 ** -24;

assert.same(f16round(maxFloat16), maxFloat16);
assert.same(f16round(-maxFloat16), -maxFloat16);
assert.same(f16round(minFloat16), minFloat16);
assert.same(f16round(-minFloat16), -minFloat16);
assert.same(f16round(minFloat16 / 2), 0);
assert.same(f16round(-minFloat16 / 2), -0);
assert.same(f16round(minFloat16 / 2 + 2 ** -25), minFloat16);
assert.same(f16round(-minFloat16 / 2 - 2 ** -25), -minFloat16);

assert.same(f16round(1.337), 1.3369140625);

const checker = createConversionChecker(1.1);
assert.same(f16round(checker), 1.099609375, 'object wrapper');
assert.same(checker.$valueOf, 1, 'valueOf calls');
assert.same(checker.$toString, 0, 'toString calls');
});
43 changes: 43 additions & 0 deletions tests/unit-pure/esnext.math.f16round.js
@@ -0,0 +1,43 @@
// some asserts based on https://github.com/petamoriken/float16/blob/master/test/f16round.js
import { createConversionChecker } from '../helpers/helpers';

import f16round from 'core-js-pure/full/math/f16round';

const { MAX_VALUE, MIN_VALUE } = Number;

QUnit.test('Math.f16round', assert => {
assert.isFunction(f16round);
assert.name(f16round, 'f16round');
assert.arity(f16round, 1);
assert.same(f16round(), NaN);
assert.same(f16round(undefined), NaN);
assert.same(f16round(NaN), NaN);
assert.same(f16round(null), 0);
assert.same(f16round(0), 0);
assert.same(f16round(-0), -0);
assert.same(f16round(MIN_VALUE), 0);
assert.same(f16round(-MIN_VALUE), -0);
assert.same(f16round(Infinity), Infinity);
assert.same(f16round(-Infinity), -Infinity);
assert.same(f16round(MAX_VALUE), Infinity);
assert.same(f16round(-MAX_VALUE), -Infinity);

const maxFloat16 = 65504;
const minFloat16 = 2 ** -24;

assert.same(f16round(maxFloat16), maxFloat16);
assert.same(f16round(-maxFloat16), -maxFloat16);
assert.same(f16round(minFloat16), minFloat16);
assert.same(f16round(-minFloat16), -minFloat16);
assert.same(f16round(minFloat16 / 2), 0);
assert.same(f16round(-minFloat16 / 2), -0);
assert.same(f16round(minFloat16 / 2 + 2 ** -25), minFloat16);
assert.same(f16round(-minFloat16 / 2 - 2 ** -25), -minFloat16);

assert.same(f16round(1.337), 1.3369140625);

const checker = createConversionChecker(1.1);
assert.same(f16round(checker), 1.099609375, 'object wrapper');
assert.same(checker.$valueOf, 1, 'valueOf calls');
assert.same(checker.$toString, 0, 'toString calls');
});

0 comments on commit 5bc70f6

Please sign in to comment.