import math
word_size = 8
word_buckets_limit = 256
double_buckets_limit = 512
exp_fractions = 4
min_chunk_size = word_size * 3
word_bins_count = (word_buckets_limit - min_chunk_size) // word_size
print("word bins count:", word_bins_count)
for i, sb in enumerate(range(min_chunk_size, word_buckets_limit, word_size)):
print("{1:>3}: {0:>8} {0:>20b} | ".format(sb, i), end='\n')
double_bins_count = (double_buckets_limit - word_buckets_limit) // (2*word_size)
print("double bins count:", double_bins_count)
for i, bsb in enumerate(range(word_buckets_limit, double_buckets_limit, 2*word_size)):
print("{1:>3}: {0:>8} {0:>20b} | ".format(bsb, i), end='\n')
print("log bins")
g_ofst = int(math.log2(double_buckets_limit)) g_p2dv = int(math.log2(exp_fractions))
for g in range(0, (word_size * 8 * 2) - word_bins_count - double_bins_count):
size = ((1 << g_p2dv) + (g & ((1<<g_p2dv)-1))) << ((g >> g_p2dv) + (g_ofst-g_p2dv))
size_log2 = math.floor(math.log2(size))
g_calc = ((size >> size_log2 - g_p2dv) ^ (1<<g_p2dv)) + ((size_log2-g_ofst) << g_p2dv)
assert g == g_calc
print("{1:>3}: {0:>8} {0:>20b} | ".format(size, g + word_bins_count + double_bins_count), end='\n')