Skip to content

Commit

Permalink
Change way of checking sign in 128-bit arithmetic
Browse files Browse the repository at this point in the history
It appears that bit shift is slightly faster that &
  • Loading branch information
skrzypo987 committed Sep 8, 2020
1 parent 9f6c316 commit 71a8fcf
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -724,27 +724,27 @@ public static void negate(Slice decimal)

public static boolean isStrictlyNegative(Slice decimal)
{
return isNegative(decimal) && (getLong(decimal, 0) != 0 || getLong(decimal, 1) != 0);
return isStrictlyNegative(getRawLong(decimal, 0), getRawLong(decimal, 1));
}

public static boolean isStrictlyNegative(long rawLow, long rawHigh)
{
return isNegative(rawLow, rawHigh) && (rawLow != 0 || unpackUnsignedLong(rawHigh) != 0);
return isNegative(rawHigh) && (rawLow != 0 || unpackUnsignedLong(rawHigh) != 0);
}

private static boolean isNegative(int lastRawHigh)
{
return (lastRawHigh & SIGN_INT_MASK) != 0;
return lastRawHigh >>> 31 != 0;
}

public static boolean isNegative(Slice decimal)
{
return (getRawInt(decimal, SIGN_INT_INDEX) & SIGN_INT_MASK) != 0;
return isNegative(getRawInt(decimal, SIGN_INT_INDEX));
}

public static boolean isNegative(long rawLow, long rawHigh)
public static boolean isNegative(long rawHigh)
{
return (rawHigh & SIGN_LONG_MASK) != 0;
return rawHigh >>> 63 != 0;
}

public static boolean isZero(Slice decimal)
Expand Down Expand Up @@ -1215,8 +1215,8 @@ private static void divide(long dividendLow, long dividendHigh, int dividendScal
throwOverflowException();
}

boolean dividendIsNegative = isNegative(dividendLow, dividendHigh);
boolean divisorIsNegative = isNegative(divisorLow, divisorHigh);
boolean dividendIsNegative = isNegative(dividendHigh);
boolean divisorIsNegative = isNegative(divisorHigh);
boolean quotientIsNegative = (dividendIsNegative != divisorIsNegative);

// to fit 128b * 128b * 32b unsigned multiplication
Expand Down

0 comments on commit 71a8fcf

Please sign in to comment.