Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1264 from zloirock/float16
- Loading branch information
Showing
25 changed files
with
282 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/core-js-pure/override/modules/esnext.data-view.get-float16.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// empty |
1 change: 1 addition & 0 deletions
1
packages/core-js-pure/override/modules/esnext.data-view.set-float16.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict'; | ||
require('../../modules/esnext.data-view.get-float16'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
'use strict'; | ||
var parent = require('../../stable/data-view'); | ||
require('../../modules/esnext.data-view.get-float16'); | ||
require('../../modules/esnext.data-view.set-float16'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict'; | ||
require('../../modules/esnext.data-view.set-float16'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
require('../../modules/esnext.math.f16round'); | ||
var path = require('../../internals/path'); | ||
|
||
module.exports = path.Math.f16round; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
'use strict'; | ||
var parent = require('../../stable/math'); | ||
require('../../modules/esnext.math.f16round'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
var parent = require('../../actual/data-view/get-float16'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
var parent = require('../../actual/data-view/set-float16'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
var parent = require('../../actual/math/f16round'); | ||
|
||
module.exports = parent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
QUnit.test('DataView.prototype.{ getFloat16, setFloat16 }', assert => { | ||
const { getFloat16, setFloat16 } = DataView.prototype; | ||
|
||
assert.isFunction(getFloat16); | ||
assert.arity(getFloat16, 1); | ||
assert.name(getFloat16, 'getFloat16'); | ||
|
||
assert.isFunction(setFloat16); | ||
assert.arity(setFloat16, 2); | ||
assert.name(setFloat16, 'setFloat16'); | ||
|
||
assert.same(new DataView(new ArrayBuffer(8)).setFloat16(0, 0), undefined, 'void'); | ||
|
||
function toString(it) { | ||
return it === 0 && 1 / it === -Infinity ? '-0' : it; | ||
} | ||
|
||
const data = [ | ||
[0b0000000000000000, 0], | ||
[0b1000000000000000, -0], | ||
[0b0011110000000000, 1], | ||
[0b1011110000000000, -1], | ||
[0b0100001001001000, 3.140625], | ||
[0b0000001000000000, 0.000030517578125], | ||
[0b0111101111111111, 65504], | ||
[0b1111101111111111, -65504], | ||
[0b0000000000000001, 2 ** -24], | ||
[0b1000000000000001, -(2 ** -24)], | ||
// [0b0111110000000001, NaN], <- what NaN representation should be used? | ||
[0b0111110000000000, Infinity], | ||
[0b1111110000000000, -Infinity], | ||
]; | ||
|
||
const buffer = new ArrayBuffer(2); | ||
const view = new DataView(buffer); | ||
|
||
for (const [bin, f16] of data) for (const LE of [false, true]) { | ||
view.setUint16(0, bin, LE); | ||
assert.same(view.getFloat16(0, LE), f16, `DataView.prototype.setUint16 + DataView.prototype.getFloat16, LE: ${ LE }, ${ toString(bin) } -> ${ toString(f16) }`); | ||
view.setFloat16(0, f16, LE); | ||
assert.same(view.getUint16(0, LE), bin, `DataView.prototype.setFloat16 + DataView.prototype.getUint16, LE: ${ LE }, ${ toString(f16) } -> ${ toString(bin) }`); | ||
assert.same(view.getFloat16(0, LE), f16, `DataView.prototype.setFloat16 + DataView.prototype.getFloat16, LE: ${ LE }, ${ toString(f16) }`); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); |
Oops, something went wrong.