RawBytesToNumber.js 5.0 KB

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