Skip to content

cvtroundeven

Converts the floating-point elements to integers of the same size, rounded to the nearest integer with the ties to the even neighbor: 2.5 gives 2, 3.5 gives 4, as cvt(roundeven(vd0)). The result is exactly the one of cvt applied to the rounded register, in one instruction on AVX-512 (AVX-512 DQ for float64), NEON from ARMv8 (float64 on AArch64), RVV, and on SSE and AVX for float32 to int32 (cvtps2dq, which rounds with the rounding mode of the environment, MXCSR, to the nearest even by default); elsewhere, the rounding then the conversion. The conversion of a NaN, or of a value whose rounding is out of the range of the destination, gives a result that depends on the target.

Mathematical semantics: \(\text{res}_i = (T_{\text{dst}}) \operatorname{roundeven}(vd0_i)\)

Open in the API Explorer →

Prototypes

// LMUL = 1 (also without the _m1 suffix)
vd_int64_m1_t mipp_cvtroundeven_float64_int64_m1(const vd_float64_m1_t vd0);
vd_int32_m1_t mipp_cvtroundeven_float32_int32_m1(const vd_float32_m1_t vd0);
vd_uint64_m1_t mipp_cvtroundeven_float64_uint64_m1(const vd_float64_m1_t vd0);
vd_uint32_m1_t mipp_cvtroundeven_float32_uint32_m1(const vd_float32_m1_t vd0);
// LMUL = 2
vd_int64_m2_t mipp_cvtroundeven_float64_int64_m2(const vd_float64_m2_t vd0);
vd_int32_m2_t mipp_cvtroundeven_float32_int32_m2(const vd_float32_m2_t vd0);
vd_uint64_m2_t mipp_cvtroundeven_float64_uint64_m2(const vd_float64_m2_t vd0);
vd_uint32_m2_t mipp_cvtroundeven_float32_uint32_m2(const vd_float32_m2_t vd0);
// LMUL = 4
vd_int64_m4_t mipp_cvtroundeven_float64_int64_m4(const vd_float64_m4_t vd0);
vd_int32_m4_t mipp_cvtroundeven_float32_int32_m4(const vd_float32_m4_t vd0);
vd_uint64_m4_t mipp_cvtroundeven_float64_uint64_m4(const vd_float64_m4_t vd0);
vd_uint32_m4_t mipp_cvtroundeven_float32_uint32_m4(const vd_float32_m4_t vd0);
// LMUL = 8
vd_int64_m8_t mipp_cvtroundeven_float64_int64_m8(const vd_float64_m8_t vd0);
vd_int32_m8_t mipp_cvtroundeven_float32_int32_m8(const vd_float32_m8_t vd0);
vd_uint64_m8_t mipp_cvtroundeven_float64_uint64_m8(const vd_float64_m8_t vd0);
vd_uint32_m8_t mipp_cvtroundeven_float32_uint32_m8(const vd_float32_m8_t vd0);
// d2 (half-vector)
vd_int64_d2_t mipp_cvtroundeven_float64_int64_d2(const vd_float64_d2_t vd0);
vd_int32_d2_t mipp_cvtroundeven_float32_int32_d2(const vd_float32_d2_t vd0);
vd_uint64_d2_t mipp_cvtroundeven_float64_uint64_d2(const vd_float64_d2_t vd0);
vd_uint32_d2_t mipp_cvtroundeven_float32_uint32_d2(const vd_float32_d2_t vd0);
// LMUL = 1
vd<int64_t> cvtroundeven_int64(const vd<float64_t> vd0);
vd<int32_t> cvtroundeven_int32(const vd<float32_t> vd0);
vd<uint64_t> cvtroundeven_uint64(const vd<float64_t> vd0);
vd<uint32_t> cvtroundeven_uint32(const vd<float32_t> vd0);
// LMUL = 2
vd<int64_t, 2> cvtroundeven_int64(const vd<float64_t, 2> vd0);
vd<int32_t, 2> cvtroundeven_int32(const vd<float32_t, 2> vd0);
vd<uint64_t, 2> cvtroundeven_uint64(const vd<float64_t, 2> vd0);
vd<uint32_t, 2> cvtroundeven_uint32(const vd<float32_t, 2> vd0);
// LMUL = 4
vd<int64_t, 4> cvtroundeven_int64(const vd<float64_t, 4> vd0);
vd<int32_t, 4> cvtroundeven_int32(const vd<float32_t, 4> vd0);
vd<uint64_t, 4> cvtroundeven_uint64(const vd<float64_t, 4> vd0);
vd<uint32_t, 4> cvtroundeven_uint32(const vd<float32_t, 4> vd0);
// LMUL = 8
vd<int64_t, 8> cvtroundeven_int64(const vd<float64_t, 8> vd0);
vd<int32_t, 8> cvtroundeven_int32(const vd<float32_t, 8> vd0);
vd<uint64_t, 8> cvtroundeven_uint64(const vd<float64_t, 8> vd0);
vd<uint32_t, 8> cvtroundeven_uint32(const vd<float32_t, 8> vd0);
// d2 (half-vector)
vd<int64_t, -2> cvtroundeven_int64(const vd<float64_t, -2> vd0);
vd<int32_t, -2> cvtroundeven_int32(const vd<float32_t, -2> vd0);
vd<uint64_t, -2> cvtroundeven_uint64(const vd<float64_t, -2> vd0);
vd<uint32_t, -2> cvtroundeven_uint32(const vd<float32_t, -2> vd0);
// LMUL = 1
template <typename T_DST, typename T_SRC> Vd<T_DST> cvtroundeven(const Vd<T_SRC>& vd0);
// LMUL = 2
template <typename T_DST, typename T_SRC> Vd<T_DST, 2> cvtroundeven(const Vd<T_SRC, 2>& vd0);
// LMUL = 4
template <typename T_DST, typename T_SRC> Vd<T_DST, 4> cvtroundeven(const Vd<T_SRC, 4>& vd0);
// LMUL = 8
template <typename T_DST, typename T_SRC> Vd<T_DST, 8> cvtroundeven(const Vd<T_SRC, 8>& vd0);
// d2 (half-vector)
template <typename T_DST, typename T_SRC> Vd<T_DST, -2> cvtroundeven(const Vd<T_SRC, -2>& vd0);

