RawBytesToNumeric.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var $pow = GetIntrinsic('%Math.pow%');
  5. var $RangeError = GetIntrinsic('%RangeError%');
  6. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  7. var $TypeError = GetIntrinsic('%TypeError%');
  8. var $BigInt = GetIntrinsic('%BigInt%', true);
  9. var hasOwnProperty = require('./HasOwnProperty');
  10. var IsArray = require('./IsArray');
  11. var IsBigIntElementType = require('./IsBigIntElementType');
  12. var IsUnsignedElementType = require('./IsUnsignedElementType');
  13. var Type = require('./Type');
  14. var every = require('../helpers/every');
  15. var isByteValue = require('../helpers/isByteValue');
  16. var $reverse = callBound('Array.prototype.reverse');
  17. var $slice = callBound('Array.prototype.slice');
  18. var keys = require('object-keys');
  19. // https://262.ecma-international.org/11.0/#table-the-typedarray-constructors
  20. var TypeToSizes = {
  21. __proto__: null,
  22. Int8: 1,
  23. Uint8: 1,
  24. Uint8C: 1,
  25. Int16: 2,
  26. Uint16: 2,
  27. Int32: 4,
  28. Uint32: 4,
  29. BigInt64: 8,
  30. BigUint64: 8,
  31. Float32: 4,
  32. Float64: 8
  33. };
  34. // https://262.ecma-international.org/11.0/#sec-rawbytestonumeric
  35. module.exports = function RawBytesToNumeric(type, rawBytes, isLittleEndian) {
  36. if (!hasOwnProperty(TypeToSizes, type)) {
  37. throw new $TypeError('Assertion failed: `type` must be a TypedArray element type: ' + keys(TypeToSizes));
  38. }
  39. if (!IsArray(rawBytes) || !every(rawBytes, isByteValue)) {
  40. throw new $TypeError('Assertion failed: `rawBytes` must be an Array of bytes');
  41. }
  42. if (Type(isLittleEndian) !== 'Boolean') {
  43. throw new $TypeError('Assertion failed: `isLittleEndian` must be a Boolean');
  44. }
  45. var elementSize = TypeToSizes[type]; // step 1
  46. if (rawBytes.length !== elementSize) {
  47. // this assertion is not in the spec, but it'd be an editorial error if it were ever violated
  48. throw new $RangeError('Assertion failed: `rawBytes` must have a length of ' + elementSize + ' for type ' + type);
  49. }
  50. var isBigInt = IsBigIntElementType(type);
  51. if (isBigInt && !$BigInt) {
  52. throw new $SyntaxError('this environment does not support BigInts');
  53. }
  54. // eslint-disable-next-line no-param-reassign
  55. rawBytes = $slice(rawBytes, 0, elementSize);
  56. if (!isLittleEndian) {
  57. // eslint-disable-next-line no-param-reassign
  58. rawBytes = $reverse(rawBytes); // step 2
  59. }
  60. /* eslint no-redeclare: 1 */
  61. if (type === 'Float32') { // step 3
  62. /*
  63. Let value be the byte elements of rawBytes concatenated and interpreted as a little-endian bit string encoding of an IEEE 754-2008 binary32 value.
  64. If value is an IEEE 754-2008 binary32 NaN value, return the NaN Number value.
  65. Return the Number value that corresponds to value.
  66. */
  67. var sign = (rawBytes[3] & 0x80) >> 7; // first bit
  68. var exponent = ((rawBytes[3] & 0x7F) << 1) // 7 bits from index 3
  69. | ((rawBytes[2] & 0x80) >> 7); // 1 bit from index 2
  70. var mantissa = ((rawBytes[2] & 0x7F) << 16) // 7 bits from index 2
  71. | (rawBytes[1] << 8) // 8 bits from index 1
  72. | rawBytes[0]; // 8 bits from index 0
  73. if (exponent === 0 && mantissa === 0) {
  74. return sign === 0 ? 0 : -0;
  75. }
  76. if (exponent === 0xFF && mantissa === 0) {
  77. return sign === 0 ? Infinity : -Infinity;
  78. }
  79. if (exponent === 0xFF && mantissa !== 0) {
  80. return NaN;
  81. }
  82. exponent -= 127; // subtract the bias
  83. // return $pow(-1, sign) * mantissa / $pow(2, 23) * $pow(2, exponent);
  84. // return $pow(-1, sign) * (mantissa + 0x1000000) * $pow(2, exponent - 23);
  85. return $pow(-1, sign) * (1 + (mantissa / $pow(2, 23))) * $pow(2, exponent);
  86. }
  87. if (type === 'Float64') { // step 4
  88. /*
  89. Let value be the byte elements of rawBytes concatenated and interpreted as a little-endian bit string encoding of an IEEE 754-2008 binary64 value.
  90. If value is an IEEE 754-2008 binary64 NaN value, return the NaN Number value.
  91. Return the Number value that corresponds to value.
  92. */
  93. var sign = rawBytes[7] & 0x80 ? -1 : 1; // first bit
  94. var exponent = ((rawBytes[7] & 0x7F) << 4) // 7 bits from index 7
  95. | ((rawBytes[6] & 0xF0) >> 4); // 4 bits from index 6
  96. var mantissa = ((rawBytes[6] & 0x0F) * 0x1000000000000) // 4 bits from index 6
  97. + (rawBytes[5] * 0x10000000000) // 8 bits from index 5
  98. + (rawBytes[4] * 0x100000000) // 8 bits from index 4
  99. + (rawBytes[3] * 0x1000000) // 8 bits from index 3
  100. + (rawBytes[2] * 0x10000) // 8 bits from index 2
  101. + (rawBytes[1] * 0x100) // 8 bits from index 1
  102. + rawBytes[0]; // 8 bits from index 0
  103. if (exponent === 0 && mantissa === 0) {
  104. return sign * 0;
  105. }
  106. if (exponent === 0x7FF && mantissa !== 0) {
  107. return NaN;
  108. }
  109. if (exponent === 0x7FF && mantissa === 0) {
  110. return sign * Infinity;
  111. }
  112. exponent -= 1023; // subtract the bias
  113. return sign * (mantissa + 0x10000000000000) * $pow(2, exponent - 52);
  114. }
  115. // this is common to both branches
  116. var intValue = isBigInt ? $BigInt(0) : 0;
  117. for (var i = 0; i < rawBytes.length; i++) {
  118. intValue |= isBigInt ? $BigInt(rawBytes[i]) << $BigInt(8 * i) : rawBytes[i] << (8 * i);
  119. }
  120. /*
  121. Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number.
  122. */
  123. if (!IsUnsignedElementType(type)) { // steps 5-6
  124. // Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of a binary little-endian 2's complement number of bit length elementSize × 8.
  125. var bitLength = elementSize * 8;
  126. if (bitLength < 32) {
  127. var x = isBigInt ? $BigInt(32 - bitLength) : 32 - bitLength;
  128. intValue = (intValue << x) >> x;
  129. }
  130. }
  131. return intValue; // step 7
  132. };