• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2022 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 >= 16
7$assert BATCH_TILE % 16 == 0
8$SIMD_TILE = BATCH_TILE // 16
9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10#include <assert.h>
11
12#include <wasm_simd128.h>
13
14#include <xnnpack/common.h>
15#include <xnnpack/vcvt.h>
16
17
18$XINT8_T = {"QS8": "int8_t", "QU8": "uint8_t"}[DATATYPE]
19$WASM_X16X8_LOAD8X8 = {"QS8": "wasm_i16x8_load8x8", "QU8": "wasm_u16x8_load8x8"}[DATATYPE]
20$WASM_X16X8_EXTEND_LOW_X8X16 = {"QS8": "wasm_i16x8_extend_low_i8x16", "QU8": "wasm_u16x8_extend_low_u8x16"}[DATATYPE]
21$WASM_X16X8_EXTEND_HIGH_X8X16 = {"QS8": "wasm_i16x8_extend_high_i8x16", "QU8": "wasm_u16x8_extend_high_u8x16"}[DATATYPE]
22$WASM_I16X8_Q15MULR = "__builtin_wasm_relaxed_q15mulr_s_i16x8" if RELAXED else "wasm_i16x8_q15mulr_sat"
23$WASM_X8X16_NARROW_I16X8 = {"QS8": "wasm_i8x16_narrow_i16x8", "QU8": "wasm_u8x16_narrow_i16x8"}[DATATYPE]
24$ISA = "wasmrelaxedsimd" if RELAXED else "wasmsimd"
25void xnn_${DATATYPE.lower()}_vlrelu_ukernel__${ISA}_arm_x${BATCH_TILE}(
26    size_t n,
27    const ${XINT8_T}* x,
28    ${XINT8_T}* y,
29    const union xnn_${DATATYPE.lower()}_lrelu_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
30{
31  assert(n != 0);
32  assert(n % sizeof(${XINT8_T}) == 0);
33  assert(x != NULL);
34  assert(y != NULL);
35
36  const v128_t vinput_zero_point = wasm_v128_load64_splat(params->wasmsimd_arm.input_zero_point);
37  const v128_t vpositive_multiplier = wasm_v128_load64_splat(params->wasmsimd_arm.positive_multiplier);
38  const v128_t vnegative_multiplier = wasm_v128_load64_splat(params->wasmsimd_arm.negative_multiplier);
39  const v128_t voutput_zero_point = wasm_v128_load64_splat(params->wasmsimd_arm.output_zero_point);
40  for (; n >= ${BATCH_TILE} * sizeof(${XINT8_T}); n -= ${BATCH_TILE} * sizeof(${XINT8_T})) {
41    v128_t vx${ABC[0]} = wasm_v128_load(x);
42    $for N in range(1, SIMD_TILE):
43      v128_t vx${ABC[N]} = wasm_v128_load(x + ${N * 16});
44    x += ${BATCH_TILE};
45
46    $for N in range(SIMD_TILE):
47      v128_t vacc${ABC[2*N]} = wasm_i16x8_sub(vinput_zero_point, ${WASM_X16X8_EXTEND_LOW_X8X16}(vx${ABC[N]}));
48      v128_t vacc${ABC[2*N+1]} = wasm_i16x8_sub(vinput_zero_point, ${WASM_X16X8_EXTEND_HIGH_X8X16}(vx${ABC[N]}));
49      v128_t vmultiplier${ABC[2*N]} = wasm_i16x8_shr(vacc${ABC[2*N]}, 15);
50      v128_t vmultiplier${ABC[2*N+1]} = wasm_i16x8_shr(vacc${ABC[2*N+1]}, 15);
51
52    $for N in range(2*SIMD_TILE):
53      vacc${ABC[N]} = wasm_i16x8_shl(vacc${ABC[N]}, 7);
54      vmultiplier${ABC[N]} = wasm_v128_bitselect(vpositive_multiplier, vnegative_multiplier, vmultiplier${ABC[N]});
55
56    $for N in range(2*SIMD_TILE):
57      vacc${ABC[N]} = ${WASM_I16X8_Q15MULR}(vacc${ABC[N]}, vmultiplier${ABC[N]});
58
59    $for N in range(2*SIMD_TILE):
60      vacc${ABC[N]} = wasm_i16x8_add_sat(vacc${ABC[N]}, voutput_zero_point);
61
62    $for N in range(SIMD_TILE):
63      const v128_t vy${ABC[N]} = ${WASM_X8X16_NARROW_I16X8}(vacc${ABC[2*N]}, vacc${ABC[2*N+1]});
64
65    wasm_v128_store(y, vy${ABC[0]});
66    $for N in range(1, SIMD_TILE):
67      wasm_v128_store((y + ${N * 16}), vy${ABC[N]});
68    y += ${BATCH_TILE};
69  }
70  for (; n >= 8 * sizeof(${XINT8_T}); n -= 8 * sizeof(${XINT8_T})) {
71    const v128_t vx = ${WASM_X16X8_LOAD8X8}(x);
72    v128_t vacc = wasm_i16x8_sub(vinput_zero_point, vx);
73    v128_t vmultiplier = wasm_i16x8_shr(vacc, 15);
74    vacc = wasm_i16x8_shl(vacc, 7);
75    vmultiplier = wasm_v128_bitselect(vpositive_multiplier, vnegative_multiplier, vmultiplier);
76    vacc = ${WASM_I16X8_Q15MULR}(vacc, vmultiplier);
77    vacc = wasm_i16x8_add_sat(vacc, voutput_zero_point);
78    x += 8;
79
80    const v128_t vy = ${WASM_X8X16_NARROW_I16X8}(vacc, vacc);
81    wasm_v128_store64_lane(y, vy, 0);
82    y += 8;
83  }
84  if XNN_UNLIKELY(n != 0) {
85    assert(n >= 1 * sizeof(${XINT8_T}));
86    assert(n <= 7 * sizeof(${XINT8_T}));
87
88    const v128_t vx = ${WASM_X16X8_LOAD8X8}(x);
89    v128_t vacc = wasm_i16x8_sub(vinput_zero_point, vx);
90    v128_t vmultiplier = wasm_i16x8_shr(vacc, 15);
91    vacc = wasm_i16x8_shl(vacc, 7);
92    vmultiplier = wasm_v128_bitselect(vpositive_multiplier, vnegative_multiplier, vmultiplier);
93    vacc = ${WASM_I16X8_Q15MULR}(vacc, vmultiplier);
94    vacc = wasm_i16x8_add_sat(vacc, voutput_zero_point);
95
96    v128_t vy = ${WASM_X8X16_NARROW_I16X8}(vacc, vacc);
97    if (n & (4 * sizeof(${XINT8_T}))) {
98      wasm_v128_store32_lane(y, vy, 0);
99      vy = wasm_u64x2_shr(vy, 32);
100      y += 4;
101    }
102    if (n & (2 * sizeof(${XINT8_T}))) {
103      wasm_v128_store16_lane(y, vy, 0);
104      vy = wasm_u32x4_shr(vy, 16);
105      y += 2;
106    }
107    if (n & (1 * sizeof(${XINT8_T}))) {
108      wasm_v128_store8_lane(y, vy, 0);
109    }
110  }
111}
112