From 5472d8418065bfd3a6e1ce3cd0afa6b60c11eb6e Mon Sep 17 00:00:00 2001 From: Steven Noonan Date: Sat, 13 Mar 2021 17:48:41 -0800 Subject: [PATCH] introduce MA_ASSUME macro This macro can be used to tell the compiler's optimization passes static assumptions which you *know* are true about code behavior. Use of these can be risky -- if you assume incorrectly, the compiler may emit code that will not work in circumstances you didn't anticipate. On the other hand, use of this macro in places where the optimizer is missing an assumption that would have been safe to make can cause it to emit more compact/optimal code. Signed-off-by: Steven Noonan --- miniaudio.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/miniaudio.h b/miniaudio.h index e22f4b1f..6d648d61 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -6740,6 +6740,18 @@ static MA_INLINE ma_bool32 ma_has_neon(void) #define MA_COMPILER_HAS_BUILTIN(x) 0 #endif +#ifndef MA_ASSUME + #if MA_COMPILER_HAS_BUILTIN(__builtin_assume) + #define MA_ASSUME(x) __builtin_assume(x) + #elif MA_COMPILER_HAS_BUILTIN(__builtin_unreachable) + #define MA_ASSUME(x) do { if (!(x)) __builtin_unreachable(); } while (0) + #elif defined(_MSC_VER) + #define MA_ASSUME(x) __assume(x) + #else + #define MA_ASSUME(x) while(0) + #endif +#endif + #if defined(_MSC_VER) && _MSC_VER >= 1400 #define MA_HAS_BYTESWAP16_INTRINSIC #define MA_HAS_BYTESWAP32_INTRINSIC