public static long UInt64Remainder(long dividend, long divisor) { //From google-guava if (divisor < 0) // i.e., divisor >= 2^63 { if (compare(dividend, divisor) < 0) { return(dividend); // dividend < divisor } else { return(dividend - divisor); // dividend >= divisor } } // Optimization - use signed modulus if dividend < 2^63 if (dividend >= 0) { return(dividend % divisor); } /* * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is * guaranteed to be either exact or one less than the correct value. This follows from fact * that floor(floor(x)/i) == floor(x/i) for any real x and integer i != 0. The proof is not * quite trivial. */ long quotiend = (Intrinsics.lushr(dividend, 1) / divisor) << 1; long rem = dividend - quotiend * divisor; return(rem - (compare(rem, divisor) >= 0 ? divisor : 0L)); }