authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-05 06:32:23-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-05 06:32:55-05:00
log1efd36cd5c9a1128ae702b081d60ee32f21bc258
tree78f20b88bdbaa755b38ab36b2b2c9b23cb9cd470
parent7352d461cff72d92b07cf2d2b7ee17714005b9cf

CBE: fix reduce of emulated integers


1 files changed, 29 insertions(+), 17 deletions(-)

src/codegen/c.zig+29-17
...@@ -6672,33 +6672,43 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6672,33 +6672,43 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
6672 const operand_ty = f.air.typeOf(reduce.operand);6672 const operand_ty = f.air.typeOf(reduce.operand);
6673 const writer = f.object.writer();6673 const writer = f.object.writer();
66746674
6675 const use_operator = scalar_ty.bitSize(target) <= 64;
6675 const op: union(enum) {6676 const op: union(enum) {
6676 float_op: []const u8,6677 const Func = struct { operation: []const u8, info: BuiltinInfo = .none };
6677 builtin: []const u8,6678 float_op: Func,
6679 builtin: Func,
6678 infix: []const u8,6680 infix: []const u8,
6679 ternary: []const u8,6681 ternary: []const u8,
6680 } = switch (reduce.operation) {6682 } = switch (reduce.operation) {
6681 .And => .{ .infix = " &= " },6683 .And => if (use_operator) .{ .infix = " &= " } else .{ .builtin = .{ .operation = "and" } },
6682 .Or => .{ .infix = " |= " },6684 .Or => if (use_operator) .{ .infix = " |= " } else .{ .builtin = .{ .operation = "or" } },
6683 .Xor => .{ .infix = " ^= " },6685 .Xor => if (use_operator) .{ .infix = " ^= " } else .{ .builtin = .{ .operation = "xor" } },
6684 .Min => switch (scalar_ty.zigTypeTag()) {6686 .Min => switch (scalar_ty.zigTypeTag()) {
6685 .Int => .{ .ternary = " < " },6687 .Int => if (use_operator) .{ .ternary = " < " } else .{
6686 .Float => .{ .float_op = "fmin" },6688 .builtin = .{ .operation = "min" },
6689 },
6690 .Float => .{ .float_op = .{ .operation = "fmin" } },
6687 else => unreachable,6691 else => unreachable,
6688 },6692 },
6689 .Max => switch (scalar_ty.zigTypeTag()) {6693 .Max => switch (scalar_ty.zigTypeTag()) {
6690 .Int => .{ .ternary = " > " },6694 .Int => if (use_operator) .{ .ternary = " > " } else .{
6691 .Float => .{ .float_op = "fmax" },6695 .builtin = .{ .operation = "max" },
6696 },
6697 .Float => .{ .float_op = .{ .operation = "fmax" } },
6692 else => unreachable,6698 else => unreachable,
6693 },6699 },
6694 .Add => switch (scalar_ty.zigTypeTag()) {6700 .Add => switch (scalar_ty.zigTypeTag()) {
6695 .Int => .{ .infix = " += " },6701 .Int => if (use_operator) .{ .infix = " += " } else .{
6696 .Float => .{ .builtin = "add" },6702 .builtin = .{ .operation = "addw", .info = .bits },
6703 },
6704 .Float => .{ .builtin = .{ .operation = "add" } },
6697 else => unreachable,6705 else => unreachable,
6698 },6706 },
6699 .Mul => switch (scalar_ty.zigTypeTag()) {6707 .Mul => switch (scalar_ty.zigTypeTag()) {
6700 .Int => .{ .infix = " *= " },6708 .Int => if (use_operator) .{ .infix = " *= " } else .{
6701 .Float => .{ .builtin = "mul" },6709 .builtin = .{ .operation = "mulw", .info = .bits },
6710 },
6711 .Float => .{ .builtin = .{ .operation = "mul" } },
6702 else => unreachable,6712 else => unreachable,
6703 },6713 },
6704 };6714 };
...@@ -6762,24 +6772,26 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -6762,24 +6772,26 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
6762 const v = try Vectorizer.start(f, inst, writer, operand_ty);6772 const v = try Vectorizer.start(f, inst, writer, operand_ty);
6763 try f.writeCValue(writer, accum, .Other);6773 try f.writeCValue(writer, accum, .Other);
6764 switch (op) {6774 switch (op) {
6765 .float_op => |operation| {6775 .float_op => |func| {
6766 try writer.writeAll(" = zig_libc_name_");6776 try writer.writeAll(" = zig_libc_name_");
6767 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);6777 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
6768 try writer.print("({s})(", .{operation});6778 try writer.print("({s})(", .{func.operation});
6769 try f.writeCValue(writer, accum, .FunctionArgument);6779 try f.writeCValue(writer, accum, .FunctionArgument);
6770 try writer.writeAll(", ");6780 try writer.writeAll(", ");
6771 try f.writeCValue(writer, operand, .Other);6781 try f.writeCValue(writer, operand, .Other);
6772 try v.elem(f, writer);6782 try v.elem(f, writer);
6783 try f.object.dg.renderBuiltinInfo(writer, scalar_ty, func.info);
6773 try writer.writeByte(')');6784 try writer.writeByte(')');
6774 },6785 },
6775 .builtin => |operation| {6786 .builtin => |func| {
6776 try writer.print(" = zig_{s}_", .{operation});6787 try writer.print(" = zig_{s}_", .{func.operation});
6777 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);6788 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
6778 try writer.writeByte('(');6789 try writer.writeByte('(');
6779 try f.writeCValue(writer, accum, .FunctionArgument);6790 try f.writeCValue(writer, accum, .FunctionArgument);
6780 try writer.writeAll(", ");6791 try writer.writeAll(", ");
6781 try f.writeCValue(writer, operand, .Other);6792 try f.writeCValue(writer, operand, .Other);
6782 try v.elem(f, writer);6793 try v.elem(f, writer);
6794 try f.object.dg.renderBuiltinInfo(writer, scalar_ty, func.info);
6783 try writer.writeByte(')');6795 try writer.writeByte(')');
6784 },6796 },
6785 .infix => |ass| {6797 .infix => |ass| {