decimal.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. ** 2020-06-22
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. ******************************************************************************
  12. **
  13. ** Routines to implement arbitrary-precision decimal math.
  14. **
  15. ** The focus here is on simplicity and correctness, not performance.
  16. */
  17. #include "sqlite3ext.h"
  18. SQLITE_EXTENSION_INIT1
  19. #include <assert.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. /* Mark a function parameter as unused, to suppress nuisance compiler
  24. ** warnings. */
  25. #ifndef UNUSED_PARAMETER
  26. # define UNUSED_PARAMETER(X) (void)(X)
  27. #endif
  28. /* A decimal object */
  29. typedef struct Decimal Decimal;
  30. struct Decimal {
  31. char sign; /* 0 for positive, 1 for negative */
  32. char oom; /* True if an OOM is encountered */
  33. char isNull; /* True if holds a NULL rather than a number */
  34. char isInit; /* True upon initialization */
  35. int nDigit; /* Total number of digits */
  36. int nFrac; /* Number of digits to the right of the decimal point */
  37. signed char *a; /* Array of digits. Most significant first. */
  38. };
  39. /*
  40. ** Release memory held by a Decimal, but do not free the object itself.
  41. */
  42. static void decimal_clear(Decimal *p){
  43. sqlite3_free(p->a);
  44. }
  45. /*
  46. ** Destroy a Decimal object
  47. */
  48. static void decimal_free(Decimal *p){
  49. if( p ){
  50. decimal_clear(p);
  51. sqlite3_free(p);
  52. }
  53. }
  54. /*
  55. ** Allocate a new Decimal object initialized to the text in zIn[].
  56. ** Return NULL if any kind of error occurs.
  57. */
  58. static Decimal *decimalNewFromText(const char *zIn, int n){
  59. Decimal *p = 0;
  60. int i;
  61. int iExp = 0;
  62. p = sqlite3_malloc( sizeof(*p) );
  63. if( p==0 ) goto new_from_text_failed;
  64. p->sign = 0;
  65. p->oom = 0;
  66. p->isInit = 1;
  67. p->isNull = 0;
  68. p->nDigit = 0;
  69. p->nFrac = 0;
  70. p->a = sqlite3_malloc64( n+1 );
  71. if( p->a==0 ) goto new_from_text_failed;
  72. for(i=0; isspace(zIn[i]); i++){}
  73. if( zIn[i]=='-' ){
  74. p->sign = 1;
  75. i++;
  76. }else if( zIn[i]=='+' ){
  77. i++;
  78. }
  79. while( i<n && zIn[i]=='0' ) i++;
  80. while( i<n ){
  81. char c = zIn[i];
  82. if( c>='0' && c<='9' ){
  83. p->a[p->nDigit++] = c - '0';
  84. }else if( c=='.' ){
  85. p->nFrac = p->nDigit + 1;
  86. }else if( c=='e' || c=='E' ){
  87. int j = i+1;
  88. int neg = 0;
  89. if( j>=n ) break;
  90. if( zIn[j]=='-' ){
  91. neg = 1;
  92. j++;
  93. }else if( zIn[j]=='+' ){
  94. j++;
  95. }
  96. while( j<n && iExp<1000000 ){
  97. if( zIn[j]>='0' && zIn[j]<='9' ){
  98. iExp = iExp*10 + zIn[j] - '0';
  99. }
  100. j++;
  101. }
  102. if( neg ) iExp = -iExp;
  103. break;
  104. }
  105. i++;
  106. }
  107. if( p->nFrac ){
  108. p->nFrac = p->nDigit - (p->nFrac - 1);
  109. }
  110. if( iExp>0 ){
  111. if( p->nFrac>0 ){
  112. if( iExp<=p->nFrac ){
  113. p->nFrac -= iExp;
  114. iExp = 0;
  115. }else{
  116. iExp -= p->nFrac;
  117. p->nFrac = 0;
  118. }
  119. }
  120. if( iExp>0 ){
  121. p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
  122. if( p->a==0 ) goto new_from_text_failed;
  123. memset(p->a+p->nDigit, 0, iExp);
  124. p->nDigit += iExp;
  125. }
  126. }else if( iExp<0 ){
  127. int nExtra;
  128. iExp = -iExp;
  129. nExtra = p->nDigit - p->nFrac - 1;
  130. if( nExtra ){
  131. if( nExtra>=iExp ){
  132. p->nFrac += iExp;
  133. iExp = 0;
  134. }else{
  135. iExp -= nExtra;
  136. p->nFrac = p->nDigit - 1;
  137. }
  138. }
  139. if( iExp>0 ){
  140. p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
  141. if( p->a==0 ) goto new_from_text_failed;
  142. memmove(p->a+iExp, p->a, p->nDigit);
  143. memset(p->a, 0, iExp);
  144. p->nDigit += iExp;
  145. p->nFrac += iExp;
  146. }
  147. }
  148. return p;
  149. new_from_text_failed:
  150. if( p ){
  151. if( p->a ) sqlite3_free(p->a);
  152. sqlite3_free(p);
  153. }
  154. return 0;
  155. }
  156. /* Forward reference */
  157. static Decimal *decimalFromDouble(double);
  158. /*
  159. ** Allocate a new Decimal object from an sqlite3_value. Return a pointer
  160. ** to the new object, or NULL if there is an error. If the pCtx argument
  161. ** is not NULL, then errors are reported on it as well.
  162. **
  163. ** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted
  164. ** directly into a Decimal. For SQLITE_FLOAT or for SQLITE_BLOB of length
  165. ** 8 bytes, the resulting double value is expanded into its decimal equivalent.
  166. ** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length,
  167. ** then NULL is returned.
  168. */
  169. static Decimal *decimal_new(
  170. sqlite3_context *pCtx, /* Report error here, if not null */
  171. sqlite3_value *pIn, /* Construct the decimal object from this */
  172. int bTextOnly /* Always interpret pIn as text if true */
  173. ){
  174. Decimal *p = 0;
  175. int eType = sqlite3_value_type(pIn);
  176. if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){
  177. eType = SQLITE_TEXT;
  178. }
  179. switch( eType ){
  180. case SQLITE_TEXT:
  181. case SQLITE_INTEGER: {
  182. const char *zIn = (const char*)sqlite3_value_text(pIn);
  183. int n = sqlite3_value_bytes(pIn);
  184. p = decimalNewFromText(zIn, n);
  185. if( p==0 ) goto new_failed;
  186. break;
  187. }
  188. case SQLITE_FLOAT: {
  189. p = decimalFromDouble(sqlite3_value_double(pIn));
  190. break;
  191. }
  192. case SQLITE_BLOB: {
  193. const unsigned char *x;
  194. unsigned int i;
  195. sqlite3_uint64 v = 0;
  196. double r;
  197. if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break;
  198. x = sqlite3_value_blob(pIn);
  199. for(i=0; i<sizeof(r); i++){
  200. v = (v<<8) | x[i];
  201. }
  202. memcpy(&r, &v, sizeof(r));
  203. p = decimalFromDouble(r);
  204. break;
  205. }
  206. case SQLITE_NULL: {
  207. break;
  208. }
  209. }
  210. return p;
  211. new_failed:
  212. if( pCtx ) sqlite3_result_error_nomem(pCtx);
  213. sqlite3_free(p);
  214. return 0;
  215. }
  216. /*
  217. ** Make the given Decimal the result.
  218. */
  219. static void decimal_result(sqlite3_context *pCtx, Decimal *p){
  220. char *z;
  221. int i, j;
  222. int n;
  223. if( p==0 || p->oom ){
  224. sqlite3_result_error_nomem(pCtx);
  225. return;
  226. }
  227. if( p->isNull ){
  228. sqlite3_result_null(pCtx);
  229. return;
  230. }
  231. z = sqlite3_malloc( p->nDigit+4 );
  232. if( z==0 ){
  233. sqlite3_result_error_nomem(pCtx);
  234. return;
  235. }
  236. i = 0;
  237. if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
  238. p->sign = 0;
  239. }
  240. if( p->sign ){
  241. z[0] = '-';
  242. i = 1;
  243. }
  244. n = p->nDigit - p->nFrac;
  245. if( n<=0 ){
  246. z[i++] = '0';
  247. }
  248. j = 0;
  249. while( n>1 && p->a[j]==0 ){
  250. j++;
  251. n--;
  252. }
  253. while( n>0 ){
  254. z[i++] = p->a[j] + '0';
  255. j++;
  256. n--;
  257. }
  258. if( p->nFrac ){
  259. z[i++] = '.';
  260. do{
  261. z[i++] = p->a[j] + '0';
  262. j++;
  263. }while( j<p->nDigit );
  264. }
  265. z[i] = 0;
  266. sqlite3_result_text(pCtx, z, i, sqlite3_free);
  267. }
  268. /*
  269. ** Make the given Decimal the result in an format similar to '%+#e'.
  270. ** In other words, show exponential notation with leading and trailing
  271. ** zeros omitted.
  272. */
  273. static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){
  274. char *z; /* The output buffer */
  275. int i; /* Loop counter */
  276. int nZero; /* Number of leading zeros */
  277. int nDigit; /* Number of digits not counting trailing zeros */
  278. int nFrac; /* Digits to the right of the decimal point */
  279. int exp; /* Exponent value */
  280. signed char zero; /* Zero value */
  281. signed char *a; /* Array of digits */
  282. if( p==0 || p->oom ){
  283. sqlite3_result_error_nomem(pCtx);
  284. return;
  285. }
  286. if( p->isNull ){
  287. sqlite3_result_null(pCtx);
  288. return;
  289. }
  290. for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){}
  291. for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){}
  292. nFrac = p->nFrac + (nDigit - p->nDigit);
  293. nDigit -= nZero;
  294. z = sqlite3_malloc( nDigit+20 );
  295. if( z==0 ){
  296. sqlite3_result_error_nomem(pCtx);
  297. return;
  298. }
  299. if( nDigit==0 ){
  300. zero = 0;
  301. a = &zero;
  302. nDigit = 1;
  303. nFrac = 0;
  304. }else{
  305. a = &p->a[nZero];
  306. }
  307. if( p->sign && nDigit>0 ){
  308. z[0] = '-';
  309. }else{
  310. z[0] = '+';
  311. }
  312. z[1] = a[0]+'0';
  313. z[2] = '.';
  314. if( nDigit==1 ){
  315. z[3] = '0';
  316. i = 4;
  317. }else{
  318. for(i=1; i<nDigit; i++){
  319. z[2+i] = a[i]+'0';
  320. }
  321. i = nDigit+2;
  322. }
  323. exp = nDigit - nFrac - 1;
  324. sqlite3_snprintf(nDigit+20-i, &z[i], "e%+03d", exp);
  325. sqlite3_result_text(pCtx, z, -1, sqlite3_free);
  326. }
  327. /*
  328. ** Compare to Decimal objects. Return negative, 0, or positive if the
  329. ** first object is less than, equal to, or greater than the second.
  330. **
  331. ** Preconditions for this routine:
  332. **
  333. ** pA!=0
  334. ** pA->isNull==0
  335. ** pB!=0
  336. ** pB->isNull==0
  337. */
  338. static int decimal_cmp(const Decimal *pA, const Decimal *pB){
  339. int nASig, nBSig, rc, n;
  340. if( pA->sign!=pB->sign ){
  341. return pA->sign ? -1 : +1;
  342. }
  343. if( pA->sign ){
  344. const Decimal *pTemp = pA;
  345. pA = pB;
  346. pB = pTemp;
  347. }
  348. nASig = pA->nDigit - pA->nFrac;
  349. nBSig = pB->nDigit - pB->nFrac;
  350. if( nASig!=nBSig ){
  351. return nASig - nBSig;
  352. }
  353. n = pA->nDigit;
  354. if( n>pB->nDigit ) n = pB->nDigit;
  355. rc = memcmp(pA->a, pB->a, n);
  356. if( rc==0 ){
  357. rc = pA->nDigit - pB->nDigit;
  358. }
  359. return rc;
  360. }
  361. /*
  362. ** SQL Function: decimal_cmp(X, Y)
  363. **
  364. ** Return negative, zero, or positive if X is less then, equal to, or
  365. ** greater than Y.
  366. */
  367. static void decimalCmpFunc(
  368. sqlite3_context *context,
  369. int argc,
  370. sqlite3_value **argv
  371. ){
  372. Decimal *pA = 0, *pB = 0;
  373. int rc;
  374. UNUSED_PARAMETER(argc);
  375. pA = decimal_new(context, argv[0], 1);
  376. if( pA==0 || pA->isNull ) goto cmp_done;
  377. pB = decimal_new(context, argv[1], 1);
  378. if( pB==0 || pB->isNull ) goto cmp_done;
  379. rc = decimal_cmp(pA, pB);
  380. if( rc<0 ) rc = -1;
  381. else if( rc>0 ) rc = +1;
  382. sqlite3_result_int(context, rc);
  383. cmp_done:
  384. decimal_free(pA);
  385. decimal_free(pB);
  386. }
  387. /*
  388. ** Expand the Decimal so that it has a least nDigit digits and nFrac
  389. ** digits to the right of the decimal point.
  390. */
  391. static void decimal_expand(Decimal *p, int nDigit, int nFrac){
  392. int nAddSig;
  393. int nAddFrac;
  394. if( p==0 ) return;
  395. nAddFrac = nFrac - p->nFrac;
  396. nAddSig = (nDigit - p->nDigit) - nAddFrac;
  397. if( nAddFrac==0 && nAddSig==0 ) return;
  398. p->a = sqlite3_realloc64(p->a, nDigit+1);
  399. if( p->a==0 ){
  400. p->oom = 1;
  401. return;
  402. }
  403. if( nAddSig ){
  404. memmove(p->a+nAddSig, p->a, p->nDigit);
  405. memset(p->a, 0, nAddSig);
  406. p->nDigit += nAddSig;
  407. }
  408. if( nAddFrac ){
  409. memset(p->a+p->nDigit, 0, nAddFrac);
  410. p->nDigit += nAddFrac;
  411. p->nFrac += nAddFrac;
  412. }
  413. }
  414. /*
  415. ** Add the value pB into pA. A := A + B.
  416. **
  417. ** Both pA and pB might become denormalized by this routine.
  418. */
  419. static void decimal_add(Decimal *pA, Decimal *pB){
  420. int nSig, nFrac, nDigit;
  421. int i, rc;
  422. if( pA==0 ){
  423. return;
  424. }
  425. if( pA->oom || pB==0 || pB->oom ){
  426. pA->oom = 1;
  427. return;
  428. }
  429. if( pA->isNull || pB->isNull ){
  430. pA->isNull = 1;
  431. return;
  432. }
  433. nSig = pA->nDigit - pA->nFrac;
  434. if( nSig && pA->a[0]==0 ) nSig--;
  435. if( nSig<pB->nDigit-pB->nFrac ){
  436. nSig = pB->nDigit - pB->nFrac;
  437. }
  438. nFrac = pA->nFrac;
  439. if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
  440. nDigit = nSig + nFrac + 1;
  441. decimal_expand(pA, nDigit, nFrac);
  442. decimal_expand(pB, nDigit, nFrac);
  443. if( pA->oom || pB->oom ){
  444. pA->oom = 1;
  445. }else{
  446. if( pA->sign==pB->sign ){
  447. int carry = 0;
  448. for(i=nDigit-1; i>=0; i--){
  449. int x = pA->a[i] + pB->a[i] + carry;
  450. if( x>=10 ){
  451. carry = 1;
  452. pA->a[i] = x - 10;
  453. }else{
  454. carry = 0;
  455. pA->a[i] = x;
  456. }
  457. }
  458. }else{
  459. signed char *aA, *aB;
  460. int borrow = 0;
  461. rc = memcmp(pA->a, pB->a, nDigit);
  462. if( rc<0 ){
  463. aA = pB->a;
  464. aB = pA->a;
  465. pA->sign = !pA->sign;
  466. }else{
  467. aA = pA->a;
  468. aB = pB->a;
  469. }
  470. for(i=nDigit-1; i>=0; i--){
  471. int x = aA[i] - aB[i] - borrow;
  472. if( x<0 ){
  473. pA->a[i] = x+10;
  474. borrow = 1;
  475. }else{
  476. pA->a[i] = x;
  477. borrow = 0;
  478. }
  479. }
  480. }
  481. }
  482. }
  483. /*
  484. ** Multiply A by B. A := A * B
  485. **
  486. ** All significant digits after the decimal point are retained.
  487. ** Trailing zeros after the decimal point are omitted as long as
  488. ** the number of digits after the decimal point is no less than
  489. ** either the number of digits in either input.
  490. */
  491. static void decimalMul(Decimal *pA, Decimal *pB){
  492. signed char *acc = 0;
  493. int i, j, k;
  494. int minFrac;
  495. if( pA==0 || pA->oom || pA->isNull
  496. || pB==0 || pB->oom || pB->isNull
  497. ){
  498. goto mul_end;
  499. }
  500. acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
  501. if( acc==0 ){
  502. pA->oom = 1;
  503. goto mul_end;
  504. }
  505. memset(acc, 0, pA->nDigit + pB->nDigit + 2);
  506. minFrac = pA->nFrac;
  507. if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
  508. for(i=pA->nDigit-1; i>=0; i--){
  509. signed char f = pA->a[i];
  510. int carry = 0, x;
  511. for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
  512. x = acc[k] + f*pB->a[j] + carry;
  513. acc[k] = x%10;
  514. carry = x/10;
  515. }
  516. x = acc[k] + carry;
  517. acc[k] = x%10;
  518. acc[k-1] += x/10;
  519. }
  520. sqlite3_free(pA->a);
  521. pA->a = acc;
  522. acc = 0;
  523. pA->nDigit += pB->nDigit + 2;
  524. pA->nFrac += pB->nFrac;
  525. pA->sign ^= pB->sign;
  526. while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
  527. pA->nFrac--;
  528. pA->nDigit--;
  529. }
  530. mul_end:
  531. sqlite3_free(acc);
  532. }
  533. /*
  534. ** Create a new Decimal object that contains an integer power of 2.
  535. */
  536. static Decimal *decimalPow2(int N){
  537. Decimal *pA = 0; /* The result to be returned */
  538. Decimal *pX = 0; /* Multiplier */
  539. if( N<-20000 || N>20000 ) goto pow2_fault;
  540. pA = decimalNewFromText("1.0", 3);
  541. if( pA==0 || pA->oom ) goto pow2_fault;
  542. if( N==0 ) return pA;
  543. if( N>0 ){
  544. pX = decimalNewFromText("2.0", 3);
  545. }else{
  546. N = -N;
  547. pX = decimalNewFromText("0.5", 3);
  548. }
  549. if( pX==0 || pX->oom ) goto pow2_fault;
  550. while( 1 /* Exit by break */ ){
  551. if( N & 1 ){
  552. decimalMul(pA, pX);
  553. if( pA->oom ) goto pow2_fault;
  554. }
  555. N >>= 1;
  556. if( N==0 ) break;
  557. decimalMul(pX, pX);
  558. }
  559. decimal_free(pX);
  560. return pA;
  561. pow2_fault:
  562. decimal_free(pA);
  563. decimal_free(pX);
  564. return 0;
  565. }
  566. /*
  567. ** Use an IEEE754 binary64 ("double") to generate a new Decimal object.
  568. */
  569. static Decimal *decimalFromDouble(double r){
  570. sqlite3_int64 m, a;
  571. int e;
  572. int isNeg;
  573. Decimal *pA;
  574. Decimal *pX;
  575. char zNum[100];
  576. if( r<0.0 ){
  577. isNeg = 1;
  578. r = -r;
  579. }else{
  580. isNeg = 0;
  581. }
  582. memcpy(&a,&r,sizeof(a));
  583. if( a==0 ){
  584. e = 0;
  585. m = 0;
  586. }else{
  587. e = a>>52;
  588. m = a & ((((sqlite3_int64)1)<<52)-1);
  589. if( e==0 ){
  590. m <<= 1;
  591. }else{
  592. m |= ((sqlite3_int64)1)<<52;
  593. }
  594. while( e<1075 && m>0 && (m&1)==0 ){
  595. m >>= 1;
  596. e++;
  597. }
  598. if( isNeg ) m = -m;
  599. e = e - 1075;
  600. if( e>971 ){
  601. return 0; /* A NaN or an Infinity */
  602. }
  603. }
  604. /* At this point m is the integer significand and e is the exponent */
  605. sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m);
  606. pA = decimalNewFromText(zNum, (int)strlen(zNum));
  607. pX = decimalPow2(e);
  608. decimalMul(pA, pX);
  609. decimal_free(pX);
  610. return pA;
  611. }
  612. /*
  613. ** SQL Function: decimal(X)
  614. ** OR: decimal_exp(X)
  615. **
  616. ** Convert input X into decimal and then back into text.
  617. **
  618. ** If X is originally a float, then a full decimal expansion of that floating
  619. ** point value is done. Or if X is an 8-byte blob, it is interpreted
  620. ** as a float and similarly expanded.
  621. **
  622. ** The decimal_exp(X) function returns the result in exponential notation.
  623. ** decimal(X) returns a complete decimal, without the e+NNN at the end.
  624. */
  625. static void decimalFunc(
  626. sqlite3_context *context,
  627. int argc,
  628. sqlite3_value **argv
  629. ){
  630. Decimal *p = decimal_new(context, argv[0], 0);
  631. UNUSED_PARAMETER(argc);
  632. if( p ){
  633. if( sqlite3_user_data(context)!=0 ){
  634. decimal_result_sci(context, p);
  635. }else{
  636. decimal_result(context, p);
  637. }
  638. decimal_free(p);
  639. }
  640. }
  641. /*
  642. ** Compare text in decimal order.
  643. */
  644. static int decimalCollFunc(
  645. void *notUsed,
  646. int nKey1, const void *pKey1,
  647. int nKey2, const void *pKey2
  648. ){
  649. const unsigned char *zA = (const unsigned char*)pKey1;
  650. const unsigned char *zB = (const unsigned char*)pKey2;
  651. Decimal *pA = decimalNewFromText((const char*)zA, nKey1);
  652. Decimal *pB = decimalNewFromText((const char*)zB, nKey2);
  653. int rc;
  654. UNUSED_PARAMETER(notUsed);
  655. if( pA==0 || pB==0 ){
  656. rc = 0;
  657. }else{
  658. rc = decimal_cmp(pA, pB);
  659. }
  660. decimal_free(pA);
  661. decimal_free(pB);
  662. return rc;
  663. }
  664. /*
  665. ** SQL Function: decimal_add(X, Y)
  666. ** decimal_sub(X, Y)
  667. **
  668. ** Return the sum or difference of X and Y.
  669. */
  670. static void decimalAddFunc(
  671. sqlite3_context *context,
  672. int argc,
  673. sqlite3_value **argv
  674. ){
  675. Decimal *pA = decimal_new(context, argv[0], 1);
  676. Decimal *pB = decimal_new(context, argv[1], 1);
  677. UNUSED_PARAMETER(argc);
  678. decimal_add(pA, pB);
  679. decimal_result(context, pA);
  680. decimal_free(pA);
  681. decimal_free(pB);
  682. }
  683. static void decimalSubFunc(
  684. sqlite3_context *context,
  685. int argc,
  686. sqlite3_value **argv
  687. ){
  688. Decimal *pA = decimal_new(context, argv[0], 1);
  689. Decimal *pB = decimal_new(context, argv[1], 1);
  690. UNUSED_PARAMETER(argc);
  691. if( pB ){
  692. pB->sign = !pB->sign;
  693. decimal_add(pA, pB);
  694. decimal_result(context, pA);
  695. }
  696. decimal_free(pA);
  697. decimal_free(pB);
  698. }
  699. /* Aggregate funcion: decimal_sum(X)
  700. **
  701. ** Works like sum() except that it uses decimal arithmetic for unlimited
  702. ** precision.
  703. */
  704. static void decimalSumStep(
  705. sqlite3_context *context,
  706. int argc,
  707. sqlite3_value **argv
  708. ){
  709. Decimal *p;
  710. Decimal *pArg;
  711. UNUSED_PARAMETER(argc);
  712. p = sqlite3_aggregate_context(context, sizeof(*p));
  713. if( p==0 ) return;
  714. if( !p->isInit ){
  715. p->isInit = 1;
  716. p->a = sqlite3_malloc(2);
  717. if( p->a==0 ){
  718. p->oom = 1;
  719. }else{
  720. p->a[0] = 0;
  721. }
  722. p->nDigit = 1;
  723. p->nFrac = 0;
  724. }
  725. if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  726. pArg = decimal_new(context, argv[0], 1);
  727. decimal_add(p, pArg);
  728. decimal_free(pArg);
  729. }
  730. static void decimalSumInverse(
  731. sqlite3_context *context,
  732. int argc,
  733. sqlite3_value **argv
  734. ){
  735. Decimal *p;
  736. Decimal *pArg;
  737. UNUSED_PARAMETER(argc);
  738. p = sqlite3_aggregate_context(context, sizeof(*p));
  739. if( p==0 ) return;
  740. if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  741. pArg = decimal_new(context, argv[0], 1);
  742. if( pArg ) pArg->sign = !pArg->sign;
  743. decimal_add(p, pArg);
  744. decimal_free(pArg);
  745. }
  746. static void decimalSumValue(sqlite3_context *context){
  747. Decimal *p = sqlite3_aggregate_context(context, 0);
  748. if( p==0 ) return;
  749. decimal_result(context, p);
  750. }
  751. static void decimalSumFinalize(sqlite3_context *context){
  752. Decimal *p = sqlite3_aggregate_context(context, 0);
  753. if( p==0 ) return;
  754. decimal_result(context, p);
  755. decimal_clear(p);
  756. }
  757. /*
  758. ** SQL Function: decimal_mul(X, Y)
  759. **
  760. ** Return the product of X and Y.
  761. */
  762. static void decimalMulFunc(
  763. sqlite3_context *context,
  764. int argc,
  765. sqlite3_value **argv
  766. ){
  767. Decimal *pA = decimal_new(context, argv[0], 1);
  768. Decimal *pB = decimal_new(context, argv[1], 1);
  769. UNUSED_PARAMETER(argc);
  770. if( pA==0 || pA->oom || pA->isNull
  771. || pB==0 || pB->oom || pB->isNull
  772. ){
  773. goto mul_end;
  774. }
  775. decimalMul(pA, pB);
  776. if( pA->oom ){
  777. goto mul_end;
  778. }
  779. decimal_result(context, pA);
  780. mul_end:
  781. decimal_free(pA);
  782. decimal_free(pB);
  783. }
  784. /*
  785. ** SQL Function: decimal_pow2(N)
  786. **
  787. ** Return the N-th power of 2. N must be an integer.
  788. */
  789. static void decimalPow2Func(
  790. sqlite3_context *context,
  791. int argc,
  792. sqlite3_value **argv
  793. ){
  794. UNUSED_PARAMETER(argc);
  795. if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
  796. Decimal *pA = decimalPow2(sqlite3_value_int(argv[0]));
  797. decimal_result_sci(context, pA);
  798. decimal_free(pA);
  799. }
  800. }
  801. #ifdef _WIN32
  802. __declspec(dllexport)
  803. #endif
  804. int sqlite3_decimal_init(
  805. sqlite3 *db,
  806. char **pzErrMsg,
  807. const sqlite3_api_routines *pApi
  808. ){
  809. int rc = SQLITE_OK;
  810. static const struct {
  811. const char *zFuncName;
  812. int nArg;
  813. int iArg;
  814. void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  815. } aFunc[] = {
  816. { "decimal", 1, 0, decimalFunc },
  817. { "decimal_exp", 1, 1, decimalFunc },
  818. { "decimal_cmp", 2, 0, decimalCmpFunc },
  819. { "decimal_add", 2, 0, decimalAddFunc },
  820. { "decimal_sub", 2, 0, decimalSubFunc },
  821. { "decimal_mul", 2, 0, decimalMulFunc },
  822. { "decimal_pow2", 1, 0, decimalPow2Func },
  823. };
  824. unsigned int i;
  825. (void)pzErrMsg; /* Unused parameter */
  826. SQLITE_EXTENSION_INIT2(pApi);
  827. for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){
  828. rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
  829. SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
  830. aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0);
  831. }
  832. if( rc==SQLITE_OK ){
  833. rc = sqlite3_create_window_function(db, "decimal_sum", 1,
  834. SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
  835. decimalSumStep, decimalSumFinalize,
  836. decimalSumValue, decimalSumInverse, 0);
  837. }
  838. if( rc==SQLITE_OK ){
  839. rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
  840. 0, decimalCollFunc);
  841. }
  842. return rc;
  843. }