| OLD | NEW |
| 1 /* ------------------------------------------------------------------ */ | 1 /* ------------------------------------------------------------------ */ |
| 2 /* Decimal Number arithmetic module */ | 2 /* Decimal Number arithmetic module */ |
| 3 /* ------------------------------------------------------------------ */ | 3 /* ------------------------------------------------------------------ */ |
| 4 /* Copyright (c) IBM Corporation, 2000-2010. All rights reserved. */ | 4 /* Copyright (c) IBM Corporation, 2000-2010. All rights reserved. */ |
| 5 /* */ | 5 /* */ |
| 6 /* This software is made available under the terms of the */ | 6 /* This software is made available under the terms of the */ |
| 7 /* ICU License -- ICU 1.8.1 and later. */ | 7 /* ICU License -- ICU 1.8.1 and later. */ |
| 8 /* */ | 8 /* */ |
| 9 /* The description and User's Guide ("The decNumber C Library") for */ | 9 /* The description and User's Guide ("The decNumber C Library") for */ |
| 10 /* this software is called decNumber.pdf. This document is */ | 10 /* this software is called decNumber.pdf. This document is */ |
| (...skipping 1374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1385 /* almost always be correctly rounded, but may be up to 1 ulp in */ | 1385 /* almost always be correctly rounded, but may be up to 1 ulp in */ |
| 1386 /* error in rare cases. */ | 1386 /* error in rare cases. */ |
| 1387 /* ------------------------------------------------------------------ */ | 1387 /* ------------------------------------------------------------------ */ |
| 1388 /* This calculates ln(A)/ln(10) using appropriate precision. For */ | 1388 /* This calculates ln(A)/ln(10) using appropriate precision. For */ |
| 1389 /* ln(A) this is the max(p, rhs->digits + t) + 3, where p is the */ | 1389 /* ln(A) this is the max(p, rhs->digits + t) + 3, where p is the */ |
| 1390 /* requested digits and t is the number of digits in the exponent */ | 1390 /* requested digits and t is the number of digits in the exponent */ |
| 1391 /* (maximum 6). For ln(10) it is p + 3; this is often handled by the */ | 1391 /* (maximum 6). For ln(10) it is p + 3; this is often handled by the */ |
| 1392 /* fastpath in decLnOp. The final division is done to the requested */ | 1392 /* fastpath in decLnOp. The final division is done to the requested */ |
| 1393 /* precision. */ | 1393 /* precision. */ |
| 1394 /* ------------------------------------------------------------------ */ | 1394 /* ------------------------------------------------------------------ */ |
| 1395 #pragma clang diagnostic push |
| 1396 #pragma clang diagnostic ignored "-Warray-bounds" |
| 1395 U_CAPI decNumber * U_EXPORT2 uprv_decNumberLog10(decNumber *res, const decNumber
*rhs, | 1397 U_CAPI decNumber * U_EXPORT2 uprv_decNumberLog10(decNumber *res, const decNumber
*rhs, |
| 1396 decContext *set) { | 1398 decContext *set) { |
| 1397 uInt status=0, ignore=0; /* status accumulators */ | 1399 uInt status=0, ignore=0; /* status accumulators */ |
| 1398 uInt needbytes; /* for space calculations */ | 1400 uInt needbytes; /* for space calculations */ |
| 1399 Int p; /* working precision */ | 1401 Int p; /* working precision */ |
| 1400 Int t; /* digits in exponent of A */ | 1402 Int t; /* digits in exponent of A */ |
| 1401 | 1403 |
| 1402 /* buffers for a and b working decimals */ | 1404 /* buffers for a and b working decimals */ |
| 1403 /* (adjustment calculator, same size) */ | 1405 /* (adjustment calculator, same size) */ |
| 1404 decNumber bufa[D2N(DECBUFFER+2)]; | 1406 decNumber bufa[D2N(DECBUFFER+2)]; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1520 #if DECSUBSET | 1522 #if DECSUBSET |
| 1521 if (allocrhs !=NULL) free(allocrhs); /* .. */ | 1523 if (allocrhs !=NULL) free(allocrhs); /* .. */ |
| 1522 #endif | 1524 #endif |
| 1523 /* apply significant status */ | 1525 /* apply significant status */ |
| 1524 if (status!=0) decStatus(res, status, set); | 1526 if (status!=0) decStatus(res, status, set); |
| 1525 #if DECCHECK | 1527 #if DECCHECK |
| 1526 decCheckInexact(res, set); | 1528 decCheckInexact(res, set); |
| 1527 #endif | 1529 #endif |
| 1528 return res; | 1530 return res; |
| 1529 } /* decNumberLog10 */ | 1531 } /* decNumberLog10 */ |
| 1532 #pragma clang diagnostic pop |
| 1530 | 1533 |
| 1531 /* ------------------------------------------------------------------ */ | 1534 /* ------------------------------------------------------------------ */ |
| 1532 /* decNumberMax -- compare two Numbers and return the maximum */ | 1535 /* decNumberMax -- compare two Numbers and return the maximum */ |
| 1533 /* */ | 1536 /* */ |
| 1534 /* This computes C = A ? B, returning the maximum by 754 rules */ | 1537 /* This computes C = A ? B, returning the maximum by 754 rules */ |
| 1535 /* */ | 1538 /* */ |
| 1536 /* res is C, the result. C may be A and/or B (e.g., X=X?X) */ | 1539 /* res is C, the result. C may be A and/or B (e.g., X=X?X) */ |
| 1537 /* lhs is A */ | 1540 /* lhs is A */ |
| 1538 /* rhs is B */ | 1541 /* rhs is B */ |
| 1539 /* set is the context */ | 1542 /* set is the context */ |
| (...skipping 1253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2793 /* else */ | 2796 /* else */ |
| 2794 /* const approxaddhalf := approx + setexp(.5, -p) */ | 2797 /* const approxaddhalf := approx + setexp(.5, -p) */ |
| 2795 /* if mulrd(approxaddhalf, approxaddhalf) < f then */ | 2798 /* if mulrd(approxaddhalf, approxaddhalf) < f then */ |
| 2796 /* approx := approx + setexp(.l, -p + 1) */ | 2799 /* approx := approx + setexp(.l, -p + 1) */ |
| 2797 /* end if */ | 2800 /* end if */ |
| 2798 /* end if */ | 2801 /* end if */ |
| 2799 /* end */ | 2802 /* end */ |
| 2800 /* result setexp(approx, e div 2) % fix exponent */ | 2803 /* result setexp(approx, e div 2) % fix exponent */ |
| 2801 /* end sqrt */ | 2804 /* end sqrt */ |
| 2802 /* ------------------------------------------------------------------ */ | 2805 /* ------------------------------------------------------------------ */ |
| 2806 #pragma clang diagnostic push |
| 2807 #pragma clang diagnostic ignored "-Warray-bounds" |
| 2803 U_CAPI decNumber * U_EXPORT2 uprv_decNumberSquareRoot(decNumber *res, const decN
umber *rhs, | 2808 U_CAPI decNumber * U_EXPORT2 uprv_decNumberSquareRoot(decNumber *res, const decN
umber *rhs, |
| 2804 decContext *set) { | 2809 decContext *set) { |
| 2805 decContext workset, approxset; /* work contexts */ | 2810 decContext workset, approxset; /* work contexts */ |
| 2806 decNumber dzero; /* used for constant zero */ | 2811 decNumber dzero; /* used for constant zero */ |
| 2807 Int maxp; /* largest working precision */ | 2812 Int maxp; /* largest working precision */ |
| 2808 Int workp; /* working precision */ | 2813 Int workp; /* working precision */ |
| 2809 Int residue=0; /* rounding residue */ | 2814 Int residue=0; /* rounding residue */ |
| 2810 uInt status=0, ignore=0; /* status accumulators */ | 2815 uInt status=0, ignore=0; /* status accumulators */ |
| 2811 uInt rstatus; /* .. */ | 2816 uInt rstatus; /* .. */ |
| 2812 Int exp; /* working exponent */ | 2817 Int exp; /* working exponent */ |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3122 if (allocbufb!=NULL) free(allocbufb); /* .. */ | 3127 if (allocbufb!=NULL) free(allocbufb); /* .. */ |
| 3123 #if DECSUBSET | 3128 #if DECSUBSET |
| 3124 if (allocrhs !=NULL) free(allocrhs); /* .. */ | 3129 if (allocrhs !=NULL) free(allocrhs); /* .. */ |
| 3125 #endif | 3130 #endif |
| 3126 if (status!=0) decStatus(res, status, set);/* then report status */ | 3131 if (status!=0) decStatus(res, status, set);/* then report status */ |
| 3127 #if DECCHECK | 3132 #if DECCHECK |
| 3128 decCheckInexact(res, set); | 3133 decCheckInexact(res, set); |
| 3129 #endif | 3134 #endif |
| 3130 return res; | 3135 return res; |
| 3131 } /* decNumberSquareRoot */ | 3136 } /* decNumberSquareRoot */ |
| 3137 #pragma clang diagnostic pop |
| 3132 | 3138 |
| 3133 /* ------------------------------------------------------------------ */ | 3139 /* ------------------------------------------------------------------ */ |
| 3134 /* decNumberSubtract -- subtract two Numbers */ | 3140 /* decNumberSubtract -- subtract two Numbers */ |
| 3135 /* */ | 3141 /* */ |
| 3136 /* This computes C = A - B */ | 3142 /* This computes C = A - B */ |
| 3137 /* */ | 3143 /* */ |
| 3138 /* res is C, the result. C may be A and/or B (e.g., X=X-X) */ | 3144 /* res is C, the result. C may be A and/or B (e.g., X=X-X) */ |
| 3139 /* lhs is A */ | 3145 /* lhs is A */ |
| 3140 /* rhs is B */ | 3146 /* rhs is B */ |
| 3141 /* set is the context */ | 3147 /* set is the context */ |
| (...skipping 2435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5577 /* would certainly save at least one if it were made ten times */ | 5583 /* would certainly save at least one if it were made ten times */ |
| 5578 /* bigger, too (for truncated fractions 0.100 through 0.999). */ | 5584 /* bigger, too (for truncated fractions 0.100 through 0.999). */ |
| 5579 /* However, for most practical evaluations, at least four or five */ | 5585 /* However, for most practical evaluations, at least four or five */ |
| 5580 /* iterations will be neede -- so this would only speed up by */ | 5586 /* iterations will be neede -- so this would only speed up by */ |
| 5581 /* 20-25% and that probably does not justify increasing the table */ | 5587 /* 20-25% and that probably does not justify increasing the table */ |
| 5582 /* size. */ | 5588 /* size. */ |
| 5583 /* */ | 5589 /* */ |
| 5584 /* 5. The static buffers are larger than might be expected to allow */ | 5590 /* 5. The static buffers are larger than might be expected to allow */ |
| 5585 /* for calls from decNumberPower. */ | 5591 /* for calls from decNumberPower. */ |
| 5586 /* ------------------------------------------------------------------ */ | 5592 /* ------------------------------------------------------------------ */ |
| 5593 #pragma clang diagnostic push |
| 5594 #pragma clang diagnostic ignored "-Warray-bounds" |
| 5587 decNumber * decLnOp(decNumber *res, const decNumber *rhs, | 5595 decNumber * decLnOp(decNumber *res, const decNumber *rhs, |
| 5588 decContext *set, uInt *status) { | 5596 decContext *set, uInt *status) { |
| 5589 uInt ignore=0; /* working status accumulator */ | 5597 uInt ignore=0; /* working status accumulator */ |
| 5590 uInt needbytes; /* for space calculations */ | 5598 uInt needbytes; /* for space calculations */ |
| 5591 Int residue; /* rounding residue */ | 5599 Int residue; /* rounding residue */ |
| 5592 Int r; /* rhs=f*10**r [see below] */ | 5600 Int r; /* rhs=f*10**r [see below] */ |
| 5593 Int p; /* working precision */ | 5601 Int p; /* working precision */ |
| 5594 Int pp; /* precision for iteration */ | 5602 Int pp; /* precision for iteration */ |
| 5595 Int t; /* work */ | 5603 Int t; /* work */ |
| 5596 | 5604 |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5798 aset.digits=set->digits; /* [use default rounding] */ | 5806 aset.digits=set->digits; /* [use default rounding] */ |
| 5799 decCopyFit(res, a, &aset, &residue, status); /* copy & shorten */ | 5807 decCopyFit(res, a, &aset, &residue, status); /* copy & shorten */ |
| 5800 decFinish(res, set, &residue, status); /* cleanup/set flags */ | 5808 decFinish(res, set, &residue, status); /* cleanup/set flags */ |
| 5801 } while(0); /* end protected */ | 5809 } while(0); /* end protected */ |
| 5802 | 5810 |
| 5803 if (allocbufa!=NULL) free(allocbufa); /* drop any storage used */ | 5811 if (allocbufa!=NULL) free(allocbufa); /* drop any storage used */ |
| 5804 if (allocbufb!=NULL) free(allocbufb); /* .. */ | 5812 if (allocbufb!=NULL) free(allocbufb); /* .. */ |
| 5805 /* [status is handled by caller] */ | 5813 /* [status is handled by caller] */ |
| 5806 return res; | 5814 return res; |
| 5807 } /* decLnOp */ | 5815 } /* decLnOp */ |
| 5816 #pragma clang diagnostic pop |
| 5808 | 5817 |
| 5809 /* ------------------------------------------------------------------ */ | 5818 /* ------------------------------------------------------------------ */ |
| 5810 /* decQuantizeOp -- force exponent to requested value */ | 5819 /* decQuantizeOp -- force exponent to requested value */ |
| 5811 /* */ | 5820 /* */ |
| 5812 /* This computes C = op(A, B), where op adjusts the coefficient */ | 5821 /* This computes C = op(A, B), where op adjusts the coefficient */ |
| 5813 /* of C (by rounding or shifting) such that the exponent (-scale) */ | 5822 /* of C (by rounding or shifting) such that the exponent (-scale) */ |
| 5814 /* of C has the value B or matches the exponent of B. */ | 5823 /* of C has the value B or matches the exponent of B. */ |
| 5815 /* The numerical value of C will equal A, except for the effects of */ | 5824 /* The numerical value of C will equal A, except for the effects of */ |
| 5816 /* any rounding that occurred. */ | 5825 /* any rounding that occurred. */ |
| 5817 /* */ | 5826 /* */ |
| (...skipping 2316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8134 for (b=b0+n+8; b<b0+n+12; b++) if (*b!=DECFENCE) | 8143 for (b=b0+n+8; b<b0+n+12; b++) if (*b!=DECFENCE) |
| 8135 printf("=== Corrupt byte [%02x] at offset +%d from %ld, n=%ld ===\n", *b, | 8144 printf("=== Corrupt byte [%02x] at offset +%d from %ld, n=%ld ===\n", *b, |
| 8136 b-b0-8, (LI)b0, (LI)n); | 8145 b-b0-8, (LI)b0, (LI)n); |
| 8137 free(b0); /* drop the storage */ | 8146 free(b0); /* drop the storage */ |
| 8138 decAllocBytes-=n; /* account for storage */ | 8147 decAllocBytes-=n; /* account for storage */ |
| 8139 /* printf(" free -- dAB: %d (%d)\n", decAllocBytes, -n); */ | 8148 /* printf(" free -- dAB: %d (%d)\n", decAllocBytes, -n); */ |
| 8140 } /* decFree */ | 8149 } /* decFree */ |
| 8141 #define malloc(a) decMalloc(a) | 8150 #define malloc(a) decMalloc(a) |
| 8142 #define free(a) decFree(a) | 8151 #define free(a) decFree(a) |
| 8143 #endif | 8152 #endif |
| OLD | NEW |