nbcd.cpp 30 KB

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