Skip to content

haddsΒΆ

Computes the horizontal addition with saturation of all elements in an integer vector register. For the 8-, 16- and 32-bit types, the sum is exact and saturated once at the end: 100 + 100 - 100 - 100 gives 0 in int8. For the 64-bit types, each addition saturates, in the order of the lanes: MAX + 1 - 1 gives MAX - 1.

Mathematical semantics: \(\text{res} = \operatorname{saturate}\left(\sum_{i=0}^{N-1} vd0_i\right)\)

Open in the API Explorer β†’

PrototypesΒΆ

// LMUL = 1 (also without the _m1 suffix)
int64_t mipp_hadds_int64_m1(const vd_int64_m1_t vd0);
int32_t mipp_hadds_int32_m1(const vd_int32_m1_t vd0);
int16_t mipp_hadds_int16_m1(const vd_int16_m1_t vd0);
int8_t mipp_hadds_int8_m1(const vd_int8_m1_t vd0);
uint64_t mipp_hadds_uint64_m1(const vd_uint64_m1_t vd0);
uint32_t mipp_hadds_uint32_m1(const vd_uint32_m1_t vd0);
uint16_t mipp_hadds_uint16_m1(const vd_uint16_m1_t vd0);
uint8_t mipp_hadds_uint8_m1(const vd_uint8_m1_t vd0);
// LMUL = 2
int64_t mipp_hadds_int64_m2(const vd_int64_m2_t vd0);
int32_t mipp_hadds_int32_m2(const vd_int32_m2_t vd0);
int16_t mipp_hadds_int16_m2(const vd_int16_m2_t vd0);
int8_t mipp_hadds_int8_m2(const vd_int8_m2_t vd0);
uint64_t mipp_hadds_uint64_m2(const vd_uint64_m2_t vd0);
uint32_t mipp_hadds_uint32_m2(const vd_uint32_m2_t vd0);
uint16_t mipp_hadds_uint16_m2(const vd_uint16_m2_t vd0);
uint8_t mipp_hadds_uint8_m2(const vd_uint8_m2_t vd0);
// LMUL = 4
int64_t mipp_hadds_int64_m4(const vd_int64_m4_t vd0);
int32_t mipp_hadds_int32_m4(const vd_int32_m4_t vd0);
int16_t mipp_hadds_int16_m4(const vd_int16_m4_t vd0);
int8_t mipp_hadds_int8_m4(const vd_int8_m4_t vd0);
uint64_t mipp_hadds_uint64_m4(const vd_uint64_m4_t vd0);
uint32_t mipp_hadds_uint32_m4(const vd_uint32_m4_t vd0);
uint16_t mipp_hadds_uint16_m4(const vd_uint16_m4_t vd0);
uint8_t mipp_hadds_uint8_m4(const vd_uint8_m4_t vd0);
// LMUL = 8
int64_t mipp_hadds_int64_m8(const vd_int64_m8_t vd0);
int32_t mipp_hadds_int32_m8(const vd_int32_m8_t vd0);
int16_t mipp_hadds_int16_m8(const vd_int16_m8_t vd0);
int8_t mipp_hadds_int8_m8(const vd_int8_m8_t vd0);
uint64_t mipp_hadds_uint64_m8(const vd_uint64_m8_t vd0);
uint32_t mipp_hadds_uint32_m8(const vd_uint32_m8_t vd0);
uint16_t mipp_hadds_uint16_m8(const vd_uint16_m8_t vd0);
uint8_t mipp_hadds_uint8_m8(const vd_uint8_m8_t vd0);
// d2 (half-vector)
int64_t mipp_hadds_int64_d2(const vd_int64_d2_t vd0);
int32_t mipp_hadds_int32_d2(const vd_int32_d2_t vd0);
int16_t mipp_hadds_int16_d2(const vd_int16_d2_t vd0);
int8_t mipp_hadds_int8_d2(const vd_int8_d2_t vd0);
uint64_t mipp_hadds_uint64_d2(const vd_uint64_d2_t vd0);
uint32_t mipp_hadds_uint32_d2(const vd_uint32_d2_t vd0);
uint16_t mipp_hadds_uint16_d2(const vd_uint16_d2_t vd0);
uint8_t mipp_hadds_uint8_d2(const vd_uint8_d2_t vd0);
// LMUL = 1
template <typename T> T hadds(const vd<T> vd0);
// LMUL = 2
template <typename T> T hadds(const vd<T, 2> vd0);
// LMUL = 4
template <typename T> T hadds(const vd<T, 4> vd0);
// LMUL = 8
template <typename T> T hadds(const vd<T, 8> vd0);
// d2 (half-vector)
template <typename T> T hadds(const vd<T, -2> vd0);
// LMUL = 1
template <typename T> T hadds(const Vd<T>& vd0);
// LMUL = 2
template <typename T> T hadds(const Vd<T, 2>& vd0);
// LMUL = 4
template <typename T> T hadds(const Vd<T, 4>& vd0);
// LMUL = 8
template <typename T> T hadds(const Vd<T, 8>& vd0);
// d2 (half-vector)
template <typename T> T hadds(const Vd<T, -2>& vd0);

