--- deliantra/server/include/util.h 2007/01/15 00:40:49 1.27 +++ deliantra/server/include/util.h 2007/01/15 01:25:41 1.28 @@ -8,6 +8,7 @@ #endif #include +#include #include #include @@ -34,6 +35,36 @@ #define IN_RANGE_EXC(val,beg,end) \ ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) +// this is much faster than crossfires original algorithm +// on modern cpus +inline int +isqrt (int n) +{ + return (int)sqrtf ((float)n); +} + +// this is only twice as fast as naive sqrtf (dx*dy+dy*dy) +#if 0 +// and has a max. error of 6 in the range -100..+100. +#else +// and has a max. error of 9 in the range -100..+100. +#endif +inline int +idistance (int dx, int dy) +{ + unsigned int dx_ = abs (dx); + unsigned int dy_ = abs (dy); + +#if 0 + return dx_ > dy_ + ? (dx_ * 61685 + dy_ * 26870) >> 16 + : (dy_ * 61685 + dx_ * 26870) >> 16; +#else + return dx + dy - min (dx, dy) * 5 / 8; +#endif +} + + // makes dynamically allocated objects zero-initialised struct zero_initialised {