Example usage for com.google.common.primitives UnsignedLongs divide

List of usage examples for com.google.common.primitives UnsignedLongs divide

Introduction

In this page you can find the example usage for com.google.common.primitives UnsignedLongs divide.

Prototype

@CheckReturnValue
public static long divide(long dividend, long divisor) 

Source Link

Document

Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64-bit quantities.

Usage

From source file:org.sosy_lab.cpachecker.cpa.value.AbstractExpressionValueVisitor.java

/**
 * Calculate an arithmetic operation on two integer types.
 *
 * @param l left hand side value//from w  w w  .ja  v  a  2 s.  com
 * @param r right hand side value
 * @param op the binary operator
 * @param calculationType The type the result of the calculation should have
 * @param machineModel the machine model
 * @param logger logging
 * @return the resulting value
 */
private static long arithmeticOperation(final long l, final long r, final BinaryOperator op,
        final CType calculationType, final MachineModel machineModel, final LogManager logger) {

    // special handling for UNSIGNED_LONGLONG (32 and 64bit), UNSIGNED_LONG (64bit)
    // because Java only has SIGNED_LONGLONG
    CSimpleType st = getArithmeticType(calculationType);
    if (st != null) {
        if (machineModel.getSizeofInBits(st) >= SIZE_OF_JAVA_LONG && st.isUnsigned()) {
            switch (op) {
            case DIVIDE:
                if (r == 0) {
                    logger.logf(Level.SEVERE, "Division by Zero (%d / %d)", l, r);
                    return 0;
                }
                return UnsignedLongs.divide(l, r);
            case MODULO:
                return UnsignedLongs.remainder(l, r);
            case SHIFT_RIGHT:
                /*
                 * from http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
                 *
                 * The unsigned right shift operator ">>>" shifts a zero
                 * into the leftmost position, while the leftmost position
                 * after ">>" depends on sign extension.
                 */
                return l >>> r;
            default:
                // fall-through, calculation is done correct as SINGED_LONG_LONG
            }
        }
    }

    switch (op) {
    case PLUS:
        return l + r;
    case MINUS:
        return l - r;
    case DIVIDE:
        if (r == 0) {
            logger.logf(Level.SEVERE, "Division by Zero (%d / %d)", l, r);
            return 0;
        }
        return l / r;
    case MODULO:
        return l % r;
    case MULTIPLY:
        return l * r;
    case SHIFT_LEFT:
        /* There is a difference in the SHIFT-operation in Java and C.
         * In C a SHIFT is a normal SHIFT, in Java the rVal is used as (r%64).
         *
         * http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19
         *
         * If the promoted type of the left-hand operand is long, then only the
         * six lowest-order bits of the right-hand operand are used as the
         * shift distance. It is as if the right-hand operand were subjected to
         * a bitwise logical AND operator & (15.22.1) with the mask value 0x3f.
         * The shift distance actually used is therefore always in the range 0 to 63.
         */
        return (r >= SIZE_OF_JAVA_LONG) ? 0 : l << r;
    case SHIFT_RIGHT:
        return l >> r;
    case BINARY_AND:
        return l & r;
    case BINARY_OR:
        return l | r;
    case BINARY_XOR:
        return l ^ r;

    default:
        throw new AssertionError("unknown binary operation: " + op);
    }

}