in java/dfp/src/main/java/com/epam/deltix/dfp/JavaImplRound.java [582:716]
public static long bid64_round_integral_negative(long /*BID_UINT64*/ x, final int rnd_mode/*, final JavaImplParse.FloatingPointStatusFlag pfpsf*/) {
long /*BID_UINT64*/ res = 0xbaddbaddbaddbaddL;
long /*BID_UINT64*/ x_sign;
int exp; // unbiased exponent
// Note: C1.w[1], C1.w[0] represent x_signif_hi, x_signif_lo (all are BID_UINT64)
//BID_UI64DOUBLE tmp1;
int x_nr_bits;
int q, ind, shift;
long /*BID_UINT64*/ C1;
// BID_UINT64 res is C* at first - represents up to 34 decimal digits ~ 113 bits
long /*BID_UINT128*/ fstar_w0 = 0x0L, fstar_w1 = 0x0L;
long /*BID_UINT128*/ P128_w0, P128_w1;
x_sign = x & MASK_SIGN; // 0 for positive, MASK_SIGN for negative
// check for NaNs and infinities
if ((x & MASK_NAN) == MASK_NAN) { // check for NaN
if ((x & 0x0003ffffffffffffL) >= 1000000000000000L)
x = x & 0xfe00000000000000L; // clear G6-G12 and the payload bits
else
x = x & 0xfe03ffffffffffffL; // clear G6-G12
if ((x & MASK_SNAN) == MASK_SNAN) { // SNaN
// set invalid flag
// __set_status_flags(pfpsf, BID_INVALID_EXCEPTION);
// return quiet (SNaN)
res = x & 0xfdffffffffffffffL;
} else { // QNaN
res = x;
}
return res;
} else if ((x & MASK_INF) == MASK_INF) { // check for Infinity
return x_sign | 0x7800000000000000L;
}
// unpack x
if ((x & MASK_STEERING_BITS) == MASK_STEERING_BITS) {
// if the steering bits are 11 (condition will be 0), then
// the exponent is G[0:w+1]
exp = (int) (((x & MASK_BINARY_EXPONENT2) >>> 51) - 398);
C1 = (x & MASK_BINARY_SIG2) | MASK_BINARY_OR2;
if (C1 > 9999999999999999L) { // non-canonical
C1 = 0;
}
} else { // if ((x & MASK_STEERING_BITS) != MASK_STEERING_BITS)
exp = (int) (((x & MASK_BINARY_EXPONENT1) >>> 53) - 398);
C1 = (x & MASK_BINARY_SIG1);
}
// if x is 0 or non-canonical
if (C1 == 0) {
if (exp < 0)
exp = 0;
return x_sign | (((long) exp + 398) << 53);
}
// x is a finite non-zero number (not 0, non-canonical, or special)
// return 0 if (exp <= -p)
if (exp <= -16) {
if (x_sign != 0) {
res = 0xb1c0000000000001L;
} else {
res = 0x31c0000000000000L;
}
return res;
}
// q = nr. of decimal digits in x (1 <= q <= 54)
// determine first the nr. of bits in x
if (UnsignedLong.isGreaterOrEqual(C1, 0x0020000000000000L)) { // x >= 2^53
q = 16;
} else { // if x < 2^53
final long tmp1_ui64 = Double.doubleToRawLongBits((double) C1); // exact conversion
x_nr_bits = 1 + ((((/*unsigned*/ int) (tmp1_ui64 >>> 52)) & 0x7ff) - 0x3ff);
q = (int) bid_nr_digits_flat[((x_nr_bits - 1) << 2) /*+ 0 .digits*/];
if (q == 0) {
q = (int) bid_nr_digits_flat[((x_nr_bits - 1) << 2) + 3 /*.digits1*/];
if (UnsignedLong.isGreaterOrEqual(C1, bid_nr_digits_flat[((x_nr_bits - 1) << 2) + 2 /*.threshold_lo*/]))
q++;
}
}
if (exp >= 0) { // -exp <= 0
// the argument is an integer already
return x;
} else if ((q + exp) > 0) { // exp < 0 and 1 <= -exp < q
// need to shift right -exp digits from the coefficient; the exp will be 0
ind = -exp; // 1 <= ind <= 16; ind is a synonym for 'x'
// chop off ind digits from the lower part of C1
// C1 fits in 64 bits
// calculate C* and f*
// C* is actually floor(C*) in this case
// C* and f* need shifting and masking, as shown by
// bid_shiftright128[] and bid_maskhigh128[]
// 1 <= x <= 16
// kx = 10^(-x) = bid_ten2mk64[ind - 1]
// C* = C1 * 10^(-x)
// the approximation of 10^(-x) was rounded up to 64 bits
//__mul_64x64_to_128(P128, C1, bid_ten2mk64[ind - 1]);
{
final long __CY = bid_ten2mk64[ind - 1];
P128_w1 = Mul64Impl.unsignedMultiplyHigh(C1, __CY);
P128_w0 = C1 * __CY;
}
// C* = floor(C*) (logical right shift; C has p decimal digits,
// correct by Property 1)
// if (0 < f* < 10^(-x)) then the result is exact
// n = C* * 10^(e+x)
if (ind - 1 <= 2) { // 0 <= ind - 1 <= 2 => shift = 0
res = P128_w1;
fstar_w1 = 0;
fstar_w0 = P128_w0;
} else if (ind - 1 <= 21) { // 3 <= ind - 1 <= 21 => 3 <= shift <= 63
shift = bid_shiftright128[ind - 1]; // 3 <= shift <= 63
res = (P128_w1 >>> shift);
fstar_w1 = P128_w1 & bid_maskhigh128[ind - 1];
fstar_w0 = P128_w0;
}
// if (f* > 10^(-x)) then the result is inexact
if (x_sign != 0 && ((fstar_w1 != 0) || UnsignedLong.isGreaterOrEqual(fstar_w0, bid_ten2mk64[ind - 1]))) {
// if negative and not exact, increment magnitude
res++;
}
// set exponent to zero as it was negative before.
return x_sign | 0x31c0000000000000L | res;
} else { // if exp < 0 and q + exp <= 0
// the result is +0 or -1
if (x_sign != 0) {
res = 0xb1c0000000000001L;
} else {
res = 0x31c0000000000000L;
}
return res;
}
}