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#include <assert.h>
10
11#include <wasm_simd128.h>
12
13#include <xnnpack/common.h>
14#include <xnnpack/vunary.h>
15
16
17void xnn_f32_vlrelu_ukernel__wasmsimd_minmax_x${BATCH_TILE}(
18    size_t n,
19    const float* x,
20    float* y,
21    const union xnn_f32_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
22{
23  assert(n != 0);
24  assert(n % sizeof(float) == 0);
25
26  const v128_t vslope = wasm_v32x4_load_splat(&params->scalar.slope);
27  const v128_t vzero = wasm_f32x4_splat(0.0f);
28  $if BATCH_TILE > 4:
29    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
30      v128_t vx${ABC[0:4]} = wasm_v128_load(x);
31      $for N in range(4, BATCH_TILE, 4):
32        v128_t vx${ABC[N:N+4]} = wasm_v128_load(x + ${N});
33      x += ${BATCH_TILE};
34
35      $for N in range(0, BATCH_TILE, 4):
36        v128_t vacc${ABC[N:N+4]} = wasm_i32x4_max(vx${ABC[N:N+4]}, vzero);
37        vx${ABC[N:N+4]} = wasm_i32x4_min(vx${ABC[N:N+4]}, vzero);
38
39      $for N in range(0, BATCH_TILE, 4):
40        vacc${ABC[N:N+4]} = wasm_f32x4_add(vacc${ABC[N:N+4]}, wasm_f32x4_mul(vx${ABC[N:N+4]}, vslope));
41
42      wasm_v128_store(y, vacc${ABC[0:4]});
43      $for N in range(4, BATCH_TILE, 4):
44        wasm_v128_store(y + ${N}, vacc${ABC[N:N+4]});
45      y += ${BATCH_TILE};
46    }
47  for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
48    v128_t vx = wasm_v128_load(x);
49    x += 4;
50    v128_t vacc = wasm_i32x4_max(vx, vzero);
51    vx = wasm_i32x4_min(vx, vzero);
52    vacc = wasm_f32x4_add(vacc, wasm_f32x4_mul(vx, vslope));
53    wasm_v128_store(y, vacc);
54    y += 4;
55  }
56  if XNN_UNLIKELY(n != 0) {
57    v128_t vx = wasm_v128_load(x);
58    v128_t vacc = wasm_i32x4_max(vx, vzero);
59    vx = wasm_i32x4_min(vx, vzero);
60    vacc = wasm_f32x4_add(vacc, wasm_f32x4_mul(vx, vslope));
61
62    if (n & (2 * sizeof(float))) {
63      *((double*) y) = wasm_f64x2_extract_lane(vacc, 0);
64      vacc = wasm_v32x4_shuffle(vacc, vacc, 2, 3, 2, 3);
65      y += 2;
66    }
67    if (n & (1 * sizeof(float))) {
68      *y = wasm_f32x4_extract_lane(vacc, 0);
69    }
70  }
71}
72