diff --git a/README.md b/README.md index 2fdc68a..fdfa1bc 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ * [Did you know that C++26 added `= delete("should have a reason")`?](https://github.com/tip-of-the-week/cpp/blob/master/tips/371.md) * [Did you know that C++26 added `span.at`?](https://github.com/tip-of-the-week/cpp/blob/master/tips/370.md) * [Did you know about C++26 simd proposal (1/N)?](https://github.com/tip-of-the-week/cpp/blob/master/tips/367.md) +* [Did you know about C++26 simd proposal (2/N)?](https://github.com/tip-of-the-week/cpp/blob/master/tips/374.md) * [Did you know about C++26 static reflection proposal (1/N)?](https://github.com/tip-of-the-week/cpp/blob/master/tips/361.md) * [Did you know about C++26 static reflection proposal (2/N)?](https://github.com/tip-of-the-week/cpp/blob/master/tips/362.md) * [Did you know about C++26 static reflection proposal (3/N)?](https://github.com/tip-of-the-week/cpp/blob/master/tips/363.md) diff --git a/tips/374.md b/tips/374.md new file mode 100644 index 0000000..093a059 --- /dev/null +++ b/tips/374.md @@ -0,0 +1,58 @@ +
Info

+ +* **Did you know about C++26 simd proposal (2/N)?** + + * https://wg21.link/P1928 + +

Example

+ +```cpp +int main() { + std::array storage{1, 2, 3, 4, 5, 6, 7, 8}; + std::experimental::fixed_size_simd data{storage.begin(), std::experimental::element_aligned}; + std::experimental::fixed_size_simd_mask 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 }?** + +```cpp +template +auto find(const std::array& 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

+ +```cpp +template +auto find(const std::array& data, const T value) { + const std::experimental::fixed_size_simd lhs{data.begin(), std::experimental::element_aligned}; + return std::experimental::any_of(lhs == value); +} +``` + +> https://godbolt.org/z/KGPhvM3bY + +