samedi 25 mai 2019

Selecting a constexpr index from a dynamic index

I have C++ code that calls some SSE/AVX functions taking immediate index values (e.g. int _mm_extract_epi16 (__m128i a, int imm8)). The problem is, that I don't know the index value at compile time, so I have to do a dynamic int to constexpr int conversion:

template <int index>
auto extract(__m128i a) noexcept
{
    static_assert(index < 8, "index must be less than 8");
    return _mm_extract_epi16(a, index);
}

auto extract(const __m128i a, const int index) noexcept
{
    switch (index) {
        case 0:  return extract<0>(a);
        case 1:  return extract<1>(a);
        case 2:  return extract<2>(a);
        case 3:  return extract<3>(a);
        case 4:  return extract<4>(a);
        case 5:  return extract<5>(a);
        case 6:  return extract<6>(a);
        case 7:  return extract<7>(a);
        default: return extract<7>(a);
    }
}

I'm upgrading my code-base to AVX2 and AVX-512 (and therefore extending the number of cases to 16 and 32, respectively), and started wondering if there was a better way to do this? Or at-least a way that avoids so much typing?

Aucun commentaire:

Enregistrer un commentaire