Kohi Game Engine
defines.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 // Unsigned int types.
17 
19 typedef unsigned char u8;
20 
22 typedef unsigned short u16;
23 
25 typedef unsigned int u32;
26 
28 typedef unsigned long long u64;
29 
30 // Signed int types.
31 
33 typedef signed char i8;
34 
36 typedef signed short i16;
37 
39 typedef signed int i32;
40 
42 typedef signed long long i64;
43 
44 // Floating point types
45 
47 typedef float f32;
48 
50 typedef double f64;
51 
52 // Boolean types
53 
55 typedef int b32;
56 
58 typedef _Bool b8;
59 
61 typedef struct krange {
67 
69 typedef struct range32 {
75 // Properly define static assertions.
76 #if defined(__clang__) || defined(__GNUC__)
78 # define STATIC_ASSERT _Static_assert
79 #else
80 
82 # define STATIC_ASSERT static_assert
83 #endif
84 
85 // Ensure all types are of the correct size.
86 
88 STATIC_ASSERT(sizeof(u8) == 1, "Expected u8 to be 1 byte.");
89 
91 STATIC_ASSERT(sizeof(u16) == 2, "Expected u16 to be 2 bytes.");
92 
94 STATIC_ASSERT(sizeof(u32) == 4, "Expected u32 to be 4 bytes.");
95 
97 STATIC_ASSERT(sizeof(u64) == 8, "Expected u64 to be 8 bytes.");
98 
100 STATIC_ASSERT(sizeof(i8) == 1, "Expected i8 to be 1 byte.");
101 
103 STATIC_ASSERT(sizeof(i16) == 2, "Expected i16 to be 2 bytes.");
104 
106 STATIC_ASSERT(sizeof(i32) == 4, "Expected i32 to be 4 bytes.");
107 
109 STATIC_ASSERT(sizeof(i64) == 8, "Expected i64 to be 8 bytes.");
110 
112 STATIC_ASSERT(sizeof(f32) == 4, "Expected f32 to be 4 bytes.");
113 
115 STATIC_ASSERT(sizeof(f64) == 8, "Expected f64 to be 8 bytes.");
116 
118 #define true 1
119 
121 #define false 0
122 
127 #define INVALID_ID_U64 18446744073709551615UL
128 #define INVALID_ID 4294967295U
129 #define INVALID_ID_U32 INVALID_ID
130 #define INVALID_ID_U16 65535U
131 #define INVALID_ID_U8 255U
132 
133 #define U64_MAX 18446744073709551615UL
134 #define U32_MAX 4294967295U
135 #define U16_MAX 65535U
136 #define U8_MAX 255U
137 #define U64_MIN 0UL
138 #define U32_MIN 0U
139 #define U16_MIN 0U
140 #define U8_MIN 0U
141 
142 #define I8_MAX 127
143 #define I16_MAX 32767
144 #define I32_MAX 2147483647
145 #define I64_MAX 9223372036854775807L
146 #define I8_MIN (-I8_MAX - 1)
147 #define I16_MIN (-I16_MAX - 1)
148 #define I32_MIN (-I32_MAX - 1)
149 #define I64_MIN (-I64_MAX - 1)
150 
151 // Platform detection
152 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
153 # define KPLATFORM_WINDOWS 1
154 # ifndef _WIN64
155 # error "64-bit is required on Windows!"
156 # endif
157 #elif defined(__linux__) || defined(__gnu_linux__)
158 // Linux OS
159 # define KPLATFORM_LINUX 1
160 # if defined(__ANDROID__)
161 # define KPLATFORM_ANDROID 1
162 # endif
163 #elif defined(__unix__)
164 // Catch anything not caught by the above.
165 # define KPLATFORM_UNIX 1
166 #elif defined(_POSIX_VERSION)
167 // Posix
168 # define KPLATFORM_POSIX 1
169 #elif __APPLE__
170 // Apple platforms
171 # define KPLATFORM_APPLE 1
172 # include <TargetConditionals.h>
173 # if TARGET_IPHONE_SIMULATOR
174 // iOS Simulator
175 # define KPLATFORM_IOS 1
176 # define KPLATFORM_IOS_SIMULATOR 1
177 # elif TARGET_OS_IPHONE
178 # define KPLATFORM_IOS 1
179 // iOS device
180 # elif TARGET_OS_MAC
181 // HACK: Should probably be in the Vulkan Renderer lib, not here.
182 # define VK_USE_PLATFORM_MACOS_MVK
183 // Other kinds of Mac OS
184 # else
185 # error "Unknown Apple platform"
186 # endif
187 #else
188 # error "Unknown platform!"
189 #endif
190 
191 #ifdef KEXPORT
192 // Exports
193 # ifdef _MSC_VER
194 # define KAPI __declspec(dllexport)
195 # else
196 # define KAPI __attribute__((visibility("default")))
197 # endif
198 #else
199 // Imports
200 # ifdef _MSC_VER
202 # define KAPI __declspec(dllimport)
203 # else
205 # define KAPI
206 # endif
207 #endif
208 
209 #if _DEBUG
210 # define KOHI_DEBUG 1
211 # define KOHI_RELEASE 0
212 #else
213 # define KOHI_RELEASE 1
214 # define KOHI_DEBUG 0
215 #endif
216 
217 // Feature build flags.
218 
219 #if KOHI_DEBUG
220 # define KOHI_HOT_RELOAD 1
221 #else
222 # define KOHI_HOT_RELOAD 0
223 #endif
224 
232 #define KCLAMP(value, min, max) ((value <= min) ? min : (value >= max) ? max \
233  : value)
234 
235 // Inlining
236 #if defined(__clang__) || defined(__gcc__)
238 # define KINLINE __attribute__((always_inline)) inline
239 
241 # define KNOINLINE __attribute__((noinline))
242 #elif defined(_MSC_VER)
243 
245 # define KINLINE __forceinline
246 
248 # define KNOINLINE __declspec(noinline)
249 #else
250 
252 # define KINLINE static inline
253 
255 # define KNOINLINE
256 #endif
257 
258 // Deprecation
259 #if defined(__clang__) || defined(__gcc__)
261 # define KDEPRECATED(message) __attribute__((deprecated(message)))
262 #elif defined(_MSC_VER)
264 # define KDEPRECATED(message) __declspec(deprecated(message))
265 #else
266 # error "Unsupported compiler - don't know how to define deprecations!"
267 #endif
268 
270 #define GIBIBYTES(amount) ((amount) * 1024ULL * 1024ULL * 1024ULL)
272 #define MEBIBYTES(amount) ((amount) * 1024ULL * 1024ULL)
274 #define KIBIBYTES(amount) ((amount) * 1024ULL)
275 
277 #define GIGABYTES(amount) ((amount) * 1000ULL * 1000ULL * 1000ULL)
279 #define MEGABYTES(amount) ((amount) * 1000ULL * 1000ULL)
281 #define KILOBYTES(amount) ((amount) * 1000ULL)
282 
283 KINLINE u64 get_aligned(u64 operand, u64 granularity) {
284  return ((operand + (granularity - 1)) & ~(granularity - 1));
285 }
286 
287 KINLINE krange get_aligned_range(u64 offset, u64 size, u64 granularity) {
288  return (krange){get_aligned(offset, granularity), get_aligned(size, granularity)};
289 }
290 
291 #define KMIN(x, y) (x < y ? x : y)
292 #define KMAX(x, y) (x > y ? x : y)
293 
297 #define FLAG_GET(flags, flag) ((flags & flag) == flag)
298 
306 #define FLAG_SET(flags, flag, enabled) (flags = (enabled ? (flags | flag) : (flags & ~flag)))
struct krange krange
A range, typically of memory.
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
#define STATIC_ASSERT
Static assertion.
Definition: defines.h:82
signed char i8
Signed 8-bit integer.
Definition: defines.h:33
KINLINE krange get_aligned_range(u64 offset, u64 size, u64 granularity)
Definition: defines.h:287
_Bool b8
8-bit boolean type
Definition: defines.h:58
struct range32 range32
A range, typically of memory.
float f32
32-bit floating point number
Definition: defines.h:47
double f64
64-bit floating point number
Definition: defines.h:50
signed int i32
Signed 32-bit integer.
Definition: defines.h:39
unsigned short u16
Unsigned 16-bit integer.
Definition: defines.h:22
#define KINLINE
Inline qualifier.
Definition: defines.h:252
KINLINE u64 get_aligned(u64 operand, u64 granularity)
Definition: defines.h:283
signed short i16
Signed 16-bit integer.
Definition: defines.h:36
int b32
32-bit boolean type, used for APIs which require it
Definition: defines.h:55
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
signed long long i64
Signed 64-bit integer.
Definition: defines.h:42
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:19
A range, typically of memory.
Definition: defines.h:61
u64 offset
The offset in bytes.
Definition: defines.h:63
u64 size
The size in bytes.
Definition: defines.h:65
A range, typically of memory.
Definition: defines.h:69
i32 offset
The offset in bytes.
Definition: defines.h:71
i32 size
The size in bytes.
Definition: defines.h:73