Headers

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

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

// Option 3: Function level (recommended for minimal dependencies)
#include <mipp/c/fun/cvtroundeven.h>                // all variants & LMULs
#include <mipp/c/fun/u/cvtroundeven.h>              // 1D: 'u' variant only (*)
#include <mipp/c/fun/m2/cvtroundeven.h>             // 1D: LMUL=2 only (*)
#include <mipp/c/fun/u/m2/cvtroundeven.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/convert.hpp>                 // all variants & LMULs
#include <mipp/cpp/cat/u/convert.hpp>               // 1D: 'u' variant only (*)
#include <mipp/cpp/cat/m2/convert.hpp>              // 1D: LMUL=2 only (*)
#include <mipp/cpp/cat/u/m2/convert.hpp>            // 2D: 'u' + LMUL=2 (*)

// Option 3: Function level (recommended for minimal dependencies)
#include <mipp/cpp/fun/cvtroundeven.hpp>            // all variants & LMULs
#include <mipp/cpp/fun/u/cvtroundeven.hpp>          // 1D: 'u' variant only (*)
#include <mipp/cpp/fun/m2/cvtroundeven.hpp>         // 1D: LMUL=2 only (*)
#include <mipp/cpp/fun/u/m2/cvtroundeven.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/convert.hpp>                 // all variants & LMULs
#include <mipp/obj/cat/u/convert.hpp>               // 1D: 'u' variant only (*)
#include <mipp/obj/cat/m2/convert.hpp>              // 1D: LMUL=2 only (*)
#include <mipp/obj/cat/u/m2/convert.hpp>            // 2D: 'u' + LMUL=2 (*)

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

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

Supported Datatypes

  • float64 → int64
  • float32 → int32
  • float64 → uint64
  • float32 → uint32

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→i64 f32→i32 f64→u64 f32→u32
SSE L2 L0 L2 L2
AVX L2 L0 L2 L2
AVX512 L0 L0 L0 L0
NEON L0 L0 L0 L0
RVV L0 L0 L0 L0
SVE L2 L2 L2 L2

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.

