------------------------------------------------------------------ */ decFloatQuantize -- quantize a decFloat */ / result gets the result of quantizing dfl to match dfr */ dfl is the first decFloat (lhs) */ dfr is the second decFloat (rhs), which sets the exponent */ set is the context
| 2897 | /* of result is guaranteed to be the same as that of dfr. */ |
| 2898 | /* ------------------------------------------------------------------ */ |
| 2899 | decFloat * decFloatQuantize(decFloat *result, |
| 2900 | const decFloat *dfl, const decFloat *dfr, |
| 2901 | decContext *set) { |
| 2902 | Int explb, exprb; // left and right biased exponents |
| 2903 | uByte *ulsd; // local LSD pointer |
| 2904 | uByte *ub, *uc; // work |
| 2905 | Int drop; // .. |
| 2906 | uInt dpd; // .. |
| 2907 | uInt encode; // encoding accumulator |
| 2908 | uInt sourhil, sourhir; // top words from source decFloats |
| 2909 | uInt uiwork; // for macros |
| 2910 | #if QUAD |
| 2911 | uShort uswork; // .. |
| 2912 | #endif |
| 2913 | // the following buffer holds the coefficient for manipulation |
| 2914 | uByte buf[4+DECPMAX*3+2*QUAD]; // + space for zeros to left or right |
| 2915 | #if DECTRACE |
| 2916 | bcdnum num; // for trace displays |
| 2917 | #endif |
| 2918 | |
| 2919 | /* Start decoding the arguments */ |
| 2920 | sourhil=DFWORD(dfl, 0); // LHS top word |
| 2921 | explb=DECCOMBEXP[sourhil>>26]; // get exponent high bits (in place) |
| 2922 | sourhir=DFWORD(dfr, 0); // RHS top word |
| 2923 | exprb=DECCOMBEXP[sourhir>>26]; |
| 2924 | |
| 2925 | if (EXPISSPECIAL(explb | exprb)) { // either is special? |
| 2926 | // NaNs are handled as usual |
| 2927 | if (DFISNAN(dfl) || DFISNAN(dfr)) return decNaNs(result, dfl, dfr, set); |
| 2928 | // one infinity but not both is bad |
| 2929 | if (DFISINF(dfl)!=DFISINF(dfr)) return decInvalid(result, set); |
| 2930 | // both infinite; return canonical infinity with sign of LHS |
| 2931 | return decInfinity(result, dfl); |
| 2932 | } |
| 2933 | |
| 2934 | /* Here when both arguments are finite */ |
| 2935 | // complete extraction of the exponents [no need to unbias] |
| 2936 | explb+=GETECON(dfl); // + continuation |
| 2937 | exprb+=GETECON(dfr); // .. |
| 2938 | |
| 2939 | // calculate the number of digits to drop from the coefficient |
| 2940 | drop=exprb-explb; // 0 if nothing to do |
| 2941 | if (drop==0) return decCanonical(result, dfl); // return canonical |
| 2942 | |
| 2943 | // the coefficient is needed; lay it out into buf, offset so zeros |
| 2944 | // can be added before or after as needed -- an extra heading is |
| 2945 | // added so can safely pad Quad DECPMAX-1 zeros to the left by |
| 2946 | // fours |
| 2947 | #define BUFOFF (buf+4+DECPMAX) |
| 2948 | GETCOEFF(dfl, BUFOFF); // decode from decFloat |
| 2949 | // [now the msd is at BUFOFF and the lsd is at BUFOFF+DECPMAX-1] |
| 2950 | |
| 2951 | #if DECTRACE |
| 2952 | num.msd=BUFOFF; |
| 2953 | num.lsd=BUFOFF+DECPMAX-1; |
| 2954 | num.exponent=explb-DECBIAS; |
| 2955 | num.sign=sourhil & DECFLOAT_Sign; |
| 2956 | decShowNum(&num, "dfl"); |
no test coverage detected