From 09ca6059317fb86b840b348d387e49ab33b9b24e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 3 May 2016 11:55:52 +0100 Subject: [PATCH] Fix alignment in gb_heap_allocator_proc --- README.md | 4 +- gb.h | 236 ++++++++++++++++++++++++++++++++++++++++++++++++------ gb_math.h | 161 +++++++++++++++++++------------------ 3 files changed, 293 insertions(+), 108 deletions(-) diff --git a/README.md b/README.md index 49b5487..7c3db15 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ gb single-file public domain libraries for C & C++ library | latest version | category | languages | description ----------------|----------------|----------|-----------|------------- -**gb.h** | 0.07 | misc | C, C++ | A C helper library for C & C++ -**gb_math.h** | 0.06a | math | C, C++ | A C/C++ vector math library geared towards game development +**gb.h** | 0.07a | misc | C, C++ | A C helper library for C & C++ +**gb_math.h** | 0.06b | math | C, C++ | A C/C++ vector math library geared towards game development **gb_gl.h** | 0.04a | graphics | C, C++ | A C/C++ OpenGL Helper Library **gb_string.h** | 0.95 | strings | C, C++ | A better string library for C & C++ (this is built into gb.h too with custom allocator support!) **gb_ini.h** | 0.93 | misc | C, C++ | A simple ini file loader library for C & C++ diff --git a/gb.h b/gb.h index 4e1fd5b..68fa9ee 100644 --- a/gb.h +++ b/gb.h @@ -1,4 +1,4 @@ -/* gb.h - v0.07 - Ginger Bill's C Helper Library - public domain +/* gb.h - v0.07a - Ginger Bill's C Helper Library - public domain - no warranty implied; use at your own risk This is a single header file with a bunch of useful stuff @@ -26,6 +26,7 @@ Conventions used: Version History: + 0.07a - Fix alignment in gb_heap_allocator_proc 0.07 - Hash Table and Hashing Functions 0.06c - Better Documentation 0.06b - OS X Support @@ -174,8 +175,10 @@ extern "C" { #include #include -#include /* TODO(bill): Remove and replace with OS Specific stuff */ -#include /* NOTE(bill): For memcpy, memmove, memcmp, etc. */ +#include /* TODO(bill): Remove and replace with OS Specific stuff */ +#include +#include /* NOTE(bill): For memcpy, memmove, memcmp, etc. */ +#include /* NOTE(bill): File info */ #if defined(GB_SYSTEM_WINDOWS) #define NOMINMAX 1 @@ -183,20 +186,19 @@ extern "C" { #define WIN32_MEAN_AND_LEAN 1 #define VC_EXTRALEAN 1 #include + #include #include + #include #else - #include - #include - #include - #include #include + #include + #include + #include + #include + #include #endif -#if !defined(GB_NO_STDLIB) -#include -#endif - #ifndef gb_malloc #define gb_malloc(sz) malloc(sz) #endif @@ -712,6 +714,7 @@ gb_init_mutex(&m); } #endif +/* TODO(bill): Should I create a Condition Type? */ typedef struct gbSemaphore { @@ -746,8 +749,8 @@ typedef struct gbThread { void *data; gbSemaphore semaphore; - isize stack_size; - b32 is_running; + isize stack_size; + b32 is_running; } gbThread; GB_DEF void gb_init_thread (gbThread *t); @@ -757,6 +760,7 @@ GB_DEF void gb_start_thread_with_stack(gbThread *t, gbThreadProc *proc, void *da GB_DEF void gb_join_thread (gbThread *t); GB_DEF b32 gb_is_thread_running (gbThread const *t); GB_DEF u32 gb_current_thread_id (void); +GB_DEF void gb_set_thread_name (gbThread *t, char const *name); /*************************************************************** * @@ -839,7 +843,7 @@ GB_DEF void *gb_alloc_copy_align(gbAllocator a, void const *src, isize size, isi GB_DEF char *gb_alloc_cstring(gbAllocator a, char const *str); -/* NOTE(bill): These are very useful and the type case has saved me from numerous bugs */ +/* NOTE(bill): These are very useful and the type cast has saved me from numerous bugs */ #ifndef gb_alloc_struct #define gb_alloc_struct(allocator, Type) (Type *)gb_alloc_align(allocator, gb_size_of(Type)) #define gb_alloc_array(allocator, Type, count) (Type *)gb_alloc(allocator, gb_size_of(Type) * (count)) @@ -1404,9 +1408,17 @@ GB_DEF gbHashTableEntry const *gb_find_next_hash_table_entry (gbHashTable const */ typedef u64 gbFileTime; +typedef enum gbFileAccess { + GB_FILE_ACCESS_READ = 1, + GB_FILE_ACCESS_WRITE = 2 +} gbFileAccess; + typedef enum gbFileType { - GB_FILE_TYPE_READ = 1, - GB_FILE_TYPE_WRITE = 2 + GB_FILE_TYPE_UNKNOWN, + GB_FILE_TYPE_FILE, + GB_FILE_TYPE_DIRECTORY, + + GB_FILE_TYPE_COUNT } gbFileType; typedef struct gbFile { @@ -1414,6 +1426,7 @@ typedef struct gbFile { char *path; i64 size; b32 is_open; + gbFileAccess access; gbFileType type; gbFileTime last_write_time; } gbFile; @@ -1509,6 +1522,16 @@ GB_DEF void gb_get_local_date (gbDate *date); * */ +GB_DEF void gb_yield(void); +GB_DEF void gb_set_env(char const *name, char const *value); +GB_DEF void gb_unset_env(char const *name); +GB_DEF i32 gb_chdir(char const *path); +GB_DEF void gb_get_working_cmd(char *buffer, isize len); + +GB_DEF u16 gb_endian_swap16(u16 i); +GB_DEF u32 gb_endian_swap32(u32 i); +GB_DEF u64 gb_endian_swap64(u64 i); + @@ -2461,6 +2484,40 @@ gb_current_thread_id(void) +void +gb_set_thread_name(gbThread *t, char const *name) +{ +#if defined(_MSC_VER) + /* TODO(bill): Bloody Windows!!! */ + #pragma pack(push, 8) + struct gbprivThreadName { + DWORD type; + LPCSTR name; + DWORD id; + DWORD flags; + }; + #pragma pack(pop) + struct gbprivThreadName tn; + tn.type = 0x1000; + tn.name = name; + tn.id = GetThreadId(t->win32_handle); + tn.flags = 0; + + __try { + RaiseException(0x406d1388, 0, gb_size_of(tn)/4, cast(ULONG_PTR *)&tn); + } __except(EXCEPTION_EXECUTE_HANDLER) { + } + +#elif defined(GB_SYSTEM_OSX) + /* TODO(bill): Test if this works */ + pthread_setname_np(name); +#else + /* TODO(bill): Test if this works */ + pthread_setname_np(t->posix_handle, name); +#endif +} + + @@ -2476,25 +2533,34 @@ gb_heap_allocator(void) GB_ALLOCATOR_PROC(gb_heap_allocator_proc) { +/* TODO(bill): Throughly test! */ switch (type) { case GB_ALLOCATION_ALLOC: { - void *ptr; - isize actual_size = size + alignment; - ptr = gb_align_forward(gb_malloc(actual_size), alignment); - - return ptr; +#if defined(_MSC_VER) + return _aligned_malloc(size, alignment); +#else + return aligned_alloc(alignment, size); +#endif } break; case GB_ALLOCATION_FREE: { - gb_mfree(old_memory); +#if defined(_MSC_VER) + _aligned_free(old_memory); +#else + free(old_memory); +#endif } break; case GB_ALLOCATION_FREE_ALL: break; case GB_ALLOCATION_RESIZE: { +#if defined(_MSC_VER) + return _aligned_realloc(old_memory, size, alignment); +#else gbAllocator a = gb_heap_allocator(); return gb_default_resize_align(a, old_memory, old_size, size, alignment); +#endif } break; } @@ -4337,7 +4403,7 @@ gb_create_file(gbFile *file, char const *filepath, ...) file->path = gb_alloc_cstring(gb_heap_allocator(), path); file->size = gb_file_size(file); file->is_open = true; - file->type = GB_FILE_TYPE_WRITE; + file->access = GB_FILE_ACCESS_WRITE; file->last_write_time = gb_file_last_write_time(file->path); return true; } @@ -4362,7 +4428,29 @@ gb_open_file(gbFile *file, char const *filepath, ...) file->path = gb_alloc_cstring(gb_heap_allocator(), path); file->size = gb_file_size(file); file->is_open = true; - file->type = GB_FILE_TYPE_READ; + file->access = GB_FILE_ACCESS_READ; + file->type = GB_FILE_TYPE_UNKNOWN; + #if defined(_MSC_VER) + { + struct _stat64 st; + if (_stat64(path, &st) == 0) { + if ((st.st_mode & _S_IFREG) != 0) + file->type = GB_FILE_TYPE_FILE; + else if ((st.st_mode & _S_IFDIR) != 0) + file->type = GB_FILE_TYPE_DIRECTORY; + } + } + #else + { + struct stat64 st; + if (stat64(path, &st) == 0) { + if ((st.st_mode & S_IFREG) != 0) + file->type = GB_FILE_TYPE_FILE; + else if ((st.st_mode & S_IFDIR) != 0) + file->type = GB_FILE_TYPE_DIRECTORY; + } + } + #endif file->last_write_time = gb_file_last_write_time(file->path); return true; } @@ -4387,7 +4475,7 @@ gb_file_read_at(gbFile *file, void *buffer, isize size, i64 offset) { i64 prev_cursor_pos; - GB_ASSERT(file->type == GB_FILE_TYPE_READ); + GB_ASSERT(file->access == GB_FILE_ACCESS_READ); prev_cursor_pos = ftell(cast(FILE *)file->handle); fseek(cast(FILE *)file->handle, offset, SEEK_SET); @@ -4402,7 +4490,7 @@ gb_file_write_at(gbFile *file, void const *buffer, isize size, i64 offset) isize written_size; i64 prev_cursor_pos; - GB_ASSERT(file->type == GB_FILE_TYPE_WRITE); + GB_ASSERT(file->access == GB_FILE_ACCESS_WRITE); prev_cursor_pos = ftell(cast(FILE *)file->handle); fseek(cast(FILE *)file->handle, offset, SEEK_SET); @@ -4788,6 +4876,102 @@ gb_inline gbDllProc gb_dll_proc_address(gbDllHandle dll, char const *proc_name) #endif + + +/*************************************************************** + * + * Miscellany + * + */ + +gb_inline void +gb_yield(void) +{ +#if defined(GB_SYSTEM_WINDOWS) + Sleep(0); +#else + sched_yield(); +#endif +} + +gb_inline void +gb_set_env(char const *name, char const *value) +{ +#if defined(GB_SYSTEM_WINDOWS) + SetEnvironmentVariableA(name, value); +#else + setenv(name, value, 1); +#endif +} + +gb_inline void +gb_unset_env(char const *name) +{ +#if defined(GB_SYSTEM_WINDOWS) + SetEnvironmentVariableA(name, NULL); +#else + unsetenv(name); +#endif +} + +gb_inline i32 +gb_chdir(char const *path) +{ +#if defined(_MSC_VER) + return _chdir(path); +#else + return chdir(path); +#endif +} + +gb_inline void +gb_get_working_cmd(char *buffer, isize len) +{ +#if defined(_MSC_VER) + _getcwd(buffer, cast(int)len); +#else + getcwd(buffer, len); +#endif +} + +gb_inline u16 +gb_endian_swap16(u16 i) +{ + return (i>>8) | (i<<8); +} + +gb_inline u32 +gb_endian_swap32(u32 i) +{ + return (i>>24) |(i<<24) | + ((i&0x00ff0000)>>8) | ((i&0x00ff0000)<<8); +} + +gb_inline u64 +gb_endian_swap64(u64 i) +{ + /* TODO(bill): Do I really need the cast here? */ + return (i>>56) | (i<<56) | + ((i&cast(u64)0x00ff000000000000)>>40) | ((i&cast(u64)0x000000000000ff00)<<40) | + ((i&cast(u64)0x0000ff0000000000)>>24) | ((i&cast(u64)0x0000000000ff0000)<<24) | + ((i&cast(u64)0x000000ff00000000)>>8) | ((i&cast(u64)0x00000000ff000000)<<8); +} + + + + + + + +/*************************************************************** + * + * Colour Type + * It's quite useful + */ + + + + #if !defined(GB_NO_COLOUR_TYPE) gb_inline gbColour gb_colour(f32 r, f32 g, f32 b, f32 a) diff --git a/gb_math.h b/gb_math.h index c8f4f02..9976a0d 100644 --- a/gb_math.h +++ b/gb_math.h @@ -1,8 +1,9 @@ -/* gb_math.h - v0.06a - public domain C math library - no warranty implied; use at your own risk +/* gb_math.h - v0.06b - public domain C math library - no warranty implied; use at your own risk A C math library geared towards game development use '#define GB_MATH_IMPLEMENTATION' before including to create the implementation in _ONE_ file Version History: + 0.06b - Just formatting 0.06a - Implement rough versions of mod, remainder, copy_sign 0.06 - Windows GCC Support and C90-ish Support 0.05 - Less/no dependencies or CRT @@ -205,50 +206,49 @@ GB_MATH_DEF float gb_angle_diff(float radians_a, float radians_b); #define gb_max(a, b) ((a) > (b) ? (a) : (b)) #endif -GB_MATH_DEF float gb_copy_sign(float x, float y); -GB_MATH_DEF float gb_remainder(float x, float y); -GB_MATH_DEF float gb_mod(float x, float y); -GB_MATH_DEF float gb_sqrt(float a); -GB_MATH_DEF float gb_rsqrt(float a); +GB_MATH_DEF float gb_copy_sign (float x, float y); +GB_MATH_DEF float gb_remainder (float x, float y); +GB_MATH_DEF float gb_mod (float x, float y); +GB_MATH_DEF float gb_sqrt (float a); +GB_MATH_DEF float gb_rsqrt (float a); GB_MATH_DEF float gb_quake_rsqrt(float a); /* NOTE(bill): It's probably better to use 1.0f/gb_sqrt(a) * And for simd, there is usually isqrt functions too! */ -GB_MATH_DEF float gb_sin(float radians); -GB_MATH_DEF float gb_cos(float radians); -GB_MATH_DEF float gb_tan(float radians); -GB_MATH_DEF float gb_arcsin(float a); -GB_MATH_DEF float gb_arccos(float a); -GB_MATH_DEF float gb_arctan(float a); +GB_MATH_DEF float gb_sin (float radians); +GB_MATH_DEF float gb_cos (float radians); +GB_MATH_DEF float gb_tan (float radians); +GB_MATH_DEF float gb_arcsin (float a); +GB_MATH_DEF float gb_arccos (float a); +GB_MATH_DEF float gb_arctan (float a); GB_MATH_DEF float gb_arctan2(float y, float x); - -GB_MATH_DEF float gb_exp(float x); -GB_MATH_DEF float gb_exp2(float x); -GB_MATH_DEF float gb_log(float x); -GB_MATH_DEF float gb_log2(float x); -GB_MATH_DEF float gb_fast_exp(float x); /* NOTE(bill): Only valid from -1 <= x <= +1 */ +GB_MATH_DEF float gb_exp (float x); +GB_MATH_DEF float gb_exp2 (float x); +GB_MATH_DEF float gb_log (float x); +GB_MATH_DEF float gb_log2 (float x); +GB_MATH_DEF float gb_fast_exp (float x); /* NOTE(bill): Only valid from -1 <= x <= +1 */ GB_MATH_DEF float gb_fast_exp2(float x); /* NOTE(bill): Only valid from -1 <= x <= +1 */ -GB_MATH_DEF float gb_pow(float x, float y); /* x^y */ +GB_MATH_DEF float gb_pow (float x, float y); /* x^y */ GB_MATH_DEF float gb_round(float x); GB_MATH_DEF float gb_floor(float x); -GB_MATH_DEF float gb_ceil(float x); +GB_MATH_DEF float gb_ceil (float x); GB_MATH_DEF float gb_half_to_float(gbHalf value); GB_MATH_DEF gbHalf gb_float_to_half(float value); GB_MATH_DEF gbVec2 gb_vec2_zero(void); -GB_MATH_DEF gbVec2 gb_vec2(float x, float y); -GB_MATH_DEF gbVec2 gb_vec2v(float x[2]); +GB_MATH_DEF gbVec2 gb_vec2 (float x, float y); +GB_MATH_DEF gbVec2 gb_vec2v (float x[2]); GB_MATH_DEF gbVec3 gb_vec3_zero(void); -GB_MATH_DEF gbVec3 gb_vec3(float x, float y, float z); -GB_MATH_DEF gbVec3 gb_vec3v(float x[3]); +GB_MATH_DEF gbVec3 gb_vec3 (float x, float y, float z); +GB_MATH_DEF gbVec3 gb_vec3v (float x[3]); GB_MATH_DEF gbVec4 gb_vec4_zero(void); -GB_MATH_DEF gbVec4 gb_vec4(float x, float y, float z, float w); -GB_MATH_DEF gbVec4 gb_vec4v(float x[4]); +GB_MATH_DEF gbVec4 gb_vec4 (float x, float y, float z, float w); +GB_MATH_DEF gbVec4 gb_vec4v (float x[4]); GB_MATH_DEF void gb_vec2_add(gbVec2 *d, gbVec2 v0, gbVec2 v1); @@ -312,12 +312,12 @@ GB_MATH_DEF void gb_vec3_refract(gbVec3 *d, gbVec3 i, gbVec3 n, float eta); GB_MATH_DEF float gb_vec2_aspect_ratio(gbVec2 v); -GB_MATH_DEF void gb_mat2_identity(gbMat2 *m); +GB_MATH_DEF void gb_mat2_identity (gbMat2 *m); GB_MATH_DEF void gb_float22_identity(float m[2][2]); GB_MATH_DEF void gb_mat2_transpose(gbMat2 *m); -GB_MATH_DEF void gb_mat2_mul(gbMat2 *out, gbMat2 *m1, gbMat2 *m2); -GB_MATH_DEF void gb_mat2_mul_vec2(gbVec2 *out, gbMat2 *m, gbVec2 in); +GB_MATH_DEF void gb_mat2_mul (gbMat2 *out, gbMat2 *m1, gbMat2 *m2); +GB_MATH_DEF void gb_mat2_mul_vec2 (gbVec2 *out, gbMat2 *m, gbVec2 in); GB_MATH_DEF gbMat2 *gb_mat2_v(gbVec2 m[2]); GB_MATH_DEF gbMat2 *gb_mat2_f(float m[2][2]); @@ -326,64 +326,65 @@ GB_MATH_DEF gbFloat2 *gb_float22_v(gbVec2 m[2]); GB_MATH_DEF gbFloat2 *gb_float22_4(float m[4]); GB_MATH_DEF void gb_float22_transpose(float (*vec)[2]); -GB_MATH_DEF void gb_float22_mul(float (*out)[2], float (*mat1)[2], float (*mat2)[2]); -GB_MATH_DEF void gb_float22_mul_vec2(gbVec2 *out, float m[2][2], gbVec2 in); +GB_MATH_DEF void gb_float22_mul (float (*out)[2], float (*mat1)[2], float (*mat2)[2]); +GB_MATH_DEF void gb_float22_mul_vec2 (gbVec2 *out, float m[2][2], gbVec2 in); -GB_MATH_DEF void gb_mat3_identity(gbMat3 *m); +GB_MATH_DEF void gb_mat3_identity (gbMat3 *m); GB_MATH_DEF void gb_float33_identity(float m[3][3]); GB_MATH_DEF void gb_mat3_transpose(gbMat3 *m); -GB_MATH_DEF void gb_mat3_mul(gbMat3 *out, gbMat3 *m1, gbMat3 *m2); -GB_MATH_DEF void gb_mat3_mul_vec3(gbVec3 *out, gbMat3 *m, gbVec3 in); +GB_MATH_DEF void gb_mat3_mul (gbMat3 *out, gbMat3 *m1, gbMat3 *m2); +GB_MATH_DEF void gb_mat3_mul_vec3 (gbVec3 *out, gbMat3 *m, gbVec3 in); GB_MATH_DEF gbMat3 *gb_mat3_v(gbVec3 m[3]); GB_MATH_DEF gbMat3 *gb_mat3_f(float m[3][3]); + GB_MATH_DEF gbFloat3 *gb_float33_m(gbMat3 *m); GB_MATH_DEF gbFloat3 *gb_float33_v(gbVec3 m[3]); GB_MATH_DEF gbFloat3 *gb_float33_9(float m[9]); GB_MATH_DEF void gb_float33_transpose(float (*vec)[3]); -GB_MATH_DEF void gb_float33_mul(float (*out)[3], float (*mat1)[3], float (*mat2)[3]); -GB_MATH_DEF void gb_float33_mul_vec3(gbVec3 *out, float m[3][3], gbVec3 in); +GB_MATH_DEF void gb_float33_mul (float (*out)[3], float (*mat1)[3], float (*mat2)[3]); +GB_MATH_DEF void gb_float33_mul_vec3 (gbVec3 *out, float m[3][3], gbVec3 in); - -GB_MATH_DEF void gb_mat4_identity(gbMat4 *m); +GB_MATH_DEF void gb_mat4_identity (gbMat4 *m); GB_MATH_DEF void gb_float44_identity(float m[4][4]); GB_MATH_DEF void gb_mat4_transpose(gbMat4 *m); -GB_MATH_DEF void gb_mat4_mul(gbMat4 *out, gbMat4 *m1, gbMat4 *m2); -GB_MATH_DEF void gb_mat4_mul_vec4(gbVec4 *out, gbMat4 *m, gbVec4 in); +GB_MATH_DEF void gb_mat4_mul (gbMat4 *out, gbMat4 *m1, gbMat4 *m2); +GB_MATH_DEF void gb_mat4_mul_vec4 (gbVec4 *out, gbMat4 *m, gbVec4 in); GB_MATH_DEF gbMat4 *gb_mat4_v(gbVec4 m[4]); GB_MATH_DEF gbMat4 *gb_mat4_f(float m[4][4]); -GB_MATH_DEF gbFloat4 *gb_float44_m(gbMat4 *m); -GB_MATH_DEF gbFloat4 *gb_float44_v(gbVec4 m[4]); + +GB_MATH_DEF gbFloat4 *gb_float44_m (gbMat4 *m); +GB_MATH_DEF gbFloat4 *gb_float44_v (gbVec4 m[4]); GB_MATH_DEF gbFloat4 *gb_float44_16(float m[16]); GB_MATH_DEF void gb_float44_transpose(float (*vec)[4]); -GB_MATH_DEF void gb_float44_mul(float (*out)[4], float (*mat1)[4], float (*mat2)[4]); -GB_MATH_DEF void gb_float44_mul_vec4(gbVec4 *out, float m[4][4], gbVec4 in); +GB_MATH_DEF void gb_float44_mul (float (*out)[4], float (*mat1)[4], float (*mat2)[4]); +GB_MATH_DEF void gb_float44_mul_vec4 (gbVec4 *out, float m[4][4], gbVec4 in); -GB_MATH_DEF void gb_mat4_translate(gbMat4 *out, gbVec3 v); -GB_MATH_DEF void gb_mat4_rotate(gbMat4 *out, gbVec3 v, float angle_radians); -GB_MATH_DEF void gb_mat4_scale(gbMat4 *out, gbVec3 v); -GB_MATH_DEF void gb_mat4_scalef(gbMat4 *out, float s); -GB_MATH_DEF void gb_mat4_ortho2d(gbMat4 *out, float left, float right, float bottom, float top); -GB_MATH_DEF void gb_mat4_ortho3d(gbMat4 *out, float left, float right, float bottom, float top, float z_near, float z_far); -GB_MATH_DEF void gb_mat4_perspective(gbMat4 *out, float fovy, float aspect, float z_near, float z_far); +GB_MATH_DEF void gb_mat4_translate (gbMat4 *out, gbVec3 v); +GB_MATH_DEF void gb_mat4_rotate (gbMat4 *out, gbVec3 v, float angle_radians); +GB_MATH_DEF void gb_mat4_scale (gbMat4 *out, gbVec3 v); +GB_MATH_DEF void gb_mat4_scalef (gbMat4 *out, float s); +GB_MATH_DEF void gb_mat4_ortho2d (gbMat4 *out, float left, float right, float bottom, float top); +GB_MATH_DEF void gb_mat4_ortho3d (gbMat4 *out, float left, float right, float bottom, float top, float z_near, float z_far); +GB_MATH_DEF void gb_mat4_perspective (gbMat4 *out, float fovy, float aspect, float z_near, float z_far); GB_MATH_DEF void gb_mat4_infinite_perspective(gbMat4 *out, float fovy, float aspect, float z_near); GB_MATH_DEF void gb_mat4_look_at(gbMat4 *out, gbVec3 eye, gbVec3 centre, gbVec3 up); -GB_MATH_DEF gbQuat gb_quat(float x, float y, float z, float w); -GB_MATH_DEF gbQuat gb_quatv(float e[4]); -GB_MATH_DEF gbQuat gb_quat_axis_angle(gbVec3 axis, float angle_radians); +GB_MATH_DEF gbQuat gb_quat (float x, float y, float z, float w); +GB_MATH_DEF gbQuat gb_quatv (float e[4]); +GB_MATH_DEF gbQuat gb_quat_axis_angle (gbVec3 axis, float angle_radians); GB_MATH_DEF gbQuat gb_quat_euler_angles(float pitch, float yaw, float roll); -GB_MATH_DEF gbQuat gb_quat_identity(void); +GB_MATH_DEF gbQuat gb_quat_identity (void); GB_MATH_DEF void gb_quat_add(gbQuat *d, gbQuat q0, gbQuat q1); GB_MATH_DEF void gb_quat_sub(gbQuat *d, gbQuat q0, gbQuat q1); @@ -409,39 +410,40 @@ GB_MATH_DEF void gb_quat_diveqf(gbQuat *d, float s); GB_MATH_DEF float gb_quat_dot(gbQuat q0, gbQuat q1); GB_MATH_DEF float gb_quat_mag(gbQuat q); -GB_MATH_DEF void gb_quat_norm(gbQuat *d, gbQuat q); -GB_MATH_DEF void gb_quat_conj(gbQuat *d, gbQuat q); +GB_MATH_DEF void gb_quat_norm (gbQuat *d, gbQuat q); +GB_MATH_DEF void gb_quat_conj (gbQuat *d, gbQuat q); GB_MATH_DEF void gb_quat_inverse(gbQuat *d, gbQuat q); -GB_MATH_DEF void gb_quat_axis(gbVec3 *axis, gbQuat q); +GB_MATH_DEF void gb_quat_axis (gbVec3 *axis, gbQuat q); GB_MATH_DEF float gb_quat_angle(gbQuat q); GB_MATH_DEF float gb_quat_pitch(gbQuat q); -GB_MATH_DEF float gb_quat_yaw(gbQuat q); -GB_MATH_DEF float gb_quat_roll(gbQuat q); +GB_MATH_DEF float gb_quat_yaw (gbQuat q); +GB_MATH_DEF float gb_quat_roll (gbQuat q); /* NOTE(bill): Rotate v by q */ GB_MATH_DEF void gb_quat_rotate_vec3(gbVec3 *d, gbQuat q, gbVec3 v); -GB_MATH_DEF void gb_mat4_from_quat(gbMat4 *out, gbQuat q); -GB_MATH_DEF void gb_quat_from_mat4(gbQuat *out, gbMat4 *m); +GB_MATH_DEF void gb_mat4_from_quat (gbMat4 *out, gbQuat q); +GB_MATH_DEF void gb_quat_from_mat4 (gbQuat *out, gbMat4 *m); /* Interpolations */ -GB_MATH_DEF float gb_lerp(float a, float b, float t); -GB_MATH_DEF float gb_smooth_step(float a, float b, float t); +GB_MATH_DEF float gb_lerp (float a, float b, float t); +GB_MATH_DEF float gb_unlerp (float t, float a, float b); +GB_MATH_DEF float gb_smooth_step (float a, float b, float t); GB_MATH_DEF float gb_smoother_step(float a, float b, float t); GB_MATH_DEF void gb_vec2_lerp(gbVec2 *d, gbVec2 a, gbVec2 b, float t); GB_MATH_DEF void gb_vec3_lerp(gbVec3 *d, gbVec3 a, gbVec3 b, float t); GB_MATH_DEF void gb_vec4_lerp(gbVec4 *d, gbVec4 a, gbVec4 b, float t); -GB_MATH_DEF void gb_quat_lerp(gbQuat *d, gbQuat a, gbQuat b, float t); +GB_MATH_DEF void gb_quat_lerp (gbQuat *d, gbQuat a, gbQuat b, float t); GB_MATH_DEF void gb_quat_nlerp(gbQuat *d, gbQuat a, gbQuat b, float t); GB_MATH_DEF void gb_quat_slerp(gbQuat *d, gbQuat a, gbQuat b, float t); -GB_MATH_DEF void gb_quat_slerp_approx(gbQuat *d, gbQuat a, gbQuat b, float t); GB_MATH_DEF void gb_quat_nquad(gbQuat *d, gbQuat p, gbQuat a, gbQuat b, gbQuat q, float t); GB_MATH_DEF void gb_quat_squad(gbQuat *d, gbQuat p, gbQuat a, gbQuat b, gbQuat q, float t); +GB_MATH_DEF void gb_quat_slerp_approx(gbQuat *d, gbQuat a, gbQuat b, float t); GB_MATH_DEF void gb_quat_squad_approx(gbQuat *d, gbQuat p, gbQuat a, gbQuat b, gbQuat q, float t); @@ -449,9 +451,9 @@ GB_MATH_DEF void gb_quat_squad_approx(gbQuat *d, gbQuat p, gbQuat a, gbQuat b, g GB_MATH_DEF gbRect2 gb_rect2(gbVec2 pos, gbVec2 dim); GB_MATH_DEF gbRect3 gb_rect3(gbVec3 pos, gbVec3 dim); -GB_MATH_DEF int gb_rect2_contains(gbRect2 a, float x, float y); -GB_MATH_DEF int gb_rect2_contains_vec2(gbRect2 a, gbVec2 p); -GB_MATH_DEF int gb_rect2_intersects(gbRect2 a, gbRect2 b); +GB_MATH_DEF int gb_rect2_contains (gbRect2 a, float x, float y); +GB_MATH_DEF int gb_rect2_contains_vec2 (gbRect2 a, gbVec2 p); +GB_MATH_DEF int gb_rect2_intersects (gbRect2 a, gbRect2 b); GB_MATH_DEF int gb_rect2_intersection_result(gbRect2 a, gbRect2 b, gbRect2 *intersection); @@ -464,7 +466,7 @@ GB_MATH_DEF gb_math_u64 gb_hash_murmur64(void const *key, size_t num_bytes, gb_m /* Random */ /* TODO(bill): Use a generator for the random numbers */ GB_MATH_DEF float gb_random_range_float(float min_inc, float max_inc); -GB_MATH_DEF int gb_random_range_int(int min_inc, int max_inc); +GB_MATH_DEF int gb_random_range_int (int min_inc, int max_inc); #if defined(__cplusplus) @@ -473,6 +475,8 @@ GB_MATH_DEF int gb_random_range_int(int min_inc, int max_inc); #if defined(__cplusplus) && defined(GB_MATH_USE_OPERATOR_OVERLOADS) +/* TODO(bill): How should I apply GB_MATH_DEF to these operator overloads? */ + bool operator==(gbVec2 a, gbVec2 b) { return (a.x == b.x) && (a.y == b.y); } bool operator!=(gbVec2 a, gbVec2 b) { return !operator==(a, b); } @@ -738,6 +742,7 @@ gbVec3 operator*(gbQuat q, gbVec3 v) { gbVec3 r; gb_quat_rotate_vec3(&r, q, v); #define GB_MATH_IMPLEMENTATION_DONE +/* NOTE(bill): To remove the need for memcpy */ static void gb__memcpy_4byte(void *dest, void const *src, size_t size) { @@ -750,6 +755,7 @@ gb__memcpy_4byte(void *dest, void const *src, size_t size) } } +/* NOTE(bill): To remove the need for memset */ static void gb__memzero_byte4(void *dest, size_t size) { @@ -1007,16 +1013,10 @@ float gb_ceil(float x) { return (x < 0) ? (int)x : ((int)x)+1; } - -typedef union { - unsigned int i; - float f; -} gb_uif32; - float gb_half_to_float(gbHalf value) { - gb_uif32 result; + union { unsigned int i; float f; } result; int s = (value >> 15) & 0x001; int e = (value >> 10) & 0x01f; int m = value & 0x3ff; @@ -1058,7 +1058,7 @@ gb_half_to_float(gbHalf value) gbHalf gb_float_to_half(float value) { - gb_uif32 v; + union { unsigned int i; float f; } v; int i, s, e, m; v.f = value; @@ -1888,8 +1888,9 @@ gb_quat_from_mat4(gbQuat *out, gbMat4 *mat) -float gb_lerp(float a, float b, float t) { return a*(1.0f-t) + b*t; } -float gb_smooth_step(float a, float b, float t) { float x = (t - a)/(b - a); return x*x*(3.0f - 2.0f*x); } +float gb_lerp (float a, float b, float t) { return a*(1.0f-t) + b*t; } +float gb_unlerp (float t, float a, float b) { return (t-a)/(b-a); } +float gb_smooth_step (float a, float b, float t) { float x = (t - a)/(b - a); return x*x*(3.0f - 2.0f*x); } float gb_smoother_step(float a, float b, float t) { float x = (t - a)/(b - a); return x*x*x*(x*(6.0f*x - 15.0f) + 10.0f); }