explicitly include muloti4 in LLVM IR

This commit is contained in:
Folkert 2021-07-21 15:01:18 +02:00
parent 0d606a348e
commit bafb16091d
3 changed files with 48 additions and 1 deletions

View File

@ -39,7 +39,7 @@ pub fn build(b: *Builder) void {
obj.linkSystemLibrary("c");
obj.setOutputDir(".");
obj.strip = true;
// obj.bundle_compiler_rt = true;
obj.bundle_compiler_rt = true;
const obj_step = b.step("object", "Build object file for linking");
obj_step.dependOn(&obj.step);

View File

@ -131,3 +131,46 @@ pub fn panic(message: []const u8, stacktrace: ?*std.builtin.StackTrace) noreturn
test "" {
testing.refAllDecls(@This());
}
export fn __muloti4(a: i128, b: i128, overflow: *c_int) callconv(.C) i128 {
// @setRuntimeSafety(builtin.is_test);
const min = @bitCast(i128, @as(u128, 1 << (128 - 1)));
const max = ~min;
overflow.* = 0;
const r = a *% b;
if (a == min) {
if (b != 0 and b != 1) {
overflow.* = 1;
}
return r;
}
if (b == min) {
if (a != 0 and a != 1) {
overflow.* = 1;
}
return r;
}
const sa = a >> (128 - 1);
const abs_a = (a ^ sa) -% sa;
const sb = b >> (128 - 1);
const abs_b = (b ^ sb) -% sb;
if (abs_a < 2 or abs_b < 2) {
return r;
}
if (sa == sb) {
if (abs_a > @divTrunc(max, abs_b)) {
overflow.* = 1;
}
} else {
if (abs_a > @divTrunc(min, -abs_b)) {
overflow.* = 1;
}
}
return r;
}

View File

@ -369,6 +369,10 @@ fn add_intrinsics<'ctx>(ctx: &'ctx Context, module: &Module<'ctx>) {
let i32_type = ctx.i32_type();
let i64_type = ctx.i64_type();
if let Some(func) = module.get_function("__muloti4") {
func.set_linkage(Linkage::External);
}
add_intrinsic(
module,
LLVM_LOG_F64,