public static int sizeOfUnsignedVarint(int value) {
        // Protocol buffers varint encoding is variable length, with a minimum of 1 byte
        // (for zero). The values themselves are not important. What's important here is
        // any leading zero bits are dropped from output. We can use this leading zero
        // count w/ fast intrinsic to calc the output length directly.

        // Test cases verify this matches the output for loop logic exactly.

        // return (38 - leadingZeros) / 7 + leadingZeros / 32;

        // The above formula provides the implementation, but the Java encoding is suboptimal
        // when we have a narrow range of integers, so we can do better manually

        int leadingZeros = Integer.numberOfLeadingZeros(value);
        int leadingZerosBelow38DividedBy7 = ((38 - leadingZeros) * 0b10010010010010011) >>> 19;
        return leadingZerosBelow38DividedBy7 + (leadingZeros >>> 5);
    }
@Test
    public void testSizeOfUnsignedVarint() {
        // The old well-known implementation for sizeOfUnsignedVarint
        IntFunction<Integer> simpleImplementation = (int value) -> {
            int bytes = 1;
            while ((value & 0xffffff80) != 0L) {
                bytes += 1;
                value >>>= 7;
            }
            return bytes;
        };

        // compare the full range of values
        for (int i = 0; i < Integer.MAX_VALUE && i >= 0; i += 13) {
            final int actual = ByteUtils.sizeOfUnsignedVarint(i);
            final int expected = simpleImplementation.apply(i);
            assertEquals(expected, actual);
        }
    }