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...@@ -1709,11 +1709,11 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
1709 .memcpy => try airMemcpy(f, inst),1709 .memcpy => try airMemcpy(f, inst),
1710 .set_union_tag => try airSetUnionTag(f, inst),1710 .set_union_tag => try airSetUnionTag(f, inst),
1711 .get_union_tag => try airGetUnionTag(f, inst),1711 .get_union_tag => try airGetUnionTag(f, inst),
1712 .clz => try airBuiltinCall(f, inst, "clz"),1712 .clz => try airBuiltinCall(f, inst, "clz", .{}),
1713 .ctz => try airBuiltinCall(f, inst, "ctz"),1713 .ctz => try airBuiltinCall(f, inst, "ctz", .{}),
1714 .popcount => try airBuiltinCall(f, inst, "popcount"),1714 .popcount => try airBuiltinCall(f, inst, "popcount", .{}),
1715 .byte_swap => try airBuiltinCall(f, inst, "byte_swap"),1715 .byte_swap => try airBuiltinCall(f, inst, "byte_swap", .{ .needs_signedness_info = true }),
1716 .bit_reverse => try airBuiltinCall(f, inst, "bit_reverse"),1716 .bit_reverse => try airBuiltinCall(f, inst, "bit_reverse", .{ .needs_signedness_info = true }),
1717 .tag_name => try airTagName(f, inst),1717 .tag_name => try airTagName(f, inst),
1718 .error_name => try airErrorName(f, inst),1718 .error_name => try airErrorName(f, inst),
1719 .splat => try airSplat(f, inst),1719 .splat => try airSplat(f, inst),
...@@ -3351,7 +3351,7 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -3351,7 +3351,7 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
3351 return local;3351 return local;
3352}3352}
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 {
3355 if (f.liveness.isUnused(inst)) return CValue.none;3355 if (f.liveness.isUnused(inst)) return CValue.none;
33563356
3357 const inst_ty = f.air.typeOfIndex(inst);3357 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...@@ -3364,14 +3364,18 @@ fn airBuiltinCall(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8) !C
3364 const int_info = operand_ty.intInfo(target);3364 const int_info = operand_ty.intInfo(target);
3365 _ = toCIntBits(int_info.bits) orelse3365 _ = toCIntBits(int_info.bits) orelse
3366 return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{});3366 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
3372 try writer.print(" = zig_{s}(", .{fn_name});3368 try writer.print(" = zig_{s}(", .{fn_name});
3373 try f.writeCValue(writer, try f.resolveInst(operand));3369 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");
3375 return local;3379 return local;
3376}3380}
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_...@@ -513,9 +513,7 @@ static inline uint128_t zig_sign_extend(uint128_t value, uint128_t zig_type_bit_
513 return (value ^ m) - m;513 return (value ^ m) - m;
514}514}
515515
516static inline int zig_clz(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {516static inline int zig_clz(unsigned long long value, uint8_t zig_type_bit_width) {
517 (void)signed_type; // unused
518
519 if (value == 0) return zig_type_bit_width;517 if (value == 0) return zig_type_bit_width;
520 if (zig_type_bit_width <= zig_bitsizeof(unsigned int))518 if (zig_type_bit_width <= zig_bitsizeof(unsigned int))
521 return (__builtin_clz(value) - zig_bitsizeof(unsigned int) + zig_type_bit_width);519 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,...@@ -524,18 +522,14 @@ static inline int zig_clz(unsigned long long value, uint8_t zig_type_bit_width,
524 return (__builtin_clzll(value) - zig_bitsizeof(unsigned long long) + zig_type_bit_width);522 return (__builtin_clzll(value) - zig_bitsizeof(unsigned long long) + zig_type_bit_width);
525}523}
526524
527static inline int zig_ctz(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {525static inline int zig_ctz(unsigned long long value, uint8_t zig_type_bit_width) {
528 (void)signed_type; // unused
529
530 if (value == 0) return zig_type_bit_width;526 if (value == 0) return zig_type_bit_width;
531 if (zig_type_bit_width <= zig_bitsizeof(unsigned int)) return __builtin_ctz(value);527 if (zig_type_bit_width <= zig_bitsizeof(unsigned int)) return __builtin_ctz(value);
532 if (zig_type_bit_width <= zig_bitsizeof(unsigned long)) return __builtin_ctzl(value);528 if (zig_type_bit_width <= zig_bitsizeof(unsigned long)) return __builtin_ctzl(value);
533 return __builtin_ctzll(value);529 return __builtin_ctzll(value);
534}530}
535531
536static inline int zig_popcount(unsigned long long value, uint8_t zig_type_bit_width, bool signed_type) {532static inline int zig_popcount(unsigned long long value, uint8_t zig_type_bit_width) {
537 (void)signed_type; // unused
538
539 const unsigned long long mask = zig_bit_mask(unsigned long long, zig_type_bit_width);533 const unsigned long long mask = zig_bit_mask(unsigned long long, zig_type_bit_width);
540 if (zig_type_bit_width <= zig_bitsizeof(unsigned int))534 if (zig_type_bit_width <= zig_bitsizeof(unsigned int))
541 return __builtin_popcount(value & mask);535 return __builtin_popcount(value & mask);