heyujie
2021-06-07 8f8155aa4c83f7c2577ac123add550766b6a7ce3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* eslint no-bitwise: "off" */
// Credit: https://github.com/paulmillr/es6-shim/
 
"use strict";
 
var abs = Math.abs
  , floor = Math.floor
  , log = Math.log
  , min = Math.min
  , pow = Math.pow
  , LN2 = Math.LN2
  , roundToEven;
 
roundToEven = function (num) {
    var whole = floor(num), fraction = num - whole;
    if (fraction < 0.5) return whole;
    if (fraction > 0.5) return whole + 1;
    return whole % 2 ? whole + 1 : whole;
};
 
// eslint-disable-next-line max-statements, max-lines-per-function
module.exports = function (value, ebits, fbits) {
    var bias = (1 << (ebits - 1)) - 1, sign, e, fraction, i, bits, str, bytes;
 
    // Compute sign, exponent, fraction
    if (isNaN(value)) {
        // NaN
        // http://dev.w3.org/2006/webapi/WebIDL/#es-type-mapping
        e = (1 << ebits) - 1;
        fraction = pow(2, fbits - 1);
        sign = 0;
    } else if (value === Infinity || value === -Infinity) {
        e = (1 << ebits) - 1;
        fraction = 0;
        sign = value < 0 ? 1 : 0;
    } else if (value === 0) {
        e = 0;
        fraction = 0;
        sign = 1 / value === -Infinity ? 1 : 0;
    } else {
        sign = value < 0;
        value = abs(value);
 
        if (value >= pow(2, 1 - bias)) {
            e = min(floor(log(value) / LN2), 1023);
            fraction = roundToEven((value / pow(2, e)) * pow(2, fbits));
            if (fraction / pow(2, fbits) >= 2) {
                e += 1;
                fraction = 1;
            }
            if (e > bias) {
                // Overflow
                e = (1 << ebits) - 1;
                fraction = 0;
            } else {
                // Normal
                e += bias;
                fraction -= pow(2, fbits);
            }
        } else {
            // Subnormal
            e = 0;
            fraction = roundToEven(value / pow(2, 1 - bias - fbits));
        }
    }
 
    // Pack sign, exponent, fraction
    bits = [];
    for (i = fbits; i; i -= 1) {
        bits.push(fraction % 2 ? 1 : 0);
        fraction = floor(fraction / 2);
    }
    for (i = ebits; i; i -= 1) {
        bits.push(e % 2 ? 1 : 0);
        e = floor(e / 2);
    }
    bits.push(sign ? 1 : 0);
    bits.reverse();
    str = bits.join("");
 
    // Bits to bytes
    bytes = [];
    while (str.length) {
        bytes.push(parseInt(str.substring(0, 8), 2));
        str = str.substring(8);
    }
    return bytes;
};