Package cc.redberry.libdivide4j
Class FastDivision
java.lang.Object
cc.redberry.libdivide4j.FastDivision
Fast integer division and modulo operation (both signed and unsigned).
Usage example:
long[] someData = ...
long denominator = 45;
FastDivision.Magic magic = FastDivision.magicSigned(denominator);
long[] reduced = new long[someData.length];
for (int i = 0; i < someData.length; ++i) {
// this is the same as someData[i] / denominator but 3 times faster
reduced[i] = FastDivision.divideSignedFast(someData[i], magic);
}
The code for fast division with preconditioning is adapted from C libdivide library ( http://github.com/ridiculousfish/libdivide)
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic long[]divideAndRemainder128(long u1, long u0, long v) Return's quotient and remainder of 128 bit integer division by 64 bit integer.static longdivideSignedFast(long dividend, FastDivision.Magic divider) Returns signeddividend / dividerusing fast integer divisionstatic longdivideUnsignedFast(long dividend, FastDivision.Magic divider) Returns unsigneddividend / dividerusing fast integer divisionstatic longfloorDivideFast(long dividend, FastDivision.Magic divider) Computes floor division of the dividend by the divider using fast integer division returning (meaningful for signed operations)static FastDivision.Magicmagic32ForMultiplyMod(long divider) Computes magic for fast mulmod operation.static FastDivision.MagicmagicSigned(long d) Computes magic for fast signed integer division.static FastDivision.MagicmagicSigned(long d, boolean branchfree) Computes magic for fast integer division.static FastDivision.MagicmagicUnsigned(long d) Computes magic for fast unsigned integer division.static FastDivision.MagicmagicUnsigned(long d, boolean branchfree) Computes magic for fast unsigned integer division.static longmodSignedFast(long dividend, FastDivision.Magic divider) Calculates the modulus using fast integer divisionstatic longmodUnsignedFast(long dividend, FastDivision.Magic divider) Calculates the modulus using fast integer divisionstatic longmultiplyHighSigned(long x, long y) Returns highest 64 bits of (signed) long multiplication.static longmultiplyHighUnsigned(long x, long y) Returns highest 64 bits of (unsigned) long multiplication.static longmultiplyLow(long x, long y) Returns lowest 64 bits of either signed or unsigned long multiplication.static longmultiplyMod128Unsigned(long a, long b, long divider, FastDivision.Magic magic32) Returns unsigned(a*b)%dividerstatic longmultiplyMod128Unsigned0(long high, long low, long divider, FastDivision.Magic magic32) Returns unsigned(low|(high<<64))%dividerstatic longremainderSignedFast(long dividend, FastDivision.Magic divider) Calculates the remainder using fast integer divisionstatic longremainderUnsignedFast(long dividend, FastDivision.Magic divider) Calculates the remainder using fast integer division
-
Constructor Details
-
FastDivision
private FastDivision()
-
-
Method Details
-
multiplyHighSigned
public static long multiplyHighSigned(long x, long y) Returns highest 64 bits of (signed) long multiplication.- Parameters:
x- the numbery- the number- Returns:
- highest 64 bits of (signed) long multiplication.
-
multiplyHighUnsigned
public static long multiplyHighUnsigned(long x, long y) Returns highest 64 bits of (unsigned) long multiplication.- Parameters:
x- the numbery- the number- Returns:
- highest 64 bits of (unsigned) long multiplication.
-
multiplyLow
public static long multiplyLow(long x, long y) Returns lowest 64 bits of either signed or unsigned long multiplication.- Parameters:
x- the numbery- the number- Returns:
- lowest 64 bits of long multiplication.
-
divideAndRemainder128
public static long[] divideAndRemainder128(long u1, long u0, long v) Return's quotient and remainder of 128 bit integer division by 64 bit integer.Code taken from Hacker's Delight: http://www.hackersdelight.org/HDcode/divlu.c.
- Parameters:
u1- highest 64 dividend bitsu0- lowest 64 dividend bitsv- the divider- Returns:
- {quotient, remainder}
-
magicUnsigned
Computes magic for fast unsigned integer division.- Parameters:
d- the divider- Returns:
- the magic
-
magicUnsigned
Computes magic for fast unsigned integer division.- Parameters:
d- the dividerbranchfree- branching free- Returns:
- the magic
-
divideUnsignedFast
Returns unsigneddividend / dividerusing fast integer division- Parameters:
dividend- the dividenddivider- the divider- Returns:
dividend / divider
-
magicSigned
Computes magic for fast signed integer division.- Parameters:
d- the divider- Returns:
- the magic
-
magicSigned
Computes magic for fast integer division.- Parameters:
d- the dividerbranchfree- use branch free computation- Returns:
- the magic
-
divideSignedFast
Returns signeddividend / dividerusing fast integer division- Parameters:
dividend- the dividenddivider- the divider- Returns:
dividend / divider
-
remainderSignedFast
Calculates the remainder using fast integer division- Parameters:
dividend- the dividenddivider- the divider- Returns:
dividend % divider
-
remainderUnsignedFast
Calculates the remainder using fast integer division- Parameters:
dividend- the dividenddivider- the divider- Returns:
dividend % divider
-
floorDivideFast
Computes floor division of the dividend by the divider using fast integer division returning (meaningful for signed operations)- Parameters:
dividend- the dividenddivider- the divider- Returns:
dividend / divider- See Also:
-
modSignedFast
Calculates the modulus using fast integer division- Parameters:
dividend- the dividenddivider- the divider- Returns:
dividend % divider
-
modUnsignedFast
Calculates the modulus using fast integer division- Parameters:
dividend- the dividenddivider- the divider- Returns:
dividend % divider
-
magic32ForMultiplyMod
Computes magic for fast mulmod operation.- Parameters:
divider- the divider (must be positive)- Returns:
- the magic
-
multiplyMod128Unsigned
Returns unsigned(a*b)%divider- Parameters:
a- the first multiplierb- the second multiplierdivider- the dividermagic32- magic for fast divisionmagic32ForMultiplyMod(long)- Returns:
(a*b)%divider
-
multiplyMod128Unsigned0
public static long multiplyMod128Unsigned0(long high, long low, long divider, FastDivision.Magic magic32) Returns unsigned(low|(high<<64))%divider- Parameters:
high- the highest bitslow- the lowest bitsdivider- the dividermagic32- magic for fast divisionmagic32ForMultiplyMod(long)- Returns:
(low|(high<<64))%divider
-