Visual Studio 2010

Visual Studio 2010 SP1 is required.

Microsoft Specific

Generates the XOP instruction vphadduwq to perform an unsigned integer horizontal add of its source.

__m128i _mm_haddq_epu16 (
   __m128i src

[in] src

A 128-bit parameter that contains eight 16-bit unsigned integers.

A 128-bit result r that contains two 64-bit unsigned integers.

r[i] := src[4*i] + src[4*i+1] + src[4*i+2] + src[4*i+3];





Header file <intrin.h>

Each of the two groups of four 16-bit unsigned integer values in src is added to produce a 64-bit unsigned integer sum that is stored as the corresponding value in the destination. No overflow is possible in this instruction.

The vphadduwq instruction is part of the XOP family of instructions. Before you use this intrinsic, you must ensure that the processor supports this instruction. To determine hardware support for this instruction, call the __cpuid intrinsic with InfoType = 0x80000001 and check bit 11 of CPUInfo[2] (ECX). This bit is 1 when the instruction is supported, and 0 otherwise.

#include <stdio.h>
#include <intrin.h>
int main()
    __m128i a, d;
    int i;
    for (i = 0; i < 4; i++) {
        a.m128i_i16[i] = i-128;
        a.m128i_i16[i+4] = 127-i;
    // Note that this intrinsic treats negative values as unsigned
    // So, e.g., -1 becomes +65535
    d = _mm_haddq_epu16(a);
    for (i = 0; i < 2; i++) printf_s(" %I64u", d.m128i_u64[i]);
261638 502

Community Additions