Remove signed_type from zig_{clz,ctz,popcount}

This parameter is only currently needed by zig_byte_swap() and
zig_bit_reverse(). This commit adds an option to airBuiltinCall() to
allow emitting the signedness information only when needed, removing
this unused parameter from the other builtins.
This commit is contained in:
Daniele Cocca 2022-03-14 01:04:24 +00:00
parent 5a971bbeea
commit d912699e08
2 changed files with 18 additions and 20 deletions

View File

@ -1709,11 +1709,11 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
.memcpy => try airMemcpy(f, inst),
.set_union_tag => try airSetUnionTag(f, inst),
.get_union_tag => try airGetUnionTag(f, inst),
.clz => try airBuiltinCall(f, inst, "clz"),
.ctz => try airBuiltinCall(f, inst, "ctz"),
.popcount => try airBuiltinCall(f, inst, "popcount"),
.byte_swap => try airBuiltinCall(f, inst, "byte_swap"),
.bit_reverse => try airBuiltinCall(f, inst, "bit_reverse"),
.clz => try airBuiltinCall(f, inst, "clz", .{}),
.ctz => try airBuiltinCall(f, inst, "ctz", .{}),
.popcount => try airBuiltinCall(f, inst, "popcount", .{}),
.byte_swap => try airBuiltinCall(f, inst, "byte_swap", .{ .needs_signedness_info = true }),
.bit_reverse => try airBuiltinCall(f, inst, "bit_reverse", .{ .needs_signedness_info = true }),
.tag_name => try airTagName(f, inst),
.error_name => try airErrorName(f, inst),
.splat => try airSplat(f, inst),
@ -3351,7 +3351,7 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
return local;
}
fn airBuiltinCall(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8) !CValue {
fn airBuiltinCall(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8, options: struct { needs_signedness_info: bool = false }) !CValue {
if (f.liveness.isUnused(inst)) return CValue.none;
const inst_ty = f.air.typeOfIndex(inst);
@ -3364,14 +3364,18 @@ fn airBuiltinCall(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8) !C
const int_info = operand_ty.intInfo(target);
_ = toCIntBits(int_info.bits) orelse
return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{});
const signed_type = switch (int_info.signedness) {
.signed => "true",
.unsigned => "false",
};
try writer.print(" = zig_{s}(", .{fn_name});
try f.writeCValue(writer, try f.resolveInst(operand));
try writer.print(", {d}, {s});\n", .{ int_info.bits, signed_type });
try writer.print(", {d}", .{int_info.bits});
if (options.needs_signedness_info) {
const signed_type = switch (int_info.signedness) {
.signed => "true",
.unsigned => "false",
};
try writer.print(", {s}", .{signed_type});
}
try writer.writeAll(");\n");
return local;
}

View File

@ -513,9 +513,7 @@ static inline uint128_t zig_sign_extend(uint128_t value, uint128_t zig_type_bit_
return (value ^ m) - m;
}
static inline int zig_clz(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {
(void)signed_type; // unused
static inline int zig_clz(unsigned long long value, uint8_t zig_type_bit_width) {
if (value == 0) return zig_type_bit_width;
if (zig_type_bit_width <= zig_bitsizeof(unsigned int))
return (__builtin_clz(value) - zig_bitsizeof(unsigned int) + zig_type_bit_width);
@ -524,18 +522,14 @@ static inline int zig_clz(unsigned long long value, uint8_t zig_type_bit_width,
return (__builtin_clzll(value) - zig_bitsizeof(unsigned long long) + zig_type_bit_width);
}
static inline int zig_ctz(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {
(void)signed_type; // unused
static inline int zig_ctz(unsigned long long value, uint8_t zig_type_bit_width) {
if (value == 0) return zig_type_bit_width;
if (zig_type_bit_width <= zig_bitsizeof(unsigned int)) return __builtin_ctz(value);
if (zig_type_bit_width <= zig_bitsizeof(unsigned long)) return __builtin_ctzl(value);
return __builtin_ctzll(value);
}
static inline int zig_popcount(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {
(void)signed_type; // unused
static inline int zig_popcount(unsigned long long value, uint8_t zig_type_bit_width) {
const unsigned long long mask = zig_bit_mask(unsigned long long, zig_type_bit_width);
if (zig_type_bit_width <= zig_bitsizeof(unsigned int))
return __builtin_popcount(value & mask);