Skip to content

Commit

Permalink
374 - Did you know about C++26 simd proposal (2/N)?
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-jusiak committed Jun 27, 2024
1 parent 80f4f0a commit 23a8940
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 58 additions & 0 deletions tips/374.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<details open><summary>Info</summary><p>

* **Did you know about C++26 simd proposal (2/N)?**

* https://wg21.link/P1928

</p></details><details open><summary>Example</summary><p>

```cpp
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
</p></details><details open><summary>Puzzle</summary><p>

* **Can you implement function find which returns { true: if given element is found; false: otherwise }?**

```cpp
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
</p></details>
</p></details><details><summary>Solutions</summary><p>
```cpp
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

</p></details>

0 comments on commit 23a8940

Please sign in to comment.