From 45ca3b6336f1c6e7c2991319837132b607d482dc Mon Sep 17 00:00:00 2001 From: tron Date: Thu, 8 Sep 2005 12:48:26 +0000 Subject: [PATCH] (svn r2924) Introduce the ALIGN() macro which aligns values to multiples of a power of 2, for exact semantics see the commment in macros.h --- gfx.c | 4 ++-- macros.h | 6 ++++++ screenshot.c | 2 +- settings.c | 2 +- video/win32_v.c | 4 ++-- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/gfx.c b/gfx.c index d22d864c7a..38c6cd009a 100644 --- a/gfx.c +++ b/gfx.c @@ -1755,8 +1755,8 @@ void RedrawScreenRect(int left, int top, int right, int bottom) void DrawDirtyBlocks(void) { byte *b = _dirty_blocks; - const int w = (_screen.width + 63) & ~63; - const int h = (_screen.height + 7) & ~7; + const int w = ALIGN(_screen.width, 64); + const int h = ALIGN(_screen.height, 8); int x; int y; diff --git a/macros.h b/macros.h index 1232005cd9..26c4d9985a 100644 --- a/macros.h +++ b/macros.h @@ -158,6 +158,12 @@ static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a #define ROL(x, n) ((x) << (n) | (x) >> (sizeof(x) * 8 - (n))) #define ROR(x, n) ((x) >> (n) | (x) << (sizeof(x) * 8 - (n))) +/** + * Return the smallest multiple of n equal or greater than x + * @note n must be a power of 2 + */ +#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1)) + /* IS_INT_INSIDE = filter for ascii-function codes like BELL and so on [we need an special filter here later] */ static inline bool IsValidAsciiChar(byte key) { diff --git a/screenshot.c b/screenshot.c index ee5153d3bf..ac9a4433fb 100644 --- a/screenshot.c +++ b/screenshot.c @@ -78,7 +78,7 @@ static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *user if (f == NULL) return false; // each scanline must be aligned on a 32bit boundary - padw = (w + 3) & ~3; + padw = ALIGN(w, 4); // setup the file header bfh.type = TO_LE16('MB'); diff --git a/settings.c b/settings.c index 65eef05441..9e3580299d 100644 --- a/settings.c +++ b/settings.c @@ -50,7 +50,7 @@ static void *pool_alloc(SettingsMemoryPool **pool, uint size) uint pos; SettingsMemoryPool *p = *pool; - size = (size + 3) & ~3; // align everything to a 32 bit boundary + size = ALIGN(size, 4); // align everything to a 32 bit boundary // first check if there's memory in the next pool if (p->next && p->next->pos + size <= p->next->size) { diff --git a/video/win32_v.c b/video/win32_v.c index 3656e66ad6..90997804a3 100644 --- a/video/win32_v.c +++ b/video/win32_v.c @@ -536,7 +536,7 @@ static bool AllocateDibSection(int w, int h) return false; _screen.width = w; - _screen.pitch = (w + 3) & ~0x3; + _screen.pitch = ALIGN(w, 4); _screen.height = h; if (_wnd.alloced_bits) { @@ -549,7 +549,7 @@ static bool AllocateDibSection(int w, int h) bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); if (_wnd.double_size) { - w = (w + 3) & ~0x3; + w = ALIGN(w, 4); _wnd.alloced_bits = _wnd.buffer_bits = malloc(w * h); w *= 2; h *= 2;