Skip to content

Adding SDL_Assume macro to give hint to the optimiser #6309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/SDL3/SDL_assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__))
Expand Down
16 changes: 15 additions & 1 deletion test/testplatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,20 @@ static int TestAssertions(SDL_bool verbose)
return 0;
}

int main(int argc, char *argv[])
int
TestAssume(SDL_bool verbose)
{
int max, count, i;

max = 16;
count = SDLTest_RandomIntegerInRange(0, max);
SDL_Assume(count <= max);
for (i = 0; i < count; i ++);
return (0);
}

int
main(int argc, char *argv[])
{
int i;
SDL_bool verbose = SDL_TRUE;
Expand Down Expand Up @@ -483,6 +496,7 @@ int main(int argc, char *argv[])
status += Test64Bit(verbose);
status += TestCPUInfo(verbose);
status += TestAssertions(verbose);
status += TestAssume(verbose);

SDLTest_CommonDestroyState(state);

Expand Down