Update convention yet again

Doing stuff inspired by Timothy Lottes's fixing c vod
This commit is contained in:
2025-10-11 00:18:44 -04:00
parent 2f797bf9e2
commit 274e0f31f5
10 changed files with 327 additions and 276 deletions
+98 -65
View File
@@ -3,65 +3,104 @@
# include "assert.h"
#endif
typedef unsigned char U8;
typedef signed char S8;
typedef unsigned short U16;
typedef signed short S16;
typedef unsigned int U32;
typedef signed int S32;
typedef unsigned char BYTE;
typedef unsigned int USIZE;
typedef signed int SSIZE;
typedef S8 B8;
typedef S16 B16;
typedef S32 B32;
enum {
false = 0,
true = 1,
true_overflow,
};
#define glue_impl(A, B) A ## B
#define glue(A, B) glue_impl(A, B)
#define stringify_impl(S) #S
#define stringify(S) stringify_impl(S)
#define tmpl(prefix, type) prefix ## _ ## type
#define align_(value) __attribute__((aligned (value))) // for easy alignment
#define expect_(x, y) __builtin_expect(x, y) // so compiler knows the common path
#define finline static inline __attribute__((always_inline)) // force inline
#define no_inline static __attribute__((noinline)) // force no inline [used in thread api]
#define R_ __restrict // pointers are either restricted or volatile and nothing else
#define V_ volatile // pointers are either restricted or volatile and nothing else
#define glue_impl(A, B) A ## B
#define glue(A, B) glue_impl(A, B)
#define stringify_impl(S) #S
#define stringify(S) stringify_impl(S)
#define tmpl(prefix, type) prefix ## _ ## type
#define local_persist static
#define internal static
#define global
#define gknown
#define offset_of(type, member) cast(SSIZE, & (((type*) 0)->member))
#define static_assert _Static_assert
#define typeof __typeof__
#define typeof_ptr(ptr) typeof((ptr)[0])
#define typeof_same(a, b) _Generic((a), typeof((b)): 1, default: 0)
#define def_R_(type) type*restrict type ## _R
#define def_V_(type) type*volatile type ## _V
#define def_ptr_set(type) def_R_(type); typedef def_V_(type)
#define def_tset(type) type; typedef def_ptr_set(type)
typedef __UINT8_TYPE__ def_tset(U1); typedef __UINT16_TYPE__ def_tset(U2); typedef __UINT32_TYPE__ def_tset(U4);
typedef __INT8_TYPE__ def_tset(S1); typedef __INT16_TYPE__ def_tset(S2); typedef __INT32_TYPE__ def_tset(S4);
typedef unsigned char def_tset(B1); typedef __UINT16_TYPE__ def_tset(B2); typedef __UINT32_TYPE__ def_tset(B4);
enum { false = 0, true = 1, true_overflow, };
#define u1_r(value) cast(U1_R, value)
#define u2_r(value) cast(U2_R, value)
#define u4_r(value) cast(U4_R, value)
#define u1_v(value) cast(U1_V, value)
#define u2_v(value) cast(U2_V, value)
#define u4_v(value) cast(U4_V, value)
#define u1_(value) cast(U1, value)
#define u2_(value) cast(U2, value)
#define u4_(value) cast(U4, value)
#define s1_(value) cast(S1, value)
#define s2_(value) cast(S2, value)
#define s4_(value) cast(S4, value)
#define alignas _Alignas
#define alignof _Alignof
#define byte_pad(amount, ...) BYTE glue(_PAD_, __VA_ARGS__) [amount]
#define farray_len(array) (SSIZE)sizeof(array) / size_of( typeof((array)[0]))
#define farray_init(type, ...) (type[]){__VA_ARGS__}
#define def_farray(type, len) type A ## len ## _ ## type[len]
#define def_farray_impl(_type, _len) _type A ## _len ## _ ## _type[_len]
#define def_farray(type, len) def_farray_impl(type, len)
#define def_enum(underlying_type, symbol) underlying_type symbol; enum symbol
#define def_struct(symbol) struct symbol symbol; struct symbol
#define def_union(symbol) union symbol symbol; union symbol
#define def_proc(symbol) symbol
#define opt_args(symbol, ...) &(symbol){__VA_ARGS__}
#define ret_type(type) type
#define local_persist static
#define internal static
#define global
#define gknown
#define offset_of(type, member) cast(SSIZE, & (((type*) 0)->member))
#define static_assert _Static_assert
#define typeof __typeof__
#define typeof_ptr(ptr) typeof(ptr[0])
#define typeof_same(a, b) _Generic((a), typeof((b)): 1, default: 0)
#define o_(field) offset_of(typeof_ptr(& field), filed))
#define alignas _Alignas
#define alignof _Alignof
#define byte_pad(amount, ...) B1 glue(_PAD_, __VA_ARGS__) [amount]
#define cast(type, data) ((type)(data))
#define pcast(type, data) * cast(type*, & (data))
#define nullptr cast(void*, 0)
#define size_of(data) cast(SSIZE, sizeof(data))
#define kilo(n) (cast(SSIZE, n) << 10)
#define mega(n) (cast(SSIZE, n) << 20)
#define giga(n) (cast(SSIZE, n) << 30)
#define tera(n) (cast(SSIZE, n) << 40)
#define size_of(data) cast(U4, sizeof(data))
#if 1
# define dbg_args(...) __VA_ARGS__
#else
# define dbg_args(...)
#endif
#define r_(ptr) cast(typeof_ptr(ptr)*R_, ptr)
#define v_(ptr) cast(typeof_ptr(ptr)*V_, ptr)
#define kilo(n) (cast(U4, n) << 10)
#define mega(n) (cast(U4, n) << 20)
#define giga(n) (cast(U4, n) << 30)
#define tera(n) (cast(U4, n) << 40)
#define dbg_args(...) __VA_ARGS__
#define sop_1(op, a, b) cast(U1, s1_(a) op s1_(b))
#define sop_2(op, a, b) cast(U2, s2_(a) op s2_(b))
#define sop_4(op, a, b) cast(U4, s4_(a) op s4_(b))
#define def_signed_op(id, op, width) finline U ## width id ## _s ## width(U ## width a, U ## width b) {return sop_ ## width(op, a, b); }
#define def_signed_ops(id, op) def_signed_op(id, op, 1) def_signed_op(id, op, 2) def_signed_op(id, op, 4)
def_signed_ops(add, +) def_signed_ops(sub, -)
def_signed_ops(mut, *) def_signed_ops(div, /)
def_signed_ops(gt, >) def_signed_ops(lt, <)
def_signed_ops(ge, >=) def_signed_ops(le, <=)
#define def_generic_sop(op, a, ...) _Generic((a), U1: op ## _s1, U2: op ## _s2, U4: op ## _s4) (a, __VA_ARGS__)
#define add_s(a,b) def_generic_sop(add,a,b)
#define sub_s(a,b) def_generic_sop(sub,a,b)
#define mut_s(a,b) def_generic_sop(mut,a,b)
#define gt_s(a,b) def_generic_sop(gt, a,b)
#define lt_s(a,b) def_generic_sop(lt, a,b)
#define ge_s(a,b) def_generic_sop(ge, a,b)
#define le_s(a,b) def_generic_sop(le, a,b)
#define span_iter(type, iter, m_begin, op, m_end) \
tmpl(Iter_Span,type) iter = { \
@@ -74,28 +113,28 @@ enum {
def_struct(tmpl( Span,type)) { type begin; type end; }; \
typedef def_struct(tmpl(Iter_Span,type)) { tmpl(Span,type) r; type cursor; }
typedef def_span(S32);
typedef def_span(U32);
typedef def_span(SSIZE);
typedef def_span(S4);
typedef def_span(U4);
typedef void def_proc(VoidFn) (void);
#define def_Slice(type) \
def_struct(tmpl(Slice,type)) { \
type* ptr; \
SSIZE len; \
}
typedef unsigned char def_tset(UTF8);
typedef def_struct(Str8) { UTF8* ptr; U4 len; }; typedef Str8 def_tset(Slice_UTF8);
typedef def_struct(Slice_Str8) { Str8* ptr; U4 len; };
#define txt(string_literal) (Str8){ (UTF8*) string_literal, size_of(string_literal) - 1 }
#define def_Slice(type) def_struct(tmpl(Slice,type)) { type* ptr; U4 len; }
#define slice_assert(slice) do { assert((slice).ptr != nullptr); assert((slice).len > 0); } while(0)
#define slice_end(slice) ((slice).ptr + (slice).len)
#define size_of_slice_type(slice) size_of( * (slice).ptr )
typedef def_Slice(void);
typedef def_Slice(BYTE);
#define slice_byte(slice) ((Slice_BYTE){cast(Byte*, (slice).ptr), (slice).len * size_of_slice_type(slice)})
#define slice_fmem(mem) ((Slice_BYTE){ mem, size_of(mem) })
typedef def_Slice(B1);
#define slice_byte(slice) ((Slice_B1){cast(B1*, (slice).ptr), (slice).len * size_of_slice_type(slice)})
#define slice_fmem(mem) ((Slice_B1){ mem, size_of(mem) })
void slice__copy(Slice_BYTE dest, SSIZE dest_typewidth, Slice_BYTE src, SSIZE src_typewidth);
void slice__zero(Slice_BYTE mem, SSIZE typewidth);
void slice__copy(Slice_B1 dest, U4 dest_typewidth, Slice_B1 src, U4 src_typewidth);
void slice__zero(Slice_B1 mem, U4 typewidth);
#define slice_copy(dest, src) do { \
static_assert(typeof_same(dest, src)); \
slice__copy(slice_byte(dest), size_of_slice_type(dest), slice_byte(src), size_of_slice_type(src)); \
@@ -106,13 +145,7 @@ void slice__zero(Slice_BYTE mem, SSIZE typewidth);
typeof((container).ptr) iter = (container).ptr; \
iter != slice_end(container); \
++ iter
#define slice_arg_from_array(type, ...) & (tmpl(Slice,type)) { \
#define slice_from_farray(type, ...) & (tmpl(Slice,type)) { \
.ptr = farray_init(type, __VA_ARGS__), \
.len = farray_len( farray_init(type, __VA_ARGS__)) \
}
typedef unsigned char UTF8;
typedef def_Slice(UTF8);
typedef Slice_UTF8 Str8;
typedef def_Slice(Str8);
#define txt(string_literal) (Str8){ (UTF8*) string_literal, size_of(string_literal) - 1 }
+7 -7
View File
@@ -6,17 +6,17 @@
#endif
typedef def_struct(Opts_farena) {
Str8 type_name;
SSIZE alignment;
Str8 type_name;
U4 alignment;
};
typedef def_struct(FArena) {
void* start;
SSIZE capacity;
SSIZE used;
U4 capacity;
U4 used;
};
FArena farena_make (Slice_BYTE mem);
void farena_init (FArena* arena, Slice_BYTE byte);
Slice_BYTE farena__push (FArena* arena, SSIZE amount, SSIZE type_width, Opts_farena* opts);
FArena farena_make (Slice_B1 mem);
void farena_init (FArena* arena, Slice_B1 byte);
Slice_B1 farena__push (FArena* arena, U4 amount, U4 type_width, Opts_farena* opts);
void farena_reset (FArena* arena);
void farena_rewind(FArena* arena, AllocatorSP save_point);
AllocatorSP farena_save (FArena arena);
+5 -6
View File
@@ -4,7 +4,7 @@
# include "math.h"
#endif
typedef def_enum(U32, gp_Commands) {
typedef def_enum(U4, gp_Commands) {
gcmd_Reset = 0b000,
gcmd_Polygon = 0b001,
gcmd_Line = 0b010,
@@ -73,19 +73,18 @@ typedef def_enum(U32, gp_Commands) {
#define gp_SetArea_TopLeft (gcmd_SetDrawArea_TopLeft << gcmd_offset)
#define gp_SetArea_BottomRight (gcmd_SetDrawArea_BotRight << gcmd_offset)
typedef def_struct(RGB8) { BYTE r; BYTE g; BYTE b; };
typedef def_struct(RGB8) { B1 r; B1 g; B1 b; };
#define rgba8(r, g, b) (RGB8){ r, g, b }
typedef BYTE gp_Pixel16[1];
typedef BYTE gp_Pixel24[3];
typedef B1 gp_Pixel16[1];
typedef B1 gp_Pixel24[3];
#define gp_b10_X 0
#define gp_b10_Y 10
#define gp_b16_X 0
#define gp_b16_Y 16
typedef def_struct(gp_Vec2) { U16 y; U16 x; };
typedef def_struct(gp_Vec2) { U2 y; U2 x; };
#if 1
void gp_screen_init(void) __asm__("gp_screen_init_asm");
+18 -15
View File
@@ -7,21 +7,24 @@
#define max(A, B) (((A) > (B)) ? (A) : (B))
#define clamp_bot(X, B) max(X, B)
typedef def_farray(S16, 2);
typedef def_farray(S32, 2);
// typedef def_farray(F32, 2);
typedef def_farray(S2, 2);
typedef def_farray(S2, 3);
typedef def_farray(S4, 2);
typedef def_farray(S4, 3);
typedef S2 A3A3_S2[3][3];
typedef def_struct(Extent_2S16) { S16 width; S16 height; };
typedef def_struct(Extent_2S32) { S32 width; S32 height; };
// typedef def_struct(Extent_2F32) { F32 width; F32 height; }
typedef def_struct(Vec_2S16) { S16 x; S16 y; };
typedef def_struct(Vec_2S32) { S32 x; S32 y; };
// typedef def_struct(Vec_2F32) { F32 x; F32 y; };
typedef def_struct(Range_2S16) { Vec_2S16 p0; Vec_2S16 p1; };
typedef def_struct(Range_2S32) { Vec_2S32 p0; Vec_2S32 p1; };
// typedef def_struct(Range_2F32) { Vec_2F32 p0; Vec_2F32 p1; };
typedef def_struct(Extent2_S2) { S2 width; S2 height; };
typedef def_struct(Extent2_S4) { S4 width; S4 height; };
typedef def_struct(V2_S2) { S2 x; S2 y; };
typedef def_struct(V2_S4) { S4 x; S4 y; };
typedef def_struct(V3_S2) { S2 x; S2 y; S2 z; S2 pad; };
typedef def_struct(V3_S4) { S4 x; S4 y; S4 z; S4 pad; };
typedef def_struct(R2_S2) { V2_S2 p0; V2_S2 p1; };
typedef def_struct(R2_S4) { V2_S4 p0; V2_S4 p1; };
typedef def_struct(Rect_S16) { S16 x; S16 y; S16 width; S16 height; };
typedef def_struct(Rect_S32) { S32 x; S32 y; S32 width; S32 height; };
typedef def_struct(Rect_S2) { S2 x; S2 y; S2 width; S2 height; };
typedef def_struct(Rect_S4) { S4 x; S4 y; S4 width; S4 height; };
#define vec_2s16(x, y) (Vec_2S16){ x, y }
typedef def_struct(M3_S2) { A3A3_S2 m; A3_S4 t; };
#define v2_s2(x, y) (V2_S2){ x, y }
+35 -35
View File
@@ -3,25 +3,25 @@
# include "dsl.h"
#endif
inline SSIZE align_pow2(SSIZE x, SSIZE b) {
inline U4 align_pow2(U4 x, U4 b) {
assert(b != 0);
assert((b & (b - 1)) == 0); // Check power of 2
return ((x + b - 1) & (~(b - 1)));
}
#define align_struct(type_width) ((SSIZE)(((type_width) + 3) & ~3))
#define align_struct(type_width) ((U4)(((type_width) + 3) & ~3))
#define assert_bounds(point, start, end) do { \
SSIZE pos_point = cast(SSIZE, point); \
SSIZE pos_start = cast(SSIZE, start); \
SSIZE pos_end = cast(SSIZE, end); \
assert(pos_start <= pos_point); \
assert(pos_point <= pos_end); \
U4 pos_point = cast(U4, point); \
U4 pos_start = cast(U4, start); \
U4 pos_end = cast(U4, end); \
assert(pos_start <= pos_point); \
assert(pos_point <= pos_end); \
} while(0)
// void* memory_copy (void* restrict dest, void const* restrict src, USIZE length);
// void* memory_copy_overlapping(void* restrict dest, void const* restrict src, USIZE length);
// B32 memory_zero (void* dest, USIZE length);
void* memory_copy (void* restrict dest, void const* restrict src, U4 length) __asm__("memcpy");
void* memory_copy_overlapping(void* restrict dest, void const* restrict src, U4 length);
B4 memory_zero (void* dest, U4 length);
#define check_nil(nil, p) ((p) == 0 || (p) == nil)
#define set_nil(nil, p) ((p) = nil)
@@ -43,7 +43,7 @@ inline SSIZE align_pow2(SSIZE x, SSIZE b) {
#define sll_queue_push_n(f, l, n, next) sll_queue_push_nz(0, f, l, n, next)
#pragma region Allocator Interface
typedef def_enum(U32, AllocatorOp) {
typedef def_enum(U4, AllocatorOp) {
AllocatorOp_Alloc_NoZero = 0, // If Alloc exist, so must No_Zero
AllocatorOp_Alloc,
AllocatorOp_Free,
@@ -55,7 +55,7 @@ typedef def_enum(U32, AllocatorOp) {
AllocatorOp_SavePoint,
AllocatorOp_Query, // Must always be implemented
};
typedef def_enum(U32, AllocatorQueryFlags) {
typedef def_enum(U4, AllocatorQueryFlags) {
AllocatorQuery_Alloc = (1 << 0),
AllocatorQuery_Free = (1 << 1),
// Wipe the allocator's state
@@ -72,14 +72,14 @@ typedef struct AllocatorProc_Out AllocatorProc_Out;
typedef void def_proc(AllocatorProc) (AllocatorProc_In In, AllocatorProc_Out* Out);
typedef def_struct(AllocatorSP) {
AllocatorProc* type_sig;
SSIZE slot;
U4 slot;
};
struct AllocatorProc_In {
void* data;
SSIZE requested_size;
SSIZE alignment;
U4 requested_size;
U4 alignment;
union {
Slice_BYTE old_allocation;
Slice_B1 old_allocation;
AllocatorSP save_point;
};
AllocatorOp op;
@@ -87,28 +87,28 @@ struct AllocatorProc_In {
};
struct AllocatorProc_Out {
union {
Slice_BYTE allocation;
Slice_B1 allocation;
AllocatorSP save_point;
};
AllocatorQueryFlags features;
SSIZE left; // Contiguous memory left
SSIZE max_alloc;
SSIZE min_alloc;
B32 continuity_break; // Whether this allocation broke continuity with the previous (address space wise)
U4 left; // Contiguous memory left
U4 max_alloc;
U4 min_alloc;
B4 continuity_break; // Whether this allocation broke continuity with the previous (address space wise)
byte_pad(4);
};
typedef def_struct(AllocatorInfo) {
AllocatorProc* proc;
void* data;
};
static_assert(size_of(AllocatorSP) <= size_of(Slice_BYTE));
static_assert(size_of(AllocatorSP) <= size_of(Slice_B1));
typedef def_struct(AllocatorQueryInfo) {
AllocatorSP save_point;
AllocatorQueryFlags features;
SSIZE left; // Contiguous memory left
SSIZE max_alloc;
SSIZE min_alloc;
B32 continuity_break; // Whether this allocation broke continuity with the previous (address space wise)
U4 left; // Contiguous memory left
U4 max_alloc;
U4 min_alloc;
B4 continuity_break; // Whether this allocation broke continuity with the previous (address space wise)
byte_pad(4);
};
static_assert(size_of(AllocatorProc_Out) == size_of(AllocatorQueryInfo));
@@ -117,20 +117,20 @@ static_assert(size_of(AllocatorProc_Out) == size_of(AllocatorQueryInfo));
AllocatorQueryInfo allocator_query(AllocatorInfo ainfo);
void mem_free (AllocatorInfo ainfo, Slice_BYTE mem);
void mem_free (AllocatorInfo ainfo, Slice_B1 mem);
void mem_reset (AllocatorInfo ainfo);
void mem_rewind (AllocatorInfo ainfo, AllocatorSP save_point);
AllocatorSP mem_save_point(AllocatorInfo ainfo);
typedef def_struct(Opts_mem_alloc) { SSIZE alignment; B32 no_zero; byte_pad(4); };
typedef def_struct(Opts_mem_grow) { SSIZE alignment; B32 no_zero; byte_pad(4); };
typedef def_struct(Opts_mem_shrink) { SSIZE alignment; };
typedef def_struct(Opts_mem_resize) { SSIZE alignment; B32 no_zero; byte_pad(4); };
typedef def_struct(Opts_mem_alloc) { U4 alignment; B4 no_zero; byte_pad(4); };
typedef def_struct(Opts_mem_grow) { U4 alignment; B4 no_zero; byte_pad(4); };
typedef def_struct(Opts_mem_shrink) { U4 alignment; };
typedef def_struct(Opts_mem_resize) { U4 alignment; B4 no_zero; byte_pad(4); };
Slice_BYTE mem__alloc (AllocatorInfo ainfo, SSIZE size, Opts_mem_alloc* opts);
Slice_BYTE mem__grow (AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_grow* opts);
Slice_BYTE mem__resize(AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_resize* opts);
Slice_BYTE mem__shrink(AllocatorInfo ainfo, Slice_BYTE mem, SSIZE size, Opts_mem_shrink* opts);
Slice_B1 mem__alloc (AllocatorInfo ainfo, U4 size, Opts_mem_alloc* opts);
Slice_B1 mem__grow (AllocatorInfo ainfo, Slice_B1 mem, U4 size, Opts_mem_grow* opts);
Slice_B1 mem__resize(AllocatorInfo ainfo, Slice_B1 mem, U4 size, Opts_mem_resize* opts);
Slice_B1 mem__shrink(AllocatorInfo ainfo, Slice_B1 mem, U4 size, Opts_mem_shrink* opts);
#define mem_alloc(ainfo, size, ...) mem__alloc (ainfo, size, opt_args(Opts_mem_alloc, __VA_ARGS__))
#define mem_grow(ainfo, mem, size, ...) mem__grow (ainfo, mem, size, opt_args(Opts_mem_grow, __VA_ARGS__))