HeadersΒΆ

// Option 1: Monolithic (entire library)
#include <mipp.h>

// Option 2: Category level
#include <mipp/c/cat/reduction.h>                   // all variants & LMULs
#include <mipp/c/cat/u/reduction.h>                 // 1D: 'u' variant only (*)
#include <mipp/c/cat/m2/reduction.h>                // 1D: LMUL=2 only (*)
#include <mipp/c/cat/u/m2/reduction.h>              // 2D: 'u' + LMUL=2 (*)

// Option 3: Function level (recommended for minimal dependencies)
#include <mipp/c/fun/hadds.h>                       // all variants & LMULs
#include <mipp/c/fun/u/hadds.h>                     // 1D: 'u' variant only (*)
#include <mipp/c/fun/m2/hadds.h>                    // 1D: LMUL=2 only (*)
#include <mipp/c/fun/u/m2/hadds.h>                  // 2D atomic: 'u' + LMUL=2 (fastest compilation) (*)

// (*) Fine-grained headers require MIPP generated with '--granularity fine'
// Option 1: Monolithic (entire library)
#include <mipp.hpp>

// Option 2: Category level
#include <mipp/cpp/cat/reduction.hpp>               // all variants & LMULs
#include <mipp/cpp/cat/u/reduction.hpp>             // 1D: 'u' variant only (*)
#include <mipp/cpp/cat/m2/reduction.hpp>            // 1D: LMUL=2 only (*)
#include <mipp/cpp/cat/u/m2/reduction.hpp>          // 2D: 'u' + LMUL=2 (*)

// Option 3: Function level (recommended for minimal dependencies)
#include <mipp/cpp/fun/hadds.hpp>                   // all variants & LMULs
#include <mipp/cpp/fun/u/hadds.hpp>                 // 1D: 'u' variant only (*)
#include <mipp/cpp/fun/m2/hadds.hpp>                // 1D: LMUL=2 only (*)
#include <mipp/cpp/fun/u/m2/hadds.hpp>              // 2D atomic: 'u' + LMUL=2 (fastest compilation) (*)

// (*) Fine-grained headers require MIPP generated with '--granularity fine'
// Option 1: Monolithic (entire library)
#include <mipp_obj.hpp>

// Option 2: Category level
#include <mipp/obj/cat/reduction.hpp>               // all variants & LMULs
#include <mipp/obj/cat/u/reduction.hpp>             // 1D: 'u' variant only (*)
#include <mipp/obj/cat/m2/reduction.hpp>            // 1D: LMUL=2 only (*)
#include <mipp/obj/cat/u/m2/reduction.hpp>          // 2D: 'u' + LMUL=2 (*)

// Option 3: Function level (recommended for minimal dependencies)
#include <mipp/obj/fun/hadds.hpp>                   // all variants & LMULs
#include <mipp/obj/fun/u/hadds.hpp>                 // 1D: 'u' variant only (*)
#include <mipp/obj/fun/m2/hadds.hpp>                // 1D: LMUL=2 only (*)
#include <mipp/obj/fun/u/m2/hadds.hpp>              // 2D atomic: 'u' + LMUL=2 (fastest compilation) (*)

// (*) Fine-grained headers require MIPP generated with '--granularity fine'

Supported DatatypesΒΆ

  • int64
  • int32
  • int16
  • int8
  • uint64
  • uint32
  • uint16
  • uint8

Mask Execution VariantsΒΆ

  • Unmasked (no suffix / <mipp::U>): every lane is computed, no mask argument.
  • Zero-Masked (_maskz / <mipp::Z>): only the active lanes are added: the inactive lanes count as 0, which is also the result when no lane is active.

The mask comes first, then the arguments of the unmasked function:

