authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-05 15:00:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-20 18:33:00-07:00
log5ec0a7d8a5201fb35334ce62f82c5958b6ba296e
tree1874ea546d4b71e1c02f3cd37e7b2747f3f791e6
parent9b74651cd2b799a73518de26c8293636f910c833

coerce vectors to arrays rather than inline for


8 files changed, 72 insertions(+), 116 deletions(-)

lib/std/Io/Writer.zig+21-15
......@@ -1370,19 +1370,12 @@ pub fn printValue(
13701370 },
13711371 .array => {
13721372 if (!is_any) @compileError("cannot format array without a specifier (i.e. {s} or {any})");
1373 if (max_depth == 0) return w.writeAll("{ ... }");
1374 try w.writeAll("{ ");
1375 for (value, 0..) |elem, i| {
1376 try w.printValue(fmt, options, elem, max_depth - 1);
1377 if (i < value.len - 1) {
1378 try w.writeAll(", ");
1379 }
1380 }
1381 try w.writeAll(" }");
1373 return printArray(w, fmt, options, &value, max_depth);
13821374 },
1383 .vector => {
1375 .vector => |vector| {
13841376 if (!is_any and fmt.len != 0) invalidFmtError(fmt, value);
1385 return printVector(w, fmt, options, value, max_depth);
1377 const array: [vector.len]vector.child = value;
1378 return printArray(w, fmt, options, &array, max_depth);
13861379 },
13871380 .@"fn" => @compileError("unable to format function body type, use '*const " ++ @typeName(T) ++ "' for a function pointer type"),
13881381 .type => {
......@@ -1436,12 +1429,25 @@ pub fn printVector(
14361429 value: anytype,
14371430 max_depth: usize,
14381431) Error!void {
1439 const len = @typeInfo(@TypeOf(value)).vector.len;
1432 const vector = @typeInfo(@TypeOf(value)).vector;
1433 const array: [vector.len]vector.child = value;
1434 return printArray(w, fmt, options, &array, max_depth);
1435}
1436
1437pub fn printArray(
1438 w: *Writer,
1439 comptime fmt: []const u8,
1440 options: std.fmt.Options,
1441 ptr_to_array: anytype,
1442 max_depth: usize,
1443) Error!void {
14401444 if (max_depth == 0) return w.writeAll("{ ... }");
14411445 try w.writeAll("{ ");
1442 inline for (0..len) |i| {
1443 try w.printValue(fmt, options, value[i], max_depth - 1);
1444 if (i < len - 1) try w.writeAll(", ");
1446 for (ptr_to_array, 0..) |elem, i| {
1447 try w.printValue(fmt, options, elem, max_depth - 1);
1448 if (i < ptr_to_array.len - 1) {
1449 try w.writeAll(", ");
1450 }
14451451 }
14461452 try w.writeAll(" }");
14471453}
lib/std/crypto/chacha20.zig+10-10
......@@ -215,7 +215,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type
215215 }
216216 }
217217
218 fn hashToBytes(comptime dm: usize, out: *[64 * dm]u8, x: BlockVec) void {
218 fn hashToBytes(comptime dm: usize, out: *[64 * dm]u8, x: *const BlockVec) void {
219219 inline for (0..dm) |d| {
220220 for (0..4) |i| {
221221 mem.writeInt(u32, out[64 * d + 16 * i + 0 ..][0..4], x[i][0 + 4 * d], .little);
......@@ -242,7 +242,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type
242242 while (degree >= d and i + 64 * d <= in.len) : (i += 64 * d) {
243243 chacha20Core(x[0..], ctx);
244244 contextFeedback(&x, ctx);
245 hashToBytes(d, buf[0 .. 64 * d], x);
245 hashToBytes(d, buf[0 .. 64 * d], &x);
246246
247247 var xout = out[i..];
248248 const xin = in[i..];
......@@ -266,7 +266,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type
266266 if (i < in.len) {
267267 chacha20Core(x[0..], ctx);
268268 contextFeedback(&x, ctx);
269 hashToBytes(1, buf[0..64], x);
269 hashToBytes(1, buf[0..64], &x);
270270
271271 var xout = out[i..];
272272 const xin = in[i..];
......@@ -284,7 +284,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type
284284 while (degree >= d and i + 64 * d <= out.len) : (i += 64 * d) {
285285 chacha20Core(x[0..], ctx);
286286 contextFeedback(&x, ctx);
287 hashToBytes(d, out[i..][0 .. 64 * d], x);
287 hashToBytes(d, out[i..][0 .. 64 * d], &x);
288288 inline for (0..d) |d_| {
289289 if (count64) {
290290 const next = @addWithOverflow(ctx[3][4 * d_], d);
......@@ -301,7 +301,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize, comptime degree: comptime_int) type
301301 contextFeedback(&x, ctx);
302302
303303 var buf: [64]u8 = undefined;
304 hashToBytes(1, buf[0..], x);
304 hashToBytes(1, buf[0..], &x);
305305 @memcpy(out[i..], buf[0 .. out.len - i]);
306306 }
307307 }
......@@ -394,7 +394,7 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type {
394394 }
395395 }
396396
397 fn hashToBytes(out: *[64]u8, x: BlockVec) void {
397 fn hashToBytes(out: *[64]u8, x: *const BlockVec) void {
398398 for (0..4) |i| {
399399 mem.writeInt(u32, out[16 * i + 0 ..][0..4], x[i * 4 + 0], .little);
400400 mem.writeInt(u32, out[16 * i + 4 ..][0..4], x[i * 4 + 1], .little);
......@@ -417,7 +417,7 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type {
417417 while (i + 64 <= in.len) : (i += 64) {
418418 chacha20Core(x[0..], ctx);
419419 contextFeedback(&x, ctx);
420 hashToBytes(buf[0..], x);
420 hashToBytes(buf[0..], &x);
421421
422422 var xout = out[i..];
423423 const xin = in[i..];
......@@ -438,7 +438,7 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type {
438438 if (i < in.len) {
439439 chacha20Core(x[0..], ctx);
440440 contextFeedback(&x, ctx);
441 hashToBytes(buf[0..], x);
441 hashToBytes(buf[0..], &x);
442442
443443 var xout = out[i..];
444444 const xin = in[i..];
......@@ -455,7 +455,7 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type {
455455 while (i + 64 <= out.len) : (i += 64) {
456456 chacha20Core(x[0..], ctx);
457457 contextFeedback(&x, ctx);
458 hashToBytes(out[i..][0..64], x);
458 hashToBytes(out[i..][0..64], &x);
459459 if (count64) {
460460 const next = @addWithOverflow(ctx[12], 1);
461461 ctx[12] = next[0];
......@@ -469,7 +469,7 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type {
469469 contextFeedback(&x, ctx);
470470
471471 var buf: [64]u8 = undefined;
472 hashToBytes(buf[0..], x);
472 hashToBytes(buf[0..], &x);
473473 @memcpy(out[i..], buf[0 .. out.len - i]);
474474 }
475475 }
lib/std/json/static.zig+5-24
......@@ -440,10 +440,11 @@ pub fn innerParse(
440440 }
441441 },
442442
443 .vector => |vecInfo| {
443 .vector => |vector_info| {
444444 switch (try source.peekNextTokenType()) {
445445 .array_begin => {
446 return internalParseVector(T, vecInfo.child, vecInfo.len, allocator, source, options);
446 const A = [vector_info.len]vector_info.child;
447 return try internalParseArray(A, vector_info.child, allocator, source, options);
447448 },
448449 else => return error.UnexpectedToken,
449450 }
......@@ -535,26 +536,6 @@ fn internalParseArray(
535536 return r;
536537}
537538
538fn internalParseVector(
539 comptime T: type,
540 comptime Child: type,
541 comptime len: comptime_int,
542 allocator: Allocator,
543 source: anytype,
544 options: ParseOptions,
545) !T {
546 assert(.array_begin == try source.next());
547
548 var r: T = undefined;
549 inline for (0..len) |i| {
550 r[i] = try innerParse(Child, allocator, source, options);
551 }
552
553 if (.array_end != try source.next()) return error.UnexpectedToken;
554
555 return r;
556}
557
558539/// This is an internal function called recursively
559540/// during the implementation of `parseFromValueLeaky`.
560541/// It is exposed primarily to enable custom `jsonParseFromValue()` methods to call back into the `parseFromValue*` system,
......@@ -587,12 +568,12 @@ pub fn innerParseFromValue(
587568 if (@round(f) != f) return error.InvalidNumber;
588569 if (f > @as(@TypeOf(f), @floatFromInt(std.math.maxInt(T)))) return error.Overflow;
589570 if (f < @as(@TypeOf(f), @floatFromInt(std.math.minInt(T)))) return error.Overflow;
590 return @as(T, @intFromFloat(f));
571 return @intFromFloat(f);
591572 },
592573 .integer => |i| {
593574 if (i > std.math.maxInt(T)) return error.Overflow;
594575 if (i < std.math.minInt(T)) return error.Overflow;
595 return @as(T, @intCast(i));
576 return @intCast(i);
596577 },
597578 .number_string, .string => |s| {
598579 return sliceToInt(T, s);
lib/std/meta.zig+1-6
......@@ -742,12 +742,7 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {
742742 if (!eql(e, b[i])) return false;
743743 return true;
744744 },
745 .vector => |info| {
746 inline for (0..info.len) |i| {
747 if (a[i] != b[i]) return false;
748 }
749 return true;
750 },
745 .vector => return @reduce(.And, a == b),
751746 .pointer => |info| {
752747 return switch (info.size) {
753748 .one, .many, .c => a == b,
lib/std/testing.zig+3-8
......@@ -135,14 +135,9 @@ fn expectEqualInner(comptime T: type, expected: T, actual: T) !void {
135135 .array => |array| try expectEqualSlices(array.child, &expected, &actual),
136136
137137 .vector => |info| {
138 inline for (0..info.len) |i| {
139 if (expected[i] != actual[i]) {
140 print("index {d} incorrect. expected {any}, found {any}\n", .{
141 i, expected[i], actual[i],
142 });
143 return error.TestExpectedEqual;
144 }
145 }
138 const expect_array: [info.len]info.child = expected;
139 const actual_array: [info.len]info.child = actual;
140 try expectEqualSlices(info.child, &expect_array, &actual_array);
146141 },
147142
148143 .@"struct" => |structType| {
lib/std/zon/Serializer.zig+15-16
......@@ -157,13 +157,11 @@ pub fn valueArbitraryDepth(self: *Serializer, val: anytype, options: ValueOption
157157 }
158158 },
159159 .array => {
160 var container = try self.beginTuple(
161 .{ .whitespace_style = .{ .fields = val.len } },
162 );
163 for (val) |item_val| {
164 try container.fieldArbitraryDepth(item_val, options);
165 }
166 try container.end();
160 try valueArbitraryDepthArray(self, @TypeOf(val), &val, options);
161 },
162 .vector => |vector| {
163 const array: [vector.len]vector.child = val;
164 try valueArbitraryDepthArray(self, @TypeOf(array), &array, options);
167165 },
168166 .@"struct" => |@"struct"| if (@"struct".is_tuple) {
169167 var container = try self.beginTuple(
......@@ -231,20 +229,21 @@ pub fn valueArbitraryDepth(self: *Serializer, val: anytype, options: ValueOption
231229 } else {
232230 try self.writer.writeAll("null");
233231 },
234 .vector => |vector| {
235 var container = try self.beginTuple(
236 .{ .whitespace_style = .{ .fields = vector.len } },
237 );
238 inline for (0..vector.len) |i| {
239 try container.fieldArbitraryDepth(val[i], options);
240 }
241 try container.end();
242 },
243232
244233 else => comptime unreachable,
245234 }
246235}
247236
237fn valueArbitraryDepthArray(s: *Serializer, comptime A: type, array: *const A, options: ValueOptions) Error!void {
238 var container = try s.beginTuple(
239 .{ .whitespace_style = .{ .fields = array.len } },
240 );
241 for (array) |elem| {
242 try container.fieldArbitraryDepth(elem, options);
243 }
244 try container.end();
245}
246
248247/// Serialize an integer.
249248pub fn int(self: *Serializer, val: anytype) Error!void {
250249 try self.writer.printInt(val, 10, .lower, .{});
lib/std/zon/parse.zig+16-33
......@@ -430,8 +430,12 @@ pub fn free(gpa: Allocator, value: anytype) void {
430430 .many, .c => comptime unreachable,
431431 }
432432 },
433 .array => for (value) |item| {
434 free(gpa, item);
433 .array => {
434 freeArray(gpa, @TypeOf(value), &value);
435 },
436 .vector => |vector| {
437 const array: [vector.len]vector.child = value;
438 freeArray(gpa, @TypeOf(array), &array);
435439 },
436440 .@"struct" => |@"struct"| inline for (@"struct".fields) |field| {
437441 free(gpa, @field(value, field.name));
......@@ -446,12 +450,15 @@ pub fn free(gpa: Allocator, value: anytype) void {
446450 .optional => if (value) |some| {
447451 free(gpa, some);
448452 },
449 .vector => |vector| inline for (0..vector.len) |i| free(gpa, value[i]),
450453 .void => {},
451454 else => comptime unreachable,
452455 }
453456}
454457
458fn freeArray(gpa: Allocator, comptime A: type, array: *const A) void {
459 for (array) |elem| free(gpa, elem);
460}
461
455462fn requiresAllocator(T: type) bool {
456463 _ = valid_types;
457464 return switch (@typeInfo(T)) {
......@@ -521,12 +528,15 @@ const Parser = struct {
521528 else => comptime unreachable,
522529 },
523530 .array => return self.parseArray(T, node),
531 .vector => |vector| {
532 const A = [vector.len]vector.child;
533 return try self.parseArray(A, node);
534 },
524535 .@"struct" => |@"struct"| if (@"struct".is_tuple)
525536 return self.parseTuple(T, node)
526537 else
527538 return self.parseStruct(T, node),
528539 .@"union" => return self.parseUnion(T, node),
529 .vector => return self.parseVector(T, node),
530540
531541 else => comptime unreachable,
532542 }
......@@ -999,33 +1009,6 @@ const Parser = struct {
9991009 }
10001010 }
10011011
1002 fn parseVector(self: *@This(), T: type, node: Zoir.Node.Index) !T {
1003 const vector_info = @typeInfo(T).vector;
1004
1005 const nodes: Zoir.Node.Index.Range = switch (node.get(self.zoir)) {
1006 .array_literal => |nodes| nodes,
1007 .empty_literal => .{ .start = node, .len = 0 },
1008 else => return error.WrongType,
1009 };
1010
1011 var result: T = undefined;
1012
1013 if (nodes.len != vector_info.len) {
1014 return self.failNodeFmt(
1015 node,
1016 "expected {} vector elements; found {}",
1017 .{ vector_info.len, nodes.len },
1018 );
1019 }
1020
1021 inline for (0..vector_info.len) |i| {
1022 errdefer inline for (0..i) |j| free(self.gpa, result[j]);
1023 result[i] = try self.parseExpr(vector_info.child, nodes.at(@intCast(i)));
1024 }
1025
1026 return result;
1027 }
1028
10291012 fn failTokenFmt(
10301013 self: @This(),
10311014 token: Ast.TokenIndex,
......@@ -3206,7 +3189,7 @@ test "std.zon vector" {
32063189 fromSlice(@Vector(2, f32), gpa, ".{0.5}", &diag, .{}),
32073190 );
32083191 try std.testing.expectFmt(
3209 "1:2: error: expected 2 vector elements; found 1\n",
3192 "1:2: error: expected 2 array elements; found 1\n",
32103193 "{f}",
32113194 .{diag},
32123195 );
......@@ -3221,7 +3204,7 @@ test "std.zon vector" {
32213204 fromSlice(@Vector(2, f32), gpa, ".{0.5, 1.5, 2.5}", &diag, .{}),
32223205 );
32233206 try std.testing.expectFmt(
3224 "1:2: error: expected 2 vector elements; found 3\n",
3207 "1:13: error: index 2 outside of array of length 2\n",
32253208 "{f}",
32263209 .{diag},
32273210 );
test/behavior/math.zig+1-4
......@@ -139,10 +139,7 @@ fn expectVectorsEqual(a: anytype, b: anytype) !void {
139139 const len_a = @typeInfo(@TypeOf(a)).vector.len;
140140 const len_b = @typeInfo(@TypeOf(b)).vector.len;
141141 try expect(len_a == len_b);
142
143 inline for (0..len_a) |i| {
144 try expect(a[i] == b[i]);
145 }
142 try expect(@reduce(.And, a == b));
146143}
147144
148145test "@ctz" {