Skip to content

Commit 668a52b

Browse files
committed
Adding SDL_Assume macro to give hint to the optimiser
with a given condition generating less instructions.
1 parent b9ab326 commit 668a52b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

include/SDL3/SDL_assert.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ assert can have unique static variables associated with it.
8080
#define SDL_TriggerBreakpoint()
8181
#endif
8282

83+
#if defined(_MSC_VER) && (_MSC_VER > 1400)
84+
#define SDL_Assume(cond) __assume(cond)
85+
#elif _SDL_HAS_BUILTIN(__builtin_assume)
86+
#define SDL_Assume(cond) __builtin_assume(cond)
87+
#elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4)))
88+
#define SDL_Assume(cond) do { \
89+
(__builtin_expect(!(cond), 0) ? __builtin_unreachable() : (void)0); \
90+
} while (0)
91+
#else
92+
#define SDL_Assume(cond)
93+
#endif
94+
8395
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
8496
# define SDL_FUNCTION __func__
8597
#elif ((defined(__GNUC__) && (__GNUC__ >= 2)) || defined(_MSC_VER) || defined (__WATCOMC__))

test/testplatform.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,25 @@ static int TestAssertions(SDL_bool verbose)
438438
return 0;
439439
}
440440

441+
<<<<<<< HEAD
441442
int main(int argc, char *argv[])
443+
=======
444+
int
445+
TestAssume(SDL_bool verbose)
446+
{
447+
int max, count, i;
448+
449+
max = 16;
450+
srand(time(0));
451+
count = rand() % max;
452+
SDL_Assume(count <= max);
453+
for (i = 0; i < count; i ++);
454+
return (0);
455+
}
456+
457+
int
458+
main(int argc, char *argv[])
459+
>>>>>>> bfb4ecda9 (Adding SDL_Assume macro to give hint to the optimiser)
442460
{
443461
int i;
444462
SDL_bool verbose = SDL_TRUE;
@@ -483,6 +501,7 @@ int main(int argc, char *argv[])
483501
status += Test64Bit(verbose);
484502
status += TestCPUInfo(verbose);
485503
status += TestAssertions(verbose);
504+
status += TestAssume(verbose);
486505

487506
SDLTest_CommonDestroyState(state);
488507

0 commit comments

Comments
 (0)