From d130420db7f68d574704a88e69ab0ccd35f3554e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 17 Dec 2015 12:26:24 +0000 Subject: [PATCH] Change conventions slightly --- README.md | 6 +- gb.h | 576 ++++++++++++++++---------------- gb.hpp | 599 +++++++++++++++++---------------- gb_math.hpp | 927 ++++++++++++++++++++++++++-------------------------- 4 files changed, 1068 insertions(+), 1040 deletions(-) diff --git a/README.md b/README.md index 2381f68..d5c6eae 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,9 @@ gb single-file public domain libraries for C & C++ library | latest version | category | languages | description ----------------|----------------|----------|-----------|------------- **gb_string.h** | 0.93 | strings | C, C++ | A better string library for C & C++ -**gb.hpp** | 0.31a | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development -**gb_math.hpp** | 0.03a | math | C++11 | A C++11 math library geared towards game development -**gb.h** | 0.04 | misc | C | (Experimental) A C helpher library geared towards game development (port of gb.hpp) +**gb.hpp** | 0.32 | misc | C++11 | (Experimental) A C++11 helper library without STL geared towards game development +**gb_math.hpp** | 0.04 | math | C++11 | A C++11 math library geared towards game development +**gb.h** | 0.04a | misc | C | (Experimental) A C helpher library geared towards game development (port of gb.hpp) **gb_ini.h** | 0.91a | misc | C, C++ | A simple ini file loader library for C & C++ ## FAQ diff --git a/gb.h b/gb.h index fad1ee1..fbfa4a9 100644 --- a/gb.h +++ b/gb.h @@ -16,8 +16,9 @@ /* Version History: + 0.04a - Change conventions to be in keeping with `gb.hpp` 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.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) 0.01 - Initial Version (just prototypes) */ @@ -41,7 +42,7 @@ extern "C" { /* Example for static defines - global_variable const f32 TAU = 6.283185f; + global_variable f32 const TAU = 6.283185f; global_variable void* g_memory; internal_linkage void @@ -142,7 +143,7 @@ extern "C" { #ifndef GB_EDIAN_ORDER #define GB_EDIAN_ORDER - #define GB_IS_BIG_EDIAN (!*(unsigned char *)&(unsigned short){1}) + #define GB_IS_BIG_EDIAN (!*(unsigned char*)&(unsigned short){1}) #define GB_IS_LITTLE_EDIAN (!GB_IS_BIG_EDIAN) #endif @@ -203,7 +204,7 @@ extern "C" { #endif #ifndef NULL -#define NULL ((void *)0) +#define NULL ((void*)0) #endif #if defined(NDEBUG) @@ -223,7 +224,8 @@ extern "C" { #if !defined(GB_NO_STDIO) && defined(_MSC_VER) /* snprintf_msvc */ - int gb__vsnprintf_compatible(char *buffer, size_t size, const char *format, va_list args) + int + gb__vsnprintf_compatible(char* buffer, size_t size, char const* format, va_list args) { int result = -1; if (size > 0) result = _vsnprintf_s(buffer, size, _TRUNCATE, format, args); @@ -231,7 +233,8 @@ extern "C" { return result; } - int gb__snprintf_compatible(char *buffer, size_t size, const char *format, ...) + int + gb__snprintf_compatible(char* buffer, size_t size, char const* format, ...) { va_list args; va_start(args, format); @@ -343,7 +346,7 @@ typedef ptrdiff_t ptrdiff; #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))) +#define pseudo_cast(Type, src) (*cast(Type*, &(src))) #define GB_UNUSED(x) cast(void, sizeof(x)) @@ -363,7 +366,8 @@ typedef ptrdiff_t ptrdiff; #define GB_TERABYTES(x) (GB_GIGABYTES(x) * 1024ll) -typedef struct gb_Mutex { +typedef struct gb_Mutex +{ #if defined(GB_SYSTEM_WINDOWS) HANDLE win32_mutex; #else @@ -372,10 +376,10 @@ typedef struct gb_Mutex { } gb_Mutex; gb_Mutex gb_mutex_make(void); -void gb_mutex_destroy(gb_Mutex *mutex); -void gb_mutex_lock(gb_Mutex *mutex); -bool32 gb_mutex_try_lock(gb_Mutex *mutex); -void gb_mutex_unlock(gb_Mutex *mutex); +void gb_mutex_destroy(gb_Mutex* mutex); +void gb_mutex_lock(gb_Mutex* mutex); +bool32 gb_mutex_try_lock(gb_Mutex* mutex); +void gb_mutex_unlock(gb_Mutex* mutex); @@ -384,27 +388,28 @@ void gb_mutex_unlock(gb_Mutex *mutex); typedef struct gb_Atomic32 { u32 nonatomic; } gb_Atomic32; typedef struct gb_Atomic64 { u64 nonatomic; } gb_Atomic64; -u32 gb_atomic32_load(const volatile gb_Atomic32 *a); -void gb_atomic32_store(volatile gb_Atomic32 *a, u32 value); -u32 gb_atomic32_compare_exchange_strong(volatile gb_Atomic32 *a, u32 expected, u32 desired); -u32 gb_atomic32_exchanged(volatile gb_Atomic32 *a, u32 desired); -u32 gb_atomic32_fetch_add(volatile gb_Atomic32 *a, s32 operand); -u32 gb_atomic32_fetch_and(volatile gb_Atomic32 *a, u32 operand); -u32 gb_atomic32_fetch_or(volatile gb_Atomic32 *a, u32 operand); +u32 gb_atomic32_load(gb_Atomic32 const volatile* a); +void gb_atomic32_store(gb_Atomic32 volatile* a, u32 value); +u32 gb_atomic32_compare_exchange_strong(gb_Atomic32 volatile* a, u32 expected, u32 desired); +u32 gb_atomic32_exchanged(gb_Atomic32 volatile* a, u32 desired); +u32 gb_atomic32_fetch_add(gb_Atomic32 volatile* a, s32 operand); +u32 gb_atomic32_fetch_and(gb_Atomic32 volatile* a, u32 operand); +u32 gb_atomic32_fetch_or(gb_Atomic32 volatile* a, u32 operand); -u64 gb_atomic64_load(const volatile gb_Atomic64 *a); -void gb_atomic64_store(volatile gb_Atomic64 *a, u64 value); -u64 gb_atomic64_compare_exchange_strong(volatile gb_Atomic64 *a, u64 expected, u64 desired); -u64 gb_atomic64_exchanged(volatile gb_Atomic64 *a, u64 desired); -u64 gb_atomic64_fetch_add(volatile gb_Atomic64 *a, s64 operand); -u64 gb_atomic64_fetch_and(volatile gb_Atomic64 *a, u64 operand); -u64 gb_atomic64_fetch_or(volatile gb_Atomic64 *a, u64 operand); +u64 gb_atomic64_load(gb_Atomic64 const volatile* a); +void gb_atomic64_store(gb_Atomic64 volatile* a, u64 value); +u64 gb_atomic64_compare_exchange_strong(gb_Atomic64 volatile* a, u64 expected, u64 desired); +u64 gb_atomic64_exchanged(gb_Atomic64 volatile* a, u64 desired); +u64 gb_atomic64_fetch_add(gb_Atomic64 volatile* a, s64 operand); +u64 gb_atomic64_fetch_and(gb_Atomic64 volatile* a, u64 operand); +u64 gb_atomic64_fetch_or(gb_Atomic64 volatile* a, u64 operand); -typedef struct gb_Semaphore { +typedef struct gb_Semaphore +{ #if defined(GB_SYSTEM_WINDOWS) HANDLE win32_handle; #else @@ -415,10 +420,10 @@ typedef struct gb_Semaphore { } gb_Semaphore; gb_Semaphore gb_semaphore_make(void); -void gb_semaphore_destroy(gb_Semaphore *s); -void gb_semaphore_post(gb_Semaphore *s); -void gb_semaphore_post_count(gb_Semaphore *s, u32 count); -void gb_semaphore_wait(gb_Semaphore *s); +void gb_semaphore_destroy(gb_Semaphore* s); +void gb_semaphore_post(gb_Semaphore* s); +void gb_semaphore_post_count(gb_Semaphore* s, u32 count); +void gb_semaphore_wait(gb_Semaphore* s); @@ -426,15 +431,16 @@ void gb_semaphore_wait(gb_Semaphore *s); typedef void(gb_Thread_Procedure)(void*); -typedef struct gb_Thread { +typedef struct gb_Thread +{ #if defined(GB_SYSTEM_WINDOWS) HANDLE win32_handle; #else pthread_t posix_handle; #endif - gb_Thread_Procedure *proc; - void *data; + gb_Thread_Procedure* proc; + void* data; gb_Semaphore semaphore; usize stack_size; @@ -442,11 +448,11 @@ typedef struct gb_Thread { } gb_Thread; gb_Thread gb_thread_make(void); -void gb_thread_destroy(gb_Thread *t); -void gb_thread_start(gb_Thread *t, gb_Thread_Procedure *proc, void *data); -void gb_thread_start_with_stack(gb_Thread *t, gb_Thread_Procedure *proc, void *data, usize stack_size); -void gb_thread_join(gb_Thread *t); -bool32 gb_thread_is_running(const gb_Thread *t); /* NOTE(bill): Can this be just pass by value? */ +void gb_thread_destroy(gb_Thread* t); +void gb_thread_start(gb_Thread* t, gb_Thread_Procedure* proc, void* data); +void gb_thread_start_with_stack(gb_Thread* t, gb_Thread_Procedure* proc, void* data, usize stack_size); +void gb_thread_join(gb_Thread* t); +bool32 gb_thread_is_running(gb_Thread t); u32 gb_thread_current_id(void); @@ -468,57 +474,58 @@ u32 gb_thread_current_id(void); /* * NOTE(bill): The cost of the function pointer lookup is minor compared to the actually allocation in most cases */ -typedef struct gb_Allocator { +typedef struct gb_Allocator +{ /* Allocates the specified amount of memory aligned to the specified alignment */ - void *(*alloc)(struct gb_Allocator *a, usize size, usize align); + void* (*alloc)(struct gb_Allocator* a, usize size, usize align); /* Frees an allocation made with alloc() */ - void (*free)(struct gb_Allocator *a, void *ptr); + void (*free)(struct gb_Allocator* a, void* ptr); /* Returns the amount of usuable memory allocated at `ptr`. * If the allocator does not support tracking of the allocation size, * the function will return -1 */ - s64 (*allocated_size)(struct gb_Allocator *a, const void *ptr); + s64 (*allocated_size)(struct gb_Allocator* a, void const* ptr); /* Returns the total amount of memory allocated by this allocator * If the allocator does not track memory, the function will return -1 */ - s64 (*total_allocated)(struct gb_Allocator *a); + s64 (*total_allocated)(struct gb_Allocator* a); } gb_Allocator; -typedef void *gb_Allocator_Ptr; +typedef void* gb_Allocator_Ptr; -void * +void* gb_alloc_align(gb_Allocator_Ptr allocator, usize size, usize align) { GB_ASSERT(allocator != NULL); - gb_Allocator *a = allocator; + gb_Allocator* a = allocator; return a->alloc(a, size, align); } -void * +void* gb_alloc(gb_Allocator_Ptr allocator, usize size) { GB_ASSERT(allocator != NULL); 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))) +#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))) void -gb_free(gb_Allocator_Ptr allocator, void *ptr) +gb_free(gb_Allocator_Ptr allocator, void* ptr) { GB_ASSERT(allocator != NULL); - gb_Allocator *a = allocator; + gb_Allocator* a = allocator; if (ptr) a->free(a, ptr); } s64 -gb_allocated_size(gb_Allocator_Ptr allocator, const void *ptr) +gb_allocated_size(gb_Allocator_Ptr allocator, void const* ptr) { GB_ASSERT(allocator != NULL); - gb_Allocator *a = allocator; + gb_Allocator* a = allocator; return a->allocated_size(a, ptr); } @@ -526,14 +533,15 @@ s64 gb_total_allocated(gb_Allocator_Ptr allocator) { GB_ASSERT(allocator != NULL); - gb_Allocator *a = allocator; + gb_Allocator* a = allocator; return a->total_allocated(a); } -typedef struct gb_Heap { +typedef struct gb_Heap +{ gb_Allocator base; /* NOTE(bill): Must be first into order to allow for polymorphism */ gb_Mutex mutex; @@ -547,51 +555,54 @@ typedef struct gb_Heap { } gb_Heap; gb_Heap gb_heap_make(bool32 use_mutex); -void gb_heap_destroy(gb_Heap *heap); +void gb_heap_destroy(gb_Heap* heap); -typedef struct gb_Arena { +typedef struct gb_Arena +{ gb_Allocator base; /* NOTE(bill): Must be first into order to allow for polymorphism */ - gb_Allocator *backing; - void *physical_start; + gb_Allocator* backing; + void* physical_start; s64 total_size; s64 total_allocated_count; s64 temp_count; } gb_Arena; -gb_Arena gb_arena_make_from_backing(gb_Allocator *backing, usize size); -gb_Arena gb_arena_make_from_pointer(void *start, usize size); -void gb_arena_destroy(gb_Arena *arena); -void gb_arena_clear(gb_Arena *arena); +gb_Arena gb_arena_make_from_backing(gb_Allocator* backing, usize size); +gb_Arena gb_arena_make_from_pointer(void* start, usize size); +void gb_arena_destroy(gb_Arena* arena); +void gb_arena_clear(gb_Arena* arena); -typedef struct gb_Temporary_Arena_Memory { - gb_Arena *arena; +typedef struct gb_Temporary_Arena_Memory +{ + gb_Arena* arena; s64 original_count; } gb_Temporary_Arena_Memory; -gb_Temporary_Arena_Memory gb_make_temporary_arena_memory(gb_Arena *arena); +gb_Temporary_Arena_Memory gb_make_temporary_arena_memory(gb_Arena* arena); void gb_temporary_arena_memory_free(gb_Temporary_Arena_Memory t); -typedef struct gb_Pool { +typedef struct gb_Pool +{ gb_Allocator base; /* NOTE(bill): Must be first into order to allow for polymorphism */ - gb_Allocator *backing; + gb_Allocator* backing; - void *physical_start; - void *free_list; + void* physical_start; + void* free_list; usize block_size; usize block_align; s64 total_size; } gb_Pool; -gb_Pool gb_pool_make(gb_Allocator *backing, usize num_blocks, usize block_size); -gb_Pool gb_pool_make_align(gb_Allocator *backing, usize num_blocks, usize block_size, usize block_align); -void gb_pool_destroy(gb_Pool *pool); +gb_Pool gb_pool_make(gb_Allocator* backing, usize num_blocks, usize block_size); +gb_Pool gb_pool_make_align(gb_Allocator* backing, usize num_blocks, usize block_size, usize block_align); +void gb_pool_destroy(gb_Pool* pool); @@ -602,10 +613,10 @@ void gb_pool_destroy(gb_Pool *pool); /* */ /**********************************/ -void *gb_align_forward(void *ptr, usize align); +void* gb_align_forward(void* ptr, usize align); -void *gb_zero_size(void *ptr, usize bytes); -#define gb_zero_struct(element) ((void)gb_zero_size(&(element), sizeof(element))) +void *gb_zero_size(void* ptr, usize bytes); +#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))) @@ -622,20 +633,21 @@ void *gb_zero_size(void *ptr, usize bytes); * Array structure: * * - * | Allocator * | usize count | usize capacity | char * | + * | Allocator * | usize count | usize capacity | char* | * | * `-- Returned pointer */ -typedef struct gb_Array_Header { - gb_Allocator *allocator; +typedef struct gb_Array_Header +{ + gb_Allocator* allocator; usize count; usize capacity; } gb_Array_Header; /* TODO(bill): Implement a c style array maybe like stb/stretchy_buffer.h but with a custom allocator */ -#define gb_array_header(arr) (cast(gb_Array_Header *, arr) - 1) +#define gb_array_header(arr) (cast(gb_Array_Header*, arr) - 1) #define gb_array_make_count(allocator, Type, count) /* TODO(bill): */ #define gb_array_make(allocator, Type) (gb_array_make_count(allocator, Type, 0)) @@ -667,7 +679,7 @@ typedef struct gb_Array_Header { /* Pascal like strings in C */ -typedef char *gb_String; +typedef char* gb_String; #ifndef GB_STRING_SIZE #define GB_STRING_SIZE @@ -675,38 +687,39 @@ typedef char *gb_String; typedef u32 gb_String_Size; #endif -typedef struct gb_String_Header { - gb_Allocator *allocator; +typedef struct gb_String_Header +{ + gb_Allocator* allocator; gb_String_Size length; gb_String_Size capacity; } gb_String_Header; -#define GB_STRING_HEADER(str) (cast(gb_String_Header *, str) - 1) +#define GB_STRING_HEADER(str) (cast(gb_String_Header*, str) - 1) -gb_String gb_string_make(gb_Allocator *a, const char* str); -gb_String gb_string_make_length(gb_Allocator *a, const void* str, gb_String_Size num_bytes); +gb_String gb_string_make(gb_Allocator* a, char const* str); +gb_String gb_string_make_length(gb_Allocator* a, void const* str, gb_String_Size num_bytes); void gb_string_free(gb_String str); -gb_String gb_string_duplicate(gb_Allocator *a, const gb_String str); +gb_String gb_string_duplicate(gb_Allocator* a, gb_String const str); -gb_String_Size gb_string_length(const gb_String str); -gb_String_Size gb_string_capacity(const gb_String str); -gb_String_Size gb_string_available_space(const gb_String str); +gb_String_Size gb_string_length(gb_String const str); +gb_String_Size gb_string_capacity(gb_String const str); +gb_String_Size gb_string_available_space(gb_String const str); void gb_string_clear(gb_String str); -gb_String gb_string_append_string(gb_String str, const gb_String other); -gb_String gb_string_append_string_length(gb_String str, const void *other, gb_String_Size num_bytes); -gb_String gb_string_append_cstring(gb_String str, const char *other); +gb_String gb_string_append_string(gb_String str, gb_String const other); +gb_String gb_string_append_string_length(gb_String str, void const* other, gb_String_Size num_bytes); +gb_String gb_string_append_cstring(gb_String str, char const* other); -gb_String gb_string_set(gb_String str, const char *cstr); +gb_String gb_string_set(gb_String str, char const* cstr); gb_String gb_string_make_space_for(gb_String str, gb_String_Size add_len); -gb_String_Size gb_string_allocation_size(const gb_String str); +gb_String_Size gb_string_allocation_size(gb_String const str); -bool32 gb_strings_are_equal(const gb_String lhs, const gb_String rhs); +bool32 gb_strings_are_equal(gb_String const lhs, gb_String const rhs); -gb_String gb_string_trim(gb_String str, const char *cut_set); +gb_String gb_string_trim(gb_String str, char const* cut_set); gb_String gb_string_trim_space(gb_String str); /* Whitespace ` \t\r\n\v\f` */ @@ -720,18 +733,18 @@ gb_String gb_string_trim_space(gb_String str); /* Whitespace ` \t\r\n\v\f` */ /**********************************/ -u32 gb_hash_adler32(const void *ket, u32 num_bytes); +u32 gb_hash_adler32(void const* ket, u32 num_bytes); -u32 gb_hash_crc32(const void* key, u32 num_bytes); -u64 gb_hash_crc64(const void* key, usize num_bytes); +u32 gb_hash_crc32(void const* key, u32 num_bytes); +u64 gb_hash_crc64(void const* key, usize num_bytes); -u32 gb_hash_fnv32(const void* key, usize num_bytes); -u64 gb_hash_fnv64(const void* key, usize num_bytes); -u32 gb_hash_fnv32a(const void* key, usize num_bytes); -u64 gb_hash_fnv64a(const void* key, usize num_bytes); +u32 gb_hash_fnv32(void const* key, usize num_bytes); +u64 gb_hash_fnv64(void const* key, usize num_bytes); +u32 gb_hash_fnv32a(void const* key, usize num_bytes); +u64 gb_hash_fnv64a(void const* key, usize num_bytes); -u32 gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed); -u64 gb_hash_murmur64(const void* key, usize num_bytes, u64 seed); +u32 gb_hash_murmur32(void const* key, u32 num_bytes, u32 seed); +u64 gb_hash_murmur64(void const* key, usize num_bytes, u64 seed); @@ -868,7 +881,7 @@ gb_mutex_make(void) } void -gb_mutex_destroy(gb_Mutex *m) +gb_mutex_destroy(gb_Mutex* m) { #if defined(GB_SYSTEM_WINDOWS) CloseHandle(m->win32_mutex); @@ -878,7 +891,7 @@ gb_mutex_destroy(gb_Mutex *m) } void -gb_mutex_lock(gb_Mutex *m) +gb_mutex_lock(gb_Mutex* m) { #if defined(GB_SYSTEM_WINDOWS) WaitForSingleObject(m->win32_mutex, INFINITE); @@ -888,7 +901,7 @@ gb_mutex_lock(gb_Mutex *m) } bool32 -gb_mutex_try_lock(gb_Mutex *m) +gb_mutex_try_lock(gb_Mutex* m) { #if defined(GB_SYSTEM_WINDOWS) return WaitForSingleObject(m->win32_mutex, 0) == WAIT_OBJECT_0; @@ -898,7 +911,7 @@ gb_mutex_try_lock(gb_Mutex *m) } void -gb_mutex_unlock(gb_Mutex *m) +gb_mutex_unlock(gb_Mutex* m) { #if defined(GB_SYSTEM_WINDOWS) ReleaseMutex(m->win32_mutex); @@ -912,50 +925,50 @@ gb_mutex_unlock(gb_Mutex *m) #if defined(_MSC_VER) u32 -gb_atomic32_load(const volatile gb_Atomic32 *object) +gb_atomic32_load(gb_Atomic32 const volatile* object) { return object->nonatomic; } void -gb_atomic32_store(volatile gb_Atomic32 *object, u32 value) +gb_atomic32_store(gb_Atomic32 volatile* object, u32 value) { object->nonatomic = value; } u32 -gb_atomic32_compare_exchange_strong(volatile gb_Atomic32 *object, u32 expected, u32 desired) +gb_atomic32_compare_exchange_strong(gb_Atomic32 volatile* object, u32 expected, u32 desired) { - return _InterlockedCompareExchange(cast(volatile long *, object), desired, expected); + return _InterlockedCompareExchange(cast(long volatile*, object), desired, expected); } u32 -gb_atomic32_exchanged(volatile gb_Atomic32 *object, u32 operand) +gb_atomic32_exchanged(gb_Atomic32 volatile* object, u32 operand) { - return _InterlockedExchangeAdd(cast(volatile long *, object), operand); + return _InterlockedExchangeAdd(cast(long volatile*, object), operand); } u32 -gb_atomic32_fetch_add(volatile gb_Atomic32 *object, s32 operand) +gb_atomic32_fetch_add(gb_Atomic32 volatile* object, s32 operand) { - return _InterlockedExchangeAdd(cast(volatile long *, object), operand); + return _InterlockedExchangeAdd(cast(long volatile*, object), operand); } u32 -gb_atomic32_fetch_and(volatile gb_Atomic32 *object, u32 operand) +gb_atomic32_fetch_and(gb_Atomic32 volatile* object, u32 operand) { - return _InterlockedAnd(cast(volatile long *, object), operand); + return _InterlockedAnd(cast(long volatile*, object), operand); } u32 -gb_atomic32_fetch_or(volatile gb_Atomic32 *object, u32 operand) +gb_atomic32_fetch_or(gb_Atomic32 volatile* object, u32 operand) { - return _InterlockedOr(cast(volatile long *, object), operand); + return _InterlockedOr(cast(long volatile*, object), operand); } u64 -gb_atomic64_load(const volatile gb_Atomic64 *object) +gb_atomic64_load(gb_Atomic64 const volatile* object) { #if defined(GB_ARCH_64_BIT) return object->nonatomic; @@ -975,7 +988,7 @@ gb_atomic64_load(const volatile gb_Atomic64 *object) } void -gb_atomic64_store(volatile gb_Atomic64 *object, u64 value) +gb_atomic64_store(gb_Atomic64 volatile* object, u64 value) { #if defined(GB_ARCH_64_BIT) object->nonatomic = value; @@ -993,20 +1006,21 @@ gb_atomic64_store(volatile gb_Atomic64 *object, u64 value) } u64 -gb_atomic64_compare_exchange_strong(volatile gb_Atomic64 *object, u64 expected, u64 desired) +gb_atomic64_compare_exchange_strong(gb_Atomic64 volatile* object, u64 expected, u64 desired) { - return _InterlockedCompareExchange64(cast(volatile s64 *, object), desired, expected); + return _InterlockedCompareExchange64(cast(s64 volatile*, object), desired, expected); } u64 -gb_atomic64_exchanged(volatile gb_Atomic64 *object, u64 desired) +gb_atomic64_exchanged(gb_Atomic64 volatile* object, u64 desired) { #if defined(GB_ARCH_64_BIT) - return _InterlockedExchange64(cast(volatile s64 *, object), desired); + return _InterlockedExchange64(cast(s64 volatile*, object), desired); #else u64 expected = object->nonatomic; - while (true) { - u64 original = _InterlockedCompareExchange64(cast(volatile s64 *, object), desired, expected); + while (true) + { + u64 original = _InterlockedCompareExchange64(cast(s64 volatile*, object), desired, expected); if (original == expected) return original; expected = original; } @@ -1014,14 +1028,15 @@ gb_atomic64_exchanged(volatile gb_Atomic64 *object, u64 desired) } u64 -gb_atomic64_fetch_add(volatile gb_Atomic64 *object, s64 operand) +gb_atomic64_fetch_add(gb_Atomic64 volatile* object, s64 operand) { #if defined(GB_ARCH_64_BIT) - return _InterlockedExchangeAdd64(cast(volatile s64 *, object), operand); + return _InterlockedExchangeAdd64(cast(s64 volatile*, object), operand); #else u64 expected = object->nonatomic; - while (true) { - u64 original = _InterlockedExchange64(cast(volatile s64 *, object), expected + operand, expected); + while (true) + { + u64 original = _InterlockedExchange64(cast(s64 volatile*, object), expected + operand, expected); if (original == expected) return original; expected = original; } @@ -1029,14 +1044,15 @@ gb_atomic64_fetch_add(volatile gb_Atomic64 *object, s64 operand) } u64 -gb_atomic64_fetch_and(volatile gb_Atomic64 *object, u64 operand) +gb_atomic64_fetch_and(gb_Atomic64 volatile* object, u64 operand) { #if defined(GB_ARCH_64_BIT) - return _InterlockedAnd64(cast(volatile s64 *, object), operand); + return _InterlockedAnd64(cast(s64 volatile*, object), operand); #else u64 expected = object->nonatomic; - while (true) { - u64 original = _InterlockedCompareExchange64(cast(volatile s64 *, object), expected & operand, expected); + while (true) + { + u64 original = _InterlockedCompareExchange64(cast(s64 volatile*, object), expected & operand, expected); if (original == expected) return original; expected = original; @@ -1044,14 +1060,15 @@ gb_atomic64_fetch_and(volatile gb_Atomic64 *object, u64 operand) #endif } u64 -gb_atomic64_fetch_or(volatile gb_Atomic64 *object, u64 operand) +gb_atomic64_fetch_or(gb_Atomic64 volatile* object, u64 operand) { #if defined(GB_ARCH_64_BIT) - return _InterlockedAnd64(cast(volatile s64 *, object), operand); + return _InterlockedAnd64(cast(s64 volatile*, object), operand); #else u64 expected = object->nonatomic; - while (true) { - u64 original = _InterlockedCompareExchange64(cast(volatile s64 *, object), expected | operand, expected); + while (true) + { + u64 original = _InterlockedCompareExchange64(cast(s64 volatile*, object), expected | operand, expected); if (original == expected) return original; expected = original; @@ -1084,7 +1101,7 @@ gb_semaphore_make(void) } void -gb_semaphore_destroy(gb_Semaphore *s) +gb_semaphore_destroy(gb_Semaphore* s) { #if defined(GB_SYSTEM_WINDOWS) BOOL err = CloseHandle(s->win32_handle); @@ -1097,13 +1114,13 @@ gb_semaphore_destroy(gb_Semaphore *s) } void -gb_semaphore_post(gb_Semaphore *s) +gb_semaphore_post(gb_Semaphore* s) { gb_semaphore_post_count(s, 1); } void -gb_semaphore_post_count(gb_Semaphore *s, u32 count) +gb_semaphore_post_count(gb_Semaphore* s, u32 count) { #if defined(GB_SYSTEM_WINDOWS) BOOL err = ReleaseSemaphore(s->win32_handle, count, NULL); @@ -1123,7 +1140,7 @@ gb_semaphore_post_count(gb_Semaphore *s, u32 count) } void -gb_semaphore_wait(gb_Semaphore *s) +gb_semaphore_wait(gb_Semaphore* s) { #if defined(GB_SYSTEM_WINDOWS) DWORD result = WaitForSingleObject(s->win32_handle, INFINITE); @@ -1163,14 +1180,14 @@ gb_thread_make(void) } void -gb_thread_destroy(gb_Thread *t) +gb_thread_destroy(gb_Thread* t) { if (t->is_running) gb_thread_join(t); gb_semaphore_destroy(&t->semaphore); } internal_linkage void -gb__thread_run(gb_Thread *t) +gb__thread_run(gb_Thread* t) { gb_semaphore_post(&t->semaphore); t->proc(t->data); @@ -1180,7 +1197,7 @@ gb__thread_run(gb_Thread *t) internal_linkage DWORD WINAPI gb__thread_proc(void* arg) { - gb__thread_run(cast(gb_Thread *, arg)); + gb__thread_run(cast(gb_Thread* , arg)); return 0; } @@ -1188,19 +1205,19 @@ gb__thread_proc(void* arg) internal_linkage void* gb__thread_proc(void* arg) { - gb__thread_run(cast(gb_Thread *, arg)); + gb__thread_run(cast(gb_Thread* , arg)); return NULL; } #endif void -gb_thread_start(gb_Thread *t, gb_Thread_Procedure *proc, void *data) +gb_thread_start(gb_Thread* t, gb_Thread_Procedure* proc, void* data) { gb_thread_start_with_stack(t, proc, data, 0); } void -gb_thread_start_with_stack(gb_Thread *t, gb_Thread_Procedure *proc, void *data, usize stack_size) +gb_thread_start_with_stack(gb_Thread* t, gb_Thread_Procedure* proc, void* data, usize stack_size) { GB_ASSERT(!t->is_running); GB_ASSERT(proc != NULL); @@ -1239,7 +1256,7 @@ gb_thread_start_with_stack(gb_Thread *t, gb_Thread_Procedure *proc, void *data, } void -gb_thread_join(gb_Thread *t) +gb_thread_join(gb_Thread* t) { if (!t->is_running) return; @@ -1256,9 +1273,9 @@ gb_thread_join(gb_Thread *t) } bool32 -gb_thread_is_running(const gb_Thread *t) /* NOTE(bill): Can this be just pass by value? */ +gb_thread_is_running(gb_Thread t) { - return t->is_running != 0; + return t.is_running != 0; } u32 @@ -1267,7 +1284,7 @@ gb_thread_current_id(void) u32 thread_id; #if defined(GB_SYSTEM_WINDOWS) - u8* thread_local_storage = cast(u8 *, __readgsqword(0x30)); + u8* thread_local_storage = cast(u8*, __readgsqword(0x30)); thread_id = *cast(u32 *, thread_local_storage + 0x48); #elif defined(GB_SYSTEM_OSX) && defined(GB_ARCH_64_BIT) @@ -1292,15 +1309,16 @@ gb_thread_current_id(void) /* */ /**********************************/ -typedef struct gb__Heap_Header { +typedef struct gb__Heap_Header +{ usize size; } gb__Heap_Header; internal_linkage void* -gb__heap_alloc(gb_Allocator *a, usize size, usize align) +gb__heap_alloc(gb_Allocator* a, usize size, usize align) { - gb_Heap *heap = cast(gb_Heap *, a); + gb_Heap* heap = cast(gb_Heap*, a); if (heap->use_mutex) gb_mutex_lock(&heap->mutex); @@ -1311,7 +1329,7 @@ gb__heap_alloc(gb_Allocator *a, usize size, usize align) void* data = HeapAlloc(heap->win32_heap_handle, 0, total); - gb__Heap_Header *h = cast(gb__Heap_Header *, data); + gb__Heap_Header* h = cast(gb__Heap_Header*, data); h->size = total; data = (h + 1); @@ -1331,11 +1349,11 @@ gb__heap_alloc(gb_Allocator *a, usize size, usize align) internal_linkage void -gb__heap_free(gb_Allocator *a, void *ptr) +gb__heap_free(gb_Allocator* a, void* ptr) { if (!ptr) return; - gb_Heap* heap = cast(gb_Heap *, a); + gb_Heap* heap = cast(gb_Heap*, a); if (heap->use_mutex) gb_mutex_lock(&heap->mutex); @@ -1343,7 +1361,7 @@ gb__heap_free(gb_Allocator *a, void *ptr) heap->allocation_count--; #if defined (GB_SYSTEM_WINDOWS) - gb__Heap_Header *header = cast(gb__Heap_Header *, ptr) - 1; + gb__Heap_Header* header = cast(gb__Heap_Header*, ptr) - 1; HeapFree(heap->win32_heap_handle, 0, header); #else free(ptr); @@ -1353,14 +1371,14 @@ gb__heap_free(gb_Allocator *a, void *ptr) } internal_linkage s64 -gb__heap_allocated_size(gb_Allocator *a, const void *ptr) +gb__heap_allocated_size(gb_Allocator* a, void const* ptr) { #if defined(GB_SYSTEM_WINDOWS) - gb_Heap *heap = cast(gb_Heap *, a); + gb_Heap* heap = cast(gb_Heap*, a); if (heap->use_mutex) gb_mutex_lock(&heap->mutex); - const gb__Heap_Header* h = cast(const gb__Heap_Header *, ptr) - 1; + gb__Heap_Header const* h = cast(gb__Heap_Header const*, ptr) - 1; s64 result = h->size; if (heap->use_mutex) gb_mutex_unlock(&heap->mutex); @@ -1379,9 +1397,9 @@ gb__heap_allocated_size(gb_Allocator *a, const void *ptr) } internal_linkage s64 -gb__heap_total_allocated(gb_Allocator *a) +gb__heap_total_allocated(gb_Allocator* a) { - gb_Heap *heap = cast(gb_Heap *, a); + gb_Heap* heap = cast(gb_Heap*, a); if (heap->use_mutex) gb_mutex_lock(&heap->mutex); @@ -1416,7 +1434,7 @@ gb_heap_make(bool32 use_mutex) } void -gb_heap_destroy(gb_Heap *heap) +gb_heap_destroy(gb_Heap* heap) { if (heap->use_mutex) gb_mutex_destroy(&heap->mutex); @@ -1429,10 +1447,10 @@ gb_heap_destroy(gb_Heap *heap) -internal_linkage void * -gb__arena_alloc(gb_Allocator *a, usize size, usize align) +internal_linkage void* +gb__arena_alloc(gb_Allocator* a, usize size, usize align) { - gb_Arena* arena = cast(gb_Arena *, a); + gb_Arena* arena = cast(gb_Arena*, a); s64 actual_size = size + align; @@ -1442,7 +1460,7 @@ gb__arena_alloc(gb_Allocator *a, usize size, usize align) return NULL; } - void *ptr = gb_align_forward(cast(u8 *, arena->physical_start) + arena->total_allocated_count, align); + void* ptr = gb_align_forward(cast(u8*, arena->physical_start) + arena->total_allocated_count, align); arena->total_allocated_count += actual_size; @@ -1450,14 +1468,14 @@ gb__arena_alloc(gb_Allocator *a, usize size, usize align) } internal_linkage void -gb__arena_free(gb_Allocator *a, void *ptr) /* NOTE(bill): Arenas free all at once */ +gb__arena_free(gb_Allocator* a, void* ptr) /* NOTE(bill): Arenas free all at once */ { GB_UNUSED(a); GB_UNUSED(ptr); } internal_linkage s64 -gb__arena_allocated_size(gb_Allocator *a, const void* ptr) +gb__arena_allocated_size(gb_Allocator* a, void const* ptr) { GB_UNUSED(a); GB_UNUSED(ptr); @@ -1465,15 +1483,15 @@ gb__arena_allocated_size(gb_Allocator *a, const void* ptr) } internal_linkage s64 -gb__arena_total_allocated(gb_Allocator *a) +gb__arena_total_allocated(gb_Allocator* a) { - return cast(gb_Arena *, a)->total_allocated_count; + return cast(gb_Arena*, a)->total_allocated_count; } gb_Arena -gb_arena_make_from_backing(gb_Allocator *backing, usize size) +gb_arena_make_from_backing(gb_Allocator* backing, usize size) { gb_Arena arena = {0}; @@ -1494,7 +1512,7 @@ gb_arena_make_from_backing(gb_Allocator *backing, usize size) } gb_Arena -gb_arena_make_from_pointer(void *start, usize size) +gb_arena_make_from_pointer(void* start, usize size) { gb_Arena arena = {0}; @@ -1513,7 +1531,7 @@ gb_arena_make_from_pointer(void *start, usize size) } void -gb_arena_destroy(gb_Arena *arena) +gb_arena_destroy(gb_Arena* arena) { if (arena->backing) gb_free(arena->backing, arena->physical_start); @@ -1522,7 +1540,7 @@ gb_arena_destroy(gb_Arena *arena) } void -gb_arena_clear(gb_Arena *arena) +gb_arena_clear(gb_Arena* arena) { GB_ASSERT(arena->temp_count == 0); @@ -1532,7 +1550,7 @@ gb_arena_clear(gb_Arena *arena) gb_Temporary_Arena_Memory -gb_make_temporary_arena_memory(gb_Arena *arena) +gb_make_temporary_arena_memory(gb_Arena* arena) { gb_Temporary_Arena_Memory tmp = {0}; tmp.arena = arena; @@ -1557,18 +1575,18 @@ gb_temporary_arena_memory_free(gb_Temporary_Arena_Memory tmp) -internal_linkage void * -gb__pool_alloc(gb_Allocator *a, usize size, usize align) +internal_linkage void* +gb__pool_alloc(gb_Allocator* a, usize size, usize align) { - gb_Pool *pool = cast(gb_Pool *, a); + gb_Pool* pool = cast(gb_Pool*, a); GB_ASSERT(size == pool->block_size); GB_ASSERT(align == pool->block_align); GB_ASSERT(pool->free_list != NULL); - uintptr next_free = *cast(uintptr *, pool->free_list); + uintptr next_free = *cast(uintptr*, pool->free_list); void* ptr = pool->free_list; - pool->free_list = cast(void *, next_free); + pool->free_list = cast(void*, next_free); pool->total_size += pool->block_size; @@ -1576,14 +1594,14 @@ gb__pool_alloc(gb_Allocator *a, usize size, usize align) } internal_linkage void -gb__pool_free(gb_Allocator *a, void *ptr) +gb__pool_free(gb_Allocator* a, void* ptr) { if (!ptr) return; - gb_Pool *pool = cast(gb_Pool *, a); + gb_Pool* pool = cast(gb_Pool*, a); - uintptr *next = cast(uintptr *, ptr); - *next = cast(uintptr , pool->free_list); + uintptr* next = cast(uintptr*, ptr); + *next = cast(uintptr, pool->free_list); pool->free_list = ptr; @@ -1591,7 +1609,7 @@ gb__pool_free(gb_Allocator *a, void *ptr) } internal_linkage s64 -gb__pool_allocated_size(gb_Allocator *a, const void *ptr) +gb__pool_allocated_size(gb_Allocator* a, void const* ptr) { GB_UNUSED(a); GB_UNUSED(ptr); @@ -1599,21 +1617,21 @@ gb__pool_allocated_size(gb_Allocator *a, const void *ptr) } internal_linkage s64 -gb__pool_total_allocated(gb_Allocator *a) +gb__pool_total_allocated(gb_Allocator* a) { - gb_Pool *pool = cast(gb_Pool *, a); + gb_Pool* pool = cast(gb_Pool*, a); return pool->total_size; } gb_Pool -gb_pool_make(gb_Allocator *backing, usize num_blocks, usize block_size) +gb_pool_make(gb_Allocator* backing, usize num_blocks, usize block_size) { return gb_pool_make_align(backing, num_blocks, block_size, GB_DEFAULT_ALIGNMENT); } gb_Pool -gb_pool_make_align(gb_Allocator *backing, usize num_blocks, usize block_size, usize block_align) +gb_pool_make_align(gb_Allocator* backing, usize num_blocks, usize block_size, usize block_align) { gb_Pool pool = {0}; @@ -1624,19 +1642,19 @@ gb_pool_make_align(gb_Allocator *backing, usize num_blocks, usize block_size, us usize actual_block_size = block_size + block_align; usize pool_size = num_blocks * actual_block_size; - u8 *data = cast(u8 *, gb_alloc_align(backing, pool_size, block_align)); + u8* data = cast(u8*, gb_alloc_align(backing, pool_size, block_align)); /* Init intrusive freelist */ - u8 *curr = data; + u8* curr = data; for (usize block_index = 0; block_index < num_blocks-1; block_index++) { - uintptr *next = cast(uintptr *, curr); + uintptr* next = cast(uintptr*, curr); *next = cast(uintptr, curr) + actual_block_size; curr += actual_block_size; } - uintptr *end = cast(uintptr *, curr); + uintptr* end = cast(uintptr*, curr); *end = cast(uintptr, NULL); pool.physical_start = data; @@ -1652,7 +1670,7 @@ gb_pool_make_align(gb_Allocator *backing, usize num_blocks, usize block_size, us } void -gb_pool_destroy(gb_Pool *pool) +gb_pool_destroy(gb_Pool* pool) { gb_free(pool->backing, pool->physical_start); } @@ -1666,18 +1684,18 @@ gb_pool_destroy(gb_Pool *pool) /* */ /**********************************/ -void * -gb_align_forward(void *ptr, usize align) +void* +gb_align_forward(void* ptr, usize align) { GB_ASSERT(GB_IS_POWER_OF_TWO(align)); uintptr p = cast(uintptr, ptr); - const usize modulo = p % align; + usize modulo = p % align; if (modulo) p += (align - modulo); - return cast(void *, p); + return cast(void*, p); } -void *gb_zero_size(void *ptr, usize bytes) { return memset(ptr, 0, bytes); } +void* gb_zero_size(void* ptr, usize bytes) { return memset(ptr, 0, bytes); } @@ -1704,23 +1722,23 @@ gb__string_set_capacity(gb_String str, gb_String_Size cap) gb_String -gb_string_make(gb_Allocator *a, const char* str) +gb_string_make(gb_Allocator* a, char const* str) { gb_String_Size len = cast(gb_String_Size, str ? strlen(str) : 0); return gb_string_make_length(a, str, len); } gb_String -gb_string_make_length(gb_Allocator *a, const void* init_str, gb_String_Size num_bytes) +gb_string_make_length(gb_Allocator* a, void const* init_str, gb_String_Size num_bytes) { gb_String_Size header_size = sizeof(gb_String_Header); - void *ptr = gb_alloc(a, header_size + num_bytes + 1); + void* ptr = gb_alloc(a, header_size + num_bytes + 1); if (!init_str) gb_zero_size(ptr, header_size + num_bytes + 1); if (ptr == NULL) return NULL; - gb_String str = (char *)ptr + header_size; - gb_String_Header *header = GB_STRING_HEADER(str); + gb_String str = (char*)ptr + header_size; + gb_String_Header* header = GB_STRING_HEADER(str); header->allocator = a; header->length = num_bytes; header->capacity = num_bytes; @@ -1736,34 +1754,34 @@ gb_string_free(gb_String str) { if (str == NULL) return; - gb_String_Header *header = GB_STRING_HEADER(str); + gb_String_Header* header = GB_STRING_HEADER(str); gb_free(header->allocator, header); } gb_String -gb_string_duplicate(gb_Allocator *a, const gb_String str) +gb_string_duplicate(gb_Allocator* a, gb_String const str) { return gb_string_make_length(a, str, gb_string_length(str)); } gb_String_Size -gb_string_length(const gb_String str) +gb_string_length(gb_String const str) { return GB_STRING_HEADER(str)->length; } gb_String_Size -gb_string_capacity(const gb_String str) +gb_string_capacity(gb_String const str) { return GB_STRING_HEADER(str)->capacity; } gb_String_Size -gb_string_available_space(const gb_String str) +gb_string_available_space(gb_String const str) { - gb_String_Header *h = GB_STRING_HEADER(str); + gb_String_Header* h = GB_STRING_HEADER(str); if (h->capacity > h->length) return h->capacity - h->length; return 0; @@ -1779,13 +1797,13 @@ gb_string_clear(gb_String str) gb_String -gb_string_append_string(gb_String str, const gb_String other) +gb_string_append_string(gb_String str, gb_String const other) { return gb_string_append_string_length(str, other, gb_string_length(other)); } gb_String -gb_string_append_string_length(gb_String str, const void *other, gb_String_Size other_len) +gb_string_append_string_length(gb_String str, void const* other, gb_String_Size other_len) { gb_String_Size curr_len = gb_string_length(str); @@ -1801,14 +1819,14 @@ gb_string_append_string_length(gb_String str, const void *other, gb_String_Size } gb_String -gb_string_append_cstring(gb_String str, const char *other) +gb_string_append_cstring(gb_String str, char const* other) { return gb_string_append_string_length(str, other, cast(gb_String_Size, strlen(other))); } gb_String -gb_string_set(gb_String str, const char *cstr) +gb_string_set(gb_String str, char const* cstr) { gb_String_Size len = cast(gb_String_Size, strlen(cstr)); if (gb_string_capacity(str) < len) @@ -1826,8 +1844,8 @@ gb_string_set(gb_String str, const char *cstr) } -internal_linkage void * -gb__string_realloc(gb_Allocator *a, void *ptr, gb_String_Size old_size, gb_String_Size new_size) +internal_linkage void* +gb__string_realloc(gb_Allocator* a, void* ptr, gb_String_Size old_size, gb_String_Size new_size) { if (!ptr) return gb_alloc(a, new_size); @@ -1861,14 +1879,14 @@ gb_string_make_space_for(gb_String str, gb_String_Size add_len) return str; - void *ptr = GB_STRING_HEADER(str); + void* ptr = GB_STRING_HEADER(str); gb_String_Size old_size = sizeof(struct gb_String_Header) + gb_string_length(str) + 1; gb_String_Size new_size = sizeof(struct gb_String_Header) + new_len + 1; - void *new_ptr = gb__string_realloc(GB_STRING_HEADER(str)->allocator, ptr, old_size, new_size); + void* new_ptr = gb__string_realloc(GB_STRING_HEADER(str)->allocator, ptr, old_size, new_size); if (new_ptr == NULL) return NULL; - str = cast(char *, GB_STRING_HEADER(new_ptr) + 1); + str = cast(char*, GB_STRING_HEADER(new_ptr) + 1); gb__string_set_capacity(str, new_len); @@ -1876,7 +1894,7 @@ gb_string_make_space_for(gb_String str, gb_String_Size add_len) } gb_String_Size -gb_string_allocation_size(const gb_String str) +gb_string_allocation_size(gb_String const str) { gb_String_Size cap = gb_string_capacity(str); return sizeof(gb_String_Header) + cap; @@ -1884,7 +1902,7 @@ gb_string_allocation_size(const gb_String str) bool32 -gb_strings_are_equal(const gb_String lhs, const gb_String rhs) +gb_strings_are_equal(gb_String const lhs, gb_String const rhs) { gb_String_Size lhs_len = gb_string_length(lhs); gb_String_Size rhs_len = gb_string_length(rhs); @@ -1901,12 +1919,12 @@ gb_strings_are_equal(const gb_String lhs, const gb_String rhs) gb_String -gb_string_trim(gb_String str, const char *cut_set) +gb_string_trim(gb_String str, char const* cut_set) { - char *start; - char *end; - char *start_pos; - char *end_pos; + char* start; + char* end; + char* start_pos; + char* end_pos; start_pos = start = str; end_pos = end = str + gb_string_length(str) - 1; @@ -1932,15 +1950,16 @@ gb_String gb_string_trim_space(gb_String str) { return gb_string_trim(str, " \t\ u32 -gb_hash_adler32(const void *key, u32 num_bytes) +gb_hash_adler32(void const* key, u32 num_bytes) { const u32 MOD_ADLER = 65521; u32 a = 1; u32 b = 0; - const u8* bytes = cast(const u8 *, key); - for (u32 i = 0; i < num_bytes; i++) { + u8 const* bytes = cast(u8 const*, key); + for (u32 i = 0; i < num_bytes; i++) + { a = (a + bytes[i]) % MOD_ADLER; b = (b + a) % MOD_ADLER; } @@ -2083,10 +2102,10 @@ global_variable const u64 GB_CRC64_TABLE[256] = { }; u32 -gb_hash_crc32(const void* key, u32 num_bytes) +gb_hash_crc32(void const* key, u32 num_bytes) { u32 result = cast(u32, ~0); - const u8 *c = cast(const u8 *, key); + u8 const* c = cast(u8 const*, key); for (u32 remaining = num_bytes; remaining--; c++) result = (result >> 8) ^ (GB_CRC32_TABLE[(result ^ *c) & 0xff]); @@ -2095,10 +2114,10 @@ gb_hash_crc32(const void* key, u32 num_bytes) } u64 -gb_hash_crc64(const void* key, usize num_bytes) +gb_hash_crc64(void const* key, usize num_bytes) { u64 result = cast(u64, ~0); - const u8 *c = cast(const u8 *, key); + u8 const* c = cast(u8 const*, key); for (usize remaining = num_bytes; remaining--; c++) result = (result >> 8) ^ (GB_CRC64_TABLE[(result ^ *c) & 0xff]); @@ -2107,60 +2126,56 @@ gb_hash_crc64(const void* key, usize num_bytes) u32 -gb_hash_fnv32(const void* key, usize num_bytes) +gb_hash_fnv32(void const* key, usize num_bytes) { u32 h = 0x811c9dc5; - const u8 *buffer = cast(const u8 *, key); + u8 const* buffer = cast(u8 const*, key); - for (usize i = 0; i < num_bytes; i++) { + for (usize i = 0; i < num_bytes; i++) h = (h * 0x01000193) ^ buffer[i]; - } return h; } u64 -gb_hash_fnv64(const void* key, usize num_bytes) +gb_hash_fnv64(void const* key, usize num_bytes) { u64 h = 0xcbf29ce484222325ull; - const u8 *buffer = cast(const u8 *, key); + u8 const* buffer = cast(u8 const*, key); - for (usize i = 0; i < num_bytes; i++) { + for (usize i = 0; i < num_bytes; i++) h = (h * 0x100000001b3ll) ^ buffer[i]; - } return h; } u32 -gb_hash_fnv32a(const void* key, usize num_bytes) +gb_hash_fnv32a(void const* key, usize num_bytes) { u32 h = 0x811c9dc5; - const u8 * buffer = cast(const u8 *, key); + u8 const* buffer = cast(u8 const*, key); - for (usize i = 0; i < num_bytes; i++) { + for (usize i = 0; i < num_bytes; i++) h = (h ^ buffer[i]) * 0x01000193; - } return h; } u64 -gb_hash_fnv64a(const void* key, usize num_bytes) +gb_hash_fnv64a(void const* key, usize num_bytes) { u64 h = 0xcbf29ce484222325ull; - const u8 * buffer = cast(const u8 *, key); + u8 const* buffer = cast(u8 const*, key); - for (usize i = 0; i < num_bytes; i++) { + for (usize i = 0; i < num_bytes; i++) h = (h ^ buffer[i]) * 0x100000001b3ll; - } return h; } u32 -gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) +gb_hash_murmur32(void const* key, u32 num_bytes, u32 seed) { const u32 c1 = 0xcc9e2d51; const u32 c2 = 0x1b873593; @@ -2172,8 +2187,9 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) u32 hash = seed; const usize nblocks = num_bytes / 4; - const u32 *blocks = cast(const u32 *, key); - for (usize i = 0; i < nblocks; i++) { + const u32* blocks = cast(const u32*, key); + for (usize i = 0; i < nblocks; i++) + { u32 k = blocks[i]; k *= c1; k = (k << r1) | (k >> (32 - r1)); @@ -2183,10 +2199,11 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) hash = ((hash << r2) | (hash >> (32 - r2))) * m + n; } - const u8 *tail = (cast(const u8 *, key)) + nblocks * 4; + u8 const* tail = (cast(u8 const*, key)) + nblocks * 4; u32 k1 = 0; - switch (num_bytes & 3) { + switch (num_bytes & 3) + { case 3: k1 ^= tail[2] << 16; case 2: @@ -2212,17 +2229,18 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) #if defined(GB_ARCH_64_BIT) u64 - gb_hash_murmur64(const void* key, usize num_bytes, u64 seed) + gb_hash_murmur64(void const* key, usize num_bytes, u64 seed) { const u64 m = 0xc6a4a7935bd1e995ULL; const s32 r = 47; u64 h = seed ^ (num_bytes * m); - const u64 *data = cast(const u64 *, key); - const u64 *end = data + (num_bytes / 8); + const u64* data = cast(const u64*, key); + const u64* end = data + (num_bytes / 8); - while (data != end) { + while (data != end) + { u64 k = *data++; k *= m; @@ -2233,9 +2251,10 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) h *= m; } - const u8* data2 = cast(const u8*, data); + u8 const* data2 = cast(u8 const*, data); - switch (num_bytes & 7) { + switch (num_bytes & 7) + { case 7: h ^= cast(u64, data2[6]) << 48; case 6: h ^= cast(u64, data2[5]) << 40; case 5: h ^= cast(u64, data2[4]) << 32; @@ -2254,7 +2273,7 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) } #elif GB_ARCH_32_BIT u64 - gb_hash_murmur64(const void* key, usize num_bytes, u64 seed) + gb_hash_murmur64(void const* key, usize num_bytes, u64 seed) { const u32 m = 0x5bd1e995; const s32 r = 24; @@ -2262,9 +2281,10 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) u32 h1 = cast(u32, seed) ^ cast(u32, num_bytes); u32 h2 = cast(u32, seed >> 32); - const u32 *data = cast(const u32 *, key); + const u32* data = cast(const u32*, key); - while (num_bytes >= 8) { + while (num_bytes >= 8) + { u32 k1 = *data++; k1 *= m; k1 ^= k1 >> r; @@ -2282,7 +2302,8 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) num_bytes -= 4; } - if (num_bytes >= 4) { + if (num_bytes >= 4) + { u32 k1 = *data++; k1 *= m; k1 ^= k1 >> r; @@ -2292,10 +2313,11 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) num_bytes -= 4; } - switch (num_bytes) { - case 3: h2 ^= cast(const u8 *, data)[2] << 16; - case 2: h2 ^= cast(const u8 *, data)[1] << 8; - case 1: h2 ^= cast(const u8 *, data)[0] << 0; + switch (num_bytes) + { + case 3: h2 ^= cast(u8 const*, data)[2] << 16; + case 2: h2 ^= cast(u8 const*, data)[1] << 8; + case 1: h2 ^= cast(u8 const*, data)[0] << 0; h2 *= m; }; @@ -2344,7 +2366,7 @@ gb_hash_murmur32(const void* key, u32 num_bytes, u32 seed) /* Get the frequency of the performance counter */ /* It is constant across the program's lifetime */ local_persist LARGE_INTEGER s_frequency; - QueryPerformanceFrequency(&s_frequency); /* Is this fast enough? */ + QueryPerformanceFrequency(&s_frequency); /* TODO(bill): Is this fast enough? */ /* Get the current time */ LARGE_INTEGER t; diff --git a/gb.hpp b/gb.hpp index 89f7dff..a6dca76 100644 --- a/gb.hpp +++ b/gb.hpp @@ -1,4 +1,4 @@ -// gb.hpp - v0.31a - public domain C++11 helper library - no warranty implied; use at your own risk +// gb.hpp - v0.32 - public domain C++11 helper library - no warranty implied; use at your own risk // (Experimental) A C++11 helper library without STL geared towards game development /* @@ -39,6 +39,7 @@ CONTENTS: /* Version History: + 0.32 - Change const position convention 0.31a - Minor fixes 0.31 - Remove `_Allocator` suffix for allocator types 0.30 - sort::quick @@ -252,7 +253,10 @@ Version History: //////////////////////////////// #include +#include +#if !defined(GB_NO_STDIO) #include +#endif #if defined(GB_SYSTEM_WINDOWS) #define NOMINMAX 1 @@ -275,7 +279,9 @@ Version History: #endif - +#ifndef GB_UNUSED +#define GB_UNUSED(x) ((void)sizeof(x)) +#endif #if !defined(GB_ASSERT) #if !defined(NDEBUG) @@ -284,9 +290,9 @@ Version History: // Helper function used as a better alternative to assert which allows for // optional printf style error messages extern "C" void - gb__assert_handler(bool condition, const char* condition_str, - const char* filename, size_t line, - const char* error_text = nullptr, ...); + gb__assert_handler(bool condition, char const* condition_str, + char const* filename, size_t line, + char const* error_text = nullptr, ...); #else #define GB_ASSERT(x, ...) ((void)sizeof(x)) #endif @@ -297,9 +303,9 @@ Version History: // snprintf_msvc // // // //////////////////////////////// -#if defined(_MSC_VER) +#if !defined(GB_NO_STDIO) && defined(_MSC_VER) extern "C" inline int - gb__vsnprintf_compatible(char* buffer, size_t size, const char* format, va_list args) + gb__vsnprintf_compatible(char* buffer, size_t size, char const* format, va_list args) { int result = -1; if (size > 0) @@ -311,7 +317,7 @@ Version History: } extern "C" inline int - gb__snprintf_compatible(char* buffer, size_t size, const char* format, ...) + gb__snprintf_compatible(char* buffer, size_t size, char const* format, ...) { va_list args; va_start(args, format); @@ -522,18 +528,18 @@ template struct Add_Lvalue_Reference_Def { usi template struct Add_Lvalue_Reference_Def { using Type = T&; }; template struct Add_Lvalue_Reference_Def { using Type = T&; }; template <> struct Add_Lvalue_Reference_Def { using Type = void; }; -template <> struct Add_Lvalue_Reference_Def { using Type = const void; }; -template <> struct Add_Lvalue_Reference_Def { using Type = volatile void; }; -template <> struct Add_Lvalue_Reference_Def { using Type = const volatile void; }; +template <> struct Add_Lvalue_Reference_Def { using Type = void const; }; +template <> struct Add_Lvalue_Reference_Def { using Type = void volatile; }; +template <> struct Add_Lvalue_Reference_Def { using Type = void const volatile; }; template using Add_Lvalue_Reference = typename Add_Lvalue_Reference_Def::Type; template struct Add_Rvalue_Reference_Def { using Type = T&&; }; template struct Add_Rvalue_Reference_Def { using Type = T&; }; template struct Add_Rvalue_Reference_Def { using Type = T&&; }; template <> struct Add_Rvalue_Reference_Def { using Type = void; }; -template <> struct Add_Rvalue_Reference_Def { using Type = const void; }; -template <> struct Add_Rvalue_Reference_Def { using Type = volatile void; }; -template <> struct Add_Rvalue_Reference_Def { using Type = const volatile void; }; +template <> struct Add_Rvalue_Reference_Def { using Type = void const; }; +template <> struct Add_Rvalue_Reference_Def { using Type = void volatile; }; +template <> struct Add_Rvalue_Reference_Def { using Type = void const volatile; }; template using Add_Rvalue_Reference = typename Add_Rvalue_Reference_Def::Type; @@ -691,10 +697,10 @@ __GB_NAMESPACE_START // NOTE(bill): Very similar to doing `*(T*)(&u)` template inline Dest - bit_cast(const Source& source) + bit_cast(Source const& source) { static_assert(sizeof(Dest) <= sizeof(Source), - "bit_cast(const Source&) - sizeof(Dest) <= sizeof(Source)"); + "bit_cast(Source const&) - sizeof(Dest) <= sizeof(Source)"); Dest dest; ::memcpy(&dest, &source, sizeof(Dest)); return dest; @@ -705,9 +711,9 @@ __GB_NAMESPACE_START // *(T*)(&u) ~~ pseudo_cast(u) template inline T - pseudo_cast(const U& u) + pseudo_cast(U const& u) { - return reinterpret_cast(u); + return reinterpret_cast(u); } /* @@ -720,11 +726,11 @@ __GB_NAMESPACE_START // pseudo_cast - except from gb_math.hpp Sphere - calculate_min_bounding(const void* vertices, usize num_vertices, usize stride, usize offset, f32 step) + calculate_min_bounding(void const* vertices, usize num_vertices, usize stride, usize offset, f32 step) { auto gen = random::make(0); - const u8* vertex = reinterpret_cast(vertices); + u8 const* vertex = reinterpret_cast(vertices); vertex += offset; Vector3 position = pseudo_cast(vertex[0]); @@ -794,7 +800,7 @@ __GB_NAMESPACE_START //////////////////////////////// template -inline usize array_count(const T(& )[N]) { return N; } +inline usize array_count(T const(& )[N]) { return N; } inline s64 kilobytes(s64 x) { return (x) * 1024ll; } inline s64 megabytes(s64 x) { return kilobytes(x) * 1024ll; } @@ -831,21 +837,21 @@ struct Atomic64 { u64 nonatomic; }; namespace atomic { -u32 load(const volatile Atomic32* object); -void store(volatile Atomic32* object, u32 value); -u32 compare_exchange_strong(volatile Atomic32* object, u32 expected, u32 desired); -u32 exchanged(volatile Atomic32* object, u32 desired); -u32 fetch_add(volatile Atomic32* object, s32 operand); -u32 fetch_and(volatile Atomic32* object, u32 operand); -u32 fetch_or(volatile Atomic32* object, u32 operand); +u32 load(Atomic32 const volatile* object); +void store(Atomic32 volatile* object, u32 value); +u32 compare_exchange_strong(Atomic32 volatile* object, u32 expected, u32 desired); +u32 exchanged(Atomic32 volatile* object, u32 desired); +u32 fetch_add(Atomic32 volatile* object, s32 operand); +u32 fetch_and(Atomic32 volatile* object, u32 operand); +u32 fetch_or(Atomic32 volatile* object, u32 operand); -u64 load(const volatile Atomic64* object); -void store(volatile Atomic64* object, u64 value); -u64 compare_exchange_strong(volatile Atomic64* object, u64 expected, u64 desired); -u64 exchanged(volatile Atomic64* object, u64 desired); -u64 fetch_add(volatile Atomic64* object, s64 operand); -u64 fetch_and(volatile Atomic64* object, u64 operand); -u64 fetch_or(volatile Atomic64* object, u64 operand); +u64 load(Atomic64 const volatile* object); +void store(Atomic64 volatile* object, u64 value); +u64 compare_exchange_strong(Atomic64 volatile* object, u64 expected, u64 desired); +u64 exchanged(Atomic64 volatile* object, u64 desired); +u64 fetch_add(Atomic64 volatile* object, s64 operand); +u64 fetch_and(Atomic64 volatile* object, u64 operand); +u64 fetch_or(Atomic64 volatile* object, u64 operand); } // namespace atomic @@ -898,7 +904,7 @@ Thread make(); void destroy(Thread* t); void start(Thread* t, Thread_Procedure* func, void* data = nullptr, usize stack_size = 0); void join(Thread* t); -bool is_running(const Thread& t); +bool is_running(Thread const& t); u32 current_id(); } // namespace thread @@ -921,7 +927,7 @@ struct Allocator /// // If the allocator does not support tracking of the allocation size, // the function will return -1 - s64 (*allocated_size)(Allocator* a, const void* ptr); + s64 (*allocated_size)(Allocator* a, void const* ptr); // Returns the total amount of memory allocated by this allocator /// // If the allocator does not track memory, the function will return -1 @@ -1021,15 +1027,15 @@ namespace memory void* align_forward(void* ptr, usize align); void* pointer_add(void* ptr, usize bytes); void* pointer_sub(void* ptr, usize bytes); -const void* pointer_add(const void* ptr, usize bytes); -const void* pointer_sub(const void* ptr, usize bytes); +void const* pointer_add(void const* ptr, usize bytes); +void const* pointer_sub(void const* ptr, usize bytes); void* set(void* ptr, usize bytes, u8 value); void* zero(void* ptr, usize bytes); -void* copy(const void* src, usize bytes, void* dest); -void* move(const void* src, usize bytes, void* dest); -bool equals(const void* a, const void* b, usize bytes); +void* copy(void const* src, usize bytes, void* dest); +void* move(void const* src, usize bytes, void* dest); +bool equals(void const* a, void const* b, usize bytes); // TODO(bill): Should this be just zero(T*) ??? template @@ -1039,7 +1045,7 @@ template T* zero_array(T* ptr, usize count); template -T* copy_array(const T* src_array, usize count, T* dest_array); +T* copy_array(T const* src_array, usize count, T* dest_array); // TODO(bill): Should I implement something like std::copy, std::fill, std::fill_n ??? @@ -1056,7 +1062,7 @@ void swap(T (& a)[N], T (& b)[N]); // Allocator Functions void* alloc(Allocator* a, usize size, usize align = GB_DEFAULT_ALIGNMENT); void free(Allocator* a, void* ptr); -s64 allocated_size(Allocator* a, const void* ptr); +s64 allocated_size(Allocator* a, void const* ptr); s64 total_allocated(Allocator* a); template @@ -1093,30 +1099,30 @@ struct Header inline Header* header(String str) { return reinterpret_cast(str) - 1; } -String make(Allocator* a, const char* str = ""); -String make(Allocator* a, const void* str, Size num_bytes); +String make(Allocator* a, char const* str = ""); +String make(Allocator* a, void const* str, Size num_bytes); void free(String str); -String duplicate(Allocator* a, const String str); +String duplicate(Allocator* a, String const str); -Size length(const String str); -Size capacity(const String str); -Size available_space(const String str); +Size length(String const str); +Size capacity(String const str); +Size available_space(String const str); void clear(String str); void append(String* str, char c); -void append(String* str, const String other); -void append_cstring(String* str, const char* other); -void append(String* str, const void* other, Size num_bytes); +void append(String* str, String const other); +void append_cstring(String* str, char const* other); +void append(String* str, void const* other, Size num_bytes); void make_space_for(String* str, Size add_len); -usize allocation_size(const String str); +usize allocation_size(String const str); -bool equals(const String lhs, const String rhs); -int compare(const String lhs, const String rhs); // NOTE(bill): three-way comparison +bool equals(String const lhs, String const rhs); +int compare(String const lhs, String const rhs); // NOTE(bill): three-way comparison -void trim(String* str, const char* cut_set); +void trim(String* str, char const* cut_set); void trim_space(String* str); } // namespace string @@ -1151,14 +1157,14 @@ struct Array ~Array(); - Array(const Array& array); + Array(Array const& array); Array(Array&& array); - Array& operator=(const Array& array); + Array& operator=(Array const& array); Array& operator=(Array&& array); - const T& operator[](usize index) const; - T& operator[](usize index); + T const& operator[](usize index) const; + T& operator[](usize index); }; @@ -1178,10 +1184,10 @@ template Array make(Allocator* allocator, usize count = 0); template void free(Array* array); // Appends `item` to the end of the array -template void append(Array* a, const T& item); +template void append(Array* a, T const& item); template void append(Array* a, T&& item); // Appends `items[count]` to the end of the array -template void append(Array* a, const T* items, usize count); +template void append(Array* a, T const* items, usize count); // Pops the last item form the array. The array cannot be empty. template void pop(Array* a); @@ -1199,10 +1205,12 @@ template void grow(Array* a, usize min_capacity = 0); } // namespace array // Used to iterate over the array with a C++11 for loop -template inline T* begin(Array& a) { return a.data; } -template inline const T* begin(const Array& a) { return a.data; } -template inline T* end(Array& a) { return a.data + a.count; } -template inline const T* end(const Array& a) { return a.data + a.count; } +template inline T* begin(Array& a) { return a.data; } +template inline T const* begin(Array const& a) { return a.data; } +template inline T* begin(Array&& a) { return a.data; } +template inline T* end(Array& a) { return a.data + a.count; } +template inline T const* end(Array const& a) { return a.data + a.count; } +template inline T* end(Array&& a) { return a.data + a.count; } @@ -1232,12 +1240,12 @@ struct Hash_Table Hash_Table(); explicit Hash_Table(Allocator* a); - Hash_Table(const Hash_Table& other); + Hash_Table(Hash_Table const& other); Hash_Table(Hash_Table&& other); ~Hash_Table() = default; - Hash_Table& operator=(const Hash_Table& other); + Hash_Table& operator=(Hash_Table const& other); Hash_Table& operator=(Hash_Table&& other); }; @@ -1247,11 +1255,11 @@ namespace hash_table template Hash_Table make(Allocator* a); // Return `true` if the specified key exist in the hash table -template bool has(const Hash_Table& h, u64 key); +template bool has(Hash_Table const& h, u64 key); // Returns the value stored at the key, or a `default_value` if the key is not found in the hash table -template const T& get(const Hash_Table& h, u64 key, const T& default_value); +template T const& get(Hash_Table const& h, u64 key, T const& default_value); // Sets the value for the key in the hash table -template void set(Hash_Table* h, u64 key, const T& value); +template void set(Hash_Table* h, u64 key, T const& value); template void set(Hash_Table* h, u64 key, T&& value); // Removes the key from the hash table if it exists template void remove(Hash_Table* h, u64 key); @@ -1262,26 +1270,26 @@ template void clear(Hash_Table* h); } // namespace hash_table // Used to iterate over the array with a C++11 for loop - in random order -template typename const Hash_Table::Entry* begin(const Hash_Table& h); -template typename const Hash_Table::Entry* end(const Hash_Table& h); +template typename Hash_Table::Entry const* begin(Hash_Table const& h); +template typename Hash_Table::Entry const* end(Hash_Table const& h); namespace multi_hash_table { // Outputs all the items that with the specified key -template void get(const Hash_Table& h, u64 key, Array& items); +template void get(Hash_Table const& h, u64 key, Array& items); // Returns the count of entries with the specified key -template usize count(const Hash_Table& h, u64 key); +template usize count(Hash_Table const& h, u64 key); // Finds the first entry with specified key in the hash table -template typename const Hash_Table::Entry* find_first(const Hash_Table& h, u64 key); +template typename Hash_Table::Entry const* find_first(Hash_Table const& h, u64 key); // Finds the next entry with same key as `e` -template typename const Hash_Table::Entry* find_next(const Hash_Table& h, typename const Hash_Table::Entry* e); +template typename Hash_Table::Entry const* find_next(Hash_Table const& h, typename Hash_Table::Entry const* e); // Inserts the `value` as an additional value for the specified key -template void insert(Hash_Table* h, u64 key, const T& value); +template void insert(Hash_Table* h, u64 key, T const& value); template void insert(Hash_Table* h, u64 key, T&& value); // Removes a specified entry `e` from the hash table -template void remove_entry(Hash_Table* h, typename const Hash_Table::Entry* e); +template void remove_entry(Hash_Table* h, typename Hash_Table::Entry const* e); // Removes all entries with from the hash table with the specified key template void remove_all(Hash_Table* h, u64 key); } // namespace multi_hash_table @@ -1298,18 +1306,18 @@ template void remove_all(Hash_Table* h, u64 key); namespace hash { -u32 adler32(const void* key, u32 num_bytes); +u32 adler32(void const* key, u32 num_bytes); -u32 crc32(const void* key, u32 num_bytes); -u64 crc64(const void* key, usize num_bytes); +u32 crc32(void const* key, u32 num_bytes); +u64 crc64(void const* key, usize num_bytes); -u32 fnv32(const void* key, usize num_bytes); -u64 fnv64(const void* key, usize num_bytes); -u32 fnv32a(const void* key, usize num_bytes); -u64 fnv64a(const void* key, usize num_bytes); +u32 fnv32(void const* key, usize num_bytes); +u64 fnv64(void const* key, usize num_bytes); +u32 fnv32a(void const* key, usize num_bytes); +u64 fnv64a(void const* key, usize num_bytes); -u32 murmur32(const void* key, u32 num_bytes, u32 seed = 0x9747b28c); -u64 murmur64(const void* key, usize num_bytes, u64 seed = 0x9747b28c); +u32 murmur32(void const* key, u32 num_bytes, u32 seed = 0x9747b28c); +u64 murmur64(void const* key, usize num_bytes, u64 seed = 0x9747b28c); } // namespace hash @@ -1347,7 +1355,7 @@ struct Time s64 microseconds; }; -extern const Time TIME_ZERO; +extern Time const TIME_ZERO; namespace time { @@ -1452,13 +1460,13 @@ Array::Array(Allocator* a, usize count_) template inline -Array::Array(const Array& other) +Array::Array(Array const& other) : allocator(other.allocator) , count(0) , capacity(0) , data(nullptr) { - const auto new_count = other.count; + auto new_count = other.count; array::set_capacity(this, new_count); memory::copy_array(other.data, new_count, data); this->count = new_count; @@ -1487,11 +1495,11 @@ Array::~Array() template Array& -Array::operator=(const Array& other) +Array::operator=(Array const& other) { if (allocator == nullptr) allocator = other.allocator; - const auto new_count = other.count; + auto new_count = other.count; array::resize(this, new_count); memory::copy_array(other.data, new_count, data); return *this; @@ -1524,7 +1532,7 @@ Array::operator=(Array&& other) template -inline const T& +inline T const& Array::operator[](usize index) const { #if GB_ARRAY_BOUND_CHECKING @@ -1575,7 +1583,7 @@ free(Array* a) template inline void -append(Array* a, const T& item) +append(Array* a, T const& item) { if (a->capacity < a->count + 1) array::grow(a); @@ -1593,7 +1601,7 @@ append(Array* a, T&& item) template inline void -append(Array* a, const T* items, usize count) +append(Array* a, T const* items, usize count) { if (a->capacity <= a->count + static_cast(count)) array::grow(a, a->count + count); @@ -1692,7 +1700,7 @@ Hash_Table::Hash_Table(Allocator* a) template inline -Hash_Table::Hash_Table(const Hash_Table& other) +Hash_Table::Hash_Table(Hash_Table const& other) : hashes(other.hashes) , entries(other.entries) { @@ -1708,7 +1716,7 @@ Hash_Table::Hash_Table(Hash_Table&& other) template inline Hash_Table& -Hash_Table::operator=(const Hash_Table& other) +Hash_Table::operator=(Hash_Table const& other) { hashes = other.hashes; entries = other.entries; @@ -1744,12 +1752,12 @@ struct Find_Result }; template usize add_entry(Hash_Table* h, u64 key); -template void erase(Hash_Table* h, const Find_Result& fr); -template Find_Result find_result_from_key(const Hash_Table& h, u64 key); -template Find_Result find_result_from_entry(const Hash_Table& h, typename const Hash_Table::Entry* e); +template void erase(Hash_Table* h, Find_Result const& fr); +template Find_Result find_result_from_key(Hash_Table const& h, u64 key); +template Find_Result find_result_from_entry(Hash_Table const& h, typename Hash_Table::Entry const* e); template s64 make_entry(Hash_Table* h, u64 key); template void find_and_erase_entry(Hash_Table* h, u64 key); -template s64 find_entry_or_fail(const Hash_Table& h, u64 key); +template s64 find_entry_or_fail(Hash_Table const& h, u64 key); template s64 find_or_make_entry(Hash_Table* h, u64 key); template void rehash(Hash_Table* h, usize new_capacity); template void grow(Hash_Table* h); @@ -1770,7 +1778,7 @@ add_entry(Hash_Table* h, u64 key) template void -erase(Hash_Table* h, const Find_Result& fr) +erase(Hash_Table* h, Find_Result const& fr) { if (fr.data_prev < 0) h->hashes[fr.hash_index] = h->entries[fr.entry_index].next; @@ -1794,7 +1802,7 @@ erase(Hash_Table* h, const Find_Result& fr) template Find_Result -find_result_from_key(const Hash_Table& h, u64 key) +find_result_from_key(Hash_Table const& h, u64 key) { Find_Result fr = {}; fr.hash_index = -1; @@ -1820,7 +1828,7 @@ find_result_from_key(const Hash_Table& h, u64 key) template Find_Result -find_result_from_entry(const Hash_Table& h, typename const Hash_Table::Entry* e) +find_result_from_entry(Hash_Table const& h, typename Hash_Table::Entry const* e) { Find_Result fr = {}; fr.hash_index = -1; @@ -1847,8 +1855,8 @@ template s64 make_entry(Hash_Table* h, u64 key) { - const Find_Result fr = impl::find_result_from_key(*h, key); - const s64 index = impl::add_entry(h, key); + Find_Result const fr = impl::find_result_from_key(*h, key); + s64 const index = impl::add_entry(h, key); if (fr.data_prev < 0) h->hashes[fr.hash_index] = index; @@ -1864,14 +1872,14 @@ template void find_and_erase_entry(Hash_Table* h, u64 key) { - const Find_Result fr = impl::find_result_from_key(*h, key); + Find_Result const fr = impl::find_result_from_key(*h, key); if (fr.entry_index >= 0) impl::erase(h, fr); } template s64 -find_entry_or_fail(const Hash_Table& h, u64 key) +find_entry_or_fail(Hash_Table const& h, u64 key) { return impl::find_result_from_key(h, key).entry_index; } @@ -1880,7 +1888,7 @@ template s64 find_or_make_entry(Hash_Table* h, u64 key) { - const auto fr = impl::find_result_from_key(*h, key); + auto const fr = impl::find_result_from_key(*h, key); if (fr.entry_index >= 0) return fr.entry_index; @@ -1906,7 +1914,7 @@ rehash(Hash_Table* h, usize new_capacity) for (u32 i = 0; i < h->entries.count; i++) { - const auto* e = &h->entries[i]; + auto const* e = &h->entries[i]; multi_hash_table::insert(&nh, e->key, e->value); } @@ -1930,23 +1938,23 @@ bool is_full(Hash_Table* h) { // Make sure that there is enough space - const f32 maximum_load_coefficient = 0.75f; + f32 const maximum_load_coefficient = 0.75f; return h->entries.count >= maximum_load_coefficient * h->hashes.count; } } // namespace impl template inline bool -has(const Hash_Table& h, u64 key) +has(Hash_Table const& h, u64 key) { return impl::find_entry_or_fail(h, key) >= 0; } template -inline const T& -get(const Hash_Table& h, u64 key, const T& default_value) +inline T const& +get(Hash_Table const& h, u64 key, T const& default_value) { - const s64 index = impl::find_entry_or_fail(h, key); + s64 const index = impl::find_entry_or_fail(h, key); if (index < 0) return default_value; @@ -1955,12 +1963,12 @@ get(const Hash_Table& h, u64 key, const T& default_value) template inline void -set(Hash_Table* h, u64 key, const T& value) +set(Hash_Table* h, u64 key, T const& value) { if (h->hashes.count == 0) impl::grow(h); - const s64 index = impl::find_or_make_entry(h, key); + s64 const index = impl::find_or_make_entry(h, key); h->entries[index].value = value; if (impl::is_full(h)) impl::grow(h); @@ -1973,7 +1981,7 @@ set(Hash_Table* h, u64 key, T&& value) if (h->hashes.count == 0) impl::grow(h); - const s64 index = impl::find_or_make_entry(h, key); + s64 const index = impl::find_or_make_entry(h, key); h->entries[index].value = move(value); if (impl::is_full(h)) impl::grow(h); @@ -2003,15 +2011,15 @@ clear(Hash_Table* h) } // namespace hash_table template -inline typename const Hash_Table::Entry* -begin(const Hash_Table& h) +inline typename Hash_Table::Entry const* +begin(Hash_Table const& h) { return begin(h.entries); } template -inline typename const Hash_Table::Entry* -end(const Hash_Table& h) +inline typename Hash_Table::Entry const* +end(Hash_Table const& h) { return end(h.entries); } @@ -2021,7 +2029,7 @@ namespace multi_hash_table { template inline void -get(const Hash_Table& h, u64 key, Array& items) +get(Hash_Table const& h, u64 key, Array& items) { auto e = multi_hash_table::find_first(h, key); while (e) @@ -2033,7 +2041,7 @@ get(const Hash_Table& h, u64 key, Array& items) template inline usize -count(const Hash_Table& h, u64 key) +count(Hash_Table const& h, u64 key) { usize count = 0; auto e = multi_hash_table::find_first(h, key); @@ -2048,18 +2056,18 @@ count(const Hash_Table& h, u64 key) template -inline typename const Hash_Table::Entry* -find_first(const Hash_Table& h, u64 key) +inline typename Hash_Table::Entry const* +find_first(Hash_Table const& h, u64 key) { - const s64 index = hash_table::impl::find_entry_or_fail(h, key); + s64 const index = hash_table::impl::find_entry_or_fail(h, key); if (index < 0) return nullptr; return &h.entries[index]; } template -typename const Hash_Table::Entry* -find_next(const Hash_Table& h, typename const Hash_Table::Entry* e) +typename Hash_Table::Entry const* +find_next(Hash_Table const& h, typename Hash_Table::Entry const* e) { if (!e) return nullptr; @@ -2078,7 +2086,7 @@ find_next(const Hash_Table& h, typename const Hash_Table::Entry* e) template inline void -insert(Hash_Table* h, u64 key, const T& value) +insert(Hash_Table* h, u64 key, T const& value) { if (h->hashes.count == 0) hash_table::impl::grow(h); @@ -2106,9 +2114,9 @@ insert(Hash_Table* h, u64 key, T&& value) template inline void -remove_entry(Hash_Table* h, typename const Hash_Table::Entry* e) +remove_entry(Hash_Table* h, typename Hash_Table::Entry const* e) { - const auto fr = hash_table::impl::find_result_from_entry(*h, e); + auto const fr = hash_table::impl::find_result_from_entry(*h, e); if (fr.entry_index >= 0) hash_table::impl::erase(h, fr); } @@ -2142,7 +2150,7 @@ zero_array(T* ptr, usize count) template inline T* -copy_array(const T* src_array, usize count, T* dest_array) +copy_array(T const* src_array, usize count, T* dest_array) { return static_cast(memory::copy(src_array, count * sizeof(T), dest_array)); } @@ -2181,7 +2189,7 @@ quick(T* array, usize count, Comparison_Function compare) { if (count < 2) return; - const T& mid = array[count/2]; + T const& mid = array[count/2]; s64 i = 0; s64 j = count-1; @@ -2282,83 +2290,85 @@ __GB_NAMESPACE_END #if defined(GB_SYSTEM_WINDOWS) - #include - #pragma comment(lib, "dbghelp.lib") // TODO(bill): Should this be pragma included or not? + #if !defined(GB_NO_STDIO) + #include + #pragma comment(lib, "dbghelp.lib") // TODO(bill): Should this be pragma included or not? - internal_linkage void - gb__print_call_stack(FILE* out_stream) - { - SymInitialize(GetCurrentProcess(), nullptr, true); - SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); - - DWORD mtype = {}; - CONTEXT ctx = {}; - ctx.ContextFlags = CONTEXT_CONTROL; - - RtlCaptureContext(&ctx); - - STACKFRAME64 stack = {}; - - #if defined(_M_IX86) - mtype = IMAGE_FILE_MACHINE_I386; - stack.AddrPC.Offset = ctx.Eip; - stack.AddrPC.Mode = AddrModeFlat; - stack.AddrFrame.Offset = ctx.Ebp; - stack.AddrFrame.Mode = AddrModeFlat; - stack.AddrStack.Offset = ctx.Esp; - stack.AddrStack.Mode = AddrModeFlat; - #elif defined(_M_X64) - mtype = IMAGE_FILE_MACHINE_AMD64; - stack.AddrPC.Offset = ctx.Rip; - stack.AddrPC.Mode = AddrModeFlat; - stack.AddrFrame.Offset = ctx.Rsp; - stack.AddrFrame.Mode = AddrModeFlat; - stack.AddrStack.Offset = ctx.Rsp; - stack.AddrStack.Mode = AddrModeFlat; - #else - #error Unknown Windows Platform - #endif - - DWORD ldsp = 0; - IMAGEHLP_LINE64 line = {}; - line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); - - char buf[sizeof(SYMBOL_INFO) + (MAX_SYM_NAME * sizeof(TCHAR))]; - - SYMBOL_INFO* sym = reinterpret_cast(buf); - sym->SizeOfStruct = sizeof(SYMBOL_INFO); - sym->MaxNameLen = MAX_SYM_NAME; - - UINT layer_count = 0; - while (StackWalk64(mtype, - GetCurrentProcess(), GetCurrentThread(), - &stack, &ctx, nullptr, - SymFunctionTableAccess64, SymGetModuleBase64, nullptr)) + internal_linkage void + gb__print_call_stack(FILE* out_stream) { - if (stack.AddrPC.Offset == 0) - break; + SymInitialize(GetCurrentProcess(), nullptr, true); + SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); - BOOL result = SymGetLineFromAddr64(GetCurrentProcess(), stack.AddrPC.Offset, &ldsp, &line); - result = result && SymFromAddr(GetCurrentProcess(), stack.AddrPC.Offset, 0, sym); + DWORD mtype = {}; + CONTEXT ctx = {}; + ctx.ContextFlags = CONTEXT_CONTROL; - if (result) + RtlCaptureContext(&ctx); + + STACKFRAME64 stack = {}; + + #if defined(_M_IX86) + mtype = IMAGE_FILE_MACHINE_I386; + stack.AddrPC.Offset = ctx.Eip; + stack.AddrPC.Mode = AddrModeFlat; + stack.AddrFrame.Offset = ctx.Ebp; + stack.AddrFrame.Mode = AddrModeFlat; + stack.AddrStack.Offset = ctx.Esp; + stack.AddrStack.Mode = AddrModeFlat; + #elif defined(_M_X64) + mtype = IMAGE_FILE_MACHINE_AMD64; + stack.AddrPC.Offset = ctx.Rip; + stack.AddrPC.Mode = AddrModeFlat; + stack.AddrFrame.Offset = ctx.Rsp; + stack.AddrFrame.Mode = AddrModeFlat; + stack.AddrStack.Offset = ctx.Rsp; + stack.AddrStack.Mode = AddrModeFlat; + #else + #error Unknown Windows Platform + #endif + + DWORD ldsp = 0; + IMAGEHLP_LINE64 line = {}; + line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); + + char buf[sizeof(SYMBOL_INFO) + (MAX_SYM_NAME * sizeof(TCHAR))]; + + SYMBOL_INFO* sym = reinterpret_cast(buf); + sym->SizeOfStruct = sizeof(SYMBOL_INFO); + sym->MaxNameLen = MAX_SYM_NAME; + + UINT layer_count = 0; + while (StackWalk64(mtype, + GetCurrentProcess(), GetCurrentThread(), + &stack, &ctx, nullptr, + SymFunctionTableAccess64, SymGetModuleBase64, nullptr)) { - fprintf(out_stream, - "\t[%u] `%s` (%s:%d)\n", - layer_count, sym->Name, line.FileName, line.LineNumber); - } - else - { - fprintf(out_stream, - "\t[%u] 0x%p\n", - layer_count, stack.AddrPC.Offset); + if (stack.AddrPC.Offset == 0) + break; + + BOOL result = SymGetLineFromAddr64(GetCurrentProcess(), stack.AddrPC.Offset, &ldsp, &line); + result = result && SymFromAddr(GetCurrentProcess(), stack.AddrPC.Offset, 0, sym); + + if (result) + { + fprintf(out_stream, + "\t[%u] `%s` (%s:%d)\n", + layer_count, sym->Name, line.FileName, line.LineNumber); + } + else + { + fprintf(out_stream, + "\t[%u] 0x%p\n", + layer_count, stack.AddrPC.Offset); + } + + layer_count++; } - layer_count++; + SymCleanup(GetCurrentProcess()); } - - SymCleanup(GetCurrentProcess()); - } + #endif #else #error gb__print_call_stack() not implemeneted // TODO(bill): Implemenet gb__print_call_stack() @@ -2367,13 +2377,14 @@ __GB_NAMESPACE_END // Helper function used as a better alternative to assert which allows for // optional printf style error messages inline void -gb__assert_handler(bool condition, const char* condition_str, - const char* filename, size_t line, - const char* error_text, ...) +gb__assert_handler(bool condition, char const* condition_str, + char const* filename, size_t line, + char const* error_text, ...) { if (condition) return; +#if !defined(GB_NO_STDIO) FILE* out_stream = stderr; fprintf(out_stream, "ASSERT! %s(%lu): %s", filename, line, condition_str); @@ -2390,7 +2401,7 @@ gb__assert_handler(bool condition, const char* condition_str, fprintf(out_stream, "Stacktrack:\n"); gb__print_call_stack(out_stream); - +#endif // TODO(bill): Are these decent breaking functions??? #if defined(GB_COMPILER_MSVC) @@ -2469,54 +2480,58 @@ unlock(Mutex* m) } } // namespace mutex + + + + // Atomics namespace atomic { #if defined(_MSC_VER) inline u32 -load(const volatile Atomic32* object) +load(Atomic32 const volatile* object) { return object->nonatomic; } inline void -store(volatile Atomic32* object, u32 value) +store(Atomic32 volatile* object, u32 value) { object->nonatomic = value; } inline u32 -compare_exchange_strong(volatile Atomic32* object, u32 expected, u32 desired) +compare_exchange_strong(Atomic32 volatile* object, u32 expected, u32 desired) { - return _InterlockedCompareExchange(reinterpret_cast(object), desired, expected); + return _InterlockedCompareExchange(reinterpret_cast(object), desired, expected); } inline u32 -exchanged(volatile Atomic32* object, u32 desired) +exchanged(Atomic32 volatile* object, u32 desired) { - return _InterlockedExchange(reinterpret_cast(object), desired); + return _InterlockedExchange(reinterpret_cast(object), desired); } inline u32 -fetch_add(volatile Atomic32* object, s32 operand) +fetch_add(Atomic32 volatile* object, s32 operand) { - return _InterlockedExchangeAdd(reinterpret_cast(object), operand); + return _InterlockedExchangeAdd(reinterpret_cast(object), operand); } inline u32 -fetch_and(volatile Atomic32* object, u32 operand) +fetch_and(Atomic32 volatile* object, u32 operand) { - return _InterlockedAnd(reinterpret_cast(object), operand); + return _InterlockedAnd(reinterpret_cast(object), operand); } inline u32 -fetch_or_32(volatile Atomic32* object, u32 operand) +fetch_or_32(Atomic32 volatile* object, u32 operand) { - return _InterlockedOr(reinterpret_cast(object), operand); + return _InterlockedOr(reinterpret_cast(object), operand); } inline u64 -load(const volatile Atomic64* object) +load(Atomic64 const volatile* object) { #if defined(GB_ARCH_64_BIT) return object->nonatomic; @@ -2537,7 +2552,7 @@ load(const volatile Atomic64* object) } inline void -store(volatile Atomic64* object, u64 value) +store(Atomic64 volatile* object, u64 value) { #if defined(GB_ARCH_64_BIT) object->nonatomic = value; @@ -2556,21 +2571,21 @@ store(volatile Atomic64* object, u64 value) } inline u64 -compare_exchange_strong(volatile Atomic64* object, u64 expected, u64 desired) +compare_exchange_strong(Atomic64 volatile* object, u64 expected, u64 desired) { - return _InterlockedCompareExchange64(reinterpret_cast(object), desired, expected); + return _InterlockedCompareExchange64(reinterpret_cast(object), desired, expected); } inline u64 -exchanged(volatile Atomic64* object, u64 desired) +exchanged(Atomic64 volatile* object, u64 desired) { #if defined(GB_ARCH_64_BIT) - return _InterlockedExchange64(reinterpret_cast(object), desired); + return _InterlockedExchange64(reinterpret_cast(object), desired); #else u64 expected = object->nonatomic; while (true) { - u64 original = _InterlockedCompareExchange64(reinterpret_cast(object), desired, expected); + u64 original = _InterlockedCompareExchange64(reinterpret_cast(object), desired, expected); if (original == expected) return original; expected = original; @@ -2579,15 +2594,15 @@ exchanged(volatile Atomic64* object, u64 desired) } inline u64 -fetch_add(volatile Atomic64* object, s64 operand) +fetch_add(Atomic64 volatile* object, s64 operand) { #if defined(GB_ARCH_64_BIT) - return _InterlockedExchangeAdd64(reinterpret_cast(object), operand); + return _InterlockedExchangeAdd64(reinterpret_cast(object), operand); #else u64 expected = object->nonatomic; while (true) { - u64 original = _InterlockedExchange64(reinterpret_cast(object), expected + operand, expected); + u64 original = _InterlockedExchange64(reinterpret_cast(object), expected + operand, expected); if (original == expected) return original; expected = original; @@ -2596,15 +2611,15 @@ fetch_add(volatile Atomic64* object, s64 operand) } inline u64 -fetch_and(volatile Atomic64* object, u64 operand) +fetch_and(Atomic64 volatile* object, u64 operand) { #if defined(GB_ARCH_64_BIT) - return _InterlockedAnd64(reinterpret_cast(object), operand); + return _InterlockedAnd64(reinterpret_cast(object), operand); #else u64 expected = object->nonatomic; while (true) { - u64 original = _InterlockedCompareExchange64(reinterpret_cast(object), expected & operand, expected); + u64 original = _InterlockedCompareExchange64(reinterpret_cast(object), expected & operand, expected); if (original == expected) return original; expected = original; @@ -2613,15 +2628,15 @@ fetch_and(volatile Atomic64* object, u64 operand) } inline u64 -fetch_or(volatile Atomic64* object, u64 operand) +fetch_or(Atomic64 volatile* object, u64 operand) { #if defined(GB_ARCH_64_BIT) - return _InterlockedAnd64(reinterpret_cast(object), operand); + return _InterlockedAnd64(reinterpret_cast(object), operand); #else u64 expected = object->nonatomic; while (true) { - u64 original = _InterlockedCompareExchange64(reinterpret_cast(object), expected | operand, expected); + u64 original = _InterlockedCompareExchange64(reinterpret_cast(object), expected | operand, expected); if (original == expected) return original; expected = original; @@ -2634,6 +2649,9 @@ fetch_or(volatile Atomic64* object, u64 operand) #endif } // namespace atomic + + + namespace semaphore { Semaphore @@ -2642,6 +2660,7 @@ make() Semaphore semaphore = {}; #if defined(GB_SYSTEM_WINDOWS) semaphore.win32_handle = CreateSemaphore(nullptr, 0, GB_S32_MAX, nullptr); + GB_ASSERT(semaphore.win32_handle != nullptr, "CreateSemaphore: GetLastError = %d", GetLastError()); #else @@ -2822,7 +2841,7 @@ join(Thread* t) } inline bool -is_running(const Thread& thread) +is_running(Thread const& thread) { return thread.is_running != 0; } @@ -2912,14 +2931,14 @@ free(Allocator* a, void* ptr) } inline s64 -allocated_size(Allocator* a, const void* ptr) +allocated_size(Allocator* a, void const* ptr) { #if defined(GB_SYSTEM_WINDOWS) auto* heap = reinterpret_cast(a); if (heap->use_mutex) mutex::lock(&heap->mutex); - const auto* h = static_cast(ptr) - 1; + auto const* h = static_cast(ptr) - 1; s64 result = h->size; if (heap->use_mutex) mutex::unlock(&heap->mutex); @@ -3010,7 +3029,7 @@ alloc(Allocator* a, usize size, usize align) inline void free(Allocator*, void*) {} // NOTE(bill): Arenas free all at once -inline s64 allocated_size(Allocator*, const void*) { return -1; } +inline s64 allocated_size(Allocator*, void const*) { return -1; } inline s64 total_allocated(Allocator* a) @@ -3142,7 +3161,7 @@ free(Allocator* a, void* ptr) } internal_linkage s64 -allocated_size(Allocator*, const void*) +allocated_size(Allocator*, void const*) { return -1; } @@ -3233,10 +3252,10 @@ pointer_add(void* ptr, usize bytes) return static_cast(static_cast(ptr) + bytes); } -inline const void* -pointer_add(const void* ptr, usize bytes) +inline void const* +pointer_add(void const* ptr, usize bytes) { - return static_cast(static_cast(ptr) + bytes); + return static_cast(static_cast(ptr) + bytes); } inline void* @@ -3245,10 +3264,10 @@ pointer_sub(void* ptr, usize bytes) return static_cast(static_cast(ptr) - bytes); } -inline const void* -pointer_sub(const void* ptr, usize bytes) +inline void const* +pointer_sub(void const* ptr, usize bytes) { - return static_cast(static_cast(ptr) - bytes); + return static_cast(static_cast(ptr) - bytes); } GB_FORCE_INLINE void* @@ -3264,19 +3283,19 @@ zero(void* ptr, usize bytes) } GB_FORCE_INLINE void* -copy(const void* src, usize bytes, void* dest) +copy(void const* src, usize bytes, void* dest) { return memcpy(dest, src, bytes); } GB_FORCE_INLINE void* -move(const void* src, usize bytes, void* dest) +move(void const* src, usize bytes, void* dest) { return memmove(dest, src, bytes); } GB_FORCE_INLINE bool -equals(const void* a, const void* b, usize bytes) +equals(void const* a, void const* b, usize bytes) { return (memcmp(a, b, bytes) == 0); } @@ -3297,7 +3316,7 @@ free(Allocator* a, void* ptr) } inline s64 -allocated_size(Allocator* a, const void* ptr) +allocated_size(Allocator* a, void const* ptr) { GB_ASSERT(a != nullptr); return a->allocated_size(a, ptr); @@ -3319,13 +3338,13 @@ total_allocated(Allocator* a) namespace string { inline String -make(Allocator* a, const char* str) +make(Allocator* a, char const* str) { return string::make(a, str, (string::Size)strlen(str)); } String -make(Allocator* a, const void* init_str, Size len) +make(Allocator* a, void const* init_str, Size len) { usize header_size = sizeof(string::Header); void* ptr = alloc(a, header_size + len + 1); @@ -3358,25 +3377,25 @@ free(String str) } inline String -duplicate(Allocator* a, const String str) +duplicate(Allocator* a, String const str) { return string::make(a, str, string::length(str)); } inline Size -length(const String str) +length(String const str) { return string::header(str)->length; } inline Size -capacity(const String str) +capacity(String const str) { return string::header(str)->capacity; } inline Size -available_space(const String str) +available_space(String const str) { string::Header* h = string::header(str); if (h->capacity > h->length) @@ -3405,19 +3424,19 @@ append(String* str, char c) } inline void -append(String* str, const String other) +append(String* str, String const other) { string::append(str, other, string::length(other)); } inline void -append_cstring(String* str, const char* other) +append_cstring(String* str, char const* other) { string::append(str, other, (Size)strlen(other)); } void -append(String* str, const void* other, Size other_len) +append(String* str, void const* other, Size other_len) { Size curr_len = string::length(*str); @@ -3485,14 +3504,14 @@ make_space_for(String* str, Size add_len) } usize -allocation_size(const String str) +allocation_size(String const str) { Size cap = string::capacity(str); return sizeof(string::Header) + cap; } bool -equals(const String lhs, const String rhs) +equals(String const lhs, String const rhs) { Size lhs_len = string::length(lhs); Size rhs_len = string::length(rhs); @@ -3509,11 +3528,11 @@ equals(const String lhs, const String rhs) } int -compare(const String lhs, const String rhs) // NOTE(bill): three-way comparison +compare(String const lhs, String const rhs) // NOTE(bill): three-way comparison { // Treat as cstring - const char* str1 = lhs; - const char* str2 = rhs; + char const* str1 = lhs; + char const* str2 = rhs; int s1; int s2; do @@ -3529,7 +3548,7 @@ compare(const String lhs, const String rhs) // NOTE(bill): three-way comparison } void -trim(String* str, const char* cut_set) +trim(String* str, char const* cut_set) { char* start; char* end; @@ -3576,14 +3595,14 @@ trim_space(String* str) namespace hash { u32 -adler32(const void* key, u32 num_bytes) +adler32(void const* key, u32 num_bytes) { const u32 MOD_ADLER = 65521; u32 a = 1; u32 b = 0; - const u8* bytes = static_cast(key); + u8 const* bytes = static_cast(key); for (u32 i = 0; i < num_bytes; i++) { a = (a + bytes[i]) % MOD_ADLER; @@ -3729,10 +3748,10 @@ global_variable const u64 GB_CRC64_TABLE[256] = { u32 -crc32(const void* key, u32 num_bytes) +crc32(void const* key, u32 num_bytes) { u32 result = static_cast(~0); - const u8* c = reinterpret_cast(key); + u8 const* c = reinterpret_cast(key); for (u32 remaining = num_bytes; remaining--; c++) result = (result >> 8) ^ (GB_CRC32_TABLE[(result ^ *c) & 0xff]); @@ -3741,10 +3760,10 @@ crc32(const void* key, u32 num_bytes) } u64 -crc64(const void* key, usize num_bytes) +crc64(void const* key, usize num_bytes) { u64 result = static_cast(~0); - const u8* c = reinterpret_cast(key); + u8 const* c = reinterpret_cast(key); for (usize remaining = num_bytes; remaining--; c++) result = (result >> 8) ^ (GB_CRC64_TABLE[(result ^ *c) & 0xff]); @@ -3752,10 +3771,10 @@ crc64(const void* key, usize num_bytes) } inline u32 -fnv32(const void* key, usize num_bytes) +fnv32(void const* key, usize num_bytes) { u32 h = 0x811c9dc5; - const u8* buffer = static_cast(key); + u8 const* buffer = static_cast(key); for (usize i = 0; i < num_bytes; i++) { @@ -3766,10 +3785,10 @@ fnv32(const void* key, usize num_bytes) } inline u64 -fnv64(const void* key, usize num_bytes) +fnv64(void const* key, usize num_bytes) { u64 h = 0xcbf29ce484222325ull; - const u8* buffer = static_cast(key); + u8 const* buffer = static_cast(key); for (usize i = 0; i < num_bytes; i++) { @@ -3780,10 +3799,10 @@ fnv64(const void* key, usize num_bytes) } inline u32 -fnv32a(const void* key, usize num_bytes) +fnv32a(void const* key, usize num_bytes) { u32 h = 0x811c9dc5; - const u8* buffer = static_cast(key); + u8 const* buffer = static_cast(key); for (usize i = 0; i < num_bytes; i++) { @@ -3794,10 +3813,10 @@ fnv32a(const void* key, usize num_bytes) } inline u64 -fnv64a(const void* key, usize num_bytes) +fnv64a(void const* key, usize num_bytes) { u64 h = 0xcbf29ce484222325ull; - const u8* buffer = static_cast(key); + u8 const* buffer = static_cast(key); for (usize i = 0; i < num_bytes; i++) { @@ -3808,7 +3827,7 @@ fnv64a(const void* key, usize num_bytes) } u32 -murmur32(const void* key, u32 num_bytes, u32 seed) +murmur32(void const* key, u32 num_bytes, u32 seed) { const u32 c1 = 0xcc9e2d51; const u32 c2 = 0x1b873593; @@ -3831,7 +3850,7 @@ murmur32(const void* key, u32 num_bytes, u32 seed) hash = ((hash << r2) | (hash >> (32 - r2))) * m + n; } - const u8* tail = (static_cast(key)) + nblocks * 4; + u8 const* tail = (static_cast(key)) + nblocks * 4; u32 k1 = 0; switch (num_bytes & 3) { @@ -3860,7 +3879,7 @@ murmur32(const void* key, u32 num_bytes, u32 seed) #if defined(GB_ARCH_64_BIT) u64 - murmur64(const void* key, usize num_bytes, u64 seed) + murmur64(void const* key, usize num_bytes, u64 seed) { const u64 m = 0xc6a4a7935bd1e995ULL; const s32 r = 47; @@ -3882,7 +3901,7 @@ murmur32(const void* key, u32 num_bytes, u32 seed) h *= m; } - const u8* data2 = reinterpret_cast(data); + u8 const* data2 = reinterpret_cast(data); switch (num_bytes & 7) { @@ -3904,7 +3923,7 @@ murmur32(const void* key, u32 num_bytes, u32 seed) } #elif GB_ARCH_32_BIT u64 - murmur64(const void* key, usize num_bytes, u64 seed) + murmur64(void const* key, usize num_bytes, u64 seed) { const u32 m = 0x5bd1e995; const s32 r = 24; @@ -3946,9 +3965,9 @@ murmur32(const void* key, u32 num_bytes, u32 seed) switch (num_bytes) { - case 3: h2 ^= reinterpret_cast(data)[2] << 16; - case 2: h2 ^= reinterpret_cast(data)[1] << 8; - case 1: h2 ^= reinterpret_cast(data)[0] << 0; + case 3: h2 ^= reinterpret_cast(data)[2] << 16; + case 2: h2 ^= reinterpret_cast(data)[1] << 8; + case 1: h2 ^= reinterpret_cast(data)[0] << 0; h2 *= m; }; @@ -3978,7 +3997,7 @@ murmur32(const void* key, u32 num_bytes, u32 seed) // // //////////////////////////////// -const Time TIME_ZERO = time::seconds(0); +Time const TIME_ZERO = time::seconds(0); namespace time { diff --git a/gb_math.hpp b/gb_math.hpp index c5001ea..e67a003 100644 --- a/gb_math.hpp +++ b/gb_math.hpp @@ -4,6 +4,7 @@ /* Version History: + 0.04 - Change const position convention 0.03a - Remove templated clamp 0.03 - Remove templated min/max/clamp 0.02b - Typo fixes @@ -221,16 +222,6 @@ CONTENTS: #include #endif -#ifndef GB_ARRAY_BOUND_CHECKING -#define GB_ARRAY_BOUND_CHECKING 1 -#endif - - -#ifndef GB_DISABLE_COPY -#define GB_DISABLE_COPY(Type) \ - Type(const Type&) = delete; \ - Type& operator=(const Type&) = delete -#endif #if !defined(GB_ASSERT) #if !defined(NDEBUG) @@ -444,18 +435,18 @@ __GB_NAMESPACE_START // *(T*)(&u) ~~ pseudo_cast(u) template inline T - pseudo_cast(const U& u) + pseudo_cast(U const& u) { - return reinterpret_cast(u); + return reinterpret_cast(u); } // NOTE(bill): Very similar to doing `*(T*)(&u)` template inline Dest - bit_cast(const Source& source) + bit_cast(Source const& source) { static_assert(sizeof(Dest) <= sizeof(Source), - "bit_cast(const Source&) - sizeof(Dest) <= sizeof(Source)"); + "bit_cast(Source const&) - sizeof(Dest) <= sizeof(Source)"); Dest dest; ::memcpy(&dest, &source, sizeof(Dest)); return dest; @@ -486,8 +477,8 @@ struct Vector2 f32 data[2]; }; - inline const f32& operator[](usize index) const { return data[index]; } - inline f32& operator[](usize index) { return data[index]; } + inline f32 operator[](usize index) const { return data[index]; } + inline f32& operator[](usize index) { return data[index]; } }; struct Vector3 @@ -500,8 +491,8 @@ struct Vector3 f32 data[3]; }; - inline const f32& operator[](usize index) const { return data[index]; } - inline f32& operator[](usize index) { return data[index]; } + inline f32 operator[](usize index) const { return data[index]; } + inline f32& operator[](usize index) { return data[index]; } }; struct Vector4 @@ -516,8 +507,8 @@ struct Vector4 f32 data[4]; }; - inline const f32& operator[](usize index) const { return data[index]; } - inline f32& operator[](usize index) { return data[index]; } + inline f32 operator[](usize index) const { return data[index]; } + inline f32& operator[](usize index) { return data[index]; } }; struct Complex @@ -529,8 +520,8 @@ struct Complex f32 data[2]; }; - inline const f32& operator[](usize index) const { return data[index]; } - inline f32& operator[](usize index) { return data[index]; } + inline f32 operator[](usize index) const { return data[index]; } + inline f32& operator[](usize index) { return data[index]; } }; struct Quaternion @@ -542,8 +533,8 @@ struct Quaternion f32 data[4]; }; - inline const f32& operator[](usize index) const { return data[index]; } - inline f32& operator[](usize index) { return data[index]; } + inline f32 operator[](usize index) const { return data[index]; } + inline f32& operator[](usize index) { return data[index]; } }; struct Matrix2 @@ -555,8 +546,8 @@ struct Matrix2 f32 data[4]; }; - inline const Vector2& operator[](usize index) const { return columns[index]; } - inline Vector2& operator[](usize index) { return columns[index]; } + inline Vector2 operator[](usize index) const { return columns[index]; } + inline Vector2& operator[](usize index) { return columns[index]; } }; struct Matrix3 @@ -568,8 +559,8 @@ struct Matrix3 f32 data[9]; }; - inline const Vector3& operator[](usize index) const { return columns[index]; } - inline Vector3& operator[](usize index) { return columns[index]; } + inline Vector3 operator[](usize index) const { return columns[index]; } + inline Vector3& operator[](usize index) { return columns[index]; } }; struct Matrix4 @@ -581,8 +572,8 @@ struct Matrix4 f32 data[16]; }; - inline const Vector4& operator[](usize index) const { return columns[index]; } - inline Vector4& operator[](usize index) { return columns[index]; } + inline Vector4 operator[](usize index) const { return columns[index]; } + inline Vector4& operator[](usize index) { return columns[index]; } }; struct Angle @@ -634,170 +625,170 @@ struct Plane //////////////////////////////// // Vector2 Operators -bool operator==(const Vector2& a, const Vector2& b); -bool operator!=(const Vector2& a, const Vector2& b); +bool operator==(Vector2 a, Vector2 b); +bool operator!=(Vector2 a, Vector2 b); -Vector2 operator+(const Vector2& a); -Vector2 operator-(const Vector2& a); +Vector2 operator+(Vector2 a); +Vector2 operator-(Vector2 a); -Vector2 operator+(const Vector2& a, const Vector2& b); -Vector2 operator-(const Vector2& a, const Vector2& b); +Vector2 operator+(Vector2 a, Vector2 b); +Vector2 operator-(Vector2 a, Vector2 b); -Vector2 operator*(const Vector2& a, f32 scalar); -Vector2 operator*(f32 scalar, const Vector2& a); +Vector2 operator*(Vector2 a, f32 scalar); +Vector2 operator*(f32 scalar, Vector2 a); -Vector2 operator/(const Vector2& a, f32 scalar); +Vector2 operator/(Vector2 a, f32 scalar); -Vector2 operator*(const Vector2& a, const Vector2& b); // Hadamard Product -Vector2 operator/(const Vector2& a, const Vector2& b); // Hadamard Product +Vector2 operator*(Vector2 a, Vector2 b); // Hadamard Product +Vector2 operator/(Vector2 a, Vector2 b); // Hadamard Product -Vector2& operator+=(Vector2& a, const Vector2& b); -Vector2& operator-=(Vector2& a, const Vector2& b); +Vector2& operator+=(Vector2& a, Vector2 b); +Vector2& operator-=(Vector2& a, Vector2 b); Vector2& operator*=(Vector2& a, f32 scalar); Vector2& operator/=(Vector2& a, f32 scalar); // Vector3 Operators -bool operator==(const Vector3& a, const Vector3& b); -bool operator!=(const Vector3& a, const Vector3& b); +bool operator==(Vector3 a, Vector3 b); +bool operator!=(Vector3 a, Vector3 b); -Vector3 operator+(const Vector3& a); -Vector3 operator-(const Vector3& a); +Vector3 operator+(Vector3 a); +Vector3 operator-(Vector3 a); -Vector3 operator+(const Vector3& a, const Vector3& b); -Vector3 operator-(const Vector3& a, const Vector3& b); +Vector3 operator+(Vector3 a, Vector3 b); +Vector3 operator-(Vector3 a, Vector3 b); -Vector3 operator*(const Vector3& a, f32 scalar); -Vector3 operator*(f32 scalar, const Vector3& a); +Vector3 operator*(Vector3 a, f32 scalar); +Vector3 operator*(f32 scalar, Vector3 a); -Vector3 operator/(const Vector3& a, f32 scalar); +Vector3 operator/(Vector3 a, f32 scalar); -Vector3 operator*(const Vector3& a, const Vector3& b); // Hadamard Product -Vector3 operator/(const Vector3& a, const Vector3& b); // Hadamard Product +Vector3 operator*(Vector3 a, Vector3 b); // Hadamard Product +Vector3 operator/(Vector3 a, Vector3 b); // Hadamard Product -Vector3& operator+=(Vector3& a, const Vector3& b); -Vector3& operator-=(Vector3& a, const Vector3& b); +Vector3& operator+=(Vector3& a, Vector3 b); +Vector3& operator-=(Vector3& a, Vector3 b); Vector3& operator*=(Vector3& a, f32 scalar); Vector3& operator/=(Vector3& a, f32 scalar); // Vector4 Operators -bool operator==(const Vector4& a, const Vector4& b); -bool operator!=(const Vector4& a, const Vector4& b); +bool operator==(Vector4 a, Vector4 b); +bool operator!=(Vector4 a, Vector4 b); -Vector4 operator+(const Vector4& a); -Vector4 operator-(const Vector4& a); +Vector4 operator+(Vector4 a); +Vector4 operator-(Vector4 a); -Vector4 operator+(const Vector4& a, const Vector4& b); -Vector4 operator-(const Vector4& a, const Vector4& b); +Vector4 operator+(Vector4 a, Vector4 b); +Vector4 operator-(Vector4 a, Vector4 b); -Vector4 operator*(const Vector4& a, f32 scalar); -Vector4 operator*(f32 scalar, const Vector4& a); +Vector4 operator*(Vector4 a, f32 scalar); +Vector4 operator*(f32 scalar, Vector4 a); -Vector4 operator/(const Vector4& a, f32 scalar); +Vector4 operator/(Vector4 a, f32 scalar); -Vector4 operator*(const Vector4& a, const Vector4& b); // Hadamard Product -Vector4 operator/(const Vector4& a, const Vector4& b); // Hadamard Product +Vector4 operator*(Vector4 a, Vector4 b); // Hadamard Product +Vector4 operator/(Vector4 a, Vector4 b); // Hadamard Product -Vector4& operator+=(Vector4& a, const Vector4& b); -Vector4& operator-=(Vector4& a, const Vector4& b); +Vector4& operator+=(Vector4& a, Vector4 b); +Vector4& operator-=(Vector4& a, Vector4 b); Vector4& operator*=(Vector4& a, f32 scalar); Vector4& operator/=(Vector4& a, f32 scalar); // Complex Operators -bool operator==(const Complex& a, const Complex& b); -bool operator!=(const Complex& a, const Complex& b); +bool operator==(Complex a, Complex b); +bool operator!=(Complex a, Complex b); -Complex operator+(const Complex& a); -Complex operator-(const Complex& a); +Complex operator+(Complex a); +Complex operator-(Complex a); -Complex operator+(const Complex& a, const Complex& b); -Complex operator-(const Complex& a, const Complex& b); +Complex operator+(Complex a, Complex b); +Complex operator-(Complex a, Complex b); -Complex operator*(const Complex& a, const Complex& b); -Complex operator*(const Complex& a, f32 s); -Complex operator*(f32 s, const Complex& a); +Complex operator*(Complex a, Complex b); +Complex operator*(Complex a, f32 s); +Complex operator*(f32 s, Complex a); -Complex operator/(const Complex& a, f32 s); +Complex operator/(Complex a, f32 s); // Quaternion Operators -bool operator==(const Quaternion& a, const Quaternion& b); -bool operator!=(const Quaternion& a, const Quaternion& b); +bool operator==(Quaternion a, Quaternion b); +bool operator!=(Quaternion a, Quaternion b); -Quaternion operator+(const Quaternion& a); -Quaternion operator-(const Quaternion& a); +Quaternion operator+(Quaternion a); +Quaternion operator-(Quaternion a); -Quaternion operator+(const Quaternion& a, const Quaternion& b); -Quaternion operator-(const Quaternion& a, const Quaternion& b); +Quaternion operator+(Quaternion a, Quaternion b); +Quaternion operator-(Quaternion a, Quaternion b); -Quaternion operator*(const Quaternion& a, const Quaternion& b); -Quaternion operator*(const Quaternion& a, f32 s); -Quaternion operator*(f32 s, const Quaternion& a); +Quaternion operator*(Quaternion a, Quaternion b); +Quaternion operator*(Quaternion a, f32 s); +Quaternion operator*(f32 s, Quaternion a); -Quaternion operator/(const Quaternion& a, f32 s); +Quaternion operator/(Quaternion a, f32 s); -Vector3 operator*(const Quaternion& a, const Vector3& v); // Rotate v by a +Vector3 operator*(Quaternion a, Vector3 v); // Rotate v by a // Matrix2 Operators -bool operator==(const Matrix2& a, const Matrix2& b); -bool operator!=(const Matrix2& a, const Matrix2& b); +bool operator==(Matrix2 a, Matrix2 b); +bool operator!=(Matrix2 a, Matrix2 b); -Matrix2 operator+(const Matrix2& a); -Matrix2 operator-(const Matrix2& a); +Matrix2 operator+(Matrix2 a); +Matrix2 operator-(Matrix2 a); -Matrix2 operator+(const Matrix2& a, const Matrix2& b); -Matrix2 operator-(const Matrix2& a, const Matrix2& b); +Matrix2 operator+(Matrix2 a, Matrix2 b); +Matrix2 operator-(Matrix2 a, Matrix2 b); -Matrix2 operator*(const Matrix2& a, const Matrix2& b); -Vector2 operator*(const Matrix2& a, const Vector2& v); -Matrix2 operator*(const Matrix2& a, f32 scalar); -Matrix2 operator*(f32 scalar, const Matrix2& a); +Matrix2 operator*(Matrix2 a, Matrix2 b); +Vector2 operator*(Matrix2 a, Vector2 v); +Matrix2 operator*(Matrix2 a, f32 scalar); +Matrix2 operator*(f32 scalar, Matrix2 a); -Matrix2 operator/(const Matrix2& a, f32 scalar); +Matrix2 operator/(Matrix2 a, f32 scalar); -Matrix2& operator+=(Matrix2& a, const Matrix2& b); -Matrix2& operator-=(Matrix2& a, const Matrix2& b); -Matrix2& operator*=(Matrix2& a, const Matrix2& b); +Matrix2& operator+=(Matrix2& a, Matrix2 b); +Matrix2& operator-=(Matrix2& a, Matrix2 b); +Matrix2& operator*=(Matrix2& a, Matrix2 b); // Matrix3 Operators -bool operator==(const Matrix3& a, const Matrix3& b); -bool operator!=(const Matrix3& a, const Matrix3& b); +bool operator==(Matrix3 const& a, Matrix3 const& b); +bool operator!=(Matrix3 const& a, Matrix3 const& b); -Matrix3 operator+(const Matrix3& a); -Matrix3 operator-(const Matrix3& a); +Matrix3 operator+(Matrix3 const& a); +Matrix3 operator-(Matrix3 const& a); -Matrix3 operator+(const Matrix3& a, const Matrix3& b); -Matrix3 operator-(const Matrix3& a, const Matrix3& b); +Matrix3 operator+(Matrix3 const& a, Matrix3 const& b); +Matrix3 operator-(Matrix3 const& a, Matrix3 const& b); -Matrix3 operator*(const Matrix3& a, const Matrix3& b); -Vector3 operator*(const Matrix3& a, const Vector3& v); -Matrix3 operator*(const Matrix3& a, f32 scalar); -Matrix3 operator*(f32 scalar, const Matrix3& a); +Matrix3 operator*(Matrix3 const& a, Matrix3 const& b); +Vector3 operator*(Matrix3 const& a, Vector3 v); +Matrix3 operator*(Matrix3 const& a, f32 scalar); +Matrix3 operator*(f32 scalar, Matrix3 const& a); -Matrix3 operator/(const Matrix3& a, f32 scalar); +Matrix3 operator/(Matrix3 const& a, f32 scalar); -Matrix3& operator+=(Matrix3& a, const Matrix3& b); -Matrix3& operator-=(Matrix3& a, const Matrix3& b); -Matrix3& operator*=(Matrix3& a, const Matrix3& b); +Matrix3& operator+=(Matrix3& a, Matrix3 const& b); +Matrix3& operator-=(Matrix3& a, Matrix3 const& b); +Matrix3& operator*=(Matrix3& a, Matrix3 const& b); // Matrix4 Operators -bool operator==(const Matrix4& a, const Matrix4& b); -bool operator!=(const Matrix4& a, const Matrix4& b); +bool operator==(Matrix4 const& a, Matrix4 const& b); +bool operator!=(Matrix4 const& a, Matrix4 const& b); -Matrix4 operator+(const Matrix4& a); -Matrix4 operator-(const Matrix4& a); +Matrix4 operator+(Matrix4 const& a); +Matrix4 operator-(Matrix4 const& a); -Matrix4 operator+(const Matrix4& a, const Matrix4& b); -Matrix4 operator-(const Matrix4& a, const Matrix4& b); +Matrix4 operator+(Matrix4 const& a, Matrix4 const& b); +Matrix4 operator-(Matrix4 const& a, Matrix4 const& b); -Matrix4 operator*(const Matrix4& a, const Matrix4& b); -Vector4 operator*(const Matrix4& a, const Vector4& v); -Matrix4 operator*(const Matrix4& a, f32 scalar); -Matrix4 operator*(f32 scalar, const Matrix4& a); +Matrix4 operator*(Matrix4 const& a, Matrix4 const& b); +Vector4 operator*(Matrix4 const& a, Vector4 v); +Matrix4 operator*(Matrix4 const& a, f32 scalar); +Matrix4 operator*(f32 scalar, Matrix4 const& a); -Matrix4 operator/(const Matrix4& a, f32 scalar); +Matrix4 operator/(Matrix4 const& a, f32 scalar); -Matrix4& operator+=(Matrix4& a, const Matrix4& b); -Matrix4& operator-=(Matrix4& a, const Matrix4& b); -Matrix4& operator*=(Matrix4& a, const Matrix4& b); +Matrix4& operator+=(Matrix4& a, Matrix4 const& b); +Matrix4& operator-=(Matrix4& a, Matrix4 const& b); +Matrix4& operator*=(Matrix4& a, Matrix4 const& b); // Angle Operators bool operator==(Angle a, Angle b); @@ -823,11 +814,11 @@ Angle& operator/=(Angle& a, f32 scalar); // Transform Operators // World = Parent * Local -Transform operator*(const Transform& ps, const Transform& ls); -Transform& operator*=(Transform& ps, const Transform& ls); +Transform operator*(Transform const& ps, Transform const& ls); +Transform& operator*=(Transform& ps, Transform const& ls); // Local = World / Parent -Transform operator/(const Transform& ws, const Transform& ps); -Transform& operator/=(Transform& ws, const Transform& ps); +Transform operator/(Transform const& ws, Transform const& ps); +Transform& operator/=(Transform& ws, Transform const& ps); namespace angle { @@ -849,31 +840,31 @@ f32 as_gons(Angle a); /// Math Functions & Constants /// /// /// ////////////////////////////////// -extern const Vector2 VECTOR2_ZERO; -extern const Vector3 VECTOR3_ZERO; -extern const Vector4 VECTOR4_ZERO; -extern const Complex COMPLEX_ZERO; -extern const Quaternion QUATERNION_IDENTITY; -extern const Matrix2 MATRIX2_IDENTITY; -extern const Matrix3 MATRIX3_IDENTITY; -extern const Matrix4 MATRIX4_IDENTITY; -extern const Euler_Angles EULER_ANGLES_ZERO; -extern const Transform TRANSFORM_IDENTITY; +extern Vector2 const VECTOR2_ZERO; +extern Vector3 const VECTOR3_ZERO; +extern Vector4 const VECTOR4_ZERO; +extern Complex const COMPLEX_ZERO; +extern Quaternion const QUATERNION_IDENTITY; +extern Matrix2 const MATRIX2_IDENTITY; +extern Matrix3 const MATRIX3_IDENTITY; +extern Matrix4 const MATRIX4_IDENTITY; +extern Euler_Angles const EULER_ANGLES_ZERO; +extern Transform const TRANSFORM_IDENTITY; namespace math { -extern const f32 ZERO; -extern const f32 ONE; -extern const f32 THIRD; -extern const f32 TWO_THIRDS; -extern const f32 E; -extern const f32 PI; -extern const f32 TAU; -extern const f32 SQRT_2; -extern const f32 SQRT_3; -extern const f32 SQRT_5; +extern f32 const ZERO; +extern f32 const ONE; +extern f32 const THIRD; +extern f32 const TWO_THIRDS; +extern f32 const E; +extern f32 const PI; +extern f32 const TAU; +extern f32 const SQRT_2; +extern f32 const SQRT_3; +extern f32 const SQRT_5; -extern const f32 F32_PRECISION; +extern f32 const F32_PRECISION; // Power f32 sqrt(f32 x); @@ -943,168 +934,168 @@ s64 clamp(s64 x, s64 min, s64 max); // TODO(bill): Should this be a template or just normal function overloading? template -T lerp(const T& x, const T& y, f32 t); +T lerp(T const& x, T const& y, f32 t); bool equals(f32 a, f32 b, f32 precision = F32_PRECISION); // Vector2 functions -f32 dot(const Vector2& a, const Vector2& b); -f32 cross(const Vector2& a, const Vector2& b); +f32 dot(Vector2 a, Vector2 b); +f32 cross(Vector2 a, Vector2 b); -f32 magnitude(const Vector2& a); -Vector2 normalize(const Vector2& a); +f32 magnitude(Vector2 a); +Vector2 normalize(Vector2 a); -Vector2 hadamard(const Vector2& a, const Vector2& b); +Vector2 hadamard(Vector2 a, Vector2 b); -f32 aspect_ratio(const Vector2& a); +f32 aspect_ratio(Vector2 a); // Vector3 functions -f32 dot(const Vector3& a, const Vector3& b); -Vector3 cross(const Vector3& a, const Vector3& b); +f32 dot(Vector3 a, Vector3 b); +Vector3 cross(Vector3 a, Vector3 b); -f32 magnitude(const Vector3& a); -Vector3 normalize(const Vector3& a); +f32 magnitude(Vector3 a); +Vector3 normalize(Vector3 a); -Vector3 hadamard(const Vector3& a, const Vector3& b); +Vector3 hadamard(Vector3 a, Vector3 b); // Vector4 functions -f32 dot(const Vector4& a, const Vector4& b); +f32 dot(Vector4 a, Vector4 b); -f32 magnitude(const Vector4& a); -Vector4 normalize(const Vector4& a); +f32 magnitude(Vector4 a); +Vector4 normalize(Vector4 a); -Vector4 hadamard(const Vector4& a, const Vector4& b); +Vector4 hadamard(Vector4 a, Vector4 b); // Complex functions -f32 dot(const Complex& a, const Complex& b); +f32 dot(Complex a, Complex b); -f32 magnitude(const Complex& a); -f32 norm(const Complex& a); -Complex normalize(const Complex& a); +f32 magnitude(Complex a); +f32 norm(Complex a); +Complex normalize(Complex a); -Complex conjugate(const Complex& a); -Complex inverse(const Complex& a); +Complex conjugate(Complex a); +Complex inverse(Complex a); -f32 complex_angle(const Complex& a); -inline f32 complex_argument(const Complex& a) { return complex_angle(a); } +f32 complex_angle(Complex a); +inline f32 complex_argument(Complex a) { return complex_angle(a); } Complex magnitude_angle(f32 magnitude, Angle a); inline Complex complex_polar(f32 magnitude, Angle a) { return magnitude_angle(magnitude, a); } // Quaternion functions -f32 dot(const Quaternion& a, const Quaternion& b); -Quaternion cross(const Quaternion& a, const Quaternion& b); +f32 dot(Quaternion a, Quaternion b); +Quaternion cross(Quaternion a, Quaternion b); -f32 magnitude(const Quaternion& a); -f32 norm(const Quaternion& a); -Quaternion normalize(const Quaternion& a); +f32 magnitude(Quaternion a); +f32 norm(Quaternion a); +Quaternion normalize(Quaternion a); -Quaternion conjugate(const Quaternion& a); -Quaternion inverse(const Quaternion& a); +Quaternion conjugate(Quaternion a); +Quaternion inverse(Quaternion a); -Angle quaternion_angle(const Quaternion& a); -Vector3 quaternion_axis(const Quaternion& a); -Quaternion axis_angle(const Vector3& axis, Angle a); +Angle quaternion_angle(Quaternion a); +Vector3 quaternion_axis(Quaternion a); +Quaternion axis_angle(Vector3 axis, Angle a); -Angle quaternion_roll(const Quaternion& a); -Angle quaternion_pitch(const Quaternion& a); -Angle quaternion_yaw(const Quaternion& a); +Angle quaternion_roll(Quaternion a); +Angle quaternion_pitch(Quaternion a); +Angle quaternion_yaw(Quaternion a); -Euler_Angles quaternion_to_euler_angles(const Quaternion& a); -Quaternion euler_angles_to_quaternion(const Euler_Angles& e, - const Vector3& x_axis = {1, 0, 0}, - const Vector3& y_axis = {0, 1, 0}, - const Vector3& z_axis = {0, 0, 1}); +Euler_Angles quaternion_to_euler_angles(Quaternion a); +Quaternion euler_angles_to_quaternion(Euler_Angles const& e, + Vector3 x_axis = {1, 0, 0}, + Vector3 y_axis = {0, 1, 0}, + Vector3 z_axis = {0, 0, 1}); // Spherical Linear Interpolation -Quaternion slerp(const Quaternion& x, const Quaternion& y, f32 t); +Quaternion slerp(Quaternion x, Quaternion y, f32 t); // Shoemake's Quaternion Curves // Sqherical Cubic Interpolation -Quaternion squad(const Quaternion& p, - const Quaternion& a, - const Quaternion& b, - const Quaternion& q, +Quaternion squad(Quaternion p, + Quaternion a, + Quaternion b, + Quaternion q, f32 t); // Matrix2 functions -Matrix2 transpose(const Matrix2& m); -f32 determinant(const Matrix2& m); -Matrix2 inverse(const Matrix2& m); -Matrix2 hadamard(const Matrix2& a, const Matrix2&b); -Matrix4 matrix2_to_matrix4(const Matrix2& m); +Matrix2 transpose(Matrix2 m); +f32 determinant(Matrix2 m); +Matrix2 inverse(Matrix2 m); +Matrix2 hadamard(Matrix2 a, const Matrix2&b); +Matrix4 matrix2_to_matrix4(Matrix2 m); // Matrix3 functions -Matrix3 transpose(const Matrix3& m); -f32 determinant(const Matrix3& m); -Matrix3 inverse(const Matrix3& m); -Matrix3 hadamard(const Matrix3& a, const Matrix3&b); -Matrix4 matrix3_to_matrix4(const Matrix3& m); +Matrix3 transpose(Matrix3 const& m); +f32 determinant(Matrix3 const& m); +Matrix3 inverse(Matrix3 const& m); +Matrix3 hadamard(Matrix3 const& a, const Matrix3&b); +Matrix4 matrix3_to_matrix4(Matrix3 const& m); // Matrix4 functions -Matrix4 transpose(const Matrix4& m); -f32 determinant(const Matrix4& m); -Matrix4 inverse(const Matrix4& m); -Matrix4 hadamard(const Matrix4& a, const Matrix4&b); -bool is_affine(const Matrix4& m); +Matrix4 transpose(Matrix4 const& m); +f32 determinant(Matrix4 const& m); +Matrix4 inverse(Matrix4 const& m); +Matrix4 hadamard(Matrix4 const& a, const Matrix4&b); +bool is_affine(Matrix4 const& m); -Matrix4 quaternion_to_matrix4(const Quaternion& a); -Quaternion matrix4_to_quaternion(const Matrix4& m); +Matrix4 quaternion_to_matrix4(Quaternion a); +Quaternion matrix4_to_quaternion(Matrix4 const& m); -Matrix4 translate(const Vector3& v); -Matrix4 rotate(const Vector3& v, Angle angle); -Matrix4 scale(const Vector3& v); +Matrix4 translate(Vector3 v); +Matrix4 rotate(Vector3 v, Angle angle); +Matrix4 scale(Vector3 v); Matrix4 ortho(f32 left, f32 right, f32 bottom, f32 top); Matrix4 ortho(f32 left, f32 right, f32 bottom, f32 top, f32 z_near, f32 z_far); Matrix4 perspective(Angle fovy, f32 aspect, f32 z_near, f32 z_far); Matrix4 infinite_perspective(Angle fovy, f32 aspect, f32 z_near); Matrix4 -look_at_matrix4(const Vector3& eye, const Vector3& center, const Vector3& up = {0, 1, 0}); +look_at_matrix4(Vector3 eye, Vector3 center, Vector3 up = {0, 1, 0}); Quaternion -look_at_quaternion(const Vector3& eye, const Vector3& center, const Vector3& up = {0, 1, 0}); +look_at_quaternion(Vector3 eye, Vector3 center, Vector3 up = {0, 1, 0}); // Transform Functions -Vector3 transform_point(const Transform& transform, const Vector3& point); -Transform inverse(const Transform& t); -Matrix4 transform_to_matrix4(const Transform& t); +Vector3 transform_point(Transform const& transform, Vector3 point); +Transform inverse(Transform const& t); +Matrix4 transform_to_matrix4(Transform const& t); } // namespace math namespace aabb { -Aabb calculate(const void* vertices, usize num_vertices, usize stride, usize offset); +Aabb calculate(void const* vertices, usize num_vertices, usize stride, usize offset); -f32 surface_area(const Aabb& aabb); -f32 volume(const Aabb& aabb); +f32 surface_area(Aabb const& aabb); +f32 volume(Aabb const& aabb); -Sphere to_sphere(const Aabb& aabb); +Sphere to_sphere(Aabb const& aabb); -bool contains(const Aabb& aabb, const Vector3& point); -bool contains(const Aabb& a, const Aabb& b); -bool intersects(const Aabb& a, const Aabb& b); +bool contains(Aabb const& aabb, Vector3 point); +bool contains(Aabb const& a, Aabb const& b); +bool intersects(Aabb const& a, Aabb const& b); -Aabb transform_affine(const Aabb& aabb, const Matrix4& m); +Aabb transform_affine(Aabb const& aabb, Matrix4 const& m); } // namespace aabb namespace sphere { -Sphere calculate_min_bounding_sphere(const void* vertices, usize num_vertices, usize stride, usize offset, f32 step); -Sphere calculate_max_bounding_sphere(const void* vertices, usize num_vertices, usize stride, usize offset); +Sphere calculate_min_bounding_sphere(void const* vertices, usize num_vertices, usize stride, usize offset, f32 step); +Sphere calculate_max_bounding_sphere(void const* vertices, usize num_vertices, usize stride, usize offset); -f32 surface_area(const Sphere& s); -f32 volume(const Sphere& s); +f32 surface_area(Sphere s); +f32 volume(Sphere s); -Aabb to_aabb(const Sphere& sphere); +Aabb to_aabb(Sphere sphere); -bool contains_point(const Sphere& s, const Vector3& point); +bool contains_point(Sphere s, Vector3 point); -f32 ray_intersection(const Vector3& from, const Vector3& dir, const Sphere& s); +f32 ray_intersection(Vector3 from, Vector3 dir, Sphere s); } // namespace sphere namespace plane { -f32 ray_intersection(const Vector3& from, const Vector3& dir, const Plane& p); +f32 ray_intersection(Vector3 from, Vector3 dir, Plane p); -bool intersection3(const Plane& p1, const Plane& p2, const Plane& p3, Vector3* ip); +bool intersection3(Plane p1, Plane p2, Plane p3, Vector3* ip); } // namespace plane @@ -1154,7 +1145,7 @@ f32 perlin_3d(f32 x, f32 y, f32 z, s32 x_wrap = 0, s32 y_wrap = 0, s32 z_wrap = namespace math { -template inline T lerp(const T& x, const T& y, f32 t) { return x + (y - x) * t; } +template inline T lerp(T const& x, T const& y, f32 t) { return x + (y - x) * t; } } // namespace math __GB_NAMESPACE_END @@ -1237,22 +1228,22 @@ __GB_NAMESPACE_START /// /// //////////////////////////////// -const Vector2 VECTOR2_ZERO = Vector2{0, 0}; -const Vector3 VECTOR3_ZERO = Vector3{0, 0, 0}; -const Vector4 VECTOR4_ZERO = Vector4{0, 0, 0, 0}; -const Complex COMPLEX_ZERO = Complex{0, 0}; -const Quaternion QUATERNION_IDENTITY = Quaternion{0, 0, 0, 1}; -const Matrix2 MATRIX2_IDENTITY = Matrix2{1, 0, +Vector2 const VECTOR2_ZERO = Vector2{0, 0}; +Vector3 const VECTOR3_ZERO = Vector3{0, 0, 0}; +Vector4 const VECTOR4_ZERO = Vector4{0, 0, 0, 0}; +Complex const COMPLEX_ZERO = Complex{0, 0}; +Quaternion const QUATERNION_IDENTITY = Quaternion{0, 0, 0, 1}; +Matrix2 const MATRIX2_IDENTITY = Matrix2{1, 0, 0, 1}; -const Matrix3 MATRIX3_IDENTITY = Matrix3{1, 0, 0, +Matrix3 const MATRIX3_IDENTITY = Matrix3{1, 0, 0, 0, 1, 0, 0, 0, 1}; -const Matrix4 MATRIX4_IDENTITY = Matrix4{1, 0, 0, 0, +Matrix4 const MATRIX4_IDENTITY = Matrix4{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; -const Euler_Angles EULER_ANGLES_ZERO = Euler_Angles{0, 0, 0}; -const Transform TRANSFORM_IDENTITY = Transform{VECTOR3_ZERO, QUATERNION_IDENTITY, 1}; +Euler_Angles const EULER_ANGLES_ZERO = Euler_Angles{0, 0, 0}; +Transform const TRANSFORM_IDENTITY = Transform{VECTOR3_ZERO, QUATERNION_IDENTITY, 1}; //////////////////////////////// /// Math Type Op Overloads /// @@ -1260,73 +1251,73 @@ const Transform TRANSFORM_IDENTITY = Transform{VECTOR3_ZERO, QUATERNION_IDEN // Vector2 Operators inline bool -operator==(const Vector2& a, const Vector2& b) +operator==(Vector2 a, Vector2 b) { return (a.x == b.x) && (a.y == b.y); } inline bool -operator!=(const Vector2& a, const Vector2& b) +operator!=(Vector2 a, Vector2 b) { return !operator==(a, b); } inline Vector2 -operator+(const Vector2& a) +operator+(Vector2 a) { return a; } inline Vector2 -operator-(const Vector2& a) +operator-(Vector2 a) { return {-a.x, -a.y}; } inline Vector2 -operator+(const Vector2& a, const Vector2& b) +operator+(Vector2 a, Vector2 b) { return {a.x + b.x, a.y + b.y}; } inline Vector2 -operator-(const Vector2& a, const Vector2& b) +operator-(Vector2 a, Vector2 b) { return {a.x - b.x, a.y - b.y}; } inline Vector2 -operator*(const Vector2& a, f32 scalar) +operator*(Vector2 a, f32 scalar) { return {a.x * scalar, a.y * scalar}; } inline Vector2 -operator*(f32 scalar, const Vector2& a) +operator*(f32 scalar, Vector2 a) { return {a.x * scalar, a.y * scalar}; } inline Vector2 -operator/(const Vector2& a, f32 scalar) +operator/(Vector2 a, f32 scalar) { return {a.x / scalar, a.y / scalar}; } inline Vector2 -operator*(const Vector2& a, const Vector2& b) // Hadamard Product +operator*(Vector2 a, Vector2 b) // Hadamard Product { return {a.x * b.x, a.y * b.y}; } inline Vector2 -operator/(const Vector2& a, const Vector2& b) // Hadamard Product +operator/(Vector2 a, Vector2 b) // Hadamard Product { return {a.x / b.x, a.y / b.y}; } inline Vector2& -operator+=(Vector2& a, const Vector2& b) +operator+=(Vector2& a, Vector2 b) { a.x += b.x; a.y += b.y; @@ -1335,7 +1326,7 @@ operator+=(Vector2& a, const Vector2& b) } inline Vector2& -operator-=(Vector2& a, const Vector2& b) +operator-=(Vector2& a, Vector2 b) { a.x -= b.x; a.y -= b.y; @@ -1363,73 +1354,73 @@ operator/=(Vector2& a, f32 scalar) // Vector3 Operators inline bool -operator==(const Vector3& a, const Vector3& b) +operator==(Vector3 a, Vector3 b) { return (a.x == b.x) && (a.y == b.y) && (a.z == b.z); } inline bool -operator!=(const Vector3& a, const Vector3& b) +operator!=(Vector3 a, Vector3 b) { return !operator==(a, b); } inline Vector3 -operator+(const Vector3& a) +operator+(Vector3 a) { return a; } inline Vector3 -operator-(const Vector3& a) +operator-(Vector3 a) { return {-a.x, -a.y, -a.z}; } inline Vector3 -operator+(const Vector3& a, const Vector3& b) +operator+(Vector3 a, Vector3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; } inline Vector3 -operator-(const Vector3& a, const Vector3& b) +operator-(Vector3 a, Vector3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; } inline Vector3 -operator*(const Vector3& a, f32 scalar) +operator*(Vector3 a, f32 scalar) { return {a.x * scalar, a.y * scalar, a.z * scalar}; } inline Vector3 -operator*(f32 scalar, const Vector3& a) +operator*(f32 scalar, Vector3 a) { return {a.x * scalar, a.y * scalar, a.z * scalar}; } inline Vector3 -operator/(const Vector3& a, f32 scalar) +operator/(Vector3 a, f32 scalar) { return {a.x / scalar, a.y / scalar, a.z / scalar}; } inline Vector3 -operator*(const Vector3& a, const Vector3& b) // Hadamard Product +operator*(Vector3 a, Vector3 b) // Hadamard Product { return {a.x * b.x, a.y * b.y, a.z * b.z}; } inline Vector3 -operator/(const Vector3& a, const Vector3& b) // Hadamard Product +operator/(Vector3 a, Vector3 b) // Hadamard Product { return {a.x / b.x, a.y / b.y, a.z / b.z}; } inline Vector3& -operator+=(Vector3& a, const Vector3& b) +operator+=(Vector3& a, Vector3 b) { a.x += b.x; a.y += b.y; @@ -1439,7 +1430,7 @@ operator+=(Vector3& a, const Vector3& b) } inline Vector3& -operator-=(Vector3& a, const Vector3& b) +operator-=(Vector3& a, Vector3 b) { a.x -= b.x; a.y -= b.y; @@ -1470,73 +1461,73 @@ operator/=(Vector3& a, f32 scalar) // Vector4 Operators inline bool -operator==(const Vector4& a, const Vector4& b) +operator==(Vector4 a, Vector4 b) { return (a.x == b.x) && (a.y == b.y) && (a.z == b.z) && (a.w == b.w); } inline bool -operator!=(const Vector4& a, const Vector4& b) +operator!=(Vector4 a, Vector4 b) { return !operator==(a, b); } inline Vector4 -operator+(const Vector4& a) +operator+(Vector4 a) { return a; } inline Vector4 -operator-(const Vector4& a) +operator-(Vector4 a) { return {-a.x, -a.y, -a.z, -a.w}; } inline Vector4 -operator+(const Vector4& a, const Vector4& b) +operator+(Vector4 a, Vector4 b) { return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; } inline Vector4 -operator-(const Vector4& a, const Vector4& b) +operator-(Vector4 a, Vector4 b) { return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; } inline Vector4 -operator*(const Vector4& a, f32 scalar) +operator*(Vector4 a, f32 scalar) { return {a.x * scalar, a.y * scalar, a.z * scalar, a.w * scalar}; } inline Vector4 -operator*(f32 scalar, const Vector4& a) +operator*(f32 scalar, Vector4 a) { return {a.x * scalar, a.y * scalar, a.z * scalar, a.w * scalar}; } inline Vector4 -operator/(const Vector4& a, f32 scalar) +operator/(Vector4 a, f32 scalar) { return {a.x / scalar, a.y / scalar, a.z / scalar, a.w / scalar}; } inline Vector4 -operator*(const Vector4& a, const Vector4& b) // Hadamard Product +operator*(Vector4 a, Vector4 b) // Hadamard Product { return {a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w}; } inline Vector4 -operator/(const Vector4& a, const Vector4& b) // Hadamard Product +operator/(Vector4 a, Vector4 b) // Hadamard Product { return {a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w}; } inline Vector4& -operator+=(Vector4& a, const Vector4& b) +operator+=(Vector4& a, Vector4 b) { a.x += b.x; a.y += b.y; @@ -1547,7 +1538,7 @@ operator+=(Vector4& a, const Vector4& b) } inline Vector4& -operator-=(Vector4& a, const Vector4& b) +operator-=(Vector4& a, Vector4 b) { a.x -= b.x; a.y -= b.y; @@ -1581,44 +1572,44 @@ operator/=(Vector4& a, f32 scalar) // Complex Operators inline bool -operator==(const Complex& a, const Complex& b) +operator==(Complex a, Complex b) { return (a.x == b.x) && (a.y == b.y); } inline bool -operator!=(const Complex& a, const Complex& b) +operator!=(Complex a, Complex b) { return !operator==(a, b); } inline Complex -operator+(const Complex& a) +operator+(Complex a) { return a; } inline Complex -operator-(const Complex& a) +operator-(Complex a) { return {-a.x, -a.y}; } inline Complex -operator+(const Complex& a, const Complex& b) +operator+(Complex a, Complex b) { return {a.x + b.x, a.y + b.y}; } inline Complex -operator-(const Complex& a, const Complex& b) +operator-(Complex a, Complex b) { return {a.x - b.x, a.y - b.y}; } inline Complex -operator*(const Complex& a, const Complex& b) +operator*(Complex a, Complex b) { Complex c = {}; @@ -1629,63 +1620,63 @@ operator*(const Complex& a, const Complex& b) } inline Complex -operator*(const Complex& a, f32 s) +operator*(Complex a, f32 s) { return {a.x * s, a.y * s}; } inline Complex -operator*(f32 s, const Complex& a) +operator*(f32 s, Complex a) { return {a.x * s, a.y * s}; } inline Complex -operator/(const Complex& a, f32 s) +operator/(Complex a, f32 s) { return {a.x / s, a.y / s}; } // Quaternion Operators inline bool -operator==(const Quaternion& a, const Quaternion& b) +operator==(Quaternion a, Quaternion b) { return (a.x == b.x) && (a.y == b.y) && (a.z == b.z) && (a.w == b.w); } inline bool -operator!=(const Quaternion& a, const Quaternion& b) +operator!=(Quaternion a, Quaternion b) { return !operator==(a, b); } inline Quaternion -operator+(const Quaternion& a) +operator+(Quaternion a) { return {+a.x, +a.y, +a.z, +a.w}; } inline Quaternion -operator-(const Quaternion& a) +operator-(Quaternion a) { return {-a.x, -a.y, -a.z, -a.w}; } inline Quaternion -operator+(const Quaternion& a, const Quaternion& b) +operator+(Quaternion a, Quaternion b) { return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; } inline Quaternion -operator-(const Quaternion& a, const Quaternion& b) +operator-(Quaternion a, Quaternion b) { return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; } inline Quaternion -operator*(const Quaternion& a, const Quaternion& b) +operator*(Quaternion a, Quaternion b) { Quaternion q = {}; @@ -1698,25 +1689,25 @@ operator*(const Quaternion& a, const Quaternion& b) } inline Quaternion -operator*(const Quaternion& a, f32 s) +operator*(Quaternion a, f32 s) { return {a.x * s, a.y * s, a.z * s, a.w * s}; } inline Quaternion -operator*(f32 s, const Quaternion& a) +operator*(f32 s, Quaternion a) { return {a.x * s, a.y * s, a.z * s, a.w * s}; } inline Quaternion -operator/(const Quaternion& a, f32 s) +operator/(Quaternion a, f32 s) { return {a.x / s, a.y / s, a.z / s, a.w / s}; } inline Vector3 -operator*(const Quaternion& a, const Vector3& v) // Rotate v by q +operator*(Quaternion a, Vector3 v) // Rotate v by q { // return (q * Quaternion{v.x, v.y, v.z, 0} * math::conjugate(q)).xyz; // More Expensive const Vector3 t = 2.0f * math::cross(a.xyz, v); @@ -1725,7 +1716,7 @@ operator*(const Quaternion& a, const Vector3& v) // Rotate v by q // Matrix2 Operators inline bool -operator==(const Matrix2& a, const Matrix2& b) +operator==(Matrix2 a, Matrix2 b) { for (usize i = 0; i < 4; i++) { @@ -1736,25 +1727,25 @@ operator==(const Matrix2& a, const Matrix2& b) } inline bool -operator!=(const Matrix2& a, const Matrix2& b) +operator!=(Matrix2 a, Matrix2 b) { return !operator==(a, b); } inline Matrix2 -operator+(const Matrix2& a) +operator+(Matrix2 a) { return a; } inline Matrix2 -operator-(const Matrix2& a) +operator-(Matrix2 a) { return {-a.x, -a.y}; } inline Matrix2 -operator+(const Matrix2& a, const Matrix2& b) +operator+(Matrix2 a, Matrix2 b) { Matrix2 mat; mat[0] = a[0] + b[0]; @@ -1763,7 +1754,7 @@ operator+(const Matrix2& a, const Matrix2& b) } inline Matrix2 -operator-(const Matrix2& a, const Matrix2& b) +operator-(Matrix2 a, Matrix2 b) { Matrix2 mat; mat[0] = a[0] - b[0]; @@ -1772,7 +1763,7 @@ operator-(const Matrix2& a, const Matrix2& b) } inline Matrix2 -operator*(const Matrix2& a, const Matrix2& b) +operator*(Matrix2 a, Matrix2 b) { Matrix2 result; result[0] = a[0] * b[0][0] + a[1] * b[0][1]; @@ -1781,14 +1772,14 @@ operator*(const Matrix2& a, const Matrix2& b) } inline Vector2 -operator*(const Matrix2& a, const Vector2& v) +operator*(Matrix2 a, Vector2 v) { return Vector2{a[0][0] * v.x + a[1][0] * v.y, a[0][1] * v.x + a[1][1] * v.y}; } inline Matrix2 -operator*(const Matrix2& a, f32 scalar) +operator*(Matrix2 a, f32 scalar) { Matrix2 mat; mat[0] = a[0] * scalar; @@ -1797,7 +1788,7 @@ operator*(const Matrix2& a, f32 scalar) } inline Matrix2 -operator*(f32 scalar, const Matrix2& a) +operator*(f32 scalar, Matrix2 a) { Matrix2 mat; mat[0] = a[0] * scalar; @@ -1806,7 +1797,7 @@ operator*(f32 scalar, const Matrix2& a) } inline Matrix2 -operator/(const Matrix2& a, f32 scalar) +operator/(Matrix2 a, f32 scalar) { Matrix2 mat; mat[0] = a[0] / scalar; @@ -1815,19 +1806,19 @@ operator/(const Matrix2& a, f32 scalar) } inline Matrix2& -operator+=(Matrix2& a, const Matrix2& b) +operator+=(Matrix2& a, Matrix2 b) { return (a = a + b); } inline Matrix2& -operator-=(Matrix2& a, const Matrix2& b) +operator-=(Matrix2& a, Matrix2 b) { return (a = a - b); } inline Matrix2& -operator*=(Matrix2& a, const Matrix2& b) +operator*=(Matrix2& a, Matrix2 b) { return (a = a * b); } @@ -1835,7 +1826,7 @@ operator*=(Matrix2& a, const Matrix2& b) // Matrix3 Operators inline bool -operator==(const Matrix3& a, const Matrix3& b) +operator==(Matrix3 const& a, Matrix3 const& b) { for (usize i = 0; i < 3; i++) { @@ -1846,25 +1837,25 @@ operator==(const Matrix3& a, const Matrix3& b) } inline bool -operator!=(const Matrix3& a, const Matrix3& b) +operator!=(Matrix3 const& a, Matrix3 const& b) { return !operator==(a, b); } inline Matrix3 -operator+(const Matrix3& a) +operator+(Matrix3 const& a) { return a; } inline Matrix3 -operator-(const Matrix3& a) +operator-(Matrix3 const& a) { return {-a.x, -a.y, -a.z}; } inline Matrix3 -operator+(const Matrix3& a, const Matrix3& b) +operator+(Matrix3 const& a, Matrix3 const& b) { Matrix3 mat; mat[0] = a[0] + b[0]; @@ -1874,7 +1865,7 @@ operator+(const Matrix3& a, const Matrix3& b) } inline Matrix3 -operator-(const Matrix3& a, const Matrix3& b) +operator-(Matrix3 const& a, Matrix3 const& b) { Matrix3 mat; mat[0] = a[0] - b[0]; @@ -1884,7 +1875,7 @@ operator-(const Matrix3& a, const Matrix3& b) } inline Matrix3 -operator*(const Matrix3& a, const Matrix3& b) +operator*(Matrix3 const& a, Matrix3 const& b) { Matrix3 result; result[0] = a[0] * b[0][0] + a[1] * b[0][1] + a[2] * b[0][2]; @@ -1894,7 +1885,7 @@ operator*(const Matrix3& a, const Matrix3& b) } inline Vector3 -operator*(const Matrix3& a, const Vector3& v) +operator*(Matrix3 const& a, Vector3 v) { return Vector3{a[0][0] * v.x + a[1][0] * v.y + a[2][0] * v.z, a[0][1] * v.x + a[1][1] * v.y + a[2][1] * v.z, @@ -1902,7 +1893,7 @@ operator*(const Matrix3& a, const Vector3& v) } inline Matrix3 -operator*(const Matrix3& a, f32 scalar) +operator*(Matrix3 const& a, f32 scalar) { Matrix3 mat; mat[0] = a[0] * scalar; @@ -1912,7 +1903,7 @@ operator*(const Matrix3& a, f32 scalar) } inline Matrix3 -operator*(f32 scalar, const Matrix3& a) +operator*(f32 scalar, Matrix3 const& a) { Matrix3 mat; mat[0] = a[0] * scalar; @@ -1922,7 +1913,7 @@ operator*(f32 scalar, const Matrix3& a) } inline Matrix3 -operator/(const Matrix3& a, f32 scalar) +operator/(Matrix3 const& a, f32 scalar) { Matrix3 mat; mat[0] = a[0] / scalar; @@ -1932,19 +1923,19 @@ operator/(const Matrix3& a, f32 scalar) } inline Matrix3& -operator+=(Matrix3& a, const Matrix3& b) +operator+=(Matrix3& a, Matrix3 const& b) { return (a = a + b); } inline Matrix3& -operator-=(Matrix3& a, const Matrix3& b) +operator-=(Matrix3& a, Matrix3 const& b) { return (a = a - b); } inline Matrix3& -operator*=(Matrix3& a, const Matrix3& b) +operator*=(Matrix3& a, Matrix3 const& b) { return (a = a * b); } @@ -1952,7 +1943,7 @@ operator*=(Matrix3& a, const Matrix3& b) // Matrix4 Operators inline bool -operator==(const Matrix4& a, const Matrix4& b) +operator==(Matrix4 const& a, Matrix4 const& b) { for (usize i = 0; i < 4; i++) { @@ -1963,25 +1954,25 @@ operator==(const Matrix4& a, const Matrix4& b) } inline bool -operator!=(const Matrix4& a, const Matrix4& b) +operator!=(Matrix4 const& a, Matrix4 const& b) { return !operator==(a, b); } inline Matrix4 -operator+(const Matrix4& a) +operator+(Matrix4 const& a) { return a; } inline Matrix4 -operator-(const Matrix4& a) +operator-(Matrix4 const& a) { return {-a.x, -a.y, -a.z, -a.w}; } inline Matrix4 -operator+(const Matrix4& a, const Matrix4& b) +operator+(Matrix4 const& a, Matrix4 const& b) { Matrix4 mat; mat[0] = a[0] + b[0]; @@ -1992,7 +1983,7 @@ operator+(const Matrix4& a, const Matrix4& b) } inline Matrix4 -operator-(const Matrix4& a, const Matrix4& b) +operator-(Matrix4 const& a, Matrix4 const& b) { Matrix4 mat; mat[0] = a[0] - b[0]; @@ -2003,7 +1994,7 @@ operator-(const Matrix4& a, const Matrix4& b) } inline Matrix4 -operator*(const Matrix4& a, const Matrix4& b) +operator*(Matrix4 const& a, Matrix4 const& b) { Matrix4 result; result[0] = a[0] * b[0][0] + a[1] * b[0][1] + a[2] * b[0][2] + a[3] * b[0][3]; @@ -2014,7 +2005,7 @@ operator*(const Matrix4& a, const Matrix4& b) } inline Vector4 -operator*(const Matrix4& a, const Vector4& v) +operator*(Matrix4 const& a, Vector4 v) { return Vector4{a[0][0] * v.x + a[1][0] * v.y + a[2][0] * v.z + a[3][0] * v.w, a[0][1] * v.x + a[1][1] * v.y + a[2][1] * v.z + a[3][1] * v.w, @@ -2023,7 +2014,7 @@ operator*(const Matrix4& a, const Vector4& v) } inline Matrix4 -operator*(const Matrix4& a, f32 scalar) +operator*(Matrix4 const& a, f32 scalar) { Matrix4 mat; mat[0] = a[0] * scalar; @@ -2034,7 +2025,7 @@ operator*(const Matrix4& a, f32 scalar) } inline Matrix4 -operator*(f32 scalar, const Matrix4& a) +operator*(f32 scalar, Matrix4 const& a) { Matrix4 mat; mat[0] = a[0] * scalar; @@ -2045,7 +2036,7 @@ operator*(f32 scalar, const Matrix4& a) } inline Matrix4 -operator/(const Matrix4& a, f32 scalar) +operator/(Matrix4 const& a, f32 scalar) { Matrix4 mat; mat[0] = a[0] / scalar; @@ -2056,19 +2047,19 @@ operator/(const Matrix4& a, f32 scalar) } inline Matrix4& -operator+=(Matrix4& a, const Matrix4& b) +operator+=(Matrix4& a, Matrix4 const& b) { return (a = a + b); } inline Matrix4& -operator-=(Matrix4& a, const Matrix4& b) +operator-=(Matrix4& a, Matrix4 const& b) { return (a = a - b); } inline Matrix4& -operator*=(Matrix4& a, const Matrix4& b) +operator*=(Matrix4& a, Matrix4 const& b) { return (a = a * b); } @@ -2162,7 +2153,7 @@ operator/=(Angle& a, f32 scalar) // Transform Operators // World = Parent * Local Transform -operator*(const Transform& ps, const Transform& ls) +operator*(Transform const& ps, Transform const& ls) { Transform ws; @@ -2175,14 +2166,14 @@ operator*(const Transform& ps, const Transform& ls) } inline Transform& -operator*=(Transform& ps, const Transform& ls) +operator*=(Transform& ps, Transform const& ls) { return (ps = ps * ls); } // Local = World / Parent Transform -operator/(const Transform& ws, const Transform& ps) +operator/(Transform const& ws, Transform const& ps) { Transform ls; @@ -2197,7 +2188,7 @@ operator/(const Transform& ws, const Transform& ps) } inline Transform& -operator/=(Transform& ws, const Transform& ps) +operator/=(Transform& ws, Transform const& ps) { return (ws = ws / ps); } @@ -2227,18 +2218,18 @@ inline f32 as_gons(Angle a) { return a.radians * (400.0f / math::TAU); } namespace math { -const f32 ZERO = 0.0f; -const f32 ONE = 1.0f; -const f32 THIRD = 0.33333333f; -const f32 TWO_THIRDS = 0.66666667f; -const f32 E = 2.718281828f; -const f32 PI = 3.141592654f; -const f32 TAU = 6.283185307f; -const f32 SQRT_2 = 1.414213562f; -const f32 SQRT_3 = 1.732050808f; -const f32 SQRT_5 = 2.236067978f; +f32 const ZERO = 0.0f; +f32 const ONE = 1.0f; +f32 const THIRD = 0.33333333f; +f32 const TWO_THIRDS = 0.66666667f; +f32 const E = 2.718281828f; +f32 const PI = 3.141592654f; +f32 const TAU = 6.283185307f; +f32 const SQRT_2 = 1.414213562f; +f32 const SQRT_3 = 1.732050808f; +f32 const SQRT_5 = 2.236067978f; -const f32 F32_PRECISION = 1.0e-7f; +f32 const F32_PRECISION = 1.0e-7f; // Power inline f32 sqrt(f32 x) { return ::sqrtf(x); } @@ -2430,25 +2421,25 @@ equals(f32 a, f32 b, f32 precision) // Vector2 functions inline f32 -dot(const Vector2& a, const Vector2& b) +dot(Vector2 a, Vector2 b) { return a.x * b.x + a.y * b.y; } inline f32 -cross(const Vector2& a, const Vector2& b) +cross(Vector2 a, Vector2 b) { return a.x * b.y - a.y * b.x; } inline f32 -magnitude(const Vector2& a) +magnitude(Vector2 a) { return math::sqrt(math::dot(a, a)); } inline Vector2 -normalize(const Vector2& a) +normalize(Vector2 a) { f32 m = magnitude(a); if (m > 0) @@ -2457,20 +2448,20 @@ normalize(const Vector2& a) } inline Vector2 -hadamard(const Vector2& a, const Vector2& b) +hadamard(Vector2 a, Vector2 b) { return {a.x * b.x, a.y * b.y}; } inline f32 -aspect_ratio(const Vector2& a) +aspect_ratio(Vector2 a) { return a.x / a.y; } inline Matrix4 -matrix2_to_matrix4(const Matrix2& m) +matrix2_to_matrix4(Matrix2 m) { Matrix4 result = MATRIX4_IDENTITY; result[0][0] = m[0][0]; @@ -2482,13 +2473,13 @@ matrix2_to_matrix4(const Matrix2& m) // Vector3 functions inline f32 -dot(const Vector3& a, const Vector3& b) +dot(Vector3 a, Vector3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } inline Vector3 -cross(const Vector3& a, const Vector3& b) +cross(Vector3 a, Vector3 b) { return Vector3{ a.y * b.z - b.y * a.z, // x @@ -2498,13 +2489,13 @@ cross(const Vector3& a, const Vector3& b) } inline f32 -magnitude(const Vector3& a) +magnitude(Vector3 a) { return math::sqrt(math::dot(a, a)); } inline Vector3 -normalize(const Vector3& a) +normalize(Vector3 a) { f32 m = magnitude(a); if (m > 0) @@ -2513,13 +2504,13 @@ normalize(const Vector3& a) } inline Vector3 -hadamard(const Vector3& a, const Vector3& b) +hadamard(Vector3 a, Vector3 b) { return {a.x * b.x, a.y * b.y, a.z * b.z}; } inline Matrix4 -matrix3_to_matrix4(const Matrix3& m) +matrix3_to_matrix4(Matrix3 const& m) { Matrix4 result = MATRIX4_IDENTITY; result[0][0] = m[0][0]; @@ -2536,19 +2527,19 @@ matrix3_to_matrix4(const Matrix3& m) // Vector4 functions inline f32 -dot(const Vector4& a, const Vector4& b) +dot(Vector4 a, Vector4 b) { return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w; } inline f32 -magnitude(const Vector4& a) +magnitude(Vector4 a) { return math::sqrt(math::dot(a, a)); } inline Vector4 -normalize(const Vector4& a) +normalize(Vector4 a) { f32 m = magnitude(a); if (m > 0) @@ -2557,32 +2548,32 @@ normalize(const Vector4& a) } inline Vector4 -hadamard(const Vector4& a, const Vector4& b) +hadamard(Vector4 a, Vector4 b) { return {a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w}; } // Complex Functions inline f32 -dot(const Complex& a, const Complex& b) +dot(Complex a, Complex b) { return a.real * b.real + a.imag * b.imag; } inline f32 -magnitude(const Complex& a) +magnitude(Complex a) { return math::sqrt(norm(a)); } inline f32 -norm(const Complex& a) +norm(Complex a) { return math::dot(a, a); } inline Complex -normalize(const Complex& a) +normalize(Complex a) { f32 m = magnitude(a); if (m > 0) @@ -2591,13 +2582,13 @@ normalize(const Complex& a) } inline Complex -conjugate(const Complex& a) +conjugate(Complex a) { return {a.real, -a.imag}; } inline Complex -inverse(const Complex& a) +inverse(Complex a) { f32 m = norm(a); if (m > 0) @@ -2606,9 +2597,9 @@ inverse(const Complex& a) } inline f32 -complex_angle(const Complex& a) +complex_angle(Complex a) { - return atan2(a.imag, a.real); + return atan2f(a.imag, a.real); } inline Complex @@ -2621,13 +2612,13 @@ magnitude_angle(f32 magnitude, Angle a) // Quaternion functions inline f32 -dot(const Quaternion& a, const Quaternion& b) +dot(Quaternion a, Quaternion b) { return math::dot(a.xyz, b.xyz) + a.w*b.w; } inline Quaternion -cross(const Quaternion& a, const Quaternion& b) +cross(Quaternion a, Quaternion b) { return Quaternion{a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y, a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z, @@ -2636,19 +2627,19 @@ cross(const Quaternion& a, const Quaternion& b) } inline f32 -magnitude(const Quaternion& a) +magnitude(Quaternion a) { return math::sqrt(math::dot(a, a)); } inline f32 -norm(const Quaternion& a) +norm(Quaternion a) { return math::dot(a, a); } inline Quaternion -normalize(const Quaternion& a) +normalize(Quaternion a) { f32 m = magnitude(a); if (m > 0) @@ -2657,26 +2648,26 @@ normalize(const Quaternion& a) } inline Quaternion -conjugate(const Quaternion& a) +conjugate(Quaternion a) { return {-a.x, -a.y, -a.z, a.w}; } inline Quaternion -inverse(const Quaternion& a) +inverse(Quaternion a) { f32 m = 1.0f / dot(a, a); return math::conjugate(a) * m; } inline Angle -quaternion_angle(const Quaternion& a) +quaternion_angle(Quaternion a) { return 2.0f * math::arccos(a.w); } inline Vector3 -quaternion_axis(const Quaternion& a) +quaternion_axis(Quaternion a) { f32 s2 = 1.0f - a.w * a.w; @@ -2689,7 +2680,7 @@ quaternion_axis(const Quaternion& a) } inline Quaternion -axis_angle(const Vector3& axis, Angle angle) +axis_angle(Vector3 axis, Angle angle) { Vector3 a = math::normalize(axis); f32 s = math::sin(0.5f * angle); @@ -2702,37 +2693,37 @@ axis_angle(const Vector3& axis, Angle angle) } inline Angle -quaternion_roll(const Quaternion& a) +quaternion_roll(Quaternion a) { return math::arctan2(2.0f * a.x * a.y + a.z * a.w, a.x * a.x + a.w * a.w - a.y * a.y - a.z * a.z); } inline Angle -quaternion_pitch(const Quaternion& a) +quaternion_pitch(Quaternion a) { return math::arctan2(2.0f * a.y * a.z + a.w * a.x, a.w * a.w - a.x * a.x - a.y * a.y + a.z * a.z); } inline Angle -quaternion_yaw(const Quaternion& a) +quaternion_yaw(Quaternion a) { return math::arcsin(-2.0f * (a.x * a.z - a.w * a.y)); } inline Euler_Angles -quaternion_to_euler_angles(const Quaternion& a) +quaternion_to_euler_angles(Quaternion a) { return {quaternion_pitch(a), quaternion_yaw(a), quaternion_roll(a)}; } inline Quaternion -euler_angles_to_quaternion(const Euler_Angles& e, - const Vector3& x_axis, - const Vector3& y_axis, - const Vector3& z_axis) +euler_angles_to_quaternion(Euler_Angles const& e, + Vector3 x_axis, + Vector3 y_axis, + Vector3 z_axis) { Quaternion p = axis_angle(x_axis, e.pitch); Quaternion y = axis_angle(y_axis, e.yaw); @@ -2744,7 +2735,7 @@ euler_angles_to_quaternion(const Euler_Angles& e, // Spherical Linear Interpolation inline Quaternion -slerp(const Quaternion& x, const Quaternion& y, f32 t) +slerp(Quaternion x, Quaternion y, f32 t) { Quaternion z = y; @@ -2773,10 +2764,10 @@ slerp(const Quaternion& x, const Quaternion& y, f32 t) // Shoemake's Quaternion Curves // Sqherical Cubic Interpolation inline Quaternion -squad(const Quaternion& p, - const Quaternion& a, - const Quaternion& b, - const Quaternion& q, +squad(Quaternion p, + Quaternion a, + Quaternion b, + Quaternion q, f32 t) { return slerp(slerp(p, q, t), slerp(a, b, t), 2.0f * t * (1.0f - t)); @@ -2784,7 +2775,7 @@ squad(const Quaternion& p, // Matrix2 functions inline Matrix2 -transpose(const Matrix2& m) +transpose(Matrix2 m) { Matrix2 result; for (usize i = 0; i < 2; i++) @@ -2796,13 +2787,13 @@ transpose(const Matrix2& m) } inline f32 -determinant(const Matrix2& m) +determinant(Matrix2 m) { return m[0][0] * m[1][1] - m[1][0] * m[0][1]; } inline Matrix2 -inverse(const Matrix2& m) +inverse(Matrix2 m) { f32 inv_det = 1.0f / (m[0][0] * m[1][1] - m[1][0] * m[0][1]); Matrix2 result; @@ -2814,7 +2805,7 @@ inverse(const Matrix2& m) } inline Matrix2 -hadamard(const Matrix2& a, const Matrix2&b) +hadamard(Matrix2 a, const Matrix2&b) { Matrix2 result; result[0] = a[0] * b[0]; @@ -2824,7 +2815,7 @@ hadamard(const Matrix2& a, const Matrix2&b) // Matrix3 functions inline Matrix3 -transpose(const Matrix3& m) +transpose(Matrix3 const& m) { Matrix3 result; @@ -2837,7 +2828,7 @@ transpose(const Matrix3& m) } inline f32 -determinant(const Matrix3& m) +determinant(Matrix3 const& m) { return (+m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) -m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2]) @@ -2845,7 +2836,7 @@ determinant(const Matrix3& m) } inline Matrix3 -inverse(const Matrix3& m) +inverse(Matrix3 const& m) { f32 inv_det = 1.0f / ( + m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) @@ -2868,7 +2859,7 @@ inverse(const Matrix3& m) } inline Matrix3 -hadamard(const Matrix3& a, const Matrix3&b) +hadamard(Matrix3 const& a, const Matrix3&b) { Matrix3 result; result[0] = a[0] * b[0]; @@ -2879,7 +2870,7 @@ hadamard(const Matrix3& a, const Matrix3&b) // Matrix4 functions inline Matrix4 -transpose(const Matrix4& m) +transpose(Matrix4 const& m) { Matrix4 result; @@ -2892,7 +2883,7 @@ transpose(const Matrix4& m) } f32 -determinant(const Matrix4& m) +determinant(Matrix4 const& m) { f32 coef00 = m[2][2] * m[3][3] - m[3][2] * m[2][3]; f32 coef02 = m[1][2] * m[3][3] - m[3][2] * m[1][3]; @@ -2947,7 +2938,7 @@ determinant(const Matrix4& m) } Matrix4 -inverse(const Matrix4& m) +inverse(Matrix4 const& m) { f32 coef00 = m[2][2] * m[3][3] - m[3][2] * m[2][3]; f32 coef02 = m[1][2] * m[3][3] - m[3][2] * m[1][3]; @@ -3000,7 +2991,7 @@ inverse(const Matrix4& m) } inline Matrix4 -hadamard(const Matrix4& a, const Matrix4& b) +hadamard(Matrix4 const& a, Matrix4 const& b) { Matrix4 result; @@ -3013,7 +3004,7 @@ hadamard(const Matrix4& a, const Matrix4& b) } inline bool -is_affine(const Matrix4& m) +is_affine(Matrix4 const& m) { // E.g. No translation return (equals(m.columns[3].x, 0)) & @@ -3024,7 +3015,7 @@ is_affine(const Matrix4& m) inline Matrix4 -quaternion_to_matrix4(const Quaternion& q) +quaternion_to_matrix4(Quaternion q) { Matrix4 mat = MATRIX4_IDENTITY; @@ -3056,7 +3047,7 @@ quaternion_to_matrix4(const Quaternion& q) } Quaternion -matrix4_to_quaternion(const Matrix4& m) +matrix4_to_quaternion(Matrix4 const& m) { f32 four_x_squared_minus_1 = m[0][0] - m[1][1] - m[2][2]; f32 four_y_squared_minus_1 = m[1][1] - m[0][0] - m[2][2]; @@ -3132,7 +3123,7 @@ matrix4_to_quaternion(const Matrix4& m) inline Matrix4 -translate(const Vector3& v) +translate(Vector3 v) { Matrix4 result = MATRIX4_IDENTITY; result[3].xyz = v; @@ -3141,7 +3132,7 @@ translate(const Vector3& v) } inline Matrix4 -rotate(const Vector3& v, Angle angle) +rotate(Vector3 v, Angle angle) { const f32 c = math::cos(angle); const f32 s = math::sin(angle); @@ -3170,7 +3161,7 @@ rotate(const Vector3& v, Angle angle) } inline Matrix4 -scale(const Vector3& v) +scale(Vector3 v) { return { v.x, 0, 0, 0, 0, v.y, 0, 0, @@ -3239,7 +3230,7 @@ infinite_perspective(Angle fovy, f32 aspect, f32 z_near) inline Matrix4 -look_at_matrix4(const Vector3& eye, const Vector3& center, const Vector3& up) +look_at_matrix4(Vector3 eye, Vector3 center, Vector3 up) { const Vector3 f = math::normalize(center - eye); const Vector3 s = math::normalize(math::cross(f, up)); @@ -3268,7 +3259,7 @@ look_at_matrix4(const Vector3& eye, const Vector3& center, const Vector3& up) inline Quaternion -look_at_quaternion(const Vector3& eye, const Vector3& center, const Vector3& up) +look_at_quaternion(Vector3 eye, Vector3 center, Vector3 up) { if (math::equals(math::magnitude(center - eye), 0, 0.001f)) return QUATERNION_IDENTITY; // You cannot look at where you are! @@ -3305,13 +3296,13 @@ look_at_quaternion(const Vector3& eye, const Vector3& center, const Vector3& up) // Transform Functions inline Vector3 -transform_point(const Transform& transform, const Vector3& point) +transform_point(Transform const& transform, Vector3 point) { return (math::conjugate(transform.orientation) * (transform.position - point)) / transform.scale; } inline Transform -inverse(const Transform& t) +inverse(Transform const& t) { const Quaternion inv_orientation = math::conjugate(t.orientation); @@ -3326,7 +3317,7 @@ inverse(const Transform& t) } inline Matrix4 -transform_to_matrix4(const Transform& t) +transform_to_matrix4(Transform const& t) { return math::translate(t.position) * math::quaternion_to_matrix4(t.orientation) * @@ -3338,7 +3329,7 @@ transform_to_matrix4(const Transform& t) namespace aabb { inline Aabb -calculate(const void* vertices, usize num_vertices, usize stride, usize offset) +calculate(void const* vertices, usize num_vertices, usize stride, usize offset) { Vector3 min; Vector3 max; @@ -3373,7 +3364,7 @@ calculate(const void* vertices, usize num_vertices, usize stride, usize offset) } inline f32 -surface_area(const Aabb& aabb) +surface_area(Aabb const& aabb) { Vector3 h = aabb.half_size * 2.0f; f32 s = 0.0f; @@ -3385,14 +3376,14 @@ surface_area(const Aabb& aabb) } inline f32 -volume(const Aabb& aabb) +volume(Aabb const& aabb) { Vector3 h = aabb.half_size * 2.0f; return h.x * h.y * h.z; } inline Sphere -to_sphere(const Aabb& aabb) +to_sphere(Aabb const& aabb) { Sphere s; s.center = aabb.center; @@ -3402,7 +3393,7 @@ to_sphere(const Aabb& aabb) inline bool -contains(const Aabb& aabb, const Vector3& point) +contains(Aabb const& aabb, Vector3 point) { Vector3 distance = aabb.center - point; @@ -3413,7 +3404,7 @@ contains(const Aabb& aabb, const Vector3& point) } inline bool -contains(const Aabb& a, const Aabb& b) +contains(Aabb const& a, Aabb const& b) { Vector3 dist = a.center - b.center; @@ -3425,7 +3416,7 @@ contains(const Aabb& a, const Aabb& b) inline bool -intersects(const Aabb& a, const Aabb& b) +intersects(Aabb const& a, Aabb const& b) { Vector3 dist = a.center - b.center; Vector3 sum_half_sizes = a.half_size + b.half_size; @@ -3437,7 +3428,7 @@ intersects(const Aabb& a, const Aabb& b) } inline Aabb -transform_affine(const Aabb& aabb, const Matrix4& m) +transform_affine(Aabb const& aabb, Matrix4 const& m) { GB_ASSERT(math::is_affine(m), "Passed Matrix4 must be an affine matrix"); @@ -3464,7 +3455,7 @@ transform_affine(const Aabb& aabb, const Matrix4& m) namespace sphere { Sphere -calculate_min_bounding(const void* vertices, usize num_vertices, usize stride, usize offset, f32 step) +calculate_min_bounding(void const* vertices, usize num_vertices, usize stride, usize offset, f32 step) { auto gen = random::make(0); @@ -3515,7 +3506,7 @@ calculate_min_bounding(const void* vertices, usize num_vertices, usize stride, u } Sphere -calculate_max_bounding(const void* vertices, usize num_vertices, usize stride, usize offset) +calculate_max_bounding(void const* vertices, usize num_vertices, usize stride, usize offset) { Aabb aabb = aabb::calculate(vertices, num_vertices, stride, offset); @@ -3543,19 +3534,19 @@ calculate_max_bounding(const void* vertices, usize num_vertices, usize stride, u } inline f32 -surface_area(const Sphere& s) +surface_area(Sphere s) { return 2.0f * math::TAU * s.radius * s.radius; } inline f32 -volume(const Sphere& s) +volume(Sphere s) { return math::TWO_THIRDS * math::TAU * s.radius * s.radius * s.radius; } inline Aabb -to_aabb(const Sphere& s) +to_aabb(Sphere s) { Aabb a; a.center = s.center; @@ -3566,7 +3557,7 @@ to_aabb(const Sphere& s) } inline bool -contains_point(const Sphere& s, const Vector3& point) +contains_point(Sphere s, Vector3 point) { Vector3 dr = point - s.center; f32 distance = math::dot(dr, dr); @@ -3574,7 +3565,7 @@ contains_point(const Sphere& s, const Vector3& point) } inline f32 -ray_intersection(const Vector3& from, const Vector3& dir, const Sphere& s) +ray_intersection(Vector3 from, Vector3 dir, Sphere s) { Vector3 v = s.center - from; f32 b = math::dot(v, dir); @@ -3589,7 +3580,7 @@ ray_intersection(const Vector3& from, const Vector3& dir, const Sphere& s) namespace plane { inline f32 -ray_intersection(const Vector3& from, const Vector3& dir, const Plane& p) +ray_intersection(Vector3 from, Vector3 dir, Plane p) { f32 nd = math::dot(dir, p.normal); f32 orpn = math::dot(from, p.normal); @@ -3602,20 +3593,16 @@ ray_intersection(const Vector3& from, const Vector3& dir, const Plane& p) } inline bool -intersection3(const Plane& p1, const Plane& p2, const Plane& p3, Vector3* ip) +intersection3(Plane p1, Plane p2, Plane p3, Vector3* ip) { - const Vector3& n1 = p1.normal; - const Vector3& n2 = p2.normal; - const Vector3& n3 = p3.normal; - - f32 den = -math::dot(math::cross(n1, n2), n3); + f32 den = -math::dot(math::cross(p1.normal, p2.normal), p3.normal); if (math::equals(den, 0.0f)) return false; - Vector3 res = p1.distance * math::cross(n2, n3) - + p2.distance * math::cross(n3, n1) - + p3.distance * math::cross(n1, n2); + Vector3 res = p1.distance * math::cross(p2.normal, p3.normal) + + p2.distance * math::cross(p3.normal, p1.normal) + + p3.distance * math::cross(p1.normal, p2.normal); *ip = res / den; return true;