Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/misc/decimal.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / misc / decimal.c` 1004 lines ยท 24175 bytes Raw Blame History 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 20 #include 21 #include 22 #include 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 ** Note that zIn[] is not necessarily zero-terminated. Always 72 ** respect the boundary imposed by the n argument. 73 */ 74 static Decimal *decimalNewFromText(const char *zIn, int n){ 75 Decimal *p = 0; 76 int i; 77 int iExp = 0; 78 79 if( zIn==0 ) goto new_from_text_failed; 80 p = sqlite3_malloc64( sizeof(*p) ); 81 if( p==0 ) goto new_from_text_failed; 82 p->sign = 0; 83 p->oom = 0; 84 p->isInit = 1; 85 p->isNull = 0; 86 p->nDigit = 0; 87 p->nFrac = 0; 88 p->a = sqlite3_malloc64( n+1 ); 89 if( p->a==0 ) goto new_from_text_failed; 90 for(i=0; isign = 1; 93 i++; 94 }else if( i='0' && c<='9' ){ 101 p->a[p->nDigit++] = c - '0'; 102 }else if( c=='.' ){ 103 p->nFrac = p->nDigit + 1; 104 }else if( c=='e' || c=='E' ){ 105 int j = i+1; 106 int neg = 0; 107 if( j>=n ) break; 108 if( zIn[j]=='-' ){ 109 neg = 1; 110 j++; 111 }else if( zIn[j]=='+' ){ 112 j++; 113 } 114 while( j='0' && zIn[j]<='9' ){ 116 iExp = iExp*10 + zIn[j] - '0'; 117 } 118 j++; 119 } 120 if( neg ) iExp = -iExp; 121 break; 122 } 123 i++; 124 } 125 if( p->nFrac ){ 126 p->nFrac = p->nDigit - (p->nFrac - 1); 127 } 128 if( iExp>0 ){ 129 if( p->nFrac>0 ){ 130 if( iExp<=p->nFrac ){ 131 p->nFrac -= iExp; 132 iExp = 0; 133 }else{ 134 iExp -= p->nFrac; 135 p->nFrac = 0; 136 } 137 } 138 if( iExp>0 ){ 139 signed char *a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit 140 + (sqlite3_int64)iExp + 1 ); 141 if( a==0 ) goto new_from_text_failed; 142 p->a = a; 143 memset(p->a+p->nDigit, 0, iExp); 144 p->nDigit += iExp; 145 } 146 }else if( iExp<0 ){ 147 int nExtra; 148 iExp = -iExp; 149 nExtra = p->nDigit - p->nFrac - 1; 150 if( nExtra ){ 151 if( nExtra>=iExp ){ 152 p->nFrac += iExp; 153 iExp = 0; 154 }else{ 155 iExp -= nExtra; 156 p->nFrac = p->nDigit - 1; 157 } 158 } 159 if( iExp>0 ){ 160 signed char *a = sqlite3_realloc64(p->a, (sqlite3_int64)p->nDigit 161 + (sqlite3_int64)iExp + 1 ); 162 if( a==0 ) goto new_from_text_failed; 163 p->a = a; 164 memmove(p->a+iExp, p->a, p->nDigit); 165 memset(p->a, 0, iExp); 166 p->nDigit += iExp; 167 p->nFrac += iExp; 168 } 169 } 170 if( p->sign ){ 171 for(i=0; inDigit && p->a[i]==0; i++){} 172 if( i>=p->nDigit ) p->sign = 0; 173 } 174 if( p->nDigit>SQLITE_DECIMAL_MAX_DIGIT ) goto new_from_text_failed; 175 return p; 176 177 new_from_text_failed: 178 if( p ){ 179 if( p->a ) sqlite3_free(p->a); 180 sqlite3_free(p); 181 } 182 return 0; 183 } 184 185 /* Forward reference */ 186 static Decimal *decimalFromDouble(double); 187 188 /* 189 ** Allocate a new Decimal object from an sqlite3_value. Return a pointer 190 ** to the new object, or NULL if there is an error. If the pCtx argument 191 ** is not NULL, then errors are reported on it as well. 192 ** 193 ** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted 194 ** directly into a Decimal. For SQLITE_FLOAT or for SQLITE_BLOB of length 195 ** 8 bytes, the resulting double value is expanded into its decimal equivalent. 196 ** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length, 197 ** then NULL is returned. 198 */ 199 static Decimal *decimal_new( 200 sqlite3_context *pCtx, /* Report error here, if not null */ 201 sqlite3_value *pIn, /* Construct the decimal object from this */ 202 int bTextOnly /* Always interpret pIn as text if true */ 203 ){ 204 Decimal *p = 0; 205 int eType = sqlite3_value_type(pIn); 206 if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){ 207 eType = SQLITE_TEXT; 208 } 209 switch( eType ){ 210 case SQLITE_TEXT: 211 case SQLITE_INTEGER: { 212 const char *zIn = (const char*)sqlite3_value_text(pIn); 213 int n = sqlite3_value_bytes(pIn); 214 p = decimalNewFromText(zIn, n); 215 if( p==0 ) goto new_failed; 216 break; 217 } 218 219 case SQLITE_FLOAT: { 220 p = decimalFromDouble(sqlite3_value_double(pIn)); 221 break; 222 } 223 224 case SQLITE_BLOB: { 225 const unsigned char *x; 226 unsigned int i; 227 sqlite3_uint64 v = 0; 228 double r; 229 230 if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break; 231 x = sqlite3_value_blob(pIn); 232 for(i=0; ioom ){ 260 sqlite3_result_error_nomem(pCtx); 261 return; 262 } 263 if( p->isNull ){ 264 sqlite3_result_null(pCtx); 265 return; 266 } 267 z = sqlite3_malloc64( (sqlite3_int64)p->nDigit+8 ); 268 if( z==0 ){ 269 sqlite3_result_error_nomem(pCtx); 270 return; 271 } 272 if( p->sign ){ 273 for(i=0; inDigit && p->a[i]==0; i++){} 274 if( i==p->nDigit ) p->sign = 0; 275 } 276 i = 0; 277 if( p->sign ){ 278 z[0] = '-'; 279 i = 1; 280 } 281 n = p->nDigit - p->nFrac; 282 if( n<=0 ){ 283 z[i++] = '0'; 284 } 285 j = 0; 286 while( n>1 && p->a[j]==0 ){ 287 j++; 288 n--; 289 } 290 while( n>0 ){ 291 z[i++] = p->a[j] + '0'; 292 j++; 293 n--; 294 } 295 if( p->nFrac ){ 296 z[i++] = '.'; 297 do{ 298 z[i++] = p->a[j] + '0'; 299 j++; 300 }while( jnDigit ); 301 } 302 z[i] = 0; 303 sqlite3_result_text(pCtx, z, i, sqlite3_free); 304 } 305 306 /* Forward declaration */ 307 static void decimal_expand(Decimal *p, int nDigit, int nFrac); 308 309 /* 310 ** Round a decimal value to N significant digits. N must be positive. 311 */ 312 static void decimal_round(Decimal *p, int N){ 313 int i; 314 int nZero; /* Number of leading zeros */ 315 if( N<1 ) return; 316 if( p==0 ) return; 317 if( p->nDigit<=N ) return; 318 for(nZero=0; nZeronDigit && p->a[nZero]==0; nZero++){} 319 N += nZero; 320 if( p->nDigit<=N ) return; 321 if( p->a[N]>=5 ){ 322 /* If all leading digits are 9, increase the number of digits 323 ** by adding a new 0 to the front */ 324 for(i=0; ia[i]==9; i++){} 325 if( i==N ){ 326 decimal_expand(p, p->nDigit+1, p->nFrac); 327 if( p->oom ) return; 328 } 329 330 /* Do the rounding */ 331 p->a[N-1]++; 332 for(i=N-1; i>0 && p->a[i]>9; i--){ 333 p->a[i] = 0; 334 p->a[i-1]++; 335 } 336 assert( p->a[0]<=9 ); 337 } 338 memset(&p->a[N], 0, p->nDigit - N); 339 } 340 341 /* 342 ** Make the given Decimal the result in an format similar to '%+#e'. 343 ** In other words, show exponential notation with leading and trailing 344 ** zeros omitted. 345 */ 346 static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p, int N){ 347 char *z; /* The output buffer */ 348 int i; /* Loop counter */ 349 int nZero; /* Number of leading zeros */ 350 int nDigit; /* Number of digits not counting trailing zeros */ 351 int nFrac; /* Digits to the right of the decimal point */ 352 int exp; /* Exponent value */ 353 signed char zero; /* Zero value */ 354 signed char *a; /* Array of digits */ 355 356 if( p==0 || p->oom ){ 357 sqlite3_result_error_nomem(pCtx); 358 return; 359 } 360 if( p->isNull ){ 361 sqlite3_result_null(pCtx); 362 return; 363 } 364 if( N<1 ) N = 0; 365 for(nDigit=p->nDigit; nDigit>N && p->a[nDigit-1]==0; nDigit--){} 366 for(nZero=0; nZeroa[nZero]==0; nZero++){} 367 nFrac = p->nFrac + (nDigit - p->nDigit); 368 nDigit -= nZero; 369 z = sqlite3_malloc64( (sqlite3_int64)nDigit+20 ); 370 if( z==0 ){ 371 sqlite3_result_error_nomem(pCtx); 372 return; 373 } 374 if( nDigit==0 ){ 375 zero = 0; 376 a = &zero; 377 nDigit = 1; 378 nFrac = 0; 379 }else{ 380 a = &p->a[nZero]; 381 } 382 if( p->sign && nDigit>0 ){ 383 z[0] = '-'; 384 }else{ 385 z[0] = '+'; 386 } 387 z[1] = a[0]+'0'; 388 z[2] = '.'; 389 if( nDigit==1 ){ 390 z[3] = '0'; 391 i = 4; 392 }else{ 393 for(i=1; iisNull==0 411 ** pB!=0 412 ** pB->isNull==0 413 */ 414 static int decimal_cmp(Decimal *pA, Decimal *pB){ 415 int nASig, nBSig, rc, n, iA, iB; 416 while( pA->nFrac>0 && pA->a[pA->nDigit-1]==0 ){ 417 pA->nDigit--; 418 pA->nFrac--; 419 } 420 while( pB->nFrac>0 && pB->a[pB->nDigit-1]==0 ){ 421 pB->nDigit--; 422 pB->nFrac--; 423 } 424 if( pA->sign!=pB->sign ){ 425 return pA->sign ? -1 : +1; 426 } 427 if( pA->sign ){ 428 Decimal *pTemp = pA; 429 pA = pB; 430 pB = pTemp; 431 } 432 for(iA=0; iAnDigit-pA->nFrac && pA->a[iA]==0; iA++){} 433 for(iB=0; iBnDigit-pB->nFrac && pB->a[iB]==0; iB++){} 434 nASig = pA->nDigit - pA->nFrac - iA; 435 nBSig = pB->nDigit - pB->nFrac - iB; 436 if( nASig!=nBSig ){ 437 return nASig - nBSig; 438 } 439 n = pA->nDigit - iA; 440 if( n>pB->nDigit - iB ) n = pB->nDigit - iB; 441 rc = memcmp(pA->a+iA, pB->a+iB, n); 442 if( rc==0 ){ 443 rc = (pA->nDigit - iA) - (pB->nDigit - iB); 444 } 445 return rc; 446 } 447 448 /* 449 ** SQL Function: decimal_cmp(X, Y) 450 ** 451 ** Return negative, zero, or positive if X is less then, equal to, or 452 ** greater than Y. 453 */ 454 static void decimalCmpFunc( 455 sqlite3_context *context, 456 int argc, 457 sqlite3_value **argv 458 ){ 459 Decimal *pA = 0, *pB = 0; 460 int rc; 461 462 UNUSED_PARAMETER(argc); 463 pA = decimal_new(context, argv[0], 1); 464 if( pA==0 || pA->isNull ) goto cmp_done; 465 pB = decimal_new(context, argv[1], 1); 466 if( pB==0 || pB->isNull ) goto cmp_done; 467 rc = decimal_cmp(pA, pB); 468 if( rc<0 ) rc = -1; 469 else if( rc>0 ) rc = +1; 470 sqlite3_result_int(context, rc); 471 cmp_done: 472 decimal_free(pA); 473 decimal_free(pB); 474 } 475 476 /* 477 ** Expand the Decimal so that it has a least nDigit digits and nFrac 478 ** digits to the right of the decimal point. 479 */ 480 static void decimal_expand(Decimal *p, int nDigit, int nFrac){ 481 int nAddSig; 482 int nAddFrac; 483 signed char *a; 484 if( p==0 ) return; 485 nAddFrac = nFrac - p->nFrac; 486 assert( nAddFrac>=0 ); 487 nAddSig = (nDigit - p->nDigit) - nAddFrac; 488 if( nAddFrac==0 && nAddSig==0 ) return; 489 if( nDigit+1>SQLITE_DECIMAL_MAX_DIGIT ){ p->oom = 1; return; } 490 a = sqlite3_realloc64(p->a, nDigit+1); 491 if( a==0 ){ 492 p->oom = 1; 493 return; 494 } 495 p->a = a; 496 if( nAddSig ){ 497 memmove(p->a+nAddSig, p->a, p->nDigit); 498 memset(p->a, 0, nAddSig); 499 p->nDigit += nAddSig; 500 } 501 if( nAddFrac ){ 502 memset(p->a+p->nDigit, 0, nAddFrac); 503 p->nDigit += nAddFrac; 504 p->nFrac += nAddFrac; 505 } 506 } 507 508 /* 509 ** Add the value pB into pA. A := A + B. 510 ** 511 ** Both pA and pB might become denormalized by this routine. 512 */ 513 static void decimal_add(Decimal *pA, Decimal *pB){ 514 int nSig, nFrac, nDigit; 515 int i, rc; 516 if( pA==0 ){ 517 return; 518 } 519 if( pA->oom || pB==0 || pB->oom ){ 520 pA->oom = 1; 521 return; 522 } 523 if( pA->isNull || pB->isNull ){ 524 pA->isNull = 1; 525 return; 526 } 527 nSig = pA->nDigit - pA->nFrac; 528 if( nSig && pA->a[0]==0 ) nSig--; 529 if( nSignDigit-pB->nFrac ){ 530 nSig = pB->nDigit - pB->nFrac; 531 } 532 nFrac = pA->nFrac; 533 if( nFracnFrac ) nFrac = pB->nFrac; 534 nDigit = nSig + nFrac + 1; 535 decimal_expand(pA, nDigit, nFrac); 536 decimal_expand(pB, nDigit, nFrac); 537 if( pA->oom || pB->oom ){ 538 pA->oom = 1; 539 }else{ 540 if( pA->sign==pB->sign ){ 541 int carry = 0; 542 for(i=nDigit-1; i>=0; i--){ 543 int x = pA->a[i] + pB->a[i] + carry; 544 if( x>=10 ){ 545 carry = 1; 546 pA->a[i] = x - 10; 547 }else{ 548 carry = 0; 549 pA->a[i] = x; 550 } 551 } 552 }else{ 553 signed char *aA, *aB; 554 int borrow = 0; 555 rc = memcmp(pA->a, pB->a, nDigit); 556 if( rc<0 ){ 557 aA = pB->a; 558 aB = pA->a; 559 pA->sign = !pA->sign; 560 }else{ 561 aA = pA->a; 562 aB = pB->a; 563 } 564 for(i=nDigit-1; i>=0; i--){ 565 int x = aA[i] - aB[i] - borrow; 566 if( x<0 ){ 567 pA->a[i] = x+10; 568 borrow = 1; 569 }else{ 570 pA->a[i] = x; 571 borrow = 0; 572 } 573 } 574 } 575 } 576 } 577 578 /* 579 ** Multiply A by B. A := A * B 580 ** 581 ** All significant digits after the decimal point are retained. 582 ** Trailing zeros after the decimal point are omitted as long as 583 ** the number of digits after the decimal point is no less than 584 ** either the number of digits in either input. 585 */ 586 static void decimalMul(Decimal *pA, Decimal *pB){ 587 signed char *acc = 0; 588 int i, j, k; 589 int minFrac; 590 sqlite3_int64 sumDigit; 591 592 if( pA==0 || pA->oom || pA->isNull 593 || pB==0 || pB->oom || pB->isNull 594 ){ 595 goto mul_end; 596 } 597 sumDigit = pA->nDigit; 598 sumDigit += pB->nDigit; 599 sumDigit += 2; 600 if( sumDigit>SQLITE_DECIMAL_MAX_DIGIT ){ pA->oom = 1; return; } 601 acc = sqlite3_malloc64( sumDigit ); 602 if( acc==0 ){ 603 pA->oom = 1; 604 goto mul_end; 605 } 606 memset(acc, 0, pA->nDigit + pB->nDigit + 2); 607 minFrac = pA->nFrac; 608 if( pB->nFracnFrac; 609 for(i=pA->nDigit-1; i>=0; i--){ 610 signed char f = pA->a[i]; 611 int carry = 0, x; 612 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){ 613 x = acc[k] + f*pB->a[j] + carry; 614 acc[k] = x%10; 615 carry = x/10; 616 } 617 x = acc[k] + carry; 618 acc[k] = x%10; 619 acc[k-1] += x/10; 620 } 621 sqlite3_free(pA->a); 622 pA->a = acc; 623 acc = 0; 624 pA->nDigit += pB->nDigit + 2; 625 pA->nFrac += pB->nFrac; 626 pA->sign ^= pB->sign; 627 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){ 628 pA->nFrac--; 629 pA->nDigit--; 630 } 631 632 mul_end: 633 sqlite3_free(acc); 634 } 635 636 /* 637 ** Create a new Decimal object that contains an integer power of 2. 638 */ 639 static Decimal *decimalPow2(int N){ 640 Decimal *pA = 0; /* The result to be returned */ 641 Decimal *pX = 0; /* Multiplier */ 642 if( N<-20000 || N>20000 ) goto pow2_fault; 643 pA = decimalNewFromText("1.0", 3); 644 if( pA==0 || pA->oom ) goto pow2_fault; 645 if( N==0 ) return pA; 646 if( N>0 ){ 647 pX = decimalNewFromText("2.0", 3); 648 }else{ 649 N = -N; 650 pX = decimalNewFromText("0.5", 3); 651 } 652 if( pX==0 || pX->oom ) goto pow2_fault; 653 while( 1 /* Exit by break */ ){ 654 if( N & 1 ){ 655 decimalMul(pA, pX); 656 if( pA->oom ) goto pow2_fault; 657 } 658 N >>= 1; 659 if( N==0 ) break; 660 decimalMul(pX, pX); 661 } 662 decimal_free(pX); 663 return pA; 664 665 pow2_fault: 666 decimal_free(pA); 667 decimal_free(pX); 668 return 0; 669 } 670 671 /* 672 ** Use an IEEE754 binary64 ("double") to generate a new Decimal object. 673 */ 674 static Decimal *decimalFromDouble(double r){ 675 sqlite3_int64 m, a; 676 int e; 677 int isNeg; 678 Decimal *pA; 679 Decimal *pX; 680 char zNum[100]; 681 if( r<0.0 ){ 682 isNeg = 1; 683 r = -r; 684 }else{ 685 isNeg = 0; 686 } 687 memcpy(&a,&r,sizeof(a)); 688 if( a==0 || a==(sqlite3_int64)0x8000000000000000LL){ 689 e = 0; 690 m = 0; 691 }else{ 692 e = a>>52; 693 m = a & ((((sqlite3_int64)1)<<52)-1); 694 if( e==0 ){ 695 m <<= 1; 696 }else{ 697 m |= ((sqlite3_int64)1)<<52; 698 } 699 while( e<1075 && m>0 && (m&1)==0 ){ 700 m >>= 1; 701 e++; 702 } 703 if( isNeg ) m = -m; 704 e = e - 1075; 705 if( e>971 ){ 706 return 0; /* A NaN or an Infinity */ 707 } 708 } 709 710 /* At this point m is the integer significand and e is the exponent */ 711 sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m); 712 pA = decimalNewFromText(zNum, (int)strlen(zNum)); 713 pX = decimalPow2(e); 714 decimalMul(pA, pX); 715 decimal_free(pX); 716 return pA; 717 } 718 719 /* 720 ** Extract a 32-bit signed integer from argument pVal and write the 721 ** results into *pResult. Return 0 on success and 1 if the value is 722 ** too larger to fit in 32-bits. 723 */ 724 static int decimalSafeInt( 725 sqlite3_context *context, 726 sqlite3_value *pVal, 727 int *pResult 728 ){ 729 sqlite3_int64 ii = sqlite3_value_int64(pVal); 730 if( ii<-2147483648LL || ii>2147483647LL ){ 731 char *zMsg = sqlite3_mprintf( 732 "value %lld is too large: range limit -`2147483648`..+`2147483647`", 733 ii); 734 sqlite3_result_error(context, zMsg, -1); 735 sqlite3_free(zMsg); 736 *pResult = 0; 737 return 1; 738 }else{ 739 *pResult = (int)ii; 740 return 0; 741 } 742 } 743 744 /* 745 ** SQL Function: decimal(X) 746 ** OR: decimal_exp(X) 747 ** 748 ** Convert input X into decimal and then back into text. 749 ** 750 ** If X is originally a float, then a full decimal expansion of that floating 751 ** point value is done. Or if X is an 8-byte blob, it is interpreted 752 ** as a float and similarly expanded. 753 ** 754 ** The decimal_exp(X) function returns the result in exponential notation. 755 ** decimal(X) returns a complete decimal, without the e+NNN at the end. 756 */ 757 static void decimalFunc( 758 sqlite3_context *context, 759 int argc, 760 sqlite3_value **argv 761 ){ 762 Decimal *p = decimal_new(context, argv[0], 0); 763 int N; 764 if( argc==2 ){ 765 if( decimalSafeInt(context, argv[1], &N) ){ 766 decimal_free(p); 767 p = 0; 768 }else if( N>0 ){ 769 decimal_round(p, N); 770 } 771 }else{ 772 N = 0; 773 } 774 if( p ){ 775 if( sqlite3_user_data(context)!=0 ){ 776 decimal_result_sci(context, p, N); 777 }else{ 778 decimal_result(context, p); 779 } 780 decimal_free(p); 781 } 782 } 783 784 /* 785 ** Compare text in decimal order. 786 */ 787 static int decimalCollFunc( 788 void *notUsed, 789 int nKey1, const void *pKey1, 790 int nKey2, const void *pKey2 791 ){ 792 const unsigned char *zA = (const unsigned char*)pKey1; 793 const unsigned char *zB = (const unsigned char*)pKey2; 794 Decimal *pA = decimalNewFromText((const char*)zA, nKey1); 795 Decimal *pB = decimalNewFromText((const char*)zB, nKey2); 796 int rc; 797 UNUSED_PARAMETER(notUsed); 798 if( pA==0 || pB==0 ){ 799 rc = 0; 800 }else{ 801 rc = decimal_cmp(pA, pB); 802 } 803 decimal_free(pA); 804 decimal_free(pB); 805 return rc; 806 } 807 808 809 /* 810 ** SQL Function: decimal_add(X, Y) 811 ** decimal_sub(X, Y) 812 ** 813 ** Return the sum or difference of X and Y. 814 */ 815 static void decimalAddFunc( 816 sqlite3_context *context, 817 int argc, 818 sqlite3_value **argv 819 ){ 820 Decimal *pA = decimal_new(context, argv[0], 1); 821 Decimal *pB = decimal_new(context, argv[1], 1); 822 UNUSED_PARAMETER(argc); 823 if( pA && pB ){ 824 decimal_add(pA, pB); 825 decimal_result(context, pA); 826 } 827 decimal_free(pA); 828 decimal_free(pB); 829 } 830 static void decimalSubFunc( 831 sqlite3_context *context, 832 int argc, 833 sqlite3_value **argv 834 ){ 835 Decimal *pA = decimal_new(context, argv[0], 1); 836 Decimal *pB = decimal_new(context, argv[1], 1); 837 UNUSED_PARAMETER(argc); 838 if( pA && pB ){ 839 pB->sign = !pB->sign; 840 decimal_add(pA, pB); 841 decimal_result(context, pA); 842 } 843 decimal_free(pA); 844 decimal_free(pB); 845 } 846 847 /* Aggregate function: decimal_sum(X) 848 ** 849 ** Works like sum() except that it uses decimal arithmetic for unlimited 850 ** precision. 851 */ 852 static void decimalSumStep( 853 sqlite3_context *context, 854 int argc, 855 sqlite3_value **argv 856 ){ 857 Decimal *p; 858 Decimal *pArg; 859 UNUSED_PARAMETER(argc); 860 p = sqlite3_aggregate_context(context, sizeof(*p)); 861 if( p==0 ) return; 862 if( !p->isInit ){ 863 p->isInit = 1; 864 p->a = sqlite3_malloc64(2); 865 if( p->a==0 ){ 866 p->oom = 1; 867 }else{ 868 p->a[0] = 0; 869 } 870 p->nDigit = 1; 871 p->nFrac = 0; 872 } 873 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 874 pArg = decimal_new(context, argv[0], 1); 875 decimal_add(p, pArg); 876 decimal_free(pArg); 877 } 878 static void decimalSumInverse( 879 sqlite3_context *context, 880 int argc, 881 sqlite3_value **argv 882 ){ 883 Decimal *p; 884 Decimal *pArg; 885 UNUSED_PARAMETER(argc); 886 p = sqlite3_aggregate_context(context, sizeof(*p)); 887 if( p==0 ) return; 888 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 889 pArg = decimal_new(context, argv[0], 1); 890 if( pArg ) pArg->sign = !pArg->sign; 891 decimal_add(p, pArg); 892 decimal_free(pArg); 893 } 894 static void decimalSumValue(sqlite3_context *context){ 895 Decimal *p = sqlite3_aggregate_context(context, 0); 896 if( p==0 ) return; 897 decimal_result(context, p); 898 } 899 static void decimalSumFinalize(sqlite3_context *context){ 900 Decimal *p = sqlite3_aggregate_context(context, 0); 901 if( p==0 ) return; 902 decimal_result(context, p); 903 decimal_clear(p); 904 } 905 906 /* 907 ** SQL Function: decimal_mul(X, Y) 908 ** 909 ** Return the product of X and Y. 910 */ 911 static void decimalMulFunc( 912 sqlite3_context *context, 913 int argc, 914 sqlite3_value **argv 915 ){ 916 Decimal *pA = decimal_new(context, argv[0], 1); 917 Decimal *pB = decimal_new(context, argv[1], 1); 918 UNUSED_PARAMETER(argc); 919 if( pA==0 || pA->oom || pA->isNull 920 || pB==0 || pB->oom || pB->isNull 921 ){ 922 goto mul_end; 923 } 924 decimalMul(pA, pB); 925 if( pA->oom ){ 926 goto mul_end; 927 } 928 decimal_result(context, pA); 929 930 mul_end: 931 decimal_free(pA); 932 decimal_free(pB); 933 } 934 935 /* 936 ** SQL Function: decimal_pow2(N) 937 ** 938 ** Return the N-th power of 2. N must be an integer. 939 */ 940 static void decimalPow2Func( 941 sqlite3_context *context, 942 int argc, 943 sqlite3_value **argv 944 ){ 945 UNUSED_PARAMETER(argc); 946 if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){ 947 int N; 948 Decimal *pA; 949 if( decimalSafeInt(context, argv[0], &N) ) return; 950 pA = decimalPow2(N); 951 if( pA ){ 952 decimal_result_sci(context, pA, 0); 953 decimal_free(pA); 954 } 955 } 956 } 957 958 #ifdef _WIN32 959 __declspec(dllexport) 960 #endif 961 int sqlite3_decimal_init( 962 sqlite3 *db, 963 char **pzErrMsg, 964 const sqlite3_api_routines *pApi 965 ){ 966 int rc = SQLITE_OK; 967 static const struct { 968 const char *zFuncName; 969 int nArg; 970 int iArg; 971 void (*xFunc)(sqlite3_context*,int,sqlite3_value**); 972 } aFunc[] = { 973 { "decimal", 1, 0, decimalFunc }, 974 { "decimal", 2, 0, decimalFunc }, 975 { "decimal_exp", 1, 1, decimalFunc }, 976 { "decimal_exp", 2, 1, decimalFunc }, 977 { "decimal_cmp", 2, 0, decimalCmpFunc }, 978 { "decimal_add", 2, 0, decimalAddFunc }, 979 { "decimal_sub", 2, 0, decimalSubFunc }, 980 { "decimal_mul", 2, 0, decimalMulFunc }, 981 { "decimal_pow2", 1, 0, decimalPow2Func }, 982 }; 983 unsigned int i; 984 (void)pzErrMsg; /* Unused parameter */ 985 986 SQLITE_EXTENSION_INIT2(pApi); 987 988 for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){ 989 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg, 990 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 991 aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0); 992 } 993 if( rc==SQLITE_OK ){ 994 rc = sqlite3_create_window_function(db, "decimal_sum", 1, 995 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0, 996 decimalSumStep, decimalSumFinalize, 997 decimalSumValue, decimalSumInverse, 0); 998 } 999 if( rc==SQLITE_OK ){ 1000 rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8, 1001 0, decimalCollFunc); 1002 } 1003 return rc; 1004 }