zig/lib/std/special/compiler_rt/mulodi4.zig
Andrew Kelley d29871977f remove redundant license headers from zig standard library
We already have a LICENSE file that covers the Zig Standard Library. We
no longer need to remind everyone that the license is MIT in every single
file.

Previously this was introduced to clarify the situation for a fork of
Zig that made Zig's LICENSE file harder to find, and replaced it with
their own license that required annual payments to their company.
However that fork now appears to be dead. So there is no need to
reinforce the copyright notice in every single file.
2021-08-24 12:25:09 -07:00

45 lines
1.1 KiB
Zig

const builtin = @import("builtin");
const compiler_rt = @import("../compiler_rt.zig");
const maxInt = std.math.maxInt;
const minInt = std.math.minInt;
pub fn __mulodi4(a: i64, b: i64, overflow: *c_int) callconv(.C) i64 {
@setRuntimeSafety(builtin.is_test);
const min = @bitCast(i64, @as(u64, 1 << (64 - 1)));
const max = ~min;
overflow.* = 0;
const result = a *% b;
// Edge cases
if (a == min) {
if (b != 0 and b != 1) overflow.* = 1;
return result;
}
if (b == min) {
if (a != 0 and a != 1) overflow.* = 1;
return result;
}
// Take absolute value of a and b via abs(x) = (x^(x >> 63)) - (x >> 63).
const abs_a = (a ^ (a >> 63)) -% (a >> 63);
const abs_b = (b ^ (b >> 63)) -% (b >> 63);
// Unitary magnitude, cannot have overflow
if (abs_a < 2 or abs_b < 2) return result;
// Compare the signs of the operands
if ((a ^ b) >> 63 != 0) {
if (abs_a > @divTrunc(max, abs_b)) overflow.* = 1;
} else {
if (abs_a > @divTrunc(min, -abs_b)) overflow.* = 1;
}
return result;
}
test "import mulodi4" {
_ = @import("mulodi4_test.zig");
}