Example usage for java.lang Integer highestOneBit

List of usage examples for java.lang Integer highestOneBit

Introduction

In this page you can find the example usage for java.lang Integer highestOneBit.

Prototype

public static int highestOneBit(int i) 

Source Link

Document

Returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value.

Usage

From source file:org.apache.fontbox.ttf.TTFSubFont.java

/**
 * @param dos The data output stream.//  w w  w  . j a v  a  2  s.  co  m
 * @param nTables The number of table.
 * @return The file offset of the first TTF table to write.
 * @throws IOException Upon errors.
 */
private static long writeFileHeader(DataOutputStream dos, int nTables) throws IOException {
    dos.writeInt(0x00010000);
    dos.writeShort(nTables);

    int mask = Integer.highestOneBit(nTables);
    int searchRange = mask * 16;
    dos.writeShort(searchRange);

    int entrySelector = log2i(mask);

    dos.writeShort(entrySelector);

    // numTables * 16 - searchRange
    int last = 16 * nTables - searchRange;
    dos.writeShort(last);

    return 0x00010000L + buildUint32(nTables, searchRange) + buildUint32(entrySelector, last);
}

From source file:lirmm.inria.fr.math.OpenLongToDoubleHashMap.java

/**
 * Find the smallest power of two greater than the input value
 *
 * @param i input value/*from   w  w  w  .ja  v  a2  s. c  o  m*/
 * @return smallest power of two greater than the input value
 */
private static int nextPowerOfTwo(final int i) {
    return Integer.highestOneBit(i) << 1;
}

From source file:org.apache.fontbox.ttf.TTFSubsetter.java

/**
 * @param out The data output stream./*  ww w  .ja  va  2 s  . c om*/
 * @param nTables The number of table.
 * @return The file offset of the first TTF table to write.
 * @throws IOException Upon errors.
 */
private long writeFileHeader(DataOutputStream out, int nTables) throws IOException {
    out.writeInt(0x00010000);
    out.writeShort(nTables);

    int mask = Integer.highestOneBit(nTables);
    int searchRange = mask * 16;
    out.writeShort(searchRange);

    int entrySelector = log2(mask);

    out.writeShort(entrySelector);

    // numTables * 16 - searchRange
    int last = 16 * nTables - searchRange;
    out.writeShort(last);

    return 0x00010000L + toUInt32(nTables, searchRange) + toUInt32(entrySelector, last);
}

From source file:org.apache.hadoop.hive.ql.exec.vector.mapjoin.VectorMapJoinRowBytesContainer.java

private void bufferedRead() throws IOException {

    // Reset for reading.
    readNextIndex = 0;//from  w  ww .  j a  v a 2s  .c o  m

    // Reset for filling.
    readNextCount = 0;

    if (readOffset < readLength) {
        // Move unprocessed remainder to beginning of buffer.
        int unprocessLength = readLength - readOffset;
        System.arraycopy(readBuffer, readOffset, readBuffer, 0, unprocessLength);

        int maxReadLength = readBuffer.length - unprocessLength;
        int partialReadLength = fileInputStream.read(readBuffer, unprocessLength, maxReadLength);
        if (partialReadLength == -1) {
            partialReadLength = 0;
        }
        totalReadLength += partialReadLength;
        readLength = unprocessLength + partialReadLength;
        readOffset = 0;
    } else {
        readOffset = 0;
        readLength = fileInputStream.read(readBuffer, 0, readBuffer.length);
        if (readLength == -1) {
            readLength = 0;
        }
        totalReadLength += readLength;
    }
    if (readLength == 0) {
        return;
    }
    if (readLength < 0) {
        throw new IOException("Negative read length");
    }

    // Get length word.
    if (readLength < 4) {
        throw new IOException("Expecting 4 byte length");
    }

    while (true) {
        // Use Input class to read length.
        int saveReadOffset = readOffset;
        int rowLength = readInt();
        if (rowLength < 0) {
            throw new IOException("Negative row length");
        }
        int remainingLength = readLength - readOffset;
        if (remainingLength < rowLength) {
            if (readNextCount > 0) {
                // Leave this one for the next round.
                readOffset = saveReadOffset;
                break;
            }

            // Buffer needed to bridge.
            if (largeRowBuffer == null || largeRowBuffer.length < rowLength) {
                int newLargeBufferLength = Math.max(Integer.highestOneBit(rowLength) << 1, INPUT_SIZE);
                largeRowBuffer = new byte[newLargeBufferLength];
            }
            System.arraycopy(readBuffer, readOffset, largeRowBuffer, 0, remainingLength);
            int expectedPartialLength = rowLength - remainingLength;
            int partialReadLength = fileInputStream.read(largeRowBuffer, remainingLength,
                    expectedPartialLength);
            if (partialReadLength == -1) {
                throw new IOException(
                        "Unexpected EOF (total write length " + totalWriteLength + ", total read length "
                                + totalReadLength + ", read length " + expectedPartialLength + ")");
            }

            if (expectedPartialLength != partialReadLength) {
                throw new IOException(
                        "Unable to read a complete row of length " + rowLength + " (total write length "
                                + totalWriteLength + ", total read length " + totalReadLength + ", read length "
                                + expectedPartialLength + ", actual length " + partialReadLength + ")");
            }
            totalReadLength += partialReadLength;

            readNextBytes[readNextCount] = largeRowBuffer;
            readNextOffsets[readNextCount] = 0;
            readNextLengths[readNextCount] = rowLength;

            // Indicate we used the last row's bytes for large buffer.
            readOffset = readLength;
            readNextCount++;
            break;
        }

        readNextBytes[readNextCount] = readBuffer;
        readNextOffsets[readNextCount] = readOffset;
        readNextLengths[readNextCount] = rowLength;
        readOffset += rowLength;
        readNextCount++;

        if (readNextCount >= readNextBytes.length) {
            break;
        }
        if (readLength - readOffset < 4) {
            // Handle in next round.
            break;
        }
    }
}

