From 5f7b1107d177ea7c0e71211c53ec7bdf811e72d3 Mon Sep 17 00:00:00 2001 From: Judah Caruso Date: Tue, 27 May 2025 16:09:05 -0600 Subject: [PATCH] improve meta.unroll --- meta/macros.jai | 63 ++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 45 deletions(-) diff --git a/meta/macros.jai b/meta/macros.jai index 8e99090..c38fb15 100644 --- a/meta/macros.jai +++ b/meta/macros.jai @@ -140,51 +140,19 @@ for_expansion :: (v: *Named_Block, code: Code, _: For_Flags) #expand { } for_expansion :: (loop: *Unrolled_Loop, body: Code, flags: For_Flags, loc := #caller_location) #expand { - #assert flags & .REVERSE == 0 "reverse iteration not supported with loop unrolling (for now)"; - #assert flags & .POINTER == 0 "pointer iteration not supported with loop unrolling (for now)"; - // runtime unroll #if loop.N == -1 { - for i: 0..loop.array.count - 1 { - `it := #no_abc loop.array[i]; + 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; } - - // @todo(judah): below doesn't properly handle counts not divisible by 4, - // so we end up going over the bounds of the array. - - // UNROLL_AMOUNT :: 4; // @todo(judah): make this configurable? - // unrolled_loops := loop.array.count / UNROLL_AMOUNT; - // remainder_loops := loop.array.count % UNROLL_AMOUNT; - // `it: loop.T; - // for i: 0..unrolled_loops #no_abc { - // basic.print("I: %\n", i); - // index := i * UNROLL_AMOUNT; - - // it_index = index + 0; - // it = loop.array[it_index]; - // #insert,scope(body) body; - - // it_index = index + 1; - // it = loop.array[it_index]; - // #insert,scope(body) body; - - // it_index = index + 2; - // it = loop.array[it_index]; - // #insert,scope(body) body; - - // it_index = index + 3; - // it = loop.array[it_index]; - // #insert,scope(body) body; - // } - - // after_big_loop := it_index; - // for i: 0..remainder_loops #no_abc { - // it_index = after_big_loop + i; - // it = loop.array[it_index]; - // #insert,scope(body) body; - // } } // compile-time unroll else { @@ -201,14 +169,19 @@ for_expansion :: (loop: *Unrolled_Loop, body: Code, flags: For_Flags, loc := #ca basic.append(*b, "`it: loop.T = ---;\n"); } - for 0..loop.N - 1 { + 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 { - basic.print_to_builder(*b, "\tit = #no_abc loop.array[%];\n", it); + #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); @@ -221,9 +194,6 @@ for_expansion :: (loop: *Unrolled_Loop, body: Code, flags: For_Flags, loc := #ca } } - -#scope_file; - Default_Name :: "block"; Named_Block :: struct(NAME: string) {} @@ -242,6 +212,9 @@ Unrolled_Loop :: struct(N: int, T: Type = void) { } } + +#scope_file; + basic :: #import "Basic"; // @future pp :: #import "Program_Print"; // @future compiler :: #import "Compiler"; // @future