authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-13 13:12:18-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-02-13 13:12:18-05:00
log1675d4f82b94b4db0272aff483760cd526963a4c
tree1c2f56508d70b602d9520eb7a593a3079c54e540
parentfa377dbd1510fc733607afe47dc04dfe55d5ff88
parentf93c219f30b4b4f3bb9280d39be7b585ddcd8447
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4443 from LemonBoy/werkzeug

A train of small patches

7 files changed, 81 insertions(+), 22 deletions(-)

lib/std/meta.zig+15
...@@ -433,6 +433,14 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {...@@ -433,6 +433,14 @@ pub fn eql(a: var, b: @TypeOf(a)) bool {
433 if (!eql(e, b[i])) return false;433 if (!eql(e, b[i])) return false;
434 return true;434 return true;
435 },435 },
436 builtin.TypeId.Vector => {
437 const info = @typeInfo(T).Vector;
438 var i: usize = 0;
439 while (i < info.len) : (i += 1) {
440 if (!eql(a[i], b[i])) return false;
441 }
442 return true;
443 },
436 builtin.TypeId.Pointer => {444 builtin.TypeId.Pointer => {
437 const info = @typeInfo(T).Pointer;445 const info = @typeInfo(T).Pointer;
438 switch (info.size) {446 switch (info.size) {
...@@ -510,6 +518,13 @@ test "std.meta.eql" {...@@ -510,6 +518,13 @@ test "std.meta.eql" {
510 testing.expect(eql(EU.tst(true), EU.tst(true)));518 testing.expect(eql(EU.tst(true), EU.tst(true)));
511 testing.expect(eql(EU.tst(false), EU.tst(false)));519 testing.expect(eql(EU.tst(false), EU.tst(false)));
512 testing.expect(!eql(EU.tst(false), EU.tst(true)));520 testing.expect(!eql(EU.tst(false), EU.tst(true)));
521
522 var v1 = @splat(4, @as(u32, 1));
523 var v2 = @splat(4, @as(u32, 1));
524 var v3 = @splat(4, @as(u32, 2));
525
526 testing.expect(eql(v1, v2));
527 testing.expect(!eql(v1, v3));
513}528}
514529
515test "intToEnum with error return" {530test "intToEnum with error return" {
lib/std/os/test.zig+8-11
...@@ -276,8 +276,11 @@ test "mmap" {...@@ -276,8 +276,11 @@ test "mmap" {
276 testing.expectEqual(@as(usize, 1234), data.len);276 testing.expectEqual(@as(usize, 1234), data.len);
277277
278 // By definition the data returned by mmap is zero-filled278 // By definition the data returned by mmap is zero-filled
279 std.mem.set(u8, data[0 .. data.len - 1], 0x55);279 testing.expect(mem.eql(u8, data, &[_]u8{0x00} ** 1234));
280 testing.expect(mem.indexOfScalar(u8, data, 0).? == 1234 - 1);280
281 // Make sure the memory is writeable as requested
282 std.mem.set(u8, data, 0x55);
283 testing.expect(mem.eql(u8, data, &[_]u8{0x55} ** 1234));
281 }284 }
282285
283 const test_out_file = "os_tmp_test";286 const test_out_file = "os_tmp_test";
...@@ -300,10 +303,7 @@ test "mmap" {...@@ -300,10 +303,7 @@ test "mmap" {
300303
301 // Map the whole file304 // Map the whole file
302 {305 {
303 const file = try fs.cwd().createFile(test_out_file, .{306 const file = try fs.cwd().openFile(test_out_file, .{});
304 .read = true,
305 .truncate = false,
306 });
307 defer file.close();307 defer file.close();
308308
309 const data = try os.mmap(309 const data = try os.mmap(
...@@ -327,15 +327,12 @@ test "mmap" {...@@ -327,15 +327,12 @@ test "mmap" {
327327
328 // Map the upper half of the file328 // Map the upper half of the file
329 {329 {
330 const file = try fs.cwd().createFile(test_out_file, .{330 const file = try fs.cwd().openFile(test_out_file, .{});
331 .read = true,
332 .truncate = false,
333 });
334 defer file.close();331 defer file.close();
335332
336 const data = try os.mmap(333 const data = try os.mmap(
337 null,334 null,
338 alloc_size,335 alloc_size / 2,
339 os.PROT_READ,336 os.PROT_READ,
340 os.MAP_PRIVATE,337 os.MAP_PRIVATE,
341 file.handle,338 file.handle,
lib/std/testing.zig+16-1
...@@ -56,7 +56,6 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {...@@ -56,7 +56,6 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
56 .EnumLiteral,56 .EnumLiteral,
57 .Enum,57 .Enum,
58 .Fn,58 .Fn,
59 .Vector,
60 .ErrorSet,59 .ErrorSet,
61 => {60 => {
62 if (actual != expected) {61 if (actual != expected) {
...@@ -88,6 +87,15 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {...@@ -88,6 +87,15 @@ pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
8887
89 .Array => |array| expectEqualSlices(array.child, &expected, &actual),88 .Array => |array| expectEqualSlices(array.child, &expected, &actual),
9089
90 .Vector => |vectorType| {
91 var i: usize = 0;
92 while (i < vectorType.len) : (i += 1) {
93 if (!std.meta.eql(expected[i], actual[i])) {
94 std.debug.panic("index {} incorrect. expected {}, found {}", .{ i, expected[i], actual[i] });
95 }
96 }
97 },
98
91 .Struct => |structType| {99 .Struct => |structType| {
92 inline for (structType.fields) |field| {100 inline for (structType.fields) |field| {
93 expectEqual(@field(expected, field.name), @field(actual, field.name));101 expectEqual(@field(expected, field.name), @field(actual, field.name));
...@@ -202,3 +210,10 @@ test "expectEqual nested array" {...@@ -202,3 +210,10 @@ test "expectEqual nested array" {
202210
203 expectEqual(a, b);211 expectEqual(a, b);
204}212}
213
214test "expectEqual vector" {
215 var a = @splat(4, @as(u32, 4));
216 var b = @splat(4, @as(u32, 4));
217
218 expectEqual(a, b);
219}
src/analyze.cpp+2-2
...@@ -6937,9 +6937,9 @@ static void render_const_val_array(CodeGen *g, Buf *buf, Buf *type_name, ZigValu...@@ -6937,9 +6937,9 @@ static void render_const_val_array(CodeGen *g, Buf *buf, Buf *type_name, ZigValu
6937 return;6937 return;
6938 }6938 }
6939 case ConstArraySpecialNone: {6939 case ConstArraySpecialNone: {
6940 ZigValue *base = &array->data.s_none.elements[start];
6941 assert(base != nullptr);
6942 assert(start + len <= const_val->type->data.array.len);6940 assert(start + len <= const_val->type->data.array.len);
6941 ZigValue *base = &array->data.s_none.elements[start];
6942 assert(len == 0 || base != nullptr);
69436943
6944 buf_appendf(buf, "%s{", buf_ptr(type_name));6944 buf_appendf(buf, "%s{", buf_ptr(type_name));
6945 for (uint64_t i = 0; i < len; i += 1) {6945 for (uint64_t i = 0; i < len; i += 1) {
src/ir.cpp+13-1
...@@ -14840,6 +14840,16 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,...@@ -14840,6 +14840,16 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
14840 }14840 }
14841 }14841 }
1484214842
14843 // @Vector(N,T1) to @Vector(N,T2)
14844 if (actual_type->id == ZigTypeIdVector && wanted_type->id == ZigTypeIdVector) {
14845 if (actual_type->data.vector.len == wanted_type->data.vector.len &&
14846 types_match_const_cast_only(ira, wanted_type->data.vector.elem_type,
14847 actual_type->data.vector.elem_type, source_node, false).id == ConstCastResultIdOk)
14848 {
14849 return ir_analyze_bit_cast(ira, source_instr, value, wanted_type);
14850 }
14851 }
14852
14843 // *@Frame(func) to anyframe->T or anyframe14853 // *@Frame(func) to anyframe->T or anyframe
14844 // *@Frame(func) to ?anyframe->T or ?anyframe14854 // *@Frame(func) to ?anyframe->T or ?anyframe
14845 // *@Frame(func) to E!anyframe->T or E!anyframe14855 // *@Frame(func) to E!anyframe->T or E!anyframe
...@@ -16409,9 +16419,11 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i...@@ -16409,9 +16419,11 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
16409 case ZigTypeIdComptimeInt:16419 case ZigTypeIdComptimeInt:
16410 case ZigTypeIdInt:16420 case ZigTypeIdInt:
16411 case ZigTypeIdFloat:16421 case ZigTypeIdFloat:
16412 case ZigTypeIdVector:
16413 zig_unreachable(); // handled with the type_is_numeric checks above16422 zig_unreachable(); // handled with the type_is_numeric checks above
1641416423
16424 case ZigTypeIdVector:
16425 // Not every case is handled by the type_is_numeric checks above,
16426 // vectors of bool trigger this code path
16415 case ZigTypeIdBool:16427 case ZigTypeIdBool:
16416 case ZigTypeIdMetaType:16428 case ZigTypeIdMetaType:
16417 case ZigTypeIdVoid:16429 case ZigTypeIdVoid:
src/link.cpp-7
...@@ -2642,13 +2642,6 @@ void codegen_link(CodeGen *g) {...@@ -2642,13 +2642,6 @@ void codegen_link(CodeGen *g) {
2642 lj.rpath_table.init(4);2642 lj.rpath_table.init(4);
2643 lj.codegen = g;2643 lj.codegen = g;
26442644
2645 if (g->verbose_llvm_ir) {
2646 fprintf(stderr, "\nOptimization:\n");
2647 fprintf(stderr, "---------------\n");
2648 fflush(stderr);
2649 LLVMDumpModule(g->module);
2650 }
2651
2652 if (g->out_type == OutTypeObj) {2645 if (g->out_type == OutTypeObj) {
2653 lj.args.append("-r");2646 lj.args.append("-r");
2654 }2647 }
test/stage1/behavior/vector.zig+27
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const mem = std.mem;2const mem = std.mem;
3const expect = std.testing.expect;3const expect = std.testing.expect;
4const expectEqual = std.testing.expectEqual;
4const builtin = @import("builtin");5const builtin = @import("builtin");
56
6test "implicit cast vector to array - bool" {7test "implicit cast vector to array - bool" {
...@@ -250,3 +251,29 @@ test "initialize vector which is a struct field" {...@@ -250,3 +251,29 @@ test "initialize vector which is a struct field" {
250 S.doTheTest();251 S.doTheTest();
251 comptime S.doTheTest();252 comptime S.doTheTest();
252}253}
254
255test "vector comparison operators" {
256 const S = struct {
257 fn doTheTest() void {
258 {
259 const v1: @Vector(4, bool) = [_]bool{ true, false, true, false };
260 const v2: @Vector(4, bool) = [_]bool{ false, true, false, true };
261 expectEqual(@splat(4, true), v1 == v1);
262 expectEqual(@splat(4, false), v1 == v2);
263 expectEqual(@splat(4, true), v1 != v2);
264 expectEqual(@splat(4, false), v2 != v2);
265 }
266 {
267 const v1 = @splat(4, @as(u32, 0xc0ffeeee));
268 const v2: @Vector(4, c_uint) = v1;
269 const v3 = @splat(4, @as(u32, 0xdeadbeef));
270 expectEqual(@splat(4, true), v1 == v2);
271 expectEqual(@splat(4, false), v1 == v3);
272 expectEqual(@splat(4, true), v1 != v3);
273 expectEqual(@splat(4, false), v1 != v2);
274 }
275 }
276 };
277 S.doTheTest();
278 comptime S.doTheTest();
279}