From source file:pku.sei.checkedcoverage.slicing.DirectSlicer.java

public Set<Instruction> getDynamicSlice(ThreadId threadId, List<SlicingCriterion> sc) {
    BackwardTraceIterator<InstructionInstance> backwardInsnItr = this.trace.getBackwardIterator(threadId, null);

    IntegerMap<Set<Instruction>> controlDependences = new IntegerMap<Set<Instruction>>();

    Set<Variable> interestingVariables = new HashSet<Variable>();
    Set<Instruction> dynamicSlice = new HashSet<Instruction>();

    long nextFrameNr = 0;
    int stackDepth = 0;

    List<ReadMethod> initialStackMethods = backwardInsnItr.getInitialStackMethods();

    int allocStack = initialStackMethods.size() + 1;
    allocStack = Integer.highestOneBit(allocStack) * 2;

    Instruction[] atCatchBlockStart = new Instruction[allocStack];
    boolean[] throwsException = new boolean[allocStack];
    boolean[] interruptedControlFlow = new boolean[allocStack];
    /**//www. ja  v  a  2 s .  c o m
     * <code>true</code> iff this frame was aborted abnormally (NOT by a RETURN
     * instruction)
     */
    boolean[] abnormalTermination = new boolean[allocStack];
    /**
     * is set to true if the methods entry label has been passed
     */
    boolean[] finished = new boolean[allocStack];
    int[] opStack = new int[allocStack];
    int[] minOpStack = new int[allocStack];
    long[] frames = new long[allocStack];
    Instruction[] lastInstruction = new Instruction[allocStack];
    @SuppressWarnings("unchecked")
    HashSet<Instruction>[] interestingInstructions = (HashSet<Instruction>[]) new HashSet<?>[allocStack];
    StackEntry[][] cachedStackEntries = new StackEntry[allocStack][];
    LocalVariable[][] cachedLocalVariables = new LocalVariable[allocStack][];
    ReadMethod[] method = new ReadMethod[allocStack];

    for (ReadMethod method0 : initialStackMethods) {
        ++stackDepth;
        method[stackDepth] = method0;
        interruptedControlFlow[stackDepth] = true;
        frames[stackDepth] = nextFrameNr++;
    }

    for (int i = 1; i < allocStack; ++i) {
        interestingInstructions[i] = new HashSet<Instruction>();
        cachedStackEntries[i] = new StackEntry[8];
        cachedLocalVariables[i] = new LocalVariable[8];
    }

    SimulationEnvironment simEnv = new SimulationEnvironment(frames, opStack, minOpStack, cachedStackEntries,
            cachedLocalVariables, throwsException, lastInstruction, method, interruptedControlFlow);

    List<SlicingCriterionInstance> slicingCriteria;
    if (sc.isEmpty())
        slicingCriteria = Collections.emptyList();
    else if (sc.size() == 1)
        slicingCriteria = Collections.singletonList(sc.get(0).getInstance());
    else {
        slicingCriteria = new ArrayList<SlicingCriterionInstance>(sc.size());
        for (SlicingCriterion crit : sc)
            slicingCriteria.add(crit.getInstance());
    }

    for (ProgressMonitor mon : this.progressMonitors)
        mon.start(backwardInsnItr);
    try {
        @SuppressWarnings("unchecked")
        Set<Variable>[] matchedCriterionVariables = (Set<Variable>[]) new Set<?>[8];

        while (backwardInsnItr.hasNext()) {
            InstructionInstance instance = backwardInsnItr.next();
            Instruction instruction = instance.getInstruction();

            int newStackDepth = instance.getStackDepth();
            assert newStackDepth > 0;

            simEnv.removedMethod = null;
            boolean reenter = false;
            if (newStackDepth != stackDepth
                    || (reenter = finished[stackDepth] || method[stackDepth] != instruction.getMethod())) {
                if (newStackDepth >= stackDepth) {
                    // in all steps, the stackDepth can change by at most 1 (except for the very first instruction)
                    assert newStackDepth == stackDepth + 1 || stackDepth == 0 || reenter;
                    if (newStackDepth >= atCatchBlockStart.length) {
                        int oldLen = atCatchBlockStart.length;
                        int newLen = oldLen == 0 ? 8 : 2 * oldLen;
                        atCatchBlockStart = Arrays.copyOf(atCatchBlockStart, newLen);
                        throwsException = Arrays.copyOf(throwsException, newLen);
                        interruptedControlFlow = Arrays.copyOf(interruptedControlFlow, newLen);
                        abnormalTermination = Arrays.copyOf(abnormalTermination, newLen);
                        finished = Arrays.copyOf(finished, newLen);
                        opStack = Arrays.copyOf(opStack, newLen);
                        minOpStack = Arrays.copyOf(minOpStack, newLen);
                        frames = Arrays.copyOf(frames, newLen);
                        interestingInstructions = Arrays.copyOf(interestingInstructions, newLen);
                        lastInstruction = Arrays.copyOf(lastInstruction, newLen);
                        cachedStackEntries = Arrays.copyOf(cachedStackEntries, newLen);
                        cachedLocalVariables = Arrays.copyOf(cachedLocalVariables, newLen);
                        method = Arrays.copyOf(method, newLen);
                        for (int i = oldLen; i < newLen; ++i) {
                            interestingInstructions[i] = new HashSet<Instruction>();
                            cachedStackEntries[i] = new StackEntry[8];
                            cachedLocalVariables[i] = new LocalVariable[8];
                        }
                        simEnv = new SimulationEnvironment(frames, opStack, minOpStack, cachedStackEntries,
                                cachedLocalVariables, throwsException, lastInstruction, method,
                                interruptedControlFlow);
                    }
                    frames[newStackDepth] = nextFrameNr++;
                    method[newStackDepth] = instruction.getMethod();

                    atCatchBlockStart[newStackDepth] = null;
                    if (instruction == method[newStackDepth].getAbnormalTerminationLabel()) {
                        throwsException[newStackDepth] = interruptedControlFlow[newStackDepth] = abnormalTermination[newStackDepth] = true;
                        interruptedControlFlow[stackDepth] = true;
                    } else {
                        throwsException[newStackDepth] = interruptedControlFlow[newStackDepth] = abnormalTermination[newStackDepth] = false;
                    }
                    finished[newStackDepth] = false;
                    opStack[newStackDepth] = 0;
                    minOpStack[newStackDepth] = 0;
                    interestingInstructions[newStackDepth].clear();
                    if (cachedLocalVariables[newStackDepth].length > 128)
                        cachedLocalVariables[newStackDepth] = new LocalVariable[8];
                    else
                        Arrays.fill(cachedLocalVariables[newStackDepth], null);
                    if (cachedStackEntries[newStackDepth].length > 128)
                        cachedStackEntries[newStackDepth] = new StackEntry[8];
                    else
                        Arrays.fill(cachedStackEntries[newStackDepth], null);
                } else {
                    assert newStackDepth == stackDepth - 1;
                    simEnv.removedMethod = method[stackDepth];
                }
            }
            stackDepth = newStackDepth;

            if (atCatchBlockStart[stackDepth] != null)
                throwsException[stackDepth] = true;

            if (instruction == instruction.getMethod().getMethodEntryLabel())
                finished[stackDepth] = true;
            lastInstruction[stackDepth] = instruction;

            DynamicInformation dynInfo = this.simulator.simulateInstruction(instance, simEnv);

            if (simEnv.removedMethod != null && !interestingInstructions[stackDepth + 1].isEmpty()) {
                // ok, we have a control dependence since the method was called by (or for) this instruction
                // checking if this is the instr. that directly called the method is impossible
                dynamicSlice.add(instruction);
                interestingInstructions[stackDepth].add(instruction);
            }

            if (matchedCriterionVariables.length <= stackDepth) {
                @SuppressWarnings("unchecked")
                Set<Variable>[] newMatchedCriterionVariables = (Set<Variable>[]) new Set<?>[2
                        * Math.max(stackDepth, matchedCriterionVariables.length)];
                System.arraycopy(matchedCriterionVariables, 0, newMatchedCriterionVariables, 0,
                        matchedCriterionVariables.length);
                matchedCriterionVariables = newMatchedCriterionVariables;
            }
            for (SlicingCriterionInstance crit : slicingCriteria) {
                if (crit.matches(instance)) {
                    if (matchedCriterionVariables[stackDepth] == null)
                        matchedCriterionVariables[stackDepth] = new HashSet<Variable>();
                    if (crit.matchAllData()) {
                        matchedCriterionVariables[stackDepth].removeAll(dynInfo.getDefinedVariables());
                        matchedCriterionVariables[stackDepth].addAll(dynInfo.getUsedVariables());
                    } else if (crit.hasLocalVariables()) {
                        for (pku.sei.checkedcoverage.common.classRepresentation.LocalVariable var : crit
                                .getLocalVariables())
                            interestingVariables.add(simEnv.getLocalVariable(stackDepth, var.getIndex()));
                    } else {
                        interestingInstructions[stackDepth].add(instance.getInstruction());
                        dynamicSlice.add(instruction);
                    }
                } else if (matchedCriterionVariables[stackDepth] != null) {
                    interestingVariables.addAll(matchedCriterionVariables[stackDepth]);
                    matchedCriterionVariables[stackDepth] = null;
                }
            }

            boolean isExceptionsThrowingInstance = throwsException[stackDepth]
                    && (instruction.getType() != InstructionType.LABEL
                            || !((LabelMarker) instruction).isAdditionalLabel())
                    && (instruction.getOpcode() != Opcodes.GOTO);
            if (!interestingInstructions[stackDepth].isEmpty() || isExceptionsThrowingInstance) {
                Set<Instruction> instrControlDependences = controlDependences.get(instruction.getIndex());
                if (instrControlDependences == null) {
                    computeControlDependences(instruction.getMethod(), controlDependences);
                    instrControlDependences = controlDependences.get(instruction.getIndex());
                    assert instrControlDependences != null;
                }
                // get all interesting instructions, that are dependent on the current one
                Set<Instruction> dependantInterestingInstructions = intersect(instrControlDependences,
                        interestingInstructions[stackDepth]);
                if (isExceptionsThrowingInstance) {
                    throwsException[stackDepth] = false;
                    // in this case, we have an additional control dependence from the catching to
                    // the throwing instruction, and a data dependence on the thrown instruction
                    for (int i = stackDepth; i > 0; --i) {
                        if (atCatchBlockStart[i] != null) {
                            if (interestingInstructions[i].contains(atCatchBlockStart[i])) {
                                if (dependantInterestingInstructions.isEmpty())
                                    dependantInterestingInstructions = Collections
                                            .singleton(atCatchBlockStart[i]);
                                else
                                    dependantInterestingInstructions.add(atCatchBlockStart[i]);
                            }
                            atCatchBlockStart[i] = null;

                            // data dependence:
                            // (the stack height has already been decremented when entering the catch block)
                            Variable definedException = simEnv.getOpStackEntry(i, opStack[i]);
                            if (interestingVariables.contains(definedException)) {
                                interestingInstructions[stackDepth].add(instruction);
                                dynamicSlice.add(instruction);
                                interestingVariables.remove(definedException);
                                interestingVariables.addAll(dynInfo.getUsedVariables());
                            }

                            break;
                        }
                    }
                }
                if (!dependantInterestingInstructions.isEmpty()) {
                    dynamicSlice.add(instruction);
                    interestingInstructions[stackDepth].removeAll(dependantInterestingInstructions);
                    interestingInstructions[stackDepth].add(instruction);
                    interestingVariables.addAll(dynInfo.getUsedVariables());
                }
            }

            if (!interestingVariables.isEmpty()) {
                for (Variable definedVariable : dynInfo.getDefinedVariables()) {
                    if (interestingVariables.contains(definedVariable)) {
                        interestingInstructions[stackDepth].add(instruction);
                        dynamicSlice.add(instruction);
                        interestingVariables.remove(definedVariable);
                        interestingVariables.addAll(dynInfo.getUsedVariables(definedVariable));
                    }
                }
            }

            if (dynInfo.isCatchBlock()) {
                atCatchBlockStart[stackDepth] = instance.getInstruction();
                interruptedControlFlow[stackDepth] = true;
            } else if (atCatchBlockStart[stackDepth] != null) {
                atCatchBlockStart[stackDepth] = null;
            }

        }
    } finally {
        for (ProgressMonitor mon : this.progressMonitors)
            mon.end();
    }

    for (Iterator<Instruction> it = dynamicSlice.iterator(); it.hasNext();) {
        Instruction instr = it.next();
        if (instr.getType() == InstructionType.LABEL || instr.getOpcode() == Opcodes.GOTO)
            it.remove();
    }

    return dynamicSlice;
}

