+ - Converted procedure argument names from PascalCase to snake_case + - Converted struct field names from PascalCase to snake_case ++*/ #module_parameters( + /// Statically link to HandmadeMath. STATIC := true, - SIMD := true, - UNITS := (enum { radians; degrees; turns; }).radians + /// Enable SIMD support. + SIMD := true, + /// Angle units to use. + UNITS : enum { radians; degrees; turns; } = .radians ); #scope_export; diff --git a/ext/luajit/module.jai b/ext/luajit/module.jai index 6dd9b9e..cd7d641 100644 --- a/ext/luajit/module.jai +++ b/ext/luajit/module.jai @@ -1,3 +1,7 @@ +/* + Module luajit provides bindings for the LuaJIT C + library (2.1.1744318430) +*/ #module_parameters(STATIC := true); #if STATIC { diff --git a/ext/objc/module.jai b/ext/objc/module.jai new file mode 100644 index 0000000..5788182 --- /dev/null +++ b/ext/objc/module.jai @@ -0,0 +1,47 @@ +#scope_export; + +UInt :: u64; + +Id :: *object; +Class :: *class; +Sel :: *selector; + +Bool :: u8; + +True : Bool : 1; +False : Bool : 0; + +msg_send :: () #foreign objc "objc_msgSend"; +msg_send_super :: () #foreign objc "objc_msgSend_super"; +msg_send_fpret :: () #foreign objc "objc_msgSend_fpret"; +msg_send_stret :: () #foreign objc "objc_msgSend_stret"; + +get_class :: (name: *u8) -> Class #foreign objc "objc_getClass"; + +sel_get_name :: (sel: Sel) -> *u8 #foreign objc "sel_getName"; +sel_register_name :: (str: *u8) -> Sel #foreign objc "sel_registerName"; +sel_get_uid :: (str: *u8) -> Sel #foreign objc "sel_getUid"; + +obj_get_class :: (obj: Id) -> Class #foreign objc "object_getClass"; +obj_set_class :: (obj: Id, cls: Class) -> Class #foreign objc "object_setClass"; +obj_is_class :: (obj: Id) -> Bool #foreign objc "object_isClass"; +obj_get_class_name :: (obj: Id) -> *u8 #foreign objc "object_getClassName"; +obj_copy :: (obj: Id, size: u64) -> Id #foreign objc "object_copy"; +obj_dispose :: (obj: Id) -> Id #foreign objc "object_dispose"; + +class_get_name :: (cls: Class) -> *u8 #foreign objc "class_getName"; +class_get_super :: (cls: Class) -> Class #foreign objc "class_getSuperclass"; + +#scope_module; + +class :: struct {}; +object :: struct {}; +method :: struct {}; +ivar :: struct {}; +category :: struct {}; +protocol :: struct {}; +selector :: struct {}; + +objc :: #library,system,link_always,no_dll "libobjc"; + +#import "jc"; diff --git a/ext/raylib/module.jai b/ext/raylib/module.jai index b080be5..503dc97 100644 --- a/ext/raylib/module.jai +++ b/ext/raylib/module.jai @@ -1,3 +1,10 @@ +/* + Module raylib provides bindings for the raylib C + library (v5.5). + + Supported platforms: Windows, Mac, Linux +*/ + #module_parameters(STATIC := true); #scope_export diff --git a/ext/remotery/module.jai b/ext/remotery/module.jai index c956ee4..23ec8f2 100644 --- a/ext/remotery/module.jai +++ b/ext/remotery/module.jai @@ -1,3 +1,7 @@ +/* + Module remotery provides bindings for the Remotery + CPU/GPU profiling library. +*/ #module_parameters(STATIC := true); #if STATIC { diff --git a/encoding/base64.jai b/fmt/base64.jai similarity index 64% rename from encoding/base64.jai rename to fmt/base64.jai index faa8a52..0f2268b 100644 --- a/encoding/base64.jai +++ b/fmt/base64.jai @@ -1,16 +1,16 @@ -#module_parameters(RUN_TESTS := false); +#module_parameters(RunTests := false); -base64_encode :: (str: string, $for_url := false) -> string, bool { - enc, ok := base64_encode(str.([]u8), for_url); +Base64Encode :: (str: string, $for_url := false) -> string, bool { + enc, ok := Base64Encode(str.([]u8), for_url); return enc.(string), ok; } -base64_decode :: (str: string) -> string, bool { - enc, ok := base64_decode(str.([]u8)); +Base64Decode :: (str: string) -> string, bool { + enc, ok := Base64Decode(str.([]u8)); return enc.(string), ok; } -base64_encode :: ($$data: []u8, $for_url := false) -> []u8, bool { +Base64Encode :: ($$data: []u8, $for_url := false) -> []u8, bool { if data.count == 0 return .[], false; #if for_url { @@ -21,7 +21,7 @@ base64_encode :: ($$data: []u8, $for_url := false) -> []u8, bool { } padded := strings.contains(data.(string), Padding_Character); - encoded := basic.NewArray(encoded_length(data.count, padded), u8, true); + encoded := basic.NewArray(EncodedLength(data.count, padded), u8, true); src_idx := 0; dst_idx := 0; @@ -70,7 +70,7 @@ base64_encode :: ($$data: []u8, $for_url := false) -> []u8, bool { return encoded, true; } -base64_decode :: (data: []u8) -> []u8, bool { +Base64Decode :: (data: []u8) -> []u8, bool { if !data.count return .[], false; lookup :: (c: u8) -> u8 #expand { @@ -144,17 +144,20 @@ base64_decode :: (data: []u8) -> []u8, bool { return decoded, true; } -#scope_file; + +#scope_file Padding_Character :: #char "="; Encoding_Url :: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; Encoding_Standard :: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -encoded_length :: (count: int, with_padding := false) -> int { +EncodedLength :: (count: int, with_padding := false) -> int { if with_padding then return (count + 2) / 3 * 4; return (count * 8 + 5) / 6; } +#import "jc"; + basic :: #import "Basic"; // @future strings :: #import "String"; // @future @@ -163,47 +166,45 @@ strings :: #import "String"; // @future // TESTS // ---------------------------------------------------------- -#if RUN_TESTS #run { - test :: #import "jc/meta/test"; - - test.run("encodes", (t) => { +#if RunTests #run { + Test("encodes", t => { str :: "Hello, World"; - encoded, ok := base64_encode(str); - test.expect(t, ok, "'%' did not properly encode!", str); + encoded, ok := Base64Encode(str); + Expect(ok, "'%' did not properly encode!", str); expected :: "SGVsbG8sIFdvcmxk"; - test.expect(t, encoded == expected, "wanted: '%', received: '%'", expected, encoded); + Expect(encoded == expected, "wanted: '%', received: '%'", expected, encoded); }); - test.run("decodes", (t) => { + Test("decodes", (t) => { encoded_str :: "SGVsbG8sIFdvcmxk"; - decoded, ok := base64_decode(encoded_str); - test.expect(t, ok, "'%' did not properly decode!", encoded_str); + decoded, ok := Base64Decode(encoded_str); + Expect(ok, "'%' did not properly decode!", encoded_str); expected :: "Hello, World"; - test.expect(t, decoded == expected, "wanted: '%', received: '%'", expected, decoded); + Expect(decoded == expected, "wanted: '%', received: '%'", expected, decoded); }); - test.run("encodes/decodes padding", (t) => { + Test("encodes/decodes padding", (t) => { str :: "VkdocGN5QnBjeUJ6YjIxbGRHaHBibWNnYlc5eVpTQmpiMjF3YkdWNExDQnBkQ0J6YUc5MWJHUWdhR0YyWlNCd1lXUmthVzVuUHc9PQ=="; - first_decode, f_ok := base64_decode(str); - test.expect(t, f_ok, "first decode failed!"); + first_decode, f_ok := Base64Decode(str); + Expect(f_ok, "first decode failed!"); - re_encode, r_ok := base64_encode(first_decode); - test.expect(t, r_ok, "re-encode failed!"); + re_encode, r_ok := Base64Encode(first_decode); + Expect(r_ok, "re-encode failed!"); - second_decode, s_ok := base64_decode(re_encode); - test.expect(t, s_ok, "second decode failed!"); + second_decode, s_ok := Base64Decode(re_encode); + Expect(s_ok, "second decode failed!"); for 0..first_decode.count - 1 { - test.expect(t, first_decode[it] == second_decode[it], "Decoded byte '%' did not match expected output", it); + Expect(first_decode[it] == second_decode[it], "Decoded byte '%' did not match expected output", it); } }); - test.run("encodes/decodes struct", (t) => { + Test("encodes/decodes struct", (t) => { Foo :: struct { first: u64; second: u64; @@ -220,14 +221,14 @@ strings :: #import "String"; // @future enc_builder: basic.String_Builder; basic.print_to_builder(*enc_builder, "%,%,%,%", foo.first, foo.second, foo.third, foo.forth); - encoded, e_ok := base64_encode(basic.builder_to_string(*enc_builder)); - test.expect(t, e_ok, "Encode of structure failed!"); + encoded, e_ok := Base64Encode(basic.builder_to_string(*enc_builder)); + Expect(e_ok, "Encode of structure failed!"); - decoded, d_ok := base64_decode(encoded); - test.expect(t, d_ok, "Decode of encoded structure failed!"); + decoded, d_ok := Base64Decode(encoded); + Expect(d_ok, "Decode of encoded structure failed!"); parts := strings.split(decoded, ","); - test.expect(t, parts.count == 4, "Invalid number of parts after decode: %", parts.count); + Expect(parts.count == 4, "Invalid number of parts after decode: %", parts.count); bar: Foo = ---; @@ -237,17 +238,17 @@ strings :: #import "String"; // @future for info.members { value, ok := strings.parse_int(*parts[it_index], u64); - test.expect(t, ok, "Integer parse of value % ('%') failed!", it_index, parts[it_index]); + Expect(ok, "Integer parse of value % ('%') failed!", it_index, parts[it_index]); offset := (ptr + it.offset_in_bytes).(*u64); offset.*= value; } } - test.expect(t, foo.first == bar.first, "Foo.first (%, %) didn't match between encoding/decoding!", foo.first, bar.first); - test.expect(t, foo.second == bar.second, "Foo.second (%, %) didn't match between encoding/decoding!", foo.second, bar.second); - test.expect(t, foo.third == bar.third, "Foo.third (%, %) didn't match between encoding/decoding!", foo.third, bar.third); - test.expect(t, foo.forth == bar.forth, "Foo.forth (%, %) didn't match between encoding/decoding!", foo.forth, bar.forth); + Expect(foo.first == bar.first, "Foo.first (%, %) didn't match between encoding/decoding!", foo.first, bar.first); + Expect(foo.second == bar.second, "Foo.second (%, %) didn't match between encoding/decoding!", foo.second, bar.second); + Expect(foo.third == bar.third, "Foo.third (%, %) didn't match between encoding/decoding!", foo.third, bar.third); + Expect(foo.forth == bar.forth, "Foo.forth (%, %) didn't match between encoding/decoding!", foo.forth, bar.forth); }); } diff --git a/hash/module.jai b/hash/module.jai deleted file mode 100644 index c29efe3..0000000 --- a/hash/module.jai +++ /dev/null @@ -1,10 +0,0 @@ -#module_parameters(RUN_TESTS := false, WITH_SUBMODULES := false); - -#scope_export; - -#if WITH_SUBMODULES { - using #import "jc/hash/murmur"(RUN_TESTS); - using #import "jc/hash/xxhash"(RUN_TESTS); - using #import "jc/hash/table"(RUN_TESTS); -} - diff --git a/hash/murmur.jai b/hash/murmur.jai deleted file mode 100644 index 5fa7fcc..0000000 --- a/hash/murmur.jai +++ /dev/null @@ -1,58 +0,0 @@ -#module_parameters(RUN_TESTS := false); - -// Implementation: Demetri Spanos (github.com/demetri/scribbles) -// Jai Port: Jesse Coyle (github.com/Zilarrezko) - -MurMur_Seed : u32 : 0xa3c91521; - -murmur32 :: inline (s: string, seed: u32 = MurMur_Seed) -> u32 { - return murmur32(s.data, s.count, seed); -} - -murmur32 :: inline (x: $T, seed: u32 = MurMur_Seed) -> u32 { - return murmur32(*x, size_of(T), seed); -} - -murmur32 :: (key: *void, len: int, seed: u32 = MurMur_Seed) -> u32 { - scrambler :: (k: u32) -> u32 #expand { - c1: u32 : 0xcc9e2d51; - c2: u32 : 0x1b873593; - r1: int : 15; - k = k*c1; - k = k <<< r1; - k = k*c2; - return k; - } - - h: u32 = seed; - tail: *u8 = cast(*u8)key + (len/4)*4; - p: *u32 = cast(*u32)key; - - while cast(*u8)p < tail { - k: u32 = <
> 16; h *= 0x85ebca6b; - h ^= h >> 13; h *= 0xc2b2ae35; - h ^= h >> 16; - return h; -} diff --git a/hash/table.jai b/hash/table.jai deleted file mode 100644 index daa9b8b..0000000 --- a/hash/table.jai +++ /dev/null @@ -1,191 +0,0 @@ -#module_parameters(RUN_TESTS := false); - -// Dead simple key-value pair type (aka. hash table or hash map) -Table :: struct(Key: Type, Value: Type) { - allocator: Allocator; - slots: [..]Slot; - free_slots: [..]int; - count: int; - - Slot :: struct { - hash: u32 = invalid_hash; - key: Key = ---; - value: Value = ---; - } - - hash_proc :: hash.murmur32; - invalid_hash :: (0x8000_dead).(u32); // @note(judah): I'm curious what values would hit this hash on accident - number_of_items_to_allocate_initially :: 16; // @note(judah): must be a power of two -} - -get :: (t: *Table, key: t.Key) -> t.Value, bool { - slot, ok := find_slot(t, get_hash(t, key)); - if !ok { - return mem.zero_of(t.Value), false; - } - - return slot.value, true; -} - -set :: (t: *Table, key: t.Key, value: t.Value) { - hash := get_hash(t, key); - slot, exists := find_slot(t, hash); - if !exists { - slot = create_or_reuse_slot(t); - slot.hash = hash; - } - - slot.key = key; - slot.value = value; -} - -exists :: (t: *Table, key: t.Key) -> bool { - _, exists := find_slot(t, get_hash(t, key)); - return exists; -} - -// @note(judah): we use 'delete' instead of 'remove' because it's a keyword... -delete :: (t: *Table, key: t.Key) -> t.Value, bool { - slot, ok, idx := find_slot(t, get_hash(t, key)); - if !ok return mem.zero_of(t.Value), false; - - last_value := slot.value; - mark_slot_for_reuse(t, idx); - - return last_value, true; -} - -reset :: (t: *Table) { - t.count = 0; - t.slots.count = 0; - t.free_slots.count = 0; -} - -for_expansion :: (t: *Table, body: Code, flags: For_Flags) #expand { - #assert (flags & .POINTER == 0) "cannot iterate by pointer"; - for <=(flags & .REVERSE == .REVERSE) slot: t.slots if slot.hash != t.invalid_hash { - `it := slot.value; - `it_index := slot.key; - #insert,scope(body)(break = break slot) body; - } -} - - -#scope_file; - -get_hash :: inline (t: *Table, key: t.Key) -> u32 { - hash := t.hash_proc(key); - basic.assert(hash != t.invalid_hash, "key % collided with invalid hash marker (%)", key, t.invalid_hash); - return hash; -} - -find_slot :: (t: *Table, hash: u32) -> *t.Slot, bool, int { - for * t.slots if it.hash == hash { - return it, true, it_index; - } - - return null, false, -1; -} - -create_or_reuse_slot :: (t: *Table) -> *t.Slot { - inline try_lazy_init(t); - - if t.free_slots.count > 0 { - slot_idx := t.free_slots[t.free_slots.count - 1]; - t.free_slots.count -= 1; - return *t.slots[slot_idx]; - } - - if t.slots.allocated == 0 { - array.resize(*t.slots, t.number_of_items_to_allocate_initially); - } - else if t.slots.count >= t.slots.allocated { - array.resize(*t.slots, mem.next_power_of_two(t.slots.allocated)); - } - - slot := array.append(*t.slots); - t.count = t.slots.count; - return slot; -} - -mark_slot_for_reuse :: (t: *Table, index: int) { - inline try_lazy_init(t); - - t.count -= 1; - t.slots[index] = .{ hash = t.invalid_hash }; - - array.append(*t.free_slots, index); -} - -try_lazy_init :: inline (t: *Table) { - mem.lazy_set_allocator(t); - mem.lazy_set_allocator(*t.slots); - mem.lazy_set_allocator(*t.free_slots); -} - -mem :: #import "jc/memory"; -array :: #import "jc/array"; -hash :: #import "jc/hash"; - -basic :: #import "Basic"; // @future - - -// ---------------------------------------------------------- -// TESTS -// ---------------------------------------------------------- - -#if RUN_TESTS #run { - test :: #import "jc/meta/test"; - - test.run("basic operations", t => { - ITERATIONS :: 64; - - values: Table(int, int); - for 0..ITERATIONS { - set(*values, it, it * it); - } - - for 0..ITERATIONS { - v, ok := get(*values, it); - test.expect(t, v == it * it); - } - - for 0..ITERATIONS if it % 2 == 0 { - _, ok := delete(*values, it); - test.expect(t, ok); - } - - for 0..ITERATIONS if it % 2 == 0 { - _, ok := get(*values, it); - test.expect(t, !ok); - } - }); - - test.run("free slots", t => { - values: Table(int, int); - - set(*values, 1, 100); - set(*values, 2, 200); - set(*values, 3, 300); - test.expect(t, values.count == 3); - test.expect(t, values.slots.allocated == values.number_of_items_to_allocate_initially); - - // deleting something that doesn't exist should do nothing - _, ok := delete(*values, 0); - test.expect(t, !ok); - test.expect(t, values.count == 3); - - delete(*values, 2); - test.expect(t, values.count == 2); - }); - - test.run("iteration", t => { - values: Table(int, int); - - for 0..10 set(*values, it, it * it); - test.expect(t, values.count == 11); - - for v, k: values test.expect(t, v == k * k); - for < v, k: values test.expect(t, v == k * k); - }); -} diff --git a/math/common.jai b/math/common.jai index 20fef1a..9423642 100644 --- a/math/common.jai +++ b/math/common.jai @@ -88,7 +88,7 @@ min :: (x: float, y: float) -> float { } abs :: (v: $T) -> T -#modify { return meta.type_is_scalar(T), "Only accepting scalar types, integer and float"; } { +#modify { return type_is_scalar(T), "Only accepting scalar types, integer and float"; } { return ifx v < 0 then -v else v; } @@ -110,6 +110,6 @@ sign :: (x: float) -> float { #scope_file +#import "jc"; // sin, cos -meta :: #import "jc/meta"; math :: #import "Math"; diff --git a/math/ease.jai b/math/ease.jai index 2b56f1e..c902888 100644 --- a/math/ease.jai +++ b/math/ease.jai @@ -28,7 +28,7 @@ Transition :: enum { @Note: Progress must be between 0.0 and 1.0 */ ease :: (progress: $T, $$ease: Ease = .linear, $$transition: Transition = .in) -> T -#modify { return meta.type_is_float(T); } +#modify { return type_is_float(T); } { p := progress; @@ -85,8 +85,6 @@ ease :: (progress: $T, $$ease: Ease = .linear, $$transition: Transition = .in) - #scope_file; - -meta :: #import "jc/meta"; - +#import "jc"; math :: #import "Math"; // @future basic :: #import "Basic"; // @future diff --git a/math/mat.jai b/math/mat.jai index 3f81380..c5cb8fd 100644 --- a/math/mat.jai +++ b/math/mat.jai @@ -156,7 +156,32 @@ make_look_at :: (camera: Vec3, at: Vec3, up_vector: Vec3) -> Mat4 } operator* :: (a: Mat4, b: Mat4) -> Mat4 { - return mul_sse(a, b); + #if CPU == .X64 { + return mul_sse(a, b); + } + else { + r: Mat4 = ---; + r._00 = a._00*b._00 + a._01*b._10 + a._02*b._20 + a._03*b._30; + r._01 = a._00*b._01 + a._01*b._11 + a._02*b._21 + a._03*b._31; + r._02 = a._00*b._02 + a._01*b._12 + a._02*b._22 + a._03*b._32; + r._03 = a._00*b._03 + a._01*b._13 + a._02*b._23 + a._03*b._33; + + r._10 = a._10*b._00 + a._11*b._10 + a._12*b._20 + a._13*b._30; + r._11 = a._10*b._01 + a._11*b._11 + a._12*b._21 + a._13*b._31; + r._12 = a._10*b._02 + a._11*b._12 + a._12*b._22 + a._13*b._32; + r._13 = a._10*b._03 + a._11*b._13 + a._12*b._23 + a._13*b._33; + + r._20 = a._20*b._00 + a._21*b._10 + a._22*b._20 + a._23*b._30; + r._21 = a._20*b._01 + a._21*b._11 + a._22*b._21 + a._23*b._31; + r._22 = a._20*b._02 + a._21*b._12 + a._22*b._22 + a._23*b._32; + r._23 = a._20*b._03 + a._21*b._13 + a._22*b._23 + a._23*b._33; + + r._30 = a._30*b._00 + a._31*b._10 + a._32*b._20 + a._33*b._30; + r._31 = a._30*b._01 + a._31*b._11 + a._32*b._21 + a._33*b._31; + r._32 = a._30*b._02 + a._31*b._12 + a._32*b._22 + a._33*b._32; + r._33 = a._30*b._03 + a._31*b._13 + a._32*b._23 + a._33*b._33; + return r; + } } // Note(Jesse): If you want to make it symmetric go ahead, I usually don't @@ -465,25 +490,6 @@ mul_sse :: inline (a: Mat4, b: Mat4) -> Mat4 { movups [*r + 48], r0; } - // r._00 = a._00*b._00 + a._01*b._10 + a._02*b._20 + a._03*b._30; - // r._01 = a._00*b._01 + a._01*b._11 + a._02*b._21 + a._03*b._31; - // r._02 = a._00*b._02 + a._01*b._12 + a._02*b._22 + a._03*b._32; - // r._03 = a._00*b._03 + a._01*b._13 + a._02*b._23 + a._03*b._33; - - // r._10 = a._10*b._00 + a._11*b._10 + a._12*b._20 + a._13*b._30; - // r._11 = a._10*b._01 + a._11*b._11 + a._12*b._21 + a._13*b._31; - // r._12 = a._10*b._02 + a._11*b._12 + a._12*b._22 + a._13*b._32; - // r._13 = a._10*b._03 + a._11*b._13 + a._12*b._23 + a._13*b._33; - - // r._20 = a._20*b._00 + a._21*b._10 + a._22*b._20 + a._23*b._30; - // r._21 = a._20*b._01 + a._21*b._11 + a._22*b._21 + a._23*b._31; - // r._22 = a._20*b._02 + a._21*b._12 + a._22*b._22 + a._23*b._32; - // r._23 = a._20*b._03 + a._21*b._13 + a._22*b._23 + a._23*b._33; - - // r._30 = a._30*b._00 + a._31*b._10 + a._32*b._20 + a._33*b._30; - // r._31 = a._30*b._01 + a._31*b._11 + a._32*b._21 + a._33*b._31; - // r._32 = a._30*b._02 + a._31*b._12 + a._32*b._22 + a._33*b._32; - // r._33 = a._30*b._03 + a._31*b._13 + a._32*b._23 + a._33*b._33; return r; } // Note(Jesse): This procedure will crash if it tries to store or access an element across a cache line @@ -568,50 +574,47 @@ mul_sse_aligned :: inline (dst: *Mat4, a: *Mat4, b: *Mat4) { } #scope_file; - is_aligned :: (p: *void, bound: int) -> bool { return cast(int)p & (bound - 1) == 0; } -#if #exists(RUN_TESTS) #run { - test :: #import "jc/meta/test"; - - test.run(basic.tprint("%: Mat2", UNITS), t => { +#if RUN_TESTS #run { + Test(basic.tprint("%: Mat2", UNITS), t => { { identity := m2_identity; a: Mat2 = Mat2.{.[1, 2, 3, 4]}; - test.expect(t, a*identity == a); + Expect(a*identity == a); } { rotator := rotation_mat2(from_rad(PI)); v2 := v2f(1.0, 0.5); v2 = rotator*v2; - test.expect(t, v2_eq(v2, v2f(-1.0, -0.5))); + Expect(v2_eq(v2, v2f(-1.0, -0.5))); v2 = rotator*v2; - test.expect(t, v2_eq(v2, v2f(1.0, 0.5))); + Expect(v2_eq(v2, v2f(1.0, 0.5))); } { m := m2(1.0, 3.0, 2.0, 2.0); det := determinate(m); - test.expect(t, det == -4); + Expect(det == -4); } { rotator := rotation_mat2(from_rad(PI)); inv_rotator := inverse(rotator); v2 := v2f(0.1, 1.0); - test.expect(t, inv_rotator*rotator == m2_identity); - test.expect(t, inv_rotator*(rotator*v2) == v2); + Expect(inv_rotator*rotator == m2_identity); + Expect(inv_rotator*(rotator*v2) == v2); v2 = rotator*v2; v2 = inv_rotator*v2; } }); - test.run(basic.tprint("%: Mat4", UNITS), t => { + Test(basic.tprint("%: Mat4", UNITS), t => { { identity := m4_identity; a: Mat4 = Mat4.{.[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]}; - test.expect(t, a*identity == a); + Expect(a*identity == a); } { UP_VECTOR :: Vec3.{.[0.0, 1.0, 0.0]}; @@ -623,16 +626,16 @@ is_aligned :: (p: *void, bound: int) -> bool { translator := translate(v3f(10.0, 5.0, 2.0)); v3 := v3f(1.0, 2.0, 1.0); v4 := v4f(v3, 1.0); - test.expect(t, v4 == v4f(1.0, 2.0, 1.0, 1.0)); - test.expect(t, translator*v4 == v4f(11.0, 7.0, 3.0, 1.0)); + Expect(v4 == v4f(1.0, 2.0, 1.0, 1.0)); + Expect(translator*v4 == v4f(11.0, 7.0, 3.0, 1.0)); } { rotator := rotation_mat4(v3f(0.0, 1.0, 0.0), from_rad(PI)); v4 := v4f(1.0, 0.5, 0.1, 1.0); v4 = rotator*v4; - test.expect(t, v4_eq(v4, v4f(-1.0, 0.5, -0.1, 1.0))); + Expect(v4_eq(v4, v4f(-1.0, 0.5, -0.1, 1.0))); v4 = rotator*v4; - test.expect(t, v4_eq(v4, v4f(1.0, 0.5, 0.1, 1.0))); + Expect(v4_eq(v4, v4f(1.0, 0.5, 0.1, 1.0))); } { rotator_x := rotation_mat4(v3f(1.0, 0.0, 0.0), from_rad(0.4*PI)); @@ -645,7 +648,7 @@ is_aligned :: (p: *void, bound: int) -> bool { inv_rotator := inverse(rotator); v4 := v4f(0.1, 1.0, 0.0, 1.0); - test.expect(t, inv_rotator*rotator == m4_identity); + Expect(inv_rotator*rotator == m4_identity); v4 = rotator*v4; v4 = inv_rotator*v4; diff --git a/math/module.jai b/math/module.jai index d0b97e6..dd34aed 100644 --- a/math/module.jai +++ b/math/module.jai @@ -5,22 +5,7 @@ RUN_TESTS := false ); -#assert meta.type_is_scalar(RECT_TYPE); - -// @todo(judah): dumb we can't use meta.range_for here. - -U8_Min, U8_Max :: #run meta.lo_for(u8), #run meta.hi_for(u8); -U16_Min, U16_Max :: #run meta.lo_for(u16), #run meta.hi_for(u16); -U32_Min, U32_Max :: #run meta.lo_for(u32), #run meta.hi_for(u32); -U64_Min, U64_Max :: #run meta.lo_for(u64), #run meta.hi_for(u64); - -S8_Min, S8_Max :: #run meta.lo_for(s8), #run meta.hi_for(s8); -S16_Min, S16_Max :: #run meta.lo_for(s16), #run meta.hi_for(s16); -S32_Min, S32_Max :: #run meta.lo_for(s32), #run meta.hi_for(s32); -S64_Min, S64_Max :: #run meta.lo_for(s64), #run meta.hi_for(s64); - -F32_Min, F32_Max :: #run meta.lo_for(float32), #run meta.hi_for(float32); -F64_Min, F64_Max :: #run meta.lo_for(float64), #run meta.hi_for(float64); +#assert type_is_scalar(RECT_TYPE); #load "common.jai"; #load "vec.jai"; @@ -30,7 +15,8 @@ F64_Min, F64_Max :: #run meta.lo_for(float64), #run meta.hi_for(float64); #scope_module; -meta :: #import "jc/meta"; +#import "jc"; + math :: #import "Math"; // @future basic :: #import "Basic"; // @future diff --git a/math/vec.jai b/math/vec.jai index 27e8a42..6ad9442 100644 --- a/math/vec.jai +++ b/math/vec.jai @@ -103,17 +103,17 @@ Vec :: struct(N: int, T: Type) } operator [] :: (v: Vec, $$idx: int) -> v.T #no_abc { - meta.check_bounds(idx, v.N); + CheckBounds(idx, v.N); return v.components[idx]; } operator *[] :: (v: *Vec, $$idx: int) -> *v.T #no_abc { - meta.check_bounds(idx, v.N); + CheckBounds(idx, v.N); return *v.components[idx]; } operator []= :: (v: *Vec, $$idx: int, value: v.T) #no_abc { - meta.check_bounds(idx, v.N); + CheckBounds(idx, v.N); v.components[idx] = value; } @@ -187,7 +187,7 @@ operator / :: inline (l: Vec, r: Vec(l.N, l.T)) -> Vec(l.N, l.T) #no_abc { } operator + :: inline (l: Vec, r: $R) -> Vec(l.N, l.T) #no_abc #symmetric -#modify { return meta.type_is_scalar(R), "type is not integer or float"; } { +#modify { return type_is_scalar(R), "type is not integer or float"; } { res: Vec(l.N, l.T) = ---; #if l.N <= 4 { res.x = l.x + r; @@ -202,7 +202,7 @@ operator + :: inline (l: Vec, r: $R) -> Vec(l.N, l.T) #no_abc #symmetric } operator - :: inline (l: Vec, r: $R) -> Vec(l.N, l.T) #no_abc -#modify { return meta.type_is_scalar(R), "type is not integer or float"; } { +#modify { return type_is_scalar(R), "type is not integer or float"; } { res: Vec(l.N, l.T) = ---; #if l.N <= 4 { res.x = l.x - r; @@ -216,7 +216,7 @@ operator - :: inline (l: Vec, r: $R) -> Vec(l.N, l.T) #no_abc return res; } operator - :: inline (l: $R, r: Vec) -> Vec(l.N, l.T) #no_abc -#modify { return meta.type_is_scalar(R), "type is not integer or float"; } { +#modify { return type_is_scalar(R), "type is not integer or float"; } { res: Vec(l.N, l.T) = ---; #if l.N <= 4 { res.x = l - r.x; @@ -245,7 +245,7 @@ operator- :: inline(v: Vec) -> Vec(v.N, v.T) #no_abc { } operator * :: inline (l: Vec, r: $R) -> Vec(l.N, l.T) #no_abc #symmetric -#modify { return meta.type_is_scalar(R), "type is not integer or float"; } { +#modify { return type_is_scalar(R), "type is not integer or float"; } { res: Vec(l.N, l.T) = ---; #if l.N <= 4 { res.x = l.x*r; @@ -260,7 +260,7 @@ operator * :: inline (l: Vec, r: $R) -> Vec(l.N, l.T) #no_abc #symmetric } operator / :: inline (l: Vec, r: $R) -> Vec(l.N, l.T) #no_abc -#modify { return meta.type_is_scalar(R), "type is not integer or float"; } { +#modify { return type_is_scalar(R), "type is not integer or float"; } { res: Vec(l.N, l.T) = ---; #if l.N <= 4 { res.x = l.x/r; @@ -486,7 +486,7 @@ reflect :: (v: Vec2, p: Vec2) -> Vec2 #no_abc { } round :: (v: Vec($N, $T)) -> Vec(N, T) #no_abc -#modify { return meta.type_is_float(T), "Used non-float vector on round"; } { +#modify { return type_is_float(T), "Used non-float vector on round"; } { r: Vec(N, T) = ---; n := N - 1; while n >= 0 { @@ -507,31 +507,31 @@ Vec4 :: Vec(4, float); Quat :: #type,distinct Vec4; // Note(Jesse): I Had to make this distinct, otherwise operators stomp on eachother v2f :: (x: $T = 0, y: T = 0) -> Vec2 -#modify { return meta.type_is_float(T), "use v2i for integer arguments"; } +#modify { return type_is_float(T), "use v2i for integer arguments"; } #expand { return .{ x = x, y = y }; } v2i :: (x: $T = 0, y: T = 0) -> Vec(2, T) -#modify { return meta.type_is_integer(T), "use v2f for float arguments"; } +#modify { return type_is_integer(T), "use v2f for float arguments"; } #expand { return .{ x = x, y = y }; } v3f :: (x: $T = 0, y: T = 0, z: T = 0) -> Vec3 -#modify { return meta.type_is_float(T), "use v3i for integer arguments"; } +#modify { return type_is_float(T), "use v3i for integer arguments"; } #expand { return .{ x = x, y = y, z = z }; } v3i :: (x: $T = 0, y: T = 0, z: T = 0) -> Vec(3, T) -#modify { return meta.type_is_integer(T), "use v3f for float arguments"; } +#modify { return type_is_integer(T), "use v3f for float arguments"; } #expand { return .{ x = x, y = y, z = z }; } v4f :: (x: $T = 0, y: T = 0, z: T = 0, w: T = 0) -> Vec4 -#modify { return meta.type_is_float(T), "use v4i for integer arguments"; } +#modify { return type_is_float(T), "use v4i for integer arguments"; } #expand { return .{ x = x, y = y, z = z, w = w }; } @@ -541,7 +541,7 @@ v4f :: (v: Vec3, $$w: float) -> Vec4 #expand { } v4i :: (x: $T = 0, y: T = 0, z: T = 0, w: T = 0) -> Vec(4, T) -#modify { return meta.type_is_integer(T), "use v4f for float arguments"; } +#modify { return type_is_integer(T), "use v4f for float arguments"; } #expand { return .{ x = x, y = y, z = z, w = w }; } @@ -661,76 +661,74 @@ same_dir :: (a: Vec3, b: Vec3) -> bool { #scope_file; #if RUN_TESTS #run,stallable { - test :: #import "jc/meta/test"; - - test.run(basic.tprint("%: Vec2", UNITS), t => { + Test(basic.tprint("%: Vec2", UNITS), t => { { a: Vec2 = v2f(0.0, 1.0); b: Vec2 = v2f(1.0, 2.0); - test.expect(t, a + b == v2f(1.0, 3.0)); - test.expect(t, b - a == v2f(1.0, 1.0)); - test.expect(t, a*b == v2f(0.0, 2.0)); - test.expect(t, a/b == v2f(0.0, 0.5)); + Expect(a + b == v2f(1.0, 3.0)); + Expect(b - a == v2f(1.0, 1.0)); + Expect(a*b == v2f(0.0, 2.0)); + Expect(a/b == v2f(0.0, 0.5)); } { a: Vec(2, int) = v2i(2, 1); b: Vec(2, int) = v2i(1, 0); - test.expect(t, a + b == v2i(3, 1)); - test.expect(t, b - a == v2i(-1, -1)); - test.expect(t, a*b == v2i(2, 0)); - test.expect(t, b/a == v2i(0, 0)); + Expect(a + b == v2i(3, 1)); + Expect(b - a == v2i(-1, -1)); + Expect(a*b == v2i(2, 0)); + Expect(b/a == v2i(0, 0)); } { a: Vec2 = v2f(2.3, -4.1); b: Vec2 = v2f(1.0, 3.6); c := min(a, b); - test.expect(t, c == v2f(1.0, -4.1)); + Expect(c == v2f(1.0, -4.1)); c = max(a, b); - test.expect(t, c == v2f(2.3, 3.6)); + Expect(c == v2f(2.3, 3.6)); } }); - test.run(basic.tprint("%: Vec3", UNITS), t => { + Test(basic.tprint("%: Vec3", UNITS), t => { { a: Vec3 = v3f(0.0, 1.0, 2.0); b: Vec3 = v3f(1.0, 2.0, 3.0); - test.expect(t, a + b == v3f(1.0, 3.0, 5.0)); - test.expect(t, b - a == v3f(1.0, 1.0, 1.0)); - test.expect(t, a*b == v3f(0.0, 2.0, 6.0)); - test.expect(t, a/b == v3f(0.0, 0.5, 0.66666667)); + Expect(a + b == v3f(1.0, 3.0, 5.0)); + Expect(b - a == v3f(1.0, 1.0, 1.0)); + Expect(a*b == v3f(0.0, 2.0, 6.0)); + Expect(a/b == v3f(0.0, 0.5, 0.66666667)); a = v3f(1.0, 1.0, 0.0); b = v3f(1.0, 0.0, 0.0); - test.expect(t, reflect(a, b) == v3f(1.0, -1.0, 0.0)); - test.expect(t, round(v3f(1.2, 1.7, 1.5)) == v3f(1.0, 2.0, 2.0)); - test.expect(t, round(v3f(-1.2, -1.7, -1.5)) == v3f(-1.0, -2.0, -2.0)); + Expect(reflect(a, b) == v3f(1.0, -1.0, 0.0)); + Expect(round(v3f(1.2, 1.7, 1.5)) == v3f(1.0, 2.0, 2.0)); + Expect(round(v3f(-1.2, -1.7, -1.5)) == v3f(-1.0, -2.0, -2.0)); a = v3f(1.0, 0.0, 0.0); b = v3f(0.0, 1.0, 0.0); - test.expect(t, cross(a, b) == v3f(0.0, 0.0, 1.0)); + Expect(cross(a, b) == v3f(0.0, 0.0, 1.0)); } { a: Vec3 = v3f(2.3, 4.1, 9.0); b: Vec3 = v3f(1.0, -3.6, 5.0); c := min(a, b); - test.expect(t, c == v3f(1.0, -3.6, 5.0)); + Expect(c == v3f(1.0, -3.6, 5.0)); c = max(a, b); - test.expect(t, c == v3f(2.3, 4.1, 9.0)); + Expect(c == v3f(2.3, 4.1, 9.0)); } }); - test.run(basic.tprint("%: Vec4", UNITS), t => { + Test(basic.tprint("%: Vec4", UNITS), t => { a: Vec4 = v4f(2.25, 1.0, 2.0, 1.0); b: Vec4 = v4f(4.0, 2.0, 3.0, 1.0); - test.expect(t, a + b == v4f(6.25, 3.0, 5.0, 2.0)); - test.expect(t, b - a == v4f(1.75, 1.0, 1.0, 0.0)); - test.expect(t, a*b == v4f(9.0, 2.0, 6.0, 1.0)); - test.expect(t, a/b == v4f(0.5625, 0.5, 2.0/3.0, 1.0)); + Expect(a + b == v4f(6.25, 3.0, 5.0, 2.0)); + Expect(b - a == v4f(1.75, 1.0, 1.0, 0.0)); + Expect(a*b == v4f(9.0, 2.0, 6.0, 1.0)); + Expect(a/b == v4f(0.5625, 0.5, 2.0/3.0, 1.0)); }); - test.run(basic.tprint("%: VecN", UNITS), t => { + Test(basic.tprint("%: VecN", UNITS), t => { a: Vec(16, float); b: Vec(16, float); for *a { @@ -739,59 +737,59 @@ same_dir :: (a: Vec3, b: Vec3) -> bool { for *b { it.* = xx(it_index + 1); } - test.expect(t, a + b == Vec(16, float).{.[1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0, 21.0, 23.0, 25.0, 27.0, 29.0, 31.0]}); - test.expect(t, b - a == Vec(16, float).{.[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]}); - test.expect(t, a*b == Vec(16, float).{.[0.0, 2.0, 6.0, 12.0, 20.0, 30.0, 42.0, 56.0, 72.0, 90.0, 110.0, 132.0, 156.0, 182.0, 210.0, 240.0]}); + Expect(a + b == Vec(16, float).{.[1.0, 3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0, 19.0, 21.0, 23.0, 25.0, 27.0, 29.0, 31.0]}); + Expect(b - a == Vec(16, float).{.[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]}); + Expect(a*b == Vec(16, float).{.[0.0, 2.0, 6.0, 12.0, 20.0, 30.0, 42.0, 56.0, 72.0, 90.0, 110.0, 132.0, 156.0, 182.0, 210.0, 240.0]}); - test.expect(t, min(a, b) == a); - test.expect(t, max(a, b) == b); - test.expect(t, max(a, 12) == Vec(16, float).{.[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 12.0, 12.0, 12.0]}); - test.expect(t, min(a, 13.2) == Vec(16, float).{.[13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 14.0, 15.0]}); - test.expect(t, clamp(a, 7.25, 12.0) == Vec(16, float).{.[7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 8, 9, 10, 11, 12, 12, 12, 12]}); + Expect(min(a, b) == a); + Expect(max(a, b) == b); + Expect(max(a, 12) == Vec(16, float).{.[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 12.0, 12.0, 12.0]}); + Expect(min(a, 13.2) == Vec(16, float).{.[13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 13.2, 14.0, 15.0]}); + Expect(clamp(a, 7.25, 12.0) == Vec(16, float).{.[7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 8, 9, 10, 11, 12, 12, 12, 12]}); a1: Vec(16, float) = Vec(16, float).{.[1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 4.7, -1.2, -1.0, -1.5, 11.2, 14.0, 15.0, 14.0, 15.0, 65536.2]}; - test.expect(t, ceil(a1) == Vec(16, float).{.[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 5.0, -1.0, -1.0, -1.0, 12.0, 14.0, 15.0, 14.0, 15.0, 65537]}); - test.expect(t, floor(a1) == Vec(16, float).{.[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 4.0, -2.0, -1.0, -2.0, 11.0, 14.0, 15.0, 14.0, 15.0, 65536]}); + Expect(ceil(a1) == Vec(16, float).{.[2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 5.0, -1.0, -1.0, -1.0, 12.0, 14.0, 15.0, 14.0, 15.0, 65537]}); + Expect(floor(a1) == Vec(16, float).{.[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 4.0, -2.0, -1.0, -2.0, 11.0, 14.0, 15.0, 14.0, 15.0, 65536]}); - test.expect(t, dot(a, b) == 1360.0); - test.expect(t, abs(a) == a); + Expect(dot(a, b) == 1360.0); + Expect(abs(a) == a); c := a; for *c { // Check making every other component negative if it_index%2 == 0 then it.* = -it.*; } - test.expect(t, abs(c) == a); - test.expect(t, float_eq(length(normalize(a)), 1)); - test.expect(t, a + 2 == Vec(16, float).{.[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]}); - test.expect(t, a - 2 == Vec(16, float).{.[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]}); - test.expect(t, a*2 == Vec(16, float).{.[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]}); + Expect(abs(c) == a); + Expect(float_eq(length(normalize(a)), 1)); + Expect(a + 2 == Vec(16, float).{.[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]}); + Expect(a - 2 == Vec(16, float).{.[-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]}); + Expect(a*2 == Vec(16, float).{.[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]}); }); - test.run(basic.tprint("%: Quat", UNITS), t => { + Test(basic.tprint("%: Quat", UNITS), t => { qi := quat_identity; q: Quat = rotation_quat(from_rad(PI), v3f(0.0, 1.0, 0.0)); q2: Quat = rotation_quat(from_rad(PI), v3f(1.0, 0.0, 1.0)); qc := conjugate(q); inv_q := inverse(q); - test.expect(t, q*inv_q == qi); + Expect(q*inv_q == qi); q1 := quat(2, 0, 0, 0); q2 = quat(1, 1, -1, 0); c := q1*q2*conjugate(q1); - test.expect(t, float_eq(c.w, 0.0)); + Expect(float_eq(c.w, 0.0)); q = rotation_quat(from_rad(PI/4.0), v3f(0.0, 0.0, 1.0)); p := v3f(2.0, 0.0, 0.0); c1 := q*quat(p, 0)*conjugate(q); c2 := q*p; - test.expect(t, v3_eq(c2, v3f(math.sqrt(2.0), math.sqrt(2.0), 0.0))); - test.expect(t, v3_eq(c1.xyz, c2)); + Expect(v3_eq(c2, v3f(math.sqrt(2.0), math.sqrt(2.0), 0.0))); + Expect(v3_eq(c1.xyz, c2)); q = rotation_quat(from_rad(PI), v3f(0.0, 1.0, 0.0)); m := rotation_mat4(q); p1 := v4f(2.0, 0.0, 0.0, 1.0); - test.expect(t, v4_eq(m*p1, v4f(-2.0, 0.0, -0.0, 1.0))); + Expect(v4_eq(m*p1, v4f(-2.0, 0.0, -0.0, 1.0))); q1 = rotation_quat(from_rad(PI), v3f(0.0, 1.0, 0.0)); q2 = rotation_quat(from_rad(2.0*PI), v3f(0.0, 1.0, 0.0)); diff --git a/memory/allocators.jai b/memory/allocators.jai deleted file mode 100644 index e25ba3d..0000000 --- a/memory/allocators.jai +++ /dev/null @@ -1,139 +0,0 @@ -#module_parameters(RUN_TESTS := false); - -make_crash_allocator :: () -> Allocator { - return .{ proc = crash_allocator_proc }; -} - -crash_allocator_proc :: (mode: Allocator_Mode, size: s64, old_size: s64, old_memory: rawptr, allocator_data: rawptr) -> rawptr { - message: string; - - if mode == { - case .ALLOCATE; - message = basic.tprint("Attempt to allocate % byte(s) using the crash allocator!", size); - case .RESIZE; - message = basic.tprint("Attempt to resize (from % to % byte(s)) using the crash allocator!", old_size, size); - case .FREE; - message = basic.tprint("Attempt to free % byte(s) using the crash allocator!", size); - } - - loc := meta.get_stack_trace_caller_location(); - basic.assert(false, message, loc = loc); - debug_break(); - return null; -} - - -Arena :: struct { - memory: rawptr; - memory_size: u64; - offset: u64; -} - -init_arena :: (a: *Arena, memory: rawptr, size: u64) { - a.memory = memory; - a.memory_size = size; - a.offset = 0; -} - -make_arena_allocator :: (arena: *Arena) -> Allocator { - return .{ - data = arena, - proc = xx arena_allocator_proc, - }; -} - -Extended_Allocator_Mode :: enum { - using Allocator_Mode; - - request_memory :: Allocator_Mode.ALLOCATE; - resize_memory :: Allocator_Mode.RESIZE; - release_memory :: Allocator_Mode.FREE; - first_time_used :: Allocator_Mode.STARTUP; - release_everything :: Allocator_Mode.SHUTDOWN; - - reset_state; - save_point; // should return *s64 - restore_save_point; // 'old_size' will be the dereferenced return value of 'save_point' -} - -arena_allocator_proc :: (mode: Extended_Allocator_Mode, size: s64, old_size: s64, old_memory: rawptr, allocator_data: rawptr) -> rawptr { - arena := allocator_data.(*Arena); - if mode == { - case .request_memory; - return arena_alloc(arena, size); - - case .resize_memory; - if old_memory == null { - return arena_alloc(arena, size); - } - - if size == 0 { - return null; - } - - if size == old_size { - return old_memory; - } - - new_memory := arena_alloc(arena, size); - memcpy(new_memory, old_memory, old_size); - return new_memory; - - case .save_point; - return *arena.offset; - - case .restore_save_point; - arena.offset = old_size.(u64); - } - - return null; -} - -arena_alloc :: (a: *Arena, count: int, alignment := mem.Default_Align, loc := #caller_location) -> rawptr { - basic.assert(a.memory != null, "arena: not initialized", loc = loc); - basic.assert(mem.power_of_two(alignment)); - - end := a.memory.(*u8) + a.offset; - ptr := mem.align_to(end.(int), alignment); - total_size := (count + ptr.(*u8) - end.(*u8)).(u64); - - basic.assert(a.offset + total_size <= a.memory_size, "arena: out of memory", loc = loc); - a.offset += total_size; - - return ptr.(rawptr); -} - - -#scope_file; - -#import "jc"; - -mem :: #import "jc/memory"; -meta :: #import "jc/meta"; - -basic :: #import "Basic"; // @future - - -// ---------------------------------------------------------- -// TESTS -// ---------------------------------------------------------- - -#if RUN_TESTS #run { - test :: #import "jc/meta/test"; - - test.run("arena:basic", t => { - memory := mem.request_memory(1 * mem.Kilobyte); - defer mem.release_memory(memory); - - arena: Arena; - init_arena(*arena, memory, 1 * mem.Kilobyte); - - context.allocator = make_arena_allocator(*arena); - save_point := mem.allocator_save(); - - i := mem.request_memory(int); - basic.assert(i != null); - basic.assert(arena.offset == size_of(int)); - mem.allocator_restore(save_point); - }); -} diff --git a/memory/buffer.jai b/memory/buffer.jai deleted file mode 100644 index 837f3f2..0000000 --- a/memory/buffer.jai +++ /dev/null @@ -1,51 +0,0 @@ -#module_parameters(RUN_TESTS := false); - -Buffer :: struct { - allocator: Allocator; - - data: [..]byte; - count: int; -} - -append :: inline (buf: *Buffer, ptr: rawptr, size: int) { - inline mem.lazy_set_allocator(buf); - - free_space := ensure_buffer_has_room(buf, size); - memcpy(free_space, ptr, size); - buf.size += size; -} - -append :: inline (buf: *Buffer, data: []byte) { - append(buf, data.data, data.count); -} - -append :: inline (buf: *Buffer, ptr: *$T) { - append(buf, ptr, size_of(T)); -} - -reset :: inline (buf: *Buffer) { - buf.count = 0; -} - -#scope_file; - -#import "jc"; - -array :: #import "jc/array"; -mem :: #import "jc/memory"; -meta :: #import "jc/meta"; - -ensure_buffer_has_room :: (buf: *Buffer, count: int) -> *u8 { - ptr := buf.data.data + buf.count; - if buf.count + count >= buf.data.allocated { - array.resize(*buf.data, buf.data.allocated * 2); - ptr = buf.data.data + buf.count; - buf.data.count = buf.data.allocated; - } - - return ptr; -} - -#if RUN_TESTS #run { - test :: #import "jc/meta/test"; -} diff --git a/memory/module.jai b/memory/module.jai deleted file mode 100644 index 41baffc..0000000 --- a/memory/module.jai +++ /dev/null @@ -1,223 +0,0 @@ -#module_parameters(RUN_TESTS := false, WITH_SUBMODULES := true); - -#scope_export; - -Kilobyte :: 1024; -Megabyte :: 1024 * Kilobyte; -Gigabyte :: 1024 * Megabyte; - -Default_Align :: #run 2 * align_of(*void); - -align_of :: ($T: Type) -> int #expand { - return #run -> int { - info := type_info(struct{ p: u8; t: T; }); - return info.members[1].offset_in_bytes.(int); - }; -} - -default_of :: ($T: Type) -> T #expand { - default: T; - return default; -} - -undefined_of :: ($T: Type) -> T #expand { - uninit: T = ---; - return uninit; -} - -zero_of :: ($T: Type) -> T #expand { - zero := undefined_of(T); - memset(*zero, 0, size_of(T)); - return zero; -} - -bitcast :: ($T: Type, expr: Code) -> T #expand { - value := expr; - return (*value).(*T).*; -} - -power_of_two :: (x: int) -> bool { - if x == 0 return false; - return x & (x - 1) == 0; -} - -next_power_of_two :: (x: int) -> int #no_aoc { - basic.assert(power_of_two(x), "value (%) must be a power of two", x); - - // Bit twiddling hacks next power of two - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; - x |= x >> 32; - - return x + 1; -} - -align_to :: (ptr: int, align: int = Default_Align) -> int { - basic.assert(power_of_two(align), "alignment must be a power of two"); - - p := ptr; - mod := p & (align - 1); - if mod != 0 then p += align - mod; - return p; -} - -init_or_zero :: inline (ptr: *$T, custom_init: (*T) = null) { - if custom_init != null { - custom_init(ptr); - } - - initializer :: initializer_of(T); - #if initializer { - inline initializer(ptr); - } - else { - memset(ptr, 0, size_of(T)); - } -} - -allocator_reset :: () { - allocator := context.allocator; - allocator.proc(xx Extended_Allocator_Mode.reset_state, 0, 0, null, allocator.data); -} - -allocator_save :: () -> int { - allocator := context.allocator; - return allocator.proc(xx Extended_Allocator_Mode.save_point, 0, 0, null, allocator.data).(*int).*; -} - -allocator_restore :: (save_point: int) { - allocator := context.allocator; - allocator.proc(xx Extended_Allocator_Mode.restore_save_point, 0, save_point, null, allocator.data); -} - -request_memory :: (size: int, align := Default_Align) -> rawptr { - allocator := context.allocator; - aligned_size := align_to(size, align); - return allocator.proc(xx Extended_Allocator_Mode.request_memory, aligned_size.(int), 0, null, allocator.data); -} - -request_memory :: ($T: Type, reserved := 0, $init := true) -> [..]T -#modify { - ok, info := meta.type_is_array(T); - if ok && info.array_type == .RESIZABLE { - T = compiler.get_type(info.element_type); - return true; - } - - return false; -} -{ - size := size_of(T) * basic.max(reserved, 0); - data := request_memory(size, align_of(T)).(*T); - #if init if size != 0 { - memset(data, 0, size); - } - - arr: [..]T; - arr.data = data; - arr.count = 0; - arr.allocated = size / size_of(T); - arr.allocator = context.allocator; - return arr; -} - -request_memory :: ($T: Type, $init := true) -> *T -#modify { return !meta.type_is_array(T); } -{ - ptr := request_memory(size_of(T), align_of(T)).(*T); - #if init init_or_zero(ptr); - return ptr; -} - -release_memory :: inline (ptr: rawptr) { - allocator := context.allocator; - allocator.proc(xx Extended_Allocator_Mode.release_memory, 0, 0, ptr, allocator.data); -} - -release_memory :: inline (arr: []$T) { - release_memory(arr.data.(*rawptr)); -} - -release_memory :: inline (arr: [..]$T) { - release_memory(arr.data.(*rawptr),, allocator = arr.allocator); -} - -release_memory :: inline (str: string) { - release_memory(str.data.(*rawptr)); -} - -// @todo(judah): why can't we use $T/struct{ allocator: Allocator }? -lazy_set_allocator :: (thing: *$T, allocator := context.allocator) #modify { - info := T.(*Type_Info_Struct); - - ok := false; - if info.type == .STRUCT ok = true; - - if ok for info.members if it.name == "allocator" && it.type == Allocator.(*Type_Info) { - ok = true; - break; - } - - return ok, "can only set allocator on struct with allocator field or dynamic array"; -} #expand { - if thing.allocator.proc == null { - thing.allocator = allocator; - } -} - -lazy_set_allocator :: (array: *[..]$T, allocator := context.allocator) #expand { - if array.allocator.proc == null { - array.allocator = allocator; - } -} - -#if WITH_SUBMODULES { - using #import "jc/memory/allocators"(RUN_TESTS); -} - - -#scope_file; - -#import "jc"; - -meta :: #import "jc/meta"; - -basic :: #import "Basic"; // @future -compiler :: #import "Compiler"; // @future - - -// ---------------------------------------------------------- -// TESTS -// ---------------------------------------------------------- - -#if RUN_TESTS #run { - test :: #import "jc/meta/test"; - - test.run("request_memory:dynamic arrays", (t) => { - a1 := request_memory([..]int); - defer release_memory(a1); - - test.expect(t, a1.count == 0); - test.expect(t, a1.allocated == 0); - - basic.array_add(*a1, 10, 20, 30); - test.expect(t, a1.count == 3); - test.expect(t, a1.allocated != 0, "%", a1.allocated); - - a2 := request_memory([..]int, 8); - defer release_memory(a2); - - test.expect(t, a2.count == 0); - test.expect(t, a2.allocated == 8); - }); - - test.run("request_memory:values", (t) => { - v1 := request_memory(int); - defer release_memory(v1); - - test.expect(t, v1.* == 0); - }); -} diff --git a/meta/macros.jai b/meta/macros.jai deleted file mode 100644 index 3905abd..0000000 --- a/meta/macros.jai +++ /dev/null @@ -1,287 +0,0 @@ -#module_parameters(RUN_TESTS := false); - -/* - Allows structs to be copied and assigned inline. - - For example, copying a Vector3 while changing a single value: - old := Vector3.{ 10, 20, 30 }; // 10, 20, 30 - new := with(old, .{ y = -20 }); // 10, -20, 30 -*/ -with :: (old: $T, $fields: Code, location := #caller_location) -> T -#modify { return T.(*Type_Info).type == .STRUCT, "with can only be used on structs"; } -#expand { - #insert,scope() -> string { - using compiler; - - ensure :: (cond: bool, message: string, args: ..Any, loc := location) { - if !cond compiler_report(basic.tprint(message, ..args), loc); - } - - b: basic.String_Builder; - root := compiler_get_nodes(fields).(*Code_Literal); - ensure(root.kind == .LITERAL, "argument must be a struct literal"); - ensure(root.value_type == .STRUCT, "argument must be a struct literal"); - - t_info := T.(*Type_Info_Struct); - - // Ensure the literal we were given is of type T so this operator is more predictable. - // i.e. disallowing Vector3.{} | SomeRandomType.{ x = 10 }; - n_type := root.struct_literal_info.type_expression; - if n_type != null { - tmp: basic.String_Builder; - pp.print_expression(*tmp, n_type); - n_typename := basic.builder_to_string(*tmp); - ensure(n_type.result == t_info, "mismatched types, % vs. %", t_info.name, n_typename); - } - - // @note(judah): if the value we're trying to copy is a constant, - // we have to create a local so we can assign to the fields. - // Doing this allows this usecase: 'with(Default_Entity, .{ kind = .Player })' - receiver := "old"; - #if is_constant(old) { - receiver = "copy"; - basic.print_to_builder(*b, "% := old;\n", receiver); - } - - for root.struct_literal_info.arguments { - op := it.(*Code_Binary_Operator); - ident := op.left.(*Code_Ident); - - ensure(op.kind == .BINARY_OPERATOR, "copy-assign requires named fields", loc = make_location(op)); - ensure(op.operator_type == #char "=", "copy-assign requires named field assignment", loc = make_location(op)); - ensure(ident.kind == .IDENT, "must be an identifier", loc = make_location(op)); - - // Catch any incorrect field assignments before the compiler does for better error reporting - exists := false; - for t_info.members if it.name == ident.name exists = true; - ensure(exists, "field % does not exist within %", ident.name, t_info.name, loc = make_location(ident)); - - // receiver.field = value; - basic.append(*b, receiver); - basic.append(*b, "."); - pp.print_expression(*b, op); - basic.append(*b, ";\n"); - } - - basic.print_to_builder(*b, "return %;\n", receiver); - return basic.builder_to_string(*b); - } -} - -// @note(judah): I like doing this with an operator, but your mileage may vary -operator | :: with; - - -/* - Creates a named block that can exit early (via 'break' or 'continue'). - - This mostly replaces the case where you'd like to jump to - the end of a scope based on some logic within. Without - gotos, this is the next best thing. - - Usage: - // within a loop - for this_block() { // this is named 'block' by default - if !moving break; - // do movement here - } - for this_block("render_player") { - if invisible break render_player; - // do rendering here - } -*/ -this_block :: ($name: string = Default_Name) -> Named_Block(name) #expand { return .{}; } - -/* - Drop-in loop unrolling macro. - - Usage: - for unroll(5) { - // duplicates this body 5 times exactly - } - - known_size: [3]float; - for unroll(known_size) { - // duplicates this body 3 times exactly - } - - var_size: []float; - for unroll(var_size) { - // duplicates this body a set number of times, - // falling back to a regular for loop to handle - // the remaining iterations. - } -*/ -unroll :: ($count: int) -> Unrolled_Loop(count) { return .{}; } -unroll :: (arr: [$N]$T) -> Unrolled_Loop(N, T) { return .{ array = arr }; } -unroll :: (arr: []$T) -> Unrolled_Loop(-1, T) { return .{ array = arr }; } - -// Call #c_call procedures inline with the current context: 'c_call(some_c_call_proc(10, 20))' -c_call :: (call: Code) #expand { - push_context context { #insert,scope(call) call; } -} - -// Call #c_call procedures inline with a custom context: 'c_call(some_c_call_proc(10, 20), c_context)' -c_call :: (call: Code, ctx: #Context) #expand { - push_context ctx { #insert,scope(call) call; } -} - -// @note(judah): for_expansions have to be exported - -for_expansion :: (v: *Named_Block, code: Code, _: For_Flags) #expand { - #insert #run basic.tprint(#string END - for `%: 0..0 { - `it :: #run mem.zero_of(void); - `it_index :: #run mem.zero_of(void); - #insert,scope(code) code; - } - END, - // @note(judah): guards against calling this_block with - // an empty string which results in weird error messages. - ifx v.NAME.count != 0 v.NAME else Default_Name); -} - -for_expansion :: (loop: *Unrolled_Loop, body: Code, flags: For_Flags, loc := #caller_location) #expand { - // runtime unroll - #if loop.N == -1 { - for #v2 <=(flags & .REVERSE == .REVERSE) i: 0..loop.array.count - 1 { - #if flags & .POINTER { - `it := *(#no_abc loop.array[i]); - } - else { - `it := #no_abc loop.array[i]; - } - - `it_index := i; - #insert,scope(body) body; - } - } - // compile-time unroll - else { - `it_index := 0; - - #insert -> string { - b: basic.String_Builder; - basic.print_to_builder(*b, "// inserted unrolled loop (N = %) at %:%\n", loop.N, loc.fully_pathed_filename, loc.line_number); - - if loop.T == void { - basic.append(*b, "`it: int = ---;\n"); - } - else { - basic.append(*b, "`it: loop.T = ---;\n"); - } - - for #v2 <=(flags & .REVERSE == .REVERSE) 0..loop.N - 1 { - basic.append(*b, "{\n"); - - if loop.T == void { - basic.print_to_builder(*b, "\tit = %;\n", it); - } - else { - #if flags & .POINTER { - basic.print_to_builder(*b, "\tit = *(#no_abc loop.array[%]);\n", it); - } - else { - basic.print_to_builder(*b, "\tit = #no_abc loop.array[%];\n", it); - } - } - - basic.print_to_builder(*b, "\tit_index = %;\n", it); - basic.append(*b, "\t#insert,scope(body) body;\n"); - basic.append(*b, "}\n"); - } - - return basic.builder_to_string(*b); - } - } -} - -Default_Name :: "block"; -Named_Block :: struct(NAME: string) {} - -Unrolled_Loop :: struct(N: int, T: Type = void) { - // Only store arrays when we absolutely have to. - #if T != void { - // @todo(judah): because this will only be created via 'unroll', - // should these be pointers to the underlying arrays so we don't - // pay for a copy? - #if N == -1 { - array: []T = ---; - } - else { - array: [N]T = ---; - } - } -} - - -#scope_file; - -mem :: #import "jc/memory"; - -basic :: #import "Basic"; // @future -pp :: #import "Program_Print"; // @future -compiler :: #import "Compiler"; // @future - - -// ---------------------------------------------------------- -// TESTS -// ---------------------------------------------------------- - -#if RUN_TESTS #run { - test :: #import "jc/meta/test"; - - test.run("this_block", (t) => { - i := 0; - - for this_block() { - i += 1; - for this_block() { - break block; - } - - for this_block() { - continue block; - } - - if i == 1 { - break block; - } - - i += 2; - } - - j := 0; - for this_block("named") { - for 0..10 { - break; - } - - if i != 1 { - break named; - } - - j = 1; - } - - test.expect(t, i == 1, "i was %", i); - test.expect(t, j == 1, "j was %", j); - }); - - test.run("copy assign", (t) => { - Value :: struct { - x: float; - y: float; - z: float; - } - - a := Value.{ 10, 20, 30 }; - b := with(a, .{ x = 1, z = 1 }); - c := b | .{ y = 500 }; - - test.expect(t, b.x == 1, "was %", b.x); - test.expect(t, b.z == 1, "was %", b.z); - test.expect(t, c.y == 500, "was %", c.y); - }); -} - diff --git a/meta/module.jai b/meta/module.jai deleted file mode 100644 index 9b6434f..0000000 --- a/meta/module.jai +++ /dev/null @@ -1,94 +0,0 @@ -#module_parameters(RUN_TESTS := false, WITH_SUBMODULES := true); - -#scope_export; - -get_stack_trace_caller_location :: (loc := #caller_location) -> Source_Code_Location { - if context.stack_trace == null || context.stack_trace.info == null { - return loc; - } - - cur := context.stack_trace; - while cur != null { - if cur.info == null break; - - if cur.info.location.fully_pathed_filename != loc.fully_pathed_filename { - break; - } - - cur = cur.next; - } - - return cur.info.location; -} - -check_bounds :: ($$index: $T, $$count: T, loc := #caller_location) #expand { - MESSAGE :: "bounds check failed! index % (max %)"; - #if is_constant(index) && is_constant(count) { - if index < 0 || index >= count { - message := basic.tprint(MESSAGE, index, count - 1); - compiler.compiler_report(message, mode = .ERROR, loc = loc); - } - } - else { - basic.assert(index >= 0 && index < count, MESSAGE, index, count - 1, loc = loc); - } -} - -// Can be passed directly to using,map -remap_snake_to_pascal :: (names: []string) { - for names { - names[it_index] = snake_to_pascal(it); - } -} - -snake_to_pascal :: (name: string) -> string { - b: basic.String_Builder; - - upper := true; - for i: 0..name.count - 1 { - c := name[i]; - if c == #char "_" { - upper = true; - continue; - } - - if upper { - basic.append(*b, basic.to_upper(c)); - upper = false; - } else { - basic.append(*b, c); - } - } - - return basic.builder_to_string(*b); -} - -#if WITH_SUBMODULES { - using #import "jc/meta/macros"(RUN_TESTS); - using #import "jc/meta/type_info"(RUN_TESTS); -} - -#scope_module; - -mem :: #import "jc/memory"; - -basic :: #import "Basic"; // @future -compiler :: #import "Compiler"; // @future - - -// ---------------------------------------------------------- -// TESTS -// ---------------------------------------------------------- - -#if RUN_TESTS #run { - test :: #import "jc/meta/test"; - - test.run("snake_to_pascal", t => { - test.expect(t, snake_to_pascal("some_name") == "SomeName"); - test.expect(t, snake_to_pascal("_some_name") == "SomeName"); - test.expect(t, snake_to_pascal("some__name") == "SomeName"); - test.expect(t, snake_to_pascal("some_name_") == "SomeName"); - test.expect(t, snake_to_pascal("X_Y_Z") == "XYZ"); - test.expect(t, snake_to_pascal("XY_Z") == "XYZ"); - }); -} diff --git a/meta/test/module.jai b/meta/test/module.jai deleted file mode 100644 index 309ddfe..0000000 --- a/meta/test/module.jai +++ /dev/null @@ -1,89 +0,0 @@ -/* - A very simple test runner that can be used at compile-time. - - Usage: - test :: #import "jx/test"; - - #if RUN_TESTS #run { - test.run("collection of tests", t => { - test.expect(t, some_condition, "error message: %", value); - }); - } -*/ -T :: struct { - location: Source_Code_Location; - expects_run: s64; - expects_ok: s64; - failed: bool; -} - -Proc :: #type (t: *T); - -expect :: (t: *T, cond: bool, message := "", args: ..Any, loc := #caller_location) { - t.expects_run += 1; - if cond { - t.expects_ok += 1; - return; - } - - msg := "expectation failed"; - if message.count != 0 { - msg = basic.tprint(message, ..args); - } - - t.failed = true; - if #compile_time { - compiler.compiler_report(msg, loc = loc, mode = .ERROR_CONTINUABLE); - } - else { - basic.assert(false, msg, loc = loc); - } -} - -run :: (name: string, proc: Proc, loc := #caller_location) { - // @note(judah): incredibly dumb way to get nicer test runs - path := loc.fully_pathed_filename; - - i := path.count - 1; - found_first_slash := false; - while i >= 0 { - if path[i] == "/" { - if found_first_slash { - i += 1; - break; - } - - found_first_slash = true; - } - - i -= 1; - } - - if !found_first_slash { - path = strings.path_filename(loc.fully_pathed_filename); - } - else { - path.count -= i; - path.data += i; - } - - - basic.print("%,%: %...", path, loc.line_number, name); - - t: T; - proc(*t); - - if t.failed { - basic.print(" failed!\n"); - } - else { - basic.print(" ok!\n"); - } -} - - -#scope_file; - -basic :: #import "Basic"; // @future -strings :: #import "String"; // @future -compiler :: #import "Compiler"; // @future diff --git a/meta/type_info.jai b/meta/type_info.jai deleted file mode 100644 index 7d20adf..0000000 --- a/meta/type_info.jai +++ /dev/null @@ -1,244 +0,0 @@ -#module_parameters(RUN_TESTS := false); - -// These return the bool first so you can check in a conditional, -// rather than having to do '_, ok := ...' - -check_type_tag :: ($$T: Type, tag: Type_Info_Tag) -> bool, *Type_Info { - #if is_constant(T) { - info :: type_info(T); - if info.type == tag return true, info; - } - else { - info := T.(*Type_Info); - if info.type == tag return true, info; - } - - return false, null; -} - -type_is_integer :: ($$T: Type) -> bool, *Type_Info_Integer { - ok, info := check_type_tag(T, .INTEGER); - return ok, info.(*Type_Info_Integer); -} - -type_is_float :: ($$T: Type) -> bool, *Type_Info_Float { - ok, info := check_type_tag(T, .FLOAT); - return ok, info.(*Type_Info_Float); -} - -type_is_scalar :: (t: Type) -> bool { - return type_is_integer(t) || type_is_float(t); -} - -type_is_array :: ($$T: Type) -> bool, *Type_Info_Array { - ok, info := check_type_tag(T, .ARRAY); - return ok, info.(*Type_Info_Array); -} - -type_is_struct :: ($$T: Type) -> bool, *Type_Info_Struct { - ok, info := check_type_tag(T, .STRUCT); - return ok, info.(*Type_Info_Struct); -} - -type_is_enum :: ($$T: Type) -> bool, *Type_Info_Enum { - ok, info := check_type_tag(T, .ENUM); - return ok, info.(*Type_Info_Enum); -} - -// Returns the lowest and highest values T can represent. -// Note: T must be an integer, float, or enum type. -range_for :: ($T: Type, loc := #caller_location) -> (T, T) #expand { - // @note(judah): we need to runs here because jai is weird. - return #run lo_for(T, loc = loc), #run hi_for(T, loc = loc); -} - -// Returns the lowest value T can represent. -// Note: T must be an integer, float, or enum type. -lo_for :: ($T: Type, loc := #caller_location) -> T #expand { - return #run -> T { - info := T.(*Type_Info); - if info.type == { - case .INTEGER; - i := info.(*Type_Info_Integer); - if i.runtime_size == { - case 1; return (ifx i.signed then -0x80 else 0).(T, no_check); - case 2; return (ifx i.signed then -0x8000 else 0).(T, no_check); - case 4; return (ifx i.signed then -0x8000_0000 else 0).(T, no_check); - case 8; return (ifx i.signed then -0x8000_0000_0000_0000 else 0).(T, no_check); - case; - compiler.compiler_report("unhandled integer type", loc = loc); - } - - case .FLOAT; - if info.runtime_size == { - case 4; return (0h00800000).(T, no_check); - case 8; return (0h00100000_00000000).(T, no_check); - case; - compiler.compiler_report("unhandled float type", loc = loc); - } - - case .ENUM; - i := info.(*Type_Info_Enum); - if i.values.count == 0 { - return 0; - } - - min: T = i.values[0].(T, no_check); - if i.internal_type.signed { - for i.values if it.(T) < min { - min = it.(T); - } - } - else { - for i.values if it.(T) < min { - min = it.(T); - } - } - - return min; - - case; - compiler.compiler_report("min requires an enum, integer, or float type", loc = loc); - } - - return 0; - }; -} - -// Returns the highest value T can represent. -// Note: T must be an integer, float, or enum type. -hi_for :: ($T: Type, loc := #caller_location) -> T #expand { - return #run -> T { - info := T.(*Type_Info); - if info.type == { - case .INTEGER; - i := info.(*Type_Info_Integer); - if i.runtime_size == { - case 1; return (ifx i.signed then 0x7f else 0xff).(T, no_check); - case 2; return (ifx i.signed then 0x7fff else 0xffff).(T, no_check); - case 4; return (ifx i.signed then 0x7fff_ffff else 0xffff_ffff).(T, no_check); - case 8; return (ifx i.signed then 0x7fff_ffff_ffff_ffff else 0xffff_ffff_ffff_ffff).(T, no_check); - case; - compiler.compiler_report("unhandled integer type", loc = loc); - } - - case .FLOAT; - if info.runtime_size == { - case 4; return (0h7F7FFFFF).(T, no_check); - case 8; return (0h7FEFFFFF_FFFFFFFF).(T, no_check); - case; - compiler.compiler_report("unhandled float type", loc = loc); - } - - case .ENUM; - i := info.(*Type_Info_Enum); - if i.values.count == 0 { - return 0; - } - - max := i.values[0].(T, no_check); - if i.internal_type.signed { - for i.values if xx it > max { - max = xx it; - } - } - else { - for i.values if xx it > max { - max = xx it; - } - } - - return max; - - case; - compiler.compiler_report("max requires an enum, integer, or float type", loc = loc); - } - - return 0; - }; -} - - -#scope_file; - -compiler :: #import "Compiler"; // @future - - -// ---------------------------------------------------------- -// TESTS -// ---------------------------------------------------------- - -#if RUN_TESTS #run { - test :: #import "jc/meta/test"; - - test.run("lo_for:primitives", t => { - test.expect(t, lo_for(u8) == 0); - test.expect(t, lo_for(s8) == -128); - test.expect(t, lo_for(u16) == 0); - test.expect(t, lo_for(s16) == -32768); - test.expect(t, lo_for(u32) == 0); - test.expect(t, lo_for(s32) == -2147483648); - test.expect(t, lo_for(u64) == 0); - test.expect(t, lo_for(s64) == -9223372036854775808); - - test.expect(t, lo_for(float32) == 0h00800000); - test.expect(t, lo_for(float64) == 0h00100000_00000000); - }); - - test.run("hi_for:primitives", t => { - test.expect(t, hi_for(u8) == 255); - test.expect(t, hi_for(s8) == 127); - test.expect(t, hi_for(u16) == 65535); - test.expect(t, hi_for(s16) == 32767); - test.expect(t, hi_for(u32) == 4294967295); - test.expect(t, hi_for(s32) == 2147483647); - test.expect(t, hi_for(u64) == 18446744073709551615); - test.expect(t, hi_for(s64) == 9223372036854775807); - - test.expect(t, hi_for(float32) == 340282346638528859000000000000000000000.0); - test.expect(t, hi_for(float64) == 179769313486231570900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0); - }); - - test.run("lo_for/hi_for:enums", t => { - U8_Enum :: enum u8 { lo :: -1; hi :: +1; } - S8_Enum :: enum s8 { lo :: -2; hi :: -1; } - { - test.expect(t, lo_for(U8_Enum) == U8_Enum.lo); - test.expect(t, lo_for(S8_Enum) == S8_Enum.lo); - test.expect(t, hi_for(U8_Enum) == U8_Enum.hi); - test.expect(t, hi_for(S8_Enum) == S8_Enum.hi); - } - - U16_Enum :: enum u16 { lo :: -1; hi :: +1; } - S16_Enum :: enum s16 { lo :: -2; hi :: -1; } - { - test.expect(t, lo_for(U16_Enum) == U16_Enum.lo); - test.expect(t, lo_for(S16_Enum) == S16_Enum.lo); - test.expect(t, hi_for(U16_Enum) == U16_Enum.hi); - test.expect(t, hi_for(S16_Enum) == S16_Enum.hi); - } - - U32_Enum :: enum u32 { lo :: -1; hi :: +1; } - S32_Enum :: enum s32 { lo :: -2; hi :: -1; } - { - test.expect(t, lo_for(U32_Enum) == U32_Enum.lo); - test.expect(t, lo_for(S32_Enum) == S32_Enum.lo); - test.expect(t, hi_for(U32_Enum) == U32_Enum.hi); - test.expect(t, hi_for(S32_Enum) == S32_Enum.hi); - } - - U64_Enum :: enum u64 { lo :: -1; hi :: +1; } - S64_Enum :: enum s64 { lo :: -2; hi :: -1; } - { - test.expect(t, lo_for(U64_Enum) == U64_Enum.lo); - test.expect(t, lo_for(S64_Enum) == S64_Enum.lo); - test.expect(t, hi_for(U64_Enum) == U64_Enum.hi); - test.expect(t, hi_for(S64_Enum) == S64_Enum.hi); - } - - // @note(judah): just making sure this compiles - lo, hi := range_for(U64_Enum); - test.expect(t, lo == U64_Enum.lo); - test.expect(t, hi == U64_Enum.hi); - }); -} diff --git a/module.jai b/module.jai index 44763e2..5d3817b 100644 --- a/module.jai +++ b/module.jai @@ -1,59 +1,21 @@ -#module_parameters(IMPORT_LOCATION := #caller_location); +/// Module jc contains procedures for working with memory, +/// arrays, and strings; as well as helpful macros and +/// constants. +/// +/// Additionally, it provides a platform-independant +/// interface for interacting with the target operating +/// system. +#module_parameters(RunTests := false); -#scope_export; +// @note(judah): this causes some very weird issues in tests so it's ifdef'd out +#if !RunTests #add_context temp_allocator: Allocator; -byte :: u8; -cstring :: *byte; -rawptr :: *void; - -#if size_of(int) == size_of(s64) { - sint :: s64; - uint :: u64; -} -else { - sint :: s32; - uint :: u32; -} - -#if size_of(rawptr) == size_of(u64) { - uptr :: u64; - sptr :: s64; -} -else { - uptr :: u32; - sptr :: s32; -} - -#if OS == { - case .WINDOWS; - #if CPU != .X64 #run report_error_on_import("JC is only supported x86-64 Windows systems, not %", CPU); - - ARCH :: "x86-64"; - PLATFORM :: "windows"; - - case .MACOS; - #if CPU == { - case .X64; ARCH :: "x86-64"; - case .ARM64; ARCH :: "arm64"; - } - - PLATFORM :: "macos"; - - case .LINUX; - #if CPU != .X64 #run report_error_on_import("JC is only supported x86-64 Unix systems, not %", CPU); - - ARCH :: "x86-64"; - PLATFORM :: "unix"; - - case; - #run report_error_on_import("JC is not supported on % % systems", CPU, OS); -} - - -#scope_file; - -report_error_on_import :: (fmt: string, args: ..Any) #compile_time { - basic :: #import "Basic"; // @future - compiler :: #import "Compiler"; // @future - compiler.compiler_report(basic.tprint(fmt, ..args), loc = IMPORT_LOCATION); -} +#load "+internal/builtin.jai"; +#load "+internal/array.jai"; +#load "+internal/kv.jai"; +#load "+internal/hashing.jai"; +#load "+internal/memory.jai"; +#load "+internal/allocators.jai"; +#load "+internal/testing.jai"; +#load "+internal/keywords.jai"; +#load "+internal/type_info.jai"; diff --git a/platform/arch.jai b/platform/arch.jai deleted file mode 100644 index d47c056..0000000 --- a/platform/arch.jai +++ /dev/null @@ -1,107 +0,0 @@ -#module_parameters(RUN_TESTS := false); - -// All of the architecture-specific extensions we care to look for. -// Note: Checking for impossible extensions will always turn into a no-op (ex. NEON support on x64). -ISA_Extension :: enum { - // x64 - sse2; - sse3; - sse41; - sse42; - ssse3; - - avx; - avx2; - avx512cd; - avx512f; - avx512vnni; - - // arm64 - neon; - - // riscv - rv64ip; -} - -// Returns true if the extension is supported by the current architecture. -arch_supports :: inline ($ext: ISA_Extension) -> bool { - lazy_init_cpu_info(); - return arch_has_extension(ext); -} - -// Returns true if any of the extensions are supported by the current architecture. -arch_supports_any :: inline ($extensions: ..ISA_Extension) -> bool { - lazy_init_cpu_info(); - - res: bool; - #insert -> string { - b: basic.String_Builder; - for extensions { - basic.print_to_builder(*b, "res ||= arch_has_extension(.%);\n", it); - } - return basic.builder_to_string(*b); - } - return res; -} - -// Returns true if all of the extensions are supported by the current architecture. -arch_supports_all :: inline ($extensions: ..ISA_Extension) -> bool { - lazy_init_cpu_info(); - - res: bool; - #insert -> string { - b: basic.String_Builder; - for extensions { - basic.print_to_builder(*b, "res &&= arch_has_extension(.%);\n", it); - } - return basic.builder_to_string(*b); - } - return res; -} - - -#scope_file; - -// @note(judah): this assumes lazy_init_cpu_info was already called -arch_has_extension :: inline ($ext: ISA_Extension) -> bool { - #if CPU == { - case .X64; #if ext == { - case .sse2; return x64.check_feature(info.feature_leaves, .SSE2); - case .sse3; return x64.check_feature(info.feature_leaves, .SSE3); - case .sse41; return x64.check_feature(info.feature_leaves, .SSE4_1); - case .sse42; return x64.check_feature(info.feature_leaves, .SSE4_2); - case .ssse3; return x64.check_feature(info.feature_leaves, .SSSE3); - case .avx; return x64.check_feature(info.feature_leaves, .AVX); - case .avx2; return x64.check_feature(info.feature_leaves, .AVX2); - case .avx512cd; return x64.check_feature(info.feature_leaves, .AVX512CD); - case .avx512f; return x64.check_feature(info.feature_leaves, .AVX512F); - case .avx512vnni; return x64.check_feature(info.feature_leaves, .AVX512_VNNI); - } - case .ARM64; #if ext == { - case .neon; - return true; // @note(judah): it's safe to assume neon support if we're on arm (for now) - } - } - - return false; -} - -info_initialized := false; - -lazy_init_cpu_info :: () #expand { - if info_initialized return; - info_initialized = true; - - #if CPU == .X64 { - info = x64.get_cpu_info(); - } -} - -#if CPU == { - case .X64; - info: x64.Cpu_X86; - x64 :: #import "Machine_X64"; // @future -} - - -basic :: #import "Basic"; // @future diff --git a/platform/module.jai b/platform/module.jai deleted file mode 100644 index eb6c097..0000000 --- a/platform/module.jai +++ /dev/null @@ -1,9 +0,0 @@ -#module_parameters(RUN_TESTS := false, WITH_SUBMODULES := true); - -#scope_export; - -#if WITH_SUBMODULES { - using #import "jc/platform/arch"(RUN_TESTS); -} - -#scope_module; diff --git a/encoding/json.jai b/x/json.jai similarity index 99% rename from encoding/json.jai rename to x/json.jai index f7d8cb0..e519d76 100644 --- a/encoding/json.jai +++ b/x/json.jai @@ -1,4 +1,4 @@ -#module_parameters(RUN_TESTS := false); +#module_parameters(RunTests := false); Json_Value :: struct { kind: enum { @@ -327,7 +327,7 @@ to_string :: (value: Json_Value, indent := 0) -> string { } -#scope_file; +#scope_file get_json_member_name :: (member: Type_Info_Struct_Member) -> string { name := member.name; @@ -609,4 +609,3 @@ at_end :: inline (p: *Parser) -> bool { basic :: #import "Basic"; // @future strings :: #import "String"; // @future - diff --git a/meta/reload/examples/everything-you-need.jai b/x/reload/examples/everything-you-need.jai similarity index 100% rename from meta/reload/examples/everything-you-need.jai rename to x/reload/examples/everything-you-need.jai diff --git a/meta/reload/examples/quickstart.jai b/x/reload/examples/quickstart.jai similarity index 100% rename from meta/reload/examples/quickstart.jai rename to x/reload/examples/quickstart.jai diff --git a/meta/reload/module.jai b/x/reload/module.jai similarity index 100% rename from meta/reload/module.jai rename to x/reload/module.jai diff --git a/meta/reload/reload_main.jai b/x/reload/reload_main.jai similarity index 100% rename from meta/reload/reload_main.jai rename to x/reload/reload_main.jai