source: frontend/node_modules/es-abstract/helpers/valueToFloat64Bytes.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.0 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $parseInt = GetIntrinsic('%parseInt%');
6var $abs = require('math-intrinsics/abs');
7var $floor = require('math-intrinsics/floor');
8var isFinite = require('math-intrinsics/isFinite');
9var isNegativeZero = require('math-intrinsics/isNegativeZero');
10
11var callBound = require('call-bound');
12
13var $strIndexOf = callBound('String.prototype.indexOf');
14var $strSlice = callBound('String.prototype.slice');
15
16var fractionToBitString = require('../helpers/fractionToBinaryString');
17var intToBinString = require('../helpers/intToBinaryString');
18var isNaN = require('../helpers/isNaN');
19
20var float64bias = 1023;
21
22var elevenOnes = '11111111111';
23var elevenZeroes = '00000000000';
24var fiftyOneZeroes = elevenZeroes + elevenZeroes + elevenZeroes + elevenZeroes + '0000000';
25
26// IEEE 754-1985
27module.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};
Note: See TracBrowser for help on using the repository browser.