From source file:de.unisb.cs.st.javaslicer.slicing.DirectSlicer.java

public Set<Instruction> getDynamicSlice(ThreadId threadId, List<SlicingCriterion> sc) {
    BackwardTraceIterator<InstructionInstance> backwardInsnItr = this.trace.getBackwardIterator(threadId, null);

    IntegerMap<Set<Instruction>> controlDependences = new IntegerMap<Set<Instruction>>();

    Set<Variable> interestingVariables = new HashSet<Variable>();
    Set<Instruction> dynamicSlice = new HashSet<Instruction>();

    long nextFrameNr = 0;
    int stackDepth = 0;

    List<ReadMethod> initialStackMethods = backwardInsnItr.getInitialStackMethods();

    int allocStack = initialStackMethods.size() + 1;
    allocStack = Integer.highestOneBit(allocStack) * 2;

    Instruction[] atCatchBlockStart = new Instruction[allocStack];
    boolean[] throwsException = new boolean[allocStack];
    boolean[] interruptedControlFlow = new boolean[allocStack];
    /**/*ww  w  .jav a 2s.c o  m*/
     * <code>true</code> iff this frame was aborted abnormally (NOT by a RETURN
     * instruction)
     */
    boolean[] abnormalTermination = new boolean[allocStack];
    /**
     * is set to true if the methods entry label has been passed
     */
    boolean[] finished = new boolean[allocStack];
    int[] opStack = new int[allocStack];
    int[] minOpStack = new int[allocStack];
    long[] frames = new long[allocStack];
    Instruction[] lastInstruction = new Instruction[allocStack];
    @SuppressWarnings("unchecked")
    HashSet<Instruction>[] interestingInstructions = (HashSet<Instruction>[]) new HashSet<?>[allocStack];
    StackEntry[][] cachedStackEntries = new StackEntry[allocStack][];
    LocalVariable[][] cachedLocalVariables = new LocalVariable[allocStack][];
    ReadMethod[] method = new ReadMethod[allocStack];

    for (ReadMethod method0 : initialStackMethods) {
        ++stackDepth;
        method[stackDepth] = method0;
        interruptedControlFlow[stackDepth] = true;
        frames[stackDepth] = nextFrameNr++;
    }

    for (int i = 1; i < allocStack; ++i) {
        interestingInstructions[i] = new HashSet<Instruction>();
        cachedStackEntries[i] = new StackEntry[8];
        cachedLocalVariables[i] = new LocalVariable[8];
    }

    SimulationEnvironment simEnv = new SimulationEnvironment(frames, opStack, minOpStack, cachedStackEntries,
            cachedLocalVariables, throwsException, lastInstruction, method, interruptedControlFlow);

    List<SlicingCriterionInstance> slicingCriteria;
    if (sc.isEmpty())
        slicingCriteria = Collections.emptyList();
    else if (sc.size() == 1)
        slicingCriteria = Collections.singletonList(sc.get(0).getInstance());
    else {
        slicingCriteria = new ArrayList<SlicingCriterionInstance>(sc.size());
        for (SlicingCriterion crit : sc)
            slicingCriteria.add(crit.getInstance());
    }

    for (ProgressMonitor mon : this.progressMonitors)
        mon.start(backwardInsnItr);
    try {
        @SuppressWarnings("unchecked")
        Set<Variable>[] matchedCriterionVariables = (Set<Variable>[]) new Set<?>[8];

        while (backwardInsnItr.hasNext()) {
            InstructionInstance instance = backwardInsnItr.next();
            Instruction instruction = instance.getInstruction();

            int newStackDepth = instance.getStackDepth();
            assert newStackDepth > 0;

            simEnv.removedMethod = null;
            boolean reenter = false;
            if (newStackDepth != stackDepth
                    || (reenter = finished[stackDepth] || method[stackDepth] != instruction.getMethod())) {
                if (newStackDepth >= stackDepth) {
                    // in all steps, the stackDepth can change by at most 1 (except for the very first instruction)
                    assert newStackDepth == stackDepth + 1 || stackDepth == 0 || reenter;
                    if (newStackDepth >= atCatchBlockStart.length) {
                        int oldLen = atCatchBlockStart.length;
                        int newLen = oldLen == 0 ? 8 : 2 * oldLen;
                        atCatchBlockStart = Arrays.copyOf(atCatchBlockStart, newLen);
                        throwsException = Arrays.copyOf(throwsException, newLen);
                        interruptedControlFlow = Arrays.copyOf(interruptedControlFlow, newLen);
                        abnormalTermination = Arrays.copyOf(abnormalTermination, newLen);
                        finished = Arrays.copyOf(finished, newLen);
                        opStack = Arrays.copyOf(opStack, newLen);
                        minOpStack = Arrays.copyOf(minOpStack, newLen);
                        frames = Arrays.copyOf(frames, newLen);
                        interestingInstructions = Arrays.copyOf(interestingInstructions, newLen);
                        lastInstruction = Arrays.copyOf(lastInstruction, newLen);
                        cachedStackEntries = Arrays.copyOf(cachedStackEntries, newLen);
                        cachedLocalVariables = Arrays.copyOf(cachedLocalVariables, newLen);
                        method = Arrays.copyOf(method, newLen);
                        for (int i = oldLen; i < newLen; ++i) {
                            interestingInstructions[i] = new HashSet<Instruction>();
                            cachedStackEntries[i] = new StackEntry[8];
                            cachedLocalVariables[i] = new LocalVariable[8];
                        }
                        simEnv = new SimulationEnvironment(frames, opStack, minOpStack, cachedStackEntries,
                                cachedLocalVariables, throwsException, lastInstruction, method,
                                interruptedControlFlow);
                    }
                    frames[newStackDepth] = nextFrameNr++;
                    method[newStackDepth] = instruction.getMethod();

                    atCatchBlockStart[newStackDepth] = null;
                    if (instruction == method[newStackDepth].getAbnormalTerminationLabel()) {
                        throwsException[newStackDepth] = interruptedControlFlow[newStackDepth] = abnormalTermination[newStackDepth] = true;
                        interruptedControlFlow[stackDepth] = true;
                    } else {
                        throwsException[newStackDepth] = interruptedControlFlow[newStackDepth] = abnormalTermination[newStackDepth] = false;
                    }
                    finished[newStackDepth] = false;
                    opStack[newStackDepth] = 0;
                    minOpStack[newStackDepth] = 0;
                    interestingInstructions[newStackDepth].clear();
                    if (cachedLocalVariables[newStackDepth].length > 128)
                        cachedLocalVariables[newStackDepth] = new LocalVariable[8];
                    else
                        Arrays.fill(cachedLocalVariables[newStackDepth], null);
                    if (cachedStackEntries[newStackDepth].length > 128)
                        cachedStackEntries[newStackDepth] = new StackEntry[8];
                    else
                        Arrays.fill(cachedStackEntries[newStackDepth], null);
                } else {
                    assert newStackDepth == stackDepth - 1;
                    simEnv.removedMethod = method[stackDepth];
                }
            }
            stackDepth = newStackDepth;

            if (atCatchBlockStart[stackDepth] != null)
                throwsException[stackDepth] = true;

            if (instruction == instruction.getMethod().getMethodEntryLabel())
                finished[stackDepth] = true;
            lastInstruction[stackDepth] = instruction;

            DynamicInformation dynInfo = this.simulator.simulateInstruction(instance, simEnv);

            if (simEnv.removedMethod != null && !interestingInstructions[stackDepth + 1].isEmpty()) {
                // ok, we have a control dependence since the method was called by (or for) this instruction
                // checking if this is the instr. that directly called the method is impossible
                dynamicSlice.add(instruction);
                interestingInstructions[stackDepth].add(instruction);
            }

            if (matchedCriterionVariables.length <= stackDepth)
                matchedCriterionVariables = Arrays.copyOf(matchedCriterionVariables,
                        2 * Math.max(stackDepth, matchedCriterionVariables.length));
            for (SlicingCriterionInstance crit : slicingCriteria) {
                if (crit.matches(instance)) {
                    if (matchedCriterionVariables[stackDepth] == null)
                        matchedCriterionVariables[stackDepth] = new HashSet<Variable>();
                    if (crit.matchAllData()) {
                        matchedCriterionVariables[stackDepth].removeAll(dynInfo.getDefinedVariables());
                        matchedCriterionVariables[stackDepth].addAll(dynInfo.getUsedVariables());
                        dynamicSlice.add(instruction);
                        interestingInstructions[stackDepth].add(instance.getInstruction());
                    } else if (crit.hasLocalVariables()) {
                        for (de.unisb.cs.st.javaslicer.common.classRepresentation.LocalVariable var : crit
                                .getLocalVariables())
                            interestingVariables.add(simEnv.getLocalVariable(stackDepth, var.getIndex()));
                    } else {
                        interestingInstructions[stackDepth].add(instance.getInstruction());
                        dynamicSlice.add(instruction);
                    }
                } else if (matchedCriterionVariables[stackDepth] != null) {
                    interestingVariables.addAll(matchedCriterionVariables[stackDepth]);
                    matchedCriterionVariables[stackDepth] = null;
                }
            }

            boolean isExceptionsThrowingInstance = throwsException[stackDepth]
                    && (instruction.getType() != InstructionType.LABEL
                            || !((LabelMarker) instruction).isAdditionalLabel())
                    && (instruction.getOpcode() != Opcodes.GOTO);
            if (!interestingInstructions[stackDepth].isEmpty() || isExceptionsThrowingInstance) {
                Set<Instruction> instrControlDependences = controlDependences.get(instruction.getIndex());
                if (instrControlDependences == null) {
                    computeControlDependences(instruction.getMethod(), controlDependences);
                    instrControlDependences = controlDependences.get(instruction.getIndex());
                    assert instrControlDependences != null;
                }
                // get all interesting instructions, that are dependent on the current one
                Set<Instruction> dependantInterestingInstructions = intersect(instrControlDependences,
                        interestingInstructions[stackDepth]);
                if (isExceptionsThrowingInstance) {
                    throwsException[stackDepth] = false;
                    // in this case, we have an additional control dependence from the catching to
                    // the throwing instruction, and a data dependence on the thrown instruction
                    for (int i = stackDepth; i > 0; --i) {
                        if (atCatchBlockStart[i] != null) {
                            if (interestingInstructions[i].contains(atCatchBlockStart[i])) {
                                if (dependantInterestingInstructions.isEmpty())
                                    dependantInterestingInstructions = Collections
                                            .singleton(atCatchBlockStart[i]);
                                else
                                    dependantInterestingInstructions.add(atCatchBlockStart[i]);
                            }
                            atCatchBlockStart[i] = null;

                            // data dependence:
                            // (the stack height has already been decremented when entering the catch block)
                            Variable definedException = simEnv.getOpStackEntry(i, opStack[i]);
                            if (interestingVariables.contains(definedException)) {
                                interestingInstructions[stackDepth].add(instruction);
                                dynamicSlice.add(instruction);
                                interestingVariables.remove(definedException);
                                interestingVariables.addAll(dynInfo.getUsedVariables());
                            }

                            break;
                        }
                    }
                }
                if (!dependantInterestingInstructions.isEmpty()) {
                    dynamicSlice.add(instruction);
                    interestingInstructions[stackDepth].removeAll(dependantInterestingInstructions);
                    interestingInstructions[stackDepth].add(instruction);
                    interestingVariables.addAll(dynInfo.getUsedVariables());
                }
            }

            if (!interestingVariables.isEmpty()) {
                for (Variable definedVariable : dynInfo.getDefinedVariables()) {
                    if (interestingVariables.contains(definedVariable)) {
                        interestingInstructions[stackDepth].add(instruction);
                        dynamicSlice.add(instruction);
                        interestingVariables.remove(definedVariable);
                        interestingVariables.addAll(dynInfo.getUsedVariables(definedVariable));
                    }
                }
            }

            if (dynInfo.isCatchBlock()) {
                atCatchBlockStart[stackDepth] = instance.getInstruction();
                interruptedControlFlow[stackDepth] = true;
            } else if (atCatchBlockStart[stackDepth] != null) {
                atCatchBlockStart[stackDepth] = null;
            }

        }
    } finally {
        for (ProgressMonitor mon : this.progressMonitors)
            mon.end();
    }

    for (Iterator<Instruction> it = dynamicSlice.iterator(); it.hasNext();) {
        Instruction instr = it.next();
        if (instr.getType() == InstructionType.LABEL || instr.getOpcode() == Opcodes.GOTO)
            it.remove();
    }

    return dynamicSlice;
}

