| // 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 DATATYPE in ["QS8", "QU8"] |
| $assert BATCH_TILE >= 1 |
| $ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| #include <assert.h> |
| |
| #include <xnnpack/math.h> |
| #include <xnnpack/vcvt.h> |
| |
| |
| $XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE] |
| $OUTPUT_MIN = {"QS8": -128, "QU8": 0}[DATATYPE] |
| $OUTPUT_MAX = {"QS8": 127, "QU8": 255}[DATATYPE] |
| void xnn_${DATATYPE.lower()}_vcvt_ukernel__scalar_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)]) |
| { |
| const int32_t vbias = params->scalar.bias; |
| const int32_t vmultiplier = params->scalar.multiplier; |
| $if BATCH_TILE == 1: |
| do { |
| int32_t vacc = *x++; |
| vacc = vbias + vacc * vmultiplier; |
| |
| int32_t vout = math_asr_s32(vacc, 8); |
| vout = math_max_s32(vout, ${OUTPUT_MIN}); |
| vout = math_min_s32(vout, ${OUTPUT_MAX}); |
| *y++ = (${XINT8_T}) vout; |
| |
| n -= sizeof(${XINT8_T}); |
| } while (n != 0); |
| $else: |
| for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) { |
| $for N in range(BATCH_TILE): |
| int32_t vacc${ABC[N]} = x[${N}]; |
| x += ${BATCH_TILE}; |
| |
| $for N in range(BATCH_TILE): |
| vacc${ABC[N]} = vbias + vacc${ABC[N]} * vmultiplier; |
| |
| $for N in range(BATCH_TILE): |
| int32_t vout${ABC[N]} = math_asr_s32(vacc${ABC[N]}, 8); |
| |
| $for N in range(BATCH_TILE): |
| vout${ABC[N]} = math_max_s32(vout${ABC[N]}, ${OUTPUT_MIN}); |
| |
| $for N in range(BATCH_TILE): |
| vout${ABC[N]} = math_min_s32(vout${ABC[N]}, ${OUTPUT_MAX}); |
| |
| $for N in range(BATCH_TILE): |
| y[${N}] = (${XINT8_T}) vout${ABC[N]}; |
| y += ${BATCH_TILE}; |
| } |
| if XNN_UNLIKELY(n != 0) { |
| $if BATCH_TILE == 2: |
| int32_t vacc = *x; |
| vacc = vbias + vacc * vmultiplier; |
| |
| int32_t vout = math_asr_s32(vacc, 8); |
| vout = math_max_s32(vout, ${OUTPUT_MIN}); |
| vout = math_min_s32(vout, ${OUTPUT_MAX}); |
| *y = (${XINT8_T}) vout; |
| $else: |
| do { |
| int32_t vacc = *x++; |
| vacc = vbias + vacc * vmultiplier; |
| |
| int32_t vout = math_asr_s32(vacc, 8); |
| vout = math_max_s32(vout, ${OUTPUT_MIN}); |
| vout = math_min_s32(vout, ${OUTPUT_MAX}); |
| *y++ = (${XINT8_T}) vout; |
| |
| n -= sizeof(${XINT8_T}); |
| } while (n != 0); |
| } |
| } |