/* * SPDX-FileCopyrightText: 2022 L. E. Segovia * * SPDX-License-Identifier: BSD-3-Clause */ #ifndef KO_RGBA_INTERLEAVERS #define KO_RGBA_INTERLEAVERS #include #if XSIMD_VERSION_MAJOR >= 10 #error "The interleavers use per-lane zipping semantics, which are not compatible with xsimd 10" #endif using namespace xsimd; template using enable_sized_t = typename std::enable_if::type; template using enable_sized_integral_t = typename std::enable_if::value && sizeof(T) == S, int>::type; template using enable_sized_vector_t = typename std::enable_if::size == S, int>::type; #if XSIMD_WITH_AVX2 template inline batch exchange_mid_halves(batch const &a, kernel::requires_arch) noexcept { return _mm256_castpd_ps(_mm256_permute4x64_pd(_mm256_castps_pd(a.data), 0xD8)); } template = 0> inline batch exchange_mid_halves(batch const &a, kernel::requires_arch) noexcept { return _mm256_permute4x64_epi64(a.data, 0xD8); } #endif #if XSIMD_WITH_AVX template inline batch merge_low(batch const &a, batch const &b, kernel::requires_arch) noexcept { return _mm256_insertf128_ps(a, _mm256_castps256_ps128(b), 1); } template = 0> inline batch merge_low(batch const &a, batch const &b, kernel::requires_arch) noexcept { return _mm256_insertf128_si256(a, _mm256_castsi256_si128(b), 1); } template inline batch merge_high(batch const &a, batch const &b, kernel::requires_arch) noexcept { return _mm256_permute2f128_ps(a, b, 0x31); } template = 0> inline batch merge_high(batch const &a, batch const &b, kernel::requires_arch) noexcept { return _mm256_permute2f128_si256(a, b, 0x31); } template inline batch duplicate_low_halves(batch const &a, batch const &b, kernel::requires_arch) noexcept { return _mm256_shuffle_ps(a, b, _MM_SHUFFLE(2, 0, 2, 0)); } template = 0> inline batch duplicate_low_halves(batch const &a, batch const &b, kernel::requires_arch) noexcept { return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(2, 0, 2, 0))); } template inline batch duplicate_high_halves(batch const &a, batch const &b, kernel::requires_arch) noexcept { return _mm256_shuffle_ps(a, b, _MM_SHUFFLE(3, 1, 3, 1)); } template = 0> inline batch duplicate_high_halves(batch const &a, batch const &b, kernel::requires_arch) noexcept { return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(3, 1, 3, 1))); } #endif template struct KoRgbaInterleavers; template<> struct KoRgbaInterleavers<16> { template = 0, enable_sized_vector_t = 0> static inline void interleave(void *dst, batch const &a, batch const &b, kernel::requires_arch) { auto *dstPtr = static_cast(dst); using U = std::conditional_t; const auto t1 = zip_lo(a, b); const auto t2 = zip_hi(a, b); t1.store(dstPtr, U{}); t2.store(dstPtr + batch::size, U{}); } // The AVX versions are handmade ports of the ones generated // by Clang 14.0.0: https://godbolt.org/z/Ts8MWosW3 // Except for interleave(avx) which comes from GCC 11.2 #if XSIMD_WITH_AVX template = 0> static inline void interleave(void *dst, batch const &a, batch const &b, kernel::requires_arch) { auto *dstPtr = static_cast(dst); using U = std::conditional_t; const auto t1 = zip_lo(a, b); const auto t2 = zip_hi(a, b); const auto src1 = merge_low(t1, t2, A{}); const auto src2 = merge_high(t1, t2, A{}); src1.store(dstPtr, U{}); src2.store(dstPtr + batch::size, U{}); } #endif template static inline void interleave(void *dst, batch const &a, batch const &b) { return interleave(dst, a, b, A{}); } template = 0, enable_sized_vector_t = 0> static inline void deinterleave(const void *src, batch &dst1, batch &dst2, kernel::requires_arch) { const auto *srcPtr = static_cast(src); using U = std::conditional_t; const auto a = batch::load(srcPtr, U{}); const auto b = batch::load(srcPtr + batch::size, U{}); const auto t1 = zip_lo(a, b); const auto t2 = zip_hi(a, b); dst1 = zip_lo(t1, t2); dst2 = zip_hi(t1, t2); } #if XSIMD_WITH_AVX2 template = 0> static inline void deinterleave(const void *src, batch &a, batch &b, kernel::requires_arch) { const auto *srcPtr = static_cast(src); using U = std::conditional_t; const auto src1 = batch::load(srcPtr, U{}); const auto src2 = batch::load(srcPtr + batch::size, U{}); const auto t1 = duplicate_low_halves(src1, src2, A{}); a = exchange_mid_halves(t1, A{}); const auto t2 = duplicate_high_halves(src1, src2, A{}); b = exchange_mid_halves(t2, A{}); } #endif #if XSIMD_WITH_AVX template = 0> static inline void deinterleave(const void *src, batch &a, batch &b, kernel::requires_arch) { const auto *srcPtr = static_cast(src); using U = std::conditional_t; const auto src1 = batch::load(srcPtr, U{}); const auto src2 = batch::load(srcPtr + batch::size, U{}); const auto t1 = merge_high(src1, src2, A{}); const auto t2 = merge_low(src1, src2, A{}); a.data = duplicate_low_halves(t2, t1, A{}); b.data = duplicate_high_halves(t2, t1, A{}); } #endif template static inline void deinterleave(const void *src, batch &a, batch &b) { return deinterleave(src, a, b, A{}); } }; template<> struct KoRgbaInterleavers<32> { template = 0, enable_sized_vector_t = 0> static inline void interleave(void *dst, batch const &a, batch const &b, batch const &c, batch const &d, kernel::requires_arch) { auto *dstPtr = static_cast(dst); using U = std::conditional_t; const auto t1 = zip_lo(a, c); const auto t2 = zip_hi(a, c); const auto t3 = zip_lo(b, d); const auto t4 = zip_hi(b, d); const auto src1 = zip_lo(t1, t3); const auto src2 = zip_hi(t1, t3); const auto src3 = zip_lo(t2, t4); const auto src4 = zip_hi(t2, t4); src1.store(dstPtr, U{}); src2.store(dstPtr + batch::size, U{}); src3.store(dstPtr + batch::size * 2, U{}); src4.store(dstPtr + batch::size * 3, U{}); } #if XSIMD_WITH_AVX template = 0> static inline void interleave(void *dst, batch const &a, batch const &b, batch const &c, batch const &d, kernel::requires_arch) { auto *dstPtr = static_cast(dst); using U = std::conditional_t; const auto t1 = zip_lo(a, c); const auto t2 = zip_lo(b, d); const auto t3 = zip_hi(a, c); const auto t4 = zip_hi(b, d); const auto t5 = zip_lo(t1, t2); const auto t6 = zip_hi(t1, t2); const auto t7 = zip_lo(t3, t4); const auto t8 = zip_hi(t3, t4); const auto src1 = merge_low(t5, t6, A{}); const auto src2 = merge_low(t7, t8, A{}); const auto src3 = merge_high(t5, t6, A{}); const auto src4 = merge_high(t7, t8, A{}); src1.store(dstPtr, U{}); src2.store(dstPtr + batch::size, U{}); src3.store(dstPtr + batch::size * 2, U{}); src4.store(dstPtr + batch::size * 3, U{}); } #endif template static inline void interleave(void *dst, batch const &a, batch const &b, batch const &c, batch const &d) { return interleave(dst, a, b, c, d, A{}); } template = 0, enable_sized_vector_t = 0> static inline void deinterleave(const void *src, batch &a, batch &b, batch &c, batch &d, kernel::requires_arch) { const auto *srcPtr = static_cast(src); using U = std::conditional_t; const auto t1 = batch::load(srcPtr, U{}); const auto t2 = batch::load(srcPtr + batch::size, U{}); const auto t3 = batch::load(srcPtr + batch::size * 2, U{}); const auto t4 = batch::load(srcPtr + batch::size * 3, U{}); const auto src1 = zip_lo(t1, t3); const auto src2 = zip_hi(t1, t3); const auto src3 = zip_lo(t2, t4); const auto src4 = zip_hi(t2, t4); a = zip_lo(src1, src3); b = zip_hi(src1, src3); c = zip_lo(src2, src4); d = zip_hi(src2, src4); } #if XSIMD_WITH_AVX template = 0> static inline void deinterleave(const void *src, batch &a, batch &b, batch &c, batch &d, kernel::requires_arch) { const auto *srcPtr = static_cast(src); using U = std::conditional_t; const auto a0b0c0d0_a1b1c1d1 = batch::load(srcPtr, U{}); const auto a2b2c2d2_a3b3c3d3 = batch::load(srcPtr + batch::size, U{}); const auto a4b4c4d4_a5b5c5d5 = batch::load(srcPtr + batch::size * 2, U{}); const auto a6b6c6d6_a7b7c7d7 = batch::load(srcPtr + batch::size * 3, U{}); const auto a0a2b0b2_a1a3b1b3 = zip_lo(a0b0c0d0_a1b1c1d1, a2b2c2d2_a3b3c3d3); const auto c0c2d0d2_c1c3d1d3 = zip_hi(a0b0c0d0_a1b1c1d1, a2b2c2d2_a3b3c3d3); const auto a0a2b0b2_c0c2d0d2 = merge_low(a0a2b0b2_a1a3b1b3, c0c2d0d2_c1c3d1d3, A{}); const auto a1a3b1b3_c1c3d1d3 = merge_high(a0a2b0b2_a1a3b1b3, c0c2d0d2_c1c3d1d3, A{}); const auto a0a1a2a3_c0c1c2c3 = zip_lo(a0a2b0b2_c0c2d0d2, a1a3b1b3_c1c3d1d3); const auto b0b1b2b3_d0d1d2d3 = zip_hi(a0a2b0b2_c0c2d0d2, a1a3b1b3_c1c3d1d3); const auto a4a6b4b6_a5a7b5b7 = zip_lo(a4b4c4d4_a5b5c5d5, a6b6c6d6_a7b7c7d7); const auto c4c6d4d6_c5c7d5d7 = zip_hi(a4b4c4d4_a5b5c5d5, a6b6c6d6_a7b7c7d7); const auto a4a6b4b6_c4c6d4d6 = merge_low(a4a6b4b6_a5a7b5b7, c4c6d4d6_c5c7d5d7, A{}); const auto a5a7b5b7_c5c7d5d7 = merge_high(a4a6b4b6_a5a7b5b7, c4c6d4d6_c5c7d5d7, A{}); const auto a4a5a6a7_c4c5c6c7 = zip_lo(a4a6b4b6_c4c6d4d6, a5a7b5b7_c5c7d5d7); const auto b4b5b6b7_d4d5d6d7 = zip_hi(a4a6b4b6_c4c6d4d6, a5a7b5b7_c5c7d5d7); a = merge_low(a0a1a2a3_c0c1c2c3, a4a5a6a7_c4c5c6c7, A{}); b = merge_low(b0b1b2b3_d0d1d2d3, b4b5b6b7_d4d5d6d7, A{}); c = merge_high(a0a1a2a3_c0c1c2c3, a4a5a6a7_c4c5c6c7, A{}); d = merge_high(b0b1b2b3_d0d1d2d3, b4b5b6b7_d4d5d6d7, A{}); } #endif template static inline void deinterleave(const void *src, batch &a, batch &b, batch &c, batch &d) { return deinterleave(src, a, b, c, d, A{}); } }; #endif // KO_RGBA_INTERLEAVERS