source: imaps-frontend/node_modules/es-abstract/helpers/valueToFloat32Bytes.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $abs = GetIntrinsic('%Math.abs%');
6var $floor = GetIntrinsic('%Math.floor%');
7var $pow = GetIntrinsic('%Math.pow%');
8
9var isFinite = require('./isFinite');
10var isNaN = require('./isNaN');
11var isNegativeZero = require('./isNegativeZero');
12
13var maxFiniteFloat32 = 3.4028234663852886e+38; // roughly 2 ** 128 - 1
14
15module.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};
Note: See TracBrowser for help on using the repository browser.