-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into solution-319
- Loading branch information
Showing
3 changed files
with
99 additions
and
1 deletion.
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
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,64 @@ | ||
<details open><summary>Info</summary><p> | ||
|
||
* **Did you know about intrisincts to support SIMD (Single Instruction, Multiple Data) instructions?** | ||
|
||
* https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html | ||
|
||
</p></details><details open><summary>Example</summary><p> | ||
|
||
```cpp | ||
#include <immintrin.h> | ||
|
||
int main() { | ||
const std::vector a = {1, 2, 3, 4}; | ||
const std::vector b = {5, 6, 7, 8}; | ||
|
||
const auto va = _mm_loadu_si128((__m128i*)a.data()); | ||
const auto vb = _mm_loadu_si128((__m128i*)b.data()); | ||
const auto result = _mm_add_epi32(va, vb); | ||
|
||
std::vector<int> v(a.size()); | ||
_mm_storeu_si128((__m128i*)v.data(), result); | ||
|
||
assert((std::vector{1 + 5, 2 + 6, 3 + 7, 4 + 8} == v)); | ||
} | ||
``` | ||
|
||
> https://godbolt.org/z/nz1hx76j7 | ||
</p></details><details open><summary>Puzzle</summary><p> | ||
|
||
> **Can you implement a function which computes the dot product of the two arrays using SIMD instructions?** | ||
```cpp | ||
[[nodiscard]] constexpr auto dot_product(const auto& lhs, const auto& rhs); // TODO | ||
|
||
int main() { | ||
using namespace boost::ut; | ||
|
||
"simd.dot_product empty"_test = [] { | ||
const std::vector<float> a{}; | ||
const std::vector<float> b{}; | ||
|
||
expect(0_i == dot_product(a, b)); | ||
}; | ||
|
||
"simd.dot_product one"_test = [] { | ||
const std::vector a = {1.f}; | ||
const std::vector b = {3.f}; | ||
|
||
expect(_i(1*3) == dot_product(a, b)); | ||
}; | ||
|
||
"simd.dot_product many"_test = [] { | ||
const std::vector a = {1.f, 2.f, 3.f, 4.f}; | ||
const std::vector b = {5.f, 6.f, 7.f, 8.f}; | ||
|
||
expect(_i(1*5+2*6+3*7+4*8) == dot_product(a, b)); | ||
}; | ||
} | ||
``` | ||
> https://godbolt.org/z/GnrEh1YM9 | ||
</p></details><details><summary>Solutions</summary><p> |