[79a0317] | 1 | 'use strict';
|
---|
| 2 | var $ = require('../internals/export');
|
---|
| 3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
| 4 | var aDataView = require('../internals/a-data-view');
|
---|
| 5 | var toIndex = require('../internals/to-index');
|
---|
| 6 | // TODO: Replace with module dependency in `core-js@4`
|
---|
| 7 | var log2 = require('../internals/math-log2');
|
---|
| 8 | var roundTiesToEven = require('../internals/math-round-ties-to-even');
|
---|
| 9 |
|
---|
| 10 | var pow = Math.pow;
|
---|
| 11 |
|
---|
| 12 | var MIN_INFINITY16 = 65520; // (2 - 2 ** -11) * 2 ** 15
|
---|
| 13 | var MIN_NORMAL16 = 0.000061005353927612305; // (1 - 2 ** -11) * 2 ** -14
|
---|
| 14 | var REC_MIN_SUBNORMAL16 = 16777216; // 2 ** 10 * 2 ** 14
|
---|
| 15 | var REC_SIGNIFICAND_DENOM16 = 1024; // 2 ** 10;
|
---|
| 16 |
|
---|
| 17 | var packFloat16 = function (value) {
|
---|
| 18 | // eslint-disable-next-line no-self-compare -- NaN check
|
---|
| 19 | if (value !== value) return 0x7E00; // NaN
|
---|
| 20 | if (value === 0) return (1 / value === -Infinity) << 15; // +0 or -0
|
---|
| 21 |
|
---|
| 22 | var neg = value < 0;
|
---|
| 23 | if (neg) value = -value;
|
---|
| 24 | if (value >= MIN_INFINITY16) return neg << 15 | 0x7C00; // Infinity
|
---|
| 25 | if (value < MIN_NORMAL16) return neg << 15 | roundTiesToEven(value * REC_MIN_SUBNORMAL16); // subnormal
|
---|
| 26 |
|
---|
| 27 | // normal
|
---|
| 28 | var exponent = log2(value) | 0;
|
---|
| 29 | if (exponent === -15) {
|
---|
| 30 | // we round from a value between 2 ** -15 * (1 + 1022/1024) (the largest subnormal) and 2 ** -14 * (1 + 0/1024) (the smallest normal)
|
---|
| 31 | // to the latter (former impossible because of the subnormal check above)
|
---|
| 32 | return neg << 15 | REC_SIGNIFICAND_DENOM16;
|
---|
| 33 | }
|
---|
| 34 | var significand = roundTiesToEven((value * pow(2, -exponent) - 1) * REC_SIGNIFICAND_DENOM16);
|
---|
| 35 | if (significand === REC_SIGNIFICAND_DENOM16) {
|
---|
| 36 | // we round from a value between 2 ** n * (1 + 1023/1024) and 2 ** (n + 1) * (1 + 0/1024) to the latter
|
---|
| 37 | return neg << 15 | exponent + 16 << 10;
|
---|
| 38 | }
|
---|
| 39 | return neg << 15 | exponent + 15 << 10 | significand;
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | // eslint-disable-next-line es/no-typed-arrays -- safe
|
---|
| 43 | var setUint16 = uncurryThis(DataView.prototype.setUint16);
|
---|
| 44 |
|
---|
| 45 | // `DataView.prototype.setFloat16` method
|
---|
| 46 | // https://github.com/tc39/proposal-float16array
|
---|
| 47 | $({ target: 'DataView', proto: true }, {
|
---|
| 48 | setFloat16: function setFloat16(byteOffset, value /* , littleEndian */) {
|
---|
| 49 | aDataView(this);
|
---|
| 50 | var offset = toIndex(byteOffset);
|
---|
| 51 | var bytes = packFloat16(+value);
|
---|
| 52 | return setUint16(this, offset, bytes, arguments.length > 2 ? arguments[2] : false);
|
---|
| 53 | }
|
---|
| 54 | });
|
---|