2018-10-14 03:18:00 +08:00
|
|
|
const builtin = @import("builtin");
|
2019-03-03 05:46:04 +08:00
|
|
|
const compiler_rt = @import("../compiler_rt.zig");
|
2018-10-14 03:18:00 +08:00
|
|
|
|
2018-10-15 21:51:15 +08:00
|
|
|
// Ported from git@github.com:llvm-project/llvm-project-20170507.git
|
2018-10-14 03:18:00 +08:00
|
|
|
// ae684fad6d34858c014c94da69c15e7774a633c3
|
|
|
|
// 2018-08-13
|
|
|
|
|
|
|
|
pub extern fn __multi3(a: i128, b: i128) i128 {
|
|
|
|
@setRuntimeSafety(builtin.is_test);
|
2018-11-13 21:08:37 +08:00
|
|
|
const x = twords{ .all = a };
|
|
|
|
const y = twords{ .all = b };
|
|
|
|
var r = twords{ .all = __mulddi3(x.s.low, y.s.low) };
|
2018-10-14 03:18:00 +08:00
|
|
|
r.s.high +%= x.s.high *% y.s.low +% x.s.low *% y.s.high;
|
|
|
|
return r.all;
|
|
|
|
}
|
|
|
|
|
2019-04-11 06:47:14 +08:00
|
|
|
const v128 = @Vector(2, u64);
|
|
|
|
pub extern fn __multi3_windows_x86_64(a: v128, b: v128) v128 {
|
|
|
|
return @bitCast(v128, @inlineCall(__multi3, @bitCast(i128, a), @bitCast(i128, b)));
|
2018-10-14 03:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn __mulddi3(a: u64, b: u64) i128 {
|
|
|
|
const bits_in_dword_2 = (@sizeOf(i64) * 8) / 2;
|
|
|
|
const lower_mask = ~u64(0) >> bits_in_dword_2;
|
|
|
|
var r: twords = undefined;
|
|
|
|
r.s.low = (a & lower_mask) *% (b & lower_mask);
|
|
|
|
var t: u64 = r.s.low >> bits_in_dword_2;
|
|
|
|
r.s.low &= lower_mask;
|
|
|
|
t +%= (a >> bits_in_dword_2) *% (b & lower_mask);
|
|
|
|
r.s.low +%= (t & lower_mask) << bits_in_dword_2;
|
|
|
|
r.s.high = t >> bits_in_dword_2;
|
|
|
|
t = r.s.low >> bits_in_dword_2;
|
|
|
|
r.s.low &= lower_mask;
|
|
|
|
t +%= (b >> bits_in_dword_2) *% (a & lower_mask);
|
|
|
|
r.s.low +%= (t & lower_mask) << bits_in_dword_2;
|
|
|
|
r.s.high +%= t >> bits_in_dword_2;
|
|
|
|
r.s.high +%= (a >> bits_in_dword_2) *% (b >> bits_in_dword_2);
|
|
|
|
return r.all;
|
|
|
|
}
|
|
|
|
|
2018-11-13 21:08:37 +08:00
|
|
|
const twords = extern union {
|
2018-10-14 03:18:00 +08:00
|
|
|
all: i128,
|
|
|
|
s: S,
|
|
|
|
|
2018-10-15 21:51:15 +08:00
|
|
|
const S = if (builtin.endian == builtin.Endian.Little)
|
2018-11-13 21:08:37 +08:00
|
|
|
struct {
|
2018-10-15 21:51:15 +08:00
|
|
|
low: u64,
|
|
|
|
high: u64,
|
|
|
|
}
|
|
|
|
else
|
2018-11-13 21:08:37 +08:00
|
|
|
struct {
|
2018-10-15 21:51:15 +08:00
|
|
|
high: u64,
|
|
|
|
low: u64,
|
|
|
|
};
|
2018-10-14 03:18:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
test "import multi3" {
|
|
|
|
_ = @import("multi3_test.zig");
|
|
|
|
}
|