blob: f8bbfd40d8e630337d7d7258a94b68bf2ee35c5a [file] [log] [blame]
Marat Dukhane6502da2020-07-15 16:31:36 -07001// 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_bitselect_x${BATCH_TILE}(
18 size_t n,
19 const float* x,
20 float* y,
Marat Dukhan7be427a2021-12-13 23:38:20 -080021 const union xnn_f32_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
Marat Dukhane6502da2020-07-15 16:31:36 -070022{
23 assert(n != 0);
24 assert(n % sizeof(float) == 0);
25
Marat Dukhan2894e992021-12-30 08:29:48 -080026 const v128_t vslope = wasm_v128_load64_splat(params->wasmsimd.slope);
Marat Dukhane6502da2020-07-15 16:31:36 -070027 $if BATCH_TILE > 4:
28 for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
29 const v128_t vx${ABC[0:4]} = wasm_v128_load(x);
30 $for N in range(4, BATCH_TILE, 4):
31 const v128_t vx${ABC[N:N+4]} = wasm_v128_load(x + ${N});
32 x += ${BATCH_TILE};
33
34 $for N in range(0, BATCH_TILE, 4):
35 v128_t vacc${ABC[N:N+4]} = wasm_f32x4_mul(vx${ABC[N:N+4]}, vslope);
Marat Dukhand4db6af2021-08-05 14:42:26 -070036 const v128_t vmask${ABC[N:N+4]} = wasm_i32x4_shr(vx${ABC[N:N+4]}, 31);
Marat Dukhane6502da2020-07-15 16:31:36 -070037
38 $for N in range(0, BATCH_TILE, 4):
39 vacc${ABC[N:N+4]} = wasm_v128_bitselect(vacc${ABC[N:N+4]}, vx${ABC[N:N+4]}, vmask${ABC[N:N+4]});
40
41 wasm_v128_store(y, vacc${ABC[0:4]});
42 $for N in range(4, BATCH_TILE, 4):
43 wasm_v128_store(y + ${N}, vacc${ABC[N:N+4]});
44 y += ${BATCH_TILE};
45 }
46 for (; n >= 4 * sizeof(float); n -= 4 * sizeof(float)) {
47 const v128_t vx = wasm_v128_load(x);
48 x += 4;
49 v128_t vacc = wasm_f32x4_mul(vx, vslope);
Marat Dukhand4db6af2021-08-05 14:42:26 -070050 const v128_t vmask = wasm_i32x4_shr(vx, 31);
Marat Dukhane6502da2020-07-15 16:31:36 -070051 vacc = wasm_v128_bitselect(vacc, vx, vmask);
52 wasm_v128_store(y, vacc);
53 y += 4;
54 }
55 if XNN_UNLIKELY(n != 0) {
56 const v128_t vx = wasm_v128_load(x);
57 v128_t vacc = wasm_f32x4_mul(vx, vslope);
Marat Dukhand4db6af2021-08-05 14:42:26 -070058 const v128_t vmask = wasm_i32x4_shr(vx, 31);
Marat Dukhane6502da2020-07-15 16:31:36 -070059 vacc = wasm_v128_bitselect(vacc, vx, vmask);
60
61 if (n & (2 * sizeof(float))) {
62 *((double*) y) = wasm_f64x2_extract_lane(vacc, 0);
63 vacc = wasm_v32x4_shuffle(vacc, vacc, 2, 3, 2, 3);
64 y += 2;
65 }
66 if (n & (1 * sizeof(float))) {
67 *y = wasm_f32x4_extract_lane(vacc, 0);
68 }
69 }
70}