Skip to content

Commit

Permalink
fix(jit): une @mod for float modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed May 21, 2024
1 parent f515af8 commit ab7ed6a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
28 changes: 11 additions & 17 deletions src/Jit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2706,24 +2706,14 @@ fn generateBinary(self: *Self, node: Ast.Node.Index) Error!?m.MIR_op_t {
},
.Percent => {
if (left_type_def == .Float or right_type_def == .Float) {
const f_res = m.MIR_new_reg_op(
self.ctx,
try self.REG("f_res", m.MIR_T_D),
);

const pdt = m.MIR_new_reg_op(
self.ctx,
try self.REG("p", m.MIR_T_D),
try self.buildExternApiCall(
.fmod,
res,
&[_]m.MIR_op_t{
left,
right,
},
);

// quotient
self.DDIV(f_res, left, right);
// product
self.DMUL(pdt, f_res, right);
// remainder
self.DSUB(f_res, f_res, pdt);

self.wrap(.Float, f_res, res);
} else {
self.MODS(res, left, right);

Expand Down Expand Up @@ -6526,3 +6516,7 @@ fn outputModule(self: *Self, name: []const u8, module: m.MIR_module_t) void {
module,
);
}

pub fn fmod(lhs: f64, rhs: f64) Value {
return Value.fromFloat(@mod(lhs, rhs));
}
24 changes: 24 additions & 0 deletions src/jit_extern_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub const ExternApi = enum {

dumpInt,
bz_valueDump,
fmod,

pub fn declare(self: ExternApi, jit: *JIT) !m.MIR_item_t {
const prototype = jit.state.?.prototypes.get(self) orelse self.proto(jit.ctx);
Expand Down Expand Up @@ -982,6 +983,25 @@ pub const ExternApi = enum {
},
},
),
.fmod => m.MIR_new_proto_arr(
ctx,
self.pname(),
1,
&[_]m.MIR_type_t{m.MIR_T_I64},
2,
&[_]m.MIR_var_t{
.{
.type = m.MIR_T_D,
.name = "lhs",
.size = undefined,
},
.{
.type = m.MIR_T_D,
.name = "rhs",
.size = undefined,
},
},
),
};
}

Expand Down Expand Up @@ -1054,13 +1074,15 @@ pub const ExternApi = enum {

.dumpInt => @as(*anyopaque, @ptrFromInt(@intFromPtr(&api.dumpInt))),
.bz_valueDump => @as(*anyopaque, @ptrFromInt(@intFromPtr(&api.Value.bz_valueDump))),
.fmod => @as(*anyopaque, @ptrFromInt(@intFromPtr(&JIT.fmod))),
else => {
io.print("{s}\n", .{self.name()});
unreachable;
},
};
}

// FIXME: no need for this we can return @tagName
pub fn name(self: ExternApi) [*:0]const u8 {
return switch (self) {
.nativefn => "NativeFn",
Expand Down Expand Up @@ -1130,6 +1152,7 @@ pub const ExternApi = enum {

.dumpInt => "dumpInt",
.bz_valueDump => "bz_valueDump",
.fmod => "fmod",
};
}

Expand Down Expand Up @@ -1202,6 +1225,7 @@ pub const ExternApi = enum {

.dumpInt => "p_dumpInt",
.bz_valueDump => "p_bz_valueDump",
.fmod => "p_fmod",
};
}
};
15 changes: 13 additions & 2 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3481,9 +3481,20 @@ pub const VM = struct {
const left_i: ?i32 = if (left.isInteger()) left.integer() else null;

if (right_f != null or left_f != null) {
self.push(Value.fromFloat(@mod((left_f orelse @as(f64, @floatFromInt(left_i.?))), (right_f orelse @as(f64, @floatFromInt(right_i.?))))));
self.push(
Value.fromFloat(
@mod(
(left_f orelse @as(f64, @floatFromInt(left_i.?))),
(right_f orelse @as(f64, @floatFromInt(right_i.?))),
),
),
);
} else {
self.push(Value.fromInteger(@mod(left_i.?, right_i.?)));
self.push(
Value.fromInteger(
@mod(left_i.?, right_i.?),
),
);
}

const next_full_instruction: u32 = self.readInstruction();
Expand Down

0 comments on commit ab7ed6a

Please sign in to comment.