authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2023-07-06 19:48:42+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-12 15:35:57-07:00
logb463e429b8e2b3d39592ff1c84bd886f46d21452
tree61639aa39187571c23225d4e2fbf41c21fce7f22
parent4bce7b1db964098e4a9163201fa3adcb26af6d97

Remove len parameter from splat in standard lib


7 files changed, 79 insertions(+), 59 deletions(-)

lib/std/http/protocol.zig+2-2
......@@ -182,8 +182,8 @@ pub const HeadersParser = struct {
182182
183183 const chunk = bytes[index..][0..vector_len];
184184 const v: Vector = chunk.*;
185 const matches_r = @as(BitVector, @bitCast(v == @splat(vector_len, @as(u8, '\r'))));
186 const matches_n = @as(BitVector, @bitCast(v == @splat(vector_len, @as(u8, '\n'))));
185 const matches_r = @as(BitVector, @bitCast(v == @as(Vector, @splat('\r'))));
186 const matches_n = @as(BitVector, @bitCast(v == @as(Vector, @splat('\n'))));
187187 const matches_or: SizeVector = matches_r | matches_n;
188188
189189 const matches = @reduce(.Add, matches_or);
lib/std/json/stringify_test.zig+1-1
......@@ -197,7 +197,7 @@ test "stringify struct with custom stringifier" {
197197}
198198
199199test "stringify vector" {
200 try teststringify("[1,1]", @splat(2, @as(u32, 1)), StringifyOptions{});
200 try teststringify("[1,1]", @as(@Vector(2, u32), @splat(1)), StringifyOptions{});
201201}
202202
203203test "stringify tuple" {
lib/std/math.zig+31-21
......@@ -507,8 +507,8 @@ pub fn shl(comptime T: type, a: T, shift_amt: anytype) T {
507507 if (@typeInfo(T) == .Vector) {
508508 const C = @typeInfo(T).Vector.child;
509509 const len = @typeInfo(T).Vector.len;
510 if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(len, @as(C, 0));
511 break :blk @splat(len, @as(Log2Int(C), @intCast(abs_shift_amt)));
510 if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(0);
511 break :blk @as(@Vector(len, Log2Int(C)), @splat(@as(Log2Int(C), @intCast(abs_shift_amt))));
512512 } else {
513513 if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0;
514514 break :blk @as(Log2Int(T), @intCast(abs_shift_amt));
......@@ -551,8 +551,8 @@ pub fn shr(comptime T: type, a: T, shift_amt: anytype) T {
551551 if (@typeInfo(T) == .Vector) {
552552 const C = @typeInfo(T).Vector.child;
553553 const len = @typeInfo(T).Vector.len;
554 if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(len, @as(C, 0));
555 break :blk @splat(len, @as(Log2Int(C), @intCast(abs_shift_amt)));
554 if (abs_shift_amt >= @typeInfo(C).Int.bits) return @splat(0);
555 break :blk @as(@Vector(len, Log2Int(C)), @splat(@as(Log2Int(C), @intCast(abs_shift_amt))));
556556 } else {
557557 if (abs_shift_amt >= @typeInfo(T).Int.bits) return 0;
558558 break :blk @as(Log2Int(T), @intCast(abs_shift_amt));
......@@ -597,7 +597,7 @@ pub fn rotr(comptime T: type, x: T, r: anytype) T {
597597 @compileError("cannot rotate signed integers");
598598 }
599599 const ar = @as(Log2Int(C), @intCast(@mod(r, @typeInfo(C).Int.bits)));
600 return (x >> @splat(@typeInfo(T).Vector.len, ar)) | (x << @splat(@typeInfo(T).Vector.len, 1 + ~ar));
600 return (x >> @splat(ar)) | (x << @splat(1 + ~ar));
601601 } else if (@typeInfo(T).Int.signedness == .signed) {
602602 @compileError("cannot rotate signed integer");
603603 } else {
......@@ -641,7 +641,7 @@ pub fn rotl(comptime T: type, x: T, r: anytype) T {
641641 @compileError("cannot rotate signed integers");
642642 }
643643 const ar = @as(Log2Int(C), @intCast(@mod(r, @typeInfo(C).Int.bits)));
644 return (x << @splat(@typeInfo(T).Vector.len, ar)) | (x >> @splat(@typeInfo(T).Vector.len, 1 +% ~ar));
644 return (x << @splat(ar)) | (x >> @splat(1 +% ~ar));
645645 } else if (@typeInfo(T).Int.signedness == .signed) {
646646 @compileError("cannot rotate signed integer");
647647 } else {
......@@ -794,10 +794,10 @@ pub fn absInt(x: anytype) !@TypeOf(x) {
794794 switch (@typeInfo(vinfo.child)) {
795795 .Int => |info| {
796796 comptime assert(info.signedness == .signed); // must pass a signed integer to absInt
797 if (@reduce(.Or, x == @splat(vinfo.len, @as(vinfo.child, minInt(vinfo.child))))) {
797 if (@reduce(.Or, x == @as(T, @splat(minInt(vinfo.child))))) {
798798 return error.Overflow;
799799 }
800 const zero = @splat(vinfo.len, @as(vinfo.child, 0));
800 const zero: T = @splat(0);
801801 break :blk @select(vinfo.child, x > zero, x, -x);
802802 },
803803 else => @compileError("Expected vector of ints, found " ++ @typeName(T)),
......@@ -1368,9 +1368,9 @@ pub fn lerp(a: anytype, b: anytype, t: anytype) @TypeOf(a, b, t) {
13681368
13691369 switch (@typeInfo(Type)) {
13701370 .Float, .ComptimeFloat => assert(t >= 0 and t <= 1),
1371 .Vector => |vector| {
1372 const lower_bound = @reduce(.And, t >= @splat(vector.len, @as(vector.child, 0)));
1373 const upper_bound = @reduce(.And, t <= @splat(vector.len, @as(vector.child, 1)));
1371 .Vector => {
1372 const lower_bound = @reduce(.And, t >= @as(Type, @splat(0)));
1373 const upper_bound = @reduce(.And, t <= @as(Type, @splat(1)));
13741374 assert(lower_bound and upper_bound);
13751375 },
13761376 else => comptime unreachable,
......@@ -1392,14 +1392,24 @@ test "lerp" {
13921392 try testing.expectEqual(@as(f32, 1.0), lerp(@as(f32, 1.0e7), 1.0, 1.0));
13931393 try testing.expectEqual(@as(f64, 1.0), lerp(@as(f64, 1.0e15), 1.0, 1.0));
13941394
1395 try testing.expectEqual(
1396 lerp(@splat(3, @as(f32, 0)), @splat(3, @as(f32, 50)), @splat(3, @as(f32, 0.5))),
1397 @Vector(3, f32){ 25, 25, 25 },
1398 );
1399 try testing.expectEqual(
1400 lerp(@splat(3, @as(f64, 50)), @splat(3, @as(f64, 100)), @splat(3, @as(f64, 0.5))),
1401 @Vector(3, f64){ 75, 75, 75 },
1402 );
1395 {
1396 const a: @Vector(3, f32) = @splat(0);
1397 const b: @Vector(3, f32) = @splat(50);
1398 const t: @Vector(3, f32) = @splat(0.5);
1399 try testing.expectEqual(
1400 lerp(a, b, t),
1401 @Vector(3, f32){ 25, 25, 25 },
1402 );
1403 }
1404 {
1405 const a: @Vector(3, f64) = @splat(50);
1406 const b: @Vector(3, f64) = @splat(100);
1407 const t: @Vector(3, f64) = @splat(0.5);
1408 try testing.expectEqual(
1409 lerp(a, b, t),
1410 @Vector(3, f64){ 75, 75, 75 },
1411 );
1412 }
14031413}
14041414
14051415/// Returns the maximum value of integer type T.
......@@ -1719,8 +1729,8 @@ pub inline fn sign(i: anytype) @TypeOf(i) {
17191729 .Vector => |vinfo| blk: {
17201730 switch (@typeInfo(vinfo.child)) {
17211731 .Int, .Float => {
1722 const zero = @splat(vinfo.len, @as(vinfo.child, 0));
1723 const one = @splat(vinfo.len, @as(vinfo.child, 1));
1732 const zero: T = @splat(0);
1733 const one: T = @splat(1);
17241734 break :blk @select(vinfo.child, i > zero, one, zero) - @select(vinfo.child, i < zero, one, zero);
17251735 },
17261736 else => @compileError("Expected vector of ints or floats, found " ++ @typeName(T)),
lib/std/mem.zig+4-4
......@@ -289,7 +289,7 @@ pub fn zeroes(comptime T: type) T {
289289 return [_]info.child{zeroes(info.child)} ** info.len;
290290 },
291291 .Vector => |info| {
292 return @splat(info.len, zeroes(info.child));
292 return @splat(zeroes(info.child));
293293 },
294294 .Union => |info| {
295295 if (comptime meta.containerLayout(T) == .Extern) {
......@@ -393,9 +393,9 @@ test "zeroes" {
393393 for (b.array) |e| {
394394 try testing.expectEqual(@as(u32, 0), e);
395395 }
396 try testing.expectEqual(@splat(2, @as(u32, 0)), b.vector_u32);
397 try testing.expectEqual(@splat(2, @as(f32, 0.0)), b.vector_f32);
398 try testing.expectEqual(@splat(2, @as(bool, false)), b.vector_bool);
396 try testing.expectEqual(@as(@TypeOf(b.vector_u32), @splat(0)), b.vector_u32);
397 try testing.expectEqual(@as(@TypeOf(b.vector_f32), @splat(0.0)), b.vector_f32);
398 try testing.expectEqual(@as(@TypeOf(b.vector_bool), @splat(false)), b.vector_bool);
399399 try testing.expectEqual(@as(?u8, null), b.optional_int);
400400 for (b.sentinel) |e| {
401401 try testing.expectEqual(@as(u8, 0), e);
lib/std/meta.zig+4-3
......@@ -860,9 +860,10 @@ test "std.meta.eql" {
860860 try testing.expect(eql(EU.tst(false), EU.tst(false)));
861861 try testing.expect(!eql(EU.tst(false), EU.tst(true)));
862862
863 var v1 = @splat(4, @as(u32, 1));
864 var v2 = @splat(4, @as(u32, 1));
865 var v3 = @splat(4, @as(u32, 2));
863 const V = @Vector(4, u32);
864 var v1: V = @splat(1);
865 var v2: V = @splat(1);
866 var v3: V = @splat(2);
866867
867868 try testing.expect(eql(v1, v2));
868869 try testing.expect(!eql(v1, v3));
lib/std/simd.zig+34-25
......@@ -107,7 +107,7 @@ pub inline fn iota(comptime T: type, comptime len: usize) @Vector(len, T) {
107107pub fn repeat(comptime len: usize, vec: anytype) @Vector(len, std.meta.Child(@TypeOf(vec))) {
108108 const Child = std.meta.Child(@TypeOf(vec));
109109
110 return @shuffle(Child, vec, undefined, iota(i32, len) % @splat(len, @as(i32, @intCast(vectorLength(@TypeOf(vec))))));
110 return @shuffle(Child, vec, undefined, iota(i32, len) % @as(@Vector(len, i32), @splat(@intCast(vectorLength(@TypeOf(vec))))));
111111}
112112
113113/// Returns a vector containing all elements of the first vector at the lower indices followed by all elements of the second vector
......@@ -147,11 +147,12 @@ pub fn interlace(vecs: anytype) @Vector(vectorLength(@TypeOf(vecs[0])) * vecs.le
147147 const len = a_len + b_len;
148148
149149 const indices = comptime blk: {
150 const Vi32 = @Vector(len, i32);
150151 const count_up = iota(i32, len);
151 const cycle = @divFloor(count_up, @splat(len, @as(i32, @intCast(vecs_arr.len))));
152 const select_mask = repeat(len, join(@splat(a_vec_count, true), @splat(b_vec_count, false)));
153 const a_indices = count_up - cycle * @splat(len, @as(i32, @intCast(b_vec_count)));
154 const b_indices = shiftElementsRight(count_up - cycle * @splat(len, @as(i32, @intCast(a_vec_count))), a_vec_count, 0);
152 const cycle = @divFloor(count_up, @as(Vi32, @splat(@intCast(vecs_arr.len))));
153 const select_mask = repeat(len, join(@as(@Vector(a_vec_count, bool), @splat(true)), @as(@Vector(b_vec_count, bool), @splat(false))));
154 const a_indices = count_up - cycle * @as(Vi32, @splat(@intCast(b_vec_count)));
155 const b_indices = shiftElementsRight(count_up - cycle * @as(Vi32, @splat(@intCast(a_vec_count))), a_vec_count, 0);
155156 break :blk @select(i32, select_mask, a_indices, ~b_indices);
156157 };
157158
......@@ -174,7 +175,7 @@ pub fn deinterlace(
174175
175176 comptime var i: usize = 0; // for-loops don't work for this, apparently.
176177 inline while (i < out.len) : (i += 1) {
177 const indices = comptime iota(i32, vec_len) * @splat(vec_len, @as(i32, @intCast(vec_count))) + @splat(vec_len, @as(i32, @intCast(i)));
178 const indices = comptime iota(i32, vec_len) * @as(@Vector(vec_len, i32), @splat(@intCast(vec_count))) + @as(@Vector(vec_len, i32), @splat(@intCast(i)));
178179 out[i] = @shuffle(Child, interlaced, undefined, indices);
179180 }
180181
......@@ -191,7 +192,7 @@ pub fn extract(
191192
192193 std.debug.assert(@as(comptime_int, @intCast(first)) + @as(comptime_int, @intCast(count)) <= len);
193194
194 return @shuffle(Child, vec, undefined, iota(i32, count) + @splat(count, @as(i32, @intCast(first))));
195 return @shuffle(Child, vec, undefined, iota(i32, count) + @as(@Vector(count, i32), @splat(@intCast(first))));
195196}
196197
197198test "vector patterns" {
......@@ -236,17 +237,18 @@ pub fn shiftElementsRight(vec: anytype, comptime amount: VectorCount(@TypeOf(vec
236237 // It may be possible to implement shifts and rotates with a runtime-friendly slice of two joined vectors, as the length of the
237238 // slice would be comptime-known. This would permit vector shifts and rotates by a non-comptime-known amount.
238239 // However, I am unsure whether compiler optimizations would handle that well enough on all platforms.
239 const len = vectorLength(@TypeOf(vec));
240 const V = @TypeOf(vec);
241 const len = vectorLength(V);
240242
241 return mergeShift(@splat(len, shift_in), vec, len - amount);
243 return mergeShift(@as(V, @splat(shift_in)), vec, len - amount);
242244}
243245
244246/// Elements are shifted leftwards (towards lower indices). New elements are added to the right, and the leftmost elements are cut off
245247/// so that no elements with indices below 0 remain.
246248pub fn shiftElementsLeft(vec: anytype, comptime amount: VectorCount(@TypeOf(vec)), shift_in: std.meta.Child(@TypeOf(vec))) @TypeOf(vec) {
247 const len = vectorLength(@TypeOf(vec));
249 const V = @TypeOf(vec);
248250
249 return mergeShift(vec, @splat(len, shift_in), amount);
251 return mergeShift(vec, @as(V, @splat(shift_in)), amount);
250252}
251253
252254/// Elements are shifted leftwards (towards lower indices). Elements that leave to the left will reappear to the right in the same order.
......@@ -263,7 +265,7 @@ pub fn reverseOrder(vec: anytype) @TypeOf(vec) {
263265 const Child = std.meta.Child(@TypeOf(vec));
264266 const len = vectorLength(@TypeOf(vec));
265267
266 return @shuffle(Child, vec, undefined, @splat(len, @as(i32, @intCast(len)) - 1) - iota(i32, len));
268 return @shuffle(Child, vec, undefined, @as(@Vector(len, i32), @splat(@as(i32, @intCast(len)) - 1)) - iota(i32, len));
267269}
268270
269271test "vector shifting" {
......@@ -283,7 +285,8 @@ pub fn firstTrue(vec: anytype) ?VectorIndex(@TypeOf(vec)) {
283285 if (!@reduce(.Or, vec)) {
284286 return null;
285287 }
286 const indices = @select(IndexInt, vec, iota(IndexInt, len), @splat(len, ~@as(IndexInt, 0)));
288 const all_max: @Vector(len, IndexInt) = @splat(~@as(IndexInt, 0));
289 const indices = @select(IndexInt, vec, iota(IndexInt, len), all_max);
287290 return @reduce(.Min, indices);
288291}
289292
......@@ -294,7 +297,9 @@ pub fn lastTrue(vec: anytype) ?VectorIndex(@TypeOf(vec)) {
294297 if (!@reduce(.Or, vec)) {
295298 return null;
296299 }
297 const indices = @select(IndexInt, vec, iota(IndexInt, len), @splat(len, @as(IndexInt, 0)));
300
301 const all_zeroes: @Vector(len, IndexInt) = @splat(0);
302 const indices = @select(IndexInt, vec, iota(IndexInt, len), all_zeroes);
298303 return @reduce(.Max, indices);
299304}
300305
......@@ -302,26 +307,29 @@ pub fn countTrues(vec: anytype) VectorCount(@TypeOf(vec)) {
302307 const len = vectorLength(@TypeOf(vec));
303308 const CountIntType = VectorCount(@TypeOf(vec));
304309
305 const one_if_true = @select(CountIntType, vec, @splat(len, @as(CountIntType, 1)), @splat(len, @as(CountIntType, 0)));
310 const all_ones: @Vector(len, CountIntType) = @splat(1);
311 const all_zeroes: @Vector(len, CountIntType) = @splat(0);
312
313 const one_if_true = @select(CountIntType, vec, all_ones, all_zeroes);
306314 return @reduce(.Add, one_if_true);
307315}
308316
309317pub fn firstIndexOfValue(vec: anytype, value: std.meta.Child(@TypeOf(vec))) ?VectorIndex(@TypeOf(vec)) {
310 const len = vectorLength(@TypeOf(vec));
318 const V = @TypeOf(vec);
311319
312 return firstTrue(vec == @splat(len, value));
320 return firstTrue(vec == @as(V, @splat(value)));
313321}
314322
315323pub fn lastIndexOfValue(vec: anytype, value: std.meta.Child(@TypeOf(vec))) ?VectorIndex(@TypeOf(vec)) {
316 const len = vectorLength(@TypeOf(vec));
324 const V = @TypeOf(vec);
317325
318 return lastTrue(vec == @splat(len, value));
326 return lastTrue(vec == @as(V, @splat(value)));
319327}
320328
321329pub fn countElementsWithValue(vec: anytype, value: std.meta.Child(@TypeOf(vec))) VectorCount(@TypeOf(vec)) {
322 const len = vectorLength(@TypeOf(vec));
330 const V = @TypeOf(vec);
323331
324 return countTrues(vec == @splat(len, value));
332 return countTrues(vec == @as(V, @splat(value)));
325333}
326334
327335test "vector searching" {
......@@ -370,7 +378,6 @@ pub fn prefixScanWithFunc(
370378pub fn prefixScan(comptime op: std.builtin.ReduceOp, comptime hop: isize, vec: anytype) @TypeOf(vec) {
371379 const VecType = @TypeOf(vec);
372380 const Child = std.meta.Child(VecType);
373 const len = vectorLength(VecType);
374381
375382 const identity = comptime switch (@typeInfo(Child)) {
376383 .Bool => switch (op) {
......@@ -397,8 +404,8 @@ pub fn prefixScan(comptime op: std.builtin.ReduceOp, comptime hop: isize, vec: a
397404 const fn_container = struct {
398405 fn opFn(a: VecType, b: VecType) VecType {
399406 return if (Child == bool) switch (op) {
400 .And => @select(bool, a, b, @splat(len, false)),
401 .Or => @select(bool, a, @splat(len, true), b),
407 .And => @select(bool, a, b, @as(VecType, @splat(false))),
408 .Or => @select(bool, a, @as(VecType, @splat(true)), b),
402409 .Xor => a != b,
403410 else => unreachable,
404411 } else switch (op) {
......@@ -431,7 +438,9 @@ test "vector prefix scan" {
431438 const float_base = @Vector(4, f32){ 2, 0.5, -10, 6.54321 };
432439 const bool_base = @Vector(4, bool){ true, false, true, false };
433440
434 try std.testing.expectEqual(iota(u8, 32) + @splat(32, @as(u8, 1)), prefixScan(.Add, 1, @splat(32, @as(u8, 1))));
441 const ones: @Vector(32, u8) = @splat(1);
442
443 try std.testing.expectEqual(iota(u8, 32) + ones, prefixScan(.Add, 1, ones));
435444 try std.testing.expectEqual(@Vector(4, i32){ 11, 3, 1, 1 }, prefixScan(.And, 1, int_base));
436445 try std.testing.expectEqual(@Vector(4, i32){ 11, 31, 31, -1 }, prefixScan(.Or, 1, int_base));
437446 try std.testing.expectEqual(@Vector(4, i32){ 11, 28, 21, -2 }, prefixScan(.Xor, 1, int_base));
lib/std/testing.zig+3-3
......@@ -606,8 +606,8 @@ test "expectEqual nested array" {
606606}
607607
608608test "expectEqual vector" {
609 var a = @splat(4, @as(u32, 4));
610 var b = @splat(4, @as(u32, 4));
609 var a: @Vector(4, u32) = @splat(4);
610 var b: @Vector(4, u32) = @splat(4);
611611
612612 try expectEqual(a, b);
613613}
......@@ -903,7 +903,7 @@ test "expectEqualDeep composite type" {
903903 try expectEqualDeep([_][]const u8{ "a", "b", "c" }, [_][]const u8{ "a", "b", "c" });
904904
905905 // vector
906 try expectEqualDeep(@splat(4, @as(u32, 4)), @splat(4, @as(u32, 4)));
906 try expectEqualDeep(@as(@Vector(4, u32), @splat(4)), @as(@Vector(4, u32), @splat(4)));
907907
908908 // nested array
909909 {