| // Copyright 2021 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 DATATYPE in ["QS8", "QU8"] |
| $assert BATCH_TILE % 8 == 0 |
| $assert BATCH_TILE >= 8 |
| $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] |
| $_MM_PACKXS_EPI16 = {"QS8": "_mm_packs_epi16", "QU8": "_mm_packus_epi16"}[DATATYPE] |
| $_MM_MAX_EPX8 = {"QS8": "_mm_max_epi8", "QU8": "_mm_max_epu8"}[DATATYPE] |
| void xnn_f32_${DATATYPE.lower()}_vcvt_ukernel__avx_x${BATCH_TILE}( |
| size_t n, |
| const float* x, |
| ${XINT8_T}* y, |
| const union xnn_f32_${DATATYPE.lower()}_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) |
| { |
| assert(n != 0); |
| assert(n % sizeof(float) == 0); |
| assert(x != NULL); |
| assert(y != NULL); |
| |
| const __m256 vscale = _mm256_load_ps(params->avx.scale); |
| const __m256 voutput_max_less_zero_point = _mm256_load_ps(params->avx.output_max_less_zero_point); |
| const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->avx.output_zero_point); |
| const __m128i voutput_min = _mm_load_si128((const __m128i*) params->avx.output_min); |
| |
| $if BATCH_TILE > 8: |
| for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) { |
| __m256 vx${ABC[0:8]} = _mm256_loadu_ps(x); |
| $for N in range(8, BATCH_TILE, 8): |
| __m256 vx${ABC[N:N+8]} = _mm256_loadu_ps(x + ${N}); |
| x += ${BATCH_TILE}; |
| |
| $for N in range(0, BATCH_TILE, 8): |
| vx${ABC[N:N+8]} = _mm256_mul_ps(vx${ABC[N:N+8]}, vscale); |
| |
| $for N in range(0, BATCH_TILE, 8): |
| vx${ABC[N:N+8]} = _mm256_min_ps(vx${ABC[N:N+8]}, voutput_max_less_zero_point); |
| |
| $for N in range(0, BATCH_TILE, 8): |
| const __m256i vacc${ABC[N:N+8]} = _mm256_cvtps_epi32(vx${ABC[N:N+8]}); |
| |
| $for N in range(0, BATCH_TILE, 8): |
| __m128i vy${ABC[N:N+8]} = _mm_packs_epi32(_mm256_castsi256_si128(vacc${ABC[N:N+8]}), _mm256_extractf128_si256(vacc${ABC[N:N+8]}, 1)); |
| |
| $for N in range(0, BATCH_TILE, 8): |
| vy${ABC[N:N+8]} = _mm_adds_epi16(vy${ABC[N:N+8]}, voutput_zero_point); |
| |
| $for N in range(0, BATCH_TILE, 16): |
| $if N + 8 < BATCH_TILE: |
| __m128i vy${ABC[N:N+16]} = ${_MM_PACKXS_EPI16}(vy${ABC[N:N+8]}, vy${ABC[N+8:N+16]}); |
| $else: |
| vy${ABC[N:N+8]} = ${_MM_PACKXS_EPI16}(vy${ABC[N:N+8]}, vy${ABC[N:N+8]}); |
| |
| $for N in range(0, BATCH_TILE, 16): |
| $if N + 8 < BATCH_TILE: |
| vy${ABC[N:N+16]} = ${_MM_MAX_EPX8}(vy${ABC[N:N+16]}, voutput_min); |
| $else: |
| vy${ABC[N:N+8]} = ${_MM_MAX_EPX8}(vy${ABC[N:N+8]}, voutput_min); |
| |
| _mm_storeu_si128((__m128i*) y, vy${ABC[0:16]}); |
| $for N in range(16, BATCH_TILE, 16): |
| $if N + 8 < BATCH_TILE: |
| _mm_storeu_si128((__m128i*) (y + ${N}), vy${ABC[N:N+16]}); |
| $else: |
| _mm_storel_epi64((__m128i*) (y + ${N}), vy${ABC[N:N+8]}); |
| y += ${BATCH_TILE}; |
| } |
| for (; n >= 8 * sizeof(float); n -= 8 * sizeof(float)) { |
| __m256 vx = _mm256_loadu_ps(x); |
| vx = _mm256_mul_ps(vx, vscale); |
| vx = _mm256_min_ps(vx, voutput_max_less_zero_point); |
| x += 8; |
| |
| const __m256i vacc = _mm256_cvtps_epi32(vx); |
| |
| __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extractf128_si256(vacc, 1)); |
| vy = _mm_adds_epi16(vy, voutput_zero_point); |
| vy = ${_MM_PACKXS_EPI16}(vy, vy); |
| vy = ${_MM_MAX_EPX8}(vy, voutput_min); |
| |
| _mm_storel_epi64((__m128i*) y, vy); |
| y += 8; |
| } |
| if XNN_UNLIKELY(n != 0) { |
| assert(n >= 1 * sizeof(float)); |
| assert(n <= 7 * sizeof(float)); |
| const __m256i vmask = _mm256_loadu_si256((const __m256i*) ((uintptr_t) ¶ms->avx.mask_table[7] - n)); |
| |
| __m256 vx = _mm256_maskload_ps(x, vmask); |
| vx = _mm256_mul_ps(vx, vscale); |
| vx = _mm256_min_ps(vx, voutput_max_less_zero_point); |
| |
| const __m256i vacc = _mm256_cvtps_epi32(vx); |
| |
| __m128i vy = _mm_packs_epi32(_mm256_castsi256_si128(vacc), _mm256_extractf128_si256(vacc, 1)); |
| vy = _mm_adds_epi16(vy, voutput_zero_point); |
| vy = ${_MM_PACKXS_EPI16}(vy, vy); |
| vy = ${_MM_MAX_EPX8}(vy, voutput_min); |
| |
| if (n & (4 * sizeof(float))) { |
| _mm_storeu_si32(y, vy); |
| y += 4; |
| vy = _mm_srli_epi64(vy, 32); |
| } |
| if (n & (2 * sizeof(float))) { |
| _mm_storeu_si16(y, vy); |
| y += 2; |
| vy = _mm_srli_epi32(vy, 16); |
| } |
| if (n & (1 * sizeof(float))) { |
| *y = (${XINT8_T}) _mm_extract_epi8(vy, 0); |
| } |
| } |
| } |