| 1 | const builtin = @import("builtin"); |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const expect = std.testing.expect; |
| 5 | const assert = std.debug.assert; |
| 6 | |
| 7 | test "@splat array" { |
| 8 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 9 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 10 | |
| 11 | const Foo = struct { x: u8 }; |
| 12 | const S = struct { |
| 13 | fn testInt(x: u32) !void { |
| 14 | const arr: [10]u32 = @splat(x); |
| 15 | for (arr) |elem| { |
| 16 | try expect(x == elem); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn testStruct(x: Foo) !void { |
| 21 | const arr: [10]Foo = @splat(x); |
| 22 | for (arr) |elem| { |
| 23 | try expect(x.x == elem.x); |
| 24 | } |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | try S.testInt(123); |
| 29 | try comptime S.testInt(123); |
| 30 | |
| 31 | try S.testStruct(.{ .x = 10 }); |
| 32 | try comptime S.testStruct(.{ .x = 10 }); |
| 33 | } |
| 34 | |
| 35 | test "@splat array with sentinel" { |
| 36 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 37 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 38 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 39 | |
| 40 | const S = struct { |
| 41 | fn doTheTest(comptime T: type, x: T, comptime s: T) !void { |
| 42 | const arr: [10:s]T = @splat(x); |
| 43 | for (arr) |elem| { |
| 44 | try expect(x == elem); |
| 45 | } |
| 46 | const ptr: [*]const T = &arr; |
| 47 | try expect(s == ptr[10]); // sentinel correct |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | try S.doTheTest(u32, 100, 42); |
| 52 | try comptime S.doTheTest(u32, 100, 42); |
| 53 | |
| 54 | try S.doTheTest(?*anyopaque, @ptrFromInt(0x1000), null); |
| 55 | try comptime S.doTheTest(?*anyopaque, @ptrFromInt(0x1000), null); |
| 56 | } |
| 57 | |
| 58 | test "@splat zero-length array" { |
| 59 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 60 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 61 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 62 | |
| 63 | const S = struct { |
| 64 | fn doTheTest(comptime T: type, comptime s: T) !void { |
| 65 | var runtime_undef: T = undefined; |
| 66 | runtime_undef = undefined; |
| 67 | // The array should be comptime-known despite the `@splat` operand being runtime-known. |
| 68 | const arr: [0:s]T = @splat(runtime_undef); |
| 69 | const ptr: [*]const T = &arr; |
| 70 | comptime assert(ptr[0] == s); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | try S.doTheTest(u32, 42); |
| 75 | try comptime S.doTheTest(u32, 42); |
| 76 | |
| 77 | try S.doTheTest(?*anyopaque, null); |
| 78 | try comptime S.doTheTest(?*anyopaque, null); |
| 79 | } |
| 80 | |
| 81 | test "splat with an error union or optional result type" { |
| 82 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 83 | |
| 84 | const S = struct { |
| 85 | fn doTest(T: type) !?T { |
| 86 | return @splat(1); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | _ = try S.doTest(@Vector(4, u32)); |
| 91 | _ = try S.doTest([4]u32); |
| 92 | } |
| 93 | |
| 94 | test "read/write through global variable array of struct fields initialized via splat" { |
| 95 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 96 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 97 | |
| 98 | const S = struct { |
| 99 | fn doTheTest() !void { |
| 100 | try expect(storage[0].term == 1); |
| 101 | storage[0] = MyStruct{ .term = 123 }; |
| 102 | try expect(storage[0].term == 123); |
| 103 | } |
| 104 | |
| 105 | pub const MyStruct = struct { |
| 106 | term: usize, |
| 107 | }; |
| 108 | |
| 109 | var storage: [1]MyStruct = @splat(.{ .term = 1 }); |
| 110 | }; |
| 111 | try S.doTheTest(); |
| 112 | } |
| 113 | |
| 114 | test "vector @splat" { |
| 115 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 116 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 117 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 118 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 119 | const S = struct { |
| 120 | fn testForT(comptime N: comptime_int, v: anytype) !void { |
| 121 | const T = @TypeOf(v); |
| 122 | var vec: @Vector(N, T) = @splat(v); |
| 123 | _ = &vec; |
| 124 | const as_array = @as([N]T, vec); |
| 125 | for (as_array) |elem| try expect(v == elem); |
| 126 | } |
| 127 | fn doTheTest() !void { |
| 128 | // Splats with multiple-of-8 bit types that fill a 128bit vector. |
| 129 | try testForT(16, @as(u8, 0xEE)); |
| 130 | try testForT(8, @as(u16, 0xBEEF)); |
| 131 | try testForT(4, @as(u32, 0xDEADBEEF)); |
| 132 | try testForT(2, @as(u64, 0xCAFEF00DDEADBEEF)); |
| 133 | |
| 134 | try testForT(8, @as(f16, 3.1415)); |
| 135 | try testForT(4, @as(f32, 3.1415)); |
| 136 | try testForT(2, @as(f64, 3.1415)); |
| 137 | |
| 138 | // Same but fill more than 128 bits. |
| 139 | try testForT(16 * 2, @as(u8, 0xEE)); |
| 140 | try testForT(8 * 2, @as(u16, 0xBEEF)); |
| 141 | try testForT(4 * 2, @as(u32, 0xDEADBEEF)); |
| 142 | try testForT(2 * 2, @as(u64, 0xCAFEF00DDEADBEEF)); |
| 143 | |
| 144 | try testForT(8 * 2, @as(f16, 3.1415)); |
| 145 | try testForT(4 * 2, @as(f32, 3.1415)); |
| 146 | try testForT(2 * 2, @as(f64, 3.1415)); |
| 147 | } |
| 148 | }; |
| 149 | try S.doTheTest(); |
| 150 | try comptime S.doTheTest(); |
| 151 | } |