from math import ceil


def storage(billions, bits):
    parameters = billions * 1_000_000_000
    payload = (parameters * bits + 7) // 8
    # Hypothetical grouped layout, not a checkpoint-size predictor.
    metadata = ((parameters + 127) // 128) * 2 if bits < 16 else 0
    total = payload + metadata
    return {"payload": payload, "metadata": metadata,
            "total": total, "gib": total / 2**30}


def reconstruct(value, scale=0.25):
    code = max(-7, min(7, round(value / scale)))
    return code * scale


# Verification: independent hand-calculated fixture and boundaries.
assert storage(8, 4)["payload"] == 4_000_000_000
assert storage(8, 4)["metadata"] == 125_000_000
assert storage(8, 16)["metadata"] == 0
assert reconstruct(0.6) == 0.5
assert reconstruct(2.0) == 1.75
fixtures = [{"inputs": {"billions": str(n), "bits": str(b)},
             "expected": storage(n, b)}
            for n in (1, 4, 8) for b in (4, 8, 16)]

import json
print(json.dumps({"fixtures": fixtures, "checks_passed": True}))
