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 {
66726672 const operand_ty = f.air.typeOf(reduce.operand);
66736673 const writer = f.object.writer();
66746674
6675 const use_operator = scalar_ty.bitSize(target) <= 64;
66756676 const op: union(enum) {
6676 float_op: []const u8,
6677 builtin: []const u8,
6677 const Func = struct { operation: []const u8, info: BuiltinInfo = .none };
6678 float_op: Func,
6679 builtin: Func,
66786680 infix: []const u8,
66796681 ternary: []const u8,
66806682 } = switch (reduce.operation) {
6681 .And => .{ .infix = " &= " },
6682 .Or => .{ .infix = " |= " },
6683 .Xor => .{ .infix = " ^= " },
6683 .And => if (use_operator) .{ .infix = " &= " } else .{ .builtin = .{ .operation = "and" } },
6684 .Or => if (use_operator) .{ .infix = " |= " } else .{ .builtin = .{ .operation = "or" } },
6685 .Xor => if (use_operator) .{ .infix = " ^= " } else .{ .builtin = .{ .operation = "xor" } },
66846686 .Min => switch (scalar_ty.zigTypeTag()) {
6685 .Int => .{ .ternary = " < " },
6686 .Float => .{ .float_op = "fmin" },
6687 .Int => if (use_operator) .{ .ternary = " < " } else .{
6688 .builtin = .{ .operation = "min" },
6689 },
6690 .Float => .{ .float_op = .{ .operation = "fmin" } },
66876691 else => unreachable,
66886692 },
66896693 .Max => switch (scalar_ty.zigTypeTag()) {
6690 .Int => .{ .ternary = " > " },
6691 .Float => .{ .float_op = "fmax" },
6694 .Int => if (use_operator) .{ .ternary = " > " } else .{
6695 .builtin = .{ .operation = "max" },
6696 },
6697 .Float => .{ .float_op = .{ .operation = "fmax" } },
66926698 else => unreachable,
66936699 },
66946700 .Add => switch (scalar_ty.zigTypeTag()) {
6695 .Int => .{ .infix = " += " },
6696 .Float => .{ .builtin = "add" },
6701 .Int => if (use_operator) .{ .infix = " += " } else .{
6702 .builtin = .{ .operation = "addw", .info = .bits },
6703 },
6704 .Float => .{ .builtin = .{ .operation = "add" } },
66976705 else => unreachable,
66986706 },
66996707 .Mul => switch (scalar_ty.zigTypeTag()) {
6700 .Int => .{ .infix = " *= " },
6701 .Float => .{ .builtin = "mul" },
6708 .Int => if (use_operator) .{ .infix = " *= " } else .{
6709 .builtin = .{ .operation = "mulw", .info = .bits },
6710 },
6711 .Float => .{ .builtin = .{ .operation = "mul" } },
67026712 else => unreachable,
67036713 },
67046714 };
......@@ -6762,24 +6772,26 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
67626772 const v = try Vectorizer.start(f, inst, writer, operand_ty);
67636773 try f.writeCValue(writer, accum, .Other);
67646774 switch (op) {
6765 .float_op => |operation| {
6775 .float_op => |func| {
67666776 try writer.writeAll(" = zig_libc_name_");
67676777 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
6768 try writer.print("({s})(", .{operation});
6778 try writer.print("({s})(", .{func.operation});
67696779 try f.writeCValue(writer, accum, .FunctionArgument);
67706780 try writer.writeAll(", ");
67716781 try f.writeCValue(writer, operand, .Other);
67726782 try v.elem(f, writer);
6783 try f.object.dg.renderBuiltinInfo(writer, scalar_ty, func.info);
67736784 try writer.writeByte(')');
67746785 },
6775 .builtin => |operation| {
6776 try writer.print(" = zig_{s}_", .{operation});
6786 .builtin => |func| {
6787 try writer.print(" = zig_{s}_", .{func.operation});
67776788 try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty);
67786789 try writer.writeByte('(');
67796790 try f.writeCValue(writer, accum, .FunctionArgument);
67806791 try writer.writeAll(", ");
67816792 try f.writeCValue(writer, operand, .Other);
67826793 try v.elem(f, writer);
6794 try f.object.dg.renderBuiltinInfo(writer, scalar_ty, func.info);
67836795 try writer.writeByte(')');
67846796 },
67856797 .infix => |ass| {