Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decimal128.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. 'use strict';
  2. var Long = require('./long');
  3. var PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
  4. var PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
  5. var PARSE_NAN_REGEXP = /^(\+|-)?NaN$/i;
  6. var EXPONENT_MAX = 6111;
  7. var EXPONENT_MIN = -6176;
  8. var EXPONENT_BIAS = 6176;
  9. var MAX_DIGITS = 34;
  10. // Nan value bits as 32 bit values (due to lack of longs)
  11. var NAN_BUFFER = [
  12. 0x7c,
  13. 0x00,
  14. 0x00,
  15. 0x00,
  16. 0x00,
  17. 0x00,
  18. 0x00,
  19. 0x00,
  20. 0x00,
  21. 0x00,
  22. 0x00,
  23. 0x00,
  24. 0x00,
  25. 0x00,
  26. 0x00,
  27. 0x00
  28. ].reverse();
  29. // Infinity value bits 32 bit values (due to lack of longs)
  30. var INF_NEGATIVE_BUFFER = [
  31. 0xf8,
  32. 0x00,
  33. 0x00,
  34. 0x00,
  35. 0x00,
  36. 0x00,
  37. 0x00,
  38. 0x00,
  39. 0x00,
  40. 0x00,
  41. 0x00,
  42. 0x00,
  43. 0x00,
  44. 0x00,
  45. 0x00,
  46. 0x00
  47. ].reverse();
  48. var INF_POSITIVE_BUFFER = [
  49. 0x78,
  50. 0x00,
  51. 0x00,
  52. 0x00,
  53. 0x00,
  54. 0x00,
  55. 0x00,
  56. 0x00,
  57. 0x00,
  58. 0x00,
  59. 0x00,
  60. 0x00,
  61. 0x00,
  62. 0x00,
  63. 0x00,
  64. 0x00
  65. ].reverse();
  66. var EXPONENT_REGEX = /^([-+])?(\d+)?$/;
  67. var utils = require('./parser/utils');
  68. // Detect if the value is a digit
  69. var isDigit = function(value) {
  70. return !isNaN(parseInt(value, 10));
  71. };
  72. // Divide two uint128 values
  73. var divideu128 = function(value) {
  74. var DIVISOR = Long.fromNumber(1000 * 1000 * 1000);
  75. var _rem = Long.fromNumber(0);
  76. var i = 0;
  77. if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) {
  78. return { quotient: value, rem: _rem };
  79. }
  80. for (i = 0; i <= 3; i++) {
  81. // Adjust remainder to match value of next dividend
  82. _rem = _rem.shiftLeft(32);
  83. // Add the divided to _rem
  84. _rem = _rem.add(new Long(value.parts[i], 0));
  85. value.parts[i] = _rem.div(DIVISOR).low_;
  86. _rem = _rem.modulo(DIVISOR);
  87. }
  88. return { quotient: value, rem: _rem };
  89. };
  90. // Multiply two Long values and return the 128 bit value
  91. var multiply64x2 = function(left, right) {
  92. if (!left && !right) {
  93. return { high: Long.fromNumber(0), low: Long.fromNumber(0) };
  94. }
  95. var leftHigh = left.shiftRightUnsigned(32);
  96. var leftLow = new Long(left.getLowBits(), 0);
  97. var rightHigh = right.shiftRightUnsigned(32);
  98. var rightLow = new Long(right.getLowBits(), 0);
  99. var productHigh = leftHigh.multiply(rightHigh);
  100. var productMid = leftHigh.multiply(rightLow);
  101. var productMid2 = leftLow.multiply(rightHigh);
  102. var productLow = leftLow.multiply(rightLow);
  103. productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
  104. productMid = new Long(productMid.getLowBits(), 0)
  105. .add(productMid2)
  106. .add(productLow.shiftRightUnsigned(32));
  107. productHigh = productHigh.add(productMid.shiftRightUnsigned(32));
  108. productLow = productMid.shiftLeft(32).add(new Long(productLow.getLowBits(), 0));
  109. // Return the 128 bit result
  110. return { high: productHigh, low: productLow };
  111. };
  112. var lessThan = function(left, right) {
  113. // Make values unsigned
  114. var uhleft = left.high_ >>> 0;
  115. var uhright = right.high_ >>> 0;
  116. // Compare high bits first
  117. if (uhleft < uhright) {
  118. return true;
  119. } else if (uhleft === uhright) {
  120. var ulleft = left.low_ >>> 0;
  121. var ulright = right.low_ >>> 0;
  122. if (ulleft < ulright) return true;
  123. }
  124. return false;
  125. };
  126. // var longtoHex = function(value) {
  127. // var buffer = utils.allocBuffer(8);
  128. // var index = 0;
  129. // // Encode the low 64 bits of the decimal
  130. // // Encode low bits
  131. // buffer[index++] = value.low_ & 0xff;
  132. // buffer[index++] = (value.low_ >> 8) & 0xff;
  133. // buffer[index++] = (value.low_ >> 16) & 0xff;
  134. // buffer[index++] = (value.low_ >> 24) & 0xff;
  135. // // Encode high bits
  136. // buffer[index++] = value.high_ & 0xff;
  137. // buffer[index++] = (value.high_ >> 8) & 0xff;
  138. // buffer[index++] = (value.high_ >> 16) & 0xff;
  139. // buffer[index++] = (value.high_ >> 24) & 0xff;
  140. // return buffer.reverse().toString('hex');
  141. // };
  142. // var int32toHex = function(value) {
  143. // var buffer = utils.allocBuffer(4);
  144. // var index = 0;
  145. // // Encode the low 64 bits of the decimal
  146. // // Encode low bits
  147. // buffer[index++] = value & 0xff;
  148. // buffer[index++] = (value >> 8) & 0xff;
  149. // buffer[index++] = (value >> 16) & 0xff;
  150. // buffer[index++] = (value >> 24) & 0xff;
  151. // return buffer.reverse().toString('hex');
  152. // };
  153. /**
  154. * A class representation of the BSON Decimal128 type.
  155. *
  156. * @class
  157. * @param {Buffer} bytes a buffer containing the raw Decimal128 bytes.
  158. * @return {Double}
  159. */
  160. var Decimal128 = function(bytes) {
  161. this._bsontype = 'Decimal128';
  162. this.bytes = bytes;
  163. };
  164. /**
  165. * Create a Decimal128 instance from a string representation
  166. *
  167. * @method
  168. * @param {string} string a numeric string representation.
  169. * @return {Decimal128} returns a Decimal128 instance.
  170. */
  171. Decimal128.fromString = function(string) {
  172. // Parse state tracking
  173. var isNegative = false;
  174. var sawRadix = false;
  175. var foundNonZero = false;
  176. // Total number of significant digits (no leading or trailing zero)
  177. var significantDigits = 0;
  178. // Total number of significand digits read
  179. var nDigitsRead = 0;
  180. // Total number of digits (no leading zeros)
  181. var nDigits = 0;
  182. // The number of the digits after radix
  183. var radixPosition = 0;
  184. // The index of the first non-zero in *str*
  185. var firstNonZero = 0;
  186. // Digits Array
  187. var digits = [0];
  188. // The number of digits in digits
  189. var nDigitsStored = 0;
  190. // Insertion pointer for digits
  191. var digitsInsert = 0;
  192. // The index of the first non-zero digit
  193. var firstDigit = 0;
  194. // The index of the last digit
  195. var lastDigit = 0;
  196. // Exponent
  197. var exponent = 0;
  198. // loop index over array
  199. var i = 0;
  200. // The high 17 digits of the significand
  201. var significandHigh = [0, 0];
  202. // The low 17 digits of the significand
  203. var significandLow = [0, 0];
  204. // The biased exponent
  205. var biasedExponent = 0;
  206. // Read index
  207. var index = 0;
  208. // Trim the string
  209. string = string.trim();
  210. // Naively prevent against REDOS attacks.
  211. // TODO: implementing a custom parsing for this, or refactoring the regex would yield
  212. // further gains.
  213. if (string.length >= 7000) {
  214. throw new Error('' + string + ' not a valid Decimal128 string');
  215. }
  216. // Results
  217. var stringMatch = string.match(PARSE_STRING_REGEXP);
  218. var infMatch = string.match(PARSE_INF_REGEXP);
  219. var nanMatch = string.match(PARSE_NAN_REGEXP);
  220. // Validate the string
  221. if ((!stringMatch && !infMatch && !nanMatch) || string.length === 0) {
  222. throw new Error('' + string + ' not a valid Decimal128 string');
  223. }
  224. // Check if we have an illegal exponent format
  225. if (stringMatch && stringMatch[4] && stringMatch[2] === undefined) {
  226. throw new Error('' + string + ' not a valid Decimal128 string');
  227. }
  228. // Get the negative or positive sign
  229. if (string[index] === '+' || string[index] === '-') {
  230. isNegative = string[index++] === '-';
  231. }
  232. // Check if user passed Infinity or NaN
  233. if (!isDigit(string[index]) && string[index] !== '.') {
  234. if (string[index] === 'i' || string[index] === 'I') {
  235. return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
  236. } else if (string[index] === 'N') {
  237. return new Decimal128(utils.toBuffer(NAN_BUFFER));
  238. }
  239. }
  240. // Read all the digits
  241. while (isDigit(string[index]) || string[index] === '.') {
  242. if (string[index] === '.') {
  243. if (sawRadix) {
  244. return new Decimal128(utils.toBuffer(NAN_BUFFER));
  245. }
  246. sawRadix = true;
  247. index = index + 1;
  248. continue;
  249. }
  250. if (nDigitsStored < 34) {
  251. if (string[index] !== '0' || foundNonZero) {
  252. if (!foundNonZero) {
  253. firstNonZero = nDigitsRead;
  254. }
  255. foundNonZero = true;
  256. // Only store 34 digits
  257. digits[digitsInsert++] = parseInt(string[index], 10);
  258. nDigitsStored = nDigitsStored + 1;
  259. }
  260. }
  261. if (foundNonZero) {
  262. nDigits = nDigits + 1;
  263. }
  264. if (sawRadix) {
  265. radixPosition = radixPosition + 1;
  266. }
  267. nDigitsRead = nDigitsRead + 1;
  268. index = index + 1;
  269. }
  270. if (sawRadix && !nDigitsRead) {
  271. throw new Error('' + string + ' not a valid Decimal128 string');
  272. }
  273. // Read exponent if exists
  274. if (string[index] === 'e' || string[index] === 'E') {
  275. // Read exponent digits
  276. var match = string.substr(++index).match(EXPONENT_REGEX);
  277. // No digits read
  278. if (!match || !match[2]) {
  279. return new Decimal128(utils.toBuffer(NAN_BUFFER));
  280. }
  281. // Get exponent
  282. exponent = parseInt(match[0], 10);
  283. // Adjust the index
  284. index = index + match[0].length;
  285. }
  286. // Return not a number
  287. if (string[index]) {
  288. return new Decimal128(utils.toBuffer(NAN_BUFFER));
  289. }
  290. // Done reading input
  291. // Find first non-zero digit in digits
  292. firstDigit = 0;
  293. if (!nDigitsStored) {
  294. firstDigit = 0;
  295. lastDigit = 0;
  296. digits[0] = 0;
  297. nDigits = 1;
  298. nDigitsStored = 1;
  299. significantDigits = 0;
  300. } else {
  301. lastDigit = nDigitsStored - 1;
  302. significantDigits = nDigits;
  303. if (exponent !== 0 && significantDigits !== 1) {
  304. while (string[firstNonZero + significantDigits - 1] === '0') {
  305. significantDigits = significantDigits - 1;
  306. }
  307. }
  308. }
  309. // Normalization of exponent
  310. // Correct exponent based on radix position, and shift significand as needed
  311. // to represent user input
  312. // Overflow prevention
  313. if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
  314. exponent = EXPONENT_MIN;
  315. } else {
  316. exponent = exponent - radixPosition;
  317. }
  318. // Attempt to normalize the exponent
  319. while (exponent > EXPONENT_MAX) {
  320. // Shift exponent to significand and decrease
  321. lastDigit = lastDigit + 1;
  322. if (lastDigit - firstDigit > MAX_DIGITS) {
  323. // Check if we have a zero then just hard clamp, otherwise fail
  324. var digitsString = digits.join('');
  325. if (digitsString.match(/^0+$/)) {
  326. exponent = EXPONENT_MAX;
  327. break;
  328. } else {
  329. return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
  330. }
  331. }
  332. exponent = exponent - 1;
  333. }
  334. while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
  335. // Shift last digit
  336. if (lastDigit === 0) {
  337. exponent = EXPONENT_MIN;
  338. significantDigits = 0;
  339. break;
  340. }
  341. if (nDigitsStored < nDigits) {
  342. // adjust to match digits not stored
  343. nDigits = nDigits - 1;
  344. } else {
  345. // adjust to round
  346. lastDigit = lastDigit - 1;
  347. }
  348. if (exponent < EXPONENT_MAX) {
  349. exponent = exponent + 1;
  350. } else {
  351. // Check if we have a zero then just hard clamp, otherwise fail
  352. digitsString = digits.join('');
  353. if (digitsString.match(/^0+$/)) {
  354. exponent = EXPONENT_MAX;
  355. break;
  356. } else {
  357. return new Decimal128(utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
  358. }
  359. }
  360. }
  361. // Round
  362. // We've normalized the exponent, but might still need to round.
  363. if (lastDigit - firstDigit + 1 < significantDigits && string[significantDigits] !== '0') {
  364. var endOfString = nDigitsRead;
  365. // If we have seen a radix point, 'string' is 1 longer than we have
  366. // documented with ndigits_read, so inc the position of the first nonzero
  367. // digit and the position that digits are read to.
  368. if (sawRadix && exponent === EXPONENT_MIN) {
  369. firstNonZero = firstNonZero + 1;
  370. endOfString = endOfString + 1;
  371. }
  372. var roundDigit = parseInt(string[firstNonZero + lastDigit + 1], 10);
  373. var roundBit = 0;
  374. if (roundDigit >= 5) {
  375. roundBit = 1;
  376. if (roundDigit === 5) {
  377. roundBit = digits[lastDigit] % 2 === 1;
  378. for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
  379. if (parseInt(string[i], 10)) {
  380. roundBit = 1;
  381. break;
  382. }
  383. }
  384. }
  385. }
  386. if (roundBit) {
  387. var dIdx = lastDigit;
  388. for (; dIdx >= 0; dIdx--) {
  389. if (++digits[dIdx] > 9) {
  390. digits[dIdx] = 0;
  391. // overflowed most significant digit
  392. if (dIdx === 0) {
  393. if (exponent < EXPONENT_MAX) {
  394. exponent = exponent + 1;
  395. digits[dIdx] = 1;
  396. } else {
  397. return new Decimal128(
  398. utils.toBuffer(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
  399. );
  400. }
  401. }
  402. } else {
  403. break;
  404. }
  405. }
  406. }
  407. }
  408. // Encode significand
  409. // The high 17 digits of the significand
  410. significandHigh = Long.fromNumber(0);
  411. // The low 17 digits of the significand
  412. significandLow = Long.fromNumber(0);
  413. // read a zero
  414. if (significantDigits === 0) {
  415. significandHigh = Long.fromNumber(0);
  416. significandLow = Long.fromNumber(0);
  417. } else if (lastDigit - firstDigit < 17) {
  418. dIdx = firstDigit;
  419. significandLow = Long.fromNumber(digits[dIdx++]);
  420. significandHigh = new Long(0, 0);
  421. for (; dIdx <= lastDigit; dIdx++) {
  422. significandLow = significandLow.multiply(Long.fromNumber(10));
  423. significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
  424. }
  425. } else {
  426. dIdx = firstDigit;
  427. significandHigh = Long.fromNumber(digits[dIdx++]);
  428. for (; dIdx <= lastDigit - 17; dIdx++) {
  429. significandHigh = significandHigh.multiply(Long.fromNumber(10));
  430. significandHigh = significandHigh.add(Long.fromNumber(digits[dIdx]));
  431. }
  432. significandLow = Long.fromNumber(digits[dIdx++]);
  433. for (; dIdx <= lastDigit; dIdx++) {
  434. significandLow = significandLow.multiply(Long.fromNumber(10));
  435. significandLow = significandLow.add(Long.fromNumber(digits[dIdx]));
  436. }
  437. }
  438. var significand = multiply64x2(significandHigh, Long.fromString('100000000000000000'));
  439. significand.low = significand.low.add(significandLow);
  440. if (lessThan(significand.low, significandLow)) {
  441. significand.high = significand.high.add(Long.fromNumber(1));
  442. }
  443. // Biased exponent
  444. biasedExponent = exponent + EXPONENT_BIAS;
  445. var dec = { low: Long.fromNumber(0), high: Long.fromNumber(0) };
  446. // Encode combination, exponent, and significand.
  447. if (
  448. significand.high
  449. .shiftRightUnsigned(49)
  450. .and(Long.fromNumber(1))
  451. .equals(Long.fromNumber)
  452. ) {
  453. // Encode '11' into bits 1 to 3
  454. dec.high = dec.high.or(Long.fromNumber(0x3).shiftLeft(61));
  455. dec.high = dec.high.or(
  456. Long.fromNumber(biasedExponent).and(Long.fromNumber(0x3fff).shiftLeft(47))
  457. );
  458. dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x7fffffffffff)));
  459. } else {
  460. dec.high = dec.high.or(Long.fromNumber(biasedExponent & 0x3fff).shiftLeft(49));
  461. dec.high = dec.high.or(significand.high.and(Long.fromNumber(0x1ffffffffffff)));
  462. }
  463. dec.low = significand.low;
  464. // Encode sign
  465. if (isNegative) {
  466. dec.high = dec.high.or(Long.fromString('9223372036854775808'));
  467. }
  468. // Encode into a buffer
  469. var buffer = utils.allocBuffer(16);
  470. index = 0;
  471. // Encode the low 64 bits of the decimal
  472. // Encode low bits
  473. buffer[index++] = dec.low.low_ & 0xff;
  474. buffer[index++] = (dec.low.low_ >> 8) & 0xff;
  475. buffer[index++] = (dec.low.low_ >> 16) & 0xff;
  476. buffer[index++] = (dec.low.low_ >> 24) & 0xff;
  477. // Encode high bits
  478. buffer[index++] = dec.low.high_ & 0xff;
  479. buffer[index++] = (dec.low.high_ >> 8) & 0xff;
  480. buffer[index++] = (dec.low.high_ >> 16) & 0xff;
  481. buffer[index++] = (dec.low.high_ >> 24) & 0xff;
  482. // Encode the high 64 bits of the decimal
  483. // Encode low bits
  484. buffer[index++] = dec.high.low_ & 0xff;
  485. buffer[index++] = (dec.high.low_ >> 8) & 0xff;
  486. buffer[index++] = (dec.high.low_ >> 16) & 0xff;
  487. buffer[index++] = (dec.high.low_ >> 24) & 0xff;
  488. // Encode high bits
  489. buffer[index++] = dec.high.high_ & 0xff;
  490. buffer[index++] = (dec.high.high_ >> 8) & 0xff;
  491. buffer[index++] = (dec.high.high_ >> 16) & 0xff;
  492. buffer[index++] = (dec.high.high_ >> 24) & 0xff;
  493. // Return the new Decimal128
  494. return new Decimal128(buffer);
  495. };
  496. // Extract least significant 5 bits
  497. var COMBINATION_MASK = 0x1f;
  498. // Extract least significant 14 bits
  499. var EXPONENT_MASK = 0x3fff;
  500. // Value of combination field for Inf
  501. var COMBINATION_INFINITY = 30;
  502. // Value of combination field for NaN
  503. var COMBINATION_NAN = 31;
  504. // Value of combination field for NaN
  505. // var COMBINATION_SNAN = 32;
  506. // decimal128 exponent bias
  507. EXPONENT_BIAS = 6176;
  508. /**
  509. * Create a string representation of the raw Decimal128 value
  510. *
  511. * @method
  512. * @return {string} returns a Decimal128 string representation.
  513. */
  514. Decimal128.prototype.toString = function() {
  515. // Note: bits in this routine are referred to starting at 0,
  516. // from the sign bit, towards the coefficient.
  517. // bits 0 - 31
  518. var high;
  519. // bits 32 - 63
  520. var midh;
  521. // bits 64 - 95
  522. var midl;
  523. // bits 96 - 127
  524. var low;
  525. // bits 1 - 5
  526. var combination;
  527. // decoded biased exponent (14 bits)
  528. var biased_exponent;
  529. // the number of significand digits
  530. var significand_digits = 0;
  531. // the base-10 digits in the significand
  532. var significand = new Array(36);
  533. for (var i = 0; i < significand.length; i++) significand[i] = 0;
  534. // read pointer into significand
  535. var index = 0;
  536. // unbiased exponent
  537. var exponent;
  538. // the exponent if scientific notation is used
  539. var scientific_exponent;
  540. // true if the number is zero
  541. var is_zero = false;
  542. // the most signifcant significand bits (50-46)
  543. var significand_msb;
  544. // temporary storage for significand decoding
  545. var significand128 = { parts: new Array(4) };
  546. // indexing variables
  547. i;
  548. var j, k;
  549. // Output string
  550. var string = [];
  551. // Unpack index
  552. index = 0;
  553. // Buffer reference
  554. var buffer = this.bytes;
  555. // Unpack the low 64bits into a long
  556. low =
  557. buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
  558. midl =
  559. buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
  560. // Unpack the high 64bits into a long
  561. midh =
  562. buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
  563. high =
  564. buffer[index++] | (buffer[index++] << 8) | (buffer[index++] << 16) | (buffer[index++] << 24);
  565. // Unpack index
  566. index = 0;
  567. // Create the state of the decimal
  568. var dec = {
  569. low: new Long(low, midl),
  570. high: new Long(midh, high)
  571. };
  572. if (dec.high.lessThan(Long.ZERO)) {
  573. string.push('-');
  574. }
  575. // Decode combination field and exponent
  576. combination = (high >> 26) & COMBINATION_MASK;
  577. if (combination >> 3 === 3) {
  578. // Check for 'special' values
  579. if (combination === COMBINATION_INFINITY) {
  580. return string.join('') + 'Infinity';
  581. } else if (combination === COMBINATION_NAN) {
  582. return 'NaN';
  583. } else {
  584. biased_exponent = (high >> 15) & EXPONENT_MASK;
  585. significand_msb = 0x08 + ((high >> 14) & 0x01);
  586. }
  587. } else {
  588. significand_msb = (high >> 14) & 0x07;
  589. biased_exponent = (high >> 17) & EXPONENT_MASK;
  590. }
  591. exponent = biased_exponent - EXPONENT_BIAS;
  592. // Create string of significand digits
  593. // Convert the 114-bit binary number represented by
  594. // (significand_high, significand_low) to at most 34 decimal
  595. // digits through modulo and division.
  596. significand128.parts[0] = (high & 0x3fff) + ((significand_msb & 0xf) << 14);
  597. significand128.parts[1] = midh;
  598. significand128.parts[2] = midl;
  599. significand128.parts[3] = low;
  600. if (
  601. significand128.parts[0] === 0 &&
  602. significand128.parts[1] === 0 &&
  603. significand128.parts[2] === 0 &&
  604. significand128.parts[3] === 0
  605. ) {
  606. is_zero = true;
  607. } else {
  608. for (k = 3; k >= 0; k--) {
  609. var least_digits = 0;
  610. // Peform the divide
  611. var result = divideu128(significand128);
  612. significand128 = result.quotient;
  613. least_digits = result.rem.low_;
  614. // We now have the 9 least significant digits (in base 2).
  615. // Convert and output to string.
  616. if (!least_digits) continue;
  617. for (j = 8; j >= 0; j--) {
  618. // significand[k * 9 + j] = Math.round(least_digits % 10);
  619. significand[k * 9 + j] = least_digits % 10;
  620. // least_digits = Math.round(least_digits / 10);
  621. least_digits = Math.floor(least_digits / 10);
  622. }
  623. }
  624. }
  625. // Output format options:
  626. // Scientific - [-]d.dddE(+/-)dd or [-]dE(+/-)dd
  627. // Regular - ddd.ddd
  628. if (is_zero) {
  629. significand_digits = 1;
  630. significand[index] = 0;
  631. } else {
  632. significand_digits = 36;
  633. i = 0;
  634. while (!significand[index]) {
  635. i++;
  636. significand_digits = significand_digits - 1;
  637. index = index + 1;
  638. }
  639. }
  640. scientific_exponent = significand_digits - 1 + exponent;
  641. // The scientific exponent checks are dictated by the string conversion
  642. // specification and are somewhat arbitrary cutoffs.
  643. //
  644. // We must check exponent > 0, because if this is the case, the number
  645. // has trailing zeros. However, we *cannot* output these trailing zeros,
  646. // because doing so would change the precision of the value, and would
  647. // change stored data if the string converted number is round tripped.
  648. if (scientific_exponent >= 34 || scientific_exponent <= -7 || exponent > 0) {
  649. // Scientific format
  650. string.push(significand[index++]);
  651. significand_digits = significand_digits - 1;
  652. if (significand_digits) {
  653. string.push('.');
  654. }
  655. for (i = 0; i < significand_digits; i++) {
  656. string.push(significand[index++]);
  657. }
  658. // Exponent
  659. string.push('E');
  660. if (scientific_exponent > 0) {
  661. string.push('+' + scientific_exponent);
  662. } else {
  663. string.push(scientific_exponent);
  664. }
  665. } else {
  666. // Regular format with no decimal place
  667. if (exponent >= 0) {
  668. for (i = 0; i < significand_digits; i++) {
  669. string.push(significand[index++]);
  670. }
  671. } else {
  672. var radix_position = significand_digits + exponent;
  673. // non-zero digits before radix
  674. if (radix_position > 0) {
  675. for (i = 0; i < radix_position; i++) {
  676. string.push(significand[index++]);
  677. }
  678. } else {
  679. string.push('0');
  680. }
  681. string.push('.');
  682. // add leading zeros after radix
  683. while (radix_position++ < 0) {
  684. string.push('0');
  685. }
  686. for (i = 0; i < significand_digits - Math.max(radix_position - 1, 0); i++) {
  687. string.push(significand[index++]);
  688. }
  689. }
  690. }
  691. return string.join('');
  692. };
  693. Decimal128.prototype.toJSON = function() {
  694. return { $numberDecimal: this.toString() };
  695. };
  696. module.exports = Decimal128;
  697. module.exports.Decimal128 = Decimal128;