// Zero-Masked, LMUL = 1
int64_t mipp_hadds_int64_maskz_m1(const vm_int64_m1_t vm0, const vd_int64_m1_t vd0);
int32_t mipp_hadds_int32_maskz_m1(const vm_int32_m1_t vm0, const vd_int32_m1_t vd0);
int16_t mipp_hadds_int16_maskz_m1(const vm_int16_m1_t vm0, const vd_int16_m1_t vd0);
int8_t mipp_hadds_int8_maskz_m1(const vm_int8_m1_t vm0, const vd_int8_m1_t vd0);
uint64_t mipp_hadds_uint64_maskz_m1(const vm_uint64_m1_t vm0, const vd_uint64_m1_t vd0);
uint32_t mipp_hadds_uint32_maskz_m1(const vm_uint32_m1_t vm0, const vd_uint32_m1_t vd0);
uint16_t mipp_hadds_uint16_maskz_m1(const vm_uint16_m1_t vm0, const vd_uint16_m1_t vd0);
uint8_t mipp_hadds_uint8_maskz_m1(const vm_uint8_m1_t vm0, const vd_uint8_m1_t vd0);
// Zero-Masked, LMUL = 1
template <VARIANT V = Z, typename T> T hadds(const vm<T> vm0, const vd<T> vd0);
// Zero-Masked, LMUL = 1
template <VARIANT V = Z, typename T> T hadds(const Vm<T>& vm0, const Vd<T>& vd0);

Architecture & Implementation MatrixΒΆ

Legend: L0 Native Hardware β€’ L1 Dedicated Emulation β€’ L2 Generic Emulation β€’ L3 Scalar Fallback β€’ βˆ’ N/A  β€”  Full details. When the level depends on a compiler flag, the best level is shown.

Architecture f64 f32 i64 i32 i16 i8 u64 u32 u16 u8
SSE βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
AVX βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
AVX512 βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
NEON βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
RVV βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
SVE βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
Architecture f64 f32 i64 i32 i16 i8 u64 u32 u16 u8
SSE βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
AVX βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
AVX512 βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
NEON βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
RVV βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3
SVE βˆ’ βˆ’ L3 L3 L3 L3 L3 L3 L3 L3

Reference AlgorithmΒΆ

The scalar reference of MIPP defines the expected result of the function, for each datatype and mask variant: the unit tests compare the SIMD implementations of every target with it.

