blob: 7a170155682a47b54f12f974da9ab55b6ecd8644 [file] [log] [blame]
// Copyright 2020 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 % 4 == 0
$assert BATCH_TILE >= 4
$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$assert OP in ["ABS", "NEG", "SQR"]
#include <assert.h>
#include <wasm_simd128.h>
#include <xnnpack/common.h>
#include <xnnpack/math.h>
#include <xnnpack/vunary.h>
$WASM_F32X4_OP = {
$ "ABS": lambda x: "wasm_v128_and(%s, vnonsign_mask)" % x,
$ "NEG": lambda x: "wasm_v128_xor(%s, vsign_mask)" % x,
$ "SQR": lambda x: "wasm_f32x4_mul(%s, %s)" % (x, x),
$}[OP]
$PARAMS = {
$ "ABS": "const union xnn_f32_abs_params params[restrict XNN_MIN_ELEMENTS(1)]",
$ "NEG": "const union xnn_f32_neg_params params[restrict XNN_MIN_ELEMENTS(1)]",
$ "SQR": "const void* params",
$}[OP]
void xnn_f32_v${OP.lower()}_ukernel__wasmsimd_x${BATCH_TILE}(
size_t n,
const float* x,
float* y,
${PARAMS}) XNN_DISABLE_TSAN
{
assert(n != 0);
assert(n % sizeof(float) == 0);
assert(x != NULL);
assert(y != NULL);
$if OP == "ABS":
const v128_t vnonsign_mask = wasm_v32x4_load_splat(&params->wasmsimd.nonsign_mask);
$elif OP == "NEG":
const v128_t vsign_mask = wasm_v32x4_load_splat(&params->wasmsimd.sign_mask);
$if BATCH_TILE > 4:
for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
const v128_t vx${ABC[0:4]} = wasm_v128_load(x);
$for N in range(4, BATCH_TILE, 4):
const v128_t vx${ABC[N:N+4]} = wasm_v128_load(x + ${N});
x += ${BATCH_TILE};
$for N in range(0, BATCH_TILE, 4):
const v128_t vy${ABC[N:N+4]} = ${WASM_F32X4_OP("vx" + ABC[N:N+4])};
wasm_v128_store(y, vy${ABC[0:4]});
$for N in range(4, BATCH_TILE, 4):
wasm_v128_store(y + ${N}, vy${ABC[N:N+4]});
y += ${BATCH_TILE};
}
for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
const v128_t vx = wasm_v128_load(x);
x += 4;
const v128_t vy = ${WASM_F32X4_OP("vx")};
wasm_v128_store(y, vy);
y += 4;
}
if XNN_UNLIKELY(n != 0) {
const v128_t vx = wasm_v128_load(x);
v128_t vy = ${WASM_F32X4_OP("vx")};
if (n & (2 * sizeof(float))) {
*((double*) y) = wasm_f64x2_extract_lane(vy, 0);
vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3);
y += 2;
}
if (n & (1 * sizeof(float))) {
*y = wasm_f32x4_extract_lane(vy, 0);
}
}
}