1// Copyright 2022 Google LLC 2// 3// This source code is licensed under the BSD-style license found in the 4// LICENSE file in the root directory of this source tree. 5 6$assert BATCH_TILE >= 8 7$assert BATCH_TILE == 8 or BATCH_TILE % 16 == 0 8$SIMD_TILE = BATCH_TILE // 16 9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 10#include <assert.h> 11 12#include <arm_neon.h> 13 14#include <xnnpack/common.h> 15#include <xnnpack/vcvt.h> 16 17 18$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE] 19$XINT8X8_T = {"QS8": "int8x8_t", "QU8": "uint8x8_t"}[DATATYPE] 20$XINT8X16_T = {"QS8": "int8x16_t", "QU8": "uint8x16_t"}[DATATYPE] 21$XINT16X8_T = {"QS8": "int16x8_t", "QU8": "uint16x8_t"}[DATATYPE] 22$VLD1_X8 = {"QS8": "vld1_s8", "QU8": "vld1_u8"}[DATATYPE] 23$VLD1Q_X8 = {"QS8": "vld1q_s8", "QU8": "vld1q_u8"}[DATATYPE] 24$VLD1Q_DUP_X16 = {"QS8": "vld1q_dup_s16", "QU8": "vld1q_dup_u16"}[DATATYPE] 25$VQMOVXN_S16 = {"QS8": "vqmovn_s16", "QU8": "vqmovun_s16"}[DATATYPE] 26$VGET_LOW_X8 = {"QS8": "vget_low_s8", "QU8": "vget_low_u8"}[DATATYPE] 27$VGET_HIGH_X8 = {"QS8": "vget_high_s8", "QU8": "vget_high_u8"}[DATATYPE] 28$VCOMBINE_X8 = {"QS8": "vcombine_s8", "QU8": "vcombine_u8"}[DATATYPE] 29$VREINTERPRET_U32_X8 = {"QS8": "vreinterpret_u32_s8", "QU8": "vreinterpret_u32_u8"}[DATATYPE] 30$VREINTERPRET_U16_X8 = {"QS8": "vreinterpret_u16_s8", "QU8": "vreinterpret_u16_u8"}[DATATYPE] 31$VEXT_X8 = {"QS8": "vext_s8", "QU8": "vext_u8"}[DATATYPE] 32$VST1_X8 = {"QS8": "vst1_s8", "QU8": "vst1_u8"}[DATATYPE] 33$VST1Q_X8 = {"QS8": "vst1q_s8", "QU8": "vst1q_u8"}[DATATYPE] 34$VST1_LANE_X8 = {"QS8": "vst1_lane_s8", "QU8": "vst1_lane_u8"}[DATATYPE] 35void xnn_${DATATYPE.lower()}_vcvt_ukernel__neon_x${BATCH_TILE}( 36 size_t n, 37 const ${XINT8_T}* x, 38 ${XINT8_T}* y, 39 const union xnn_${DATATYPE.lower()}_cvt_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS 40{ 41 assert(n != 0); 42 assert(n % sizeof(${XINT8_T}) == 0); 43 assert(x != NULL); 44 assert(y != NULL); 45 46 const ${XINT16X8_T} vinput_zero_point = ${VLD1Q_DUP_X16}(¶ms->neon.input_zero_point); 47 const int16x8_t vmultiplier = vld1q_dup_s16(¶ms->neon.multiplier); 48 const int16x8_t voutput_zero_point = vld1q_dup_s16(¶ms->neon.output_zero_point); 49 $if BATCH_TILE > 8: 50 for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) { 51 $for N in range(SIMD_TILE): 52 const ${XINT8X16_T} vx${ABC[N]} = ${VLD1Q_X8}(x); x += 16; 53 54 $for N in range(SIMD_TILE): 55 $if DATATYPE == "QU8": 56 int16x8_t vacc${ABC[2*N]} = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, ${VGET_LOW_X8}(vx${ABC[N]}))); 57 int16x8_t vacc${ABC[2*N+1]} = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, ${VGET_HIGH_X8}(vx${ABC[N]}))); 58 $else: 59 int16x8_t vacc${ABC[2*N]} = vsubw_s8(vinput_zero_point, ${VGET_LOW_X8}(vx${ABC[N]})); 60 int16x8_t vacc${ABC[2*N+1]} = vsubw_s8(vinput_zero_point, ${VGET_HIGH_X8}(vx${ABC[N]})); 61 62 $for N in range(2*SIMD_TILE): 63 vacc${ABC[N]} = vshlq_n_s16(vacc${ABC[N]}, 7); 64 65 $for N in range(2*SIMD_TILE): 66 vacc${ABC[N]} = vqrdmulhq_s16(vacc${ABC[N]}, vmultiplier); 67 68 $for N in range(2*SIMD_TILE): 69 vacc${ABC[N]} = vqaddq_s16(vacc${ABC[N]}, voutput_zero_point); 70 71 $for N in range(SIMD_TILE): 72 const ${XINT8X16_T} vy${ABC[N]} = ${VCOMBINE_X8}(${VQMOVXN_S16}(vacc${ABC[2*N]}), ${VQMOVXN_S16}(vacc${ABC[2*N+1]})); 73 74 $for N in range(SIMD_TILE): 75 ${VST1Q_X8}(y, vy${ABC[N]}); y += 16; 76 } 77 for (; n >= 8 * sizeof(${XINT8_T}); n -= 8 * sizeof(${XINT8_T})) { 78 const ${XINT8X8_T} vx = ${VLD1_X8}(x); x += 8; 79 $if DATATYPE == "QU8": 80 int16x8_t vacc = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vx)); 81 $else: 82 int16x8_t vacc = vsubw_s8(vinput_zero_point, vx); 83 vacc = vshlq_n_s16(vacc, 7); 84 vacc = vqrdmulhq_s16(vacc, vmultiplier); 85 vacc = vqaddq_s16(vacc, voutput_zero_point); 86 const ${XINT8X8_T} vy = ${VQMOVXN_S16}(vacc); 87 ${VST1_X8}(y, vy); y += 8; 88 } 89 if XNN_UNLIKELY(n != 0) { 90 assert(n >= 1 * sizeof(${XINT8_T})); 91 assert(n <= 7 * sizeof(${XINT8_T})); 92 93 const ${XINT8X8_T} vx = ${VLD1_X8}(x); 94 $if DATATYPE == "QU8": 95 int16x8_t vacc = vreinterpretq_s16_u16(vsubw_u8(vinput_zero_point, vx)); 96 $else: 97 int16x8_t vacc = vsubw_s8(vinput_zero_point, vx); 98 vacc = vshlq_n_s16(vacc, 7); 99 vacc = vqrdmulhq_s16(vacc, vmultiplier); 100 vacc = vqaddq_s16(vacc, voutput_zero_point); 101 ${XINT8X8_T} vy = ${VQMOVXN_S16}(vacc); 102 103 if (n & (4 * sizeof(${XINT8_T}))) { 104 vst1_lane_u32((void*) y, ${VREINTERPRET_U32_X8}(vy), 0); y += 4; 105 vy = ${VEXT_X8}(vy, vy, 4); 106 } 107 if (n & (2 * sizeof(${XINT8_T}))) { 108 vst1_lane_u16((void*) y, ${VREINTERPRET_U16_X8}(vy), 0); y += 2; 109 vy = ${VEXT_X8}(vy, vy, 2); 110 } 111 if (n & (1 * sizeof(${XINT8_T}))) { 112 ${VST1_LANE_X8}(y, vy, 0); 113 } 114 } 115} 116