• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 % 4 == 0
7$assert CHANNEL_TILE >= 4
8$assert ROW_TILE >= 1
9$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10#include <assert.h>
11
12#include <arm_neon.h>
13
14#include <xnnpack/math.h>
15#include <xnnpack/vmulcaddc.h>
16
17
18void xnn_f32_vmulcaddc_minmax_ukernel_c${CHANNEL_TILE}__${"neonfma" if FMA else "neon"}_${ROW_TILE}x(
19    size_t rows,
20    size_t channels,
21    const float*restrict input,
22    size_t input_stride,
23    const float*restrict weights,
24    float*restrict output,
25    size_t output_stride,
26    const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
27{
28  assert(rows != 0);
29  assert(channels != 0);
30  assert(channels % sizeof(float) == 0);
31
32  const float* i0 = input;
33  float* o0 = output;
34  $for M in range(1, ROW_TILE):
35    const float* i${M} = (const float*) ((uintptr_t) i${M-1} + input_stride);
36    float* o${M} = (float*) ((uintptr_t) o${M-1} + output_stride);
37    $if M % 2 == 0:
38      if XNN_UNPREDICTABLE(rows <= ${M}) {
39        i${M} = i${M-1};
40        o${M} = o${M-1};
41      }
42    $else:
43      if XNN_UNPREDICTABLE(rows < ${M+1}) {
44        i${M} = i${M-1};
45        o${M} = o${M-1};
46      }
47
48  const size_t input_increment = input_stride * ${ROW_TILE} - channels;
49  const size_t output_increment = output_stride * ${ROW_TILE} - channels;
50
51  const float32x4_t vmin = vld1q_dup_f32(&params->scalar.min);
52  const float32x4_t vmax = vld1q_dup_f32(&params->scalar.max);
53  do {
54    const float* w = weights;
55    size_t c = channels;
56    for (; c >= ${CHANNEL_TILE} * sizeof(float); c -= ${CHANNEL_TILE} * sizeof(float)) {
57      $for C in range(0, CHANNEL_TILE, 4):
58        const float32x4_t vscale${ABC[C:C+4]} = vld1q_f32(w); w += 4;
59
60      $for M in range(ROW_TILE):
61        $for C in range(0, CHANNEL_TILE, 4):
62          float32x4_t vacc${M}x${ABC[C:C+4]} = vld1q_f32(i${M}); i${M} += 4;
63
64      $if not FMA:
65        $for M in range(ROW_TILE):
66          $for C in range(0, CHANNEL_TILE, 4):
67            vacc${M}x${ABC[C:C+4]} = vmulq_f32(vacc${M}x${ABC[C:C+4]}, vscale${ABC[C:C+4]});
68
69      $for C in range(0, CHANNEL_TILE, 4):
70        const float32x4_t vbias${ABC[C:C+4]} = vld1q_f32(w); w += 4;
71
72      $if not FMA:
73        $for M in range(ROW_TILE):
74          $for C in range(0, CHANNEL_TILE, 4):
75            vacc${M}x${ABC[C:C+4]} = vaddq_f32(vacc${M}x${ABC[C:C+4]}, vbias${ABC[C:C+4]});
76      $else:
77        $for M in range(ROW_TILE):
78          $for C in range(0, CHANNEL_TILE, 4):
79            vacc${M}x${ABC[C:C+4]} = vfmaq_f32(vbias${ABC[C:C+4]}, vscale${ABC[C:C+4]}, vacc${M}x${ABC[C:C+4]});
80
81      $for M in range(ROW_TILE):
82        $for C in range(0, CHANNEL_TILE, 4):
83          vacc${M}x${ABC[C:C+4]} = vmaxq_f32(vacc${M}x${ABC[C:C+4]}, vmin);
84
85      $for M in range(ROW_TILE):
86        $for C in range(0, CHANNEL_TILE, 4):
87          vacc${M}x${ABC[C:C+4]} = vminq_f32(vacc${M}x${ABC[C:C+4]}, vmax);
88
89      $for M in range(ROW_TILE):
90        $for C in range(0, CHANNEL_TILE, 4):
91          vst1q_f32(o${M}, vacc${M}x${ABC[C:C+4]}); o${M} += 4;
92    }
93    $if CHANNEL_TILE > 4:
94      for (; c >= 4 * sizeof(float); c -= 4 * sizeof(float)) {
95        const float32x4_t vscale0123 = vld1q_f32(w); w += 4;
96
97        $for M in range(ROW_TILE):
98          float32x4_t vacc${M}x0123 = vld1q_f32(i${M}); i${M} += 4;
99
100        $if not FMA:
101          $for M in range(ROW_TILE):
102            vacc${M}x0123 = vmulq_f32(vacc${M}x0123, vscale0123);
103
104        const float32x4_t vbias0123 = vld1q_f32(w + ${CHANNEL_TILE - 4});
105
106        $if not FMA:
107          $for M in range(ROW_TILE):
108            vacc${M}x0123 = vaddq_f32(vacc${M}x0123, vbias0123);
109        $else:
110          $for M in range(ROW_TILE):
111            vacc${M}x0123 = vfmaq_f32(vbias0123, vscale0123, vacc${M}x0123);
112
113        $for M in range(ROW_TILE):
114          vacc${M}x0123 = vmaxq_f32(vacc${M}x0123, vmin);
115
116        $for M in range(ROW_TILE):
117          vacc${M}x0123 = vminq_f32(vacc${M}x0123, vmax);
118
119        $for M in range(ROW_TILE):
120          vst1q_f32(o${M}, vacc${M}x0123); o${M} += 4;
121      }
122    if XNN_UNLIKELY(c != 0) {
123      const float32x4_t vscale0123 = vld1q_f32(w);
124
125      $for M in range(ROW_TILE):
126        float32x4_t vacc${M}x0123 = vld1q_f32(i${M}); i${M} = (const float*) ((uintptr_t) i${M} + c);
127
128      $if not FMA:
129        $for M in range(ROW_TILE):
130          vacc${M}x0123 = vmulq_f32(vacc${M}x0123, vscale0123);
131
132      const float32x4_t vbias0123 = vld1q_f32(w + ${CHANNEL_TILE});
133
134      $if not FMA:
135        $for M in range(ROW_TILE):
136          vacc${M}x0123 = vaddq_f32(vacc${M}x0123, vbias0123);
137      $else:
138        $for M in range(ROW_TILE):
139          vacc${M}x0123 = vfmaq_f32(vbias0123, vscale0123, vacc${M}x0123);
140
141      $for M in range(ROW_TILE):
142        vacc${M}x0123 = vmaxq_f32(vacc${M}x0123, vmin);
143
144      $for M in range(ROW_TILE):
145        vacc${M}x0123 = vminq_f32(vacc${M}x0123, vmax);
146
147      $for M in range(ROW_TILE):
148        float32x2_t vacc${M}x01 = vget_low_f32(vacc${M}x0123);
149      if (c & (2 * sizeof(float))) {
150        $for M in range(ROW_TILE):
151          vst1_f32(o${M}, vacc${M}x01); o${M} += 2;
152
153        $for M in range(ROW_TILE):
154          vacc${M}x01 = vget_high_f32(vacc${M}x0123);
155      }
156      if (c & (1 * sizeof(float))) {
157        $for M in range(ROW_TILE):
158          vst1_lane_f32(o${M}, vacc${M}x01, 0); o${M} += 1;
159      }
160    }
161    $for M in range(ROW_TILE):
162      i${M} = (const float*) ((uintptr_t) i${M} + input_increment);
163      o${M} = (float*) ((uintptr_t) o${M} + output_increment);
164      $if M % 2 == 1:
165        if XNN_UNPREDICTABLE(rows < ${ROW_TILE + M + 1}) {
166          i${M} = i${M-1};
167          o${M} = o${M-1};
168        }
169      $elif M != 0:
170        if XNN_UNPREDICTABLE(rows <= ${ROW_TILE + M}) {
171          i${M} = i${M-1};
172          o${M} = o${M-1};
173        }
174    rows = doz(rows, ${ROW_TILE});
175  } while (rows != 0);
176}
177