diff --git a/Sources/Core/Debug.h b/Sources/Core/Debug.h index 16af52473..893dc7ecc 100644 --- a/Sources/Core/Debug.h +++ b/Sources/Core/Debug.h @@ -102,6 +102,12 @@ namespace spades { #define __PRETTY_FUNCTION__ __FUNCDNAME__ #endif +#if defined(__GNUC__) && defined(__GNUC_MINOR__) +#define GCCVERSION (__GNUC__ * 1000 + __GNUC_MINOR__) +#else +#define GCCVERSION 0 +#endif + #define SPADES_MARK_FUNCTION() \ static constexpr ::spades::reflection::Function thisFunction{__PRETTY_FUNCTION__, __FILE__, \ __LINE__}; \ @@ -124,6 +130,23 @@ namespace spades { #define SPAssert(cond) ((!(cond)) ? SPRaise("SPAssertion failed: %s", #cond) : (void)0) #endif +#if NDEBUG +#ifdef _MSC_VER +#define SPAssume(cond) __assume(cond) +#elif defined(__clang__) && __clang_major__ > 3 +#define SPAssume(cond) __builtin_assume(cond) +#elif defined(__GNUC__) && GCCVERSION >= 4005 +#define SPAssume(cond) \ + do { \ + if (!(cond)) __builtin_unreachable(); \ + } while (0) +#else +#define SPAssume(cond) +#endif +#else +#define SPAssume(cond) SPAssert(cond) +#endif + #ifdef _MSC_VER #define SPLog(format, ...) ::spades::LogMessage(__FILE__, __LINE__, format, __VA_ARGS__) #else diff --git a/Sources/Draw/SWImageRenderer.cpp b/Sources/Draw/SWImageRenderer.cpp index d46e45d0d..d328ff51e 100644 --- a/Sources/Draw/SWImageRenderer.cpp +++ b/Sources/Draw/SWImageRenderer.cpp @@ -410,7 +410,7 @@ namespace spades { if (depthTest) { depthOut = depthBuffer + (y * fbW); } - SPAssert(x1 < x2); + SPAssume(x1 < x2); int width = x2 - x1; SWImageGouraudInterpolator vary(vary1, vary2, width); int minX = std::max(x1, 0); diff --git a/Sources/Draw/SWModelRenderer.cpp b/Sources/Draw/SWModelRenderer.cpp index c7703b360..14e50120a 100644 --- a/Sources/Draw/SWModelRenderer.cpp +++ b/Sources/Draw/SWModelRenderer.cpp @@ -189,7 +189,7 @@ namespace spades { uint32_t normal = *(mp++); int z = static_cast(data >> 24); // SPAssert(z < d); - SPAssert(z >= 0); + SPAssume(z >= 0); auto vv = v2 + tAxis3 * zvals[z]; if (vv.z < zNear) diff --git a/Sources/Draw/SWRenderer.cpp b/Sources/Draw/SWRenderer.cpp index 19968db00..bc4be8dff 100644 --- a/Sources/Draw/SWRenderer.cpp +++ b/Sources/Draw/SWRenderer.cpp @@ -242,10 +242,10 @@ namespace spades { int maxY = light.maxY; int lightHeight = maxY - minY; - SPAssert(minX >= 0); - SPAssert(minY >= 0); - SPAssert(maxX <= fw); - SPAssert(maxY <= fh); + SPAssume(minX >= 0); + SPAssume(minY >= 0); + SPAssume(maxX <= fw); + SPAssume(maxY <= fh); Vector3 lightCenter; Vector3 diff = light.param.origin - sceneDef.viewOrigin; diff --git a/Sources/Draw/SWUtils.h b/Sources/Draw/SWUtils.h index 966ec839a..c90e3dc7d 100644 --- a/Sources/Draw/SWUtils.h +++ b/Sources/Draw/SWUtils.h @@ -32,7 +32,7 @@ namespace spades { int GetNumSWRendererThreads(); template static void InvokeParallel(F f, unsigned int numThreads) { - SPAssert(numThreads <= 32); + SPAssume(numThreads <= 32); std::array, 32> disp; for (auto i = 1U; i < numThreads; i++) { auto ff = [i, &f]() { f(i); };