public static long remainderUnsignedLong(long dividend, long divisor) {
    if (divisor < 0L) {

      // because divisor is negative, quotient is at most 1.
      // remainder must be dividend itself (quotient=0), or dividend -
      // divisor
      return (compareUnsignedLong(dividend, divisor)) < 0 ? dividend : dividend
          - divisor;
    }

    if (dividend >= 0L) { // signed comparisons
      return dividend % divisor;
    } else {
      // same above
      long quotient = ((dividend >>> 1L) / divisor) << 1L;
      long remainder = dividend - quotient * divisor;
      if (compareUnsignedLong(remainder, divisor) >= 0) {
        return remainder - divisor;
      }
      return remainder;
    }
  }
If we can assume JDK 1.8, this should use
java.lang.Long.remainderUnsigned(), which will be replaced with intrinsics
in JVM.

@param dividend
         the value to be divided
@param divisor
         the value doing the dividing
@return the unsigned remainder of the first argument divided by the second
        argument
@see "http://hg.openjdk.java.net/jdk8/tl/jdk/rev/71200c517524"
common/src/java/org/apache/hadoop/hive/common/type/SqlMathUtil.java