aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2020-11-01 19:51:59 +0100
committerAndrew Kelley <andrew@ziglang.org>2020-11-01 14:30:31 -0700
commit577b99450764f5271b232eda3589eae94c9eb147 (patch)
tree73b8e443671f153b7b618480cb14ad6b7d597693 /doc
parent0d6a7088dc82cfe686beb5ebfe540ba2b7935cd6 (diff)
downloadzig-577b99450764f5271b232eda3589eae94c9eb147.tar.gz
zig-577b99450764f5271b232eda3589eae94c9eb147.zip
docs: Add @reduce documentation
Diffstat (limited to 'doc')
-rw-r--r--doc/langref.html.in43
1 files changed, 43 insertions, 0 deletions
diff --git a/doc/langref.html.in b/doc/langref.html.in
index 5e8ec10534..1a37ae5bcf 100644
--- a/doc/langref.html.in
+++ b/doc/langref.html.in
@@ -8209,6 +8209,49 @@ test "vector @splat" {
</p>
{#see_also|Vectors|@shuffle#}
{#header_close#}
+
+ {#header_open|@reduce#}
+ <pre>{#syntax#}@reduce(comptime op: builtin.ReduceOp, value: anytype) std.meta.Child(value){#endsyntax#}</pre>
+ <p>
+ Transforms a {#link|vector|Vectors#} into a scalar value by performing a
+ sequential horizontal reduction of its elements using the specified
+ specified operator {#syntax#}op{#endsyntax#}.
+ </p>
+ <p>
+ Not every operator is available for every vector element type:
+ <ul>
+ <li>{#syntax#}.And{#endsyntax#}, {#syntax#}.Or{#endsyntax#},
+ {#syntax#}.Xor{#endsyntax#} are available for
+ {#syntax#}bool{#endsyntax#} vectors,</li>
+ <li>{#syntax#}.Min{#endsyntax#}, {#syntax#}.Max{#endsyntax#},
+ {#syntax#}.Add{#endsyntax#}, {#syntax#}.Mul{#endsyntax#} are
+ available for {#link|floating point|Floats#} vectors,</li>
+ <li>Every operator is available for {#link|integer|Integers#} vectors.
+ </ul>
+ </p>
+ <p>
+ Note that {#syntax#}.Add{#endsyntax#} and {#syntax#}.Mul{#endsyntax#}
+ reductions on integral types are wrapping; when applied on floating point
+ types the operation associativity is preserved, unless the float mode is
+ set to {#syntax#}Optimized{#endsyntax#}.
+ </p>
+ {#code_begin|test#}
+const std = @import("std");
+const expect = std.testing.expect;
+
+test "vector @reduce" {
+ const value: std.meta.Vector(4, i32) = [_]i32{ 1, -1, 1, -1 };
+ const result = value > @splat(4, @as(i32, 0));
+ // result is { true, false, true, false };
+ comptime expect(@TypeOf(result) == std.meta.Vector(4, bool));
+ const is_all_true = @reduce(.And, result);
+ comptime expect(@TypeOf(is_all_true) == bool);
+ expect(is_all_true == false);
+}
+ {#code_end#}
+ {#see_also|Vectors|@setFloatMode#}
+ {#header_close#}
+
{#header_open|@src#}
<pre>{#syntax#}@src() std.builtin.SourceLocation{#endsyntax#}</pre>
<p>