zhangnaisong
2023-08-05 24d66c8d82b628a06e93dbb1abfea2049b3d45ab
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict';
 
var GetIntrinsic = require('get-intrinsic');
 
var $floor = GetIntrinsic('%Math.floor%');
var $log = GetIntrinsic('%Math.log%');
var $log2E = GetIntrinsic('%Math.LOG2E%');
var $log2 = GetIntrinsic('%Math.log2%', true) || function log2(x) {
    return $log(x) * $log2E;
};
var $parseInt = GetIntrinsic('%parseInt%');
var $pow = GetIntrinsic('%Math.pow%');
var $TypeError = GetIntrinsic('%TypeError%');
 
var callBound = require('call-bind/callBound');
 
var $reverse = callBound('Array.prototype.reverse');
var $numberToString = callBound('Number.prototype.toString');
var $strSlice = callBound('String.prototype.slice');
 
var abs = require('./abs');
var hasOwnProperty = require('./HasOwnProperty');
var ToInt16 = require('./ToInt16');
var ToInt32 = require('./ToInt32');
var ToInt8 = require('./ToInt8');
var ToUint16 = require('./ToUint16');
var ToUint32 = require('./ToUint32');
var ToUint8 = require('./ToUint8');
var ToUint8Clamp = require('./ToUint8Clamp');
var Type = require('./Type');
 
var isNaN = require('../helpers/isNaN');
var isFinite = require('../helpers/isFinite');
 
var keys = require('object-keys');
 
// https://262.ecma-international.org/8.0/#table-50
var TypeToSizes = {
    __proto__: null,
    Int8: 1,
    Uint8: 1,
    Uint8C: 1,
    Int16: 2,
    Uint16: 2,
    Int32: 4,
    Uint32: 4,
    Float32: 4,
    Float64: 8
};
 
var TypeToAO = {
    __proto__: null,
    Int8: ToInt8,
    Uint8: ToUint8,
    Uint8C: ToUint8Clamp,
    Int16: ToInt16,
    Uint16: ToUint16,
    Int32: ToInt32,
    Uint32: ToUint32
};
 
// https://262.ecma-international.org/8.0/#sec-numbertorawbytes
 
module.exports = function NumberToRawBytes(type, value, isLittleEndian) {
    if (typeof type !== 'string' || !hasOwnProperty(TypeToSizes, type)) {
        throw new $TypeError('Assertion failed: `type` must be a TypedArray element type: ' + keys(TypeToSizes));
    }
    if (Type(value) !== 'Number') {
        throw new $TypeError('Assertion failed: `value` must be a Number');
    }
    if (Type(isLittleEndian) !== 'Boolean') {
        throw new $TypeError('Assertion failed: `isLittleEndian` must be a Boolean');
    }
 
    var rawBytes = [];
    var exponent;
 
    if (type === 'Float32') { // step 1
        if (isNaN(value)) {
            return isLittleEndian ? [0, 0, 192, 127] : [127, 192, 0, 0]; // hardcoded
        }
 
        var leastSig;
 
        if (value === 0) {
            leastSig = Object.is(value, -0) ? 0x80 : 0;
            return isLittleEndian ? [0, 0, 0, leastSig] : [leastSig, 0, 0, 0];
        }
 
        if (!isFinite(value)) {
            leastSig = value < 0 ? 255 : 127;
            return isLittleEndian ? [0, 0, 128, leastSig] : [leastSig, 128, 0, 0];
        }
 
        var sign = value < 0 ? 1 : 0;
        value = abs(value); // eslint-disable-line no-param-reassign
 
        exponent = 0;
        while (value >= 2) {
            exponent += 1;
            value /= 2; // eslint-disable-line no-param-reassign
        }
 
        while (value < 1) {
            exponent -= 1;
            value *= 2; // eslint-disable-line no-param-reassign
        }
 
        var mantissa = value - 1;
        mantissa *= $pow(2, 23);
        mantissa = $floor(mantissa);
 
        exponent += 127;
        exponent = exponent << 23;
 
        var result = sign << 31;
        result |= exponent;
        result |= mantissa;
 
        var byte0 = result & 255;
        result = result >> 8;
        var byte1 = result & 255;
        result = result >> 8;
        var byte2 = result & 255;
        result = result >> 8;
        var byte3 = result & 255;
 
        if (isLittleEndian) {
            return [byte0, byte1, byte2, byte3];
        }
        return [byte3, byte2, byte1, byte0];
    } else if (type === 'Float64') { // step 2
        if (value === 0) {
            leastSig = Object.is(value, -0) ? 0x80 : 0;
            return isLittleEndian ? [0, 0, 0, 0, 0, 0, 0, leastSig] : [leastSig, 0, 0, 0, 0, 0, 0, 0];
        }
        if (isNaN(value)) {
            return isLittleEndian ? [0, 0, 0, 0, 0, 0, 248, 127] : [127, 248, 0, 0, 0, 0, 0, 0];
        }
        if (!isFinite(value)) {
            var infBytes = value < 0 ? [0, 0, 0, 0, 0, 0, 240, 255] : [0, 0, 0, 0, 0, 0, 240, 127];
            return isLittleEndian ? infBytes : $reverse(infBytes);
        }
 
        var isNegative = value < 0;
        if (isNegative) { value = -value; } // eslint-disable-line no-param-reassign
 
        exponent = $floor($log2(value));
        var significand = (value / $pow(2, exponent)) - 1;
 
        var bitString = '';
        for (var i = 0; i < 52; i++) {
            significand *= 2;
            if (significand >= 1) {
                bitString += '1';
                significand -= 1;
            } else {
                bitString += '0';
            }
        }
 
        exponent += 1023;
        var exponentBits = $numberToString(exponent, 2);
        while (exponentBits.length < 11) { exponentBits = '0' + exponentBits; }
 
        var fullBitString = (isNegative ? '1' : '0') + exponentBits + bitString;
        while (fullBitString.length < 64) { fullBitString = '0' + fullBitString; }
 
        for (i = 0; i < 8; i++) {
            rawBytes[i] = $parseInt($strSlice(fullBitString, i * 8, (i + 1) * 8), 2);
        }
 
        return isLittleEndian ? $reverse(rawBytes) : rawBytes;
    } // step 3
 
    var n = TypeToSizes[type]; // step 3.a
 
    var convOp = TypeToAO[type]; // step 3.b
 
    var intValue = convOp(value); // step 3.c
 
    /*
    if (intValue >= 0) { // step 3.d
        // Let rawBytes be a List containing the n-byte binary encoding of intValue. If isLittleEndian is false, the bytes are ordered in big endian order. Otherwise, the bytes are ordered in little endian order.
    } else { // step 3.e
        // Let rawBytes be a List containing the n-byte binary 2's complement encoding of intValue. If isLittleEndian is false, the bytes are ordered in big endian order. Otherwise, the bytes are ordered in little endian order.
    }
    */
    if (intValue < 0) {
        intValue = intValue >>> 0;
    }
    for (i = 0; i < n; i++) {
        rawBytes[isLittleEndian ? i : n - 1 - i] = intValue & 0xff;
        intValue = intValue >> 8;
    }
 
    return rawBytes; // step 4
};