------------------------------------------------------------------ */ decNumberCompareTotalMag -- compare, total ordering of magnitudes */ / This computes C = |A| ? |B|, under total ordering */ / res is C, the result. C may be A and/or B (e.g., X=X?X) */ lhs is A */ rhs is B
| 913 | /* -1, 0, or 1. */ |
| 914 | /* ------------------------------------------------------------------ */ |
| 915 | decNumber * decNumberCompareTotalMag(decNumber *res, const decNumber *lhs, |
| 916 | const decNumber *rhs, decContext *set) { |
| 917 | uInt status=0; // accumulator |
| 918 | uInt needbytes; // for space calculations |
| 919 | decNumber bufa[D2N(DECBUFFER+1)];// +1 in case DECBUFFER=0 |
| 920 | decNumber *allocbufa=NULL; // -> allocated bufa, iff allocated |
| 921 | decNumber bufb[D2N(DECBUFFER+1)]; |
| 922 | decNumber *allocbufb=NULL; // -> allocated bufb, iff allocated |
| 923 | decNumber *a, *b; // temporary pointers |
| 924 | |
| 925 | #if DECCHECK |
| 926 | if (decCheckOperands(res, lhs, rhs, set)) return res; |
| 927 | #endif |
| 928 | |
| 929 | do { // protect allocated storage |
| 930 | // if either is negative, take a copy and absolute |
| 931 | if (decNumberIsNegative(lhs)) { // lhs<0 |
| 932 | a=bufa; |
| 933 | needbytes=sizeof(decNumber)+(D2U(lhs->digits)-1)*sizeof(Unit); |
| 934 | if (needbytes>sizeof(bufa)) { // need malloc space |
| 935 | allocbufa=(decNumber *)malloc(needbytes); |
| 936 | if (allocbufa==NULL) { // hopeless -- abandon |
| 937 | status|=DEC_Insufficient_storage; |
| 938 | break;} |
| 939 | a=allocbufa; // use the allocated space |
| 940 | } |
| 941 | decNumberCopy(a, lhs); // copy content |
| 942 | a->bits&=~DECNEG; // .. and clear the sign |
| 943 | lhs=a; // use copy from here on |
| 944 | } |
| 945 | if (decNumberIsNegative(rhs)) { // rhs<0 |
| 946 | b=bufb; |
| 947 | needbytes=sizeof(decNumber)+(D2U(rhs->digits)-1)*sizeof(Unit); |
| 948 | if (needbytes>sizeof(bufb)) { // need malloc space |
| 949 | allocbufb=(decNumber *)malloc(needbytes); |
| 950 | if (allocbufb==NULL) { // hopeless -- abandon |
| 951 | status|=DEC_Insufficient_storage; |
| 952 | break;} |
| 953 | b=allocbufb; // use the allocated space |
| 954 | } |
| 955 | decNumberCopy(b, rhs); // copy content |
| 956 | b->bits&=~DECNEG; // .. and clear the sign |
| 957 | rhs=b; // use copy from here on |
| 958 | } |
| 959 | decCompareOp(res, lhs, rhs, set, COMPTOTAL, &status); |
| 960 | } while(0); // end protected |
| 961 | |
| 962 | if (allocbufa!=NULL) free(allocbufa); // drop any storage used |
| 963 | if (allocbufb!=NULL) free(allocbufb); // .. |
| 964 | if (status!=0) decStatus(res, status, set); |
| 965 | return res; |
| 966 | } // decNumberCompareTotalMag |
| 967 | |
| 968 | /* ------------------------------------------------------------------ */ |
| 969 | /* decNumberDivide -- divide one number by another */ |
nothing calls this directly
no test coverage detected