From 77dc743f09898613c3da877537136f4c62d84492 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 31 Aug 2014 11:15:17 +0000 Subject: [PATCH] Factor out [l]dist() implementations into static inline functions in common.h. And use these in jmact/mathutil.c's FindDistance2D()/FindDistance3D(). The main use is to allow passing dx/dy instead of sprite positions; the code that actually uses this is not committed. git-svn-id: https://svn.eduke32.com/eduke32@4579 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/build/include/common.h | 40 +++++++++++++++++++++++++ polymer/eduke32/build/src/common.c | 26 ++-------------- polymer/eduke32/source/jmact/mathutil.c | 21 ++----------- 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/polymer/eduke32/build/include/common.h b/polymer/eduke32/build/include/common.h index cd0bf622f..9028714a4 100644 --- a/polymer/eduke32/build/include/common.h +++ b/polymer/eduke32/build/include/common.h @@ -7,8 +7,12 @@ #ifndef EDUKE32_COMMON_H_ #define EDUKE32_COMMON_H_ +#include #include "scriptfile.h" #include "cache1d.h" +#include "pragmas.h" // klabs +#include "build.h" + #ifdef EXTERNC extern "C" { @@ -82,6 +86,42 @@ int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern, char *dup_filename(const char *fn); int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext); +// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says +// in jmact/mathutil.c: "Ken's reverse-engineering job". +// Note that jmact/mathutil.c contains practically the same code, but where the +// individual x/y(/z) distances are passed instead. +static inline int32_t sepldist(const int32_t dx, const int32_t dy) +{ + int32_t x = klabs(dx); + int32_t y = klabs(dy); + + if (x < y) + swaplong(&x, &y); + + { + int32_t t = y + (y>>1); + return x - (x>>5) - (x>>7) + (t>>2) + (t>>6); + } +} + +// dz: in Build coordinates +static inline int32_t sepdist(int32_t dx, int32_t dy, int32_t dz) +{ + int32_t x = klabs(dx); + int32_t y = klabs(dy); + int32_t z = klabs(dz>>4); + + if (x < y) + swaplong(&x, &y); + if (x < z) + swaplong(&x, &z); + + { + int32_t t = y + z; + return x - (x>>4) + (t>>2) + (t>>3); + } +} + int32_t ldist(const spritetype *s1, const spritetype *s2); int32_t dist(const spritetype *s1, const spritetype *s2); diff --git a/polymer/eduke32/build/src/common.c b/polymer/eduke32/build/src/common.c index 9d2b7590f..810ad97e2 100644 --- a/polymer/eduke32/build/src/common.c +++ b/polymer/eduke32/build/src/common.c @@ -197,36 +197,14 @@ int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char } -// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says -// in jmact/mathutil.c: "Ken's reverse-engineering job". -// Note that jmact/mathutil.c contains practically the same code, but where the -// individual x/y(/z) distances are passed instead. int32_t ldist(const spritetype *s1, const spritetype *s2) { - int32_t x = klabs(s1->x-s2->x); - int32_t y = klabs(s1->y-s2->y); - - if (x>1); - return (x - (x>>5) - (x>>7) + (t>>2) + (t>>6)); - } + return sepldist(s1->x-s2->x, s1->y-s2->y); } int32_t dist(const spritetype *s1, const spritetype *s2) { - int32_t x = klabs(s1->x-s2->x); - int32_t y = klabs(s1->y-s2->y); - int32_t z = klabs((s1->z-s2->z)>>4); - - if (x>4) + (t>>2) + (t>>3)); - } + return sepdist(s1->x-s2->x, s1->y-s2->y, s1->z-s2->z); } diff --git a/polymer/eduke32/source/jmact/mathutil.c b/polymer/eduke32/source/jmact/mathutil.c index 06803ce94..b43469e85 100644 --- a/polymer/eduke32/source/jmact/mathutil.c +++ b/polymer/eduke32/source/jmact/mathutil.c @@ -31,29 +31,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ //------------------------------------------------------------------------- -#include -#include "compat.h" -#include "pragmas.h" - -// I wonder if it's faster to use Ken's functions here... +#include "common.h" int32_t FindDistance2D(int32_t x, int32_t y) { - if ((x=klabs(x)) < (y=klabs(y))) swaplong(&x,&y); - - { - int32_t t = y + (y>>1); - return (x - (x>>5) - (x>>7) + (t>>2) + (t>>6)); - } + return sepldist(x, y); } int32_t FindDistance3D(int32_t x, int32_t y, int32_t z) { - if ((x=klabs(x)) < (y=klabs(y))) swaplong(&x,&y); - if (x < (z=klabs(z))) swaplong(&x,&z); - - { - int32_t t = y + z; - return (x - (x>>4) + (t>>2) + (t>>3)); - } + return sepdist(x, y, z); }