def varint_max_size(max_value):
    """Returns the maximum number of bytes a varint can take when encoded."""
    if max_value < 0:
        max_value = 2 ** 64 - max_value
    for i in range(1, 11):
        if max_value >> i * 7 == 0:
            return i
    raise ValueError('Value too large for varint: ' + str(max_value))
