2024-02-22 10:35:31 -05:00
|
|
|
# Copyright 2023-2024 (C) Peter McGoron
|
|
|
|
#
|
|
|
|
# This file is a part of Upsilon, a free and open source software project.
|
|
|
|
# For license terms, refer to the files in `doc/copying` in the Upsilon
|
|
|
|
# source distribution.
|
|
|
|
|
|
|
|
def minbits(n):
|
|
|
|
""" Return the amount of bits necessary to store n. """
|
2024-02-26 01:02:48 -05:00
|
|
|
from math import log2, floor
|
2024-02-22 10:35:31 -05:00
|
|
|
return floor(log2(n) + 1)
|
|
|
|
|
2024-02-26 01:02:48 -05:00
|
|
|
def round_up_to_pow_2(n):
|
|
|
|
""" Round up to nearest power of 2 if not already a power of 2. """
|
|
|
|
assert n > 0
|
|
|
|
# If n is a power of 2, then n - 1 has a smaller bit length than n.
|
|
|
|
# If n is not a power of 2, then n - 1 has the same bit length.
|
|
|
|
l = (n - 1).bit_length()
|
2024-03-11 00:31:30 -04:00
|
|
|
return 1 << l
|
2024-02-26 01:02:48 -05:00
|
|
|
|
|
|
|
def round_up_to_word(n):
|
|
|
|
""" Round up to 8, 16, or 32. """
|
|
|
|
if n <= 8:
|
|
|
|
return 8
|
|
|
|
if n <= 16:
|
|
|
|
return 16
|
|
|
|
if n <= 32:
|
|
|
|
return 32
|
|
|
|
raise Exception(f"{n} must be less than or equal to 32")
|