Source code for xma.math

# **************************************************
# Copyright (c) 2025, Mayank Mishra
# **************************************************


[docs] def ceil_divide(x: int, y: int) -> int: return (x + y - 1) // y
[docs] def check_power_of_2(n: int) -> bool: return n & (n - 1) == 0 and n != 0
[docs] def get_powers_of_2(start: int, end: int) -> list[int]: assert check_power_of_2(start), "start is not a power of 2" assert check_power_of_2(end), "end is not a power of 2" output = [] n = start while n <= end: output.append(n) n = n << 1 return output
[docs] def divide_if_divisible(dividend: int, divisor: int, msg: str = "") -> int: assert dividend % divisor == 0, msg return dividend // divisor
_POWERS_OF_2 = get_powers_of_2(1, 4294967296)
[docs] def get_next_power_of_2(x: int) -> int: for p in _POWERS_OF_2: if p >= x: return p raise ValueError(f"x ({x}) is bigger than the max allowable power of 2 ({p})")