xref: /aosp_15_r20/external/XNNPACK/src/f32-prelu/scalar.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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
34  const size_t input_increment = input_stride * ${ROW_TILE} - channels;
35  const size_t output_increment = output_stride * ${ROW_TILE} - channels;
36
37  do {
38    $for M in range(1, ROW_TILE):
39      $if M % 2 == 0:
40        if XNN_UNPREDICTABLE(rows <= ${M}) {
41          i${M} = i${M-1};
42          o${M} = o${M-1};
43        }
44      $else:
45        if XNN_UNPREDICTABLE(rows < ${M+1}) {
46          i${M} = i${M-1};
47          o${M} = o${M-1};
48        }
49
50    const float* w = weights;
51    size_t c = channels;
52    $if CHANNEL_TILE > 1:
53      for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) {
54        $for C in range(CHANNEL_TILE):
55          const float vw${ABC[C]} = w[${C}];
56
57        $for M in range(ROW_TILE):
58          $for C in range(CHANNEL_TILE):
59            const float vi${M}x${ABC[C]} = i${M}[${C}];
60          i${M} += ${CHANNEL_TILE};
61
62        $for M in range(ROW_TILE):
63          $for C in range(CHANNEL_TILE):
64            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]};
65
66        $for M in range(ROW_TILE):
67          $for C in range(CHANNEL_TILE):
68            o${M}[${C}] = vacc${M}x${ABC[C]};
69          o${M} += ${CHANNEL_TILE};
70
71        w += ${CHANNEL_TILE};
72      }
73      for (; c != 0; c -= sizeof(float)) {
74        const float vw = *w++;
75
76        $for M in range(ROW_TILE):
77          const float vi${M} = *i${M}++;
78
79        $for M in range(ROW_TILE):
80          const float vacc${M} = XNN_UNPREDICTABLE(vi${M} < 0.0f) ? vi${M} * vw : vi${M};
81
82        $for M in range(ROW_TILE):
83          *o${M}++ = vacc${M};
84      }
85    $else:
86      do {
87        const float vw = *w++;
88
89        $for M in range(ROW_TILE):
90          const float vi${M} = *i${M}++;
91
92        $for M in range(ROW_TILE):
93          const float vacc${M} = XNN_UNPREDICTABLE(vi${M} < 0.0f) ? vi${M} * vw : vi${M};
94
95        $for M in range(ROW_TILE):
96          *o${M}++ = vacc${M};
97
98        c -= sizeof(float);
99      } while (c != 0);
100    $for M in range(ROW_TILE):
101      i${M} = (const float*) ((uintptr_t) i${M} + input_increment);
102      o${M} = (float*) ((uintptr_t) o${M} + output_increment);
103    rows = doz(rows, ${ROW_TILE});
104  } while (rows != 0);
105}
106