Fix PMAPBITS compile-time sanity check

This commit is contained in:
Tamás Bálint Misius 2020-01-24 22:04:41 +01:00
parent 5dfda0c528
commit 3c6ae35cc4
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
2 changed files with 11 additions and 7 deletions

View File

@ -68,13 +68,6 @@
#define PT_NUM (1<<PMAPBITS)
#if PMAPBITS > 16
#error PMAPBITS is too large
#endif
#if ((XRES*YRES)<<PMAPBITS) > 0x100000000L
#error not enough space in pmap
#endif
struct playerst;

View File

@ -5267,3 +5267,14 @@ float Simulation::remainder_p(float x, float y)
{
return std::fmod(x, y) + (x>=0 ? 0 : y);
}
constexpr size_t ce_log2(size_t n)
{
return ((n < 2) ? 1 : 1 + ce_log2(n / 2));
}
static_assert(PMAPBITS <= 16, "PMAPBITS is too large");
// * This will technically fail in some cases where (XRES * YRES) << PMAPBITS would
// fit in 31 bits but multiplication is evil and wraps around without you knowing it.
// * Whoever runs into a problem with this (e.g. with XRES = 612, YRES = 384 and
// PMAPBITS = 13) should just remove the check and take responsibility otherwise.
static_assert(ce_log2(XRES) + ce_log2(YRES) + PMAPBITS <= 31, "not enough space in pmap");