authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-11-01 19:51:59+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-11-01 14:30:31-07:00
log577b99450764f5271b232eda3589eae94c9eb147
tree73b8e443671f153b7b618480cb14ad6b7d597693
parent0d6a7088dc82cfe686beb5ebfe540ba2b7935cd6

docs: Add @reduce documentation


1 files changed, 43 insertions(+), 0 deletions(-)

doc/langref.html.in+43
......@@ -8209,6 +8209,49 @@ test "vector @splat" {
82098209 </p>
82108210 {#see_also|Vectors|@shuffle#}
82118211 {#header_close#}
8212
8213 {#header_open|@reduce#}
8214 <pre>{#syntax#}@reduce(comptime op: builtin.ReduceOp, value: anytype) std.meta.Child(value){#endsyntax#}</pre>
8215 <p>
8216 Transforms a {#link|vector|Vectors#} into a scalar value by performing a
8217 sequential horizontal reduction of its elements using the specified
8218 specified operator {#syntax#}op{#endsyntax#}.
8219 </p>
8220 <p>
8221 Not every operator is available for every vector element type:
8222 <ul>
8223 <li>{#syntax#}.And{#endsyntax#}, {#syntax#}.Or{#endsyntax#},
8224 {#syntax#}.Xor{#endsyntax#} are available for
8225 {#syntax#}bool{#endsyntax#} vectors,</li>
8226 <li>{#syntax#}.Min{#endsyntax#}, {#syntax#}.Max{#endsyntax#},
8227 {#syntax#}.Add{#endsyntax#}, {#syntax#}.Mul{#endsyntax#} are
8228 available for {#link|floating point|Floats#} vectors,</li>
8229 <li>Every operator is available for {#link|integer|Integers#} vectors.
8230 </ul>
8231 </p>
8232 <p>
8233 Note that {#syntax#}.Add{#endsyntax#} and {#syntax#}.Mul{#endsyntax#}
8234 reductions on integral types are wrapping; when applied on floating point
8235 types the operation associativity is preserved, unless the float mode is
8236 set to {#syntax#}Optimized{#endsyntax#}.
8237 </p>
8238 {#code_begin|test#}
8239const std = @import("std");
8240const expect = std.testing.expect;
8241
8242test "vector @reduce" {
8243 const value: std.meta.Vector(4, i32) = [_]i32{ 1, -1, 1, -1 };
8244 const result = value > @splat(4, @as(i32, 0));
8245 // result is { true, false, true, false };
8246 comptime expect(@TypeOf(result) == std.meta.Vector(4, bool));
8247 const is_all_true = @reduce(.And, result);
8248 comptime expect(@TypeOf(is_all_true) == bool);
8249 expect(is_all_true == false);
8250}
8251 {#code_end#}
8252 {#see_also|Vectors|@setFloatMode#}
8253 {#header_close#}
8254
82128255 {#header_open|@src#}
82138256 <pre>{#syntax#}@src() std.builtin.SourceLocation{#endsyntax#}</pre>
82148257 <p>