Xmchx's Blog
Technology

使用 SIMD 来计算一个数组的和

single instruction multiple data

Code

Desc

Read over the Intel Intrinsics Guide to learn about the available SIMD instructions (an intrinsic function is a function whose implementation is handled by the compiler). The Intrinsics Naming and Usage documentation will be helpful in understanding the documentation.
this guide to help you understand how to use the SIMD functions.

SIMD Visualizer

Compiler Explorer

主要注意处理 tail case

Let's generate a randomized array.
Starting randomized sum.
Time taken: 7.902477 s
Sum: 103161741312

Starting randomized unrolled sum.
Time taken: 6.405713 s
Sum: 103161741312

Starting randomized SIMD sum.
Time taken: 1.645757 s
Sum: 103161741312

Starting randomized SIMD unrolled sum.
Time taken: 1.522628 s
Sum: 103161741312