Given two arrays of 32-bit signed integers a and b, compute their element-wise sum into output array c such that c[i] = a[i] + b[i] for all i.
#include <immintrin.h>
#include <cstdint>
void add_arrays(const int32_t* a, const int32_t* b, int32_t* c, int n);
Parameters:
a — pointer to the first input array of n signed 32-bit integers, guaranteed 32-byte alignedb — pointer to the second input array of n signed 32-bit integers, guaranteed 32-byte alignedc — pointer to the output array of n signed 32-bit integers, guaranteed 32-byte alignedn — number of elements, guaranteed to be a multiple of 8 and at least 8Returns: nothing — results are written to c
a = [1, 2, 3, 4, 5, 6, 7, 8]
b = [10, 20, 30, 40, 50, 60, 70, 80]
c = [11, 22, 33, 44, 55, 66, 77, 88]
8 ≤ n ≤ 1,000,000n is always a multiple of 8[-10000, 10000]int32_ta, b, and c are all 32-byte alignedThis is the "Hello, World!" of SIMD. Your solution should use AVX2 intrinsics to process 8 integers at a time — load a chunk from each input, add them together, and store the result.
| Intrinsic | Description |
|---|---|
_mm256_load_si256(ptr) | Load 256 bits from aligned memory |
_mm256_add_epi32(a, b) | Add packed 32-bit integers element-wise |
_mm256_store_si256(ptr, v) | Store 256 bits to aligned memory |
Output will appear here after you run or submit.