blob: 95936e3cd2d8802056c35bf9e92c6d4ee24be27e [file] [log] [blame] [edit]
// 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 == 0
$SIMD_TILE = BATCH_TILE // 16
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>
#include <tmmintrin.h>
#include <xnnpack/common.h>
#include <xnnpack/vcvt.h>
#include <xnnpack/unaligned.h>
$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
$_MM_PACKXS_EPI16 = {"QS8": "_mm_packs_epi16", "QU8": "_mm_packus_epi16"}[DATATYPE]
$_MM_MULHI_EPX16 = {"QS8": "_mm_mulhi_epi16", "QU8": "_mm_mulhi_epu16"}[DATATYPE]
void xnn_${DATATYPE.lower()}_vcvt_ukernel__sse2_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 __m128i vmultiplier = _mm_load_si128((const __m128i*) params->sse2.multiplier);
const __m128i vbias = _mm_load_si128((const __m128i*) params->sse2.bias);
$if DATATYPE == "QU8":
const __m128i vzero = _mm_setzero_si128();
$if BATCH_TILE > 8:
for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
const __m128i vx${ABC[0]} = _mm_loadu_si128((const __m128i*) x);
$for N in range(1, SIMD_TILE):
const __m128i vx${ABC[N]} = _mm_loadu_si128((const __m128i*) (x + ${N * 16}));
x += ${BATCH_TILE};
$for N in range(SIMD_TILE):
$if DATATYPE == "QU8":
const __m128i vextx${ABC[2*N]} = _mm_unpacklo_epi8(vx${ABC[N]}, vzero);
const __m128i vextx${ABC[2*N+1]} = _mm_unpackhi_epi8(vx${ABC[N]}, vzero);
$else:
const __m128i vm${ABC[N]} = _mm_cmpgt_epi8(_mm_setzero_si128(), vx${ABC[N]});
const __m128i vextx${ABC[2*N]} = _mm_unpacklo_epi8(vx${ABC[N]}, vm${ABC[N]});
const __m128i vextx${ABC[2*N+1]} = _mm_unpackhi_epi8(vx${ABC[N]}, vm${ABC[N]});
$for N in range(2*SIMD_TILE):
const __m128i vprodlo${ABC[N]} = _mm_mullo_epi16(vextx${ABC[N]}, vmultiplier);
const __m128i vprodhi${ABC[N]} = ${_MM_MULHI_EPX16}(vextx${ABC[N]}, vmultiplier);
$for N in range(2*SIMD_TILE):
__m128i vacc${ABC[2*N]} = _mm_unpacklo_epi16(vprodlo${ABC[N]}, vprodhi${ABC[N]});
__m128i vacc${ABC[2*N+1]} = _mm_unpackhi_epi16(vprodlo${ABC[N]}, vprodhi${ABC[N]});
$for N in range(4*SIMD_TILE):
$if DATATYPE == "QU8":
vacc${ABC[N]} = _mm_add_epi32(vacc${ABC[N]}, vbias);
$else:
vacc${ABC[N]} = _mm_sub_epi32(vbias, vacc${ABC[N]});
$for N in range(4*SIMD_TILE):
vacc${ABC[N]} = _mm_srai_epi32(vacc${ABC[N]}, 8);
$for N in range(2*SIMD_TILE):
vacc${ABC[N]} = _mm_packs_epi32(vacc${ABC[2*N]}, vacc${ABC[2*N+1]});
$for N in range(SIMD_TILE):
const __m128i vy${ABC[N]} = ${_MM_PACKXS_EPI16}(vacc${ABC[2*N]}, vacc${ABC[2*N+1]});
_mm_storeu_si128((__m128i*) y, vy${ABC[0]});
$for N in range(1, SIMD_TILE):
_mm_storeu_si128((__m128i*) (y + ${N * 16}), vy${ABC[N]});
y += ${BATCH_TILE};
}
for (; n >= 16 * sizeof(${XINT8_T}); n -= 16 * sizeof(${XINT8_T})) {
const __m128i vx = _mm_loadu_si128((const __m128i*) x);
x += 16;
$if DATATYPE == "QU8":
const __m128i vextx_lo = _mm_unpacklo_epi8(vx, vzero);
const __m128i vextx_hi = _mm_unpackhi_epi8(vx, vzero);
$else:
const __m128i vm = _mm_cmpgt_epi8(_mm_setzero_si128(), vx);
const __m128i vextx_lo = _mm_unpacklo_epi8(vx, vm);
const __m128i vextx_hi = _mm_unpackhi_epi8(vx, vm);
const __m128i vprodlo_lo = _mm_mullo_epi16(vextx_lo, vmultiplier);
const __m128i vprodlo_hi = _mm_mullo_epi16(vextx_hi, vmultiplier);
const __m128i vprodhi_lo = ${_MM_MULHI_EPX16}(vextx_lo, vmultiplier);
const __m128i vprodhi_hi = ${_MM_MULHI_EPX16}(vextx_hi, vmultiplier);
__m128i vacc_ll = _mm_unpacklo_epi16(vprodlo_lo, vprodhi_lo);
__m128i vacc_lh = _mm_unpackhi_epi16(vprodlo_lo, vprodhi_lo);
__m128i vacc_hl = _mm_unpacklo_epi16(vprodlo_hi, vprodhi_hi);
__m128i vacc_hh = _mm_unpackhi_epi16(vprodlo_hi, vprodhi_hi);
$if DATATYPE == "QU8":
vacc_ll = _mm_add_epi32(vacc_ll, vbias);
vacc_lh = _mm_add_epi32(vacc_lh, vbias);
vacc_hl = _mm_add_epi32(vacc_hl, vbias);
vacc_hh = _mm_add_epi32(vacc_hh, vbias);
$else:
vacc_ll = _mm_sub_epi32(vbias, vacc_ll);
vacc_lh = _mm_sub_epi32(vbias, vacc_lh);
vacc_hl = _mm_sub_epi32(vbias, vacc_hl);
vacc_hh = _mm_sub_epi32(vbias, vacc_hh);
vacc_ll = _mm_srai_epi32(vacc_ll, 8);
vacc_lh = _mm_srai_epi32(vacc_lh, 8);
vacc_hl = _mm_srai_epi32(vacc_hl, 8);
vacc_hh = _mm_srai_epi32(vacc_hh, 8);
const __m128i vacc_lo = _mm_packs_epi32(vacc_ll, vacc_lh);
const __m128i vacc_hi = _mm_packs_epi32(vacc_hl, vacc_hh);
const __m128i vy = ${_MM_PACKXS_EPI16}(vacc_lo, 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}));
const __m128i vx = _mm_loadu_si128((const __m128i*) x);
$if DATATYPE == "QU8":
const __m128i vextx_lo = _mm_unpacklo_epi8(vx, vzero);
const __m128i vextx_hi = _mm_unpackhi_epi8(vx, vzero);
$else:
const __m128i vm = _mm_cmpgt_epi8(_mm_setzero_si128(), vx);
const __m128i vextx_lo = _mm_unpacklo_epi8(vx, vm);
const __m128i vextx_hi = _mm_unpackhi_epi8(vx, vm);
const __m128i vprodlo_lo = _mm_mullo_epi16(vextx_lo, vmultiplier);
const __m128i vprodlo_hi = _mm_mullo_epi16(vextx_hi, vmultiplier);
const __m128i vprodhi_lo = ${_MM_MULHI_EPX16}(vextx_lo, vmultiplier);
const __m128i vprodhi_hi = ${_MM_MULHI_EPX16}(vextx_hi, vmultiplier);
__m128i vacc_ll = _mm_unpacklo_epi16(vprodlo_lo, vprodhi_lo);
__m128i vacc_lh = _mm_unpackhi_epi16(vprodlo_lo, vprodhi_lo);
__m128i vacc_hl = _mm_unpacklo_epi16(vprodlo_hi, vprodhi_hi);
__m128i vacc_hh = _mm_unpackhi_epi16(vprodlo_hi, vprodhi_hi);
$if DATATYPE == "QU8":
vacc_ll = _mm_add_epi32(vacc_ll, vbias);
vacc_lh = _mm_add_epi32(vacc_lh, vbias);
vacc_hl = _mm_add_epi32(vacc_hl, vbias);
vacc_hh = _mm_add_epi32(vacc_hh, vbias);
$else:
vacc_ll = _mm_sub_epi32(vbias, vacc_ll);
vacc_lh = _mm_sub_epi32(vbias, vacc_lh);
vacc_hl = _mm_sub_epi32(vbias, vacc_hl);
vacc_hh = _mm_sub_epi32(vbias, vacc_hh);
vacc_ll = _mm_srai_epi32(vacc_ll, 8);
vacc_lh = _mm_srai_epi32(vacc_lh, 8);
vacc_hl = _mm_srai_epi32(vacc_hl, 8);
vacc_hh = _mm_srai_epi32(vacc_hh, 8);
const __m128i vacc_lo = _mm_packs_epi32(vacc_ll, vacc_lh);
const __m128i vacc_hi = _mm_packs_epi32(vacc_hl, vacc_hh);
__m128i vy = ${_MM_PACKXS_EPI16}(vacc_lo, 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}))) {
unaligned_store_u32(y, (uint32_t) _mm_cvtsi128_si32(vy));
vy = _mm_srli_epi64(vy, 32);
y += 4;
}
uint32_t vy_lo = (uint32_t) _mm_cvtsi128_si32(vy);
if (n & (2 * sizeof(${XINT8_T}))) {
unaligned_store_u16(y, (uint16_t) vy_lo);
vy_lo >>= 16;
y += 2;
}
if (n & (1 * sizeof(${XINT8_T}))) {
*y = (${XINT8_T}) vy_lo;
}
}
}