// Copyright (c) Epic Games Tools // Licensed under the MIT license (https://opensource.org/license/mit/) #ifndef BASE_CORE_H #define BASE_CORE_H //////////////////////////////// //~ rjf: Foreign Includes #include #include #include #include //////////////////////////////// //~ rjf: Codebase Keywords #define internal static #define global static #define local_persist static //////////////////////////////// //~ rjf: Versions #define Version(major, minor, patch) (U64)((((U64)(major) & 0xffff) << 32) | ((((U64)(minor) & 0xffff) << 16)) | ((((U64)(patch) & 0xffff) << 0))) #define MajorFromVersion(version) (((version) & 0xffff00000000ull) >> 32) #define MinorFromVersion(version) (((version) & 0x0000ffff0000ull) >> 16) #define PatchFromVersion(version) (((version) & 0x00000000ffffull) >> 0) //////////////////////////////// //~ rjf: Units #define KB(n) (((U64)(n)) << 10) #define MB(n) (((U64)(n)) << 20) #define GB(n) (((U64)(n)) << 30) #define TB(n) (((U64)(n)) << 40) #define Thousand(n) ((n)*1000) #define Million(n) ((n)*1000000) #define Billion(n) ((n)*1000000000) //////////////////////////////// //~ rjf: Clamps, Mins, Maxes #define Min(A,B) (((A)<(B))?(A):(B)) #define Max(A,B) (((A)>(B))?(A):(B)) #define ClampTop(A,X) Min(A,X) #define ClampBot(X,B) Max(X,B) #define Clamp(A,X,B) (((X)<(A))?(A):((X)>(B))?(B):(X)) //////////////////////////////// //~ rjf: Member Offsets #define Member(T,m) (((T*)0)->m) #define OffsetOf(T,m) IntFromPtr(&Member(T,m)) #define MemberFromOffset(T,ptr,off) (T)((((U8 *)ptr)+(off))) #define CastFromMember(T,m,ptr) (T*)(((U8*)ptr) - OffsetOf(T,m)) //////////////////////////////// //~ rjf: Memory Operation Macros #define MemoryCopy(dst, src, size) memmove((dst), (src), (size)) #define MemorySet(dst, byte, size) memset((dst), (byte), (size)) #define MemoryCompare(a, b, size) memcmp((a), (b), (size)) #define MemoryStrlen(ptr) strlen(ptr) #define MemoryCopyStruct(d,s) MemoryCopy((d),(s),sizeof(*(d))) #define MemoryCopyArray(d,s) MemoryCopy((d),(s),sizeof(d)) #define MemoryCopyTyped(d,s,c) MemoryCopy((d),(s),sizeof(*(d))*(c)) #define MemoryCopyStr8(dst, s) MemoryCopy(dst, (s).str, (s).size) #define MemoryZero(s,z) memset((s),0,(z)) #define MemoryZeroStruct(s) MemoryZero((s),sizeof(*(s))) #define MemoryZeroArray(a) MemoryZero((a),sizeof(a)) #define MemoryZeroTyped(m,c) MemoryZero((m),sizeof(*(m))*(c)) #define MemoryMatch(a,b,z) (MemoryCompare((a),(b),(z)) == 0) #define MemoryMatchStruct(a,b) MemoryMatch((a),(b),sizeof(*(a))) #define MemoryMatchArray(a,b) MemoryMatch((a),(b),sizeof(a)) #define MemoryIsZeroStruct(ptr) memory_is_zero((ptr), sizeof(*(ptr))) #define MemoryRead(T,p,e) ( ((p)+sizeof(T)<=(e))?(*(T*)(p)):(0) ) #define MemoryConsume(T,p,e) ( ((p)+sizeof(T)<=(e))?((p)+=sizeof(T),*(T*)((p)-sizeof(T))):((p)=(e),0) ) //////////////////////////////// //~ rjf: Asserts #define Trap() __builtin_trap() #define AssertAlways(x) do{if(!(x)) {Trap();}}while(0) #if BUILD_DEBUG # define Assert(x) AssertAlways(x) #else # define Assert(x) (void)(x) #endif #define InvalidPath Assert(!"Invalid Path!") #define NotImplemented Assert(!"Not Implemented!") #define NoOp ((void)0) #define StaticAssert(C, ID) global U8 Glue(ID, __LINE__)[(C)?1:-1] //////////////////////////////// //~ rjf: Misc. Helper Macros #define Stringify_(S) #S #define Stringify(S) Stringify_(S) #define Glue_(A,B) A##B #define Glue(A,B) Glue_(A,B) #define ArrayCount(a) (sizeof(a) / sizeof((a)[0])) #define CeilIntegerDiv(a,b) (((a) + (b) - 1)/(b)) #define Swap(T,a,b) do{T t__ = a; a = b; b = t__;}while(0) #define Compose64Bit(a,b) ((((U64)a) << 32) | ((U64)b)) #define Compose32Bit(a,b) ((((U32)a) << 16) | ((U32)b)) #define AlignPow2(x,b) (((x) + (b) - 1)&(~((b) - 1))) #define AlignDownPow2(x,b) ((x)&(~((b) - 1))) #define AlignPadPow2(x,b) ((0-(x)) & ((b) - 1)) #define IsPow2(x) ((x)!=0 && ((x)&((x)-1))==0) #define IsPow2OrZero(x) ((((x) - 1)&(x)) == 0) #define ExtractBit(word, idx) (((word) >> (idx)) & 1) #define Extract8(word, pos) (((word) >> ((pos)*8)) & max_U8) #define Extract16(word, pos) (((word) >> ((pos)*16)) & max_U16) #define Extract32(word, pos) (((word) >> ((pos)*32)) & max_U32) #if LANG_CPP # define zero_struct {} #else # define zero_struct {0} #endif //////////////////////////////// //~ rjf: Base Types typedef unsigned char U8; typedef unsigned short U16; typedef unsigned int U32; typedef unsigned long long U64; typedef signed char S8; typedef short S16; typedef int S32; typedef long long S64; typedef S8 B8; typedef S16 B16; typedef S32 B32; typedef S64 B64; typedef float F32; typedef double F64; typedef void VoidProc(void); typedef union U128 U128; union U128 { U8 u8[16]; U16 u16[8]; U32 u32[4]; U64 u64[2]; }; typedef union U256 U256; union U256 { U8 u8[32]; U16 u16[16]; U32 u32[8]; U64 u64[4]; U128 u128[2]; }; typedef union U512 U512; union U512 { U8 u8[64]; U16 u16[32]; U32 u32[16]; U64 u64[8]; U128 u128[4]; U256 u256[2]; }; //////////////////////////////// //~ rjf: Basic Type Structures typedef struct U16Array U16Array; struct U16Array { U64 count; U16 *v; }; typedef struct U32Array U32Array; struct U32Array { U64 count; U32 *v; }; typedef struct U64Array U64Array; struct U64Array { U64 count; U64 *v; }; typedef struct U128Array U128Array; struct U128Array { U64 count; U128 *v; }; //////////////////////////////// //~ rjf: Basic Constants global U32 sign32 = 0x80000000; global U32 exponent32 = 0x7F800000; global U32 mantissa32 = 0x007FFFFF; global F32 big_golden32 = 1.61803398875f; global F32 small_golden32 = 0.61803398875f; global F32 pi32 = 3.1415926535897f; global F64 machine_epsilon64 = 4.94065645841247e-324; global U64 max_U64 = 0xffffffffffffffffull; global U32 max_U32 = 0xffffffff; global U16 max_U16 = 0xffff; global U8 max_U8 = 0xff; global S64 max_S64 = (S64)0x7fffffffffffffffll; global S32 max_S32 = (S32)0x7fffffff; global S16 max_S16 = (S16)0x7fff; global S8 max_S8 = (S8)0x7f; global S64 min_S64 = (S64)0x8000000000000000ll; global S32 min_S32 = (S32)0x80000000; global S16 min_S16 = (S16)0x8000; global S8 min_S8 = (S8)0x80; global const U32 bitmask1 = 0x00000001; global const U32 bitmask2 = 0x00000003; global const U32 bitmask3 = 0x00000007; global const U32 bitmask4 = 0x0000000f; global const U32 bitmask5 = 0x0000001f; global const U32 bitmask6 = 0x0000003f; global const U32 bitmask7 = 0x0000007f; global const U32 bitmask8 = 0x000000ff; global const U32 bitmask9 = 0x000001ff; global const U32 bitmask10 = 0x000003ff; global const U32 bitmask11 = 0x000007ff; global const U32 bitmask12 = 0x00000fff; global const U32 bitmask13 = 0x00001fff; global const U32 bitmask14 = 0x00003fff; global const U32 bitmask15 = 0x00007fff; global const U32 bitmask16 = 0x0000ffff; global const U32 bitmask17 = 0x0001ffff; global const U32 bitmask18 = 0x0003ffff; global const U32 bitmask19 = 0x0007ffff; global const U32 bitmask20 = 0x000fffff; global const U32 bitmask21 = 0x001fffff; global const U32 bitmask22 = 0x003fffff; global const U32 bitmask23 = 0x007fffff; global const U32 bitmask24 = 0x00ffffff; global const U32 bitmask25 = 0x01ffffff; global const U32 bitmask26 = 0x03ffffff; global const U32 bitmask27 = 0x07ffffff; global const U32 bitmask28 = 0x0fffffff; global const U32 bitmask29 = 0x1fffffff; global const U32 bitmask30 = 0x3fffffff; global const U32 bitmask31 = 0x7fffffff; global const U32 bitmask32 = 0xffffffff; global const U64 bitmask33 = 0x00000001ffffffffull; global const U64 bitmask34 = 0x00000003ffffffffull; global const U64 bitmask35 = 0x00000007ffffffffull; global const U64 bitmask36 = 0x0000000fffffffffull; global const U64 bitmask37 = 0x0000001fffffffffull; global const U64 bitmask38 = 0x0000003fffffffffull; global const U64 bitmask39 = 0x0000007fffffffffull; global const U64 bitmask40 = 0x000000ffffffffffull; global const U64 bitmask41 = 0x000001ffffffffffull; global const U64 bitmask42 = 0x000003ffffffffffull; global const U64 bitmask43 = 0x000007ffffffffffull; global const U64 bitmask44 = 0x00000fffffffffffull; global const U64 bitmask45 = 0x00001fffffffffffull; global const U64 bitmask46 = 0x00003fffffffffffull; global const U64 bitmask47 = 0x00007fffffffffffull; global const U64 bitmask48 = 0x0000ffffffffffffull; global const U64 bitmask49 = 0x0001ffffffffffffull; global const U64 bitmask50 = 0x0003ffffffffffffull; global const U64 bitmask51 = 0x0007ffffffffffffull; global const U64 bitmask52 = 0x000fffffffffffffull; global const U64 bitmask53 = 0x001fffffffffffffull; global const U64 bitmask54 = 0x003fffffffffffffull; global const U64 bitmask55 = 0x007fffffffffffffull; global const U64 bitmask56 = 0x00ffffffffffffffull; global const U64 bitmask57 = 0x01ffffffffffffffull; global const U64 bitmask58 = 0x03ffffffffffffffull; global const U64 bitmask59 = 0x07ffffffffffffffull; global const U64 bitmask60 = 0x0fffffffffffffffull; global const U64 bitmask61 = 0x1fffffffffffffffull; global const U64 bitmask62 = 0x3fffffffffffffffull; global const U64 bitmask63 = 0x7fffffffffffffffull; global const U64 bitmask64 = 0xffffffffffffffffull; global const U32 bit1 = (1<<0); global const U32 bit2 = (1<<1); global const U32 bit3 = (1<<2); global const U32 bit4 = (1<<3); global const U32 bit5 = (1<<4); global const U32 bit6 = (1<<5); global const U32 bit7 = (1<<6); global const U32 bit8 = (1<<7); global const U32 bit9 = (1<<8); global const U32 bit10 = (1<<9); global const U32 bit11 = (1<<10); global const U32 bit12 = (1<<11); global const U32 bit13 = (1<<12); global const U32 bit14 = (1<<13); global const U32 bit15 = (1<<14); global const U32 bit16 = (1<<15); global const U32 bit17 = (1<<16); global const U32 bit18 = (1<<17); global const U32 bit19 = (1<<18); global const U32 bit20 = (1<<19); global const U32 bit21 = (1<<20); global const U32 bit22 = (1<<21); global const U32 bit23 = (1<<22); global const U32 bit24 = (1<<23); global const U32 bit25 = (1<<24); global const U32 bit26 = (1<<25); global const U32 bit27 = (1<<26); global const U32 bit28 = (1<<27); global const U32 bit29 = (1<<28); global const U32 bit30 = (1<<29); global const U32 bit31 = (1<<30); global const U32 bit32 = (1<<31); global const U64 bit33 = (1ull<<32); global const U64 bit34 = (1ull<<33); global const U64 bit35 = (1ull<<34); global const U64 bit36 = (1ull<<35); global const U64 bit37 = (1ull<<36); global const U64 bit38 = (1ull<<37); global const U64 bit39 = (1ull<<38); global const U64 bit40 = (1ull<<39); global const U64 bit41 = (1ull<<40); global const U64 bit42 = (1ull<<41); global const U64 bit43 = (1ull<<42); global const U64 bit44 = (1ull<<43); global const U64 bit45 = (1ull<<44); global const U64 bit46 = (1ull<<45); global const U64 bit47 = (1ull<<46); global const U64 bit48 = (1ull<<47); global const U64 bit49 = (1ull<<48); global const U64 bit50 = (1ull<<49); global const U64 bit51 = (1ull<<50); global const U64 bit52 = (1ull<<51); global const U64 bit53 = (1ull<<52); global const U64 bit54 = (1ull<<53); global const U64 bit55 = (1ull<<54); global const U64 bit56 = (1ull<<55); global const U64 bit57 = (1ull<<56); global const U64 bit58 = (1ull<<57); global const U64 bit59 = (1ull<<58); global const U64 bit60 = (1ull<<59); global const U64 bit61 = (1ull<<60); global const U64 bit62 = (1ull<<61); global const U64 bit63 = (1ull<<62); global const U64 bit64 = (1ull<<63); //////////////////////////////// //~ rjf: Sorts #define quick_sort(ptr, count, element_size, cmp_function) qsort((ptr), (count), (element_size), (int (*)(const void *, const void *))(cmp_function)) #endif // BASE_CORE_H