{"id":10888,"date":"2025-07-28T10:10:02","date_gmt":"2025-07-28T10:10:02","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10888"},"modified":"2025-07-28T10:10:02","modified_gmt":"2025-07-28T10:10:02","slug":"data-parallel-types-reduction","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/data-parallel-types-reduction\/","title":{"rendered":"Data-Parallel Types: Reduction"},"content":{"rendered":"\n<p>In this article, I will discuss reduction and mask reduction for data-parallel types.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"755\" height=\"509\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/06\/Time26ConcurrencySimd-1.png\" alt=\"\" class=\"wp-image-10788\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/06\/Time26ConcurrencySimd-1.png 755w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/06\/Time26ConcurrencySimd-1-300x202.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/06\/Time26ConcurrencySimd-1-705x475.png 705w\" sizes=\"auto, (max-width: 755px) 100vw, 755px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Reduction<\/h2>\n\n\n\n<p>A reduction reduces the SIMD vector to a single element. The library provides three functions for this purpose: <code>reduce<\/code>, <code>hmin<\/code>, and <code>hmax<\/code>.<br>The following program shows how these functions are used.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ reduction.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;array&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;experimental\/simd&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;functional&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string_view&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold\">namespace<\/span> stdx <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>experimental;\n \n<span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">println<\/span>(std<span style=\"color: #555555\">::<\/span>string_view name, <span style=\"color: #006699; font-weight: bold\">auto<\/span> <span style=\"color: #006699; font-weight: bold\">const<\/span><span style=\"color: #555555\">&amp;<\/span> a) {\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> name <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;: &quot;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (std<span style=\"color: #555555\">::<\/span><span style=\"color: #007788; font-weight: bold\">size_t<\/span> i{}; i <span style=\"color: #555555\">!=<\/span> std<span style=\"color: #555555\">::<\/span>size(a); <span style=\"color: #555555\">++<\/span>i)\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> a[i] <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39; &#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n}\n\n \n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    <span style=\"color: #006699; font-weight: bold\">const<\/span> stdx<span style=\"color: #555555\">::<\/span>fixed_size_simd<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span>, <span style=\"color: #FF6600\">16<\/span><span style=\"color: #555555\">&gt;<\/span> a([](<span style=\"color: #007788; font-weight: bold\">int<\/span> i) { <span style=\"color: #006699; font-weight: bold\">return<\/span> i; });    \n    println(<span style=\"color: #CC3300\">&quot;a&quot;<\/span>, a);\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> sum <span style=\"color: #555555\">=<\/span> stdx<span style=\"color: #555555\">::<\/span>reduce(a);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;sum: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> sum <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">const<\/span> stdx<span style=\"color: #555555\">::<\/span>fixed_size_simd<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span>, <span style=\"color: #FF6600\">8<\/span><span style=\"color: #555555\">&gt;<\/span> b([](<span style=\"color: #007788; font-weight: bold\">int<\/span> i) { <span style=\"color: #006699; font-weight: bold\">return<\/span> i <span style=\"color: #555555\">+<\/span> <span style=\"color: #FF6600\">1<\/span>; });    \n    println(<span style=\"color: #CC3300\">&quot;b&quot;<\/span>, b);\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> product <span style=\"color: #555555\">=<\/span> stdx<span style=\"color: #555555\">::<\/span>reduce(b, std<span style=\"color: #555555\">::<\/span>multiplies<span style=\"color: #555555\">&lt;&gt;<\/span>());\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;product: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> product <span style=\"color: #555555\">&lt;&lt;<\/span>  <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> maximum <span style=\"color: #555555\">=<\/span> stdx<span style=\"color: #555555\">::<\/span>hmax(b);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;maximum: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> maximum <span style=\"color: #555555\">&lt;&lt;<\/span>  <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> minimum <span style=\"color: #555555\">=<\/span> stdx<span style=\"color: #555555\">::<\/span>hmin(b);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;minimum: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> minimum <span style=\"color: #555555\">&lt;&lt;<\/span>  <span style=\"color: #CC3300\">&quot;<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n    \n}\n<\/pre><\/div>\n\n\n\n<p>First, the <code>reduce<\/code> function is used. By default, the + operator is applied in the usual way. However, this function can also be parameterized with any binary operator. The expression<code> stdx::reduce(b, std::multiplies&lt;&gt;()) <\/code>applies the function object <code>std::multiplies <\/code>from the header functional. The functions <code>hmax<\/code> and <code>hmin<\/code> determine the maximum and minimum of the SIMD vector <code>b<\/code>.<br>Finally, the program output follows:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"360\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/reduction.png\" alt=\"\" class=\"wp-image-10893\" style=\"width:400px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/reduction.png 680w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/reduction-300x159.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Mask Reduction <\/h2>\n\n\n\n<p>When reducing a mask, the SIMD mask is reduced to either <code>true <\/code>or <code>false <\/code>value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>all_of, any_of, none_of, <\/code>and <code>some_of<\/code><\/h3>\n\n\n\n<p><\/p>\n\n\n\n<p>Here we encounter some old friends from C++: <code>all_of, any_of, none_of, <\/code>and <code>some_of<\/code>. When using these functions, the SIMD mask is reduced to either  <code>true <\/code>or <code>false <\/code>value.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>all_of<\/code>: Returns <code>true<\/code> if all values in the SIMD mask are <code>true<\/code>.<\/li>\n\n\n\n<li><code>any_of<\/code>: Returns <code>true<\/code> if at least one value in the SIMD mask is <code>true<\/code>.<\/li>\n\n\n\n<li><code>none_of<\/code>: Returns <code>true <\/code>if all values in the SIMD mask are <code>false<\/code>.<\/li>\n\n\n\n<li><code>some_of<\/code>: Returns <code>true<\/code> if at least one value in the SIMD mask is <code>true<\/code>, but not all values in it are <code>true<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/cppreference.com\/w\/cpp\/experimental\/simd\/all_of.html\">cppreference.com<\/a> has a nice example of these functions:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ reductionWithMask.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;cassert&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;experimental\/simd&gt;<\/span>\n \n<span style=\"color: #006699; font-weight: bold\">namespace<\/span> stq <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>experimental;\n \n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>()\n{\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> mask <span style=\"color: #555555\">=<\/span> stq<span style=\"color: #555555\">::<\/span>fixed_size_simd_mask<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span>, <span style=\"color: #FF6600\">4<\/span><span style=\"color: #555555\">&gt;<\/span>;\n \n    mask mask1{<span style=\"color: #336666\">false<\/span>}; <span style=\"color: #0099FF; font-style: italic\">\/\/ = {0, 0, 0, 0}<\/span>\n    assert\n    (\n        stq<span style=\"color: #555555\">::<\/span>none_of(mask1) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">true<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>any_of(mask1) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">false<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>some_of(mask1) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">false<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>all_of(mask1) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">false<\/span>\n    );\n \n    mask mask2{<span style=\"color: #336666\">true<\/span>}; <span style=\"color: #0099FF; font-style: italic\">\/\/ = {1, 1, 1, 1}<\/span>\n    assert\n    (\n        stq<span style=\"color: #555555\">::<\/span>none_of(mask2) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">false<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>any_of(mask2) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">true<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>some_of(mask2) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">false<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>all_of(mask2) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">true<\/span>\n    );\n \n    mask mask3{<span style=\"color: #336666\">true<\/span>};\n    mask3[<span style=\"color: #FF6600\">0<\/span>] <span style=\"color: #555555\">=<\/span> mask3[<span style=\"color: #FF6600\">1<\/span>] <span style=\"color: #555555\">=<\/span> <span style=\"color: #336666\">false<\/span>; <span style=\"color: #0099FF; font-style: italic\">\/\/ mask3 = {0, 0, 1, 1}<\/span>\n    assert\n    (\n        stq<span style=\"color: #555555\">::<\/span>none_of(mask3) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">false<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>any_of(mask3) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">true<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>some_of(mask3) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">true<\/span> <span style=\"color: #555555\">&amp;&amp;<\/span>\n        stq<span style=\"color: #555555\">::<\/span>all_of(mask3) <span style=\"color: #555555\">==<\/span> <span style=\"color: #336666\">false<\/span>\n    );\n}\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><code>popcount<\/code><\/h3>\n\n\n\n<p><code>popcount<\/code> determines how many values in a SIMD mask are true. A program can be written quickly to do this:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ popcount.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;experimental\/simd&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string_view&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold\">namespace<\/span> stdx <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>experimental;\n \n<span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">println<\/span>(std<span style=\"color: #555555\">::<\/span>string_view name, <span style=\"color: #006699; font-weight: bold\">auto<\/span> <span style=\"color: #006699; font-weight: bold\">const<\/span><span style=\"color: #555555\">&amp;<\/span> a) {\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>boolalpha <span style=\"color: #555555\">&lt;&lt;<\/span> name <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;: &quot;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (std<span style=\"color: #555555\">::<\/span><span style=\"color: #007788; font-weight: bold\">size_t<\/span> i{}; i <span style=\"color: #555555\">!=<\/span> std<span style=\"color: #555555\">::<\/span>size(a); <span style=\"color: #555555\">++<\/span>i)\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> a[i] <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39; &#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n}\n\n \n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    <span style=\"color: #006699; font-weight: bold\">const<\/span> stdx<span style=\"color: #555555\">::<\/span>native_simd<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> a <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">1<\/span>;\n    println(<span style=\"color: #CC3300\">&quot;a&quot;<\/span>, a);\n \n    <span style=\"color: #006699; font-weight: bold\">const<\/span> stdx<span style=\"color: #555555\">::<\/span>native_simd<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> b([](<span style=\"color: #007788; font-weight: bold\">int<\/span> i) { <span style=\"color: #006699; font-weight: bold\">return<\/span> i <span style=\"color: #555555\">-<\/span> <span style=\"color: #FF6600\">2<\/span>; });\n    println(<span style=\"color: #CC3300\">&quot;b&quot;<\/span>, b);\n \n    <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #006699; font-weight: bold\">auto<\/span> c <span style=\"color: #555555\">=<\/span> a <span style=\"color: #555555\">+<\/span> b;\n    println(<span style=\"color: #CC3300\">&quot;c&quot;<\/span>, c);\n \n    <span style=\"color: #006699; font-weight: bold\">const<\/span> stdx<span style=\"color: #555555\">::<\/span>native_simd_mask x <span style=\"color: #555555\">=<\/span> c <span style=\"color: #555555\">&lt;<\/span> <span style=\"color: #FF6600\">0<\/span>; \n    println(<span style=\"color: #CC3300\">&quot;x&quot;<\/span>, x);\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> cnt <span style=\"color: #555555\">=<\/span> popcount(x);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;cnt: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> cnt <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p>Thanks to the program output, this should be self-explanatory.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"442\" height=\"209\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/popcount.png\" alt=\"\" class=\"wp-image-10897\" style=\"width:250px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/popcount.png 442w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/popcount-300x142.png 300w\" sizes=\"auto, (max-width: 442px) 100vw, 442px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><code>find_first_set<\/code> and <code>find_last_set<\/code><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>find_first_set<\/code>: Returns the lowest index i where the SIMD mask is <code>true<\/code>.<\/li>\n\n\n\n<li><code>find_last_set<\/code>: Returns the greatest index i where the SIMD mask is <code>true<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>The following program demonstrates the two functions in use:<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ find_first_set.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;experimental\/simd&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string_view&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold\">namespace<\/span> stdx <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>experimental;\n \n<span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">println<\/span>(std<span style=\"color: #555555\">::<\/span>string_view name, <span style=\"color: #006699; font-weight: bold\">auto<\/span> <span style=\"color: #006699; font-weight: bold\">const<\/span><span style=\"color: #555555\">&amp;<\/span> a) {\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>boolalpha <span style=\"color: #555555\">&lt;&lt;<\/span> name <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;: &quot;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (std<span style=\"color: #555555\">::<\/span><span style=\"color: #007788; font-weight: bold\">size_t<\/span> i{}; i <span style=\"color: #555555\">!=<\/span> std<span style=\"color: #555555\">::<\/span>size(a); <span style=\"color: #555555\">++<\/span>i)\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> a[i] <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39; &#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n}\n\n \n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    stdx<span style=\"color: #555555\">::<\/span>simd_mask<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">short<\/span><span style=\"color: #555555\">&gt;<\/span> x{<span style=\"color: #FF6600\">0<\/span>};\n    println(<span style=\"color: #CC3300\">&quot;x&quot;<\/span>, x);\n\n    x[<span style=\"color: #FF6600\">1<\/span>] <span style=\"color: #555555\">=<\/span> <span style=\"color: #336666\">true<\/span>;\n    x[x.size() <span style=\"color: #555555\">-<\/span> <span style=\"color: #FF6600\">1<\/span>] <span style=\"color: #555555\">=<\/span> <span style=\"color: #336666\">true<\/span>;\n    println(<span style=\"color: #CC3300\">&quot;x&quot;<\/span>, x);\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> first <span style=\"color: #555555\">=<\/span> stdx<span style=\"color: #555555\">::<\/span>find_first_set(x);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;find_first_set(x): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> first <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> last <span style=\"color: #555555\">=<\/span> stdx<span style=\"color: #555555\">::<\/span>find_last_set(x);\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;find_last_set(x): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> last<span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    \n}\n<\/pre><\/div>\n\n\n\n\n<p>Finally, here is the output of the program:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"180\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/find_first_set.png\" alt=\"\" class=\"wp-image-10903\" style=\"width:500px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/find_first_set.png 860w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/find_first_set-300x63.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/find_first_set-768x161.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/07\/find_first_set-705x148.png 705w\" sizes=\"auto, (max-width: 860px) 100vw, 860px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s next?<\/h2>\n\n\n\n<p>In my next article, I will focus on the algorithms of data-parallel types.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I will discuss reduction and mask reduction for data-parallel types. Reduction A reduction reduces the SIMD vector to a single element. The library provides three functions for this purpose: reduce, hmin, and hmax.The following program shows how these functions are used. \/\/ reduction.cpp #include &lt;array&gt; #include &lt;experimental\/simd&gt; #include &lt;functional&gt; #include &lt;iostream&gt; #include [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":10788,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[559],"tags":[566],"class_list":["post-10888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c26-blog","tag-data-parallel-types"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=10888"}],"version-history":[{"count":11,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10888\/revisions"}],"predecessor-version":[{"id":10906,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10888\/revisions\/10906"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/10788"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=10888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}