From 668a52bde0b9e1d8820ed394c9ab13180f849fdd Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 1 Oct 2022 08:43:23 +0100 Subject: [PATCH 1/2] Adding SDL_Assume macro to give hint to the optimiser with a given condition generating less instructions. --- include/SDL3/SDL_assert.h | 12 ++++++++++++ test/testplatform.c | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/SDL3/SDL_assert.h b/include/SDL3/SDL_assert.h index c2ecadbcf..3a57f8384 100644 --- a/include/SDL3/SDL_assert.h +++ b/include/SDL3/SDL_assert.h @@ -80,6 +80,18 @@ assert can have unique static variables associated with it. #define SDL_TriggerBreakpoint() #endif +#if defined(_MSC_VER) && (_MSC_VER > 1400) + #define SDL_Assume(cond) __assume(cond) +#elif _SDL_HAS_BUILTIN(__builtin_assume) + #define SDL_Assume(cond) __builtin_assume(cond) +#elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))) + #define SDL_Assume(cond) do { \ + (__builtin_expect(!(cond), 0) ? __builtin_unreachable() : (void)0); \ + } while (0) +#else + #define SDL_Assume(cond) +#endif + #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */ # define SDL_FUNCTION __func__ #elif ((defined(__GNUC__) && (__GNUC__ >= 2)) || defined(_MSC_VER) || defined (__WATCOMC__)) diff --git a/test/testplatform.c b/test/testplatform.c index dee807dd2..c5a78c84c 100644 --- a/test/testplatform.c +++ b/test/testplatform.c @@ -438,7 +438,25 @@ static int TestAssertions(SDL_bool verbose) return 0; } +<<<<<<< HEAD int main(int argc, char *argv[]) +======= +int +TestAssume(SDL_bool verbose) +{ + int max, count, i; + + max = 16; + srand(time(0)); + count = rand() % max; + SDL_Assume(count <= max); + for (i = 0; i < count; i ++); + return (0); +} + +int +main(int argc, char *argv[]) +>>>>>>> bfb4ecda9 (Adding SDL_Assume macro to give hint to the optimiser) { int i; SDL_bool verbose = SDL_TRUE; @@ -483,6 +501,7 @@ int main(int argc, char *argv[]) status += Test64Bit(verbose); status += TestCPUInfo(verbose); status += TestAssertions(verbose); + status += TestAssume(verbose); SDLTest_CommonDestroyState(state); From cdbcd73bb9d37f4bc1c021fbef970ecb73f8317d Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 10 Mar 2024 18:04:47 +0000 Subject: [PATCH 2/2] fix build --- include/SDL3/SDL_assert.h | 2 +- test/testplatform.c | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/include/SDL3/SDL_assert.h b/include/SDL3/SDL_assert.h index 3a57f8384..4dce8f2de 100644 --- a/include/SDL3/SDL_assert.h +++ b/include/SDL3/SDL_assert.h @@ -82,7 +82,7 @@ assert can have unique static variables associated with it. #if defined(_MSC_VER) && (_MSC_VER > 1400) #define SDL_Assume(cond) __assume(cond) -#elif _SDL_HAS_BUILTIN(__builtin_assume) +#elif SDL_HAS_BUILTIN(__builtin_assume) #define SDL_Assume(cond) __builtin_assume(cond) #elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))) #define SDL_Assume(cond) do { \ diff --git a/test/testplatform.c b/test/testplatform.c index c5a78c84c..1298cba8b 100644 --- a/test/testplatform.c +++ b/test/testplatform.c @@ -438,17 +438,13 @@ static int TestAssertions(SDL_bool verbose) return 0; } -<<<<<<< HEAD -int main(int argc, char *argv[]) -======= int TestAssume(SDL_bool verbose) { int max, count, i; max = 16; - srand(time(0)); - count = rand() % max; + count = SDLTest_RandomIntegerInRange(0, max); SDL_Assume(count <= max); for (i = 0; i < count; i ++); return (0); @@ -456,7 +452,6 @@ TestAssume(SDL_bool verbose) int main(int argc, char *argv[]) ->>>>>>> bfb4ecda9 (Adding SDL_Assume macro to give hint to the optimiser) { int i; SDL_bool verbose = SDL_TRUE;