| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var GetIntrinsic = require('get-intrinsic');
|
|---|
| 4 |
|
|---|
| 5 | var $parseInt = GetIntrinsic('%parseInt%');
|
|---|
| 6 | var $abs = require('math-intrinsics/abs');
|
|---|
| 7 | var $floor = require('math-intrinsics/floor');
|
|---|
| 8 | var isFinite = require('math-intrinsics/isFinite');
|
|---|
| 9 | var isNegativeZero = require('math-intrinsics/isNegativeZero');
|
|---|
| 10 |
|
|---|
| 11 | var callBound = require('call-bound');
|
|---|
| 12 |
|
|---|
| 13 | var $strIndexOf = callBound('String.prototype.indexOf');
|
|---|
| 14 | var $strSlice = callBound('String.prototype.slice');
|
|---|
| 15 |
|
|---|
| 16 | var fractionToBitString = require('../helpers/fractionToBinaryString');
|
|---|
| 17 | var intToBinString = require('../helpers/intToBinaryString');
|
|---|
| 18 | var isNaN = require('../helpers/isNaN');
|
|---|
| 19 |
|
|---|
| 20 | var float64bias = 1023;
|
|---|
| 21 |
|
|---|
| 22 | var elevenOnes = '11111111111';
|
|---|
| 23 | var elevenZeroes = '00000000000';
|
|---|
| 24 | var fiftyOneZeroes = elevenZeroes + elevenZeroes + elevenZeroes + elevenZeroes + '0000000';
|
|---|
| 25 |
|
|---|
| 26 | // IEEE 754-1985
|
|---|
| 27 | module.exports = function valueToFloat64Bytes(value, isLittleEndian) {
|
|---|
| 28 | var signBit = value < 0 || isNegativeZero(value) ? '1' : '0';
|
|---|
| 29 | var exponentBits;
|
|---|
| 30 | var significandBits;
|
|---|
| 31 |
|
|---|
| 32 | if (isNaN(value)) {
|
|---|
| 33 | exponentBits = elevenOnes;
|
|---|
| 34 | significandBits = '1' + fiftyOneZeroes;
|
|---|
| 35 | } else if (!isFinite(value)) {
|
|---|
| 36 | exponentBits = elevenOnes;
|
|---|
| 37 | significandBits = '0' + fiftyOneZeroes;
|
|---|
| 38 | } else if (value === 0) {
|
|---|
| 39 | exponentBits = elevenZeroes;
|
|---|
| 40 | significandBits = '0' + fiftyOneZeroes;
|
|---|
| 41 | } else {
|
|---|
| 42 | value = $abs(value); // eslint-disable-line no-param-reassign
|
|---|
| 43 |
|
|---|
| 44 | // Isolate the integer part (digits before the decimal):
|
|---|
| 45 | var integerPart = $floor(value);
|
|---|
| 46 |
|
|---|
| 47 | var intBinString = intToBinString(integerPart); // bit string for integer part
|
|---|
| 48 | var fracBinString = fractionToBitString(value - integerPart); // bit string for fractional part
|
|---|
| 49 |
|
|---|
| 50 | var numberOfBits;
|
|---|
| 51 | // find exponent needed to normalize integer+fractional parts
|
|---|
| 52 | if (intBinString) {
|
|---|
| 53 | exponentBits = intBinString.length - 1; // move the decimal to the left
|
|---|
| 54 | } else {
|
|---|
| 55 | var first1 = $strIndexOf(fracBinString, '1');
|
|---|
| 56 | if (first1 > -1) {
|
|---|
| 57 | numberOfBits = first1 + 1;
|
|---|
| 58 | }
|
|---|
| 59 | exponentBits = -numberOfBits; // move the decimal to the right
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | significandBits = intBinString + fracBinString;
|
|---|
| 63 | if (exponentBits < 0) {
|
|---|
| 64 | // subnormals
|
|---|
| 65 | if (exponentBits <= -float64bias) {
|
|---|
| 66 | numberOfBits = float64bias - 1; // limit number of removed bits
|
|---|
| 67 | }
|
|---|
| 68 | significandBits = $strSlice(significandBits, numberOfBits); // remove all leading 0s and the first 1 for normal values; for subnormals, remove up to `float64bias - 1` leading bits
|
|---|
| 69 | } else {
|
|---|
| 70 | significandBits = $strSlice(significandBits, 1); // remove the leading '1' (implicit/hidden bit)
|
|---|
| 71 | }
|
|---|
| 72 | exponentBits = $strSlice(elevenZeroes + intToBinString(exponentBits + float64bias), -11); // Convert the exponent to a bit string
|
|---|
| 73 |
|
|---|
| 74 | significandBits = $strSlice(significandBits + fiftyOneZeroes + '0', 0, 52); // fill in any trailing zeros and ensure we have only 52 fraction bits
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | var bits = signBit + exponentBits + significandBits;
|
|---|
| 78 | var rawBytes = [];
|
|---|
| 79 | for (var i = 0; i < 8; i++) {
|
|---|
| 80 | var targetIndex = isLittleEndian ? 8 - i - 1 : i;
|
|---|
| 81 | rawBytes[targetIndex] = $parseInt($strSlice(bits, i * 8, (i + 1) * 8), 2);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | return rawBytes;
|
|---|
| 85 | };
|
|---|