xref: /aosp_15_r20/external/XNNPACK/src/f32-vrelu/wasm.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1*4bdc9457SAndroid Build Coastguard Worker// Copyright 2020 Google LLC
2*4bdc9457SAndroid Build Coastguard Worker//
3*4bdc9457SAndroid Build Coastguard Worker// This source code is licensed under the BSD-style license found in the
4*4bdc9457SAndroid Build Coastguard Worker// LICENSE file in the root directory of this source tree.
5*4bdc9457SAndroid Build Coastguard Worker
6*4bdc9457SAndroid Build Coastguard Worker$assert BATCH_TILE >= 1
7*4bdc9457SAndroid Build Coastguard Worker$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8*4bdc9457SAndroid Build Coastguard Worker#include <assert.h>
9*4bdc9457SAndroid Build Coastguard Worker
10*4bdc9457SAndroid Build Coastguard Worker#include <xnnpack/vunary.h>
11*4bdc9457SAndroid Build Coastguard Worker#include <xnnpack/common.h>
12*4bdc9457SAndroid Build Coastguard Worker#include <xnnpack/math.h>
13*4bdc9457SAndroid Build Coastguard Worker
14*4bdc9457SAndroid Build Coastguard Worker
15*4bdc9457SAndroid Build Coastguard Workervoid xnn_f32_vrelu_ukernel__wasm_x${BATCH_TILE}(
16*4bdc9457SAndroid Build Coastguard Worker    size_t n,
17*4bdc9457SAndroid Build Coastguard Worker    const float* x,
18*4bdc9457SAndroid Build Coastguard Worker    float* y,
19*4bdc9457SAndroid Build Coastguard Worker    const union xnn_f32_relu_params params[restrict XNN_MIN_ELEMENTS(1)])
20*4bdc9457SAndroid Build Coastguard Worker{
21*4bdc9457SAndroid Build Coastguard Worker  assert(n != 0);
22*4bdc9457SAndroid Build Coastguard Worker  assert(n % sizeof(float) == 0);
23*4bdc9457SAndroid Build Coastguard Worker  assert(x != NULL);
24*4bdc9457SAndroid Build Coastguard Worker  assert(y != NULL);
25*4bdc9457SAndroid Build Coastguard Worker
26*4bdc9457SAndroid Build Coastguard Worker  const float vzero = 0.0f;
27*4bdc9457SAndroid Build Coastguard Worker
28*4bdc9457SAndroid Build Coastguard Worker  $if BATCH_TILE > 1:
29*4bdc9457SAndroid Build Coastguard Worker    for (; n >= ${BATCH_TILE} * sizeof(float); n -= ${BATCH_TILE} * sizeof(float)) {
30*4bdc9457SAndroid Build Coastguard Worker      $for N in range(BATCH_TILE):
31*4bdc9457SAndroid Build Coastguard Worker        float vacc${ABC[N]} = x[${N}];
32*4bdc9457SAndroid Build Coastguard Worker      x += ${BATCH_TILE};
33*4bdc9457SAndroid Build Coastguard Worker
34*4bdc9457SAndroid Build Coastguard Worker      $for N in range(BATCH_TILE):
35*4bdc9457SAndroid Build Coastguard Worker        vacc${ABC[N]} = __builtin_wasm_max_f32(vacc${ABC[N]}, vzero);
36*4bdc9457SAndroid Build Coastguard Worker
37*4bdc9457SAndroid Build Coastguard Worker      $for N in range(BATCH_TILE):
38*4bdc9457SAndroid Build Coastguard Worker        y[${N}] = vacc${ABC[N]};
39*4bdc9457SAndroid Build Coastguard Worker      y += ${BATCH_TILE};
40*4bdc9457SAndroid Build Coastguard Worker    }
41*4bdc9457SAndroid Build Coastguard Worker    if XNN_UNLIKELY(n != 0) {
42*4bdc9457SAndroid Build Coastguard Worker      $if BATCH_TILE > 2:
43*4bdc9457SAndroid Build Coastguard Worker        do {
44*4bdc9457SAndroid Build Coastguard Worker          float vacc = *x++;
45*4bdc9457SAndroid Build Coastguard Worker          vacc = __builtin_wasm_max_f32(vacc, vzero);
46*4bdc9457SAndroid Build Coastguard Worker          *y++ = vacc;
47*4bdc9457SAndroid Build Coastguard Worker          n -= sizeof(float);
48*4bdc9457SAndroid Build Coastguard Worker        } while (n != 0);
49*4bdc9457SAndroid Build Coastguard Worker      $else:
50*4bdc9457SAndroid Build Coastguard Worker        float vacc = *x;
51*4bdc9457SAndroid Build Coastguard Worker        vacc = __builtin_wasm_max_f32(vacc, vzero);
52*4bdc9457SAndroid Build Coastguard Worker        *y = vacc;
53*4bdc9457SAndroid Build Coastguard Worker    }
54*4bdc9457SAndroid Build Coastguard Worker  $else:
55*4bdc9457SAndroid Build Coastguard Worker    for (; n >= sizeof(float); n -= sizeof(float)) {
56*4bdc9457SAndroid Build Coastguard Worker      float vacc = *x++;
57*4bdc9457SAndroid Build Coastguard Worker      vacc = __builtin_wasm_max_f32(vacc, vzero);
58*4bdc9457SAndroid Build Coastguard Worker      *y++ = vacc;
59*4bdc9457SAndroid Build Coastguard Worker    }
60*4bdc9457SAndroid Build Coastguard Worker}
61