zig/lib/compiler_rt/parity.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

50 lines
1.5 KiB
Zig

//! parity - if number of bits set is even => 0, else => 1
//! - pariytXi2_generic for big and little endian
const std = @import("std");
const builtin = @import("builtin");
const common = @import("common.zig");
pub const panic = common.panic;
comptime {
@export(__paritysi2, .{ .name = "__paritysi2", .linkage = common.linkage, .visibility = common.visibility });
@export(__paritydi2, .{ .name = "__paritydi2", .linkage = common.linkage, .visibility = common.visibility });
@export(__parityti2, .{ .name = "__parityti2", .linkage = common.linkage, .visibility = common.visibility });
}
pub fn __paritysi2(a: i32) callconv(.C) i32 {
return parityXi2(i32, a);
}
pub fn __paritydi2(a: i64) callconv(.C) i32 {
return parityXi2(i64, a);
}
pub fn __parityti2(a: i128) callconv(.C) i32 {
return parityXi2(i128, a);
}
inline fn parityXi2(comptime T: type, a: T) i32 {
var x = switch (@bitSizeOf(T)) {
32 => @bitCast(u32, a),
64 => @bitCast(u64, a),
128 => @bitCast(u128, a),
else => unreachable,
};
// Bit Twiddling Hacks: Compute parity in parallel
comptime var shift: u8 = @bitSizeOf(T) / 2;
inline while (shift > 2) {
x ^= x >> shift;
shift = shift >> 1;
}
x &= 0xf;
return (@intCast(u16, 0x6996) >> @intCast(u4, x)) & 1; // optimization for >>2 and >>1
}
test {
_ = @import("paritysi2_test.zig");
_ = @import("paritydi2_test.zig");
_ = @import("parityti2_test.zig");
}