[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
---|
| 4 |
|
---|
| 5 | var $abs = GetIntrinsic('%Math.abs%');
|
---|
| 6 | var $floor = GetIntrinsic('%Math.floor%');
|
---|
| 7 | var $pow = GetIntrinsic('%Math.pow%');
|
---|
| 8 |
|
---|
| 9 | var isFinite = require('./isFinite');
|
---|
| 10 | var isNaN = require('./isNaN');
|
---|
| 11 | var isNegativeZero = require('./isNegativeZero');
|
---|
| 12 |
|
---|
| 13 | var maxFiniteFloat32 = 3.4028234663852886e+38; // roughly 2 ** 128 - 1
|
---|
| 14 |
|
---|
| 15 | module.exports = function valueToFloat32Bytes(value, isLittleEndian) {
|
---|
| 16 | if (isNaN(value)) {
|
---|
| 17 | return isLittleEndian ? [0, 0, 192, 127] : [127, 192, 0, 0]; // hardcoded
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | var leastSig;
|
---|
| 21 |
|
---|
| 22 | if (value === 0) {
|
---|
| 23 | leastSig = isNegativeZero(value) ? 0x80 : 0;
|
---|
| 24 | return isLittleEndian ? [0, 0, 0, leastSig] : [leastSig, 0, 0, 0];
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | if ($abs(value) > maxFiniteFloat32 || !isFinite(value)) {
|
---|
| 28 | leastSig = value < 0 ? 255 : 127;
|
---|
| 29 | return isLittleEndian ? [0, 0, 128, leastSig] : [leastSig, 128, 0, 0];
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | var sign = value < 0 ? 1 : 0;
|
---|
| 33 | value = $abs(value); // eslint-disable-line no-param-reassign
|
---|
| 34 |
|
---|
| 35 | var exponent = 0;
|
---|
| 36 | while (value >= 2) {
|
---|
| 37 | exponent += 1;
|
---|
| 38 | value /= 2; // eslint-disable-line no-param-reassign
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | while (value < 1) {
|
---|
| 42 | exponent -= 1;
|
---|
| 43 | value *= 2; // eslint-disable-line no-param-reassign
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | var mantissa = value - 1;
|
---|
| 47 | mantissa *= $pow(2, 23) + 0.5;
|
---|
| 48 | mantissa = $floor(mantissa);
|
---|
| 49 |
|
---|
| 50 | exponent += 127;
|
---|
| 51 | exponent <<= 23;
|
---|
| 52 |
|
---|
| 53 | var result = (sign << 31)
|
---|
| 54 | | exponent
|
---|
| 55 | | mantissa;
|
---|
| 56 |
|
---|
| 57 | var byte0 = result & 255;
|
---|
| 58 | result >>= 8;
|
---|
| 59 | var byte1 = result & 255;
|
---|
| 60 | result >>= 8;
|
---|
| 61 | var byte2 = result & 255;
|
---|
| 62 | result >>= 8;
|
---|
| 63 | var byte3 = result & 255;
|
---|
| 64 |
|
---|
| 65 | if (isLittleEndian) {
|
---|
| 66 | return [byte0, byte1, byte2, byte3];
|
---|
| 67 | }
|
---|
| 68 | return [byte3, byte2, byte1, byte0];
|
---|
| 69 | };
|
---|