in java/dfp/src/main/java/com/epam/deltix/dfp/JavaImplCmp.java [148:240]
public static boolean bid64_quiet_equal(final long /*BID_UINT64*/ x, final long /*BID_UINT64*/ y) {
int exp_x, exp_y, exp_t;
long /*BID_UINT64*/ sig_x, sig_y, sig_t;
boolean x_is_zero = false, y_is_zero = false, non_canon_x, non_canon_y;
// NaN (CASE1)
// if either number is NAN, the comparison is unordered,
// rather than equal : return 0
if (((x & MASK_NAN) == MASK_NAN) || ((y & MASK_NAN) == MASK_NAN)) {
return false;
}
// SIMPLE (CASE2)
// if all the bits are the same, these numbers are equivalent.
if (x == y) {
return true;
}
// INFINITY (CASE3)
if (((x & MASK_INF) == MASK_INF) && ((y & MASK_INF) == MASK_INF)) {
return (((x ^ y) & MASK_SIGN) != MASK_SIGN);
}
// ONE INFINITY (CASE3')
if (((x & MASK_INF) == MASK_INF) || ((y & MASK_INF) == MASK_INF)) {
return false;
}
// if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
exp_x = (int) ((x & MASK_BINARY_EXPONENT2) >>> 51);
sig_x = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
non_canon_x = sig_x > 9999999999999999L;
} else {
exp_x = (int) ((x & MASK_BINARY_EXPONENT1) >>> 53);
sig_x = (x & MASK_BINARY_SIG1);
non_canon_x = false;
}
// if steering bits are 11 (condition will be 0), then exponent is G[0:w+1] =>
if ((y & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
exp_y = (int) ((y & MASK_BINARY_EXPONENT2) >>> 51);
sig_y = (y & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
non_canon_y = sig_y > 9999999999999999L;
} else {
exp_y = (int) ((y & MASK_BINARY_EXPONENT1) >>> 53);
sig_y = (y & MASK_BINARY_SIG1);
non_canon_y = false;
}
// ZERO (CASE4)
// some properties:
// (+ZERO==-ZERO) => therefore ignore the sign
// (ZERO x 10^A == ZERO x 10^B) for any valid A, B =>
// therefore ignore the exponent field
// (Any non-canonical # is considered 0)
if (non_canon_x || sig_x == 0) {
x_is_zero = true;
}
if (non_canon_y || sig_y == 0) {
y_is_zero = true;
}
if (x_is_zero && y_is_zero) {
return true;
} else if ((x_is_zero && !y_is_zero) || (!x_is_zero && y_is_zero)) {
return false;
}
// OPPOSITE SIGN (CASE5)
// now, if the sign bits differ => not equal : return 0
if (((x ^ y) & MASK_SIGN) != 0) {
return false;
}
// REDUNDANT REPRESENTATIONS (CASE6)
if (exp_x > exp_y) { // to simplify the loop below,
// SWAP (exp_x, exp_y, exp_t); // put the larger exp in y,
{
exp_t = exp_x;
exp_x = exp_y;
exp_y = exp_t;
}
// SWAP (sig_x, sig_y, sig_t); // and the smaller exp in x
{
sig_t = sig_x;
sig_x = sig_y;
sig_y = sig_t;
}
}
if (exp_y - exp_x > 15) {
return false; // difference cannot be greater than 10^15
}
for (int lcv = 0, lce = exp_y - exp_x; lcv < lce; lcv++) {
// recalculate y's significand upwards
sig_y = sig_y * 10;
if (UnsignedLong.isGreater(sig_y, 9999999999999999L)) {
return false;
}
}
return sig_y == sig_x;
}