通常你会在整个循环中保持一个向量计数,然后在循环终止时将向量的元素相加,例如
#include <emmintrin.h>
uint32_t sse(const uint32_t *dst, const size_t N)
{
const __m128i condition = _mm_set1_epi32(2);
__m128i vcount = _mm_set1_epi32(0);
uint32_t count = 0;
for (size_t i = 0; i < N; i += 4)
{
__m128i v = _mm_load_si128((__m128i *)&dst[i]);
__m128i vcmp = _mm_cmpgt_epi32(v, condition);
v = _mm_and_si128(v, vcmp);
_mm_store_si128((__m128i *)&dst[i], v);
vcount = _mm_add_epi32(vcount, vcmp); // accumulate (negative) counts
}
// ... sum vcount here and store in count (see below) ...
return count;
}
请注意,我们将每个掩码元素视为一个 int,即 0 或 -1,因此我们正在累积一个总和,该总和是实际总和的负数。
最终求和的效率vcount
通常不太重要,因为它在整个循环中只执行一次,因此只要提供N
相当大的值,无论需要多少指令(在合理范围内)都无关紧要。
有几种处理最终总和的方法,例如,您可以使用_mm_movemask_epi8
(SSE2) 提取 16 位掩码并使用它,或者您可以使用_mm_hadd_epi32
(SSSE3) 计算向量上的水平总和,然后将总和提取为标量,例如
上交所2:
#include <emmintrin.h>
int16_t mask = _mm_movemask_epi8(vcount); // extract 16 bit mask
count = !!(mask & 0x0001) + // count non-zero 32 bit elements
!!(mask & 0x0010) +
!!(mask & 0x0100) +
!!(mask & 0x1000);
SSSE3:
#include <tmmintrin.h>
vcount = _mm_hadd_epi32(vcount, vcount); // horizontal sum of 4 elements
vcount = _mm_hadd_epi32(vcount, vcount);
count = - ((_mm_extract_epi16(vcount, 1) << 16) // extract (and negate) sum to
| _mm_extract_epi16(vcount, 1)); // get total (positive) count
SSE4.2:
#include <smmintrin.h>
vcount = _mm_hadd_epi32(vcount, vcount); // horizontal sum of 4 elements
vcount = _mm_hadd_epi32(vcount, vcount);
count = - _mm_extract_epi32(vcount, 0); // extract (and negate) sum to
// get total (positive) count
这是一个完整的工作版本,带有 SSE4.2 版本的测试工具:
#include <stdio.h>
#include <stdint.h>
#include <smmintrin.h>
uint32_t sse(const uint32_t *dst, const size_t N)
{
const __m128i condition = _mm_set1_epi32(2);
__m128i vcount = _mm_set1_epi32(0);
uint32_t count = 0;
for (size_t i = 0; i < N; i += 4)
{
__m128i v = _mm_load_si128((__m128i *)&dst[i]);
__m128i vcmp = _mm_cmpgt_epi32(v, condition);
v = _mm_and_si128(v, vcmp);
_mm_store_si128((__m128i *)&dst[i], v);
vcount = _mm_add_epi32(vcount, vcmp); // accumulate (negative) counts
}
vcount = _mm_hadd_epi32(vcount, vcount); // horizontal sum of 4 elements
vcount = _mm_hadd_epi32(vcount, vcount);
count = - _mm_extract_epi32(vcount, 0); // extract (and negate) sum to
// get total (positive) count
return count;
}
int main(void)
{
uint32_t a[4] __attribute__ ((aligned(16))) = { 1, 2, 3, 4 };
uint32_t count;
count = sse(a, 4);
printf("a = %u %u %u %u \n", a[0], a[1], a[2], a[3]);
printf("count = %u\n", count);
return 0;
}
$ gcc -Wall -std=c99 -msse4 sse_count.c -o sse_count
$ ./sse_count
a = 0 0 3 4
count = 2