1// Copyright 2019 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__scalar_${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  do {
48    const float* w = weights;
49    size_t c = channels;
50    $if CHANNEL_TILE > 1:
51      for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) {
52        $for C in range(CHANNEL_TILE):
53          const float vw${ABC[C]} = w[${C}];
54
55        $for M in range(ROW_TILE):
56          $for C in range(CHANNEL_TILE):
57            const float vi${M}x${ABC[C]} = i${M}[${C}];
58          i${M} += ${CHANNEL_TILE};
59
60        $for M in range(ROW_TILE):
61          $for C in range(CHANNEL_TILE):
62            const float vacc${M}x${ABC[C]} = XNN_UNPREDICTABLE(vi${M}x${ABC[C]} < 0.0f) ? vi${M}x${ABC[C]} * vw${ABC[C]} : vi${M}x${ABC[C]};
63
64        $for M in range(ROW_TILE):
65          $for C in range(CHANNEL_TILE):
66            o${M}[${C}] = vacc${M}x${ABC[C]};
67          o${M} += ${CHANNEL_TILE};
68
69        w += ${CHANNEL_TILE};
70      }
71      for (; c != 0; c -= sizeof(float)) {
72        const float vw = *w++;
73
74        $for M in range(ROW_TILE):
75          const float vi${M} = *i${M}++;
76
77        $for M in range(ROW_TILE):
78          const float vacc${M} = XNN_UNPREDICTABLE(vi${M} < 0.0f) ? vi${M} * vw : vi${M};
79
80        $for M in range(ROW_TILE):
81          *o${M}++ = vacc${M};
82      }
83    $else:
84      do {
85        const float vw = *w++;
86
87        $for M in range(ROW_TILE):
88          const float vi${M} = *i${M}++;
89
90        $for M in range(ROW_TILE):
91          const float vacc${M} = XNN_UNPREDICTABLE(vi${M} < 0.0f) ? vi${M} * vw : vi${M};
92
93        $for M in range(ROW_TILE):
94          *o${M}++ = vacc${M};
95
96        c -= sizeof(float);
97      } while (c != 0);
98    $for M in range(ROW_TILE):
99      i${M} = (const float*) ((uintptr_t) i${M} + input_increment);
100      o${M} = (float*) ((uintptr_t) o${M} + output_increment);
101      $if M % 2 == 1:
102        if XNN_UNPREDICTABLE(rows < ${ROW_TILE + M+1}) {
103          i${M} = i${M-1};
104          o${M} = o${M-1};
105        }
106      $elif M != 0:
107        if XNN_UNPREDICTABLE(rows <= ${ROW_TILE + M}) {
108          i${M} = i${M-1};
109          o${M} = o${M-1};
110        }
111    rows = doz(rows, ${ROW_TILE});
112  } while (rows != 0);
113}
114