zig/lib/std/special/compiler_rt/mulodi4.zig
Andrew Kelley a2ff3a13fe std, compiler-rt: remove test names where applicable
Tests with no names are executed when using `zig test` regardless of the
`--test-filter` used. Non-named tests should be used when simply
importing unit tests from another file. This allows `zig test` to find
all the appropriate tests, even when using `--test-filter`.
2021-09-01 17:54:06 -07:00

43 lines
1.1 KiB
Zig

const builtin = @import("builtin");
const compiler_rt = @import("../compiler_rt.zig");
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_test.zig");
}