compat.h: Add logbase<size_t>().

git-svn-id: https://svn.eduke32.com/eduke32@6119 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
hendricks266 2017-04-12 08:30:35 +00:00
parent 73654c2079
commit d3c56b8a89

View file

@ -936,6 +936,25 @@ CLAMP_DECL int32_t clamp2(int32_t in, int32_t min, int32_t max) { return in >= m
CLAMP_DECL float fclamp2(float in, float min, float max) { return in >= max ? max : (in <= min ? min : in); }
////////// Mathematical operations //////////
#ifdef __cplusplus
template <size_t base, typename T>
CONSTEXPR size_t logbase(T n)
{
return n < static_cast<T>(base) ? 1 : 1 + logbase<base>(n / static_cast<T>(base));
}
// hackish version to work around the impossibility of representing abs(INT*_MIN)
template <size_t base, typename T>
CONSTEXPR size_t logbasenegative(T n)
{
return n > static_cast<T>(-(native_t)base) ? 1 : 1 + logbase<base>(n / static_cast<T>(-(native_t)base));
}
#endif
////////// Utility functions //////////
#if RAND_MAX == 32767