From 577b99450764f5271b232eda3589eae94c9eb147 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 1 Nov 2020 19:51:59 +0100 Subject: [PATCH] docs: Add @reduce documentation --- doc/langref.html.in | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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" {

{#see_also|Vectors|@shuffle#} {#header_close#} + + {#header_open|@reduce#} +
{#syntax#}@reduce(comptime op: builtin.ReduceOp, value: anytype) std.meta.Child(value){#endsyntax#}
+

+ 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#}. +

+

+ Not every operator is available for every vector element type: +

+

+

+ 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#}. +

+ {#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#}
{#syntax#}@src() std.builtin.SourceLocation{#endsyntax#}