From 0720025073a49784be637e4aadcb47552da4d488 Mon Sep 17 00:00:00 2001 From: janwas Date: Sat, 30 Apr 2011 13:22:46 +0000 Subject: [PATCH] cleanup reduce dependency on PCH. move KiB constants to alignment, ARRAY_SIZE to code_annotation.h. move glext_funcs.h to external_libraries/glext_funcs.h, move part of ogl.h to external_libraries/opengl.h remove unused saturating arithmetic functions This was SVN commit r9363. --- source/graphics/tests/test_TextureConverter.h | 1 + source/graphics/tests/test_TextureManager.h | 1 + source/lib/alignment.h | 13 +++- source/lib/code_annotation.h | 36 +++++++++- source/lib/debug.cpp | 1 + .../{ => external_libraries}/glext_funcs.h | 0 source/lib/external_libraries/opengl.h | 66 +++++++++++++++++++ source/lib/file/archive/codec_zlib.cpp | 1 + source/lib/lib.cpp | 13 ---- source/lib/lib.h | 48 +------------- source/lib/ogl.cpp | 4 +- source/lib/ogl.h | 43 +----------- source/lib/res/sound/snd_mgr.cpp | 1 + source/lib/sysdep/arch/x86_x64/cache.cpp | 1 + source/lib/sysdep/os/win/wcpu.cpp | 1 + source/lib/sysdep/os/win/wnuma.cpp | 1 + source/lib/sysdep/os/win/wsysdep.cpp | 1 + source/lib/sysdep/os_cpu.cpp | 1 + source/lib/sysdep/smbios.cpp | 1 + source/lib/tests/test_lib.h | 19 ------ source/renderer/PatchRData.cpp | 1 + .../serialization/BinarySerializer.cpp | 1 + 22 files changed, 129 insertions(+), 126 deletions(-) rename source/lib/{ => external_libraries}/glext_funcs.h (100%) create mode 100644 source/lib/external_libraries/opengl.h diff --git a/source/graphics/tests/test_TextureConverter.h b/source/graphics/tests/test_TextureConverter.h index b3500d1d7c..5fc7379987 100644 --- a/source/graphics/tests/test_TextureConverter.h +++ b/source/graphics/tests/test_TextureConverter.h @@ -19,6 +19,7 @@ #include "graphics/TextureConverter.h" +#include "lib/alignment.h" #include "lib/file/vfs/vfs.h" #include "lib/res/h_mgr.h" #include "lib/tex/tex.h" diff --git a/source/graphics/tests/test_TextureManager.h b/source/graphics/tests/test_TextureManager.h index 2fa91617fa..5f31e649e0 100644 --- a/source/graphics/tests/test_TextureManager.h +++ b/source/graphics/tests/test_TextureManager.h @@ -18,6 +18,7 @@ #include "lib/self_test.h" #include "graphics/TextureManager.h" +#include "lib/alignment.h" #include "lib/external_libraries/sdl.h" #include "lib/file/vfs/vfs.h" #include "lib/res/h_mgr.h" diff --git a/source/lib/alignment.h b/source/lib/alignment.h index a31b33b927..9c7cdd6b84 100644 --- a/source/lib/alignment.h +++ b/source/lib/alignment.h @@ -18,9 +18,6 @@ inline size_t Align(size_t n) } -static const size_t allocationAlignment = ARCH_AMD64? 16 : 8; - - // // SIMD vector // @@ -54,6 +51,16 @@ static const size_t pageSize = 0x1000; // 4 KB static const size_t largePageSize = 0x200000; // 2 MB +// +// misc +// + +static const size_t allocationAlignment = ARCH_AMD64? 16 : 8; + +static const size_t KiB = size_t(1) << 10; +static const size_t MiB = size_t(1) << 20; +static const size_t GiB = size_t(1) << 30; + // waio opens files with FILE_FLAG_NO_BUFFERING, so Windows requires // file offsets / buffers and sizes to be sector-aligned. querying the // actual sector size via GetDiskFreeSpace is inconvenient and slow. diff --git a/source/lib/code_annotation.h b/source/lib/code_annotation.h index 4fb95e9e8f..0dc31c49e2 100644 --- a/source/lib/code_annotation.h +++ b/source/lib/code_annotation.h @@ -134,8 +134,12 @@ switch(x % 2) #define UID__ PASTE3__(LINE_, __LINE__, _) #define UID2__ PASTE3__(LINE_, __LINE__, _2) + +//----------------------------------------------------------------------------- +// cassert + /** - * Compile-time ENSURE. Causes a compile error if the expression + * Compile-time assertion. Causes a compile error if the expression * evaluates to zero/false. * * No runtime overhead; may be used anywhere, including file scope. @@ -164,7 +168,8 @@ template<> struct static_assert_ * This version has a less helpful error message, but redefinition doesn't * trigger warnings. **/ -#define cassert2(expr) extern u8 CASSERT_FAILURE[1][(expr)] +#define cassert2(expr) extern char CASSERT_FAILURE[1][(expr)] + // indicate a class is noncopyable (usually due to const or reference members). // example: @@ -281,6 +286,33 @@ private:\ #endif +// +// number of array elements +// + +#if GCC_VERSION + +// The function trick below does not work in GCC. Instead use the old fashioned +// divide-by-sizeof-element. This causes problems when the argument to +// ARRAY_SIZE is a pointer and not an array, but we will catch those when we +// compile on something other than GCC. + +#define ARRAY_SIZE(name) (sizeof(name) / (sizeof((name)[0]))) + +#else + +// (function taking a reference to an array and returning a pointer to +// an array of characters. it's only declared and never defined; we just +// need it to determine n, the size of the array that was passed.) +template char (*ArraySizeDeducer(T (&)[n]))[n]; + +// (although requiring C++, this method is much better than the standard +// sizeof(name) / sizeof(name[0]) because it doesn't compile when a +// pointer is passed, which can easily happen under maintenance.) +#define ARRAY_SIZE(name) (sizeof(*ArraySizeDeducer(name))) + +#endif // GCC_VERSION + // C99-style __func__ // .. newer GCC already have it #if GCC_VERSION >= 300 diff --git a/source/lib/debug.cpp b/source/lib/debug.cpp index 9452fd6de7..dae58e2e8b 100644 --- a/source/lib/debug.cpp +++ b/source/lib/debug.cpp @@ -31,6 +31,7 @@ #include #include +#include "lib/alignment.h" #include "lib/app_hooks.h" #include "lib/allocators/page_aligned.h" #include "lib/fnv_hash.h" diff --git a/source/lib/glext_funcs.h b/source/lib/external_libraries/glext_funcs.h similarity index 100% rename from source/lib/glext_funcs.h rename to source/lib/external_libraries/glext_funcs.h diff --git a/source/lib/external_libraries/opengl.h b/source/lib/external_libraries/opengl.h new file mode 100644 index 0000000000..e8c9bf7724 --- /dev/null +++ b/source/lib/external_libraries/opengl.h @@ -0,0 +1,66 @@ +/* Copyright (c) 2011 Wildfire Games + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * bring in OpenGL header+library, with compatibility fixes + */ + +#ifndef INCLUDED_OPENGL +#define INCLUDED_OPENGL + +#if OS_WIN +// wgl.h is a private header and should only be included from here. +// if this isn't defined, it'll complain. +#define WGL_HEADER_NEEDED +#include "lib/sysdep/os/win/wgl.h" +#endif + +#if OS_MACOSX || OS_MAC +# include +#else +# include +#endif + +// if gl.h provides real prototypes for 1.2 / 1.3 functions, +// exclude the corresponding function pointers in glext_funcs.h +#ifdef GL_VERSION_1_2 +#define REAL_GL_1_2 +#endif +#ifdef GL_VERSION_1_3 +#define REAL_GL_1_3 +#endif + +// this must come after GL/gl.h include, so we can't combine the +// including GL/glext.h. +#undef GL_GLEXT_PROTOTYPES + +#if OS_MACOSX || OS_MAC +# include +#else +# include +# if OS_WIN +# include +# define GL_TEXTURE_IMAGE_SIZE_ARB 0x86A0 +# endif +#endif + +#endif // #ifndef INCLUDED_OPENGL diff --git a/source/lib/file/archive/codec_zlib.cpp b/source/lib/file/archive/codec_zlib.cpp index a192d4f224..4b58e6f2d9 100644 --- a/source/lib/file/archive/codec_zlib.cpp +++ b/source/lib/file/archive/codec_zlib.cpp @@ -23,6 +23,7 @@ #include "precompiled.h" #include "lib/file/archive/codec_zlib.h" +#include "lib/alignment.h" #include "lib/file/archive/codec.h" #include "lib/external_libraries/zlib.h" diff --git a/source/lib/lib.cpp b/source/lib/lib.cpp index 6a3ae7c94b..0d1988824c 100644 --- a/source/lib/lib.cpp +++ b/source/lib/lib.cpp @@ -35,19 +35,6 @@ #include "lib/sysdep/sysdep.h" -u16 addusw(u16 x, u16 y) -{ - u32 t = x; - return (u16)std::min(t+y, 0xFFFFu); -} - -u16 subusw(u16 x, u16 y) -{ - long t = x; - return (u16)(std::max(t-y, 0l)); -} - - //----------------------------------------------------------------------------- // type conversion diff --git a/source/lib/lib.h b/source/lib/lib.h index 32cd8601c2..21497683d9 100644 --- a/source/lib/lib.h +++ b/source/lib/lib.h @@ -60,49 +60,10 @@ scope #ifndef INCLUDED_LIB #define INCLUDED_LIB -#include // fabsf +#include // fabsf #include // numeric_limits #include // out_of_range -#include "lib/config.h" -#include "lib/debug.h" - - -const size_t KiB = size_t(1) << 10; -const size_t MiB = size_t(1) << 20; -const size_t GiB = size_t(1) << 30; - - -// -// number of array elements -// - -#if GCC_VERSION - -// The function trick below does not work in GCC. Instead use the old fashioned -// divide-by-sizeof-element. This causes problems when the argument to -// ARRAY_SIZE is a pointer and not an array, but we will catch those when we -// compile on something other than GCC. - -#define ARRAY_SIZE(name) (sizeof(name) / (sizeof((name)[0]))) - -#else - -// (function taking a reference to an array and returning a pointer to -// an array of characters. it's only declared and never defined; we just -// need it to determine n, the size of the array that was passed.) -template u8 (*ArraySizeDeducer(T (&)[n]))[n]; - -// (although requiring C++, this method is much better than the standard -// sizeof(name) / sizeof(name[0]) because it doesn't compile when a -// pointer is passed, which can easily happen under maintenance.) -#define ARRAY_SIZE(name) (sizeof(*ArraySizeDeducer(name))) - -#endif // GCC_VERSION - - -//----------------------------------------------------------------------------- - template T Clamp(T val, T min, T max) { @@ -117,11 +78,6 @@ T DivideRoundUp(T dividend, T divisor) return (dividend + divisor-1) / divisor; } -/// 16-bit saturating (does not overflow) addition. -extern u16 addusw(u16 x, u16 y); -/// 16-bit saturating (does not underflow) subtraction. -extern u16 subusw(u16 x, u16 y); - /** * are the given floats nearly "equal"? * @@ -134,7 +90,7 @@ extern u16 subusw(u16 x, u16 y); * - floating-point numbers don't magically lose precision. addition, * subtraction and multiplication results are precise up to the mantissa's * least-significant bit. only division, sqrt, sin/cos and other - * trancendental operations introduce error. + * transcendental operations introduce error. **/ inline bool feq(double d1, double d2, double epsilon = 0.00001) { diff --git a/source/lib/ogl.cpp b/source/lib/ogl.cpp index 4f601c6a83..7dfb283772 100644 --- a/source/lib/ogl.cpp +++ b/source/lib/ogl.cpp @@ -51,7 +51,7 @@ extern "C" #define FUNC(ret, name, params) ret (GL_CALL_CONV *p##name) params; #define FUNC2(ret, nameARB, nameCore, version, params) ret (GL_CALL_CONV *p##nameARB) params; #define FUNC3(ret, nameARB, nameCore, version, params) ret (GL_CALL_CONV *p##nameCore) params; -#include "lib/glext_funcs.h" +#include "lib/external_libraries/glext_funcs.h" #undef FUNC3 #undef FUNC2 #undef FUNC @@ -325,7 +325,7 @@ static void importExtensionFunctions() pname = (ret (GL_CALL_CONV*) params)SDL_GL_GetProcAddress(#nameARB); #define FUNC2(ret, nameARB, nameCore, version, params) FUNC23(p##nameARB, ret, nameARB, nameCore, version, params) #define FUNC3(ret, nameARB, nameCore, version, params) FUNC23(p##nameCore, ret, nameARB, nameCore, version, params) -#include "lib/glext_funcs.h" +#include "lib/external_libraries/glext_funcs.h" #undef FUNC3 #undef FUNC2 #undef FUNC23 diff --git a/source/lib/ogl.h b/source/lib/ogl.h index 6ec7b7eef8..bcb92db440 100644 --- a/source/lib/ogl.h +++ b/source/lib/ogl.h @@ -27,46 +27,7 @@ #ifndef INCLUDED_OGL #define INCLUDED_OGL -#if OS_WIN -// wgl.h is a private header and should only be included from here. -// if this isn't defined, it'll complain. -#define WGL_HEADER_NEEDED -#include "lib/sysdep/os/win/wgl.h" -#endif - - -// -// bring in the platform's OpenGL headers (with fixes, if necessary) -// - -#if OS_MACOSX || OS_MAC -# include -#else -# include -#endif - -// if gl.h provides real prototypes for 1.2 / 1.3 functions, -// exclude the corresponding function pointers in glext_funcs.h -#ifdef GL_VERSION_1_2 -#define REAL_GL_1_2 -#endif -#ifdef GL_VERSION_1_3 -#define REAL_GL_1_3 -#endif - -// this must come after GL/gl.h include, so we can't combine the -// including GL/glext.h. -#undef GL_GLEXT_PROTOTYPES - -#if OS_MACOSX || OS_MAC -# include -#else -# include -# if OS_WIN -# include -# define GL_TEXTURE_IMAGE_SIZE_ARB 0x86A0 -# endif -#endif +#include "lib/external_libraries/opengl.h" /** @@ -131,7 +92,7 @@ extern const char* ogl_ExtensionString(); #define FUNC(ret, name, params) EXTERN_C ret (GL_CALL_CONV *p##name) params; #define FUNC2(ret, nameARB, nameCore, version, params) EXTERN_C ret (GL_CALL_CONV *p##nameARB) params; #define FUNC3(ret, nameARB, nameCore, version, params) EXTERN_C ret (GL_CALL_CONV *p##nameCore) params; -#include "lib/glext_funcs.h" +#include "lib/external_libraries/glext_funcs.h" #undef FUNC3 #undef FUNC2 #undef FUNC diff --git a/source/lib/res/sound/snd_mgr.cpp b/source/lib/res/sound/snd_mgr.cpp index 9dd9ad1a66..3baca70130 100644 --- a/source/lib/res/sound/snd_mgr.cpp +++ b/source/lib/res/sound/snd_mgr.cpp @@ -37,6 +37,7 @@ #include #include +#include "lib/alignment.h" #include "lib/res/h_mgr.h" #include "lib/file/vfs/vfs.h" diff --git a/source/lib/sysdep/arch/x86_x64/cache.cpp b/source/lib/sysdep/arch/x86_x64/cache.cpp index 82d6cfd019..97ded947af 100644 --- a/source/lib/sysdep/arch/x86_x64/cache.cpp +++ b/source/lib/sysdep/arch/x86_x64/cache.cpp @@ -24,6 +24,7 @@ #include "lib/sysdep/arch/x86_x64/cache.h" #include "lib/bits.h" +#include "lib/alignment.h" #include "lib/module_init.h" #include "lib/sysdep/os_cpu.h" #include "lib/sysdep/arch/x86_x64/x86_x64.h" diff --git a/source/lib/sysdep/os/win/wcpu.cpp b/source/lib/sysdep/os/win/wcpu.cpp index 61ac986869..fa3cff6916 100644 --- a/source/lib/sysdep/os/win/wcpu.cpp +++ b/source/lib/sysdep/os/win/wcpu.cpp @@ -29,6 +29,7 @@ #include "lib/sysdep/os_cpu.h" #include "lib/bits.h" +#include "lib/alignment.h" #include "lib/module_init.h" #include "lib/sysdep/os/win/wutil.h" #include "lib/sysdep/arch/x86_x64/x86_x64.h" diff --git a/source/lib/sysdep/os/win/wnuma.cpp b/source/lib/sysdep/os/win/wnuma.cpp index 5580e6617c..de0b69bd95 100644 --- a/source/lib/sysdep/os/win/wnuma.cpp +++ b/source/lib/sysdep/os/win/wnuma.cpp @@ -24,6 +24,7 @@ #include "lib/sysdep/numa.h" #include "lib/bits.h" // PopulationCount +#include "lib/alignment.h" #include "lib/timer.h" #include "lib/module_init.h" #include "lib/allocators/page_aligned.h" diff --git a/source/lib/sysdep/os/win/wsysdep.cpp b/source/lib/sysdep/os/win/wsysdep.cpp index 08577765ef..aafeafb9e4 100644 --- a/source/lib/sysdep/os/win/wsysdep.cpp +++ b/source/lib/sysdep/os/win/wsysdep.cpp @@ -27,6 +27,7 @@ #include "precompiled.h" #include "lib/sysdep/sysdep.h" +#include "lib/alignment.h" #include "lib/sysdep/os/win/win.h" // includes windows.h; must come before shlobj #include // pick_dir #include // open_url diff --git a/source/lib/sysdep/os_cpu.cpp b/source/lib/sysdep/os_cpu.cpp index 00f4fa318d..3df8101538 100644 --- a/source/lib/sysdep/os_cpu.cpp +++ b/source/lib/sysdep/os_cpu.cpp @@ -27,6 +27,7 @@ #include "precompiled.h" #include "lib/sysdep/os_cpu.h" +#include "lib/alignment.h" #include "lib/sysdep/smbios.h" #if OS_WIN diff --git a/source/lib/sysdep/smbios.cpp b/source/lib/sysdep/smbios.cpp index bb87308aed..eefbdd9800 100644 --- a/source/lib/sysdep/smbios.cpp +++ b/source/lib/sysdep/smbios.cpp @@ -28,6 +28,7 @@ #include "lib/sysdep/smbios.h" #include "lib/bits.h" +#include "lib/alignment.h" #include "lib/byte_order.h" // FOURCC_BE #include "lib/module_init.h" diff --git a/source/lib/tests/test_lib.h b/source/lib/tests/test_lib.h index a0f38d6ae3..772220fb8f 100644 --- a/source/lib/tests/test_lib.h +++ b/source/lib/tests/test_lib.h @@ -27,25 +27,6 @@ class TestLib : public CxxTest::TestSuite { public: - // 16-bit saturating arithmetic - void test_addusw() - { - TS_ASSERT_EQUALS(addusw(4u, 0x100u), 0x0104u); - TS_ASSERT_EQUALS(addusw(0u, 0xFFFFu), 0xFFFFu); - TS_ASSERT_EQUALS(addusw(0x8000u, 0x8000u), 0xFFFFu); - TS_ASSERT_EQUALS(addusw(0xFFF0u, 0x0004u), 0xFFF4u); - TS_ASSERT_EQUALS(addusw(0xFFFFu, 0xFFFFu), 0xFFFFu); - } - - void test_subusw() - { - TS_ASSERT_EQUALS(subusw(4u, 0x100u), 0u); - TS_ASSERT_EQUALS(subusw(100u, 90u), 10u); - TS_ASSERT_EQUALS(subusw(0x8000u, 0x8000u), 0u); - TS_ASSERT_EQUALS(subusw(0x0FFFu, 0xFFFFu), 0u); - TS_ASSERT_EQUALS(subusw(0xFFFFu, 0x0FFFu), 0xF000u); - } - void test_hi_lo() { TS_ASSERT_EQUALS(u64_hi(0x0123456789ABCDEFull), 0x01234567u); diff --git a/source/renderer/PatchRData.cpp b/source/renderer/PatchRData.cpp index 8a2cd2afc7..051d35b19b 100644 --- a/source/renderer/PatchRData.cpp +++ b/source/renderer/PatchRData.cpp @@ -24,6 +24,7 @@ #include "graphics/LightEnv.h" #include "graphics/Patch.h" #include "graphics/Terrain.h" +#include "lib/alignment.h" #include "lib/allocators/pool.h" #include "lib/res/graphics/unifont.h" #include "maths/MathUtil.h" diff --git a/source/simulation2/serialization/BinarySerializer.cpp b/source/simulation2/serialization/BinarySerializer.cpp index 6ddade2be0..b35c60f719 100644 --- a/source/simulation2/serialization/BinarySerializer.cpp +++ b/source/simulation2/serialization/BinarySerializer.cpp @@ -21,6 +21,7 @@ #include "SerializedScriptTypes.h" +#include "lib/alignment.h" #include "ps/CLogger.h" #include "scriptinterface/ScriptInterface.h"