ncvtsrshiftround¶
Shifts each element right by s0 bits with a rounding to the nearest (ties up), then converts it to the integer type of half the size with a saturation: ncvts(rshiftround(vd0, s0)) in one operation, the requantization of the fixed-point arithmetic and of the quantized neural networks (an int32 accumulator scaled down to int16, an int16 to int8). The result has half the LMUL of the argument, as ncvts. Counts from 0 to the number of bits of the source type minus 1. One instruction on RVV (vnclip in the rounding mode rnu), two on NEON (vrshl, then vqmovn); the shift then the narrowing elsewhere.
Mathematical semantics: \(\text{res}_i = \operatorname{sat}_{T_{\text{dst}}}\left( \left\lfloor \frac{vd0_i + 2^{s0 - 1}}{2^{s0}} \right\rfloor \right)\)
Prototypes¶
// LMUL = 1 (also without the _m1 suffix)
vd_int32_d2_t mipp_ncvtsrshiftround_int64_int32_m1(const vd_int64_m1_t vd0, const int32_t s0);
vd_int16_d2_t mipp_ncvtsrshiftround_int32_int16_m1(const vd_int32_m1_t vd0, const int32_t s0);
vd_int8_d2_t mipp_ncvtsrshiftround_int16_int8_m1(const vd_int16_m1_t vd0, const int32_t s0);
vd_uint32_d2_t mipp_ncvtsrshiftround_uint64_uint32_m1(const vd_uint64_m1_t vd0, const int32_t s0);
vd_uint16_d2_t mipp_ncvtsrshiftround_uint32_uint16_m1(const vd_uint32_m1_t vd0, const int32_t s0);
vd_uint8_d2_t mipp_ncvtsrshiftround_uint16_uint8_m1(const vd_uint16_m1_t vd0, const int32_t s0);
// LMUL = 2
vd_int32_m1_t mipp_ncvtsrshiftround_int64_int32_m2(const vd_int64_m2_t vd0, const int32_t s0);
vd_int16_m1_t mipp_ncvtsrshiftround_int32_int16_m2(const vd_int32_m2_t vd0, const int32_t s0);
vd_int8_m1_t mipp_ncvtsrshiftround_int16_int8_m2(const vd_int16_m2_t vd0, const int32_t s0);
vd_uint32_m1_t mipp_ncvtsrshiftround_uint64_uint32_m2(const vd_uint64_m2_t vd0, const int32_t s0);
vd_uint16_m1_t mipp_ncvtsrshiftround_uint32_uint16_m2(const vd_uint32_m2_t vd0, const int32_t s0);
vd_uint8_m1_t mipp_ncvtsrshiftround_uint16_uint8_m2(const vd_uint16_m2_t vd0, const int32_t s0);
// LMUL = 4
vd_int32_m2_t mipp_ncvtsrshiftround_int64_int32_m4(const vd_int64_m4_t vd0, const int32_t s0);
vd_int16_m2_t mipp_ncvtsrshiftround_int32_int16_m4(const vd_int32_m4_t vd0, const int32_t s0);
vd_int8_m2_t mipp_ncvtsrshiftround_int16_int8_m4(const vd_int16_m4_t vd0, const int32_t s0);
vd_uint32_m2_t mipp_ncvtsrshiftround_uint64_uint32_m4(const vd_uint64_m4_t vd0, const int32_t s0);
vd_uint16_m2_t mipp_ncvtsrshiftround_uint32_uint16_m4(const vd_uint32_m4_t vd0, const int32_t s0);
vd_uint8_m2_t mipp_ncvtsrshiftround_uint16_uint8_m4(const vd_uint16_m4_t vd0, const int32_t s0);
// LMUL = 8
vd_int32_m4_t mipp_ncvtsrshiftround_int64_int32_m8(const vd_int64_m8_t vd0, const int32_t s0);
vd_int16_m4_t mipp_ncvtsrshiftround_int32_int16_m8(const vd_int32_m8_t vd0, const int32_t s0);
vd_int8_m4_t mipp_ncvtsrshiftround_int16_int8_m8(const vd_int16_m8_t vd0, const int32_t s0);
vd_uint32_m4_t mipp_ncvtsrshiftround_uint64_uint32_m8(const vd_uint64_m8_t vd0, const int32_t s0);
vd_uint16_m4_t mipp_ncvtsrshiftround_uint32_uint16_m8(const vd_uint32_m8_t vd0, const int32_t s0);
vd_uint8_m4_t mipp_ncvtsrshiftround_uint16_uint8_m8(const vd_uint16_m8_t vd0, const int32_t s0);
// LMUL = 1
vd<int32_t, -2> ncvtsrshiftround_int32(const vd<int64_t> vd0);
vd<int16_t, -2> ncvtsrshiftround_int16(const vd<int32_t> vd0);
vd<int8_t, -2> ncvtsrshiftround_int8(const vd<int16_t> vd0);
vd<uint32_t, -2> ncvtsrshiftround_uint32(const vd<uint64_t> vd0);
vd<uint16_t, -2> ncvtsrshiftround_uint16(const vd<uint32_t> vd0);
vd<uint8_t, -2> ncvtsrshiftround_uint8(const vd<uint16_t> vd0);
// LMUL = 2
vd<int32_t> ncvtsrshiftround_int32(const vd<int64_t, 2> vd0);
vd<int16_t> ncvtsrshiftround_int16(const vd<int32_t, 2> vd0);
vd<int8_t> ncvtsrshiftround_int8(const vd<int16_t, 2> vd0);
vd<uint32_t> ncvtsrshiftround_uint32(const vd<uint64_t, 2> vd0);
vd<uint16_t> ncvtsrshiftround_uint16(const vd<uint32_t, 2> vd0);
vd<uint8_t> ncvtsrshiftround_uint8(const vd<uint16_t, 2> vd0);
// LMUL = 4
vd<int32_t, 2> ncvtsrshiftround_int32(const vd<int64_t, 4> vd0);
vd<int16_t, 2> ncvtsrshiftround_int16(const vd<int32_t, 4> vd0);
vd<int8_t, 2> ncvtsrshiftround_int8(const vd<int16_t, 4> vd0);
vd<uint32_t, 2> ncvtsrshiftround_uint32(const vd<uint64_t, 4> vd0);
vd<uint16_t, 2> ncvtsrshiftround_uint16(const vd<uint32_t, 4> vd0);
vd<uint8_t, 2> ncvtsrshiftround_uint8(const vd<uint16_t, 4> vd0);
// LMUL = 8
vd<int32_t, 4> ncvtsrshiftround_int32(const vd<int64_t, 8> vd0);
vd<int16_t, 4> ncvtsrshiftround_int16(const vd<int32_t, 8> vd0);
vd<int8_t, 4> ncvtsrshiftround_int8(const vd<int16_t, 8> vd0);
vd<uint32_t, 4> ncvtsrshiftround_uint32(const vd<uint64_t, 8> vd0);
vd<uint16_t, 4> ncvtsrshiftround_uint16(const vd<uint32_t, 8> vd0);
vd<uint8_t, 4> ncvtsrshiftround_uint8(const vd<uint16_t, 8> vd0);
// LMUL = 1
template <typename T_DST, typename T_SRC> Vd<T_DST, -2> ncvtsrshiftround(const Vd<T_SRC>& vd0);
// LMUL = 2
template <typename T_DST, typename T_SRC> Vd<T_DST> ncvtsrshiftround(const Vd<T_SRC, 2>& vd0);
// LMUL = 4
template <typename T_DST, typename T_SRC> Vd<T_DST, 2> ncvtsrshiftround(const Vd<T_SRC, 4>& vd0);
// LMUL = 8
template <typename T_DST, typename T_SRC> Vd<T_DST, 4> ncvtsrshiftround(const Vd<T_SRC, 8>& 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/ncvtsrshiftround.h> // all variants & LMULs
#include <mipp/c/fun/u/ncvtsrshiftround.h> // 1D: 'u' variant only (*)
#include <mipp/c/fun/m2/ncvtsrshiftround.h> // 1D: LMUL=2 only (*)
#include <mipp/c/fun/u/m2/ncvtsrshiftround.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/ncvtsrshiftround.hpp> // all variants & LMULs
#include <mipp/cpp/fun/u/ncvtsrshiftround.hpp> // 1D: 'u' variant only (*)
#include <mipp/cpp/fun/m2/ncvtsrshiftround.hpp> // 1D: LMUL=2 only (*)
#include <mipp/cpp/fun/u/m2/ncvtsrshiftround.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/ncvtsrshiftround.hpp> // all variants & LMULs
#include <mipp/obj/fun/u/ncvtsrshiftround.hpp> // 1D: 'u' variant only (*)
#include <mipp/obj/fun/m2/ncvtsrshiftround.hpp> // 1D: LMUL=2 only (*)
#include <mipp/obj/fun/u/m2/ncvtsrshiftround.hpp> // 2D atomic: 'u' + LMUL=2 (fastest compilation) (*)
// (*) Fine-grained headers require MIPP generated with '--granularity fine'
Supported Datatypes¶
int64→int32int32→int16int16→int8uint64→uint32uint32→uint16uint16→uint8
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.
The levels are the ones at LMUL = 1 of the argument.
| Architecture | i64→i32 | i32→i16 | i16→i8 | u64→u32 | u32→u16 | u16→u8 |
|---|---|---|---|---|---|---|
| SSE | L2 | L2 | L2 | L2 | L2 | L2 |
| AVX | L2 | L2 | L2 | L2 | L2 | L2 |
| AVX512 | L2 | L2 | L2 | L2 | L2 | L2 |
| NEON | L1 | L1 | L1 | L1 | L1 | L1 |
| RVV | L0 | L0 | L0 | L0 | L0 | L0 |
| SVE | L2 | L2 | 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_int32_d2_t mipp_scalar_ncvtsrshiftround_int64_int32_m1(const vd_scalar_int64_m1_t vd0, const int32_t s0) {
vd_scalar_int32_d2_t res;
for (size_t i = 0; i < MIPP_SCALAR_N_INT32_D2; i++) {
// (v + 2^(s0-1)) >> s0 without overflow, then the saturation to the narrower type
int64_t v = vd0.vd[i];
v = (int64_t)((v >> s0) + (s0 > 0 ? ((v >> (s0 - 1)) & 1) : 0));
res.vd[i] = (int32_t)(v < INT32_MIN ? INT32_MIN : (v > INT32_MAX ? INT32_MAX : v));
}
return res;
}
vd_scalar_int16_d2_t mipp_scalar_ncvtsrshiftround_int32_int16_m1(const vd_scalar_int32_m1_t vd0, const int32_t s0) {
vd_scalar_int16_d2_t res;
for (size_t i = 0; i < MIPP_SCALAR_N_INT16_D2; i++) {
// (v + 2^(s0-1)) >> s0 without overflow, then the saturation to the narrower type
int32_t v = vd0.vd[i];
v = (int32_t)((v >> s0) + (s0 > 0 ? ((v >> (s0 - 1)) & 1) : 0));
res.vd[i] = (int16_t)(v < INT16_MIN ? INT16_MIN : (v > INT16_MAX ? INT16_MAX : v));
}
return res;
}
vd_scalar_int8_d2_t mipp_scalar_ncvtsrshiftround_int16_int8_m1(const vd_scalar_int16_m1_t vd0, const int32_t s0) {
vd_scalar_int8_d2_t res;
for (size_t i = 0; i < MIPP_SCALAR_N_INT8_D2; i++) {
// (v + 2^(s0-1)) >> s0 without overflow, then the saturation to the narrower type
int16_t v = vd0.vd[i];
v = (int16_t)((v >> s0) + (s0 > 0 ? ((v >> (s0 - 1)) & 1) : 0));
res.vd[i] = (int8_t)(v < INT8_MIN ? INT8_MIN : (v > INT8_MAX ? INT8_MAX : v));
}
return res;
}
vd_scalar_uint32_d2_t mipp_scalar_ncvtsrshiftround_uint64_uint32_m1(const vd_scalar_uint64_m1_t vd0, const int32_t s0) {
vd_scalar_uint32_d2_t res;
for (size_t i = 0; i < MIPP_SCALAR_N_UINT32_D2; i++) {
// (v + 2^(s0-1)) >> s0 without overflow, then the saturation to the narrower type
uint64_t v = vd0.vd[i];
v = (uint64_t)((v >> s0) + (s0 > 0 ? ((v >> (s0 - 1)) & 1) : 0));
res.vd[i] = (uint32_t)(v > UINT32_MAX ? UINT32_MAX : v);
}
return res;
}
vd_scalar_uint16_d2_t mipp_scalar_ncvtsrshiftround_uint32_uint16_m1(const vd_scalar_uint32_m1_t vd0, const int32_t s0) {
vd_scalar_uint16_d2_t res;
for (size_t i = 0; i < MIPP_SCALAR_N_UINT16_D2; i++) {
// (v + 2^(s0-1)) >> s0 without overflow, then the saturation to the narrower type
uint32_t v = vd0.vd[i];
v = (uint32_t)((v >> s0) + (s0 > 0 ? ((v >> (s0 - 1)) & 1) : 0));
res.vd[i] = (uint16_t)(v > UINT16_MAX ? UINT16_MAX : v);
}
return res;
}
vd_scalar_uint8_d2_t mipp_scalar_ncvtsrshiftround_uint16_uint8_m1(const vd_scalar_uint16_m1_t vd0, const int32_t s0) {
vd_scalar_uint8_d2_t res;
for (size_t i = 0; i < MIPP_SCALAR_N_UINT8_D2; i++) {
// (v + 2^(s0-1)) >> s0 without overflow, then the saturation to the narrower type
uint16_t v = vd0.vd[i];
v = (uint16_t)((v >> s0) + (s0 > 0 ? ((v >> (s0 - 1)) & 1) : 0));
res.vd[i] = (uint8_t)(v > UINT8_MAX ? UINT8_MAX : v);
}
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 (in1, in2, in3: inputs in the order of the arguments).
| Datatypes | Inputs | Comparison with the reference |
|---|---|---|
int64 → int32, uint64 → uint32 |
in1: full range in2: [0, 63] |
Exact |
int32 → int16, uint32 → uint16 |
in1: full range in2: [0, 31] |
Exact |
int16 → int8, uint16 → uint8 |
in1: full range in2: [0, 15] |
Exact |