#ifndef _AL_MACROS_H #define _AL_MACROS_H #define AL_PASTER_EVALUATOR(X, Y) X##_##Y #define AL_PASTER(X, Y) AL_PASTER_EVALUATOR(X, Y) #if defined LIKELY || defined UNLIKELY #error "Conflicting definitions for LIKELY/UNLIKELY" #endif #ifdef AL_HAVE_BUILTIN_EXPECT #define LIKELY(x) __builtin_expect((x), 1) #else #define LIKELY(x) x #endif #ifdef AL_HAVE_BUILTIN_EXPECT #define UNLIKELY(x) __builtin_expect((x), 0) #else #define UNLIKELY(x) x #endif #if defined MIN || defined MAX || defined CLAMP || defined SWAP || defined ARRAY_SIZE || defined STR || defined XSTR || defined BOOLSTR #error "Conflicting definitions for common macros" #endif #ifdef AL_HAVE_GNU_EXTENSIONS #define MIN(a, b) \ ({ \ __typeof__(a) ac = a; \ __typeof__(b) bc = b; \ (void)(&ac == &bc); \ (ac < bc) ? ac : bc; \ }) #define MAX(a, b) \ ({ \ __typeof__(a) ac = a; \ __typeof__(b) bc = b; \ (void)(&ac == &bc); \ (ac > bc) ? ac : bc; \ }) #define CLAMP(n, lo, hi) MIN(hi, MAX(lo, n)) #else /* #error "MIN/MAX/CLAMP should not be used in this mode as they\ differ in behavior without support for statement expressions" */ // Compatible/Pedantic min/max. #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define CLAMP(n, lo, hi) MIN(hi, MAX(lo, n)) #endif #define SWAP(a, b) \ do { \ __typeof__(a) tmp = a; \ a = b; \ b = tmp; \ } while (0) #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) #define STR(s) #s #define XSTR(s) STR(s) #define BOOLSTR(b) ((b) ? "true" : "false") #if defined KB || defined MB || defined GB || defined TB #error "Conflicting definitions for byte size macros" #endif #define KB(n) ((n) << 10) #define MB(n) ((n) << 20) #define GB(n) ((n) << 30) #define TB(n) (((u64)n) << 40) //#if defined __counted_by || defined __sized_by #if defined __sized_by #error "Conflicting definitions for common attributes" #endif // https://github.com/google/double-conversion/pull/131/files #ifdef __has_attribute #define AL_HAS_ATTRIBUTE(x) __has_attribute(x) #else #define AL_HAS_ATTRIBUTE(x) 0 #endif //#if AL_HAS_ATTRIBUTE(__counted_by__) //#define __counted_by(member) __attribute__((__counted_by__(member))) //#else //#define __counted_by(member) //#endif #if AL_HAS_ATTRIBUTE(__sized_by__) #define __sized_by(member) __attribute__((__sized_by__(member))) #else #define __sized_by(member) #endif #ifdef _MSC_VER #define __thread __declspec(thread) #endif #endif // _AL_MACROS_H