commit 31ddfc81840d33a9fae220c89a368e3a4fe5ff17
parent 132494ba62ffb033d10925a38f2aea573a022e84
Author: Thomas Vigouroux <thomas.vigouroux@univ-grenoble-alpes.fr>
Date: Fri, 14 Jun 2024 13:41:14 +0200
docs: document optimizations
Diffstat:
1 file changed, 5 insertions(+), 0 deletions(-)
diff --git a/src/compile.zig b/src/compile.zig
@@ -69,6 +69,8 @@ pub const Compiler = struct {
try self.getChunk().write(.{ .call = val.arity });
}
+ // "macro" that allows to perform pattern mathing
+ // this is nice to generate intrinsics for certain patterns
fn checkMatch(comptime desc: anytype, val: *v.Value) bool {
const rootT = @TypeOf(desc);
switch (rootT) {
@@ -120,12 +122,15 @@ pub const Compiler = struct {
fn compileInner(self: *Self, val: *v.Value, astate: CompileState) CompileError!void {
var state = astate;
+ // (= (% val 2) 0) => (even? val)
if (Compiler.checkMatch(.{ v.Special.@"=", .{ v.Special.@"%", .any, 2 }, 0 }, val)) {
try self.compileInner(val.val.list[1].val.list[1], .{ .return_this = false });
try self.compileIntrinsic(.isEven);
+ // (= (% val 2) 1) => (odd? val)
} else if (Compiler.checkMatch(.{ v.Special.@"=", .{ v.Special.@"%", .any, 2 }, 1 }, val)) {
try self.compileInner(val.val.list[1].val.list[1], .{ .return_this = false });
try self.compileIntrinsic(.isOdd);
+ // (+ (* v1 v2) v3) => (fma v1 v2 v3)
} else if (Compiler.checkMatch(.{ v.Special.@"+", .{ v.Special.@"*", .any, .any }, .any }, val)) {
try self.compileInner(val.val.list[1].val.list[1], .{ .return_this = false });
try self.compileInner(val.val.list[1].val.list[2], .{ .return_this = false });