• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2020 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 % 4 == 0
7$assert BATCH_TILE >= 4
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9$assert OP in ["ABS", "NEG", "SQR"]
10#include <assert.h>
11
12#include <wasm_simd128.h>
13
14#include <xnnpack/common.h>
15#include <xnnpack/math.h>
16#include <xnnpack/vunary.h>
17
18
19$WASM_F32X4_OP = {
20$  "ABS": lambda x: "wasm_v128_and(%s, vnonsign_mask)" % x,
21$  "NEG": lambda x: "wasm_v128_xor(%s, vsign_mask)" % x,
22$  "SQR": lambda x: "wasm_f32x4_mul(%s, %s)" % (x, x),
23$}[OP]
24$PARAMS = {
25$  "ABS": "xnn_f32_abs_params",
26$  "NEG": "xnn_f32_neg_params",
27$  "SQR": "xnn_f32_default_params",
28$}[OP]
29void xnn_f32_v${OP.lower()}_ukernel__wasmsimd_x${BATCH_TILE}(
30    size_t n,
31    const float* x,
32    float* y,
33    const union ${PARAMS} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
34{
35  assert(n != 0);
36  assert(n % sizeof(float) == 0);
37  assert(x != NULL);
38  assert(y != NULL);
39
40  $if OP == "ABS":
41    const v128_t vnonsign_mask = wasm_v128_load64_splat(&params->wasmsimd.nonsign_mask);
42  $elif OP == "NEG":
43    const v128_t vsign_mask = wasm_v128_load64_splat(&params->wasmsimd.sign_mask);
44  $if BATCH_TILE > 4:
45    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
46      const v128_t vx${ABC[0:4]} = wasm_v128_load(x);
47      $for N in range(4, BATCH_TILE, 4):
48        const v128_t vx${ABC[N:N+4]} = wasm_v128_load(x + ${N});
49      x += ${BATCH_TILE};
50
51      $for N in range(0, BATCH_TILE, 4):
52        const v128_t vy${ABC[N:N+4]} = ${WASM_F32X4_OP("vx" + ABC[N:N+4])};
53
54      wasm_v128_store(y, vy${ABC[0:4]});
55      $for N in range(4, BATCH_TILE, 4):
56        wasm_v128_store(y + ${N}, vy${ABC[N:N+4]});
57      y += ${BATCH_TILE};
58    }
59  for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
60    const v128_t vx = wasm_v128_load(x);
61    x += 4;
62
63    const v128_t vy = ${WASM_F32X4_OP("vx")};
64
65    wasm_v128_store(y, vy);
66    y += 4;
67  }
68  if XNN_UNLIKELY(n != 0) {
69    const v128_t vx = wasm_v128_load(x);
70    v128_t vy = ${WASM_F32X4_OP("vx")};
71    if (n & (2 * sizeof(float))) {
72      *((double*) y) = wasm_f64x2_extract_lane(vy, 0);
73      vy = wasm_v32x4_shuffle(vy, vy, 2, 3, 2, 3);
74      y += 2;
75    }
76    if (n & (1 * sizeof(float))) {
77      *y = wasm_f32x4_extract_lane(vy, 0);
78    }
79  }
80}
81