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 range {
67 
68 // Properly define static assertions.
69 #if defined(__clang__) || defined(__GNUC__)
71 #define STATIC_ASSERT _Static_assert
72 #else
73 
75 #define STATIC_ASSERT static_assert
76 #endif
77 
78 // Ensure all types are of the correct size.
79 
81 STATIC_ASSERT(sizeof(u8) == 1, "Expected u8 to be 1 byte.");
82 
84 STATIC_ASSERT(sizeof(u16) == 2, "Expected u16 to be 2 bytes.");
85 
87 STATIC_ASSERT(sizeof(u32) == 4, "Expected u32 to be 4 bytes.");
88 
90 STATIC_ASSERT(sizeof(u64) == 8, "Expected u64 to be 8 bytes.");
91 
93 STATIC_ASSERT(sizeof(i8) == 1, "Expected i8 to be 1 byte.");
94 
96 STATIC_ASSERT(sizeof(i16) == 2, "Expected i16 to be 2 bytes.");
97 
99 STATIC_ASSERT(sizeof(i32) == 4, "Expected i32 to be 4 bytes.");
100 
102 STATIC_ASSERT(sizeof(i64) == 8, "Expected i64 to be 8 bytes.");
103 
105 STATIC_ASSERT(sizeof(f32) == 4, "Expected f32 to be 4 bytes.");
106 
108 STATIC_ASSERT(sizeof(f64) == 8, "Expected f64 to be 8 bytes.");
109 
111 #define true 1
112 
114 #define false 0
115 
120 #define INVALID_ID_U64 18446744073709551615UL
121 #define INVALID_ID 4294967295U
122 #define INVALID_ID_U16 65535U
123 #define INVALID_ID_U8 255U
124 
125 // Platform detection
126 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
127 #define KPLATFORM_WINDOWS 1
128 #ifndef _WIN64
129 #error "64-bit is required on Windows!"
130 #endif
131 #elif defined(__linux__) || defined(__gnu_linux__)
132 // Linux OS
133 #define KPLATFORM_LINUX 1
134 #if defined(__ANDROID__)
135 #define KPLATFORM_ANDROID 1
136 #endif
137 #elif defined(__unix__)
138 // Catch anything not caught by the above.
139 #define KPLATFORM_UNIX 1
140 #elif defined(_POSIX_VERSION)
141 // Posix
142 #define KPLATFORM_POSIX 1
143 #elif __APPLE__
144 // Apple platforms
145 #define KPLATFORM_APPLE 1
146 #include <TargetConditionals.h>
147 #if TARGET_IPHONE_SIMULATOR
148 // iOS Simulator
149 #define KPLATFORM_IOS 1
150 #define KPLATFORM_IOS_SIMULATOR 1
151 #elif TARGET_OS_IPHONE
152 #define KPLATFORM_IOS 1
153 // iOS device
154 #elif TARGET_OS_MAC
155 // Other kinds of Mac OS
156 #else
157 #error "Unknown Apple platform"
158 #endif
159 #else
160 #error "Unknown platform!"
161 #endif
162 
163 #ifdef KEXPORT
164 // Exports
165 #ifdef _MSC_VER
166 #define KAPI __declspec(dllexport)
167 #else
168 #define KAPI __attribute__((visibility("default")))
169 #endif
170 #else
171 // Imports
172 #ifdef _MSC_VER
174 #define KAPI __declspec(dllimport)
175 #else
177 #define KAPI
178 #endif
179 #endif
180 
188 #define KCLAMP(value, min, max) ((value <= min) ? min : (value >= max) ? max \
189  : value)
190 
191 // Inlining
192 #if defined(__clang__) || defined(__gcc__)
194 #define KINLINE __attribute__((always_inline)) inline
195 
197 #define KNOINLINE __attribute__((noinline))
198 #elif defined(_MSC_VER)
199 
201 #define KINLINE __forceinline
202 
204 #define KNOINLINE __declspec(noinline)
205 #else
206 
208 #define KINLINE static inline
209 
211 #define KNOINLINE
212 #endif
213 
215 #define GIBIBYTES(amount) ((amount) * 1024ULL * 1024ULL * 1024ULL)
217 #define MEBIBYTES(amount) ((amount) * 1024ULL * 1024ULL)
219 #define KIBIBYTES(amount) ((amount) * 1024ULL)
220 
222 #define GIGABYTES(amount) ((amount) * 1000ULL * 1000ULL * 1000ULL)
224 #define MEGABYTES(amount) ((amount) * 1000ULL * 1000ULL)
226 #define KILOBYTES(amount) ((amount) * 1000ULL)
227 
228 KINLINE u64 get_aligned(u64 operand, u64 granularity) {
229  return ((operand + (granularity - 1)) & ~(granularity - 1));
230 }
231 
232 KINLINE range get_aligned_range(u64 offset, u64 size, u64 granularity) {
233  return (range){get_aligned(offset, granularity), get_aligned(size, granularity)};
234 }
235 
236 #define KMIN(x, y) (x < y ? x : y)
237 #define KMAX(x, y) (x > y ? x : y)
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
KINLINE range get_aligned_range(u64 offset, u64 size, u64 granularity)
Definition: defines.h:232
#define STATIC_ASSERT
Static assertion.
Definition: defines.h:75
signed char i8
Signed 8-bit integer.
Definition: defines.h:33
_Bool b8
8-bit boolean type
Definition: defines.h:58
float f32
32-bit floating point number
Definition: defines.h:47
struct range range
A range, typically of memory.
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:208
KINLINE u64 get_aligned(u64 operand, u64 granularity)
Definition: defines.h:228
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