• 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 CHANNEL_TILE > 0
7$assert ROW_TILE >= 1
8$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9#include <assert.h>
10
11#include <xnnpack/math.h>
12#include <xnnpack/prelu.h>
13
14
15void xnn_f32_prelu_ukernel__wasm_${ROW_TILE}x${CHANNEL_TILE}(
16    size_t rows,
17    size_t channels,
18    const float*restrict input,
19    size_t input_stride,
20    const float*restrict weights,
21    float*restrict output,
22    size_t output_stride)
23{
24  assert(rows != 0);
25  assert(channels != 0);
26  assert(channels % sizeof(float) == 0);
27
28  const float* i0 = input;
29  float* o0 = output;
30  $for M in range(1, ROW_TILE):
31    const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride);
32    float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride);
33    $if M % 2 == 0:
34      if XNN_UNPREDICTABLE(rows <= ${M}) {
35        i${M} = i${M-1};
36        o${M} = o${M-1};
37      }
38    $else:
39      if XNN_UNPREDICTABLE(rows < ${M+1}) {
40        i${M} = i${M-1};
41        o${M} = o${M-1};
42      }
43
44  const size_t input_increment = input_stride * ${ROW_TILE} - channels;
45  const size_t output_increment = output_stride * ${ROW_TILE} - channels;
46
47  const float vzero = 0.0f;
48  do {
49    const float* w = weights;
50    size_t c = channels;
51    $if CHANNEL_TILE > 1:
52      for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) {
53        $for C in range(CHANNEL_TILE):
54          const float vw${ABC[C]} = w[${C}];
55
56        $for M in range(ROW_TILE):
57          $for C in range(CHANNEL_TILE):
58            float vi${M}x${ABC[C]} = i${M}[${C}];
59          i${M} += ${CHANNEL_TILE};
60
61        $for M in range(ROW_TILE):
62          $for C in range(CHANNEL_TILE):
63            float vacc${M}x${ABC[C]} = __builtin_wasm_max_f32(vi${M}x${ABC[C]}, vzero);
64            vi${M}x${ABC[C]} = __builtin_wasm_min_f32(vi${M}x${ABC[C]}, vzero);
65
66        $for M in range(ROW_TILE):
67          $for C in range(CHANNEL_TILE):
68            vacc${M}x${ABC[C]} += vi${M}x${ABC[C]} * vw${ABC[C]};
69
70        $for M in range(ROW_TILE):
71          $for C in range(CHANNEL_TILE):
72            o${M}[${C}] = vacc${M}x${ABC[C]};
73          o${M} += ${CHANNEL_TILE};
74
75        w += ${CHANNEL_TILE};
76      }
77      for (; c != 0; c -= sizeof(float)) {
78        const float vw = *w++;
79
80        $for M in range(ROW_TILE):
81          float vi${M} = *i${M}++;
82
83        $for M in range(ROW_TILE):
84          float vacc${M} = __builtin_wasm_max_f32(vi${M}, vzero);
85          vi${M} = __builtin_wasm_min_f32(vi${M}, vzero);
86
87        $for M in range(ROW_TILE):
88          vacc${M} += vi${M} * vw;
89
90        $for M in range(ROW_TILE):
91          *o${M}++ = vacc${M};
92      }
93    $else:
94      do {
95        const float vw = *w++;
96
97        $for M in range(ROW_TILE):
98          float vi${M} = *i${M}++;
99
100        $for M in range(ROW_TILE):
101          float vacc${M} = __builtin_wasm_max_f32(vi${M}, vzero);
102          vi${M} = __builtin_wasm_min_f32(vi${M}, vzero);
103
104        $for M in range(ROW_TILE):
105          vacc${M} += vi${M} * vw;
106
107        $for M in range(ROW_TILE):
108          *o${M}++ = vacc${M};
109
110        c -= sizeof(float);
111      } while (c != 0);
112    $for M in range(ROW_TILE):
113      i${M} = (const float*) ((uintptr_t) i${M} + input_increment);
114      o${M} = (float*) ((uintptr_t) o${M} + output_increment);
115      $if M % 2 == 1:
116        if XNN_UNPREDICTABLE(rows < ${ROW_TILE + M+1}) {
117          i${M} = i${M-1};
118          o${M} = o${M-1};
119        }
120      $elif M != 0:
121        if XNN_UNPREDICTABLE(rows <= ${ROW_TILE + M}) {
122          i${M} = i${M-1};
123          o${M} = o${M-1};
124        }
125    rows = doz(rows, ${ROW_TILE});
126  } while (rows != 0);
127}
128