nbcd.cpp 29 KB

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