vd_scalar_int64_m1_t mipp_scalar_cvtroundeven_float64_int64_m1(const vd_scalar_float64_m1_t vd0) {
    vd_scalar_int64_m1_t res;
    for (size_t i = 0; i < MIPP_SCALAR_N_FLOAT64; i++)
    {
        // requires #include <math.h>; rounded to the nearest integer, a tie k + 0.5 to the even neighbor
        // 2 * round((k + 0.5) / 2), whatever the rounding mode of the environment
        res.vd[i] = (int64_t)(fabs(vd0.vd[i] - trunc(vd0.vd[i])) == 0.5 ? 2.0 * round(0.5 * vd0.vd[i]) : round(vd0.vd[i]));
    }
    return res;
}
vd_scalar_int32_m1_t mipp_scalar_cvtroundeven_float32_int32_m1(const vd_scalar_float32_m1_t vd0) {
    vd_scalar_int32_m1_t res;
    for (size_t i = 0; i < MIPP_SCALAR_N_FLOAT32; i++)
    {
        // requires #include <math.h>; rounded to the nearest integer, a tie k + 0.5 to the even neighbor
        // 2 * round((k + 0.5) / 2), whatever the rounding mode of the environment
        res.vd[i] = (int32_t)(fabsf(vd0.vd[i] - truncf(vd0.vd[i])) == 0.5f ? 2.0f * roundf(0.5f * vd0.vd[i]) : roundf(vd0.vd[i]));
    }
    return res;
}
vd_scalar_uint64_m1_t mipp_scalar_cvtroundeven_float64_uint64_m1(const vd_scalar_float64_m1_t vd0) {
    vd_scalar_uint64_m1_t res;
    for (size_t i = 0; i < MIPP_SCALAR_N_FLOAT64; i++)
    {
        // requires #include <math.h>; rounded to the nearest integer, a tie k + 0.5 to the even neighbor
        // 2 * round((k + 0.5) / 2), whatever the rounding mode of the environment
        res.vd[i] = (uint64_t)(fabs(vd0.vd[i] - trunc(vd0.vd[i])) == 0.5 ? 2.0 * round(0.5 * vd0.vd[i]) : round(vd0.vd[i]));
    }
    return res;
}
vd_scalar_uint32_m1_t mipp_scalar_cvtroundeven_float32_uint32_m1(const vd_scalar_float32_m1_t vd0) {
    vd_scalar_uint32_m1_t res;
    for (size_t i = 0; i < MIPP_SCALAR_N_FLOAT32; i++)
    {
        // requires #include <math.h>; rounded to the nearest integer, a tie k + 0.5 to the even neighbor
        // 2 * round((k + 0.5) / 2), whatever the rounding mode of the environment
        res.vd[i] = (uint32_t)(fabsf(vd0.vd[i] - truncf(vd0.vd[i])) == 0.5f ? 2.0f * roundf(0.5f * vd0.vd[i]) : roundf(vd0.vd[i]));
    }
    return res;
}

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 → int64 [-9.223372036854775e+18, 9.223372036854775e+18] Exact
float32 → int32 [-2147483520, 2147483520] Exact
float64 → uint64 [0, 1.844674407370955e+19] Exact
float32 → uint32 [0, 4294967040] Exact
Special values

Every other iteration, the lanes of the inputs take these values (cycling, each further input one value later in the list), to cover the cases that random values never hit:

  • float32,int32: 0.5, 1.5, 2.5, -0.5, -1.5, -2.5, 0.25, -0.25, 0.75, -0.75, 2.999, -2.999, -0.0, 8388607.5, 16777215.0, 2147483520.0, -2147483520.0
  • float32,uint32: 0.5, 1.5, 2.5, 0.25, 0.75, 2.999, 0.0, 8388607.5, 16777215.0, 4294967040.0
  • float64,int64: 0.5, 1.5, 2.5, -0.5, -1.5, -2.5, 0.25, -0.25, 0.75, -0.75, 2.999, -2.999, -0.0, 4503599627370495.5, 1000000000000000.5, 9.223372036854775e+18, -9.223372036854775e+18
  • float64,uint64: 0.5, 1.5, 2.5, 0.25, 0.75, 2.999, 0.0, 4503599627370495.5, 1000000000000000.5, 1.844674407370955e+19