2
3
4
5
6
7
8
9
10
11
12
13
14
23# include <type_traits>
27
28
29
42# if defined(__SH__) || defined(__sh__)
43# define SHZ_TARGET SHZ_SH4
44# elif defined(__powerpc__) || defined(__PPC__) || defined(_ARCH_PPC)
45# define SHZ_TARGET SHZ_PPC
46# elif defined(_mips) || defined(__mips)
47# define SHZ_TARGET SHZ_MIPS
48# elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
49# define SHZ_TARGET SHZ_ARM
50# elif defined(__x86_64__
) || defined(_M_X64)
52# elif defined(__SPU__)
53# define SHZ_TARGET SHZ_SPU
54# elif defined(__wasm__) || defined(__wasm32__) || defined(__wasm64__)
55# define SHZ_TARGET SHZ_WASM
57# define SHZ_TARGET SHZ_SW
63# if SHZ_TARGET == SHZ_SH4 && (defined(__SH4_SINGLE__) || defined(__SH4_SINGLE_ONLY__))
64# define SHZ_BACKEND SHZ_SH4
66# define SHZ_BACKEND SHZ_SPU
73
74
75
76#define SHZ_TLS_DISABLED 0
77#define SHZ_TLS_IMPLICIT 1
78#define SHZ_TLS_PTHREAD 2
79#define SHZ_TLS_CTHREAD 3
85# define SHZ_TLS_MODEL SHZ_TLS_IMPLICIT
87# define SHZ_TLS_MODEL SHZ_TLS_DISABLED
94
95
96
99#elif defined(__GNUC__
)
101# if defined(__clang__
)
103# elif defined(__MINGW64__)
104# define SHZ_MINGW64 1
105# elif defined(__MINGW32__)
106# define SHZ_MINGW32 1
116
117
118
120#define SHZ_STRINGIFY_LITERAL(a) #a
124#define SHZ_COUNT_OF(array) (sizeof(array) / sizeof((array)[0
]))
126#define SHZ_CONTAINER_OF(ptr, type, member) ((type*)((char*)(ptr) - offsetof(type, member)))
128#define SHZ_SWAP(a, b) do { typeof(a) tmp = a; a = b; b = tmp; } while(false)
130#define SHZ_DECLARE_STRUCT(n, t) struct n; typedef struct n t
132#define SHZ_DECLARE_STRUCT_ALIGNED(n, t, a) struct SHZ_ALIGNAS
(a) n; typedef struct n t
137
138
139
142# define SHZ_ALIGNAS(N) __attribute__((aligned((N))))
144# define SHZ_SIMD(N) __attribute__((vector_size((N))))
146# define SHZ_HOT __attribute__((hot))
148# define SHZ_COLD __attribute__((cold))
150# define SHZ_FAST_MATH __attribute__((optimize("fast-math")))
152# define SHZ_NO_FAST_MATH __attribute__((optimize("no-fast-math")))
154# define SHZ_NO_UNROLL_LOOPS __attribute__((optimize("no-unroll-loops")))
156# define SHZ_NO_OPTIMIZATION __attribute__((optimize("O0")))
158# define SHZ_FUNC_ALIGNAS(N) __attribute__((aligned(N)))
160# define SHZ_FORCE_INLINE __attribute__((always_inline)) SHZ_INLINE
162# define SHZ_NO_INLINE __attribute__((noinline))
164# define SHZ_FLATTEN __attribute__((flatten))
166# define SHZ_PACKED __attribute__((packed))
168# define SHZ_PURE __attribute__((pure))
170# define SHZ_CONST __attribute__((const))
172# define SHZ_ALIASING __attribute__((__may_alias__))
174# define SHZ_DEPRECATED(m) __attribute__((deprecated(m)))
176# define SHZ_LIKELY(e) __builtin_expect(!!(e), 1
)
178# define SHZ_UNLIKELY(e) __builtin_expect(!!(e), 0
)
180# define SHZ_RESTRICT __restrict__
182# define SHZ_MEMORY_BARRIER_SOFT() asm volatile("" : : : "memory")
184# define SHZ_MEMORY_BARRIER_HARD() __sync_synchronize()
186# define SHZ_INSTR_BARRIER() asm volatile("")
187#elif defined(SHZ_MSVC)
189# define SHZ_ALIGNAS(N) __declspec(align(N))
191# define SHZ_SIMD(N) [N/4
]
197# define SHZ_FAST_MATH
199# define SHZ_NO_FAST_MATH
201# define SHZ_NO_UNROLL_LOOPS
203# define SHZ_NO_OPTIMIZATION
205# define SHZ_FUNC_ALIGNAS(N)
207# define SHZ_FORCE_INLINE __forceinline
209# define SHZ_NO_INLINE __declspec(noinline)
215# define SHZ_PURE SHZ_CONST
217# define SHZ_CONST constexpr
221# define SHZ_DEPRECATED(m) __declspec(deprecated(m))
223# define SHZ_LIKELY(e) (e)
225# define SHZ_UNLIKELY(e) (e)
227# define SHZ_RESTRICT __restrict
229# define SHZ_MEMORY_BARRIER_SOFT()
231# define SHZ_MEMORY_BARRIER_HARD()
233# define SHZ_INSTR_BARRIER()
238# define SHZ_DECLS_BEGIN
240# define SHZ_DECLS_END
242# define SHZ_INLINE inline static
247# define SHZ_INIT(type, ...) ((type){ __VA_ARGS__ })
249# define SHZ_INIT(type, ...) ({ type r = { __VA_ARGS__ }; r; })
252# define SHZ_CONVERT(type, value)
258 static_assert(sizeof(type) == sizeof(value), "SHZ_CONVERT: Cannot convert between types of differing sizes.");
262# define SHZ_DECLS_BEGIN extern "C" {
264# define SHZ_DECLS_END }
266# define SHZ_INLINE inline
268# define SHZ_NOEXCEPT noexcept
271# define SHZ_INIT(type, ...) (type{ __VA_ARGS__ })
273# define SHZ_INIT(type, ...) ({ type r = { __VA_ARGS__ }; r; })
276# define SHZ_CONVERT(type, from)
277 [&]<typename To, typename V>(V&& value) -> To {
278 using TNR = std::remove_reference_t<To>;
279 using VNR = std::remove_reference_t<V>;
280 if constexpr (std::is_pointer_v<To>) {
281 if constexpr (std::is_pointer_v<VNR>) {
282 return reinterpret_cast<To>(value);
284 return reinterpret_cast<To>(&value);
286 } else if constexpr (std::is_reference_v<To>) {
287 static_assert(sizeof(VNR) == sizeof(TNR), "SHZ_CONVERT: Cannot convert between types of differing sizes when converting to a reference type.");
288 return reinterpret_cast<To>(value);
290 static_assert(sizeof(VNR) == sizeof(TNR), "SHZ_CONVERT: Cannot convert between types of differing sizes.");
291 return reinterpret_cast<To&>(value);
293 }.template operator
()<type>(from)
299
300
301
304# define SHZ_TLS_DECL(type, name, ...) static type name = __VA_ARGS__;
306# define SHZ_TLS_REF(name) (&name)
308# if !defined(__cplusplus) && !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L
)
310# define SHZ_THREAD_LOCAL _Thread_local
312# define SHZ_THREAD_LOCAL thread_local
315# define SHZ_TLS_DECL(type, name, ...) static SHZ_THREAD_LOCAL type name = __VA_ARGS__;
317# define SHZ_TLS_REF(name) (&name)
322# define SHZ_TLS_DECL(type, name, ...)
323 static pthread_key_t name;
324 static void tls_##name##_init_(void) {
325 int res = pthread_key_create(&name, free);
326 (void)res; assert
(!res);
328 static type* tls_##name##_ref_(void) {
329 static pthread_once_t once_ctrl = PTHREAD_ONCE_INIT
;
330 int ret = pthread_once(&once_ctrl, tls_##name##_init_);
331 (void)ret; assert
(!ret);
332 type* ptr = pthread_getspecific(name);
334 ptr = (type *)malloc(sizeof(type));
336 type temp = __VA_ARGS__;
337 memcpy(ptr, &temp, sizeof(type));
338 ret = pthread_setspecific(name, ptr);
344# define SHZ_TLS_REF(name) tls_##name##_ref_()
345#elif SHZ_TLS_MODEL == SHZ_TLS_CTHREAD
349# define SHZ_TLS_DECL(type, name, ...)
351 static void tls_##name##_init_(void) {
352 int res = tss_create(&name, free);
353 (void)res; assert(res == thrd_success);
355 static type* tls_##name##_ref_(void) {
356 static once_flag once_ctrl = ONCE_FLAG_INIT;
357 call_once(&once_ctrl, tls_##name##_init_);
358 type* ptr = (type *)tss_get(name);
360 ptr = (type *)malloc(sizeof(type));
362 type temp = __VA_ARGS__;
363 memcpy(ptr, &temp, sizeof(type));
364 int res = tss_set(name, ptr);
365 (void)res; assert(res == thrd_success);
370# define SHZ_TLS_REF(name) tls_##name##_ref_()
375
376
377
#define SHZ_SH4
Back-end for the Dreamcast's SH4.
SHZ_ALIASING int16_t shz_alias_int16_t
int16_t type whose value may be aliased as another type.
SHZ_ALIASING double shz_alias_double_t
double type whose value may be aliased as another type.
#define SHZ_TLS_IMPLICIT
Use thread_local keyword for TLS.
SHZ_ALIASING uint32_t shz_alias_uint32_t
uint32_t type whose value may be aliased as another type.
#define SHZ_SW
Generic C-based software back-end.
#define SHZ_STRINGIFY_LITERAL(a)
Stringifies a literal expression.
#define SHZ_X86_64
Back-end for x86_64 architectures.
SHZ_ALIASING int64_t shz_alias_int64_t
int64_t type whose value may be aliased as another type.
SHZ_ALIASING uint16_t shz_alias_uint16_t
uint16_t type whose value may be aliased as another type.
SHZ_ALIASING int32_t shz_alias_int32_t
int32_t type whose value may be aliased as another type.
SHZ_ALIASING uint64_t shz_alias_uint64_t
uint64_t type whose value may be aliased as another type.
SHZ_ALIASING float shz_alias_float_t
float type whose value may be aliased as another type.
#define SHZ_PPC
Back-end for the Gamecube/Wii's Gekko PPC.
#define SHZ_SPU
Back-end for the Cell B/E SPU architecture.
#define SHZ_TLS_PTHREAD
Use pthread unique keys for TLS.
#define SHZ_TLS_DISABLED
No thread-local variables.