From source file:org.bigtextml.topics.ParallelTopicModel.java

public ParallelTopicModel(BigLabelAlphabet topicAlphabet, double alphaSum, double beta) {

    //this.id=System.currentTimeMillis();
    //this.data = new ArrayList<TopicAssignment>();
    //this.data = CacheManagementServices.cacheManager.getCache("topicass");
    this.data = (TopicAssignmentBigMap) ManagementServices.getBigMap("TopicAssignment");
    this.topicAlphabet = topicAlphabet;
    this.numTopics = topicAlphabet.size();

    if (Integer.bitCount(numTopics) == 1) {
        // exact power of 2
        topicMask = numTopics - 1;//w  w  w . java2  s.  c  o m
        topicBits = Integer.bitCount(topicMask);
    } else {
        // otherwise add an extra bit
        topicMask = Integer.highestOneBit(numTopics) * 2 - 1;
        topicBits = Integer.bitCount(topicMask);
    }

    this.alphaSum = alphaSum;
    this.alpha = new double[numTopics];
    Arrays.fill(alpha, alphaSum / numTopics);
    this.beta = beta;

    tokensPerTopic = new int[numTopics];

    formatter = NumberFormat.getInstance();
    formatter.setMaximumFractionDigits(5);

    logger.info("Coded LDA: " + numTopics + " topics, " + topicBits + " topic bits, "
            + Integer.toBinaryString(topicMask) + " topic mask");
}

