blob: 44dc0ada6fc956654d97c9bf4c73ee6aa9700aac [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 DATATYPE in ["QS8", "QU8"]
$assert BATCH_TILE >= 1
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include <assert.h>
#include <xnnpack/math.h>
#include <xnnpack/vlrelu.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()}_vlrelu_ukernel__scalar_select_x${BATCH_TILE}(
size_t n,
const ${XINT8_T}* x,
${XINT8_T}* y,
const union xnn_${DATATYPE.lower()}_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)])
{
const int32_t vinput_zero_point = params->scalar_select.input_zero_point;
const int32_t vpositive_multiplier = params->scalar_select.positive_multiplier;
const int32_t vnegative_multiplier = params->scalar_select.negative_multiplier;
const int32_t vbias = params->scalar_select.bias;
$if BATCH_TILE == 1:
do {
int32_t vacc = (int32_t) *x++ - vinput_zero_point;
const int32_t vmultiplier = XNN_UNPREDICTABLE(vacc >= 0) ? vpositive_multiplier : vnegative_multiplier;
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]} = (int32_t) x[${N}];
x += ${BATCH_TILE};
$for N in range(BATCH_TILE):
vacc${ABC[N]} -= vinput_zero_point;
$for N in range(BATCH_TILE):
const int32_t vmultiplier${ABC[N]} = XNN_UNPREDICTABLE(vacc${ABC[N]} >= 0) ? vpositive_multiplier : vnegative_multiplier;
$for N in range(BATCH_TILE):
vacc${ABC[N]} = vbias + vacc${ABC[N]} * vmultiplier${ABC[N]};
$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 = (int32_t) *x++ - vinput_zero_point;
const int32_t vmultiplier = XNN_UNPREDICTABLE(vacc >= 0) ? vpositive_multiplier : vnegative_multiplier;
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 = (int32_t) *x++ - vinput_zero_point;
const int32_t vmultiplier = XNN_UNPREDICTABLE(vacc >= 0) ? vpositive_multiplier : vnegative_multiplier;
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);
}
}