diff options
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/langref.html.in | 43 |
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> |
