Skip to content

Commit

Permalink
369 - Did you know that C++17 added Hardware interference size?
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-jusiak committed Mar 11, 2024
1 parent d0a50ff commit a7c00a1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@

> C++17
* [Did you know that C++17 added Hardware interference size?](https://github.com/tip-of-the-week/cpp/blob/master/tips/369.md)
* [Did you know that the underlying visit implementation of std::visit has changed since GCC12+, Clang15+?](https://github.com/tip-of-the-week/cpp/blob/master/tips/353.md)
* [Did you know that run-time dispatching over type-list can be implemented many different ways?](https://github.com/tip-of-the-week/cpp/blob/master/tips/337.md)
* [Did you know that you can simplify `boost.mp11` API with DSL?](https://github.com/tip-of-the-week/cpp/blob/master/tips/335.md)
Expand Down
47 changes: 47 additions & 0 deletions tips/369.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<details open><summary>Info</summary><p>

* **Did you know that C++17 added Hardware interference size?**

* https://wg21.link/P0154

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

```cpp
#include <new>
static_assert(64u == std::hardware_destructive_interference_size); // x86-64
```
> https://godbolt.org/z/haPq9583q
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement `cache_aligned_array` function which returns cache size aligned std::array with provided values?
```cpp
constexpr auto cache_aligned_array(auto... args) {
// TODO
return std::array{args...};
};
static_assert(std::hardware_destructive_interference_size == alignof(cache_aligned_array(1, 2, 3)));
```

> https://godbolt.org/z/EvKco4fG7
</p></details>

</p></details><details><summary>Solutions</summary><p>

```cpp
constexpr auto cache_aligned_array(auto... args) {
struct alignas(std::hardware_destructive_interference_size)
: decltype(std::array{args...}) { } array {args...};
return array;
};

static_assert(std::hardware_destructive_interference_size == alignof(cache_aligned_array(1, 2, 3)));
```
> https://godbolt.org/z/oTezYjsW4
</p></details>

0 comments on commit a7c00a1

Please sign in to comment.