Given an array of 32-bit signed integers, find and return the minimum value using AVX2 intrinsics.
#include <immintrin.h>
#include <cstdint>
int32_t array_min(const int32_t* arr, int n);
Parameters:
arr — pointer to an 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: the minimum element in the array
Input: [3, 1, 4, 1, 5, 9, 2, 6]
Output: 1
8 ≤ n ≤ 1,000,000n is always a multiple of 8[-1,000,000, 1,000,000]arr is 32-byte alignedYour solution should use AVX2 intrinsics to compare 8 integers at a time. Unlike a sum reduction, you cannot use _mm_hadd for the final horizontal step — you need a shuffle-and-min pattern instead.
| Intrinsic | Description |
|---|---|
_mm256_load_si256(ptr) | Load 256 bits from aligned memory |
_mm256_min_epi32(a, b) | Packed 32-bit integer minimum |
_mm256_extracti128_si256(v, 1) | Extract high 128-bit lane |
_mm256_castsi256_si128(v) | Cast to low 128-bit lane (free) |
_mm_min_epi32(a, b) | Packed 32-bit integer minimum (128-bit) |
_mm_shuffle_epi32(v, imm) | Shuffle 32-bit elements within 128-bit register |
_mm_extract_epi32(v, idx) | Extract a 32-bit integer |
Output will appear here after you run or submit.