| ... | @@ -8209,6 +8209,49 @@ test "vector @splat" { | ... | @@ -8209,6 +8209,49 @@ test "vector @splat" { |
| 8209 | </p> | 8209 | </p> |
| 8210 | {#see_also|Vectors|@shuffle#} | 8210 | {#see_also|Vectors|@shuffle#} |
| 8211 | {#header_close#} | 8211 | {#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#} |
| | 8239 | const std = @import("std"); |
| | 8240 | const expect = std.testing.expect; |
| | 8241 | |
| | 8242 | test "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 | |
| 8212 | {#header_open|@src#} | 8255 | {#header_open|@src#} |
| 8213 | <pre>{#syntax#}@src() std.builtin.SourceLocation{#endsyntax#}</pre> | 8256 | <pre>{#syntax#}@src() std.builtin.SourceLocation{#endsyntax#}</pre> |
| 8214 | <p> | 8257 | <p> |