X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Futil%2Fnumeric.h;h=643c444409af64aca6ba094901ce4b6559914d41;hb=8f49358153637bb87f492e558a13cf9f23046e64;hp=42d9a87a91a7af8d416b938fc6615057cc5d605b;hpb=3b6480c5b0c968ad9f5a7cfb7ca494989be03629;p=dragonfireclient.git diff --git a/src/util/numeric.h b/src/util/numeric.h index 42d9a87a9..643c44440 100644 --- a/src/util/numeric.h +++ b/src/util/numeric.h @@ -20,14 +20,15 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef UTIL_NUMERIC_HEADER #define UTIL_NUMERIC_HEADER +#include "../basicmacros.h" #include "../irrlichttypes.h" #include "../irr_v2d.h" #include "../irr_v3d.h" #include "../irr_aabb3d.h" +#include "../threading/mutex.h" #include #include #include -#include /* @@ -41,6 +42,7 @@ class FacePositionCache private: static void generateFacePosition(u16 d); static std::map > m_cache; + static Mutex m_cache_mutex; }; class IndentationRaiser @@ -164,9 +166,6 @@ inline v3s16 arealim(v3s16 p, s16 d) return p; } -#define ARRLEN(x) (sizeof(x) / sizeof((x)[0])) -#define CONTAINS(c, v) (std::find((c).begin(), (c).end(), (v)) != (c).end()) - // The naive swap performs better than the xor version #define SWAP(t, x, y) do { \ t temp = x; \ @@ -239,34 +238,50 @@ inline float wrapDegrees_180(float f) /* Pseudo-random (VC++ rand() sucks) */ -int myrand(void); -void mysrand(unsigned seed); -#define MYRAND_MAX 32767 - +#define MYRAND_RANGE 0xffffffff +u32 myrand(); +void mysrand(unsigned int seed); +void myrand_bytes(void *out, size_t len); int myrand_range(int min, int max); /* Miscellaneous functions */ +inline u32 get_bits(u32 x, u32 pos, u32 len) +{ + u32 mask = (1 << len) - 1; + return (x >> pos) & mask; +} + +inline void set_bits(u32 *x, u32 pos, u32 len, u32 val) +{ + u32 mask = (1 << len) - 1; + *x &= ~(mask << pos); + *x |= (val & mask) << pos; +} + +inline u32 calc_parity(u32 v) +{ + v ^= v >> 16; + v ^= v >> 8; + v ^= v >> 4; + v &= 0xf; + return (0x6996 >> v) & 1; +} + u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed); bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 camera_fov, f32 range, f32 *distance_ptr=NULL); -/* - Some helper stuff -*/ -#define MYMIN(a,b) ((a)<(b)?(a):(b)) -#define MYMAX(a,b) ((a)>(b)?(a):(b)) - /* Returns nearest 32-bit integer for given floating point number. and in VC++ don't provide round(). */ inline s32 myround(f32 f) { - return floor(f + 0.5); + return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f)); } /* @@ -389,5 +404,16 @@ inline bool is_power_of_two(u32 n) return n != 0 && (n & (n-1)) == 0; } -#endif +// Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes. +// Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 +inline u32 npot2(u32 orig) { + orig--; + orig |= orig >> 1; + orig |= orig >> 2; + orig |= orig >> 4; + orig |= orig >> 8; + orig |= orig >> 16; + return orig + 1; +} +#endif