diff --git a/src/codegen/c.zig b/src/codegen/c.zig index c871f2d765bea5eac10790eebb9c4cff53276832..3804e2ba4f42be8f36c9d052983cfd9261d2e8ea 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -6875,6 +6875,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { }, .big => .{ false, true }, } else .{ false, false }; + const operator_wraps = use_operator and scalar_ty.intInfo(zcu).bits == 8 * scalar_ty.abiSize(zcu); const op: union(enum) { const Func = struct { operation: []const u8, info: BuiltinInfo = .none }; builtin: Func, @@ -6895,12 +6896,12 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { else => unreachable, }, .Add => switch (scalar_ty.zigTypeTag(zcu)) { - .int => if (use_operator) .{ .infix = " += " } else .{ .builtin = .{ .operation = "addw", .info = .bits } }, + .int => if (operator_wraps) .{ .infix = " += " } else .{ .builtin = .{ .operation = "addw", .info = .bits } }, .float => .{ .builtin = .{ .operation = "add" } }, else => unreachable, }, .Mul => switch (scalar_ty.zigTypeTag(zcu)) { - .int => if (use_operator) .{ .infix = " *= " } else .{ .builtin = .{ .operation = "mulw", .info = .bits } }, + .int => if (operator_wraps) .{ .infix = " *= " } else .{ .builtin = .{ .operation = "mulw", .info = .bits } }, .float => .{ .builtin = .{ .operation = "mul" } }, else => unreachable, }, diff --git a/test/behavior/vector.zig b/test/behavior/vector.zig index 02e674ecb4c748faacf31c7609c1ea507a2c6ffc..1c15acb053da4d17c052f31a1caed509dd24d5c4 100644 --- a/test/behavior/vector.zig +++ b/test/behavior/vector.zig @@ -969,6 +969,7 @@ test "vector reduce operation" { try testReduce(.Add, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 11106)); try testReduce(.Add, [4]i128{ -9, -99, -999, -9999 }, @as(i128, -11106)); try testReduce(.Add, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 11106)); + try testReduce(.Add, [4]u7{ 100, 100, 0, 0 }, @as(u7, 72)); try testReduce(.Add, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 42.9)); try testReduce(.Add, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 42.9)); try testReduce(.Add, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 42.9)); @@ -1017,6 +1018,7 @@ test "vector reduce operation" { try testReduce(.Mul, [4]u64{ 9, 99, 999, 9999 }, @as(u64, 8900199891)); try testReduce(.Mul, [4]i128{ -9, -99, -999, 9999 }, @as(i128, -8900199891)); try testReduce(.Mul, [4]u128{ 9, 99, 999, 9999 }, @as(u128, 8900199891)); + try testReduce(.Mul, [4]i3{ 3, 2, 1, 1 }, @as(i3, -2)); try testReduce(.Mul, [4]f16{ -1.9, 5.1, -60.3, 100.0 }, @as(f16, 58430.7)); try testReduce(.Mul, [4]f32{ -1.9, 5.1, -60.3, 100.0 }, @as(f32, 58430.7)); try testReduce(.Mul, [4]f64{ -1.9, 5.1, -60.3, 100.0 }, @as(f64, 58430.7));