nbcd.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "nbcd.hpp"
  15. #include "jlib.hpp"
  16. #include "jexcept.hpp"
  17. #ifdef _WIN32
  18. #define NOMEMCPY volatile // stop VC++ doing a stupid optimization
  19. #else
  20. #define NOMEMCPY
  21. #endif
  22. static double Pow10[] = { 1, 10, 100, 1000, 10000, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10,1e11,1e12,1e13,1e14,1e15,1e16 };
  23. static int signMap[16] = { 0,0,0,0,0,0,0,0,0,0,+1,-1,+1,-1,+1,+1 };
  24. Decimal & Decimal::abs()
  25. {
  26. negative = false;
  27. return *this;
  28. }
  29. Decimal & Decimal::add(const Decimal & other)
  30. {
  31. if (negative == other.negative)
  32. return addDigits(other);
  33. else
  34. return subtractDigits(other);
  35. }
  36. Decimal & Decimal::addDigits(const Decimal & other)
  37. {
  38. extendRange(other);
  39. byte oLo = other.lsb;
  40. byte oHi = other.msb;
  41. byte hi = msb;
  42. unsigned idx;
  43. byte carry = 0;
  44. for (idx = oLo; (idx <= oHi); idx++)
  45. {
  46. digits[idx] += other.digits[idx] + carry;
  47. carry = 0;
  48. if (digits[idx] > 9)
  49. {
  50. carry++;
  51. digits[idx] -= 10;
  52. }
  53. }
  54. for (;carry && idx <= hi; idx++)
  55. {
  56. digits[idx]++;
  57. carry = 0;
  58. if (digits[idx] > 9)
  59. {
  60. carry = 1;
  61. digits[idx] -= 10;
  62. }
  63. }
  64. if (carry && hi != lastDigit)
  65. {
  66. digits[++hi] = carry;
  67. msb = hi;
  68. }
  69. return *this;
  70. }
  71. int Decimal::compareNull() const
  72. {
  73. byte idx;
  74. for (idx = lsb; idx <= msb; idx++)
  75. {
  76. if (digits[idx]) return negative ? -1 : +1;
  77. }
  78. return 0;
  79. }
  80. int Decimal::compare(const Decimal & other) const
  81. {
  82. int lo1, hi1, lo2, hi2;
  83. clip(lo1, hi1);
  84. other.clip(lo2, hi2);
  85. //First check for zero comparison..
  86. if (lo1 > hi1)
  87. {
  88. if (lo2 > hi2)
  89. return 0;
  90. return other.negative ? +1 : -1;
  91. }
  92. if (lo2 > hi2)
  93. return negative ? -1 : +1;
  94. if (negative ^ other.negative)
  95. return negative ? -1 : +1;
  96. if (hi1 != hi2)
  97. return (hi1 > hi2) ^ negative ? +1 : -1;
  98. int limit = lo1 < lo2 ? lo2 : lo1;
  99. for (;hi1 >= limit; hi1--)
  100. {
  101. int diff = digits[hi1] - other.digits[hi1];
  102. if (diff != 0)
  103. return (diff > 0) ^ negative ? +1 : -1;
  104. }
  105. if (lo1 == lo2)
  106. return 0;
  107. if (lo1 < lo2)
  108. return negative ? -1 : +1;
  109. return negative ? +1 : -1;
  110. }
  111. Decimal & Decimal::divide(const Decimal & other)
  112. {
  113. //NB: Round towards zero
  114. int lo1, hi1, lo2, hi2;
  115. clip(lo1, hi1);
  116. other.clip(lo2, hi2);
  117. int nd1 = hi1+1-lo1;
  118. int nd2 = hi2+1-lo2;
  119. int hi = (hi1-hi2)+zeroDigit; // how many digits will there be in the result
  120. int iters = hi+1;
  121. if (hi < 0)
  122. {
  123. setZero();
  124. return *this;
  125. }
  126. if (hi2 < lo2)
  127. {
  128. //Division by zero defined to return 0 instead of throw an exception
  129. setZero();
  130. return *this;
  131. }
  132. lsb = 0;
  133. msb = hi >= maxDigits ? maxDigits-1 : hi;
  134. const byte spare = 3;
  135. byte temp[maxDigits*2 + 3];
  136. unsigned numeratorDigits = (hi + 1) + nd2;
  137. memset(temp, 0, numeratorDigits+spare); // ensure two zero in msb, and below lsb. Also 2 zeros for looking 2 bytes ahead..
  138. byte * numerator = temp+spare;
  139. if (numeratorDigits > (unsigned)nd1)
  140. memcpy(numerator + numeratorDigits - 1 - nd1, digits+lo1, nd1);
  141. else
  142. memcpy(numerator, digits + hi1 + 1 - (numeratorDigits-1), numeratorDigits-1);
  143. unsigned divisor01 = other.digits[hi2] * 10;
  144. if (hi2 != lo2)
  145. divisor01 += other.digits[hi2-1];
  146. //MORE: Terminate early for exact divide..
  147. const byte * divisor = other.digits + lo2;
  148. for (int iter = iters; iter--; )
  149. {
  150. //The following guess for q is never too small, may be 1 too large
  151. byte * curNumerator = numerator + iter;
  152. unsigned numerator012 = curNumerator[nd2] * 100 + curNumerator[nd2-1] * 10 + curNumerator[nd2-2];
  153. unsigned q = numerator012 / divisor01;
  154. if (q == 10) q--;
  155. if (q)
  156. {
  157. unsigned carry = 0;
  158. for (int i = 0; i < nd2; i++)
  159. {
  160. int next = 90 + curNumerator[i] - divisor[i] * q - carry;
  161. div_t values = div(next, 10);
  162. carry = 9 - values.quot;
  163. curNumerator[i] = values.rem;
  164. }
  165. carry -= curNumerator[nd2];
  166. if (carry)
  167. {
  168. q--;
  169. assertex(carry==1);
  170. carry = 0;
  171. for (int i = 0; i < nd2; i++)
  172. {
  173. byte next = curNumerator[i] + divisor[i] + carry;
  174. carry = 0;
  175. if (next >= 10)
  176. {
  177. next -= 10;
  178. carry = 1;
  179. }
  180. curNumerator[i] = next;
  181. }
  182. assertex(carry);
  183. }
  184. }
  185. if (iter < maxDigits)
  186. digits[iter] = q;
  187. }
  188. //MORE: This should really calculate the next digit, and conditionally round the least significant digit.
  189. if (true)
  190. {
  191. //The following guess for q is never too small, may be 1 too large
  192. byte * curNumerator = numerator-1;
  193. unsigned numerator012 = curNumerator[nd2] * 100 + curNumerator[nd2-1] * 10 + curNumerator[nd2-2];
  194. unsigned q = numerator012 / divisor01;
  195. if (q == 5)
  196. {
  197. unsigned carry = 0;
  198. for (int i = 0; i < nd2; i++)
  199. {
  200. int next = 90 + curNumerator[i] - divisor[i] * q - carry;
  201. carry = 9 - next / 10;
  202. }
  203. carry -= curNumerator[nd2];
  204. if (carry)
  205. q--;
  206. }
  207. if (q >= 5)
  208. {
  209. for (int roundDigit=0; roundDigit < iters; roundDigit++)
  210. {
  211. unsigned next = digits[roundDigit]+1;
  212. if (next == 10)
  213. next = 0;
  214. digits[roundDigit] = next;
  215. if (next != 0)
  216. break;
  217. }
  218. }
  219. }
  220. negative ^= other.negative;
  221. return *this;
  222. }
  223. void Decimal::extendRange(byte oLsb, byte oMsb)
  224. {
  225. byte index;
  226. if (lsb > oLsb)
  227. {
  228. for (index = oLsb; index != lsb; index++)
  229. digits[index] =0;
  230. lsb = oLsb;
  231. }
  232. if (msb < oMsb)
  233. {
  234. for (index = msb+1; index <= oMsb; index++)
  235. digits[index] = 0;
  236. msb = oMsb;
  237. }
  238. }
  239. bool Decimal::isZero() const
  240. {
  241. //NB: Round towards zero
  242. int lo, hi;
  243. clip(lo, hi);
  244. return (hi < lo);
  245. }
  246. Decimal & Decimal::modulus(const Decimal & other)
  247. {
  248. Decimal left(*this);
  249. left.divide(other).truncate(0).multiply(other);
  250. return subtract(left);
  251. }
  252. Decimal & Decimal::multiply(const Decimal & other)
  253. {
  254. int low1, high1, low2, high2, lowt, hight;
  255. clip(low1, high1);
  256. other.clip(low2, high2);
  257. lowt = low1+low2-zeroDigit;
  258. if (lowt < 0) lowt = 0;
  259. hight = high1 + high2 - zeroDigit;
  260. if (hight >= maxDigits) hight = maxDigits-1;
  261. else if (hight < 0)
  262. {
  263. if (hight < -1)
  264. {
  265. setZero();
  266. return *this;
  267. }
  268. hight = 0;
  269. }
  270. unsigned temp[maxDigits*2];
  271. _clear(temp);
  272. // memset(temp+low1+low2, 0, (high1+high2-low1-low2+2)*sizeof(unsigned)); // only need to clear part of the target we're adding to.
  273. //More: could copy across 1st time round - might be worth it.
  274. const byte * digits1 = digits;
  275. const byte * digits2 = other.digits;
  276. for (int i = low1; i <= high1; i++)
  277. {
  278. byte next = digits1[i];
  279. if (next)
  280. {
  281. for (int j=low2; j <= high2; j++)
  282. temp[i+j] += next * digits2[j];
  283. }
  284. }
  285. //Now copy the results, taking care of the carries
  286. unsigned carry = 0;
  287. int j;
  288. for (j = low1+low2 - zeroDigit; j < lowt; j++)
  289. {
  290. unsigned next = temp[j+zeroDigit]+carry;
  291. //Round the least significant digit
  292. if (j+1 == lowt)
  293. next += 5;
  294. carry = next / 10;
  295. }
  296. for (j = lowt; j <= hight; j++)
  297. {
  298. div_t next = div(temp[j+zeroDigit]+carry, 10);
  299. digits[j] = next.rem;
  300. carry = next.quot;
  301. }
  302. while ((hight < maxDigits-1) && (carry != 0))
  303. {
  304. digits[++hight] = carry % 10;
  305. carry = carry / 10;
  306. }
  307. lsb = lowt;
  308. msb = hight;
  309. negative ^= other.negative;
  310. return *this;
  311. }
  312. Decimal & Decimal::negate()
  313. {
  314. negative = !negative;
  315. return *this;
  316. }
  317. Decimal & Decimal::power(unsigned value)
  318. {
  319. if (value == 0)
  320. setInt(1);
  321. else
  322. doPower(value);
  323. return *this;
  324. }
  325. Decimal & Decimal::power(int value)
  326. {
  327. if ( value >= 0)
  328. return power((unsigned)value);
  329. #if 1
  330. //This probably gives slightly more expected results, but both suffer from rounding errors.
  331. Decimal reciprocal;
  332. reciprocal.setInt(1);
  333. reciprocal.divide(*this);
  334. set(reciprocal);
  335. doPower((unsigned)-value);
  336. return *this;
  337. #else
  338. doPower((unsigned)-value);
  339. Decimal reciprocal;
  340. reciprocal.setInt(1);
  341. reciprocal.divide(*this);
  342. set(reciprocal);
  343. return *this;
  344. #endif
  345. }
  346. Decimal & Decimal::incLSD()
  347. {
  348. unsigned index = lsb;
  349. while (index <= msb)
  350. {
  351. if (++digits[index] != 10)
  352. {
  353. lsb = index;
  354. return *this;
  355. }
  356. digits[index] = 0;
  357. index++;
  358. }
  359. digits[++msb] = 1;
  360. return *this;
  361. }
  362. Decimal & Decimal::round(int places)
  363. {
  364. //out of range - either 0 or overflow
  365. if (places < -maxPrecision)
  366. {
  367. setZero();
  368. return *this;
  369. }
  370. if (zeroDigit - places <= lsb)
  371. return *this;
  372. lsb = zeroDigit - places;
  373. if (lsb > msb)
  374. {
  375. digits[lsb] = 0;
  376. if ((lsb == msb+1) && digits[msb] >= 5)
  377. digits[lsb]++;
  378. msb = lsb;
  379. return *this;
  380. }
  381. if (digits[lsb-1] < 5)
  382. return *this;
  383. return incLSD();
  384. }
  385. Decimal & Decimal::roundup(int places)
  386. {
  387. if ((places >= maxPrecision) || (zeroDigit - places <= lsb))
  388. return *this;
  389. unsigned lower = lsb;
  390. lsb = zeroDigit - places;
  391. for (unsigned i=lower; i < lsb; i++)
  392. {
  393. if (digits[i])
  394. return incLSD();
  395. }
  396. return *this;
  397. }
  398. void Decimal::getPrecision(unsigned & digits, unsigned & precision)
  399. {
  400. //Ensures digits>=precision && precision >= 0
  401. unsigned top = msb >= zeroDigit ? msb+1 : zeroDigit;
  402. unsigned low = lsb >= zeroDigit ? zeroDigit : lsb;
  403. digits = (top == low) ? 1 : top - low;
  404. precision = zeroDigit-low;
  405. }
  406. void Decimal::getClipPrecision(unsigned & digits, unsigned & precision)
  407. {
  408. int lo, hi;
  409. clip(lo, hi);
  410. if (lo > hi)
  411. {
  412. digits = 1;
  413. precision = 0;
  414. }
  415. else
  416. {
  417. //Ensures digits>=precision && precision >= 0
  418. unsigned top = hi >= zeroDigit ? hi+1 : zeroDigit;
  419. unsigned low = lo >= zeroDigit ? zeroDigit : lo;
  420. digits = (top == low) ? 1 : top - low;
  421. precision = zeroDigit-low;
  422. }
  423. }
  424. Decimal & Decimal::setPrecision(byte numDigits, byte precision)
  425. {
  426. unsigned char newhigh = zeroDigit + numDigits - precision - 1;
  427. unsigned char newlow = zeroDigit - precision;
  428. if (msb > newhigh)
  429. msb = newhigh;
  430. if (lsb < newlow)
  431. lsb = newlow;
  432. if (lsb > msb)
  433. {
  434. lsb = msb;
  435. digits[lsb] = 0;
  436. }
  437. return *this;
  438. }
  439. Decimal & Decimal::subtract(const Decimal & other)
  440. {
  441. if (negative != other.negative)
  442. return addDigits(other);
  443. else
  444. return subtractDigits(other);
  445. }
  446. Decimal & Decimal::subtractDigits(const Decimal & other)
  447. {
  448. extendRange(other);
  449. byte oLo = other.lsb;
  450. byte oHi = other.msb;
  451. byte hi = msb;
  452. unsigned idx;
  453. byte carry = 0;
  454. for (idx = oLo; (idx <= oHi); idx++)
  455. {
  456. int next = digits[idx] - (other.digits[idx] + carry);
  457. carry = 0;
  458. if (next < 0)
  459. {
  460. carry++;
  461. next += 10;
  462. }
  463. digits[idx] = next;
  464. }
  465. for (;carry && idx <= hi; idx++)
  466. {
  467. digits[idx]--;
  468. carry = 0;
  469. if (digits[idx] == 255)
  470. {
  471. carry = 1;
  472. digits[idx] += 10;
  473. }
  474. }
  475. if (carry)
  476. {
  477. //underflow => complement the result and add 1
  478. negative = !negative;
  479. carry = 1;
  480. for (idx = lsb; idx <= hi; idx++)
  481. {
  482. byte next = 9 - digits[idx] + carry;
  483. carry = 0;
  484. if (next == 10)
  485. {
  486. carry = 1;
  487. next -= 10;
  488. }
  489. digits[idx] = next;
  490. }
  491. assertex(!carry);
  492. }
  493. return *this;
  494. }
  495. Decimal & Decimal::truncate(int places)
  496. {
  497. //out of range - either 0 or overflow
  498. if (places <= -maxIntegerDigits)
  499. {
  500. setZero();
  501. return *this;
  502. }
  503. if (zeroDigit - places > lsb)
  504. {
  505. lsb = zeroDigit - places;
  506. if (lsb > msb)
  507. {
  508. digits[lsb] = 0;
  509. msb = lsb;
  510. }
  511. }
  512. return *this;
  513. }
  514. size32_t Decimal::getStringLength() const
  515. {
  516. int lo, hi;
  517. clip(lo, hi);
  518. if (lo > hi) // (lo == hi) && (digits[lo] == 0))
  519. return 1;
  520. byte top = (hi < zeroDigit) ? zeroDigit-1 : hi;
  521. byte bottom = (lo > zeroDigit) ? zeroDigit : lo;
  522. unsigned outLen = (top + 1 - bottom);
  523. if (negative) outLen++; // '-'
  524. if (lo < zeroDigit) outLen++; // '.'
  525. if (hi < zeroDigit) outLen++; // '0'
  526. return outLen;
  527. }
  528. void Decimal::getCString(size32_t length, char * buffer) const
  529. {
  530. unsigned len = getStringLength();
  531. if (len >= length)
  532. {
  533. memset(buffer, '*', length-1);
  534. buffer[length-1] = 0;
  535. return;
  536. }
  537. unsigned written = doGetString(buffer);
  538. assertex(len == written);
  539. buffer[len] = 0;
  540. }
  541. char * Decimal::getCString() const
  542. {
  543. unsigned len = getStringLength();
  544. char * buffer = (char *)malloc(len+1);
  545. unsigned written = doGetString(buffer);
  546. assertex(len == written);
  547. buffer[len] = 0;
  548. return buffer;
  549. }
  550. void Decimal::getDecimal(byte length, byte precision, void * buffer, byte signs) const
  551. {
  552. doGetDecimal(length, 2*length-1, precision, buffer);
  553. byte sign = negative ? (signs & 0x0f) : (signs >> 4);
  554. ((byte *)buffer)[length-1] |= sign;
  555. }
  556. __int64 Decimal::getInt64() const
  557. {
  558. unsigned __int64 value = getUInt64();
  559. if (negative)
  560. return -(__int64)value;
  561. return (__int64)value;
  562. }
  563. int Decimal::getInt() const
  564. {
  565. unsigned int value = getUInt();
  566. if (negative)
  567. return -(int) value;
  568. return (int) value;
  569. }
  570. double Decimal::getReal() const
  571. {
  572. int lo, hi;
  573. clip(lo, hi);
  574. double total = 0;
  575. byte sigDigits = 16; // Only worth taking 16 places over
  576. int i;
  577. for (i = hi; sigDigits && i >= lo; i--, sigDigits-- )
  578. total = total * 10 + digits[i];
  579. i++;
  580. if ( i > zeroDigit )
  581. {
  582. unsigned amount = i - zeroDigit;
  583. while ( amount > 15 )
  584. {
  585. total *= 1e16;
  586. amount -= 16;
  587. }
  588. total *= Pow10[ amount ];
  589. }
  590. else if ( i < zeroDigit )
  591. {
  592. unsigned amount = zeroDigit - i;
  593. while ( amount > 15 )
  594. {
  595. total /= 1e16;
  596. amount -= 16;
  597. }
  598. total /= Pow10[ amount ];
  599. }
  600. if (negative )
  601. return -total;
  602. else
  603. return total;
  604. }
  605. void Decimal::getString(size32_t length, char * buffer) const
  606. {
  607. unsigned len = getStringLength();
  608. if (len > length)
  609. {
  610. memset(buffer, '*', length);
  611. return;
  612. }
  613. unsigned written = doGetString(buffer);
  614. assertex(len == written);
  615. memset(buffer+len, ' ', length-len);
  616. }
  617. void Decimal::getStringX(size32_t & length, char * & buffer) const
  618. {
  619. unsigned len = getStringLength();
  620. buffer = (char *)malloc(len);
  621. unsigned written = doGetString(buffer);
  622. assertex(len == written);
  623. length = len;
  624. }
  625. void Decimal::getUDecimal(byte length, byte precision, void * buffer) const
  626. {
  627. doGetDecimal(length, 2*length, precision, buffer);
  628. }
  629. unsigned int Decimal::getUInt() const
  630. {
  631. const unsigned hi = msb;
  632. unsigned int value = 0;
  633. if (hi >= zeroDigit)
  634. {
  635. value = digits[hi];
  636. unsigned lo = lsb;
  637. if (lo < zeroDigit)
  638. {
  639. for (unsigned idx = hi-1; idx >= zeroDigit; idx--)
  640. value = value * 10 + digits[idx];
  641. }
  642. else
  643. {
  644. unsigned idx;
  645. for (idx = hi-1; idx >= lo; idx--)
  646. value = value * 10 + digits[idx];
  647. for (;idx >= zeroDigit;idx--)
  648. value *= 10;
  649. }
  650. }
  651. return value;
  652. }
  653. unsigned __int64 Decimal::getUInt64() const
  654. {
  655. //MORE: This isn't the most efficient way of doing it - see num2str in jutil for some hints.
  656. const unsigned hi = msb;
  657. unsigned __int64 value = 0;
  658. if (hi >= zeroDigit)
  659. {
  660. value = digits[hi];
  661. unsigned lo = lsb;
  662. if (lo < zeroDigit)
  663. {
  664. for (unsigned idx = hi-1; idx >= zeroDigit; idx--)
  665. value = value * 10 + digits[idx];
  666. }
  667. else
  668. {
  669. unsigned idx;
  670. for (idx = hi-1; idx >= lo; idx--)
  671. value = value * 10 + digits[idx];
  672. for (;idx >= zeroDigit;idx--)
  673. value *= 10;
  674. }
  675. }
  676. return value;
  677. }
  678. void Decimal::overflow()
  679. {
  680. }
  681. void Decimal::set(const Decimal & value)
  682. {
  683. memcpy(this, &value, sizeof(*this));
  684. }
  685. void Decimal::setCString(const char * buffer)
  686. {
  687. const char * cur = buffer;
  688. while (*cur == ' ')
  689. cur++;
  690. negative = false;
  691. if (*cur == '-')
  692. {
  693. negative = true;
  694. cur++;
  695. }
  696. const char * start = cur;
  697. while (isdigit(*cur))
  698. cur++;
  699. unsigned numDigits = (cur-start);
  700. if (numDigits > maxIntegerDigits)
  701. {
  702. overflow();
  703. numDigits = maxIntegerDigits;
  704. }
  705. int idx;
  706. for (idx = 0; (unsigned)idx < numDigits; idx++)
  707. digits[zeroDigit+idx] = cur[-(idx+1)] - '0'; // careful - if idx is unsigned, -(idx+1) is not sign-extended and fails if int size is not pointer size
  708. msb = zeroDigit+(numDigits-1);
  709. if (*cur == '.')
  710. {
  711. cur++;
  712. const char * start = cur;
  713. const char * limit = cur + maxPrecision;
  714. byte * digit = digits + zeroDigit;
  715. while ((cur < limit) && (isdigit(*cur)))
  716. *--digit = *cur++ - '0';
  717. lsb = zeroDigit - (cur-start);
  718. }
  719. else
  720. lsb = zeroDigit;
  721. }
  722. void Decimal::setDecimal(byte length, byte precision, const void * _buffer)
  723. {
  724. const byte * buffer = (const byte *)_buffer;
  725. lsb = zeroDigit - precision;
  726. msb = lsb + length*2 - 2;
  727. unsigned idx = 0;
  728. while (msb > maxDigits)
  729. {
  730. msb -= 2;
  731. idx++;
  732. }
  733. unsigned cur = msb;
  734. if (msb == maxDigits)
  735. {
  736. msb--;
  737. cur = msb;
  738. digits[cur--] = buffer[idx] & 0x0f;
  739. idx++;
  740. }
  741. for (; (int)idx < length-1; idx++)
  742. {
  743. byte next = buffer[idx];
  744. digits[cur--] = next >> 4;
  745. digits[cur--] = next & 0x0f;
  746. }
  747. byte next = buffer[idx];
  748. digits[cur--] = next >> 4;
  749. negative = (signMap[next & 0x0f] == -1);
  750. }
  751. void Decimal::setInt64(__int64 value)
  752. {
  753. if (value >= 0)
  754. setUInt64((unsigned __int64)value);
  755. else
  756. {
  757. setUInt64((unsigned __int64)-value);
  758. negative = true;
  759. }
  760. }
  761. void Decimal::setInt(int value)
  762. {
  763. if (value >= 0)
  764. setUInt((unsigned int)value);
  765. else
  766. {
  767. setUInt((unsigned int)-value);
  768. negative = true;
  769. }
  770. }
  771. void Decimal::setReal(double value)
  772. {
  773. setZero();
  774. int dec;
  775. int sign;
  776. char digitText[DOUBLE_SIG_DIGITS+2];
  777. if (!safe_ecvt(sizeof(digitText), digitText, value, DOUBLE_SIG_DIGITS, &dec, &sign))
  778. return;
  779. int len = DOUBLE_SIG_DIGITS;
  780. int hi = zeroDigit - 1 + dec;
  781. int lo = hi - (len -1);
  782. const char * finger = digitText;
  783. //Number too big - should it create a maximum value? or truncate as it currently does.
  784. if (hi >= maxDigits) // Most of this work is dealing with out of range cases
  785. {
  786. if (lo >= maxDigits)
  787. return;
  788. finger += (hi - (maxDigits-1));
  789. hi = maxDigits-1;
  790. }
  791. if (lo < 0)
  792. {
  793. if (hi < 0)
  794. return;
  795. lo = 0;
  796. }
  797. msb = hi;
  798. lsb = lo;
  799. for ( int i = hi; i >= lo; i-- )
  800. {
  801. byte next = *finger++ - '0';
  802. if (next < 10)
  803. digits[i] = next;
  804. else
  805. {
  806. //infinity????
  807. setZero();
  808. return;
  809. }
  810. }
  811. if (sign)
  812. negative = true;
  813. }
  814. void Decimal::setString(size32_t length, const char * buffer)
  815. {
  816. const char * limit = buffer+length;
  817. const char * cur = buffer;
  818. while ((cur < limit) && (*cur == ' '))
  819. cur++;
  820. negative = false;
  821. if ((cur < limit) && (*cur == '-'))
  822. {
  823. negative = true;
  824. cur++;
  825. }
  826. const char * start = cur;
  827. while ((cur < limit) && (isdigit(*cur)))
  828. cur++;
  829. unsigned numDigits = (cur-start);
  830. if (numDigits > maxIntegerDigits)
  831. {
  832. overflow();
  833. numDigits = maxIntegerDigits;
  834. }
  835. int idx;
  836. for (idx = 0; idx < (int)numDigits; idx++)
  837. digits[zeroDigit+idx] = cur[-(idx+1)] - '0'; // careful - if idx is unsigned, -(idx+1) is not sign-extended and fails if int size is not pointer size
  838. msb = zeroDigit+(numDigits-1);
  839. if ((cur < limit) && (*cur == '.'))
  840. {
  841. cur++;
  842. const char * start = cur;
  843. if (limit-cur > maxPrecision)
  844. limit = cur + maxPrecision;
  845. byte * digit = digits + zeroDigit;
  846. while ((cur < limit) && (isdigit(*cur)))
  847. *--digit = *cur++ - '0';
  848. lsb = zeroDigit - (cur-start);
  849. }
  850. else
  851. lsb = zeroDigit;
  852. }
  853. void Decimal::setUInt(unsigned int value)
  854. {
  855. negative = false;
  856. lsb = zeroDigit;
  857. unsigned idx = zeroDigit;
  858. while (value > 9)
  859. {
  860. unsigned int next = value / 10;
  861. digits[idx++] = value - next*10;
  862. value = next;
  863. }
  864. digits[idx] = value;
  865. msb = idx;
  866. }
  867. void Decimal::setUInt64(unsigned __int64 value)
  868. {
  869. negative = false;
  870. //MORE: This isn't the most efficient way of doing it - see num2str in jutil for some hints.
  871. lsb = zeroDigit;
  872. unsigned idx = zeroDigit;
  873. while (value > 9)
  874. {
  875. unsigned __int64 next = value / 10;
  876. digits[idx++] = (byte)(value - next*10);
  877. value = next;
  878. }
  879. digits[idx] = (byte)value;
  880. msb = idx;
  881. }
  882. void Decimal::setUDecimal(byte length, byte precision, const void * _buffer)
  883. {
  884. const byte * buffer = (const byte *)_buffer;
  885. lsb = zeroDigit - precision;
  886. msb = lsb + length*2 - 1;
  887. unsigned cur = msb;
  888. for (unsigned idx=0; idx < length; idx++)
  889. {
  890. byte next = buffer[idx];
  891. digits[cur--] = next >> 4;
  892. digits[cur--] = next & 0x0f;
  893. }
  894. negative = false;
  895. }
  896. //-- helper functions:
  897. void Decimal::clip(int & newLsb, int & newMsb) const
  898. {
  899. int lo = lsb;
  900. int hi = msb;
  901. while (digits[lo] == 0 && lo < hi)
  902. lo++;
  903. while (digits[hi] == 0 && hi >= lo)
  904. hi--;
  905. newLsb = lo;
  906. newMsb = hi;
  907. }
  908. void Decimal::clip(int & newLsb, int & newMsb, unsigned minLsb, unsigned maxMsb) const
  909. {
  910. clip(newLsb, newMsb);
  911. if (newLsb < (int)minLsb)
  912. newLsb = minLsb;
  913. if (newMsb > (int)maxMsb)
  914. newMsb = maxMsb;
  915. if (newMsb < newLsb)
  916. newMsb = newLsb-1;
  917. }
  918. unsigned Decimal::doGetString(char * buffer) const
  919. {
  920. char * cur = buffer;
  921. int lo, hi;
  922. clip(lo, hi);
  923. if (lo > hi) // || (lo == hi) && (digits[hi] == 0))
  924. {
  925. *cur = '0';
  926. return 1;
  927. }
  928. if (negative)
  929. *cur++ = '-';
  930. int idx;
  931. if (hi < zeroDigit)
  932. {
  933. *cur++ = '0';
  934. *cur++ = '.';
  935. for (idx = zeroDigit-1; idx > hi; idx--)
  936. *cur++ = '0';
  937. for (;idx >= lo; idx--)
  938. *cur++ = '0' + digits[idx];
  939. }
  940. else
  941. {
  942. if (lo < zeroDigit)
  943. {
  944. for (idx = hi; idx >= zeroDigit; idx--)
  945. *cur++ = '0' + digits[idx];
  946. *cur++ = '.';
  947. for (; idx >= lo; idx--)
  948. *cur++ = '0' + digits[idx];
  949. }
  950. else
  951. {
  952. for (idx = hi; idx >= lo; idx--)
  953. *cur++ = '0' + digits[idx];
  954. for (; idx >= zeroDigit; idx--)
  955. *cur++ = '0';
  956. }
  957. }
  958. return cur-buffer;
  959. }
  960. void Decimal::doGetDecimal(byte length, byte maxDigits, byte precision, void * buffer) const
  961. {
  962. int tgtHighPos = zeroDigit+maxDigits-precision-1;
  963. int tgtLowPos = tgtHighPos - length*2 + 1;
  964. int lowPos, highPos;
  965. clip(lowPos, highPos, zeroDigit-precision, zeroDigit+maxDigits-precision-1);
  966. if ((lowPos > highPos) || (highPos > tgtHighPos))
  967. {
  968. //zero or overflow...
  969. memset(buffer, 0, length);
  970. return;
  971. }
  972. int copyHighPos = highPos;
  973. if (tgtLowPos > copyHighPos)
  974. copyHighPos = tgtLowPos;
  975. else if (tgtHighPos < copyHighPos)
  976. copyHighPos = tgtHighPos; // overflow....
  977. int copyLowPos = lowPos;
  978. if (tgtLowPos > copyLowPos)
  979. copyLowPos = tgtLowPos;
  980. NOMEMCPY byte * tgt = (byte *)buffer;
  981. //First pad with zeros.
  982. if (tgtHighPos > copyHighPos)
  983. {
  984. unsigned zeros = tgtHighPos-copyHighPos;
  985. while (zeros >= 2)
  986. {
  987. *tgt++ = 0;
  988. zeros -= 2;
  989. }
  990. if (zeros != 0)
  991. *tgt++ = digits[copyHighPos--];
  992. }
  993. //Now will with characters
  994. while (copyLowPos < copyHighPos)
  995. {
  996. byte next = digits[copyHighPos];
  997. byte next2 = digits[copyHighPos-1];
  998. *tgt++ = (byte)(next << 4) | next2;
  999. copyHighPos -= 2;
  1000. }
  1001. if (copyLowPos == copyHighPos)
  1002. {
  1003. *tgt++ = digits[copyLowPos] << 4;
  1004. copyLowPos -= 2;
  1005. }
  1006. while (copyLowPos > tgtLowPos)
  1007. {
  1008. *tgt++ = 0;
  1009. copyLowPos -= 2;
  1010. }
  1011. }
  1012. void Decimal::doPower(unsigned value)
  1013. {
  1014. if (value == 1)
  1015. return;
  1016. if (value & 1)
  1017. {
  1018. Decimal saved(*this);
  1019. doPower(value >> 1);
  1020. multiply(*this);
  1021. multiply(saved);
  1022. }
  1023. else
  1024. {
  1025. doPower(value >> 1);
  1026. multiply(*this);
  1027. }
  1028. }
  1029. void Decimal::setZero()
  1030. {
  1031. negative = false;
  1032. lsb = msb = zeroDigit;
  1033. digits[zeroDigit] = 0;
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. bool dec2Bool(size32_t bytes, const void * _data)
  1037. {
  1038. const byte * data = (const byte *)_data;
  1039. //ignore the sign
  1040. if (data[--bytes] & 0xf0)
  1041. return true;
  1042. while (bytes--)
  1043. if (*data++)
  1044. return true;
  1045. return false;
  1046. }
  1047. bool udec2Bool(size32_t bytes, const void * _data)
  1048. {
  1049. const byte * data = (const byte *)_data;
  1050. while (bytes--)
  1051. if (*data++)
  1052. return true;
  1053. return false;
  1054. }
  1055. int decCompareDecimal(size32_t bytes, const void * _left, const void * _right)
  1056. {
  1057. const byte * left = (const byte *)_left;
  1058. const byte * right = (const byte *)_right;
  1059. bytes--;
  1060. byte signLeft = left[bytes] & 0x0f;
  1061. byte signRight = right[bytes] & 0x0f;
  1062. if (signMap[signLeft] != signMap[signRight])
  1063. {
  1064. int res = signMap[signLeft] - signMap[signRight];
  1065. // could be +0 and -0......
  1066. if ((left[bytes] & 0xf0) || (right[bytes] & 0xf0))
  1067. return res;
  1068. while (bytes--)
  1069. if (left[bytes] || right[bytes])
  1070. return res;
  1071. return 0;
  1072. }
  1073. bool numbersAreNegative = (signMap[signLeft] == -1);
  1074. while (bytes--)
  1075. {
  1076. byte l = *left++;
  1077. byte r = *right++;
  1078. int res = l - r;
  1079. if (res)
  1080. return numbersAreNegative ? -res : res;
  1081. }
  1082. int res = (*left & 0xf0) - (*right & 0xf0);
  1083. if (res)
  1084. return numbersAreNegative ? -res : res;
  1085. return 0;
  1086. }
  1087. int decCompareUDecimal(size32_t bytes, const void * _left, const void * _right)
  1088. {
  1089. const byte * left = (const byte *)_left;
  1090. const byte * right = (const byte *)_right;
  1091. while (bytes--)
  1092. {
  1093. byte l = *left++;
  1094. byte r = *right++;
  1095. int res = l - r;
  1096. if (res)
  1097. return res;
  1098. }
  1099. return 0;
  1100. }
  1101. bool decValid(bool isSigned, unsigned digits, const void * data)
  1102. {
  1103. if (data && digits)
  1104. {
  1105. if ((isSigned && digits % 2 == 0) || (!isSigned && digits % 2 != 0))
  1106. {
  1107. if (*((byte*)data)&0xf0)
  1108. return false;
  1109. digits++;
  1110. }
  1111. unsigned bytes = isSigned ? digits/2+1 : (digits+1)/2;
  1112. byte *dp = (byte * )data;
  1113. if (isSigned)
  1114. {
  1115. byte sign = dp[--bytes] & 0x0f;
  1116. // allow 0x0F and 0x0C for positive and 0x0D for negative signs
  1117. if (!(sign == 0x0f || sign == 0x0d || sign == 0x0c))
  1118. return false;
  1119. if ((byte)(dp[bytes] & 0xf0u) > 0x90u)
  1120. return false;
  1121. }
  1122. // This code assumes 32bit registers.
  1123. unsigned dwords = bytes / 4;
  1124. bytes %= 4;
  1125. while(bytes--)
  1126. {
  1127. byte b = dp[dwords*4 + bytes];
  1128. if (((b&0xF0u) > 0x90u) || ((b&0x0Fu) > 0x09u))
  1129. return false;
  1130. }
  1131. // see http://www.cs.uiowa.edu/~jones/bcd/bcd.html for an explanation of this code
  1132. unsigned __int32 *wp = (unsigned __int32 *)data;
  1133. while (dwords--)
  1134. {
  1135. unsigned __int32 l = wp[dwords];
  1136. if ((unsigned __int32 )(l&0xF0000000) > 0x90000000u)
  1137. return false;
  1138. __int32 l1 = l + 0x66666666;
  1139. __int32 l2 = l1 ^ l;
  1140. __int32 l3 = l2 & 0x11111110;
  1141. if (l3)
  1142. return false;
  1143. }
  1144. }
  1145. return true;
  1146. }