zig/lib/compiler_rt/negv.zig
Luuk de Gram 30f2bb8464
compiler-rt: Set the symbol visibility
When we're compiling compiler_rt for any WebAssembly target, we do
not want to expose all the compiler-rt functions to the host runtime.
By setting the visibility of all exports to `hidden`, we allow the
linker to resolve the symbols during linktime, while not expose the
functions to the host runtime. This also means the linker can
properly garbage collect any compiler-rt function that does not get
resolved. The symbol visibility for all target remains the same as
before: `default`.
2022-12-28 14:57:17 +01:00

46 lines
1.3 KiB
Zig

//! negv - negate oVerflow
//! * @panic, if result can not be represented
//! - negvXi4_generic for unoptimized version
const std = @import("std");
const builtin = @import("builtin");
const common = @import("common.zig");
pub const panic = common.panic;
comptime {
@export(__negvsi2, .{ .name = "__negvsi2", .linkage = common.linkage, .visibility = common.visibility });
@export(__negvdi2, .{ .name = "__negvdi2", .linkage = common.linkage, .visibility = common.visibility });
@export(__negvti2, .{ .name = "__negvti2", .linkage = common.linkage, .visibility = common.visibility });
}
pub fn __negvsi2(a: i32) callconv(.C) i32 {
return negvXi(i32, a);
}
pub fn __negvdi2(a: i64) callconv(.C) i64 {
return negvXi(i64, a);
}
pub fn __negvti2(a: i128) callconv(.C) i128 {
return negvXi(i128, a);
}
inline fn negvXi(comptime ST: type, a: ST) ST {
const UT = switch (ST) {
i32 => u32,
i64 => u64,
i128 => u128,
else => unreachable,
};
const N: UT = @bitSizeOf(ST);
const min: ST = @bitCast(ST, (@as(UT, 1) << (N - 1)));
if (a == min)
@panic("compiler_rt negv: overflow");
return -a;
}
test {
_ = @import("negvsi2_test.zig");
_ = @import("negvdi2_test.zig");
_ = @import("negvti2_test.zig");
}