/** * Find the next larger positive power of two value up from the given value. If value is a power of two then * this value will be returned. * * @param value from which next positive power of two will be found. * @return the next positive power of 2 or this value if it is a power of 2. */ public static int roundToPowerOfTwo(final int value) { return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); }
private static int log2(int val) { // compute the (0-based, with lsb = 0) position of highest set bit i.e, log2 return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val); }
/** * Fast method of finding the next power of 2 greater than or equal to the supplied value. * * <p>If the value is {@code <= 0} then 1 will be returned. * This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30. * * @param value from which to search for next power of 2 * @return The next power of 2 or the value itself if it is a power of 2 */ public static int findNextPositivePowerOfTwo(final int value) { assert value > Integer.MIN_VALUE && value < 0x40000000; return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); }
/** * Calculate the next power of 2, greater than or equal to x.<p> * From Hacker's Delight, Chapter 3, Harry S. Warren Jr. * * @param x Value to round up * @return The next power of 2 from x inclusive */ public static int ceilingNextPowerOfTwo(final int x) { return 1 << (32 - Integer.numberOfLeadingZeros(x - 1)); }
private static int encodedLengthSize(int length) { if (length < 128) { return 1; } int numberOfBits = 32 - Integer.numberOfLeadingZeros(length); int numberOfBytes = (numberOfBits + 7) / 8; return numberOfBytes + 1; }
/** * Find the next larger positive power of two value up from the given value. If value is a power of two then * this value will be returned. * * @param value from which next positive power of two will be found. * @return the next positive power of 2 or this value if it is a power of 2. */ public static int roundToPowerOfTwo(final int value) { return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); }
public static int getBitSize(long value) { if (value > Integer.MAX_VALUE) { return 64 - Integer.numberOfLeadingZeros((int) (value >> 32)); } else { return 32 - Integer.numberOfLeadingZeros((int) value); } }
int computeListIndex( int len ) { int powIndex = 32-Integer.numberOfLeadingZeros(len-1); return powIndex; }
/** * Fast method of finding the next power of 2 greater than or equal to the supplied value. * * <p>If the value is {@code <= 0} then 1 will be returned. * This method is not suitable for {@link Integer#MIN_VALUE} or numbers greater than 2^30. * * @param value from which to search for next power of 2 * @return The next power of 2 or the value itself if it is a power of 2 */ public static int findNextPositivePowerOfTwo(final int value) { assert value > Integer.MIN_VALUE && value < 0x40000000; return 1 << (32 - Integer.numberOfLeadingZeros(value - 1)); }
private static int log2(int val) { // compute the (0-based, with lsb = 0) position of highest set bit i.e, log2 return INTEGER_SIZE_MINUS_ONE - Integer.numberOfLeadingZeros(val); }
int computeLen( int len ) { int powIndex = 32-Integer.numberOfLeadingZeros(len-1); return 1<<powIndex; }
private static int validateAndCalculatePageShifts(int pageSize) { if (pageSize < MIN_PAGE_SIZE) { throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")"); } if ((pageSize & pageSize - 1) != 0) { throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)"); } // Logarithm base 2. At this point we know that pageSize is a power of two. return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(pageSize); }
/** * Calculates compression level on the basis of block size. */ private static int compressionLevel(int blockSize) { if (blockSize < MIN_BLOCK_SIZE || blockSize > MAX_BLOCK_SIZE) { throw new IllegalArgumentException(String.format( "blockSize: %d (expected: %d-%d)", blockSize, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE)); } int compressionLevel = 32 - Integer.numberOfLeadingZeros(blockSize - 1); // ceil of log2 compressionLevel = Math.max(0, compressionLevel - COMPRESSION_LEVEL_BASE); return compressionLevel; }
private static int validateAndCalculatePageShifts(int pageSize) { if (pageSize < MIN_PAGE_SIZE) { throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: " + MIN_PAGE_SIZE + ")"); } if ((pageSize & pageSize - 1) != 0) { throw new IllegalArgumentException("pageSize: " + pageSize + " (expected: power of 2)"); } // Logarithm base 2. At this point we know that pageSize is a power of two. return Integer.SIZE - 1 - Integer.numberOfLeadingZeros(pageSize); }
/** * Calculates compression level on the basis of block size. */ private static int compressionLevel(int blockSize) { if (blockSize < MIN_BLOCK_SIZE || blockSize > MAX_BLOCK_SIZE) { throw new IllegalArgumentException(String.format( "blockSize: %d (expected: %d-%d)", blockSize, MIN_BLOCK_SIZE, MAX_BLOCK_SIZE)); } int compressionLevel = 32 - Integer.numberOfLeadingZeros(blockSize - 1); // ceil of log2 compressionLevel = Math.max(0, compressionLevel - COMPRESSION_LEVEL_BASE); return compressionLevel; }
private static int log10Floor(int x) { /* * Based on Hacker's Delight Fig. 11-5, the two-table-lookup, branch-free implementation. * * The key idea is that based on the number of leading zeros (equivalently, floor(log2(x))), we * can narrow the possible floor(log10(x)) values to two. For example, if floor(log2(x)) is 6, * then 64 <= x < 128, so floor(log10(x)) is either 1 or 2. */ int y = maxLog10ForLeadingZeros[Integer.numberOfLeadingZeros(x)]; /* * y is the higher of the two possible values of floor(log10(x)). If x < 10^y, then we want the * lower of the two possible values, or y - 1, otherwise, we want y. */ return y - lessThanBranchFree(x, powersOf10[y]); }
/** * Returns the smallest power of two greater than or equal to {@code x}. This is equivalent to * {@code checkedPow(2, log2(x, CEILING))}. * * @throws IllegalArgumentException if {@code x <= 0} * @throws ArithmeticException of the next-higher power of two is not representable as an {@code * int}, i.e. when {@code x > 2^30} * @since 20.0 */ @Beta public static int ceilingPowerOfTwo(int x) { checkPositive("x", x); if (x > MAX_SIGNED_POWER_OF_TWO) { throw new ArithmeticException("ceilingPowerOfTwo(" + x + ") not representable as an int"); } return 1 << -Integer.numberOfLeadingZeros(x - 1); }
public NodeLabelsCache( NumberArrayFactory cacheFactory, int highLabelId, int chunkSize ) { this.cache = cacheFactory.newDynamicLongArray( chunkSize, 0 ); this.spillOver = cacheFactory.newDynamicLongArray( chunkSize / 5, 0 ); // expect way less of these this.bitsPerLabel = max( Integer.SIZE - numberOfLeadingZeros( highLabelId ), 1 ); this.worstCaseLongsNeeded = ((bitsPerLabel * (highLabelId + 1 /*length slot*/)) - 1) / Long.SIZE + 1; this.putClient = new Client( worstCaseLongsNeeded ); }
@Override public Mesh obtain (VertexAttributes vertexAttributes, int vertexCount, int indexCount) { for (int i = 0, n = freeMeshes.size; i < n; ++i) { final Mesh mesh = freeMeshes.get(i); if (mesh.getVertexAttributes().equals(vertexAttributes) && mesh.getMaxVertices() >= vertexCount && mesh.getMaxIndices() >= indexCount) { freeMeshes.removeIndex(i); usedMeshes.add(mesh); return mesh; } } vertexCount = 1 + (int)Short.MAX_VALUE; indexCount = Math.max(1 + (int)Short.MAX_VALUE, 1 << (32 - Integer.numberOfLeadingZeros(indexCount - 1))); Mesh result = new Mesh(false, vertexCount, indexCount, vertexAttributes); usedMeshes.add(result); return result; }
@Override public Mesh obtain (VertexAttributes vertexAttributes, int vertexCount, int indexCount) { for (int i = 0, n = freeMeshes.size; i < n; ++i) { final Mesh mesh = freeMeshes.get(i); if (mesh.getVertexAttributes().equals(vertexAttributes) && mesh.getMaxVertices() >= vertexCount && mesh.getMaxIndices() >= indexCount) { freeMeshes.removeIndex(i); usedMeshes.add(mesh); return mesh; } } vertexCount = 1 + (int)Short.MAX_VALUE; indexCount = Math.max(1 + (int)Short.MAX_VALUE, 1 << (32 - Integer.numberOfLeadingZeros(indexCount - 1))); Mesh result = new Mesh(false, vertexCount, indexCount, vertexAttributes); usedMeshes.add(result); return result; }