diff --git a/all.h b/all.h index 59718c1..2a9199a 100644 --- a/all.h +++ b/all.h @@ -185,6 +185,13 @@ #define stmnt(S) do{ S }while(0) +#ifndef STATIC_ASSERT + #define STATIC_ASSERT3(cond, msg) typedef char static_assertion_##msg[(!!(cond))*2-1] + #define STATIC_ASSERT2(cond, line) STATIC_ASSERT3(cond, static_assertion_at_line_##line) + #define STATIC_ASSERT1(cond, line) STATIC_ASSERT2(cond, line) + #define STATIC_ASSERT(cond) STATIC_ASSERT1(cond, __LINE__) +#endif + #if !defined(assertBreak) # define assertBreak() (*(volatile int*)0 = 0) #endif @@ -260,6 +267,17 @@ typedef const char* str; typedef float f32; typedef double f64; +STATIC_ASSERT(sizeof(u8) == 1); +STATIC_ASSERT(sizeof(i8) == 1); +STATIC_ASSERT(sizeof(u16) == 2); +STATIC_ASSERT(sizeof(i16) == 2); +STATIC_ASSERT(sizeof(u32) == 4); +STATIC_ASSERT(sizeof(i32) == 4); +STATIC_ASSERT(sizeof(u64) == 8); +STATIC_ASSERT(sizeof(i64) == 8); +STATIC_ASSERT(sizeof(f32) == 4); +STATIC_ASSERT(sizeof(f64) == 8); + //typedef long double f80; only returning 8 bytes on my mac for some reason /* printf("u8 %d\n", (int)sizeof(u8) * 8); @@ -625,6 +643,9 @@ fn void u32ReverseArray(u32 arr[], u32 size); fn Vec2f32 vec2Add(Vec2 a, Vec2 b); fn Vec2f32 vec2Subtract(Vec2 a, Vec2 b); fn Vec2f32 vec2FromHexCube(Vec2 map_center, XYZ cube, f32 size, f32 border_thickness); +fn i32 i32Middle(i32 x, i32 y); +fn u32 u32Middle(u32 x, u32 y); +fn u64 u64SafeMul(u64 x, u64 y); ///// MEMORY (Arenas) #define ARENA_MAX GB(1) diff --git a/math.c b/math.c index 0aeceea..b889e2f 100644 --- a/math.c +++ b/math.c @@ -99,3 +99,40 @@ fn Vec2f32 vec2FromHexCube(Vec2 map_center, XYZ cube, f32 size, f32 border_thick }; } +/* from https://graphitemaster.github.io/aau/ +T midpoint(T x, T y) { + // U is the unsigned version of your type T, or same as T if T already unsigned + // digits is the number of numeric digits (not including sign) in your type T. + // std::numeric_limits::digits in C++ is helpful here. + U shift = digits - 1; + U difference = (U)x - (U)y; + U sign = y < x; + U half = (difference / 2) + (sign << shift) + (sign & difference); + T mid = (T)(x + half); + return mid; +} + */ + +fn i32 i32Middle(i32 x, i32 y) { + u32 shift = 31 - 1; + u32 difference = (u32)x - (u32)y; + u32 sign = y < x; + u32 half = (difference / 2) + (sign << shift) + (sign & difference); + i32 mid = (i32)(x + half); + return mid; +} + +fn u32 u32Middle(u32 x, u32 y) { + u32 shift = 32 - 1; + u32 difference = (u32)x - (u32)y; + u32 sign = y < x; + u32 half = (difference / 2) + (sign << shift) + (sign & difference); + u32 mid = (u32)(x + half); + return mid; +} + +fn u64 u64SafeMul(u64 x, u64 y) { + assert(!(y && x > MAX_u64/y)); + return x * y; +} + diff --git a/memory.c b/memory.c index b0655ac..92f0536 100644 --- a/memory.c +++ b/memory.c @@ -47,7 +47,7 @@ fn void* arenaAllocZero(Arena* a, u64 size) { } fn void* arenaAllocArraySized(Arena* arena, u64 elem_size, u64 count) { - return arenaAlloc(arena, elem_size * count); + return arenaAlloc(arena, u64SafeMul(elem_size, count)); } fn void arenaDealloc(Arena* arena, u64 size) {