float_parser.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. // Copyright (c) 2008, Fair Oaks Labs, Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. //
  11. // * Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
  16. // may be used to endorse or promote products derived from this software
  17. // without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. // POSSIBILITY OF SUCH DAMAGE.
  30. //
  31. //
  32. // Modifications to writeIEEE754 to support negative zeroes made by Brian White
  33. function readIEEE754(buffer, offset, endian, mLen, nBytes) {
  34. let e,
  35. m,
  36. bBE = endian === 'big',
  37. eLen = nBytes * 8 - mLen - 1,
  38. eMax = (1 << eLen) - 1,
  39. eBias = eMax >> 1,
  40. nBits = -7,
  41. i = bBE ? 0 : nBytes - 1,
  42. d = bBE ? 1 : -1,
  43. s = buffer[offset + i];
  44. i += d;
  45. e = s & ((1 << -nBits) - 1);
  46. s >>= -nBits;
  47. nBits += eLen;
  48. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
  49. m = e & ((1 << -nBits) - 1);
  50. e >>= -nBits;
  51. nBits += mLen;
  52. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
  53. if (e === 0) {
  54. e = 1 - eBias;
  55. } else if (e === eMax) {
  56. return m ? NaN : (s ? -1 : 1) * Infinity;
  57. } else {
  58. m = m + Math.pow(2, mLen);
  59. e = e - eBias;
  60. }
  61. return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
  62. }
  63. function writeIEEE754(buffer, value, offset, endian, mLen, nBytes) {
  64. let e,
  65. m,
  66. c,
  67. bBE = endian === 'big',
  68. eLen = nBytes * 8 - mLen - 1,
  69. eMax = (1 << eLen) - 1,
  70. eBias = eMax >> 1,
  71. rt = mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0,
  72. i = bBE ? nBytes - 1 : 0,
  73. d = bBE ? -1 : 1,
  74. s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
  75. value = Math.abs(value);
  76. if (isNaN(value) || value === Infinity) {
  77. m = isNaN(value) ? 1 : 0;
  78. e = eMax;
  79. } else {
  80. e = Math.floor(Math.log(value) / Math.LN2);
  81. if (value * (c = Math.pow(2, -e)) < 1) {
  82. e--;
  83. c *= 2;
  84. }
  85. if (e + eBias >= 1) {
  86. value += rt / c;
  87. } else {
  88. value += rt * Math.pow(2, 1 - eBias);
  89. }
  90. if (value * c >= 2) {
  91. e++;
  92. c /= 2;
  93. }
  94. if (e + eBias >= eMax) {
  95. m = 0;
  96. e = eMax;
  97. } else if (e + eBias >= 1) {
  98. m = (value * c - 1) * Math.pow(2, mLen);
  99. e = e + eBias;
  100. } else {
  101. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
  102. e = 0;
  103. }
  104. }
  105. if (isNaN(value)) m = 0;
  106. while (mLen >= 8) {
  107. buffer[offset + i] = m & 0xff;
  108. i += d;
  109. m /= 256;
  110. mLen -= 8;
  111. }
  112. e = (e << mLen) | m;
  113. if (isNaN(value)) e += 8;
  114. eLen += mLen;
  115. while (eLen > 0) {
  116. buffer[offset + i] = e & 0xff;
  117. i += d;
  118. e /= 256;
  119. eLen -= 8;
  120. }
  121. buffer[offset + i - d] |= s * 128;
  122. }
  123. module.exports = {
  124. readIEEE754,
  125. writeIEEE754
  126. };