--- deliantra/Deliantra-Client/Client.xs 2018/11/18 12:47:55 1.328 +++ 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; @@ -298,49 +318,48 @@ typedef mapgrid *DC__Map; -static char * -prepend (char *ptr, int sze, int inc) -{ - char *p; - - New (0, p, sze + inc, char); - Zero (p, inc, char); - Move (ptr, p + inc, sze, char); +template +ecb_cold static void +prepend (T *&ptr, int sze, int inc) +{ + T *p; + + Newx (p, inc + sze, T); + Zero (p, inc, T); + Move (ptr, p + inc, sze, T); Safefree (ptr); - return p; + ptr = p; } -static char * -append (char *ptr, int sze, int inc) +template +ecb_cold static void +append (T *&ptr, int sze, int inc) { - Renew (ptr, sze + inc, char); - Zero (ptr + sze, inc, char); - - return ptr; + Renew (ptr, sze + inc, T); + Zero (ptr + sze, inc, T); } -#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) -#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) - static void need_facenum (struct mapgrid *self, faceid face) { - while (self->faces <= face) - { - Append (tileid, 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 (maptex, 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 * @@ -349,7 +368,7 @@ if (0 > y) { int extend = - y + MAP_EXTEND_Y; - Prepend (maprow, self->row, self->rows, extend); + prepend (self->row, self->rows, extend); self->rows += extend; self->y += extend; @@ -358,7 +377,7 @@ else if (y >= self->rows) { int extend = y - self->rows + MAP_EXTEND_Y; - Append (maprow, self->row, self->rows, extend); + append (self->row, self->rows, extend); self->rows += extend; } @@ -378,13 +397,13 @@ if (row->c0 > x) { int extend = row->c0 - x + MAP_EXTEND_X; - Prepend (mapcell, row->col, row->c1 - row->c0, extend); + prepend (row->col, row->c1 - row->c0, extend); row->c0 -= extend; } else if (x >= row->c1) { int extend = x - row->c1 + MAP_EXTEND_X; - Append (mapcell, row->col, row->c1 - row->c0, extend); + append (row->col, row->c1 - row->c0, extend); row->c1 += extend; } @@ -464,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 <> @@ -514,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 @@ -887,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: @@ -2058,7 +2052,7 @@ while (self->y < 0) { - Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); + prepend (self->row, self->rows, MAP_EXTEND_Y); self->rows += MAP_EXTEND_Y; self->y += MAP_EXTEND_Y; @@ -2312,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; @@ -2365,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) @@ -2494,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;