-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
369 - Did you know that C++17 added Hardware interference size?
- Loading branch information
1 parent
d0a50ff
commit a7c00a1
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |