374.md

June 27, 2024 ยท View on GitHub

Info

Example

int main() {
  std::array<char, 8> storage{1, 2, 3, 4, 5, 6, 7, 8};
  std::experimental::fixed_size_simd<char, 8> data{storage.begin(), std::experimental::element_aligned};
  std::experimental::fixed_size_simd_mask<char, 8> values = (data == 7);
  std::cout << std::experimental::any_of(values) << std::endl;
}

https://godbolt.org/z/3389qGWh6

Puzzle

  • Can you implement function find which returns { true: if given element is found; false: otherwise }?
template<class T, auto Size>
auto find(const std::array<T, Size>& data, const T value); // TODO

int main() {
  using namespace ut;

  "simd.find"_test = []() mutable {
    expect(true_b == find(std::array{1, 2, 3, 4}, 1));
    expect(true_b == find(std::array{1, 2, 3, 4}, 2));
    expect(true_b == find(std::array{1, 2, 3, 4}, 3));
    expect(true_b == find(std::array{1, 2, 3, 4}, 4));
    expect(false_b == find(std::array{1, 2, 3, 4}, 0));
    expect(false_b == find(std::array{1, 2, 3, 4}, 6));
  };
}

https://godbolt.org/z/9hqq1nq6z

Solutions

template<class T, auto Size>
auto find(const std::array<T, Size>& data, const T value) {
  const std::experimental::fixed_size_simd<T, Size> lhs{data.begin(), std::experimental::element_aligned};
  return std::experimental::any_of(lhs == value);
}

https://godbolt.org/z/KGPhvM3bY