Skip to content

hadd

Computes the horizontal addition (reduction sum) of all elements in a vector register. The integer sum wraps around (modulo \(2^n\)), so its result depends on the number of lanes; hadds saturates instead. For the floating-point types, the order of the additions depends on the target: the last bits of the result can differ from one target to another, and from a sequential sum.

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

Open in the API Explorer →

Prototypes

// LMUL = 1 (also without the _m1 suffix)
float64_t mipp_hadd_float64_m1(const vd_float64_m1_t vd0);
float32_t mipp_hadd_float32_m1(const vd_float32_m1_t vd0);
int64_t mipp_hadd_int64_m1(const vd_int64_m1_t vd0);
int32_t mipp_hadd_int32_m1(const vd_int32_m1_t vd0);
int16_t mipp_hadd_int16_m1(const vd_int16_m1_t vd0);
int8_t mipp_hadd_int8_m1(const vd_int8_m1_t vd0);
uint64_t mipp_hadd_uint64_m1(const vd_uint64_m1_t vd0);
uint32_t mipp_hadd_uint32_m1(const vd_uint32_m1_t vd0);
uint16_t mipp_hadd_uint16_m1(const vd_uint16_m1_t vd0);
uint8_t mipp_hadd_uint8_m1(const vd_uint8_m1_t vd0);
// LMUL = 2
float64_t mipp_hadd_float64_m2(const vd_float64_m2_t vd0);
float32_t mipp_hadd_float32_m2(const vd_float32_m2_t vd0);
int64_t mipp_hadd_int64_m2(const vd_int64_m2_t vd0);
int32_t mipp_hadd_int32_m2(const vd_int32_m2_t vd0);
int16_t mipp_hadd_int16_m2(const vd_int16_m2_t vd0);
int8_t mipp_hadd_int8_m2(const vd_int8_m2_t vd0);
uint64_t mipp_hadd_uint64_m2(const vd_uint64_m2_t vd0);
uint32_t mipp_hadd_uint32_m2(const vd_uint32_m2_t vd0);
uint16_t mipp_hadd_uint16_m2(const vd_uint16_m2_t vd0);
uint8_t mipp_hadd_uint8_m2(const vd_uint8_m2_t vd0);
// LMUL = 4
float64_t mipp_hadd_float64_m4(const vd_float64_m4_t vd0);
float32_t mipp_hadd_float32_m4(const vd_float32_m4_t vd0);
int64_t mipp_hadd_int64_m4(const vd_int64_m4_t vd0);
int32_t mipp_hadd_int32_m4(const vd_int32_m4_t vd0);
int16_t mipp_hadd_int16_m4(const vd_int16_m4_t vd0);
int8_t mipp_hadd_int8_m4(const vd_int8_m4_t vd0);
uint64_t mipp_hadd_uint64_m4(const vd_uint64_m4_t vd0);
uint32_t mipp_hadd_uint32_m4(const vd_uint32_m4_t vd0);
uint16_t mipp_hadd_uint16_m4(const vd_uint16_m4_t vd0);
uint8_t mipp_hadd_uint8_m4(const vd_uint8_m4_t vd0);
// LMUL = 8
float64_t mipp_hadd_float64_m8(const vd_float64_m8_t vd0);
float32_t mipp_hadd_float32_m8(const vd_float32_m8_t vd0);
int64_t mipp_hadd_int64_m8(const vd_int64_m8_t vd0);
int32_t mipp_hadd_int32_m8(const vd_int32_m8_t vd0);
int16_t mipp_hadd_int16_m8(const vd_int16_m8_t vd0);
int8_t mipp_hadd_int8_m8(const vd_int8_m8_t vd0);
uint64_t mipp_hadd_uint64_m8(const vd_uint64_m8_t vd0);
uint32_t mipp_hadd_uint32_m8(const vd_uint32_m8_t vd0);
uint16_t mipp_hadd_uint16_m8(const vd_uint16_m8_t vd0);
uint8_t mipp_hadd_uint8_m8(const vd_uint8_m8_t vd0);
// d2 (half-vector)
float64_t mipp_hadd_float64_d2(const vd_float64_d2_t vd0);
float32_t mipp_hadd_float32_d2(const vd_float32_d2_t vd0);
int64_t mipp_hadd_int64_d2(const vd_int64_d2_t vd0);
int32_t mipp_hadd_int32_d2(const vd_int32_d2_t vd0);
int16_t mipp_hadd_int16_d2(const vd_int16_d2_t vd0);
int8_t mipp_hadd_int8_d2(const vd_int8_d2_t vd0);
uint64_t mipp_hadd_uint64_d2(const vd_uint64_d2_t vd0);
uint32_t mipp_hadd_uint32_d2(const vd_uint32_d2_t vd0);
uint16_t mipp_hadd_uint16_d2(const vd_uint16_d2_t vd0);
uint8_t mipp_hadd_uint8_d2(const vd_uint8_d2_t vd0);
// LMUL = 1
template <typename T> T hadd(const vd<T> vd0);
// LMUL = 2
template <typename T> T hadd(const vd<T, 2> vd0);
// LMUL = 4
template <typename T> T hadd(const vd<T, 4> vd0);
// LMUL = 8
template <typename T> T hadd(const vd<T, 8> vd0);
// d2 (half-vector)
template <typename T> T hadd(const vd<T, -2> vd0);
// LMUL = 1
template <typename T> T hadd(const Vd<T>& vd0);
// LMUL = 2
template <typename T> T hadd(const Vd<T, 2>& vd0);
// LMUL = 4
template <typename T> T hadd(const Vd<T, 4>& vd0);
// LMUL = 8
template <typename T> T hadd(const Vd<T, 8>& vd0);
// d2 (half-vector)
template <typename T> T hadd(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/hadd.h>                        // all variants & LMULs
#include <mipp/c/fun/u/hadd.h>                      // 1D: 'u' variant only (*)
#include <mipp/c/fun/m2/hadd.h>                     // 1D: LMUL=2 only (*)
#include <mipp/c/fun/u/m2/hadd.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/hadd.hpp>                    // all variants & LMULs
#include <mipp/cpp/fun/u/hadd.hpp>                  // 1D: 'u' variant only (*)
#include <mipp/cpp/fun/m2/hadd.hpp>                 // 1D: LMUL=2 only (*)
#include <mipp/cpp/fun/u/m2/hadd.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/hadd.hpp>                    // all variants & LMULs
#include <mipp/obj/fun/u/hadd.hpp>                  // 1D: 'u' variant only (*)
#include <mipp/obj/fun/m2/hadd.hpp>                 // 1D: LMUL=2 only (*)
#include <mipp/obj/fun/u/m2/hadd.hpp>               // 2D atomic: 'u' + LMUL=2 (fastest compilation) (*)

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

Supported Datatypes

  • float64
  • float32
  • 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
float64_t mipp_hadd_float64_maskz_m1(const vm_float64_m1_t vm0, const vd_float64_m1_t vd0);
float32_t mipp_hadd_float32_maskz_m1(const vm_float32_m1_t vm0, const vd_float32_m1_t vd0);
int64_t mipp_hadd_int64_maskz_m1(const vm_int64_m1_t vm0, const vd_int64_m1_t vd0);
int32_t mipp_hadd_int32_maskz_m1(const vm_int32_m1_t vm0, const vd_int32_m1_t vd0);
int16_t mipp_hadd_int16_maskz_m1(const vm_int16_m1_t vm0, const vd_int16_m1_t vd0);
int8_t mipp_hadd_int8_maskz_m1(const vm_int8_m1_t vm0, const vd_int8_m1_t vd0);
uint64_t mipp_hadd_uint64_maskz_m1(const vm_uint64_m1_t vm0, const vd_uint64_m1_t vd0);
uint32_t mipp_hadd_uint32_maskz_m1(const vm_uint32_m1_t vm0, const vd_uint32_m1_t vd0);
uint16_t mipp_hadd_uint16_maskz_m1(const vm_uint16_m1_t vm0, const vd_uint16_m1_t vd0);
uint8_t mipp_hadd_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 hadd(const vm<T> vm0, const vd<T> vd0);
// Zero-Masked, LMUL = 1
template <VARIANT V = Z, typename T> T hadd(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 L1 L1 L1 L1 L1 L1 L1 L1 L1 L1
AVX L1 L1 L1 L1 L1 L1 L1 L1 L1 L1
AVX512 L1 L1 L1 L1 L1 L1 L0 L0 L1 L1
NEON L0 L0 L0 L0 L0 L0 L0 L0 L0 L0
RVV L1 L1 L1 L1 L1 L1 L1 L1 L1 L1
SVE L0 L0 L0 L0 L0 L0 L0 L0 L0 L0
Architecture f64 f32 i64 i32 i16 i8 u64 u32 u16 u8
SSE L2 L2 L2 L2 L2 L2 L2 L2 L2 L2
AVX L2 L2 L2 L2 L2 L2 L2 L2 L2 L2
AVX512 L0 L0 L0 L0 L2 L2 L0 L0 L2 L2
NEON L2 L2 L2 L2 L2 L2 L2 L2 L2 L2
RVV L1 L1 L1 L1 L1 L1 L1 L1 L1 L1
SVE L0 L0 L0 L0 L0 L0 L0 L0 L0 L0

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.

float64_t mipp_scalar_hadd_float64_m1(const vd_scalar_float64_m1_t vd0) {
    float64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_FLOAT64; i++)
        resv +=   vd0.vd[i];
    return resv;
}
float64_t mipp_scalar_hadd_float64_maskz_m1(const vm_scalar_float64_m1_t vm0, const vd_scalar_float64_m1_t vd0) {
    float64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_FLOAT64; i++)
        resv +=  vm0.vm[i] ? ( vd0.vd[i]) : 0;
    return resv;
}
float32_t mipp_scalar_hadd_float32_m1(const vd_scalar_float32_m1_t vd0) {
    float32_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_FLOAT32; i++)
        resv +=   vd0.vd[i];
    return resv;
}
float32_t mipp_scalar_hadd_float32_maskz_m1(const vm_scalar_float32_m1_t vm0, const vd_scalar_float32_m1_t vd0) {
    float32_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_FLOAT32; i++)
        resv +=  vm0.vm[i] ? ( vd0.vd[i]) : 0;
    return resv;
}
int64_t mipp_scalar_hadd_int64_m1(const vd_scalar_int64_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT64; i++)
        resv += (uint64_t)vd0.vd[i];
    return (int64_t)resv;
}
int64_t mipp_scalar_hadd_int64_maskz_m1(const vm_scalar_int64_m1_t vm0, const vd_scalar_int64_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT64; i++)
        resv += vm0.vm[i] ? ((uint64_t)vd0.vd[i]) : 0;
    return (int64_t)resv;
}
int32_t mipp_scalar_hadd_int32_m1(const vd_scalar_int32_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT32; i++)
        resv += (uint64_t)vd0.vd[i];
    return (int32_t)resv;
}
int32_t mipp_scalar_hadd_int32_maskz_m1(const vm_scalar_int32_m1_t vm0, const vd_scalar_int32_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT32; i++)
        resv += vm0.vm[i] ? ((uint64_t)vd0.vd[i]) : 0;
    return (int32_t)resv;
}
int16_t mipp_scalar_hadd_int16_m1(const vd_scalar_int16_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT16; i++)
        resv += (uint64_t)vd0.vd[i];
    return (int16_t)resv;
}
int16_t mipp_scalar_hadd_int16_maskz_m1(const vm_scalar_int16_m1_t vm0, const vd_scalar_int16_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT16; i++)
        resv += vm0.vm[i] ? ((uint64_t)vd0.vd[i]) : 0;
    return (int16_t)resv;
}
int8_t mipp_scalar_hadd_int8_m1(const vd_scalar_int8_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT8; i++)
        resv += (uint64_t)vd0.vd[i];
    return (int8_t)resv;
}
int8_t mipp_scalar_hadd_int8_maskz_m1(const vm_scalar_int8_m1_t vm0, const vd_scalar_int8_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_INT8; i++)
        resv += vm0.vm[i] ? ((uint64_t)vd0.vd[i]) : 0;
    return (int8_t)resv;
}
uint64_t mipp_scalar_hadd_uint64_m1(const vd_scalar_uint64_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT64; i++)
        resv += (uint64_t)vd0.vd[i];
    return (uint64_t)resv;
}
uint64_t mipp_scalar_hadd_uint64_maskz_m1(const vm_scalar_uint64_m1_t vm0, const vd_scalar_uint64_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT64; i++)
        resv += vm0.vm[i] ? ((uint64_t)vd0.vd[i]) : 0;
    return (uint64_t)resv;
}
uint32_t mipp_scalar_hadd_uint32_m1(const vd_scalar_uint32_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT32; i++)
        resv += (uint64_t)vd0.vd[i];
    return (uint32_t)resv;
}
uint32_t mipp_scalar_hadd_uint32_maskz_m1(const vm_scalar_uint32_m1_t vm0, const vd_scalar_uint32_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT32; i++)
        resv += vm0.vm[i] ? ((uint64_t)vd0.vd[i]) : 0;
    return (uint32_t)resv;
}
uint16_t mipp_scalar_hadd_uint16_m1(const vd_scalar_uint16_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT16; i++)
        resv += (uint64_t)vd0.vd[i];
    return (uint16_t)resv;
}
uint16_t mipp_scalar_hadd_uint16_maskz_m1(const vm_scalar_uint16_m1_t vm0, const vd_scalar_uint16_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT16; i++)
        resv += vm0.vm[i] ? ((uint64_t)vd0.vd[i]) : 0;
    return (uint16_t)resv;
}
uint8_t mipp_scalar_hadd_uint8_m1(const vd_scalar_uint8_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT8; i++)
        resv += (uint64_t)vd0.vd[i];
    return (uint8_t)resv;
}
uint8_t mipp_scalar_hadd_uint8_maskz_m1(const vm_scalar_uint8_m1_t vm0, const vd_scalar_uint8_m1_t vd0) {
    // in uint64_t: the integer sums wrap around (modulo 2^n), without the undefined overflow of the signed types
    uint64_t resv = 0;
    for (size_t i = 0; i < MIPP_SCALAR_N_UINT8; i++)
        resv += vm0.vm[i] ? ((uint64_t)vd0.vd[i]) : 0;
    return (uint8_t)resv;
}

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
float64 [-1, 1] |r − ref| ≤ 0.01 % of |ref|
float32 [-1, 1] |r − ref| ≤ 1 % of |ref|
int64, int32, int16, int8, uint64, uint32, uint16, uint8 full range Exact

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

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