authorgravatar for daniele.cocca@gmail.comDaniele Cocca <daniele.cocca@gmail.com> 2022-03-14 01:04:24+00:00
committergravatar for daniele.cocca@gmail.comDaniele Cocca <daniele.cocca@gmail.com> 2022-03-14 01:04:24+00:00
logd912699e0853e8fe1a9dd2c863f3c163d123c407
tree15ee5b0ad3ec0bb5b6a65ddc890c2f7eae2cc05a
parent5a971bbeeaa58e66e5a10243a9716aabac72bdd0

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.

2 files changed, 18 insertions(+), 20 deletions(-)

src/codegen/c.zig+15-11
......@@ -1709,11 +1709,11 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
17091709 .memcpy => try airMemcpy(f, inst),
17101710 .set_union_tag => try airSetUnionTag(f, inst),
17111711 .get_union_tag => try airGetUnionTag(f, inst),
1712 .clz => try airBuiltinCall(f, inst, "clz"),
1713 .ctz => try airBuiltinCall(f, inst, "ctz"),
1714 .popcount => try airBuiltinCall(f, inst, "popcount"),
1715 .byte_swap => try airBuiltinCall(f, inst, "byte_swap"),
1716 .bit_reverse => try airBuiltinCall(f, inst, "bit_reverse"),
1712 .clz => try airBuiltinCall(f, inst, "clz", .{}),
1713 .ctz => try airBuiltinCall(f, inst, "ctz", .{}),
1714 .popcount => try airBuiltinCall(f, inst, "popcount", .{}),
1715 .byte_swap => try airBuiltinCall(f, inst, "byte_swap", .{ .needs_signedness_info = true }),
1716 .bit_reverse => try airBuiltinCall(f, inst, "bit_reverse", .{ .needs_signedness_info = true }),
17171717 .tag_name => try airTagName(f, inst),
17181718 .error_name => try airErrorName(f, inst),
17191719 .splat => try airSplat(f, inst),
......@@ -3351,7 +3351,7 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
33513351 return local;
33523352}
33533353
3354fn airBuiltinCall(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8) !CValue {
3354fn airBuiltinCall(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8, options: struct { needs_signedness_info: bool = false }) !CValue {
33553355 if (f.liveness.isUnused(inst)) return CValue.none;
33563356
33573357 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
33643364 const int_info = operand_ty.intInfo(target);
33653365 _ = toCIntBits(int_info.bits) orelse
33663366 return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{});
3367 const signed_type = switch (int_info.signedness) {
3368 .signed => "true",
3369 .unsigned => "false",
3370 };
33713367
33723368 try writer.print(" = zig_{s}(", .{fn_name});
33733369 try f.writeCValue(writer, try f.resolveInst(operand));
3374 try writer.print(", {d}, {s});\n", .{ int_info.bits, signed_type });
3370 try writer.print(", {d}", .{int_info.bits});
3371 if (options.needs_signedness_info) {
3372 const signed_type = switch (int_info.signedness) {
3373 .signed => "true",
3374 .unsigned => "false",
3375 };
3376 try writer.print(", {s}", .{signed_type});
3377 }
3378 try writer.writeAll(");\n");
33753379 return local;
33763380}
33773381
src/link/C/zig.h+3-9
......@@ -513,9 +513,7 @@ static inline uint128_t zig_sign_extend(uint128_t value, uint128_t zig_type_bit_
513513 return (value ^ m) - m;
514514}
515515
516static inline int zig_clz(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {
517 (void)signed_type; // unused
518
516static inline int zig_clz(unsigned long long value, uint8_t zig_type_bit_width) {
519517 if (value == 0) return zig_type_bit_width;
520518 if (zig_type_bit_width <= zig_bitsizeof(unsigned int))
521519 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,
524522 return (__builtin_clzll(value) - zig_bitsizeof(unsigned long long) + zig_type_bit_width);
525523}
526524
527static inline int zig_ctz(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {
528 (void)signed_type; // unused
529
525static inline int zig_ctz(unsigned long long value, uint8_t zig_type_bit_width) {
530526 if (value == 0) return zig_type_bit_width;
531527 if (zig_type_bit_width <= zig_bitsizeof(unsigned int)) return __builtin_ctz(value);
532528 if (zig_type_bit_width <= zig_bitsizeof(unsigned long)) return __builtin_ctzl(value);
533529 return __builtin_ctzll(value);
534530}
535531
536static inline int zig_popcount(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {
537 (void)signed_type; // unused
538
532static inline int zig_popcount(unsigned long long value, uint8_t zig_type_bit_width) {
539533 const unsigned long long mask = zig_bit_mask(unsigned long long, zig_type_bit_width);
540534 if (zig_type_bit_width <= zig_bitsizeof(unsigned int))
541535 return __builtin_popcount(value & mask);