From 577b99450764f5271b232eda3589eae94c9eb147 Mon Sep 17 00:00:00 2001
From: LemonBoy
{#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#}