MCPcopy Create free account
hub / github.com/FirebirdSQL/firebird / decNumCompare

Function decNumCompare

extern/decNumber/decBasic.c:3621–3772  ·  view source on GitHub ↗

------------------------------------------------------------------ */ decNumCompare -- numeric comparison of two decFloats */ / dfl is the left-hand decFloat, which is not a NaN */ dfr is the right-hand decFloat, which is not a NaN */ tot is 1 for total order compare, 0 for simple numeric */ returns -1, 0, or +1 for dfl dfr

Source from the content-addressed store, hash-verified

3619/* No error is possible; status and mode are unchanged. */
3620/* ------------------------------------------------------------------ */
3621static Int decNumCompare(const decFloat *dfl, const decFloat *dfr, Flag tot) {
3622 Int sigl, sigr; // LHS and RHS non-0 signums
3623 Int shift; // shift needed to align operands
3624 uByte *ub, *uc; // work
3625 uInt uiwork; // for macros
3626 // buffers +2 if Quad (36 digits), need double plus 4 for safe padding
3627 uByte bufl[DECPMAX*2+QUAD*2+4]; // for LHS coefficient + padding
3628 uByte bufr[DECPMAX*2+QUAD*2+4]; // for RHS coefficient + padding
3629
3630 sigl=1;
3631 if (DFISSIGNED(dfl)) {
3632 if (!DFISSIGNED(dfr)) { // -LHS +RHS
3633 if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0;
3634 return -1; // RHS wins
3635 }
3636 sigl=-1;
3637 }
3638 if (DFISSIGNED(dfr)) {
3639 if (!DFISSIGNED(dfl)) { // +LHS -RHS
3640 if (DFISZERO(dfl) && DFISZERO(dfr) && !tot) return 0;
3641 return +1; // LHS wins
3642 }
3643 }
3644
3645 // signs are the same; operand(s) could be zero
3646 sigr=-sigl; // sign to return if abs(RHS) wins
3647
3648 if (DFISINF(dfl)) {
3649 if (DFISINF(dfr)) return 0; // both infinite & same sign
3650 return sigl; // inf > n
3651 }
3652 if (DFISINF(dfr)) return sigr; // n < inf [dfl is finite]
3653
3654 // here, both are same sign and finite; calculate their offset
3655 shift=GETEXP(dfl)-GETEXP(dfr); // [0 means aligned]
3656 // [bias can be ignored -- the absolute exponent is not relevant]
3657
3658 if (DFISZERO(dfl)) {
3659 if (!DFISZERO(dfr)) return sigr; // LHS=0, RHS!=0
3660 // both are zero, return 0 if both same exponent or numeric compare
3661 if (shift==0 || !tot) return 0;
3662 if (shift>0) return sigl;
3663 return sigr; // [shift<0]
3664 }
3665 else { // LHS!=0
3666 if (DFISZERO(dfr)) return sigl; // LHS!=0, RHS=0
3667 }
3668 // both are known to be non-zero at this point
3669
3670 // if the exponents are so different that the coefficients do not
3671 // overlap (by even one digit) then a full comparison is not needed
3672 if (abs(shift)>=DECPMAX) { // no overlap
3673 // coefficients are known to be non-zero
3674 if (shift>0) return sigl;
3675 return sigr; // [shift<0]
3676 }
3677
3678 // decode the coefficients

Callers 8

decFloatCompareFunction · 0.85
decFloatCompareSignalFunction · 0.85
decFloatCompareTotalFunction · 0.85
decFloatMaxFunction · 0.85
decFloatMaxMagFunction · 0.85
decFloatMinFunction · 0.85
decFloatMinMagFunction · 0.85
decFloatNextTowardFunction · 0.85

Calls 1

absFunction · 0.85

Tested by

no test coverage detected