From source file:om.sstvencoder.CropView.java

private int getSampleSize() {
    int sx = Math.round(mInputRect.width() / mModeSize.getWidth());
    int sy = Math.round(mInputRect.height() / mModeSize.getHeight());
    int scale = Math.max(1, Math.max(sx, sy));
    return Integer.highestOneBit(scale);
}

From source file:com.android.camera.manager.ThumbnailViewManager.java

private Thumbnail createThumbnailWithYuv(byte[] yuvData, int thumbnailWidth, int yuvWidth, int yuvHeight,
        int orientation, int imageFormat) {
    Thumbnail thumb = null;//from   w  w  w. j a  va2  s  . c  o  m
    if (yuvData != null) {
        Log.d(TAG, "[createThumbnailWithYuv]...");
        // Create a thumbnail whose width is equal or bigger than
        // that of the thumbnail view.
        int ratio = (int) Math.ceil((double) yuvWidth / thumbnailWidth);
        int inSampleSize = Integer.highestOneBit(ratio);
        thumb = Thumbnail.createThumbnail(covertYuvDataToJpeg(yuvData, yuvWidth, yuvHeight, imageFormat),
                orientation, inSampleSize, null, null);
        Log.i(TAG, "[createThumbnailWithYuv] end");
    }
    return thumb;
}

From source file:org.apache.drill.exec.physical.impl.common.HashTableTemplate.java

private static int roundUpToPowerOf2(int number) {
    int rounded = number >= MAXIMUM_CAPACITY ? MAXIMUM_CAPACITY
            : (rounded = Integer.highestOneBit(number)) != 0
                    ? (Integer.bitCount(number) > 1) ? rounded << 1 : rounded
                    : 1;//from  w  ww.  jav a2  s  . co  m

    return rounded;
}