diff --git a/gb.h b/gb.h index 3b76232..143c895 100644 --- a/gb.h +++ b/gb.h @@ -15,12 +15,8 @@ /* Version History: -<<<<<<< HEAD - + 0.05 - Fix Macros 0.04a - Change conventions to be in keeping with `gb.hpp` -======= - 0.04a - Remove C++ specific macros ->>>>>>> origin/master 0.04 - Allow for no 0.03 - Allocators can be passed to gb_alloc/free/etc. without cast using `typedef void* gb_Allocator_Ptr` 0.02 - Implement all functions (from gb.hpp) @@ -151,7 +147,9 @@ extern "C" { #define GB_IS_LITTLE_EDIAN (!GB_IS_BIG_EDIAN) #endif +#ifndef GB_IS_POWER_OF_TWO #define GB_IS_POWER_OF_TWO(x) ((x) != 0) && !((x) & ((x) - 1)) +#endif #ifndef GB_FORCE_INLINE #if defined(_MSC_VER) @@ -211,7 +209,7 @@ extern "C" { #if !defined(GB_NO_STDIO) && defined(_MSC_VER) /* snprintf_msvc */ - int + GB_FORCE_INLINE int gb__vsnprintf_compatible(char* buffer, size_t size, char const* format, va_list args) { int result = -1; @@ -220,7 +218,7 @@ extern "C" { return result; } - int + GB_FORCE_INLINE int gb__snprintf_compatible(char* buffer, size_t size, char const* format, ...) { va_list args; @@ -326,16 +324,32 @@ typedef ptrdiff_t ptrdiff; /**********************************/ /* NOTE(bill): Easier to grep/find for casts */ -/* Still not as type safe as C++ static_cast, reinterpret_cast, const_cast */ +/* NOTE(bill): Still not as type safe as C++ static_cast, reinterpret_cast, const_cast */ +#ifndef cast #define cast(Type, src) ((Type)(src)) - -#if defined(GB_COMPILER_GNU_GCC) - #define bit_cast(Type, src) ({ GB_ASSERT(sizeof(Type) <= sizeof(src)); Type dst; memcpy(&dst, &(src), sizeof(Type)); dst; }) #endif -#define pseudo_cast(Type, src) (*cast(Type*, &(src))) +#if defined(GB_COMPILER_GNU_GCC) + #ifndef bit_cast + #define bit_cast(Type, src) ({ GB_ASSERT(sizeof(Type) <= sizeof(src)); Type dst; memcpy(&dst, &(src), sizeof(Type)); dst; }) + #endif +#endif +#ifndef pseudo_cast +#define pseudo_cast(Type, src) (*cast(Type*, &(src))) +#endif + +#ifndef GB_UNUSED #define GB_UNUSED(x) cast(void, sizeof(x)) +#endif + + + + + + + + /**********************************/ @@ -345,13 +359,16 @@ typedef ptrdiff_t ptrdiff; /**********************************/ /* NOTE(bill): 0[x] is used to prevent C++ style arrays with operator overloading */ +#ifndef GB_ARRAY_COUNT #define GB_ARRAY_COUNT(x) ((sizeof(x)/sizeof(0[x])) / (cast(size_t, !(sizeof(x) % sizeof(0[x]))))) +#endif -#define GB_KILOBYTES(x) ( (x) * 1024ll) +#ifndef GB_KILOBYTES +#define GB_KILOBYTES(x) ( (x) * 1024ll) #define GB_MEGABYTES(x) (GB_KILOBYTES(x) * 1024ll) #define GB_GIGABYTES(x) (GB_MEGABYTES(x) * 1024ll) #define GB_TERABYTES(x) (GB_GIGABYTES(x) * 1024ll) - +#endif typedef struct gb_Mutex { @@ -416,7 +433,7 @@ void gb_semaphore_wait(gb_Semaphore* s); -typedef void(gb_Thread_Procedure)(void*); +typedef void(gb_Thread_Procedure)(void* data); typedef struct gb_Thread { @@ -487,7 +504,7 @@ void* gb_alloc_align(gb_Allocator_Ptr allocator, usize size, usize align) { GB_ASSERT(allocator != NULL); - gb_Allocator* a = allocator; + gb_Allocator* a = cast(gb_Allocator*, allocator); return a->alloc(a, size, align); } void* @@ -497,14 +514,16 @@ gb_alloc(gb_Allocator_Ptr allocator, usize size) return gb_alloc_align(allocator, size, GB_DEFAULT_ALIGNMENT); } -#define gb_alloc_struct(allocator, Type) cast((Type)*, gb_alloc_align(allocator, sizeof(Type), alignof(Type))) -#define gb_alloc_array(allocator, Type, count) cast((Type)*, gb_alloc_align(allocator, sizeof(Type)*(count), alignof(Type))) +#ifndef gb_alloc_struct +#define gb_alloc_struct(allocator, Type) cast(Type*, gb_alloc_align(allocator, sizeof(Type), alignof(Type))) +#define gb_alloc_array(allocator, Type, count) cast(Type*, gb_alloc_align(allocator, sizeof(Type)*(count), alignof(Type))) +#endif void gb_free(gb_Allocator_Ptr allocator, void* ptr) { GB_ASSERT(allocator != NULL); - gb_Allocator* a = allocator; + gb_Allocator* a = cast(gb_Allocator*, allocator); if (ptr) a->free(a, ptr); } @@ -512,7 +531,7 @@ s64 gb_allocated_size(gb_Allocator_Ptr allocator, void const* ptr) { GB_ASSERT(allocator != NULL); - gb_Allocator* a = allocator; + gb_Allocator* a = cast(gb_Allocator*, allocator); return a->allocated_size(a, ptr); } @@ -520,7 +539,7 @@ s64 gb_total_allocated(gb_Allocator_Ptr allocator) { GB_ASSERT(allocator != NULL); - gb_Allocator* a = allocator; + gb_Allocator* a = cast(gb_Allocator*, allocator); return a->total_allocated(a); } @@ -529,7 +548,7 @@ gb_total_allocated(gb_Allocator_Ptr allocator) typedef struct gb_Heap { - gb_Allocator base; /* NOTE(bill): Must be first into order to allow for polymorphism */ + gb_Allocator base; /* NOTE(bill): Must be first into order to act as a vtable */ gb_Mutex mutex; bool32 use_mutex; @@ -547,7 +566,7 @@ void gb_heap_destroy(gb_Heap* heap); typedef struct gb_Arena { - gb_Allocator base; /* NOTE(bill): Must be first into order to allow for polymorphism */ + gb_Allocator base; /* NOTE(bill): Must be first into order to act as a vtable */ gb_Allocator* backing; void* physical_start; @@ -575,7 +594,7 @@ void gb_temporary_arena_memory_free(gb_Temporary_Arena_Memory t); typedef struct gb_Pool { - gb_Allocator base; /* NOTE(bill): Must be first into order to allow for polymorphism */ + gb_Allocator base; /* NOTE(bill): Must be first into order to act as a vtable */ gb_Allocator* backing; @@ -602,10 +621,11 @@ void gb_pool_destroy(gb_Pool* pool); void* gb_align_forward(void* ptr, usize align); -void *gb_zero_size(void* ptr, usize bytes); +void* gb_zero_size(void* ptr, usize bytes); +#ifndef gb_zero_struct #define gb_zero_struct(element) (cast(void, gb_zero_size(&(element), sizeof(element)))) #define gb_zero_array(ptr, Type, count) cast(Type, gb_zero_size((ptr), sizeof(Type)*(count))) - +#endif @@ -744,9 +764,14 @@ u64 gb_hash_murmur64(void const* key, usize num_bytes, u64 seed); /**********************************/ /* TODO(bill): How should I this */ -typedef struct gb_Time { s64 microseconds; } gb_Time; +typedef struct gb_Time +{ + s64 microseconds; +} gb_Time; +#ifndef GB_TIME_ZERO #define GB_TIME_ZERO (cast(gb_Time, {0})) +#endif gb_Time gb_time_now(void); void gb_time_sleep(gb_Time time); diff --git a/gb.hpp b/gb.hpp index a6dca76..0a1054e 100644 --- a/gb.hpp +++ b/gb.hpp @@ -215,8 +215,9 @@ Version History: #define GB_IS_LITTLE_EDIAN (!GB_IS_BIG_EDIAN) #endif +#ifndef GB_IS_POWER_OF_TWO #define GB_IS_POWER_OF_TWO(x) ((x) != 0) && !((x) & ((x) - 1)) - +#endif #if !defined(GB_HAS_NO_CONSTEXPR) #if defined(_GNUC_VER) && _GNUC_VER < 406 // Less than gcc 4.06 diff --git a/gb_math.hpp b/gb_math.hpp index e67a003..b81bde9 100644 --- a/gb_math.hpp +++ b/gb_math.hpp @@ -144,38 +144,17 @@ CONTENTS: #endif -// TODO(bill): Get this to work -// #if !defined(GB_LITTLE_EDIAN) && !defined(GB_BIG_EDIAN) - -// // Source: http://sourceforge.net/p/predef/wiki/Endianness/ -// #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \ -// defined(__BIG_ENDIAN__) || \ -// defined(__ARMEB__) || \ -// defined(__THUMBEB__) || \ -// defined(__AARCH64EB__) || \ -// defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__) -// // It's a big-endian target architecture -// #define GB_BIG_EDIAN 1 - -// #elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \ -// defined(__LITTLE_ENDIAN__) || \ -// defined(__ARMEL__) || \ -// defined(__THUMBEL__) || \ -// defined(__AARCH64EL__) || \ -// defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) -// // It's a little-endian target architecture -// #define GB_LITTLE_EDIAN 1 - -// #else -// #error I don't know what architecture this is! -// #endif -// #endif +#ifndef GB_EDIAN_ORDER +#define GB_EDIAN_ORDER + #define GB_IS_BIG_EDIAN (!*(unsigned char*)&(unsigned short){1}) + #define GB_IS_LITTLE_EDIAN (!GB_IS_BIG_EDIAN) +#endif +#ifndef GB_IS_POWER_OF_TWO #define GB_IS_POWER_OF_TWO(x) ((x) != 0) && !((x) & ((x) - 1)) +#endif -#include -#include #if !defined(GB_HAS_NO_CONSTEXPR) #if defined(_GNUC_VER) && _GNUC_VER < 406 // Less than gcc 4.06 @@ -208,18 +187,14 @@ CONTENTS: #define WIN32_LEAN_AND_MEAN 1 #include - #include // Time functions #include #undef NOMINMAX #undef VC_EXTRALEAN #undef WIN32_EXTRA_LEAN #undef WIN32_LEAN_AND_MEAN - - #include #else - #include - #include + // #endif @@ -1099,6 +1074,7 @@ bool intersection3(Plane p1, Plane p2, Plane p3, Vector3* ip); } // namespace plane +#if !defined(GB_MATH_NO_RANDOM) namespace random { @@ -1142,6 +1118,7 @@ f32 perlin_3d(f32 x, f32 y, f32 z, s32 x_wrap = 0, s32 y_wrap = 0, s32 z_wrap = } // namespace random +#endif namespace math { @@ -1220,6 +1197,9 @@ __GB_NAMESPACE_END /// /// //////////////////////////////// #if defined(GB_MATH_IMPLEMENTATION) + +#include + __GB_NAMESPACE_START //////////////////////////////// @@ -3457,9 +3437,11 @@ namespace sphere Sphere calculate_min_bounding(void const* vertices, usize num_vertices, usize stride, usize offset, f32 step) { +#if !defined(GB_MATH_NO_RANDOM) auto gen = random::make(0); +#endif - const u8* vertex = reinterpret_cast(vertices); + u8 const* vertex = reinterpret_cast(vertices); vertex += offset; Vector3 position = pseudo_cast(vertex[0]); @@ -3475,9 +3457,15 @@ calculate_min_bounding(void const* vertices, usize num_vertices, usize stride, u do { done = true; +#if !defined(GB_MATH_NO_RANDOM) for (u32 i = 0, index = random::uniform_u32(&gen, 0, num_vertices-1); i < num_vertices; i++, index = (index + 1)%num_vertices) +#else + for (u32 i = 0, index = num_vertices/2; + i < num_vertices; + i++, index = (index + 1)%num_vertices) +#endif { Vector3 position = pseudo_cast(vertex[index * stride]); @@ -3609,6 +3597,7 @@ intersection3(Plane p1, Plane p2, Plane p3, Vector3* ip) } } // namespace plane +#if !defined(GB_MATH_NO_RANDOM) namespace random { inline Random @@ -3886,6 +3875,8 @@ perlin_3d(f32 x, f32 y, f32 z, s32 x_wrap, s32 y_wrap, s32 z_wrap) } } // namespace random +#endif + __GB_NAMESPACE_END #endif // GB_MATH_IMPLEMENTATION