Merge pull request #438 from s4Ys369/revert-434-patch-1
[sm64pc.git] / include / macros.h
blob85512d2310455567d7cd4fe7eca1e9c4cf0c7334
1 #ifndef _MACROS_H_
2 #define _MACROS_H_
4 #include "platform_info.h"
6 #ifndef __sgi
7 #define GLOBAL_ASM(...)
8 #endif
10 #if !defined(__sgi) && (!defined(NON_MATCHING) || !defined(AVOID_UB))
11 // asm-process isn't supported outside of IDO, and undefined behavior causes
12 // crashes.
13 #error Matching build is only possible on IDO; please build with NON_MATCHING=1.
14 #endif
16 #define ARRAY_COUNT(arr) (s32)(sizeof(arr) / sizeof(arr[0]))
18 #define GLUE(a, b) a ## b
19 #define GLUE2(a, b) GLUE(a, b)
21 // Avoid compiler warnings for unused variables
22 #ifdef __GNUC__
23 #define UNUSED __attribute__((unused))
24 #else
25 #define UNUSED
26 #endif
28 // Static assertions
29 #ifdef __GNUC__
30 #define STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
31 #else
32 #define STATIC_ASSERT(cond, msg) typedef char GLUE2(static_assertion_failed, __LINE__)[(cond) ? 1 : -1]
33 #endif
35 // Align to 8-byte boundary for DMA requirements
36 #ifdef __GNUC__
37 #define ALIGNED8 __attribute__((aligned(8)))
38 #else
39 #define ALIGNED8
40 #endif
42 // Align to 16-byte boundary for audio lib requirements
43 #ifdef __GNUC__
44 #define ALIGNED16 __attribute__((aligned(16)))
45 #else
46 #define ALIGNED16
47 #endif
49 // no conversion for pc port other than cast
50 #define VIRTUAL_TO_PHYSICAL(addr) ((uintptr_t)(addr))
51 #define PHYSICAL_TO_VIRTUAL(addr) ((uintptr_t)(addr))
52 #define VIRTUAL_TO_PHYSICAL2(addr) ((void *)(addr))
54 // Byteswap macros
55 #define BSWAP16(x) (((x) & 0xFF) << 8 | (((x) >> 8) & 0xFF))
56 #define BSWAP32(x) \
57 ( (((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) | \
58 (((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000) )
60 // Convenience macros for endian conversions
61 #if IS_BIG_ENDIAN
62 # define BE_TO_HOST16(x) (x)
63 # define BE_TO_HOST32(x) (x)
64 # define LE_TO_HOST16(x) BSWAP16(x)
65 # define LE_TO_HOST32(x) BSWAP32(x)
66 #else
67 # define BE_TO_HOST16(x) BSWAP16(x)
68 # define BE_TO_HOST32(x) BSWAP32(x)
69 # define LE_TO_HOST16(x) (x)
70 # define LE_TO_HOST32(x) (x)
71 #endif
73 #endif