diff --git a/source/lib/res/graphics/ogl_tex.cpp b/source/lib/res/graphics/ogl_tex.cpp index 79a7291aaa..3698a1c7ca 100644 --- a/source/lib/res/graphics/ogl_tex.cpp +++ b/source/lib/res/graphics/ogl_tex.cpp @@ -749,7 +749,7 @@ struct UploadParams }; static void upload_level(uint level, uint level_w, uint level_h, - const u8* restrict level_data, size_t UNUSED(level_data_size), void* restrict ctx) + const u8* RESTRICT level_data, size_t UNUSED(level_data_size), void* RESTRICT ctx) { const UploadParams* up = (const UploadParams*)ctx; glTexImage2D(GL_TEXTURE_2D, level, up->int_fmt, level_w, level_h, 0, @@ -757,7 +757,7 @@ static void upload_level(uint level, uint level_w, uint level_h, } static void upload_compressed_level(uint level, uint level_w, uint level_h, - const u8* restrict level_data, size_t level_data_size, void* restrict ctx) + const u8* RESTRICT level_data, size_t level_data_size, void* RESTRICT ctx) { const UploadParams* up = (const UploadParams*)ctx; pglCompressedTexImage2DARB(GL_TEXTURE_2D, level, up->fmt, diff --git a/source/lib/res/graphics/tex.cpp b/source/lib/res/graphics/tex.cpp index e4fd97ecc1..ba88ac67ef 100644 --- a/source/lib/res/graphics/tex.cpp +++ b/source/lib/res/graphics/tex.cpp @@ -123,8 +123,8 @@ LibError tex_validate_plain_format(uint bpp, uint flags) // mipmaps //----------------------------------------------------------------------------- -void tex_util_foreach_mipmap(uint w, uint h, uint bpp, const u8* restrict data, - int levels_to_skip, uint data_padding, MipmapCB cb, void* restrict ctx) +void tex_util_foreach_mipmap(uint w, uint h, uint bpp, const u8* RESTRICT data, + int levels_to_skip, uint data_padding, MipmapCB cb, void* RESTRICT ctx) { uint level_w = w, level_h = h; const u8* level_data = data; @@ -179,7 +179,7 @@ struct CreateLevelData // uses 2x2 box filter static void create_level(uint level, uint level_w, uint level_h, - const u8* restrict level_data, size_t level_data_size, void* restrict ctx) + const u8* RESTRICT level_data, size_t level_data_size, void* RESTRICT ctx) { CreateLevelData* cld = (CreateLevelData*)ctx; const size_t src_w = cld->prev_level_w; @@ -578,7 +578,7 @@ u8* tex_get_data(const Tex* t) static void add_level_size(uint UNUSED(level), uint UNUSED(level_w), uint UNUSED(level_h), - const u8* restrict UNUSED(level_data), size_t level_data_size, void* restrict ctx) + const u8* RESTRICT UNUSED(level_data), size_t level_data_size, void* RESTRICT ctx) { size_t* ptotal_size = (size_t*)ctx; *ptotal_size += level_data_size; diff --git a/source/lib/res/graphics/tex.h b/source/lib/res/graphics/tex.h index 0b223bca2b..c5422ccc31 100644 --- a/source/lib/res/graphics/tex.h +++ b/source/lib/res/graphics/tex.h @@ -371,7 +371,7 @@ const int TEX_BASE_LEVEL_ONLY = -1; * @param ctx passed through from tex_util_foreach_mipmap. **/ typedef void (*MipmapCB)(uint level, uint level_w, uint level_h, - const u8* level_data, size_t level_data_size, void* ctx); + const u8* RESTRICT level_data, size_t level_data_size, void* RESTRICT ctx); /** * for a series of mipmaps stored from base to highest, call back for @@ -390,8 +390,8 @@ typedef void (*MipmapCB)(uint level, uint level_w, uint level_h, * @param cb MipmapCB to call * @param ctx extra data to pass to cb **/ -extern void tex_util_foreach_mipmap(uint w, uint h, uint bpp, const u8* restrict data, - int levels_to_skip, uint data_padding, MipmapCB cb, void* restrict ctx); +extern void tex_util_foreach_mipmap(uint w, uint h, uint bpp, const u8* RESTRICT data, + int levels_to_skip, uint data_padding, MipmapCB cb, void* RESTRICT ctx); // diff --git a/source/lib/res/graphics/tex_bmp.cpp b/source/lib/res/graphics/tex_bmp.cpp index 6609b0a9d3..e5e7ef154c 100644 --- a/source/lib/res/graphics/tex_bmp.cpp +++ b/source/lib/res/graphics/tex_bmp.cpp @@ -91,7 +91,7 @@ static size_t bmp_hdr_size(const u8* file) // requirements: uncompressed, direct colour, bottom up -static LibError bmp_decode(DynArray* restrict da, Tex* restrict t) +static LibError bmp_decode(DynArray* RESTRICT da, Tex* RESTRICT t) { u8* file = da->base; @@ -122,7 +122,7 @@ static LibError bmp_decode(DynArray* restrict da, Tex* restrict t) } -static LibError bmp_encode(Tex* restrict t, DynArray* restrict da) +static LibError bmp_encode(Tex* RESTRICT t, DynArray* RESTRICT da) { const size_t hdr_size = sizeof(BmpHeader); // needed for BITMAPFILEHEADER const size_t img_size = tex_img_size(t); diff --git a/source/lib/res/graphics/tex_codec.h b/source/lib/res/graphics/tex_codec.h index 1eae9a9793..bb8e41a037 100644 --- a/source/lib/res/graphics/tex_codec.h +++ b/source/lib/res/graphics/tex_codec.h @@ -46,7 +46,7 @@ struct TexCodecVTbl * @param t output texture object * @return LibError **/ - LibError (*decode)(DynArray * restrict da, Tex * restrict t); + LibError (*decode)(DynArray * RESTRICT da, Tex * RESTRICT t); /** @@ -60,7 +60,7 @@ struct TexCodecVTbl * by the caller. * @return LibError **/ - LibError (*encode)(Tex * restrict t, DynArray * restrict da); + LibError (*encode)(Tex * RESTRICT t, DynArray * RESTRICT da); /** * transform the texture's pixel format. diff --git a/source/lib/res/graphics/tex_dds.cpp b/source/lib/res/graphics/tex_dds.cpp index 97c4fa12ea..13f152f9a3 100644 --- a/source/lib/res/graphics/tex_dds.cpp +++ b/source/lib/res/graphics/tex_dds.cpp @@ -99,7 +99,7 @@ struct S3tcBlock }; -static void s3tc_precalc_alpha(uint dxt, const u8* restrict a_block, S3tcBlock* restrict b) +static void s3tc_precalc_alpha(uint dxt, const u8* RESTRICT a_block, S3tcBlock* RESTRICT b) { // read block contents const uint a0 = a_block[0], a1 = a_block[1]; @@ -138,7 +138,7 @@ static void s3tc_precalc_alpha(uint dxt, const u8* restrict a_block, S3tcBlock* } -static void s3tc_precalc_color(uint dxt, const u8* restrict c_block, S3tcBlock* restrict b) +static void s3tc_precalc_color(uint dxt, const u8* RESTRICT c_block, S3tcBlock* RESTRICT b) { // read block contents // .. S3TC reference colors (565 format). the color table is generated @@ -175,7 +175,7 @@ static void s3tc_precalc_color(uint dxt, const u8* restrict c_block, S3tcBlock* } -static void s3tc_precalc_block(uint dxt, const u8* restrict block, S3tcBlock* restrict b) +static void s3tc_precalc_block(uint dxt, const u8* RESTRICT block, S3tcBlock* RESTRICT b) { b->dxt = dxt; @@ -188,7 +188,7 @@ static void s3tc_precalc_block(uint dxt, const u8* restrict block, S3tcBlock* re } -static void s3tc_write_pixel(const S3tcBlock* restrict b, uint pixel_idx, u8* restrict out) +static void s3tc_write_pixel(const S3tcBlock* RESTRICT b, uint pixel_idx, u8* RESTRICT out) { debug_assert(pixel_idx < 16); @@ -231,7 +231,7 @@ struct S3tcDecompressInfo }; static void s3tc_decompress_level(uint UNUSED(level), uint level_w, uint level_h, - const u8* restrict level_data, size_t level_data_size, void* restrict ctx) + const u8* RESTRICT level_data, size_t level_data_size, void* RESTRICT ctx) { S3tcDecompressInfo* di = (S3tcDecompressInfo*)ctx; const uint dxt = di->dxt; @@ -615,7 +615,7 @@ static size_t dds_hdr_size(const u8* UNUSED(file)) } -static LibError dds_decode(DynArray* restrict da, Tex* restrict t) +static LibError dds_decode(DynArray* RESTRICT da, Tex* RESTRICT t) { u8* file = da->base; const DDSURFACEDESC2* sd = (const DDSURFACEDESC2*)(file+4); @@ -633,7 +633,7 @@ static LibError dds_decode(DynArray* restrict da, Tex* restrict t) } -static LibError dds_encode(Tex* restrict UNUSED(t), DynArray* restrict UNUSED(da)) +static LibError dds_encode(Tex* RESTRICT UNUSED(t), DynArray* RESTRICT UNUSED(da)) { // note: do not return ERR::NOT_IMPLEMENTED et al. because that would // break tex_write (which assumes either this, 0 or errors are returned). diff --git a/source/lib/res/graphics/tex_jpg.cpp b/source/lib/res/graphics/tex_jpg.cpp index 590fad9f21..8e1f781a80 100644 --- a/source/lib/res/graphics/tex_jpg.cpp +++ b/source/lib/res/graphics/tex_jpg.cpp @@ -589,7 +589,7 @@ static size_t jpg_hdr_size(const u8* UNUSED(file)) } -static LibError jpg_decode(DynArray* restrict da, Tex* restrict t) +static LibError jpg_decode(DynArray* RESTRICT da, Tex* RESTRICT t) { LibError err; @@ -628,7 +628,7 @@ fail: // limitation: palette images aren't supported -static LibError jpg_encode(Tex* restrict t, DynArray* restrict da) +static LibError jpg_encode(Tex* RESTRICT t, DynArray* RESTRICT da) { LibError err; diff --git a/source/lib/res/graphics/tex_png.cpp b/source/lib/res/graphics/tex_png.cpp index 328dce34f1..e07f68bf39 100644 --- a/source/lib/res/graphics/tex_png.cpp +++ b/source/lib/res/graphics/tex_png.cpp @@ -217,7 +217,7 @@ static size_t png_hdr_size(const u8* UNUSED(file)) TIMER_ADD_CLIENT(tc_png_decode); // limitation: palette images aren't supported -static LibError png_decode(DynArray* restrict da, Tex* restrict t) +static LibError png_decode(DynArray* RESTRICT da, Tex* RESTRICT t) { TIMER_ACCRUE(tc_png_decode); @@ -260,7 +260,7 @@ ret: // limitation: palette images aren't supported -static LibError png_encode(Tex* restrict t, DynArray* restrict da) +static LibError png_encode(Tex* RESTRICT t, DynArray* RESTRICT da) { LibError err = ERR::FAIL; // freed when ret is reached: diff --git a/source/lib/res/graphics/tex_tga.cpp b/source/lib/res/graphics/tex_tga.cpp index b4380e410d..ec1d1fd61b 100644 --- a/source/lib/res/graphics/tex_tga.cpp +++ b/source/lib/res/graphics/tex_tga.cpp @@ -108,7 +108,7 @@ static size_t tga_hdr_size(const u8* file) // requirements: uncompressed, direct colour, bottom up -static LibError tga_decode(DynArray* restrict da, Tex* restrict t) +static LibError tga_decode(DynArray* RESTRICT da, Tex* RESTRICT t) { u8* file = da->base; @@ -142,7 +142,7 @@ static LibError tga_decode(DynArray* restrict da, Tex* restrict t) } -static LibError tga_encode(Tex* restrict t, DynArray* restrict da) +static LibError tga_encode(Tex* RESTRICT t, DynArray* RESTRICT da) { u8 img_desc = 0; if(t->flags & TEX_TOP_DOWN) diff --git a/source/lib/sysdep/sysdep.h b/source/lib/sysdep/sysdep.h index a4b78ed69c..8cb135e093 100644 --- a/source/lib/sysdep/sysdep.h +++ b/source/lib/sysdep/sysdep.h @@ -153,17 +153,37 @@ extern void* alloca(size_t size); # endif #endif -// C99 restrict +// C99-like restrict (non-standard in C++, but widely supported in various forms). +// Must only be used on pointers - some compilers (g++) support it on references +// and member functions, but not all do (and we want to work on as many as possible). +// +// We call this "RESTRICT" to avoid conflicts with VC's __declspec(restrict), +// and because it's not really the same as C99's restrict. +// +// To be safe and satisfy the compilers' stated requirements: an object accessed +// by a restricted pointer must not be accessed by any other pointer within the +// lifetime of the restricted pointer, if the object is modified. +// To maximise the chance of optimisation, any pointers that could potentially +// alias with the restricted one should be marked as restricted too. +// // .. for some reason, g++-3.3 claims to support C99 (according to // __STDC_VERSION__) but doesn't have the restrict keyword. // use the extension __restrict__ instead. #if GCC_VERSION -# define restrict __restrict__ -// .. already available; need do nothing +# define RESTRICT __restrict__ +// .. already available as 'restrict' #elif HAVE_C99 +# define RESTRICT restrict +// .. ICC provides restrict, though you have to make sure you pass /Qrestrict +// (-restrict on Linux) in the compiler options +#elif ICC_VERSION +# define RESTRICT restrict +// .. VC8 provides __restrict +#elif MSC_VERSION >= 800 +# define RESTRICT __restrict // .. unsupported; remove it from code #else -# define restrict +# define RESTRICT #endif // C99 __func__