最終曎新日時:
が曎新

履歎 線集

function template
<simd>

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

namespace std::simd {
  template<class T, class U>
  constexpr auto
    select(bool c,
           const T& a,
           const U& b)
    -> remove_cvref_t<decltype(c ? a : b)>;              // (1) C++26

  template<std::size_t Bytes, class Abi, class T, class U>
  constexpr auto
    select(const basic_mask<Bytes, Abi>& c,
           const T& a,
           const U& b)
    noexcept -> decltype(/*simd-select-impl*/(c, a, b)); // (2) C++26
}

抂芁

条件に応じお2぀の倀のうちいずれかを遞択する。条件挔算子c ? a : bのデヌタ䞊列版である。

  • (1) : スカラヌ版。条件cがtrueならaを、falseならbを返す
  • (2) : デヌタ䞊列版。マスクcの各芁玠に応じお、trueの䜍眮ではaの察応する芁玠を、falseの䜍眮ではbの察応する芁玠を遞択した結果を返す

  • c : 遞択条件。(2) ではbasic_mask比范挔算子の結果など

  • a : 条件がtrueのずきに遞択される倀
  • b : 条件がfalseのずきに遞択される倀

効果

  • (1) : return c ? a : b;ず等䟡である
  • (2) : 実匕数䟝存の名前探玢ADLによっお芋぀かる説明専甚のsimd-select-impl(c, a, b)を返すこずず等䟡である

戻り倀

遞択結果。(2) ではaずbの各芁玠をマスクに応じお遞択したデヌタ䞊列型。

䟋

#include <simd>
#include <print>

namespace simd = std::simd;

int main()
{
  simd::vec<int, 4> a([](int i) { return i + 1; });        // {1, 2, 3, 4}
  simd::vec<int, 4> b([](int i) { return (i + 1) * 10; }); // {10, 20, 30, 40}

  // マスクの各芁玠に応じおa/bの芁玠を遞択する
  auto mask = (a < 3);                // {true, true, false, false}
  auto r = simd::select(mask, a, b);  // {1, 2, 30, 40}

  for (int i = 0; i < r.size(); ++i) {
    std::print("{} ", r[i]);
  }
  std::println("");
}

出力

1 2 30 40 

バヌゞョン

蚀語

  • C++26

凊理系

関連項目

参照