最終更新日時:
が更新

履歴 編集

function template
<simd>

std::simd::reduce(C++26)

namespace std::simd {
  template<class T, class Abi, class BinaryOperation = std::plus<>>
  constexpr T
    reduce(const basic_vec<T, Abi>& x,
           BinaryOperation binary_op = {});                           // (1) C++26

  template<class T, class Abi, class BinaryOperation = std::plus<>>
  constexpr T
    reduce(const basic_vec<T, Abi>& x,
           const typename basic_vec<T, Abi>::mask_type& mask,
           BinaryOperation binary_op = {},
           std::type_identity_t<T> identity_element = /*see below*/); // (2) C++26

  template<class T, class BinaryOperation = std::plus<>>
  constexpr T
    reduce(const T& x,
           BinaryOperation binary_op = {}); // (3) C++26

  template<class T, class BinaryOperation = std::plus<>>
  constexpr T
    reduce(const T& x,
           std::same_as<bool> auto mask,
           BinaryOperation binary_op = {},
           std::type_identity_t<T> identity_element = /*see below*/); // (4) C++26
}

概要

basic_vecの全要素を二項演算binary_opで集計し、単一の値を求める。

  • (1) : xの全要素をbinary_opで集計する。
  • (2) : maskで選択された要素のみをbinary_opで集計する。ひとつも選択されていない場合はidentity_element(単位元)を返す。
  • (3) : スカラー値xをそのまま返す(SIMD-genericなコードを書けるようにするためのオーバーロード)。
  • (4) : masktrueならxを、falseならidentity_elementを返す。

binary_opは結合的かつ可換な二項演算でなければならず、要素の集計順は規定されない。

テンプレートパラメータ制約

事前条件

  • (1), (2) : binary_opxを書き換えないこと
  • (2), (4) : Tで表現可能な任意の有限値yについて、identity_elementbinary_opの単位元として作用させた結果がyと等しくなること

戻り値

  • (1) : xの全要素x[0], …, x[x.size() - 1]binary_opで集計した結果を返す。
  • (2) : none_of(mask)trueならidentity_elementを返す。そうでなければmaskで選択された要素をbinary_opで集計した結果を返す。
  • (3) : xを返す。
  • (4) : maskfalseならidentity_elementを、そうでなければxを返す。

例外

  • (1), (2) : binary_opが送出する例外を送出する
  • (3), (4) : 投げない

備考

#include <simd>
#include <print>
#include <functional>

namespace simd = std::simd;

int main()
{
  simd::vec<int, 4> v([](int i) { return i + 1; }); // {1, 2, 3, 4}

  // 全要素の総和(既定の二項演算はstd::plus<>)
  std::println("{}", simd::reduce(v));

  // 全要素の積
  std::println("{}", simd::reduce(v, std::multiplies<>{}));

  // 偶数要素だけの総和
  auto mask = (v % 2 == 0);
  std::println("{}", simd::reduce(v, mask));
}

出力

10
24
6

バージョン

言語

  • C++26

処理系

関連項目

参照