Commit 577b994507

LemonBoy <thatlemon@gmail.com>
2020-11-01 19:51:59
docs: Add @reduce documentation
1 parent 0d6a708
Changed files (1)
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>