Simple Graphics Library 1.0.0.0
Runtime-independent image processing primitives
Loading...
Searching...
No Matches
sgl-compiler.h
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2025 Dylan Hong
5 *
6 * This code is released under the MIT License.
7 * For conditions of distribution and use, see the LICENSE file.
8 */
9#ifndef SGL_COMPILER_H_
10#define SGL_COMPILER_H_
11
31#define SGL_PREFETCH_RW_READ (0)
33#define SGL_PREFETCH_RW_WRITE (1)
34
36#define SGL_PREFETCH_LOCALITY_NON_TEMPORAL (0)
38#define SGL_PREFETCH_LOCALITY_LOW (1)
40#define SGL_PREFETCH_LOCALITY_MODERATE (2)
42#define SGL_PREFETCH_LOCALITY_HIGH (3)
43
88/*
89 * Branch prediction contract
90 * --------------------------
91 * Use SGL_LIKELY and SGL_UNLIKELY only when one outcome is consistently
92 * dominant across supported workloads. Input-dependent hints can penalize
93 * the opposite workload, and unnecessary hints can inhibit branchless code
94 * generation.
95 */
96#if defined(_MSC_VER)
97
98#define SGL_ALWAYS_INLINE __forceinline
99#define SGL_NOINLINE __declspec(noinline)
100#define SGL_RESTRICT __restrict
101#define SGL_ALIGNED(n) __declspec(align(n))
102#define SGL_PACKED
103#define SGL_NORETURN __declspec(noreturn)
104#define SGL_DEPRECATED(message) __declspec(deprecated(message))
105#define SGL_WARN_UNUSED_RESULT
106#define SGL_LIKELY(expression) (!!(expression))
107#define SGL_UNLIKELY(expression) (!!(expression))
108#define SGL_FALLTHROUGH
109#define SGL_UNREACHABLE() __assume(0)
110#define SGL_CPU_RELAX() ((void)0)
111#define SGL_PREFETCH(address, byte_distance, rw, locality) \
112 ((void)(address), (void)(byte_distance), (void)(rw), (void)(locality))
113
114#elif defined(__GNUC__) || defined(__clang__)
115
116#define SGL_ALWAYS_INLINE inline __attribute__((always_inline))
117#define SGL_NOINLINE __attribute__((noinline))
118#define SGL_RESTRICT __restrict__
119#define SGL_ALIGNED(n) __attribute__((aligned(n)))
120#define SGL_PACKED __attribute__((packed))
121#define SGL_NORETURN __attribute__((noreturn))
122#define SGL_DEPRECATED(message) __attribute__((deprecated(message)))
123#define SGL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
124#define SGL_LIKELY(expression) __builtin_expect(!!(expression), 1)
125#define SGL_UNLIKELY(expression) __builtin_expect(!!(expression), 0)
126#define SGL_FALLTHROUGH __attribute__((fallthrough))
127#define SGL_UNREACHABLE() __builtin_unreachable()
128#if defined(__aarch64__) || defined(__arm__)
129#define SGL_CPU_RELAX() __asm__ __volatile__("yield")
130#elif defined(__i386__) || defined(__x86_64__)
131#define SGL_CPU_RELAX() __asm__ __volatile__("pause")
132#else
133#define SGL_CPU_RELAX() __asm__ __volatile__("" ::: "memory")
134#endif
135#define SGL_PREFETCH(address, byte_distance, rw, locality) \
136 __builtin_prefetch( \
137 ((const char *)(address)) + (byte_distance), \
138 (rw), \
139 (locality))
140
141#else
142
143#define SGL_ALWAYS_INLINE inline
144#define SGL_NOINLINE
145#define SGL_RESTRICT
146#define SGL_ALIGNED(n)
147#define SGL_PACKED
148#define SGL_NORETURN
149#define SGL_DEPRECATED(message)
150#define SGL_WARN_UNUSED_RESULT
151#define SGL_LIKELY(expression) (!!(expression))
152#define SGL_UNLIKELY(expression) (!!(expression))
153#define SGL_FALLTHROUGH
154#define SGL_UNREACHABLE() ((void)0)
155#define SGL_CPU_RELAX() ((void)0)
156#define SGL_PREFETCH(address, byte_distance, rw, locality) \
157 ((void)(address), (void)(byte_distance), (void)(rw), (void)(locality))
158
159#endif
160
162#define SGL_PREFETCH_READ(address, byte_distance) \
163 SGL_PREFETCH( \
164 address, \
165 byte_distance, \
166 SGL_PREFETCH_RW_READ, \
167 SGL_PREFETCH_LOCALITY_HIGH)
168
170#define SGL_PREFETCH_WRITE(address, byte_distance) \
171 SGL_PREFETCH( \
172 address, \
173 byte_distance, \
174 SGL_PREFETCH_RW_WRITE, \
175 SGL_PREFETCH_LOCALITY_HIGH)
176
178#define SGL_UNUSED(value) ((void)(value))
181#endif /* SGL_COMPILER_H_ */