xref: /aosp_15_r20/external/XNNPACK/src/qs8-gemm/neon-mlal-lane.c.in (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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$ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
7$assert NR % 8 == 0
8$assert 8 <= NR <= 16
9$assert REQUANTIZATION in ["FP32", "RNDNU"]
10$assert DATATYPE in ["QC8", "QS8", "QU8"]
11$assert DATATYPE != "QC8" or REQUANTIZATION == "FP32"
12#include <assert.h>
13
14#include <arm_neon.h>
15
16#include <xnnpack/common.h>
17#include <xnnpack/gemm.h>
18$if REQUANTIZATION == "FP32" and ARMV8:
19  #include <xnnpack/intrinsics-polyfill.h>
20
21
22$PARAMS_STRUCT = REQUANTIZATION.lower() + "_" + ("neonv8" if REQUANTIZATION == "FP32" and ARMV8 else "neon")
23$PARAMS_UNION = "xnn_%s_conv_minmax_params" % DATATYPE.lower()
24$XINT8_T = "uint8_t" if DATATYPE == "QU8" else "int8_t"
25$XINT8X8_T = "uint8x8_t" if DATATYPE == "QU8" else "int8x8_t"
26$XINT8X16_T = "uint8x16_t" if DATATYPE == "QU8" else "int8x16_t"
27$VGET_LOW_X8 = "vget_low_u8" if DATATYPE == "QU8" else "vget_low_s8"
28$VGET_HIGH_X8 = "vget_high_u8" if DATATYPE == "QU8" else "vget_high_s8"
29$VCOMBINE_X8 = "vcombine_u8" if DATATYPE == "QU8" else "vcombine_s8"
30$VREINTERPRET_U32_X8 = "vreinterpret_u32_u8" if DATATYPE == "QU8" else "vreinterpret_u32_s8"
31$VREINTERPRETQ_U32_X8 = "vreinterpretq_u32_u8" if DATATYPE == "QU8" else "vreinterpretq_u32_s8"
32$VREINTERPRET_U16_X8 = "vreinterpret_u16_u8" if DATATYPE == "QU8" else "vreinterpret_u16_s8"
33$VREINTERPRETQ_U16_X8 = "vreinterpretq_u16_u8" if DATATYPE == "QU8" else "vreinterpretq_u16_s8"
34$VLD1_X8 = "vld1_u8" if DATATYPE == "QU8" else "vld1_s8"
35$VLD1_DUP_X8 = "vld1_dup_u8" if DATATYPE == "QU8" else "vld1_dup_s8"
36$VLD1Q_DUP_X8 = "vld1q_dup_u8" if DATATYPE == "QU8" else "vld1q_dup_s8"
37$VST1_X8 = "vst1_u8" if DATATYPE == "QU8" else "vst1_s8"
38$VST1Q_X8 = "vst1q_u8" if DATATYPE == "QU8" else "vst1q_s8"
39$VST1_LANE_X8 = "vst1_lane_u8" if DATATYPE == "QU8" else "vst1_lane_s8"
40$VST1Q_LANE_X8 = "vst1q_lane_u8" if DATATYPE == "QU8" else "vst1q_lane_s8"
41$VMIN_X8 = "vmin_u8" if DATATYPE == "QU8" else "vmin_s8"
42$VMAX_X8 = "vmax_u8" if DATATYPE == "QU8" else "vmax_s8"
43$VMINQ_X8 = "vminq_u8" if DATATYPE == "QU8" else "vminq_s8"
44$VMAXQ_X8 = "vmaxq_u8" if DATATYPE == "QU8" else "vmaxq_s8"
45$VEXT_X8 = "vext_u8" if DATATYPE == "QU8" else "vext_s8"
46$VEXTQ_X8 = "vextq_u8" if DATATYPE == "QU8" else "vextq_s8"
47$VQMOVXN_S16 = "vqmovun_s16" if DATATYPE == "QU8" else "vqmovn_s16"
48$VQMOVXN_HIGH_S16 = "vqmovun_high_s16" if DATATYPE == "QU8" else "vqmovn_high_s16"
49$ISA = "neonv8" if ARMV8 else "neon"
50void xnn_${DATATYPE.lower()}_gemm_minmax_${REQUANTIZATION.lower()}_ukernel_${MR}x${NR}__${ISA}_mlal_lane${"_prfm" if PREFETCH else ""}(
51    size_t mr,
52    size_t nc,
53    size_t kc,
54    const ${XINT8_T}* restrict a,
55    size_t a_stride,
56    const void* restrict w,
57    ${XINT8_T}* restrict c,
58    size_t cm_stride,
59    size_t cn_stride,
60    const union ${PARAMS_UNION} params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
61{
62  assert(mr != 0);
63  assert(mr <= ${MR});
64  assert(nc != 0);
65  assert(kc != 0);
66  assert(kc % sizeof(${XINT8_T}) == 0);
67  assert(a != NULL);
68  assert(w != NULL);
69  assert(c != NULL);
70
71  const ${XINT8_T}* a0 = a;
72  ${XINT8_T}* c0 = c;
73  $for M in range(1, MR):
74    const ${XINT8_T}* a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M-1} + a_stride);
75    ${XINT8_T}* c${M} = (${XINT8_T}*) ((uintptr_t) c${M-1} + cm_stride);
76    $if M % 2 == 0:
77      if XNN_UNPREDICTABLE(mr <= ${M}) {
78        a${M} = a${M-1};
79        c${M} = c${M-1};
80      }
81    $elif M + 1 == MR:
82      if XNN_UNPREDICTABLE(mr != ${M+1}) {
83        a${M} = a${M-1};
84        c${M} = c${M-1};
85      }
86    $else:
87      if XNN_UNPREDICTABLE(mr < ${M+1}) {
88        a${M} = a${M-1};
89        c${M} = c${M-1};
90      }
91
92  $if DATATYPE == "QU8":
93    const uint8x8_t vb_zero_point = vld1_dup_u8(&params->${PARAMS_STRUCT}.kernel_zero_point[0]);
94  do {
95    $for N in range(0, NR, 4):
96      int32x4_t vacc0x${ABC[N:N+4]} = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
97    $for M in range(1, MR):
98      $for N in range(0, NR, 4):
99        int32x4_t vacc${M}x${ABC[N:N+4]} = vacc0x${ABC[N:N+4]};
100
101    size_t k = kc;
102    while (k >= 8 * sizeof(${XINT8_T})) {
103      $for M in range(MR):
104        const ${XINT8X8_T} va${M} = ${VLD1_X8}(a${M}); a${M} += 8;
105        $if DATATYPE == "QU8":
106          const int16x8_t vxa${M} = vreinterpretq_s16_u16(vmovl_u8(va${M}));
107        $else:
108          const int16x8_t vxa${M} = vmovl_s8(va${M});
109
110      $for K in range(4):
111        $for N in range(0, NR, 8):
112          const ${XINT8X8_T} vb${ABC[N:N+8]}c${K} = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
113          $if DATATYPE == "QU8":
114            const int16x8_t vxb${ABC[N:N+8]}c${K} = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c${K}, vb_zero_point));
115          $else:
116            const int16x8_t vxb${ABC[N:N+8]}c${K} = vmovl_s8(vb${ABC[N:N+8]}c${K});
117
118          $for M in range(MR):
119            vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c${K}), vget_low_s16(vxa${M}), ${K});
120            vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c${K}), vget_low_s16(vxa${M}), ${K});
121
122      $if PREFETCH:
123        $for N in range(0, NR, 8):
124          __builtin_prefetch((const ${XINT8_T}*) w + ${N * 8 + 448});
125
126      $for K in range(4, 8):
127        $for N in range(0, NR, 8):
128          const ${XINT8X8_T} vb${ABC[N:N+8]}c${K} = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
129          $if DATATYPE == "QU8":
130            const int16x8_t vxb${ABC[N:N+8]}c${K} = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c${K}, vb_zero_point));
131          $else:
132            const int16x8_t vxb${ABC[N:N+8]}c${K} = vmovl_s8(vb${ABC[N:N+8]}c${K});
133
134          $for M in range(MR):
135            vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c${K}), vget_high_s16(vxa${M}), ${K-4});
136            vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c${K}), vget_high_s16(vxa${M}), ${K-4});
137
138      k -= 8 * sizeof(${XINT8_T});
139    }
140    if XNN_UNLIKELY(k != 0) {
141      $for M in range(MR):
142        const ${XINT8X8_T} va${M} = ${VLD1_X8}(a${M}); a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} + k);
143        $if DATATYPE == "QU8":
144          const int16x8_t vxa${M} = vreinterpretq_s16_u16(vmovl_u8(va${M}));
145        $else:
146          const int16x8_t vxa${M} = vmovl_s8(va${M});
147
148      $for N in range(0, NR, 8):
149        const ${XINT8X8_T} vb${ABC[N:N+8]}c0 = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
150        $if DATATYPE == "QU8":
151          const int16x8_t vxb${ABC[N:N+8]}c0 = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c0, vb_zero_point));
152        $else:
153          const int16x8_t vxb${ABC[N:N+8]}c0 = vmovl_s8(vb${ABC[N:N+8]}c0);
154
155      $for M in range(MR):
156        $for N in range(0, NR, 8):
157          vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c0), vget_low_s16(vxa${M}), 0);
158          vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c0), vget_low_s16(vxa${M}), 0);
159
160      if (k >= 2 * sizeof(${XINT8_T})) {
161        $for N in range(0, NR, 8):
162          const ${XINT8X8_T} vb${ABC[N:N+8]}c1 = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
163          $if DATATYPE == "QU8":
164            const int16x8_t vxb${ABC[N:N+8]}c1 = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c1, vb_zero_point));
165          $else:
166            const int16x8_t vxb${ABC[N:N+8]}c1 = vmovl_s8(vb${ABC[N:N+8]}c1);
167
168        $for M in range(MR):
169          $for N in range(0, NR, 8):
170            vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c1), vget_low_s16(vxa${M}), 1);
171            vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c1), vget_low_s16(vxa${M}), 1);
172
173        if (k > 2 * sizeof(${XINT8_T})) {
174          $for N in range(0, NR, 8):
175            const ${XINT8X8_T} vb${ABC[N:N+8]}c2 = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
176            $if DATATYPE == "QU8":
177              const int16x8_t vxb${ABC[N:N+8]}c2 = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c2, vb_zero_point));
178            $else:
179              const int16x8_t vxb${ABC[N:N+8]}c2 = vmovl_s8(vb${ABC[N:N+8]}c2);
180
181          $for M in range(MR):
182            $for N in range(0, NR, 8):
183              vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c2), vget_low_s16(vxa${M}), 2);
184              vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c2), vget_low_s16(vxa${M}), 2);
185
186          if (k >= 4 * sizeof(${XINT8_T})) {
187            $for N in range(0, NR, 8):
188              const ${XINT8X8_T} vb${ABC[N:N+8]}c3 = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
189              $if DATATYPE == "QU8":
190                const int16x8_t vxb${ABC[N:N+8]}c3 = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c3, vb_zero_point));
191              $else:
192                const int16x8_t vxb${ABC[N:N+8]}c3 = vmovl_s8(vb${ABC[N:N+8]}c3);
193
194            $for M in range(MR):
195              $for N in range(0, NR, 8):
196                vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c3), vget_low_s16(vxa${M}), 3);
197                vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c3), vget_low_s16(vxa${M}), 3);
198
199            if (k > 4 * sizeof(${XINT8_T})) {
200              $for N in range(0, NR, 8):
201                const ${XINT8X8_T} vb${ABC[N:N+8]}c4 = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
202                $if DATATYPE == "QU8":
203                  const int16x8_t vxb${ABC[N:N+8]}c4 = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c4, vb_zero_point));
204                $else:
205                  const int16x8_t vxb${ABC[N:N+8]}c4 = vmovl_s8(vb${ABC[N:N+8]}c4);
206
207              $for M in range(MR):
208                $for N in range(0, NR, 8):
209                  vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c4), vget_high_s16(vxa${M}), 0);
210                  vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c4), vget_high_s16(vxa${M}), 0);
211
212              if (k >= 6 * sizeof(${XINT8_T})) {
213                $for N in range(0, NR, 8):
214                  const ${XINT8X8_T} vb${ABC[N:N+8]}c5 = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
215                  $if DATATYPE == "QU8":
216                    const int16x8_t vxb${ABC[N:N+8]}c5 = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c5, vb_zero_point));
217                  $else:
218                    const int16x8_t vxb${ABC[N:N+8]}c5 = vmovl_s8(vb${ABC[N:N+8]}c5);
219
220                $for M in range(MR):
221                  $for N in range(0, NR, 8):
222                    vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c5), vget_high_s16(vxa${M}), 1);
223                    vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c5), vget_high_s16(vxa${M}), 1);
224
225                if (k > 6 * sizeof(${XINT8_T})) {
226                  $for N in range(0, NR, 8):
227                    const ${XINT8X8_T} vb${ABC[N:N+8]}c6 = ${VLD1_X8}(w); w = (const void*) ((const ${XINT8_T}*) w + 8);
228                    $if DATATYPE == "QU8":
229                      const int16x8_t vxb${ABC[N:N+8]}c6 = vreinterpretq_s16_u16(vsubl_u8(vb${ABC[N:N+8]}c6, vb_zero_point));
230                    $else:
231                      const int16x8_t vxb${ABC[N:N+8]}c6 = vmovl_s8(vb${ABC[N:N+8]}c6);
232
233                  $for M in range(MR):
234                    $for N in range(0, NR, 8):
235                      vacc${M}x${ABC[N:N+4]} = vmlal_lane_s16(vacc${M}x${ABC[N:N+4]}, vget_low_s16(vxb${ABC[N:N+8]}c6), vget_high_s16(vxa${M}), 2);
236                      vacc${M}x${ABC[N+4:N+8]} = vmlal_lane_s16(vacc${M}x${ABC[N+4:N+8]}, vget_high_s16(vxb${ABC[N:N+8]}c6), vget_high_s16(vxa${M}), 2);
237                }
238              }
239            }
240          }
241        }
242      }
243    }
244
245    $if REQUANTIZATION == "RNDNU":
246      const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->${PARAMS_STRUCT}.right_pre_shift);
247      const int32x4_t vmultiplier = vld1q_dup_s32(&params->${PARAMS_STRUCT}.multiplier);
248      const int32x4_t vright_post_shift = vld1q_dup_s32(&params->${PARAMS_STRUCT}.right_post_shift);
249
250      $for M in range(MR):
251        $for N in range(0, NR, 4):
252          vacc${M}x${ABC[N:N+4]} = vqshlq_s32(vacc${M}x${ABC[N:N+4]}, vright_pre_shift);
253
254      $for M in range(MR):
255        $for N in range(0, NR, 4):
256          vacc${M}x${ABC[N:N+4]} = vqdmulhq_s32(vacc${M}x${ABC[N:N+4]}, vmultiplier);
257
258      $for M in range(MR):
259        $for N in range(0, NR, 4):
260          vacc${M}x${ABC[N:N+4]} = vrshlq_s32(vacc${M}x${ABC[N:N+4]}, vright_post_shift);
261    $elif REQUANTIZATION == "FP32":
262      $for M in range(MR):
263        $for N in range(0, NR, 4):
264          float32x4_t vfpacc${M}x${ABC[N:N+4]} = vcvtq_f32_s32(vacc${M}x${ABC[N:N+4]});
265
266      $if DATATYPE == "QC8":
267        $for N in range(0, NR, 4):
268          const float32x4_t vscale${ABC[N:N+4]} = vld1q_f32((const float*) w); w = (const void*) ((const float*) w + 4);
269          $for M in range(MR):
270            vfpacc${M}x${ABC[N:N+4]} = vmulq_f32(vfpacc${M}x${ABC[N:N+4]}, vscale${ABC[N:N+4]});
271      $else:
272        const float32x4_t vscale = vld1q_dup_f32(&params->${PARAMS_STRUCT}.scale);
273        $for M in range(MR):
274          $for N in range(0, NR, 4):
275            vfpacc${M}x${ABC[N:N+4]} = vmulq_f32(vfpacc${M}x${ABC[N:N+4]}, vscale);
276
277      $if ARMV8:
278        $for M in range(MR):
279          $for N in range(0, NR, 4):
280            vacc${M}x${ABC[N:N+4]} = vcvtnq_s32_f32(vfpacc${M}x${ABC[N:N+4]});
281      $else:
282        const float32x4_t vmagic_bias = vld1q_dup_f32(&params->${PARAMS_STRUCT}.magic_bias);
283        $for M in range(MR):
284          $for N in range(0, NR, 4):
285            vacc${M}x${ABC[N:N+4]} = vreinterpretq_s32_f32(vaddq_f32(vfpacc${M}x${ABC[N:N+4]}, vmagic_bias));
286
287        const int32x4_t vmagic_bias_less_output_zero_point = vld1q_dup_s32(&params->${PARAMS_STRUCT}.magic_bias_less_output_zero_point);
288        $for M in range(MR):
289          $for N in range(0, NR, 4):
290            vacc${M}x${ABC[N:N+4]} = vqsubq_s32(vacc${M}x${ABC[N:N+4]}, vmagic_bias_less_output_zero_point);
291
292    $if REQUANTIZATION != "FP32" or ARMV8:
293      const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->${PARAMS_STRUCT}.output_zero_point);
294#if XNN_ARCH_ARM64
295    $for M in range(MR):
296      $for N in range(0, NR, 8):
297        int16x8_t vacc${M}x${ABC[N:N+8]} = vqmovn_high_s32(vqmovn_s32(vacc${M}x${ABC[N:N+4]}), vacc${M}x${ABC[N+4:N+8]});
298
299    $if REQUANTIZATION != "FP32" or ARMV8:
300      $for M in range(MR):
301        $for N in range(0, NR, 8):
302          vacc${M}x${ABC[N:N+8]} = vqaddq_s16(vacc${M}x${ABC[N:N+8]}, voutput_zero_point);
303
304    $for M in range(MR):
305      $for N in range(0, NR, 16):
306        $if N + 8 < NR:
307          ${XINT8X16_T} vout${M}x${ABC[N:N+16]} = ${VQMOVXN_HIGH_S16}(${VQMOVXN_S16}(vacc${M}x${ABC[N:N+8]}), vacc${M}x${ABC[N+8:N+16]});
308        $elif M % 2 == 1:
309          ${XINT8X16_T} vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]} = ${VQMOVXN_HIGH_S16}(${VQMOVXN_S16}(vacc${M-1}x${ABC[N:N+8]}), vacc${M}x${ABC[N:N+8]});
310        $elif M + 1 == MR:
311          ${XINT8X8_T} vout${M}x${ABC[N:N+8]} = ${VQMOVXN_S16}(vacc${M}x${ABC[N:N+8]});
312#else
313    $for M in range(MR):
314      $for N in range(0, NR, 8):
315        int16x8_t vacc${M}x${ABC[N:N+8]} = vcombine_s16(vqmovn_s32(vacc${M}x${ABC[N:N+4]}), vqmovn_s32(vacc${M}x${ABC[N+4:N+8]}));
316
317    $if REQUANTIZATION != "FP32" or ARMV8:
318      $for M in range(MR):
319        $for N in range(0, NR, 8):
320          vacc${M}x${ABC[N:N+8]} = vqaddq_s16(vacc${M}x${ABC[N:N+8]}, voutput_zero_point);
321
322    $for M in range(MR):
323      $for N in range(0, NR, 16):
324        $if N + 8 < NR:
325          ${XINT8X16_T} vout${M}x${ABC[N:N+16]} = ${VCOMBINE_X8}(${VQMOVXN_S16}(vacc${M}x${ABC[N:N+8]}), ${VQMOVXN_S16}(vacc${M}x${ABC[N+8:N+16]}));
326        $elif M % 2 == 1:
327          ${XINT8X16_T} vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]} = ${VCOMBINE_X8}(${VQMOVXN_S16}(vacc${M-1}x${ABC[N:N+8]}), ${VQMOVXN_S16}(vacc${M}x${ABC[N:N+8]}));
328        $elif M + 1 == MR:
329          ${XINT8X8_T} vout${M}x${ABC[N:N+8]} = ${VQMOVXN_S16}(vacc${M}x${ABC[N:N+8]});
330#endif
331
332    $if NR == 8 and MR == 1:
333      const ${XINT8X8_T} voutput_min = ${VLD1_DUP_X8}(&params->${PARAMS_STRUCT}.output_min);
334    $else:
335      const ${XINT8X16_T} voutput_min = ${VLD1Q_DUP_X8}(&params->${PARAMS_STRUCT}.output_min);
336    $for M in range(MR):
337      $for N in range(0, NR, 16):
338        $if N + 8 < NR:
339          vout${M}x${ABC[N:N+16]} = ${VMAXQ_X8}(vout${M}x${ABC[N:N+16]}, voutput_min);
340        $elif M % 2 == 1:
341          vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]} = ${VMAXQ_X8}(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}, voutput_min);
342        $elif M + 1 == MR:
343          $if NR == 8 and MR == 1:
344            vout${M}x${ABC[N:N+8]} = ${VMAX_X8}(vout${M}x${ABC[N:N+8]}, voutput_min);
345          $else:
346            vout${M}x${ABC[N:N+8]} = ${VMAX_X8}(vout${M}x${ABC[N:N+8]}, ${VGET_LOW_X8}(voutput_min));
347
348    $if NR == 8 and MR == 1:
349      const ${XINT8X8_T} voutput_max = ${VLD1_DUP_X8}(&params->${PARAMS_STRUCT}.output_max);
350    $else:
351      const ${XINT8X16_T} voutput_max = ${VLD1Q_DUP_X8}(&params->${PARAMS_STRUCT}.output_max);
352    $for M in range(MR):
353      $for N in range(0, NR, 16):
354        $if N + 8 < NR:
355          vout${M}x${ABC[N:N+16]} = ${VMINQ_X8}(vout${M}x${ABC[N:N+16]}, voutput_max);
356        $elif M % 2 == 1:
357          vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]} = ${VMINQ_X8}(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}, voutput_max);
358        $elif M + 1 == MR:
359          $if NR == 8 and MR == 1:
360            vout${M}x${ABC[N:N+8]} = ${VMIN_X8}(vout${M}x${ABC[N:N+8]}, voutput_max);
361          $else:
362            vout${M}x${ABC[N:N+8]} = ${VMIN_X8}(vout${M}x${ABC[N:N+8]}, ${VGET_LOW_X8}(voutput_max));
363
364    if (nc >= ${NR}) {
365      $for M in range(MR):
366        $for N in range(0, NR, 16):
367          $if N + 8 < NR:
368            ${VST1Q_X8}(c${M} + ${N}, vout${M}x${ABC[N:N+16]});
369          $elif M % 2 == 1:
370            ${VST1_X8}(c${M-1} + ${N}, ${VGET_LOW_X8}(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}));
371            ${VST1_X8}(c${M} + ${N}, ${VGET_HIGH_X8}(vout${M-1}x${ABC[N:N+8]}_${M}x${ABC[N:N+8]}));
372          $elif M + 1 == MR:
373            ${VST1_X8}(c${M} + ${N}, vout${M}x${ABC[N:N+8]});
374
375      $for M in range(MR):
376        c${M} = (${XINT8_T}*) ((uintptr_t) c${M} + cn_stride);
377
378      $for M in range(MR):
379        a${M} = (const ${XINT8_T}*) ((uintptr_t) a${M} - kc);
380
381      nc -= ${NR};
382    } else {
383      $if NR == 16:
384        $for M in range(MR):
385          $if M % 2 == 1:
386            ${XINT8X16_T} vout${M-1}x01234567_${M}x01234567 = ${VCOMBINE_X8}(${VGET_LOW_X8}(vout${M-1}x0123456789ABCDEF), ${VGET_LOW_X8}(vout${M}x0123456789ABCDEF));
387          $elif M + 1 == MR:
388            ${XINT8X8_T} vout${M}x01234567 = ${VGET_LOW_X8}(vout${M}x0123456789ABCDEF);
389        if (nc & 8) {
390          $for M in range(MR):
391            $if M % 2 == 1:
392              ${VST1_X8}(c${M-1}, ${VGET_LOW_X8}(vout${M-1}x01234567_${M}x01234567)); c${M-1} += 8;
393              ${VST1_X8}(c${M}, ${VGET_HIGH_X8}(vout${M-1}x01234567_${M}x01234567)); c${M} += 8;
394            $elif M + 1 == MR:
395              ${VST1_X8}(c${M}, vout${M}x01234567); c${M} += 8;
396          $for M in range(MR):
397            $if M % 2 == 1:
398              vout${M-1}x01234567_${M}x01234567 = ${VCOMBINE_X8}(${VGET_HIGH_X8}(vout${M-1}x0123456789ABCDEF), ${VGET_HIGH_X8}(vout${M}x0123456789ABCDEF));
399            $elif M + 1 == MR:
400              vout${M}x01234567 = ${VGET_HIGH_X8}(vout${M}x0123456789ABCDEF);
401        }
402      if (nc & 4) {
403        $for M in range(MR):
404          $if M % 2 == 1:
405            vst1q_lane_u32((void*) c${M-1}, ${VREINTERPRETQ_U32_X8}(vout${M-1}x01234567_${M}x01234567), 0); c${M-1} += 4;
406            vst1q_lane_u32((void*) c${M}, ${VREINTERPRETQ_U32_X8}(vout${M-1}x01234567_${M}x01234567), 2); c${M} += 4;
407          $elif M + 1 == MR:
408            vst1_lane_u32((void*) c${M}, ${VREINTERPRET_U32_X8}(vout${M}x01234567), 0); c${M} += 4;
409        $for M in range(MR):
410          $if M % 2 == 1:
411            vout${M-1}x01234567_${M}x01234567 = ${VEXTQ_X8}(vout${M-1}x01234567_${M}x01234567, vout${M-1}x01234567_${M}x01234567, 4);
412          $elif M + 1 == MR:
413            vout${M}x01234567 = ${VEXT_X8}(vout${M}x01234567, vout${M}x01234567, 4);
414      }
415      if (nc & 2) {
416        $for M in range(MR):
417          $if M % 2 == 1:
418            vst1q_lane_u16((void*) c${M-1}, ${VREINTERPRETQ_U16_X8}(vout${M-1}x01234567_${M}x01234567), 0); c${M-1} += 2;
419            vst1q_lane_u16((void*) c${M}, ${VREINTERPRETQ_U16_X8}(vout${M-1}x01234567_${M}x01234567), 4); c${M} += 2;
420          $elif M + 1 == MR:
421            vst1_lane_u16((void*) c${M}, ${VREINTERPRET_U16_X8}(vout${M}x01234567), 0); c${M} += 2;
422        $for M in range(MR):
423          $if M % 2 == 1:
424            vout${M-1}x01234567_${M}x01234567 = ${VEXTQ_X8}(vout${M-1}x01234567_${M}x01234567, vout${M-1}x01234567_${M}x01234567, 2);
425          $elif M + 1 == MR:
426            vout${M}x01234567 = ${VEXT_X8}(vout${M}x01234567, vout${M}x01234567, 2);
427      }
428      if (nc & 1) {
429        $for M in range(MR):
430          $if M % 2 == 1:
431            ${VST1Q_LANE_X8}(c${M-1}, vout${M-1}x01234567_${M}x01234567, 0);
432            ${VST1Q_LANE_X8}(c${M}, vout${M-1}x01234567_${M}x01234567, 8);
433          $elif M + 1 == MR:
434            ${VST1_LANE_X8}(c${M}, vout${M}x01234567, 0);
435      }
436
437      nc = 0;
438    }
439  } while (nc != 0);
440}
441