| // Copyright 2022 Google LLC |
| // |
| // This source code is licensed under the BSD-style license found in the |
| // LICENSE file in the root directory of this source tree. |
| |
| $assert BATCH_TILE >= 16 |
| $assert BATCH_TILE == 16 or BATCH_TILE % 32 == 0 |
| $SIMD_TILE = BATCH_TILE // 32 |
| $ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| #include <assert.h> |
| |
| #include <immintrin.h> |
| |
| #include <xnnpack/common.h> |
| #include <xnnpack/intrinsics-polyfill.h> |
| #include <xnnpack/vcvt.h> |
| |
| |
| $XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE] |
| $_MM256_CVTEPX8_EPI16 = {"QS8": "_mm256_cvtepi8_epi16", "QU8": "_mm256_cvtepu8_epi16"}[DATATYPE] |
| $_MM256_PACKXS_EPI16 = {"QS8": "_mm256_packs_epi16", "QU8": "_mm256_packus_epi16"}[DATATYPE] |
| $_MM_PACKXS_EPI16 = {"QS8": "_mm_packs_epi16", "QU8": "_mm_packus_epi16"}[DATATYPE] |
| void xnn_${DATATYPE.lower()}_vcvt_ukernel__avx2_x${BATCH_TILE}( |
| size_t n, |
| const ${XINT8_T}* x, |
| ${XINT8_T}* y, |
| const union xnn_${DATATYPE.lower()}_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS |
| { |
| assert(n != 0); |
| assert(n % sizeof(${XINT8_T}) == 0); |
| assert(x != NULL); |
| assert(y != NULL); |
| |
| const __m256i vinput_zero_point = _mm256_load_si256((const __m256i*) params->avx2.input_zero_point); |
| const __m256i vmultiplier = _mm256_load_si256((const __m256i*) params->avx2.multiplier); |
| const __m256i voutput_zero_point = _mm256_load_si256((const __m256i*) params->avx2.output_zero_point); |
| $if BATCH_TILE > 16: |
| for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) { |
| __m256i vacc${ABC[0]} = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) x)); |
| $for N in range(1, 2*SIMD_TILE): |
| __m256i vacc${ABC[N]} = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) (x + ${N * 16}))); |
| x += ${BATCH_TILE}; |
| |
| $for N in range(2*SIMD_TILE): |
| vacc${ABC[N]} = _mm256_sub_epi16(vinput_zero_point, vacc${ABC[N]}); |
| |
| $for N in range(2*SIMD_TILE): |
| vacc${ABC[N]} = _mm256_slli_epi16(vacc${ABC[N]}, 7); |
| |
| $for N in range(2*SIMD_TILE): |
| vacc${ABC[N]} = _mm256_mulhrs_epi16(vacc${ABC[N]}, vmultiplier); |
| |
| $for N in range(2*SIMD_TILE): |
| vacc${ABC[N]} = _mm256_adds_epi16(vacc${ABC[N]}, voutput_zero_point); |
| |
| $for N in range(SIMD_TILE): |
| __m256i vy${ABC[N]} = ${_MM256_PACKXS_EPI16}(vacc${ABC[2*N]}, vacc${ABC[2*N+1]}); |
| |
| $for N in range(SIMD_TILE): |
| vy${ABC[N]} = _mm256_permute4x64_epi64(vy${ABC[N]}, _MM_SHUFFLE(3, 1, 2, 0)); |
| |
| _mm256_storeu_si256((__m256i*) y, vy${ABC[0]}); |
| $for N in range(1, SIMD_TILE): |
| _mm256_storeu_si256((__m256i*) (y + ${N * 32}), vy${ABC[N]}); |
| y += ${BATCH_TILE}; |
| } |
| for (; n >= 16 * sizeof(${XINT8_T}); n -= 16 * sizeof(${XINT8_T})) { |
| __m256i vacc = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) x)); |
| vacc = _mm256_sub_epi16(vinput_zero_point, vacc); |
| vacc = _mm256_slli_epi16(vacc, 7); |
| vacc = _mm256_mulhrs_epi16(vacc, vmultiplier); |
| vacc = _mm256_adds_epi16(vacc, voutput_zero_point); |
| x += 16; |
| |
| const __m128i vacc_hi = _mm256_extracti128_si256(vacc, 1); |
| const __m128i vy = ${_MM_PACKXS_EPI16}(_mm256_castsi256_si128(vacc), vacc_hi); |
| _mm_storeu_si128((__m128i*) y, vy); |
| y += 16; |
| } |
| if XNN_UNLIKELY(n != 0) { |
| assert(n >= 1 * sizeof(${XINT8_T})); |
| assert(n <= 15 * sizeof(${XINT8_T})); |
| |
| __m256i vacc = ${_MM256_CVTEPX8_EPI16}(_mm_loadu_si128((const __m128i*) x)); |
| vacc = _mm256_sub_epi16(vinput_zero_point, vacc); |
| vacc = _mm256_slli_epi16(vacc, 7); |
| vacc = _mm256_mulhrs_epi16(vacc, vmultiplier); |
| vacc = _mm256_adds_epi16(vacc, voutput_zero_point); |
| |
| const __m128i vacc_hi = _mm256_extracti128_si256(vacc, 1); |
| __m128i vy = ${_MM_PACKXS_EPI16}(_mm256_castsi256_si128(vacc), vacc_hi); |
| if (n & (8 * sizeof(${XINT8_T}))) { |
| _mm_storel_epi64((__m128i*) y, vy); |
| vy = _mm_unpackhi_epi64(vy, vy); |
| y += 8; |
| } |
| if (n & (4 * sizeof(${XINT8_T}))) { |
| _mm_storeu_si32(y, vy); |
| vy = _mm_srli_epi64(vy, 32); |
| y += 4; |
| } |
| if (n & (2 * sizeof(${XINT8_T}))) { |
| _mm_storeu_si16(y, vy); |
| vy = _mm_srli_epi32(vy, 16); |
| y += 2; |
| } |
| if (n & (1 * sizeof(${XINT8_T}))) { |
| *y = (${XINT8_T}) _mm_extract_epi8(vy, 0); |
| } |
| } |
| } |