nbcd.cpp 29 KB

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