Debian Codegraph @

ext/misc/decimal.c

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