--- deliantra/Deliantra-Client/Client.xs 2018/11/18 13:07:51 1.329 +++ deliantra/Deliantra-Client/Client.xs 2018/11/19 01:56:11 1.335 @@ -78,14 +78,8 @@ # include #endif -#if __GNUC__ >= 4 -# define expect(expr,value) __builtin_expect ((expr),(value)) -#else -# define expect(expr,value) (expr) -#endif - -#define expect_false(expr) expect ((expr) != 0, 0) -#define expect_true(expr) expect ((expr) != 0, 1) +#include "ecb.h" +#include "salloc.h" #define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ @@ -107,13 +101,37 @@ #define TEXID_SPEECH 1 #define TEXID_NOFACE 2 +// approximately divide by 255 +static unsigned int +div255 (unsigned int n) +{ + return (n + (n >> 8)) >> 8; +} + +static unsigned int +minpot (unsigned int n) +{ + if (!n) + return 0; + + --n; + + n |= n >> 1; + n |= n >> 2; + n |= n >> 4; + n |= n >> 8; + n |= n >> 16; + + return n + 1; +} + static char * fast_sv_grow (SV *sv, STRLEN need) { STRLEN len = SvLEN (sv); STRLEN want = SvCUR (sv) + need; - if (expect_false (len < want)) + if (ecb_expect_false (len < want)) { do len *= 2; @@ -258,6 +276,8 @@ *h = rect.height; } +///////////////////////////////////////////////////////////////////////////// + typedef uint16_t tileid; typedef uint16_t faceid; @@ -299,12 +319,12 @@ typedef mapgrid *DC__Map; template -static void +ecb_cold static void prepend (T *&ptr, int sze, int inc) { T *p; - New (0, p, sze + inc, T); + Newx (p, inc + sze, T); Zero (p, inc, T); Move (ptr, p + inc, sze, T); Safefree (ptr); @@ -313,7 +333,7 @@ } template -static void +ecb_cold static void append (T *&ptr, int sze, int inc) { Renew (ptr, sze + inc, T); @@ -323,21 +343,23 @@ static void need_facenum (struct mapgrid *self, faceid face) { - while (self->faces <= face) - { - append (self->face2tile, self->faces, self->faces); - self->faces *= 2; - } + if (ecb_expect_true (self->faces > face)) + return; + + size_t newsize = minpot (face + 1); + append (self->face2tile, self->faces, newsize - self->faces); + self->faces = newsize; } static void need_texid (struct mapgrid *self, int texid) { - while (self->texs <= texid) - { - append (self->tex, self->texs, self->texs); - self->texs *= 2; - } + if (ecb_expect_true (self->texs > texid)) + return; + + size_t newsize = minpot (texid + 1); + append (self->tex, self->texs, newsize - self->texs); + self->texs = newsize; } static maprow * @@ -461,7 +483,7 @@ } }; -typedef ska::flat_hash_map smooth_hash; +typedef ska::flat_hash_map, std::equal_to, slice_allocator> smooth_hash; namespace std { template <> @@ -511,41 +533,6 @@ SDL_PushEvent ((SDL_Event *)&ev); } -// approximately divide by 255 -static unsigned int -div255 (unsigned int n) -{ - return (n + (n >> 8)) >> 8; -} - -static unsigned int -minpot (unsigned int n) -{ - if (!n) - return 0; - - --n; - - n |= n >> 1; - n |= n >> 2; - n |= n >> 4; - n |= n >> 8; - n |= n >> 16; - - return n + 1; -} - -static unsigned int -popcount (unsigned int n) -{ - n -= (n >> 1) & 0x55555555U; - n = ((n >> 2) & 0x33333333U) + (n & 0x33333333U); - n = ((n >> 4) + n) & 0x0f0f0f0fU; - n *= 0x01010101U; - - return n >> 24; -} - /* SDL should provide this, really. */ #define SDLK_MODIFIER_MIN 300 #define SDLK_MODIFIER_MAX 314 @@ -884,7 +871,17 @@ IV minpot (UV n) +UV ld32 (UV n) + CODE: + RETVAL = ecb_ld32 (n); + OUTPUT: + RETVAL + IV popcount (UV n) + CODE: + RETVAL = ecb_popcount32 (n); + OUTPUT: + RETVAL NV distance (NV dx, NV dy) CODE: @@ -2309,7 +2306,7 @@ px = (x + 1) * Th - tex.w; py = (y + 1) * Tw - tex.h; - if (expect_false (cell->player == player) && expect_false (z == 2)) + if (ecb_expect_false (cell->player == player) && ecb_expect_false (z == 2)) { pl_x = px; pl_y = py; @@ -2362,7 +2359,7 @@ } } - if (expect_false (z == 2) && expect_false (cell->flags)) + if (ecb_expect_false (z == 2) && ecb_expect_false (cell->flags)) { // overlays such as the speech bubble, probably more to come if (cell->flags & 1) @@ -2491,7 +2488,7 @@ int px = x * Tw; int py = y * Th; - if (expect_false (cell->player == player)) + if (ecb_expect_false (cell->player == player)) { px += sdx; py += sdy;