int64_t mipp_scalar_hadds_int64_m1(const vd_scalar_int64_m1_t vd0) {
    int64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT64; i++)
    {
        int64_t a = vd0.vd[i];
        int64_t b = resv64;

        resv64 =
            (b > 0 && a > INT64_MAX - b) ? (int64_t)INT64_MAX :
            (b < 0 && a < INT64_MIN - b) ? (int64_t)INT64_MIN :
            (int64_t)(a + b);
    }
    return resv64;
}
int64_t mipp_scalar_hadds_int64_maskz_m1(const vm_scalar_int64_m1_t vm0, const vd_scalar_int64_m1_t vd0) {
    int64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT64; i++)
    {
        int64_t a = vm0.vm[i] ? (vd0.vd[i]) : 0;
        int64_t b = resv64;

        resv64 =
            (b > 0 && a > INT64_MAX - b) ? (int64_t)INT64_MAX :
            (b < 0 && a < INT64_MIN - b) ? (int64_t)INT64_MIN :
            (int64_t)(a + b);
    }
    return resv64;
}
int32_t mipp_scalar_hadds_int32_m1(const vd_scalar_int32_m1_t vd0) {
    int64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT32; i++){
        resv64 += vd0.vd[i];
    }
    return resv64 > INT32_MAX ? (int32_t)INT32_MAX : resv64 < INT32_MIN ? (int32_t)INT32_MIN : (int32_t)resv64;
}
int32_t mipp_scalar_hadds_int32_maskz_m1(const vm_scalar_int32_m1_t vm0, const vd_scalar_int32_m1_t vd0) {
    int64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT32; i++){
        resv64 += vm0.vm[i] ? (vd0.vd[i]) : 0;
    }
    return resv64 > INT32_MAX ? (int32_t)INT32_MAX : resv64 < INT32_MIN ? (int32_t)INT32_MIN : (int32_t)resv64;
}
int16_t mipp_scalar_hadds_int16_m1(const vd_scalar_int16_m1_t vd0) {
    int64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT16; i++){
        resv64 += vd0.vd[i];
    }
    return resv64 > INT16_MAX ? (int16_t)INT16_MAX : resv64 < INT16_MIN ? (int16_t)INT16_MIN : (int16_t)resv64;
}
int16_t mipp_scalar_hadds_int16_maskz_m1(const vm_scalar_int16_m1_t vm0, const vd_scalar_int16_m1_t vd0) {
    int64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT16; i++){
        resv64 += vm0.vm[i] ? (vd0.vd[i]) : 0;
    }
    return resv64 > INT16_MAX ? (int16_t)INT16_MAX : resv64 < INT16_MIN ? (int16_t)INT16_MIN : (int16_t)resv64;
}
int8_t mipp_scalar_hadds_int8_m1(const vd_scalar_int8_m1_t vd0) {
    int64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT8; i++){
        resv64 += vd0.vd[i];
    }
    return resv64 > INT8_MAX ? (int8_t)INT8_MAX : resv64 < INT8_MIN ? (int8_t)INT8_MIN : (int8_t)resv64;
}
int8_t mipp_scalar_hadds_int8_maskz_m1(const vm_scalar_int8_m1_t vm0, const vd_scalar_int8_m1_t vd0) {
    int64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT8; i++){
        resv64 += vm0.vm[i] ? (vd0.vd[i]) : 0;
    }
    return resv64 > INT8_MAX ? (int8_t)INT8_MAX : resv64 < INT8_MIN ? (int8_t)INT8_MIN : (int8_t)resv64;
}
uint64_t mipp_scalar_hadds_uint64_m1(const vd_scalar_uint64_m1_t vd0) {
    uint64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT64; i++)
    {
        uint64_t a = vd0.vd[i];
        uint64_t b = resv64;

        resv64 =
            (a > UINT64_MAX - b) ? UINT64_MAX :
            (uint64_t)(a + b);
    }
    return resv64;
}
uint64_t mipp_scalar_hadds_uint64_maskz_m1(const vm_scalar_uint64_m1_t vm0, const vd_scalar_uint64_m1_t vd0) {
    uint64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT64; i++)
    {
        uint64_t a = vm0.vm[i] ? (vd0.vd[i]) : 0;
        uint64_t b = resv64;

        resv64 =
            (a > UINT64_MAX - b) ? UINT64_MAX :
            (uint64_t)(a + b);
    }
    return resv64;
}
uint32_t mipp_scalar_hadds_uint32_m1(const vd_scalar_uint32_m1_t vd0) {
    uint64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT32; i++)
    {
        resv64 += vd0.vd[i];
    }
    return resv64 > UINT32_MAX ? (uint32_t)UINT32_MAX : (uint32_t)resv64;
}
uint32_t mipp_scalar_hadds_uint32_maskz_m1(const vm_scalar_uint32_m1_t vm0, const vd_scalar_uint32_m1_t vd0) {
    uint64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT32; i++)
    {
        resv64 += vm0.vm[i] ? (vd0.vd[i]) : 0;
    }
    return resv64 > UINT32_MAX ? (uint32_t)UINT32_MAX : (uint32_t)resv64;
}
uint16_t mipp_scalar_hadds_uint16_m1(const vd_scalar_uint16_m1_t vd0) {
    uint64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT16; i++)
    {
        resv64 += vd0.vd[i];
    }
    return resv64 > UINT16_MAX ? (uint16_t)UINT16_MAX : (uint16_t)resv64;
}
uint16_t mipp_scalar_hadds_uint16_maskz_m1(const vm_scalar_uint16_m1_t vm0, const vd_scalar_uint16_m1_t vd0) {
    uint64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT16; i++)
    {
        resv64 += vm0.vm[i] ? (vd0.vd[i]) : 0;
    }
    return resv64 > UINT16_MAX ? (uint16_t)UINT16_MAX : (uint16_t)resv64;
}
uint8_t mipp_scalar_hadds_uint8_m1(const vd_scalar_uint8_m1_t vd0) {
    uint64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT8; i++)
    {
        resv64 += vd0.vd[i];
    }
    return resv64 > UINT8_MAX ? (uint8_t)UINT8_MAX : (uint8_t)resv64;
}
uint8_t mipp_scalar_hadds_uint8_maskz_m1(const vm_scalar_uint8_m1_t vm0, const vd_scalar_uint8_m1_t vd0) {
    uint64_t resv64 = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT8; i++)
    {
        resv64 += vm0.vm[i] ? (vd0.vd[i]) : 0;
    }
    return resv64 > UINT8_MAX ? (uint8_t)UINT8_MAX : (uint8_t)resv64;
}

Test & Verification SpecificationsΒΆ

How the unit tests draw the inputs (random values) and compare each lane of the result with the scalar reference, as in the generated C API tests.

Datatypes Inputs Comparison with the reference
int64 [-2000000, 2000000] Exact
int32 [-20000, 20000] Exact
int16 [-1000, 1000] Exact
int8 [-30, 30] Exact
uint64 [0, 4000000] Exact
uint32 [0, 40000] Exact
uint16 [0, 2000] Exact
uint8 [0, 40] Exact

Input masks: random (each lane active with probability 1/2).

  • An iteration is skipped when the sum of the lanes overflows the datatype.