blob: f6816ed3d2b1adbdb014a7accf4b9d0d192cfc7a [file] [log] [blame] [edit]
// 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 BATCH_TILE >= 1
#include <assert.h>
#include <xnnpack/common.h>
#include <xnnpack/math.h>
#include <xnnpack/vcvt.h>
$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
void xnn_f32_${DATATYPE.lower()}_vcvt_ukernel__scalar_imagic_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 float vscale = params->scalar_imagic.scale;
const float vmagic_bias = params->scalar_imagic.magic_bias;
const int32_t vmagic_min = params->scalar_imagic.magic_min;
const int32_t vmagic_max = params->scalar_imagic.magic_max;
const int32_t vmagic_bias_less_zero_point = params->scalar_imagic.magic_bias_less_zero_point;
$if BATCH_TILE > 1:
for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
$for N in range(BATCH_TILE):
float vx${N} = x[${N}];
x += ${BATCH_TILE};
$for N in range(BATCH_TILE):
vx${N} *= vscale;
$for N in range(BATCH_TILE):
vx${N} += vmagic_bias;
$for N in range(BATCH_TILE):
int32_t vy${N} = (int32_t) float_as_uint32(vx${N});
$for N in range(BATCH_TILE):
vy${N} = math_max_s32(vy${N}, vmagic_min);
$for N in range(BATCH_TILE):
vy${N} = math_min_s32(vy${N}, vmagic_max);
$for N in range(BATCH_TILE):
vy${N} -= vmagic_bias_less_zero_point;
$for N in range(BATCH_TILE):
y[${N}] = (${XINT8_T}) vy${N};
y += ${BATCH_TILE};
}
$if BATCH_TILE == 1:
do {
float vx = *x++;
vx *= vscale;
vx += vmagic_bias;
int32_t vy = (int32_t) float_as_uint32(vx);
vy = math_max_s32(vy, vmagic_min);
vy = math_min_s32(vy, vmagic_max);
vy -= vmagic_bias_less_zero_point;
*y++ = (${XINT8_T}) vy;
n -= sizeof(float);
} while (n != 0);
$elif BATCH_TILE == 2:
if XNN_UNLIKELY(n != 0) {
float vx = *x;
vx *= vscale;
vx += vmagic_bias;
int32_t vy = (int32_t) float_as_uint32(vx);
vy = math_max_s32(vy, vmagic_min);
vy = math_min_s32(vy, vmagic_max);
vy -= vmagic_bias_less_zero_point;
*y = (${XINT8_T}) vy;
}
$else:
if XNN_UNLIKELY(n != 0) {
do {
float vx = *x++;
vx *= vscale;
vx += vmagic_bias;
int32_t vy = (int32_t) float_as_uint32(vx);
vy = math_max_s32(vy, vmagic_min);
vy = math_min_s32(vy, vmagic_max);
vy -= vmagic_bias_less_zero_point;
*y++ = (${XINT8_T}) vy;
n -= sizeof(float);
} while (n != 0);
}
}