authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-13 22:21:16-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-14 00:11:46-07:00
logd42d31f72f38165f70c2850e9cc63da44b3b470c
tree9ba0f7964691a492585cf7dcbe53f1879895d2e6
parenteeaaefb925a659f7bab1beea35b7c1b5f567d027

basic language features do not belong in std.meta


3 files changed, 38 insertions(+), 32 deletions(-)

doc/langref.html.in+36-31
......@@ -2515,13 +2515,14 @@ test "null terminated array" {
25152515
25162516 {#header_open|Vectors#}
25172517 <p>
2518 A vector is a group of booleans, {#link|Integers#}, {#link|Floats#}, or {#link|Pointers#} which are operated on
2519 in parallel using SIMD instructions. Vector types are created with the builtin function {#link|@Type#},
2520 or using the shorthand function {#syntax#}std.meta.Vector{#endsyntax#}.
2518 A vector is a group of booleans, {#link|Integers#}, {#link|Floats#}, or
2519 {#link|Pointers#} which are operated on in parallel, using SIMD instructions if possible.
2520 Vector types are created with the builtin function {#link|@Vector#}.
25212521 </p>
25222522 <p>
2523 Vectors support the same builtin operators as their underlying base types. These operations are performed
2524 element-wise, and return a vector of the same length as the input vectors. This includes:
2523 Vectors support the same builtin operators as their underlying base types.
2524 These operations are performed element-wise, and return a vector of the same length
2525 as the input vectors. This includes:
25252526 </p>
25262527 <ul>
25272528 <li>Arithmetic ({#syntax#}+{#endsyntax#}, {#syntax#}-{#endsyntax#}, {#syntax#}/{#endsyntax#}, {#syntax#}*{#endsyntax#},
......@@ -2532,10 +2533,11 @@ test "null terminated array" {
25322533 <li>Comparison operators ({#syntax#}<{#endsyntax#}, {#syntax#}>{#endsyntax#}, {#syntax#}=={#endsyntax#}, etc.)</li>
25332534 </ul>
25342535 <p>
2535 It is prohibited to use a math operator on a mixture of scalars (individual numbers) and vectors.
2536 Zig provides the {#link|@splat#} builtin to easily convert from scalars to vectors, and it supports {#link|@reduce#}
2537 and array indexing syntax to convert from vectors to scalars. Vectors also support assignment to and from
2538 fixed-length arrays with comptime known length.
2536 It is prohibited to use a math operator on a mixture of scalars (individual numbers)
2537 and vectors. Zig provides the {#link|@splat#} builtin to easily convert from scalars
2538 to vectors, and it supports {#link|@reduce#} and array indexing syntax to convert
2539 from vectors to scalars. Vectors also support assignment to and from fixed-length
2540 arrays with comptime known length.
25392541 </p>
25402542 <p>
25412543 For rearranging elements within and between vectors, Zig provides the {#link|@shuffle#} and {#link|@select#} functions.
......@@ -2550,16 +2552,14 @@ test "null terminated array" {
25502552 </p>
25512553 {#code_begin|test|vector_example#}
25522554const std = @import("std");
2553const Vector = std.meta.Vector;
25542555const expectEqual = std.testing.expectEqual;
25552556
25562557test "Basic vector usage" {
2557 // Vectors have a compile-time known length and base type,
2558 // and can be assigned to using array literal syntax
2559 const a: Vector(4, i32) = [_]i32{ 1, 2, 3, 4 };
2560 const b: Vector(4, i32) = [_]i32{ 5, 6, 7, 8 };
2558 // Vectors have a compile-time known length and base type.
2559 const a = @Vector(4, i32){ 1, 2, 3, 4 };
2560 const b = @Vector(4, i32){ 5, 6, 7, 8 };
25612561
2562 // Math operations take place element-wise
2562 // Math operations take place element-wise.
25632563 const c = a + b;
25642564
25652565 // Individual vector elements can be accessed using array indexing syntax.
......@@ -2572,19 +2572,19 @@ test "Basic vector usage" {
25722572test "Conversion between vectors, arrays, and slices" {
25732573 // Vectors and fixed-length arrays can be automatically assigned back and forth
25742574 var arr1: [4]f32 = [_]f32{ 1.1, 3.2, 4.5, 5.6 };
2575 var vec: Vector(4, f32) = arr1;
2575 var vec: @Vector(4, f32) = arr1;
25762576 var arr2: [4]f32 = vec;
25772577 try expectEqual(arr1, arr2);
25782578
25792579 // You can also assign from a slice with comptime-known length to a vector using .*
2580 const vec2: Vector(2, f32) = arr1[1..3].*;
2580 const vec2: @Vector(2, f32) = arr1[1..3].*;
25812581
25822582 var slice: []const f32 = &arr1;
25832583 var offset: u32 = 1;
25842584 // To extract a comptime-known length from a runtime-known offset,
25852585 // first extract a new slice from the starting offset, then an array of
25862586 // comptime known length
2587 const vec3: Vector(2, f32) = slice[offset..][0..2].*;
2587 const vec3: @Vector(2, f32) = slice[offset..][0..2].*;
25882588 try expectEqual(slice[offset], vec2[0]);
25892589 try expectEqual(slice[offset + 1], vec2[1]);
25902590 try expectEqual(vec2, vec3);
......@@ -9084,7 +9084,7 @@ pub const PrefetchOptions = struct {
90849084 {#header_close#}
90859085
90869086 {#header_open|@select#}
9087 <pre>{#syntax#}@select(comptime T: type, pred: std.meta.Vector(len, bool), a: std.meta.Vector(len, T), b: std.meta.Vector(len, T)) std.meta.Vector(len, T){#endsyntax#}</pre>
9087 <pre>{#syntax#}@select(comptime T: type, pred: @Vector(len, bool), a: @Vector(len, T), b: @Vector(len, T)) @Vector(len, T){#endsyntax#}</pre>
90889088 <p>
90899089 Selects values element-wise from {#syntax#}a{#endsyntax#} or {#syntax#}b{#endsyntax#} based on {#syntax#}pred{#endsyntax#}. If {#syntax#}pred[i]{#endsyntax#} is {#syntax#}true{#endsyntax#}, the corresponding element in the result will be {#syntax#}a[i]{#endsyntax#} and otherwise {#syntax#}b[i]{#endsyntax#}.
90909090 </p>
......@@ -9252,7 +9252,7 @@ test "@setRuntimeSafety" {
92529252 {#header_close#}
92539253
92549254 {#header_open|@shuffle#}
9255 <pre>{#syntax#}@shuffle(comptime E: type, a: std.meta.Vector(a_len, E), b: std.meta.Vector(b_len, E), comptime mask: std.meta.Vector(mask_len, i32)) std.meta.Vector(mask_len, E){#endsyntax#}</pre>
9255 <pre>{#syntax#}@shuffle(comptime E: type, a: @Vector(a_len, E), b: @Vector(b_len, E), comptime mask: @Vector(mask_len, i32)) @Vector(mask_len, E){#endsyntax#}</pre>
92569256 <p>
92579257 Constructs a new {#link|vector|Vectors#} by selecting elements from {#syntax#}a{#endsyntax#} and
92589258 {#syntax#}b{#endsyntax#} based on {#syntax#}mask{#endsyntax#}.
......@@ -9287,22 +9287,21 @@ test "@setRuntimeSafety" {
92879287 </p>
92889288 {#code_begin|test|vector_shuffle#}
92899289const std = @import("std");
9290const Vector = std.meta.Vector;
92919290const expect = std.testing.expect;
92929291
92939292test "vector @shuffle" {
9294 const a: Vector(7, u8) = [_]u8{ 'o', 'l', 'h', 'e', 'r', 'z', 'w' };
9295 const b: Vector(4, u8) = [_]u8{ 'w', 'd', '!', 'x' };
9293 const a = @Vector(7, u8){ 'o', 'l', 'h', 'e', 'r', 'z', 'w' };
9294 const b = @Vector(4, u8){ 'w', 'd', '!', 'x' };
92969295
92979296 // To shuffle within a single vector, pass undefined as the second argument.
92989297 // Notice that we can re-order, duplicate, or omit elements of the input vector
9299 const mask1: Vector(5, i32) = [_]i32{ 2, 3, 1, 1, 0 };
9300 const res1: Vector(5, u8) = @shuffle(u8, a, undefined, mask1);
9298 const mask1 = @Vector(5, i32){ 2, 3, 1, 1, 0 };
9299 const res1: @Vector(5, u8) = @shuffle(u8, a, undefined, mask1);
93019300 try expect(std.mem.eql(u8, &@as([5]u8, res1), "hello"));
93029301
93039302 // Combining two vectors
9304 const mask2: Vector(6, i32) = [_]i32{ -1, 0, 4, 1, -2, -3 };
9305 const res2: Vector(6, u8) = @shuffle(u8, a, b, mask2);
9303 const mask2 = @Vector(6, i32){ -1, 0, 4, 1, -2, -3 };
9304 const res2: @Vector(6, u8) = @shuffle(u8, a, b, mask2);
93069305 try expect(std.mem.eql(u8, &@as([6]u8, res2), "world!"));
93079306}
93089307 {#code_end#}
......@@ -9329,7 +9328,7 @@ test "vector @shuffle" {
93299328 {#header_close#}
93309329
93319330 {#header_open|@splat#}
9332 <pre>{#syntax#}@splat(comptime len: u32, scalar: anytype) std.meta.Vector(len, @TypeOf(scalar)){#endsyntax#}</pre>
9331 <pre>{#syntax#}@splat(comptime len: u32, scalar: anytype) @Vector(len, @TypeOf(scalar)){#endsyntax#}</pre>
93339332 <p>
93349333 Produces a vector of length {#syntax#}len{#endsyntax#} where each element is the value
93359334 {#syntax#}scalar{#endsyntax#}:
......@@ -9341,7 +9340,7 @@ const expect = std.testing.expect;
93419340test "vector @splat" {
93429341 const scalar: u32 = 5;
93439342 const result = @splat(4, scalar);
9344 comptime try expect(@TypeOf(result) == std.meta.Vector(4, u32));
9343 comptime try expect(@TypeOf(result) == @Vector(4, u32));
93459344 try expect(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 }));
93469345}
93479346 {#code_end#}
......@@ -9381,10 +9380,10 @@ const std = @import("std");
93819380const expect = std.testing.expect;
93829381
93839382test "vector @reduce" {
9384 const value: std.meta.Vector(4, i32) = [_]i32{ 1, -1, 1, -1 };
9383 const value = @Vector(4, i32){ 1, -1, 1, -1 };
93859384 const result = value > @splat(4, @as(i32, 0));
93869385 // result is { true, false, true, false };
9387 comptime try expect(@TypeOf(result) == std.meta.Vector(4, bool));
9386 comptime try expect(@TypeOf(result) == @Vector(4, bool));
93889387 const is_all_true = @reduce(.And, result);
93899388 comptime try expect(@TypeOf(is_all_true) == bool);
93909389 try expect(is_all_true == false);
......@@ -9743,6 +9742,12 @@ fn foo(comptime T: type, ptr: *T) T {
97439742 {#syntax#}@unionInit{#endsyntax#} forwards its {#link|result location|Result Location Semantics#} to {#syntax#}init_expr{#endsyntax#}.
97449743 </p>
97459744 {#header_close#}
9745
9746
9747 {#header_open|@Vector#}
9748 <pre>{#syntax#}@Vector(len: comptime_int, Element: type) type{#endsyntax#}</pre>
9749 <p>Creates {#link|Vectors#}.</p>
9750 {#header_close#}
97469751 {#header_close#}
97479752
97489753 {#header_open|Build Mode#}
lib/std/meta.zig+1
......@@ -930,6 +930,7 @@ test "std.meta.Float" {
930930 try testing.expectEqual(f128, Float(128));
931931}
932932
933/// Deprecated. Use `@Vector`.
933934pub fn Vector(comptime len: u32, comptime child: type) type {
934935 return @Type(.{
935936 .Vector = .{
lib/std/multi_array_list.zig+1-1
......@@ -421,7 +421,7 @@ pub fn MultiArrayList(comptime S: type) type {
421421 }
422422
423423 fn capacityInBytes(capacity: usize) usize {
424 const sizes_vector: std.meta.Vector(sizes.bytes.len, usize) = sizes.bytes;
424 const sizes_vector: @Vector(sizes.bytes.len, usize) = sizes.bytes;
425425 const capacity_vector = @splat(sizes.bytes.len, capacity);
426426 return @reduce(.Add, capacity_vector * sizes_vector);
427427 }