diff --git a/code/base/arena.h b/code/base/arena.h index 994b27b..27a28dc 100644 --- a/code/base/arena.h +++ b/code/base/arena.h @@ -85,18 +85,18 @@ void arena_release(Arena *arena); //- rjf: arena push/pop/pos core functions -internal void *arena_push (Arena *arena, SSIZE size, SSIZE align); -internal U64 arena_pos (Arena *arena); -internal void arena_pop_to(Arena *arena, SSIZE pos); +internal void *arena_push (Arena* arena, SSIZE size, SSIZE align); +internal U64 arena_pos (Arena* arena); +internal void arena_pop_to(Arena* arena, SSIZE pos); //- rjf: arena push/pop helpers -internal void arena_clear(Arena *arena); -internal void arena_pop (Arena *arena, SSIZE amt); +internal void arena_clear(Arena* arena); +internal void arena_pop (Arena* arena, SSIZE amt); //- rjf: temporary arena scopes -internal TempArena temp_arena_begin(Arena *arena); +internal TempArena temp_arena_begin(Arena* arena); internal void temp_arena_end(TempArena temp); //- rjf: push helper macros diff --git a/code/base/generic_macros.h b/code/base/generic_macros.h new file mode 100644 index 0000000..bcc6d3f --- /dev/null +++ b/code/base/generic_macros.h @@ -0,0 +1,116 @@ + +#pragma region _Generic Macros +// ____ _ ______ _ _ ____ _ __ _ +// / ___} (_) | ____} | | (_) / __ \ | | | |(_) +// | | ___ ___ _ __ ___ _ __ _ ___ | |__ _ _ _ __ ___| |_ _ ___ _ __ | | | |_ _____ _ __ | | ___ __ _ __| | _ _ __ __ _ +// | |{__ |/ _ \ '_ \ / _ \ '__} |/ __| | __} | | | '_ \ / __} __} |/ _ \| '_ \ | | | \ \ / / _ \ '_ }| |/ _ \ / _` |/ _` || | '_ \ / _` | +// | |__j | __/ | | | __/ | | | (__ | | | |_| | | | | (__| l_| | (_) | | | | | l__| |\ V / __/ | | | (_) | (_| | (_| || | | | | (_| | +// \____/ \___}_l l_l\___}_l l_l\___| l_l \__,_l_l l_l\___}\__}_l\___/l_l l_l \____/ \_/ \___}_l l_l\___/ \__,_l\__,_l|_|_| |_|\__, | +// This implemnents macros for utilizing "The Naive Extendible _Generic Macro" explained in: __| | +// https://github.com/JacksonAllan/CC/blob/main/articles/Better_C_Generics_Part_1_The_Extendible_Generic.md {___/ +// It was choosen over the more novel implementations to keep the macros as easy to understand and unobfuscated as possible. + +#define MD_COMMA_OPERATOR , // The comma operator is used by preprocessor macros to delimit arguments, so we have to represent it via a macro to prevent parsing incorrectly. + +// Helper macros for argument selection +#define MD_SELECT_ARG_1( _1, ... ) _1 // <-- Of all th args passed pick _1. +#define MD_SELECT_ARG_2( _1, _2, ... ) _2 // <-- Of all the args passed pick _2. +#define MD_SELECT_ARG_3( _1, _2, _3, ... ) _3 // etc.. + +#define MD_GENERIC_SEL_ENTRY_TYPE MD_SELECT_ARG_1 // Use the arg expansion macro to select arg 1 which should have the type. +#define MD_GENERIC_SEL_ENTRY_FUNCTION MD_SELECT_ARG_2 // Use the arg expansion macro to select arg 2 which should have the function. +#define MD_GENERIC_SEL_ENTRY_COMMA_DELIMITER MD_SELECT_ARG_3 // Use the arg expansion macro to select arg 3 which should have the comma delimiter ','. + +#define MD_RESOLVED_FUNCTION_CALL // Just used to indicate where the call "occurs" + +// ---------------------------------------------------------------------------------------------------------------------------------- +// MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( macro ) includes a _Generic slot only if the specified macro is defined (as type, function_name). +// It takes advantage of the fact that if the macro is defined, then the expanded text will contain a comma. +// Expands to ',' if it can find (type): (function) +// Where MD_GENERIC_SEL_ENTRY_COMMA_DELIMITER is specifically looking for that , +#define MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( slot_exp ) MD_GENERIC_SEL_ENTRY_COMMA_DELIMITER( slot_exp, MD_GENERIC_SEL_ENTRY_TYPE( slot_exp, ): MD_GENERIC_SEL_ENTRY_FUNCTION( slot_exp, ) MD_COMMA_OPERATOR, , ) +// ^ Selects the comma ^ is the type ^ is the function ^ Insert a comma +// The slot won't exist if that comma is not found. + +// For the occastion where an expression didn't resolve to a selection option the "default: " will be set to: +typedef struct METADESK_NO_RESOLVED_GENERIC_SELECTION METADESK_NO_RESOLVED_GENERIC_SELECTION; +struct METADESK_NO_RESOLVED_GENERIC_SELECTION { + void* _THE_VOID_SLOT_; +}; +METADESK_NO_RESOLVED_GENERIC_SELECTION const MD_generic_selection_fail = {0}; +// Which will provide the message: error: called object type 'struct NO_RESOLVED_GENERIC_SELECTION' is not a function or function pointer +// ---------------------------------------------------------------------------------------------------------------------------------- + +// Below are generated on demand for an overlaod depdendent on a type: +// ---------------------------------------------------------------------------------------------------------------------------------- +#define MD_FUNCTION_GENERIC_EXAMPLE( selector_arg ) _Generic( \ +(selector_arg), /* Select Via Expression*/ \ + /* Extendibility slots: */ \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_1__function_sig ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_2__function_sig ) \ + default: MD_generic_selection_fail \ +) MD_RESOLVED_FUNCTION_CALL( selector_arg ) +// ---------------------------------------------------------------------------------------------------------------------------------- + +// Then each definiton of a function has an associated define: +#// #define GENERIC_SLOT_<#>_ , + +// Then somehwere later on +// ( ) { } + +// Concrete example: + +// To add support for long: +#define GENERIC_SLOT_1_MD_example_hash long, MD_example_hash__P_long +size_t MD_example_hash__P_long( long val ) { return val * 2654435761ull; } + +// To add support for long long: +#define GENERIC_SLOT_2_MD_example_hash long long, MD_example_hash__P_long_long +size_t MD_example_hash__P_long_long( long long val ) { return val * 2654435761ull; } + +// If using an Editor with support for syntax hightlighting macros: +// GENERIC_SLOT_1_MD_example_hash and GENERIC_SLOT_2_MD_example_hash should show color highlighting indicating the slot is enabled, +// or, "defined" for usage during the compilation pass that handles the _Generic instrinsic. +#define MD_hash_example( function_arguments ) _Generic( \ +(function_arguments), /* Select Via Expression*/ \ + /* Extendibility slots: */ \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_1_MD_example_hash ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_2_MD_example_hash ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_3_MD_example_hash ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_4_MD_example_hash ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_5_MD_example_hash ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_6_MD_example_hash ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_7_MD_example_hash ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_8_MD_example_hash ) \ + default: MD_generic_selection_fail \ +) MD_RESOLVED_FUNCTION_CALL( function_arguments ) + +// Additional Variations: + +// If the function takes more than one argument the following is used: +#define MD_FUNCTION_GENERIC_EXAMPLE_VARADIC( selector_arg, ... ) _Generic( \ +(selector_arg), \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_1__function_sig ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_2__function_sig ) \ + /* ... */ \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT(GENERIC_SLOT_N__function_sig ) \ + default: MD_generic_selection_fail \ +) MD_RESOLVED_FUNCTION_CALL( selector_arg, __VA_ARG__ ) + +// If the function does not take the arugment as a parameter: +#define MD_FUNCTION_GENERIC_EXAMPLE_DIRECT_TYPE( selector_arg ) _Generic( \ +( MD_TYPE_TO_EXP(selector_arg) ), \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_1__function_sig ) \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_2__function_sig ) \ + /* ... */ \ + MD_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT(GENERIC_SLOT_N__function_sig ) \ + default: MD_generic_selection_fail \ +) MD_RESOLVED_FUNCTION_CALL() + +// Used to keep the _Generic keyword happy as bare types are not considered "expressions" +#define MD_TYPE_TO_EXP(type) (* (type*)NULL) +// Instead of using this macro, you'll see it directly expanded by the code generation. + +// typedef void* MD_GenericExampleType; +// MD_FUNCTION_GENERIC_EXAMPLE_DIRECT_TYPE( MD_GenericExampleType ); +#pragma endregion _Generic Macros diff --git a/code/base/math.c b/code/base/math.c index 5585443..d231a71 100644 --- a/code/base/math.c +++ b/code/base/math.c @@ -4,120 +4,7 @@ //////////////////////////////// //~ rjf: Scalar Ops -internal F32 -mix_1f32(F32 a, F32 b, F32 t) -{ - F32 c = (a + (b-a) * Clamp(0.f, t, 1.f)); - return c; -} -internal F64 -mix_1f64(F64 a, F64 b, F64 t) -{ - F64 c = (a + (b-a) * Clamp(0.0, t, 1.0)); - return c; -} - -//////////////////////////////// -//~ rjf: Vector Ops - -internal Vec2F32 vec_2f32(F32 x, F32 y) {Vec2F32 v = {x, y}; return v;} -internal Vec2F32 add_2f32(Vec2F32 a, Vec2F32 b) {Vec2F32 c = {a.x+b.x, a.y+b.y}; return c;} -internal Vec2F32 sub_2f32(Vec2F32 a, Vec2F32 b) {Vec2F32 c = {a.x-b.x, a.y-b.y}; return c;} -internal Vec2F32 mul_2f32(Vec2F32 a, Vec2F32 b) {Vec2F32 c = {a.x*b.x, a.y*b.y}; return c;} -internal Vec2F32 div_2f32(Vec2F32 a, Vec2F32 b) {Vec2F32 c = {a.x/b.x, a.y/b.y}; return c;} -internal Vec2F32 scale_2f32(Vec2F32 v, F32 s) {Vec2F32 c = {v.x*s, v.y*s}; return c;} -internal F32 dot_2f32(Vec2F32 a, Vec2F32 b) {F32 c = a.x*b.x + a.y*b.y; return c;} -internal F32 length_squared_2f32(Vec2F32 v) {F32 c = v.x*v.x + v.y*v.y; return c;} -internal F32 length_2f32(Vec2F32 v) {F32 c = sqrt_f32(v.x*v.x + v.y*v.y); return c;} -internal Vec2F32 normalize_2f32(Vec2F32 v) {v = scale_2f32(v, 1.f/length_2f32(v)); return v;} -internal Vec2F32 mix_2f32(Vec2F32 a, Vec2F32 b, F32 t) {Vec2F32 c = {mix_1f32(a.x, b.x, t), mix_1f32(a.y, b.y, t)}; return c;} - -internal Vec2S64 vec_2s64(S64 x, S64 y) {Vec2S64 v = {x, y}; return v;} -internal Vec2S64 add_2s64(Vec2S64 a, Vec2S64 b) {Vec2S64 c = {a.x+b.x, a.y+b.y}; return c;} -internal Vec2S64 sub_2s64(Vec2S64 a, Vec2S64 b) {Vec2S64 c = {a.x-b.x, a.y-b.y}; return c;} -internal Vec2S64 mul_2s64(Vec2S64 a, Vec2S64 b) {Vec2S64 c = {a.x*b.x, a.y*b.y}; return c;} -internal Vec2S64 div_2s64(Vec2S64 a, Vec2S64 b) {Vec2S64 c = {a.x/b.x, a.y/b.y}; return c;} -internal Vec2S64 scale_2s64(Vec2S64 v, S64 s) {Vec2S64 c = {v.x*s, v.y*s}; return c;} -internal S64 dot_2s64(Vec2S64 a, Vec2S64 b) {S64 c = a.x*b.x + a.y*b.y; return c;} -internal S64 length_squared_2s64(Vec2S64 v) {S64 c = v.x*v.x + v.y*v.y; return c;} -internal S64 length_2s64(Vec2S64 v) {S64 c = (S64)sqrt_f64((F64)(v.x*v.x + v.y*v.y)); return c;} -internal Vec2S64 normalize_2s64(Vec2S64 v) {v = scale_2s64(v, (S64)(1.f/length_2s64(v))); return v;} -internal Vec2S64 mix_2s64(Vec2S64 a, Vec2S64 b, F32 t) {Vec2S64 c = {(S64)mix_1f32((F32)a.x, (F32)b.x, t), (S64)mix_1f32((F32)a.y, (F32)b.y, t)}; return c;} - -internal Vec2S32 vec_2s32(S32 x, S32 y) {Vec2S32 v = {x, y}; return v;} -internal Vec2S32 add_2s32(Vec2S32 a, Vec2S32 b) {Vec2S32 c = {a.x+b.x, a.y+b.y}; return c;} -internal Vec2S32 sub_2s32(Vec2S32 a, Vec2S32 b) {Vec2S32 c = {a.x-b.x, a.y-b.y}; return c;} -internal Vec2S32 mul_2s32(Vec2S32 a, Vec2S32 b) {Vec2S32 c = {a.x*b.x, a.y*b.y}; return c;} -internal Vec2S32 div_2s32(Vec2S32 a, Vec2S32 b) {Vec2S32 c = {a.x/b.x, a.y/b.y}; return c;} -internal Vec2S32 scale_2s32(Vec2S32 v, S32 s) {Vec2S32 c = {v.x*s, v.y*s}; return c;} -internal S32 dot_2s32(Vec2S32 a, Vec2S32 b) {S32 c = a.x*b.x + a.y*b.y; return c;} -internal S32 length_squared_2s32(Vec2S32 v) {S32 c = v.x*v.x + v.y*v.y; return c;} -internal S32 length_2s32(Vec2S32 v) {S32 c = (S32)sqrt_f32((F32)v.x*(F32)v.x + (F32)v.y*(F32)v.y); return c;} -internal Vec2S32 normalize_2s32(Vec2S32 v) {v = scale_2s32(v, (S32)(1.f/length_2s32(v))); return v;} -internal Vec2S32 mix_2s32(Vec2S32 a, Vec2S32 b, F32 t) {Vec2S32 c = {(S32)mix_1f32((F32)a.x, (F32)b.x, t), (S32)mix_1f32((F32)a.y, (F32)b.y, t)}; return c;} - -internal Vec2S16 vec_2s16(S16 x, S16 y) {Vec2S16 v = {x, y}; return v;} -internal Vec2S16 add_2s16(Vec2S16 a, Vec2S16 b) {Vec2S16 c = {(S16)(a.x+b.x), (S16)(a.y+b.y)}; return c;} -internal Vec2S16 sub_2s16(Vec2S16 a, Vec2S16 b) {Vec2S16 c = {(S16)(a.x-b.x), (S16)(a.y-b.y)}; return c;} -internal Vec2S16 mul_2s16(Vec2S16 a, Vec2S16 b) {Vec2S16 c = {(S16)(a.x*b.x), (S16)(a.y*b.y)}; return c;} -internal Vec2S16 div_2s16(Vec2S16 a, Vec2S16 b) {Vec2S16 c = {(S16)(a.x/b.x), (S16)(a.y/b.y)}; return c;} -internal Vec2S16 scale_2s16(Vec2S16 v, S16 s) {Vec2S16 c = {(S16)(v.x*s), (S16)(v.y*s)}; return c;} -internal S16 dot_2s16(Vec2S16 a, Vec2S16 b) {S16 c = a.x*b.x + a.y*b.y; return c;} -internal S16 length_squared_2s16(Vec2S16 v) {S16 c = v.x*v.x + v.y*v.y; return c;} -internal S16 length_2s16(Vec2S16 v) {S16 c = (S16)sqrt_f32((F32)(v.x*v.x + v.y*v.y)); return c;} -internal Vec2S16 normalize_2s16(Vec2S16 v) {v = scale_2s16(v, (S16)(1.f/length_2s16(v))); return v;} -internal Vec2S16 mix_2s16(Vec2S16 a, Vec2S16 b, F32 t) {Vec2S16 c = {(S16)mix_1f32((F32)a.x, (F32)b.x, t), (S16)mix_1f32((F32)a.y, (F32)b.y, t)}; return c;} - -internal Vec3F32 vec_3f32(F32 x, F32 y, F32 z) {Vec3F32 v = {x, y, z}; return v;} -internal Vec3F32 add_3f32(Vec3F32 a, Vec3F32 b) {Vec3F32 c = {a.x+b.x, a.y+b.y, a.z+b.z}; return c;} -internal Vec3F32 sub_3f32(Vec3F32 a, Vec3F32 b) {Vec3F32 c = {a.x-b.x, a.y-b.y, a.z-b.z}; return c;} -internal Vec3F32 mul_3f32(Vec3F32 a, Vec3F32 b) {Vec3F32 c = {a.x*b.x, a.y*b.y, a.z*b.z}; return c;} -internal Vec3F32 div_3f32(Vec3F32 a, Vec3F32 b) {Vec3F32 c = {a.x/b.x, a.y/b.y, a.z/b.z}; return c;} -internal Vec3F32 scale_3f32(Vec3F32 v, F32 s) {Vec3F32 c = {v.x*s, v.y*s, v.z*s}; return c;} -internal F32 dot_3f32(Vec3F32 a, Vec3F32 b) {F32 c = a.x*b.x + a.y*b.y + a.z*b.z; return c;} -internal F32 length_squared_3f32(Vec3F32 v) {F32 c = v.x*v.x + v.y*v.y + v.z*v.z; return c;} -internal F32 length_3f32(Vec3F32 v) {F32 c = sqrt_f32(v.x*v.x + v.y*v.y + v.z*v.z); return c;} -internal Vec3F32 normalize_3f32(Vec3F32 v) {v = scale_3f32(v, 1.f/length_3f32(v)); return v;} -internal Vec3F32 mix_3f32(Vec3F32 a, Vec3F32 b, F32 t) {Vec3F32 c = {mix_1f32(a.x, b.x, t), mix_1f32(a.y, b.y, t), mix_1f32(a.z, b.z, t)}; return c;} -internal Vec3F32 cross_3f32(Vec3F32 a, Vec3F32 b) {Vec3F32 c = {a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x}; return c;} - -internal Vec3S32 vec_3s32(S32 x, S32 y, S32 z) {Vec3S32 v = {x, y, z}; return v;} -internal Vec3S32 add_3s32(Vec3S32 a, Vec3S32 b) {Vec3S32 c = {a.x+b.x, a.y+b.y, a.z+b.z}; return c;} -internal Vec3S32 sub_3s32(Vec3S32 a, Vec3S32 b) {Vec3S32 c = {a.x-b.x, a.y-b.y, a.z-b.z}; return c;} -internal Vec3S32 mul_3s32(Vec3S32 a, Vec3S32 b) {Vec3S32 c = {a.x*b.x, a.y*b.y, a.z*b.z}; return c;} -internal Vec3S32 div_3s32(Vec3S32 a, Vec3S32 b) {Vec3S32 c = {a.x/b.x, a.y/b.y, a.z/b.z}; return c;} -internal Vec3S32 scale_3s32(Vec3S32 v, S32 s) {Vec3S32 c = {v.x*s, v.y*s, v.z*s}; return c;} -internal S32 dot_3s32(Vec3S32 a, Vec3S32 b) {S32 c = a.x*b.x + a.y*b.y + a.z*b.z; return c;} -internal S32 length_squared_3s32(Vec3S32 v) {S32 c = v.x*v.x + v.y*v.y + v.z*v.z; return c;} -internal S32 length_3s32(Vec3S32 v) {S32 c = (S32)sqrt_f32((F32)(v.x*v.x + v.y*v.y + v.z*v.z)); return c;} -internal Vec3S32 normalize_3s32(Vec3S32 v) {v = scale_3s32(v, (S32)(1.f/length_3s32(v))); return v;} -internal Vec3S32 mix_3s32(Vec3S32 a, Vec3S32 b, F32 t) {Vec3S32 c = {(S32)mix_1f32((F32)a.x, (F32)b.x, t), (S32)mix_1f32((F32)a.y, (F32)b.y, t), (S32)mix_1f32((F32)a.z, (F32)b.z, t)}; return c;} -internal Vec3S32 cross_3s32(Vec3S32 a, Vec3S32 b) {Vec3S32 c = {a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x}; return c;} - -internal Vec4F32 vec_4f32(F32 x, F32 y, F32 z, F32 w) {Vec4F32 v = {x, y, z, w}; return v;} -internal Vec4F32 add_4f32(Vec4F32 a, Vec4F32 b) {Vec4F32 c = {a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w}; return c;} -internal Vec4F32 sub_4f32(Vec4F32 a, Vec4F32 b) {Vec4F32 c = {a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w}; return c;} -internal Vec4F32 mul_4f32(Vec4F32 a, Vec4F32 b) {Vec4F32 c = {a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w}; return c;} -internal Vec4F32 div_4f32(Vec4F32 a, Vec4F32 b) {Vec4F32 c = {a.x/b.x, a.y/b.y, a.z/b.z, a.w/b.w}; return c;} -internal Vec4F32 scale_4f32(Vec4F32 v, F32 s) {Vec4F32 c = {v.x*s, v.y*s, v.z*s, v.w*s}; return c;} -internal F32 dot_4f32(Vec4F32 a, Vec4F32 b) {F32 c = a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w; return c;} -internal F32 length_squared_4f32(Vec4F32 v) {F32 c = v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w; return c;} -internal F32 length_4f32(Vec4F32 v) {F32 c = sqrt_f32(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w); return c;} -internal Vec4F32 normalize_4f32(Vec4F32 v) {v = scale_4f32(v, 1.f/length_4f32(v)); return v;} -internal Vec4F32 mix_4f32(Vec4F32 a, Vec4F32 b, F32 t) {Vec4F32 c = {mix_1f32(a.x, b.x, t), mix_1f32(a.y, b.y, t), mix_1f32(a.z, b.z, t), mix_1f32(a.w, b.w, t)}; return c;} - -internal Vec4S32 vec_4s32(S32 x, S32 y, S32 z, S32 w) {Vec4S32 v = {x, y, z, w}; return v;} -internal Vec4S32 add_4s32(Vec4S32 a, Vec4S32 b) {Vec4S32 c = {a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w}; return c;} -internal Vec4S32 sub_4s32(Vec4S32 a, Vec4S32 b) {Vec4S32 c = {a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w}; return c;} -internal Vec4S32 mul_4s32(Vec4S32 a, Vec4S32 b) {Vec4S32 c = {a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w}; return c;} -internal Vec4S32 div_4s32(Vec4S32 a, Vec4S32 b) {Vec4S32 c = {a.x/b.x, a.y/b.y, a.z/b.z, a.w/b.w}; return c;} -internal Vec4S32 scale_4s32(Vec4S32 v, S32 s) {Vec4S32 c = {v.x*s, v.y*s, v.z*s, v.w*s}; return c;} -internal S32 dot_4s32(Vec4S32 a, Vec4S32 b) {S32 c = a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w; return c;} -internal S32 length_squared_4s32(Vec4S32 v) {S32 c = v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w; return c;} -internal S32 length_4s32(Vec4S32 v) {S32 c = (S32)sqrt_f32((F32)(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w)); return c;} -internal Vec4S32 normalize_4s32(Vec4S32 v) {v = scale_4s32(v, (S32)(1.f/length_4s32(v))); return v;} -internal Vec4S32 mix_4s32(Vec4S32 a, Vec4S32 b, F32 t) {Vec4S32 c = {(S32)mix_1f32((F32)a.x, (F32)b.x, t), (S32)mix_1f32((F32)a.y, (F32)b.y, t), (S32)mix_1f32((F32)a.z, (F32)b.z, t), (S32)mix_1f32((F32)a.w, (F32)b.w, t)}; return c;} //////////////////////////////// //~ rjf: Matrix Ops diff --git a/code/base/math.h b/code/base/math.h index 95ff6f9..631b9cd 100644 --- a/code/base/math.h +++ b/code/base/math.h @@ -3,6 +3,7 @@ # include "context_cracking.h" # include "linkage.h" # include "macros.h" +# include "generic_macros.h" #endif // Copyright (c) 2024 Epic Games Tools @@ -16,45 +17,45 @@ typedef union Vec2F32 Vec2F32; union Vec2F32 { - struct - { - F32 x; - F32 y; - }; - F32 v[2]; + struct + { + F32 x; + F32 y; + }; + F32 v[2]; }; typedef union Vec2S64 Vec2S64; union Vec2S64 { - struct - { - S64 x; - S64 y; - }; - S64 v[2]; + struct + { + S64 x; + S64 y; + }; + S64 v[2]; }; typedef union Vec2S32 Vec2S32; union Vec2S32 { - struct - { - S32 x; - S32 y; - }; - S32 v[2]; + struct + { + S32 x; + S32 y; + }; + S32 v[2]; }; typedef union Vec2S16 Vec2S16; union Vec2S16 { - struct - { - S16 x; - S16 y; - }; - S16 v[2]; + struct + { + S16 x; + S16 y; + }; + S16 v[2]; }; //- rjf: 3-vectors @@ -62,45 +63,45 @@ union Vec2S16 typedef union Vec3F32 Vec3F32; union Vec3F32 { - struct - { - F32 x; - F32 y; - F32 z; - }; - struct - { - Vec2F32 xy; - F32 _z0; - }; - struct - { - F32 _x0; - Vec2F32 yz; - }; - F32 v[3]; + struct + { + F32 x; + F32 y; + F32 z; + }; + struct + { + Vec2F32 xy; + F32 _z0; + }; + struct + { + F32 _x0; + Vec2F32 yz; + }; + F32 v[3]; }; typedef union Vec3S32 Vec3S32; union Vec3S32 { - struct - { - S32 x; - S32 y; - S32 z; - }; - struct - { - Vec2S32 xy; - S32 _z0; - }; - struct - { - S32 _x0; - Vec2S32 yz; - }; - S32 v[3]; + struct + { + S32 x; + S32 y; + S32 z; + }; + struct + { + Vec2S32 xy; + S32 _z0; + }; + struct + { + S32 _x0; + Vec2S32 yz; + }; + S32 v[3]; }; //- rjf: 4-vectors @@ -108,57 +109,57 @@ union Vec3S32 typedef union Vec4F32 Vec4F32; union Vec4F32 { - struct - { - F32 x; - F32 y; - F32 z; - F32 w; - }; - struct - { - Vec2F32 xy; - Vec2F32 zw; - }; - struct - { - Vec3F32 xyz; - F32 _z0; - }; - struct - { - F32 _x0; - Vec3F32 yzw; - }; - F32 v[4]; + struct + { + F32 x; + F32 y; + F32 z; + F32 w; + }; + struct + { + Vec2F32 xy; + Vec2F32 zw; + }; + struct + { + Vec3F32 xyz; + F32 _z0; + }; + struct + { + F32 _x0; + Vec3F32 yzw; + }; + F32 v[4]; }; typedef union Vec4S32 Vec4S32; union Vec4S32 { - struct - { - S32 x; - S32 y; - S32 z; - S32 w; - }; - struct - { - Vec2S32 xy; - Vec2S32 zw; - }; - struct - { - Vec3S32 xyz; - S32 _z0; - }; - struct - { - S32 _x0; - Vec3S32 yzw; - }; - S32 v[4]; + struct + { + S32 x; + S32 y; + S32 z; + S32 w; + }; + struct + { + Vec2S32 xy; + Vec2S32 zw; + }; + struct + { + Vec3S32 xyz; + S32 _z0; + }; + struct + { + S32 _x0; + Vec3S32 yzw; + }; + S32 v[4]; }; //////////////////////////////// @@ -167,13 +168,13 @@ union Vec4S32 typedef struct Mat3x3F32 Mat3x3F32; struct Mat3x3F32 { - F32 v[3][3]; + F32 v[3][3]; }; typedef struct Mat4x4F32 Mat4x4F32; struct Mat4x4F32 { - F32 v[4][4]; + F32 v[4][4]; }; //////////////////////////////// @@ -184,56 +185,56 @@ struct Mat4x4F32 typedef union Rng1U32 Rng1U32; union Rng1U32 { - struct - { - U32 min; - U32 max; - }; - U32 v[2]; + struct + { + U32 min; + U32 max; + }; + U32 v[2]; }; typedef union Rng1S32 Rng1S32; union Rng1S32 { - struct - { - S32 min; - S32 max; - }; - S32 v[2]; + struct + { + S32 min; + S32 max; + }; + S32 v[2]; }; typedef union Rng1U64 Rng1U64; union Rng1U64 { - struct - { - U64 min; - U64 max; - }; - U64 v[2]; + struct + { + U64 min; + U64 max; + }; + U64 v[2]; }; typedef union Rng1S64 Rng1S64; union Rng1S64 { - struct - { - S64 min; - S64 max; - }; - S64 v[2]; + struct + { + S64 min; + S64 max; + }; + S64 v[2]; }; typedef union Rng1F32 Rng1F32; union Rng1F32 { - struct - { - F32 min; - F32 max; - }; - F32 v[2]; + struct + { + F32 min; + F32 max; + }; + F32 v[2]; }; //- rjf: 2-range (rectangles) @@ -241,93 +242,93 @@ union Rng1F32 typedef union Rng2S16 Rng2S16; union Rng2S16 { - struct - { - Vec2S16 min; - Vec2S16 max; - }; - struct - { - Vec2S16 p0; - Vec2S16 p1; - }; - struct - { - S16 x0; - S16 y0; - S16 x1; - S16 y1; - }; - Vec2S16 v[2]; + struct + { + Vec2S16 min; + Vec2S16 max; + }; + struct + { + Vec2S16 p0; + Vec2S16 p1; + }; + struct + { + S16 x0; + S16 y0; + S16 x1; + S16 y1; + }; + Vec2S16 v[2]; }; typedef union Rng2S32 Rng2S32; union Rng2S32 { - struct - { - Vec2S32 min; - Vec2S32 max; - }; - struct - { - Vec2S32 p0; - Vec2S32 p1; - }; - struct - { - S32 x0; - S32 y0; - S32 x1; - S32 y1; - }; - Vec2S32 v[2]; + struct + { + Vec2S32 min; + Vec2S32 max; + }; + struct + { + Vec2S32 p0; + Vec2S32 p1; + }; + struct + { + S32 x0; + S32 y0; + S32 x1; + S32 y1; + }; + Vec2S32 v[2]; }; typedef union Rng2F32 Rng2F32; union Rng2F32 { - struct - { - Vec2F32 min; - Vec2F32 max; - }; - struct - { - Vec2F32 p0; - Vec2F32 p1; - }; - struct - { - F32 x0; - F32 y0; - F32 x1; - F32 y1; - }; - Vec2F32 v[2]; + struct + { + Vec2F32 min; + Vec2F32 max; + }; + struct + { + Vec2F32 p0; + Vec2F32 p1; + }; + struct + { + F32 x0; + F32 y0; + F32 x1; + F32 y1; + }; + Vec2F32 v[2]; }; typedef union Rng2S64 Rng2S64; union Rng2S64 { - struct - { - Vec2S64 min; - Vec2S64 max; - }; - struct - { - Vec2S64 p0; - Vec2S64 p1; - }; - struct - { - S64 x0; - S64 y0; - S64 x1; - S64 y1; - }; - Vec2S64 v[2]; + struct + { + Vec2S64 min; + Vec2S64 max; + }; + struct + { + Vec2S64 p0; + Vec2S64 p1; + }; + struct + { + S64 x0; + S64 y0; + S64 x1; + S64 y1; + }; + Vec2S64 v[2]; }; //////////////////////////////// @@ -336,23 +337,23 @@ union Rng2S64 typedef struct Rng1S64Node Rng1S64Node; struct Rng1S64Node { - Rng1S64Node *next; - Rng1S64 v; + Rng1S64Node *next; + Rng1S64 v; }; typedef struct Rng1S64List Rng1S64List; struct Rng1S64List { - Rng1S64Node *first; - Rng1S64Node *last; - U64 count; + Rng1S64Node *first; + Rng1S64Node *last; + U64 count; }; typedef struct Rng1S64Array Rng1S64Array; struct Rng1S64Array { - Rng1S64 *v; - U64 count; + Rng1S64 *v; + U64 count; }; //////////////////////////////// @@ -367,15 +368,18 @@ struct Rng1S64Array #define floor_f32(v) floorf(v) #define round_f32(v) roundf(v) #define abs_f32(v) fabsf(v) -#define radians_from_turns_f32(v) ((v)*2*3.1415926535897f) -#define turns_from_radians_f32(v) ((v)/2*3.1415926535897f) -#define degrees_from_turns_f32(v) ((v)*360.f) -#define turns_from_degrees_f32(v) ((v)/360.f) + +#define radians_from_turns_f32(v) ((v) * 2 * 3.1415926535897f) +#define turns_from_radians_f32(v) ((v) / 2 * 3.1415926535897f) +#define degrees_from_turns_f32(v) ((v) * 360.f) +#define turns_from_degrees_f32(v) ((v) / 360.f) + #define degrees_from_radians_f32(v) (degrees_from_turns_f32(turns_from_radians_f32(v))) #define radians_from_degrees_f32(v) (radians_from_turns_f32(turns_from_degrees_f32(v))) -#define sin_f32(v) sinf(radians_from_turns_f32(v)) -#define cos_f32(v) cosf(radians_from_turns_f32(v)) -#define tan_f32(v) tanf(radians_from_turns_f32(v)) + +#define sin_f32(v) sinf( radians_from_turns_f32(v) ) +#define cos_f32(v) cosf( radians_from_turns_f32(v) ) +#define tan_f32(v) tanf( radians_from_turns_f32(v) ) #define sqrt_f64(v) sqrt(v) #define mod_f64(a, b) fmod((a), (b)) @@ -384,127 +388,173 @@ struct Rng1S64Array #define floor_f64(v) floor(v) #define round_f64(v) round(v) #define abs_f64(v) fabs(v) -#define radians_from_turns_f64(v) ((v)*2*3.1415926535897) -#define turns_from_radians_f64(v) ((v)/2*3.1415926535897) -#define degrees_from_turns_f64(v) ((v)*360.0) -#define turns_from_degrees_f64(v) ((v)/360.0) + +#define radians_from_turns_f64(v) ((v) * 2 * 3.1415926535897) +#define turns_from_radians_f64(v) ((v) / 2 * 3.1415926535897) +#define degrees_from_turns_f64(v) ((v) * 360.0) +#define turns_from_degrees_f64(v) ((v) / 360.0) + #define degrees_from_radians_f64(v) (degrees_from_turns_f64(turns_from_radians_f64(v))) #define radians_from_degrees_f64(v) (radians_from_turns_f64(turns_from_degrees_f64(v))) + #define sin_f64(v) sin(radians_from_turns_f64(v)) #define cos_f64(v) cos(radians_from_turns_f64(v)) #define tan_f64(v) tan(radians_from_turns_f64(v)) -internal F32 mix_1f32(F32 a, F32 b, F32 t); -internal F64 mix_1f64(F64 a, F64 b, F64 t); +inline F32 mix_1f32(F32 a, F32 b, F32 t) { F32 c = (a + (b - a) * Clamp(0.f, t, 1.f)); return c; } +inline F64 mix_1f64(F64 a, F64 b, F64 t) { F64 c = (a + (b - a) * Clamp(0.0, t, 1.0)); return c; } //////////////////////////////// //~ rjf: Vector Ops +// ==================== 2D Vectors ==================== + #define v2f32(x, y) vec_2f32((x), (y)) -internal Vec2F32 vec_2f32(F32 x, F32 y); -internal Vec2F32 add_2f32(Vec2F32 a, Vec2F32 b); -internal Vec2F32 sub_2f32(Vec2F32 a, Vec2F32 b); -internal Vec2F32 mul_2f32(Vec2F32 a, Vec2F32 b); -internal Vec2F32 div_2f32(Vec2F32 a, Vec2F32 b); -internal Vec2F32 scale_2f32(Vec2F32 v, F32 s); -internal F32 dot_2f32(Vec2F32 a, Vec2F32 b); -internal F32 length_squared_2f32(Vec2F32 v); -internal F32 length_2f32(Vec2F32 v); -internal Vec2F32 normalize_2f32(Vec2F32 v); -internal Vec2F32 mix_2f32(Vec2F32 a, Vec2F32 b, F32 t); +inline Vec2F32 vec_2f32 (F32 x, F32 y) { Vec2F32 v = {x, y}; return v; } +inline Vec2F32 add_2f32 (Vec2F32 a, Vec2F32 b) { Vec2F32 c = {a.x + b.x, a.y + b.y}; return c; } +inline Vec2F32 sub_2f32 (Vec2F32 a, Vec2F32 b) { Vec2F32 c = {a.x - b.x, a.y - b.y}; return c; } +inline Vec2F32 mul_2f32 (Vec2F32 a, Vec2F32 b) { Vec2F32 c = {a.x * b.x, a.y * b.y}; return c; } +inline Vec2F32 div_2f32 (Vec2F32 a, Vec2F32 b) { Vec2F32 c = {a.x / b.x, a.y / b.y}; return c; } +inline Vec2F32 scale_2f32 (Vec2F32 v, F32 s) { Vec2F32 c = {v.x * s, v.y * s }; return c; } +inline F32 dot_2f32 (Vec2F32 a, Vec2F32 b) { F32 c = a.x * b.x + a.y * b.y; return c; } +inline F32 length_squared_2f32(Vec2F32 v) { F32 c = v.x * v.x + v.y * v.y; return c; } +inline F32 length_2f32 (Vec2F32 v) { F32 c = sqrt_f32(v.x*v.x + v.y*v.y); return c; } +inline Vec2F32 normalize_2f32 (Vec2F32 v) { v = scale_2f32(v, 1.f / length_2f32(v)); return v; } +inline Vec2F32 mix_2f32 (Vec2F32 a, Vec2F32 b, F32 t) { Vec2F32 c = {mix_1f32(a.x, b.x, t), mix_1f32(a.y, b.y, t)}; return c; } #define v2s64(x, y) vec_2s64((x), (y)) -internal Vec2S64 vec_2s64(S64 x, S64 y); -internal Vec2S64 add_2s64(Vec2S64 a, Vec2S64 b); -internal Vec2S64 sub_2s64(Vec2S64 a, Vec2S64 b); -internal Vec2S64 mul_2s64(Vec2S64 a, Vec2S64 b); -internal Vec2S64 div_2s64(Vec2S64 a, Vec2S64 b); -internal Vec2S64 scale_2s64(Vec2S64 v, S64 s); -internal S64 dot_2s64(Vec2S64 a, Vec2S64 b); -internal S64 length_squared_2s64(Vec2S64 v); -internal S64 length_2s64(Vec2S64 v); -internal Vec2S64 normalize_2s64(Vec2S64 v); -internal Vec2S64 mix_2s64(Vec2S64 a, Vec2S64 b, F32 t); +inline Vec2S64 vec_2s64 (S64 x, S64 y) { Vec2S64 v = {x, y}; return v; } +inline Vec2S64 add_2s64 (Vec2S64 a, Vec2S64 b) { Vec2S64 c = {a.x + b.x, a.y + b.y}; return c; } +inline Vec2S64 sub_2s64 (Vec2S64 a, Vec2S64 b) { Vec2S64 c = {a.x - b.x, a.y - b.y}; return c; } +inline Vec2S64 mul_2s64 (Vec2S64 a, Vec2S64 b) { Vec2S64 c = {a.x * b.x, a.y * b.y}; return c; } +inline Vec2S64 div_2s64 (Vec2S64 a, Vec2S64 b) { Vec2S64 c = {a.x / b.x, a.y / b.y}; return c; } +inline Vec2S64 scale_2s64 (Vec2S64 v, S64 s) { Vec2S64 c = {v.x * s, v.y * s }; return c; } +inline S64 dot_2s64 (Vec2S64 a, Vec2S64 b) { S64 c = a.x * b.x + a.y * b.y; return c; } +inline S64 length_squared_2s64(Vec2S64 v) { S64 c = v.x * v.x + v.y * v.y; return c; } +inline S64 length_2s64 (Vec2S64 v) { S64 c = (S64)sqrt_f64((F64)(v.x*v.x + v.y*v.y)); return c; } +inline Vec2S64 normalize_2s64 (Vec2S64 v) { v = scale_2s64(v, (S64)(1.f / length_2s64(v))); return v; } +inline Vec2S64 mix_2s64 (Vec2S64 a, Vec2S64 b, F32 t) { Vec2S64 c = {(S64)mix_1f32((F32)a.x, (F32)b.x, t), (S64)mix_1f32((F32)a.y, (F32)b.y, t)}; return c; } #define v2s32(x, y) vec_2s32((x), (y)) -internal Vec2S32 vec_2s32(S32 x, S32 y); -internal Vec2S32 add_2s32(Vec2S32 a, Vec2S32 b); -internal Vec2S32 sub_2s32(Vec2S32 a, Vec2S32 b); -internal Vec2S32 mul_2s32(Vec2S32 a, Vec2S32 b); -internal Vec2S32 div_2s32(Vec2S32 a, Vec2S32 b); -internal Vec2S32 scale_2s32(Vec2S32 v, S32 s); -internal S32 dot_2s32(Vec2S32 a, Vec2S32 b); -internal S32 length_squared_2s32(Vec2S32 v); -internal S32 length_2s32(Vec2S32 v); -internal Vec2S32 normalize_2s32(Vec2S32 v); -internal Vec2S32 mix_2s32(Vec2S32 a, Vec2S32 b, F32 t); +inline Vec2S32 vec_2s32(S32 x, S32 y) { Vec2S32 v = {x, y}; return v; } +inline Vec2S32 add_2s32(Vec2S32 a, Vec2S32 b) { Vec2S32 c = {a.x + b.x, a.y + b.y}; return c; } +inline Vec2S32 sub_2s32(Vec2S32 a, Vec2S32 b) { Vec2S32 c = {a.x - b.x, a.y - b.y}; return c; } +inline Vec2S32 mul_2s32(Vec2S32 a, Vec2S32 b) { Vec2S32 c = {a.x * b.x, a.y * b.y}; return c; } +inline Vec2S32 div_2s32(Vec2S32 a, Vec2S32 b) { Vec2S32 c = {a.x / b.x, a.y / b.y}; return c; } +inline Vec2S32 scale_2s32(Vec2S32 v, S32 s) { Vec2S32 c = {v.x * s, v.y * s }; return c; } +inline S32 dot_2s32 (Vec2S32 a, Vec2S32 b) { S32 c = a.x * b.x + a.y * b.y; return c; } +inline S32 length_squared_2s32(Vec2S32 v) { S32 c = v.x * v.x + v.y * v.y; return c; } +inline S32 length_2s32 (Vec2S32 v) { S32 c = (S32)sqrt_f32((F32)v.x*(F32)v.x + (F32)v.y*(F32)v.y); return c; } +inline Vec2S32 normalize_2s32(Vec2S32 v) { v = scale_2s32(v, (S32)(1.f / length_2s32(v))); return v; } +inline Vec2S32 mix_2s32 (Vec2S32 a, Vec2S32 b, F32 t) { Vec2S32 c = {(S32)mix_1f32((F32)a.x, (F32)b.x, t), (S32)mix_1f32((F32)a.y, (F32)b.y, t)}; return c; } #define v2s16(x, y) vec_2s16((x), (y)) -internal Vec2S16 vec_2s16(S16 x, S16 y); -internal Vec2S16 add_2s16(Vec2S16 a, Vec2S16 b); -internal Vec2S16 sub_2s16(Vec2S16 a, Vec2S16 b); -internal Vec2S16 mul_2s16(Vec2S16 a, Vec2S16 b); -internal Vec2S16 div_2s16(Vec2S16 a, Vec2S16 b); -internal Vec2S16 scale_2s16(Vec2S16 v, S16 s); -internal S16 dot_2s16(Vec2S16 a, Vec2S16 b); -internal S16 length_squared_2s16(Vec2S16 v); -internal S16 length_2s16(Vec2S16 v); -internal Vec2S16 normalize_2s16(Vec2S16 v); -internal Vec2S16 mix_2s16(Vec2S16 a, Vec2S16 b, F32 t); +inline Vec2S16 vec_2s16(S16 x, S16 y) { Vec2S16 v = {x, y}; return v; } +inline Vec2S16 add_2s16(Vec2S16 a, Vec2S16 b) { Vec2S16 c = {(S16)(a.x + b.x), (S16)(a.y + b.y)}; return c; } +inline Vec2S16 sub_2s16(Vec2S16 a, Vec2S16 b) { Vec2S16 c = {(S16)(a.x - b.x), (S16)(a.y - b.y)}; return c; } +inline Vec2S16 mul_2s16(Vec2S16 a, Vec2S16 b) { Vec2S16 c = {(S16)(a.x * b.x), (S16)(a.y * b.y)}; return c; } +inline Vec2S16 div_2s16(Vec2S16 a, Vec2S16 b) { Vec2S16 c = {(S16)(a.x / b.x), (S16)(a.y / b.y)}; return c; } +inline Vec2S16 scale_2s16(Vec2S16 v, S16 s) { Vec2S16 c = {(S16)(v.x * s), (S16)(v.y * s )}; return c; } +inline S16 dot_2s16 (Vec2S16 a, Vec2S16 b) { S16 c = a.x * b.x + a.y * b.y; return c; } +inline S16 length_squared_2s16(Vec2S16 v) { S16 c = v.x * v.x + v.y * v.y; return c; } +inline S16 length_2s16 (Vec2S16 v) { S16 c = (S16)sqrt_f32((F32)(v.x*v.x + v.y*v.y)); return c; } +inline Vec2S16 normalize_2s16(Vec2S16 v) { v = scale_2s16(v, (S16)(1.f / length_2s16(v))); return v; } +inline Vec2S16 mix_2s16 (Vec2S16 a, Vec2S16 b, F32 t) { Vec2S16 c = {(S16)mix_1f32((F32)a.x, (F32)b.x, t), (S16)mix_1f32((F32)a.y, (F32)b.y, t)}; return c; } + +#define vec2(a, b) _Generic(a, S16: vec_2s16, S32: vec_2s32, S64: vec_2s64, F32: vec_2f32 )((a), (b)) +#define add_vec2(a, b) _Generic(a, S16: add_2s16, S32: add_2s32, S64: add_2s64, F32: add_2f32 )((a), (b)) +#define sub_vec2(a, b) _Generic(a, S16: sub_2s16, S32: sub_2s32, S64: sub_2s64, F32: sub_2f32 )((a), (b)) +#define mul_vec2(a, b) _Generic(a, S16: mul_2s16, S32: mul_2s32, S64: mul_2s64, F32: mul_2f32 )((a), (b)) +#define div_vec2(a, b) _Generic(a, S16: div_2s16, S32: div_2s32, S64: div_2s64, F32: div_2f32 )((a), (b)) +#define scale_vec2(v, s) _Generic(v, S16: scale_2s16, S32: scale_2s32, S64: scale_2s64, F32: scale_2f32 )((v), (s)) +#define dot_vec2(a, b) _Generic(a, S16: dot_2s16, S32: dot_2s32, S64: dot_2s64, F32: dot_2f32 )((a), (b)) +#define length_squared_vec2(v) _Generic(v, S16: length_squared_2s16, S32: length_squared_2s32, S64: length_squared_2s64, F32: length_squared_2f32)((v)) +#define length_vec2(v) _Generic(v, S16: length_2s16, S32: length_2s32, S64: length_2s64, F32: length_2f32 )((v)) +#define normalize_vec2(v) _Generic(v, S16: normalize_2s16, S32: normalize_2s32, S64: normalize_2s64, F32: normalize_2f32 )((v)) +#define mix_vec2(a, b, t) _Generic(a, S16: mix_2s16, S32: mix_2s32, S64: mix_2s64, F32: mix_2f32 )((a), (b), (t)) + +// ==================== 3D Vectors ==================== #define v3f32(x, y, z) vec_3f32((x), (y), (z)) -internal Vec3F32 vec_3f32(F32 x, F32 y, F32 z); -internal Vec3F32 add_3f32(Vec3F32 a, Vec3F32 b); -internal Vec3F32 sub_3f32(Vec3F32 a, Vec3F32 b); -internal Vec3F32 mul_3f32(Vec3F32 a, Vec3F32 b); -internal Vec3F32 div_3f32(Vec3F32 a, Vec3F32 b); -internal Vec3F32 scale_3f32(Vec3F32 v, F32 s); -internal F32 dot_3f32(Vec3F32 a, Vec3F32 b); -internal F32 length_squared_3f32(Vec3F32 v); -internal F32 length_3f32(Vec3F32 v); -internal Vec3F32 normalize_3f32(Vec3F32 v); -internal Vec3F32 mix_3f32(Vec3F32 a, Vec3F32 b, F32 t); -internal Vec3F32 cross_3f32(Vec3F32 a, Vec3F32 b); +inline Vec3F32 vec_3f32 (F32 x, F32 y, F32 z) { Vec3F32 v = {x, y, z}; return v; } +inline Vec3F32 add_3f32 (Vec3F32 a, Vec3F32 b) { Vec3F32 c = {a.x + b.x, a.y + b.y, a.z + b.z}; return c; } +inline Vec3F32 sub_3f32 (Vec3F32 a, Vec3F32 b) { Vec3F32 c = {a.x - b.x, a.y - b.y, a.z - b.z}; return c; } +inline Vec3F32 mul_3f32 (Vec3F32 a, Vec3F32 b) { Vec3F32 c = {a.x * b.x, a.y * b.y, a.z * b.z}; return c; } +inline Vec3F32 div_3f32 (Vec3F32 a, Vec3F32 b) { Vec3F32 c = {a.x / b.x, a.y / b.y, a.z / b.z}; return c; } +inline Vec3F32 scale_3f32 (Vec3F32 v, F32 s) { Vec3F32 c = {v.x * s, v.y * s, v.z * s}; return c; } +inline F32 dot_3f32 (Vec3F32 a, Vec3F32 b) { F32 c = a.x * b.x + a.y * b.y + a.z * b.z; return c; } +inline F32 length_squared_3f32(Vec3F32 v) { F32 c = v.x * v.x + v.y * v.y + v.z * v.z; return c; } +inline F32 length_3f32 (Vec3F32 v) { F32 c = sqrt_f32(v.x * v.x + v.y * v.y + v.z * v.z); return c; } +inline Vec3F32 normalize_3f32 (Vec3F32 v) { v = scale_3f32(v, 1.f / length_3f32(v)); return v; } +inline Vec3F32 mix_3f32 (Vec3F32 a, Vec3F32 b, F32 t) { Vec3F32 c = {mix_1f32(a.x, b.x, t), mix_1f32(a.y, b.y, t), mix_1f32(a.z, b.z, t)}; return c; } +inline Vec3F32 cross_3f32 (Vec3F32 a, Vec3F32 b) { Vec3F32 c = {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; return c; } #define v3s32(x, y, z) vec_3s32((x), (y), (z)) -internal Vec3S32 vec_3s32(S32 x, S32 y, S32 z); -internal Vec3S32 add_3s32(Vec3S32 a, Vec3S32 b); -internal Vec3S32 sub_3s32(Vec3S32 a, Vec3S32 b); -internal Vec3S32 mul_3s32(Vec3S32 a, Vec3S32 b); -internal Vec3S32 div_3s32(Vec3S32 a, Vec3S32 b); -internal Vec3S32 scale_3s32(Vec3S32 v, S32 s); -internal S32 dot_3s32(Vec3S32 a, Vec3S32 b); -internal S32 length_squared_3s32(Vec3S32 v); -internal S32 length_3s32(Vec3S32 v); -internal Vec3S32 normalize_3s32(Vec3S32 v); -internal Vec3S32 mix_3s32(Vec3S32 a, Vec3S32 b, F32 t); -internal Vec3S32 cross_3s32(Vec3S32 a, Vec3S32 b); +inline Vec3S32 vec_3s32 (S32 x, S32 y, S32 z) { Vec3S32 v = {x, y, z}; return v; } +inline Vec3S32 add_3s32 (Vec3S32 a, Vec3S32 b) { Vec3S32 c = {a.x + b.x, a.y + b.y, a.z + b.z}; return c; } +inline Vec3S32 sub_3s32 (Vec3S32 a, Vec3S32 b) { Vec3S32 c = {a.x - b.x, a.y - b.y, a.z - b.z}; return c; } +inline Vec3S32 mul_3s32 (Vec3S32 a, Vec3S32 b) { Vec3S32 c = {a.x * b.x, a.y * b.y, a.z * b.z}; return c; } +inline Vec3S32 div_3s32 (Vec3S32 a, Vec3S32 b) { Vec3S32 c = {a.x / b.x, a.y / b.y, a.z / b.z}; return c; } +inline Vec3S32 scale_3s32 (Vec3S32 v, S32 s) { Vec3S32 c = {v.x * s, v.y * s, v.z * s }; return c; } +inline S32 dot_3s32 (Vec3S32 a, Vec3S32 b) { S32 c = a.x * b.x + a.y * b.y + a.z * b.z; return c; } +inline S32 length_squared_3s32(Vec3S32 v) { S32 c = v.x * v.x + v.y * v.y + v.z * v.z; return c; } +inline S32 length_3s32 (Vec3S32 v) { S32 c = (S32)sqrt_f32((F32)(v.x * v.x + v.y * v.y + v.z * v.z)); return c; } +inline Vec3S32 normalize_3s32 (Vec3S32 v) { v = scale_3s32(v, (S32)(1.f / length_3s32(v))); return v; } +inline Vec3S32 mix_3s32 (Vec3S32 a, Vec3S32 b, F32 t) { Vec3S32 c = {(S32)mix_1f32((F32)a.x, (F32)b.x, t), (S32)mix_1f32((F32)a.y, (F32)b.y, t), (S32)mix_1f32((F32)a.z, (F32)b.z, t)}; return c; } +inline Vec3S32 cross_3s32 (Vec3S32 a, Vec3S32 b) { Vec3S32 c = {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; return c; } + +#define vec3(a, b, c) _Generic(a, S32: vec_3s32, F32: vec_3f32 )((a), (b), (c)) +#define add_vec3(a, b) _Generic(a, S32: add_3s32, F32: add_3f32 )((a), (b)) +#define sub_vec3(a, b) _Generic(a, S32: sub_3s32, F32: sub_3f32 )((a), (b)) +#define mul_vec3(a, b) _Generic(a, S32: mul_3s32, F32: mul_3f32 )((a), (b)) +#define div_vec3(a, b) _Generic(a, S32: div_3s32, F32: div_3f32 )((a), (b)) +#define scale_vec3(v, s) _Generic(v, S32: scale_3s32, F32: scale_3f32 )((v), (s)) +#define dot_vec3(a, b) _Generic(a, S32: dot_3s32, F32: dot_3f32 )((a), (b)) +#define length_squared_vec3(v) _Generic(v, S32: length_squared_3s32, F32: length_squared_3f32)((v)) +#define length_vec3(v) _Generic(v, S32: length_3s32, F32: length_3f32 )((v)) +#define normalize_vec3(v) _Generic(v, S32: normalize_3s32, F32: normalize_3f32 )((v)) +#define mix_vec3(a, b, t) _Generic(a, S32: mix_3s32, F32: mix_3f32 )((a), (b), (t)) +#define cross_vec3(a, b) _Generic(a, S32: cross_3s32, F32: cross_3f32 )((a), (b)) + +// ==================== 4D Vectors ==================== #define v4f32(x, y, z, w) vec_4f32((x), (y), (z), (w)) -internal Vec4F32 vec_4f32(F32 x, F32 y, F32 z, F32 w); -internal Vec4F32 add_4f32(Vec4F32 a, Vec4F32 b); -internal Vec4F32 sub_4f32(Vec4F32 a, Vec4F32 b); -internal Vec4F32 mul_4f32(Vec4F32 a, Vec4F32 b); -internal Vec4F32 div_4f32(Vec4F32 a, Vec4F32 b); -internal Vec4F32 scale_4f32(Vec4F32 v, F32 s); -internal F32 dot_4f32(Vec4F32 a, Vec4F32 b); -internal F32 length_squared_4f32(Vec4F32 v); -internal F32 length_4f32(Vec4F32 v); -internal Vec4F32 normalize_4f32(Vec4F32 v); -internal Vec4F32 mix_4f32(Vec4F32 a, Vec4F32 b, F32 t); +internal Vec4F32 vec_4f32(F32 x, F32 y, F32 z, F32 w) { Vec4F32 v = {x, y, z, w}; return v; } +internal Vec4F32 add_4f32(Vec4F32 a, Vec4F32 b) { Vec4F32 c = {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; return c; } +internal Vec4F32 sub_4f32(Vec4F32 a, Vec4F32 b) { Vec4F32 c = {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; return c; } +internal Vec4F32 mul_4f32(Vec4F32 a, Vec4F32 b) { Vec4F32 c = {a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w}; return c; } +internal Vec4F32 div_4f32(Vec4F32 a, Vec4F32 b) { Vec4F32 c = {a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w}; return c; } +internal Vec4F32 scale_4f32(Vec4F32 v, F32 s) { Vec4F32 c = {v.x * s, v.y * s, v.z * s, v.w * s }; return c; } +internal F32 dot_4f32(Vec4F32 a, Vec4F32 b) { F32 c = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; return c; } +internal F32 length_squared_4f32(Vec4F32 v) { F32 c = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w; return c; } +internal F32 length_4f32(Vec4F32 v) { F32 c = sqrt_f32(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); return c; } +internal Vec4F32 normalize_4f32(Vec4F32 v) { v = scale_4f32(v, 1.f / length_4f32(v)); return v; } +internal Vec4F32 mix_4f32(Vec4F32 a, Vec4F32 b, F32 t) { Vec4F32 c = {mix_1f32(a.x, b.x, t), mix_1f32(a.y, b.y, t), mix_1f32(a.z, b.z, t), mix_1f32(a.w, b.w, t)}; return c; } #define v4s32(x, y, z, w) vec_4s32((x), (y), (z), (w)) -internal Vec4S32 vec_4s32(S32 x, S32 y, S32 z, S32 w); -internal Vec4S32 add_4s32(Vec4S32 a, Vec4S32 b); -internal Vec4S32 sub_4s32(Vec4S32 a, Vec4S32 b); -internal Vec4S32 mul_4s32(Vec4S32 a, Vec4S32 b); -internal Vec4S32 div_4s32(Vec4S32 a, Vec4S32 b); -internal Vec4S32 scale_4s32(Vec4S32 v, S32 s); -internal S32 dot_4s32(Vec4S32 a, Vec4S32 b); -internal S32 length_squared_4s32(Vec4S32 v); -internal S32 length_4s32(Vec4S32 v); -internal Vec4S32 normalize_4s32(Vec4S32 v); -internal Vec4S32 mix_4s32(Vec4S32 a, Vec4S32 b, F32 t); +internal Vec4S32 vec_4s32(S32 x, S32 y, S32 z, S32 w) { Vec4S32 v = {x, y, z, w}; return v; } +internal Vec4S32 add_4s32(Vec4S32 a, Vec4S32 b) { Vec4S32 c = {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; return c; } +internal Vec4S32 sub_4s32(Vec4S32 a, Vec4S32 b) { Vec4S32 c = {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; return c; } +internal Vec4S32 mul_4s32(Vec4S32 a, Vec4S32 b) { Vec4S32 c = {a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w}; return c; } +internal Vec4S32 div_4s32(Vec4S32 a, Vec4S32 b) { Vec4S32 c = {a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w}; return c; } +internal Vec4S32 scale_4s32(Vec4S32 v, S32 s) { Vec4S32 c = {v.x * s, v.y * s, v.z * s, v.w * s }; return c; } +internal S32 dot_4s32(Vec4S32 a, Vec4S32 b) { S32 c = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; return c; } +internal S32 length_squared_4s32(Vec4S32 v) { S32 c = v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w; return c; } +internal S32 length_4s32(Vec4S32 v) { S32 c = (S32)sqrt_f32((F32)(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w)); return c; } +internal Vec4S32 normalize_4s32(Vec4S32 v) { v = scale_4s32(v, (S32)(1.f / length_4s32(v))); return v; } +internal Vec4S32 mix_4s32(Vec4S32 a, Vec4S32 b, F32 t) { Vec4S32 c = {(S32)mix_1f32((F32)a.x, (F32)b.x, t), (S32)mix_1f32((F32)a.y, (F32)b.y, t), (S32)mix_1f32((F32)a.z, (F32)b.z, t), (S32)mix_1f32((F32)a.w, (F32)b.w, t)}; return c; } + +#define vec4(a, b, c, d) _Generic(a, S32: vec_4s32, F32: vec_4f32 )((a), (b), (c), (d)) +#define add_vec4(a, b) _Generic(a, S32: add_4s32, F32: add_4f32 )((a), (b)) +#define sub_vec4(a, b) _Generic(a, S32: sub_4s32, F32: sub_4f32 )((a), (b)) +#define mul_vec4(a, b) _Generic(a, S32: mul_4s32, F32: mul_4f32 )((a), (b)) +#define div_vec4(a, b) _Generic(a, S32: div_4s32, F32: div_4f32 )((a), (b)) +#define scale_vec4(v, s) _Generic(v, S32: scale_4s32, F32: scale_4f32 )((v), (s)) +#define dot_vec4(a, b) _Generic(a, S32: dot_4s32, F32: dot_4f32 )((a), (b)) +#define length_squared_vec4(v) _Generic(v, S32: length_squared_4s32, F32: length_squared_4f32)((v)) +#define length_vec4(v) _Generic(v, S32: length_4s32, F32: length_4f32 )((v)) +#define normalize_vec4(v) _Generic(v, S32: normalize_4s32, F32: normalize_4f32 )((v)) +#define mix_vec4(a, b, t) _Generic(a, S32: mix_4s32, F32: mix_4f32 )((a), (b), (t)) //////////////////////////////// //~ rjf: Matrix Ops @@ -602,7 +652,7 @@ internal Rng2S32 rng_2s32(Vec2S32 min, Vec2S32 max); internal Rng2S32 shift_2s32(Rng2S32 r, Vec2S32 x); internal Rng2S32 pad_2s32(Rng2S32 r, S32 x); internal Vec2S32 center_2s32(Rng2S32 r); -internal B32 contains_2s32(Rng2S32 r, Vec2S32 x); +internal B32 contains_2s32(Rng2S32 r, Vec2S32 x); internal Vec2S32 dim_2s32(Rng2S32 r); internal Rng2S32 union_2s32(Rng2S32 a, Rng2S32 b); internal Rng2S32 intersect_2s32(Rng2S32 a, Rng2S32 b); @@ -614,7 +664,7 @@ internal Rng2S64 rng_2s64(Vec2S64 min, Vec2S64 max); internal Rng2S64 shift_2s64(Rng2S64 r, Vec2S64 x); internal Rng2S64 pad_2s64(Rng2S64 r, S64 x); internal Vec2S64 center_2s64(Rng2S64 r); -internal B32 contains_2s64(Rng2S64 r, Vec2S64 x); +internal B32 contains_2s64(Rng2S64 r, Vec2S64 x); internal Vec2S64 dim_2s64(Rng2S64 r); internal Rng2S64 union_2s64(Rng2S64 a, Rng2S64 b); internal Rng2S64 intersect_2s64(Rng2S64 a, Rng2S64 b); @@ -626,7 +676,7 @@ internal Rng2F32 rng_2f32(Vec2F32 min, Vec2F32 max); internal Rng2F32 shift_2f32(Rng2F32 r, Vec2F32 x); internal Rng2F32 pad_2f32(Rng2F32 r, F32 x); internal Vec2F32 center_2f32(Rng2F32 r); -internal B32 contains_2f32(Rng2F32 r, Vec2F32 x); +internal B32 contains_2f32(Rng2F32 r, Vec2F32 x); internal Vec2F32 dim_2f32(Rng2F32 r); internal Rng2F32 union_2f32(Rng2F32 a, Rng2F32 b); internal Rng2F32 intersect_2f32(Rng2F32 a, Rng2F32 b); @@ -635,17 +685,23 @@ internal Vec2F32 clamp_2f32(Rng2F32 r, Vec2F32 v); //////////////////////////////// //~ rjf: Miscellaneous Ops -internal Vec3F32 hsv_from_rgb(Vec3F32 rgb); -internal Vec3F32 rgb_from_hsv(Vec3F32 hsv); +internal Vec3F32 hsv_from_rgb (Vec3F32 rgb); +internal Vec3F32 rgb_from_hsv (Vec3F32 hsv); internal Vec4F32 hsva_from_rgba(Vec4F32 rgba); internal Vec4F32 rgba_from_hsva(Vec4F32 hsva); -internal Vec4F32 rgba_from_u32(U32 hex); -internal U32 u32_from_rgba(Vec4F32 rgba); +internal Vec4F32 rgba_from_u32 (U32 hex); +internal U32 u32_from_rgba (Vec4F32 rgba); -#define rgba_from_u32_lit_comp(h) { (((h)&0xff000000)>>24)/255.f, (((h)&0x00ff0000)>>16)/255.f, (((h)&0x0000ff00)>> 8)/255.f, (((h)&0x000000ff)>> 0)/255.f } +#define rgba_from_u32_lit_comp(h) \ +{ \ + (((h) & 0xff000000) >> 24) / 255.f, \ + (((h) & 0x00ff0000) >> 16) / 255.f, \ + (((h) & 0x0000ff00) >> 8 ) / 255.f, \ + (((h) & 0x000000ff) >> 0 ) / 255.f \ +} //////////////////////////////// //~ rjf: List Type Functions -internal void rng1s64_list_push(Arena *arena, Rng1S64List *list, Rng1S64 rng); +internal void rng1s64_list_push (Arena *arena, Rng1S64List *list, Rng1S64 rng); internal Rng1S64Array rng1s64_array_from_list(Arena *arena, Rng1S64List *list); diff --git a/code/base/memory_substrate.h b/code/base/memory_substrate.h index f9a1bd4..8b1336d 100644 --- a/code/base/memory_substrate.h +++ b/code/base/memory_substrate.h @@ -232,6 +232,9 @@ void* alloc_align( AllocatorInfo a, SSIZE size, SSIZE alignment ) { inline void* alloc( AllocatorInfo a, SSIZE size ) { + if (a.proc == nullptr) { + a = default_allocator(); + } return alloc_align( a, size, MD_DEFAULT_MEMORY_ALIGNMENT ); } diff --git a/code/base/space.h b/code/base/space.h index bd4c5de..1012a14 100644 --- a/code/base/space.h +++ b/code/base/space.h @@ -7,52 +7,52 @@ typedef enum Dimension { - Dimension_X, - Dimension_Y, - Dimension_Z, - Dimension_W, + Dimension_X, + Dimension_Y, + Dimension_Z, + Dimension_W, } Dimension; typedef enum Side { - Side_Invalid = -1, - Side_Min, - Side_Max, - Side_COUNT, + Side_Invalid = -1, + Side_Min, + Side_Max, + Side_COUNT, } Side; #define side_flip(s) ((Side)(!(s))) typedef enum Axis2 { - Axis2_Invalid = -1, - Axis2_X, - Axis2_Y, - Axis2_COUNT, + Axis2_Invalid = -1, + Axis2_X, + Axis2_Y, + Axis2_COUNT, } Axis2; #define axis2_flip(a) ((Axis2)(!(a))) typedef enum Corner { - Corner_Invalid = -1, - Corner_00, - Corner_01, - Corner_10, - Corner_11, - Corner_COUNT + Corner_Invalid = -1, + Corner_00, + Corner_01, + Corner_10, + Corner_11, + Corner_COUNT } Corner; typedef enum Dir2 { - Dir2_Invalid = -1, - Dir2_Left, - Dir2_Up, - Dir2_Right, - Dir2_Down, - Dir2_COUNT + Dir2_Invalid = -1, + Dir2_Left, + Dir2_Up, + Dir2_Right, + Dir2_Down, + Dir2_COUNT } Dir2; diff --git a/code/base/strings.c b/code/base/strings.c index 37dac52..40bc499 100644 --- a/code/base/strings.c +++ b/code/base/strings.c @@ -398,49 +398,64 @@ str8_skip_chop_whitespace(String8 string){ //////////////////////////////// //~ rjf: String Formatting & Copying -internal String8 -push_str8_cat(Arena *arena, String8 s1, String8 s2){ - String8 str; - str.size = s1.size + s2.size; - str.str = push_array_no_zero(arena, U8, str.size + 1); - MemoryCopy(str.str, s1.str, s1.size); - MemoryCopy(str.str + s1.size, s2.str, s2.size); - str.str[str.size] = 0; - return(str); +String8 +push_str8_cat(Arena* arena, String8 s1, String8 s2) +{ + String8 str; + str.size = s1.size + s2.size; + str.str = push_array_no_zero(arena, U8, str.size + 1); + MemoryCopy(str.str, s1.str, s1.size); + MemoryCopy(str.str + s1.size, s2.str, s2.size); + str.str[str.size] = 0; + return(str); } internal String8 push_str8_copy(Arena *arena, String8 s){ - String8 str; - str.size = s.size; - str.str = push_array_no_zero(arena, U8, str.size + 1); - MemoryCopy(str.str, s.str, s.size); - str.str[str.size] = 0; - return(str); + String8 str; + str.size = s.size; + str.str = push_array_no_zero(arena, U8, str.size + 1); + MemoryCopy(str.str, s.str, s.size); + str.str[str.size] = 0; + return(str); } internal String8 push_str8fv(Arena *arena, char *fmt, va_list args){ - va_list args2; - va_copy(args2, args); - U32 needed_bytes = raddbg_vsnprintf(0, 0, fmt, args) + 1; - String8 result = {0}; - result.str = push_array_no_zero(arena, U8, needed_bytes); - result.size = raddbg_vsnprintf((char*)result.str, needed_bytes, fmt, args2); - result.str[result.size] = 0; - va_end(args2); - return(result); + va_list args2; + va_copy(args2, args); + U32 needed_bytes = raddbg_vsnprintf(0, 0, fmt, args) + 1; + String8 result = {0}; + result.str = push_array_no_zero(arena, U8, needed_bytes); + result.size = raddbg_vsnprintf((char*)result.str, needed_bytes, fmt, args2); + result.str[result.size] = 0; + va_end(args2); + return(result); } internal String8 push_str8f(Arena *arena, char *fmt, ...){ - va_list args; - va_start(args, fmt); - String8 result = push_str8fv(arena, fmt, args); - va_end(args); - return(result); + va_list args; + va_start(args, fmt); + String8 result = push_str8fv(arena, fmt, args); + va_end(args); + return(result); } +// String8 +// str8__cat(String8 s1, String8 s2, AllocatorInfo ainfo) +// { +// String8 str; +// str.size = s1.size + s2.size; +// str.str = alloc_array(ainfo, U8, str.size + 1); + +// memory_copy(str.str, s1.str, s1.size); +// memory_copy(str.str + s1.size, s2.str, s2.size); +// str.str[str.size] = 0; + +// return str; +// } + //////////////////////////////// //~ rjf: String <=> Integer Conversions diff --git a/code/base/strings.h b/code/base/strings.h index 5950638..461de40 100644 --- a/code/base/strings.h +++ b/code/base/strings.h @@ -30,21 +30,21 @@ typedef struct String8 String8; struct String8 { - U8 *str; + U8* str; U64 size; }; typedef struct String16 String16; struct String16 { - U16 *str; + U16* str; U64 size; }; typedef struct String32 String32; struct String32 { - U32 *str; + U32* str; U64 size; }; @@ -54,22 +54,22 @@ struct String32 typedef struct String8Node String8Node; struct String8Node { - String8Node *next; + String8Node* next; String8 string; }; typedef struct String8MetaNode String8MetaNode; struct String8MetaNode { - String8MetaNode *next; - String8Node *node; + String8MetaNode* next; + String8Node* node; }; typedef struct String8List String8List; struct String8List { - String8Node *first; - String8Node *last; + String8Node* first; + String8Node* last; U64 node_count; U64 total_size; }; @@ -77,7 +77,7 @@ struct String8List typedef struct String8Array String8Array; struct String8Array { - String8 *v; + String8* v; U64 count; }; @@ -138,15 +138,15 @@ struct UnicodeDecode typedef struct FuzzyMatchRangeNode FuzzyMatchRangeNode; struct FuzzyMatchRangeNode { - FuzzyMatchRangeNode *next; + FuzzyMatchRangeNode* next; Rng1U64 range; }; typedef struct FuzzyMatchRangeList FuzzyMatchRangeList; struct FuzzyMatchRangeList { - FuzzyMatchRangeNode *first; - FuzzyMatchRangeNode *last; + FuzzyMatchRangeNode* first; + FuzzyMatchRangeNode* last; U64 count; U64 needle_part_count; U64 total_dim; @@ -224,10 +224,13 @@ internal String8 str8_skip_chop_whitespace(String8 string); //////////////////////////////// //~ rjf: String Formatting & Copying -internal String8 push_str8_cat (Arena *arena, String8 s1, String8 s2); -internal String8 push_str8_copy(Arena *arena, String8 s); -internal String8 push_str8fv (Arena *arena, char *fmt, va_list args); -internal String8 push_str8f (Arena *arena, char *fmt, ...); +internal String8 push_str8_cat (Arena* arena, String8 s1, String8 s2); +internal String8 push_str8_copy(Arena* arena, String8 s); +internal String8 push_str8fv (Arena* arena, char* fmt, va_list args); +internal String8 push_str8f (Arena* arena, char* fmt, ...); + +// String8 str8__cat(String8 s1, String8 s2, AllocatorInfo a); +// #define str8_cat(s1, s2, ...) str8__cat(s1, s2, (AllocatorInfo) {__VA_ARGS__}) //////////////////////////////// //~ rjf: String <=> Integer Conversions @@ -359,8 +362,8 @@ internal void str8_serial_begin (Arena *arena, String8List *srl); internal String8 str8_serial_end (Arena *arena, String8List *srl); internal void str8_serial_write_to_dst (String8List *srl, void *out); internal U64 str8_serial_push_align (Arena *arena, String8List *srl, U64 align); -internal void * str8_serial_push_size (Arena *arena, String8List *srl, U64 size); -internal void * str8_serial_push_data (Arena *arena, String8List *srl, void *data, U64 size); +internal void* str8_serial_push_size (Arena *arena, String8List *srl, U64 size); +internal void* str8_serial_push_data (Arena *arena, String8List *srl, void *data, U64 size); internal void str8_serial_push_data_list(Arena *arena, String8List *srl, String8Node *first); internal void str8_serial_push_u64 (Arena *arena, String8List *srl, U64 x); internal void str8_serial_push_u32 (Arena *arena, String8List *srl, U32 x); @@ -375,12 +378,12 @@ internal void str8_serial_push_string (Arena *arena, String8List *srl, Stri //////////////////////////////// //~ rjf: Deserialization Helpers -internal U64 str8_deserial_read (String8 string, U64 off, void *read_dst, U64 read_size, U64 granularity); -internal U64 str8_deserial_find_first_match (String8 string, U64 off, U16 scan_val); -internal void * str8_deserial_get_raw_ptr (String8 string, U64 off, U64 size); -internal U64 str8_deserial_read_cstr (String8 string, U64 off, String8 *cstr_out); -internal U64 str8_deserial_read_windows_utf16_string16(String8 string, U64 off, String16 *str_out); -internal U64 str8_deserial_read_block (String8 string, U64 off, U64 size, String8 *block_out); +internal U64 str8_deserial_read (String8 string, U64 off, void* read_dst, U64 read_size, U64 granularity); +internal U64 str8_deserial_find_first_match (String8 string, U64 off, U16 scan_val); +internal void* str8_deserial_get_raw_ptr (String8 string, U64 off, U64 size); +internal U64 str8_deserial_read_cstr (String8 string, U64 off, String8* cstr_out); +internal U64 str8_deserial_read_windows_utf16_string16(String8 string, U64 off, String16* str_out); +internal U64 str8_deserial_read_block (String8 string, U64 off, U64 size, String8* block_out); #define str8_deserial_read_array(string, off, ptr, count) str8_deserial_read((string), (off), (ptr), sizeof(*(ptr)) * (count), sizeof( *(ptr))) #define str8_deserial_read_struct(string, off, ptr) str8_deserial_read((string), (off), (ptr), sizeof(*(ptr)), sizeof( *(ptr))) diff --git a/code/base/toolchain.h b/code/base/toolchain.h index 6cf0fb8..e58ef69 100644 --- a/code/base/toolchain.h +++ b/code/base/toolchain.h @@ -7,31 +7,31 @@ typedef enum OperatingSystem { - OperatingSystem_Null, - OperatingSystem_Windows, - OperatingSystem_Linux, - OperatingSystem_Mac, - OperatingSystem_COUNT, + OperatingSystem_Null, + OperatingSystem_Windows, + OperatingSystem_Linux, + OperatingSystem_Mac, + OperatingSystem_COUNT, } OperatingSystem; typedef enum Architecture { - Architecture_Null, - Architecture_x64, - Architecture_x86, - Architecture_arm64, - Architecture_arm32, - Architecture_COUNT, + Architecture_Null, + Architecture_x64, + Architecture_x86, + Architecture_arm64, + Architecture_arm32, + Architecture_COUNT, } Architecture; typedef enum Compiler { - Compiler_Null, - Compiler_msvc, - Compiler_gcc, - Compiler_clang, - Compiler_COUNT, + Compiler_Null, + Compiler_msvc, + Compiler_gcc, + Compiler_clang, + Compiler_COUNT, } Compiler; diff --git a/code/metadesk.h b/code/metadesk.h index 7a33bc8..8e6b375 100644 --- a/code/metadesk.h +++ b/code/metadesk.h @@ -9,6 +9,7 @@ #include "base/context_cracking.h" #include "base/linkage.h" #include "base/macros.h" +#include "base/generic_macros.h" #include "base/platform.h" #include "base/namespace.h" @@ -20,6 +21,7 @@ MD_NS_BEGIN #include "base/memory_substrate.h" #include "base/arena.h" #include "base/space.h" +#include "base/math.h" #include "base/toolchain.h" #include "base/strings.h" #include "base/text.h"