xref: /aosp_15_r20/external/XNNPACK/src/qs8-gemm/gen/1x16c2s4-minmax-rndnu-neon-mull.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-gemm/c2-neon-mull-shuffle.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2021 Google LLC
6 //
7 // This source code is licensed under the BSD-style license found in the
8 // LICENSE file in the root directory of this source tree.
9 
10 #include <assert.h>
11 
12 #include <arm_neon.h>
13 
14 #include <xnnpack/gemm.h>
15 #include <xnnpack/math.h>
16 
17 
xnn_qs8_gemm_minmax_rndnu_ukernel_1x16c2s4__neon_mull(size_t mr,size_t nc,size_t kc,const int8_t * restrict a,size_t a_stride,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qs8_gemm_minmax_rndnu_ukernel_1x16c2s4__neon_mull(
19     size_t mr,
20     size_t nc,
21     size_t kc,
22     const int8_t* restrict a,
23     size_t a_stride,
24     const void* restrict w,
25     int8_t* restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
29 {
30   assert(mr != 0);
31   assert(mr <= 1);
32   assert(nc != 0);
33   assert(kc != 0);
34   assert(kc % sizeof(int8_t) == 0);
35   assert(a != NULL);
36   assert(w != NULL);
37   assert(c != NULL);
38 
39   const int8_t* a0 = a;
40   int8_t* c0 = c;
41 
42   kc = round_up_po2(kc, 8 * sizeof(int8_t));
43   do {
44     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const int32_t*) w + 4;
45     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const int32_t*) w + 4;
46     int32x4_t vacc0x89AB = vld1q_s32(w); w = (const int32_t*) w + 4;
47     int32x4_t vacc0xCDEF = vld1q_s32(w); w = (const int32_t*) w + 4;
48 
49     size_t k = kc;
50     do {
51       int8x8_t va0x0 = vld1_s8(a0); a0 += 8;
52 
53       const int8x8_t vb0123c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
54       const int8x8_t vb4567c0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
55       const int8x8_t vb89ABc0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
56       const int8x8_t vbCDEFc0x0 = vld1_s8(w); w = (const int8_t*) w + 8;
57       const int8x8_t vb0123c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
58       const int8x8_t vb4567c1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
59       const int8x8_t vb89ABc1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
60       const int8x8_t vbCDEFc1x0 = vld1_s8(w); w = (const int8_t*) w + 8;
61       const int8x8_t vb0123c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
62       const int8x8_t vb4567c2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
63       const int8x8_t vb89ABc2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
64       const int8x8_t vbCDEFc2x0 = vld1_s8(w); w = (const int8_t*) w + 8;
65       const int8x8_t vb0123c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
66       const int8x8_t vb4567c3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
67       const int8x8_t vb89ABc3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
68       const int8x8_t vbCDEFc3x0 = vld1_s8(w); w = (const int8_t*) w + 8;
69 
70       int16x8_t vprod0x0123c0 = vmull_s8(vb0123c0x0, va0x0);
71       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c0);
72       int16x8_t vprod0x4567c0 = vmull_s8(vb4567c0x0, va0x0);
73       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c0);
74       int16x8_t vprod0x89ABc0 = vmull_s8(vb89ABc0x0, va0x0);
75       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc0);
76       int16x8_t vprod0xCDEFc0 = vmull_s8(vbCDEFc0x0, va0x0);
77       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc0);
78       va0x0 = vext_s8(va0x0, va0x0, 2);
79       int16x8_t vprod0x0123c1 = vmull_s8(vb0123c1x0, va0x0);
80       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c1);
81       int16x8_t vprod0x4567c1 = vmull_s8(vb4567c1x0, va0x0);
82       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c1);
83       int16x8_t vprod0x89ABc1 = vmull_s8(vb89ABc1x0, va0x0);
84       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc1);
85       int16x8_t vprod0xCDEFc1 = vmull_s8(vbCDEFc1x0, va0x0);
86       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc1);
87       va0x0 = vext_s8(va0x0, va0x0, 2);
88       int16x8_t vprod0x0123c2 = vmull_s8(vb0123c2x0, va0x0);
89       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c2);
90       int16x8_t vprod0x4567c2 = vmull_s8(vb4567c2x0, va0x0);
91       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c2);
92       int16x8_t vprod0x89ABc2 = vmull_s8(vb89ABc2x0, va0x0);
93       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc2);
94       int16x8_t vprod0xCDEFc2 = vmull_s8(vbCDEFc2x0, va0x0);
95       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc2);
96       va0x0 = vext_s8(va0x0, va0x0, 2);
97       int16x8_t vprod0x0123c3 = vmull_s8(vb0123c3x0, va0x0);
98       vacc0x0123 = vpadalq_s16(vacc0x0123, vprod0x0123c3);
99       int16x8_t vprod0x4567c3 = vmull_s8(vb4567c3x0, va0x0);
100       vacc0x4567 = vpadalq_s16(vacc0x4567, vprod0x4567c3);
101       int16x8_t vprod0x89ABc3 = vmull_s8(vb89ABc3x0, va0x0);
102       vacc0x89AB = vpadalq_s16(vacc0x89AB, vprod0x89ABc3);
103       int16x8_t vprod0xCDEFc3 = vmull_s8(vbCDEFc3x0, va0x0);
104       vacc0xCDEF = vpadalq_s16(vacc0xCDEF, vprod0xCDEFc3);
105 
106       k -= 8 * sizeof(int8_t);
107     } while (k != 0);
108 
109     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
110     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
111     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
112 
113     vacc0x0123 = vqshlq_s32(vacc0x0123, vright_pre_shift);
114     vacc0x4567 = vqshlq_s32(vacc0x4567, vright_pre_shift);
115     vacc0x89AB = vqshlq_s32(vacc0x89AB, vright_pre_shift);
116     vacc0xCDEF = vqshlq_s32(vacc0xCDEF, vright_pre_shift);
117 
118     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
119     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
120     vacc0x89AB = vqdmulhq_s32(vacc0x89AB, vmultiplier);
121     vacc0xCDEF = vqdmulhq_s32(vacc0xCDEF, vmultiplier);
122 
123     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
124     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
125     vacc0x89AB = vrshlq_s32(vacc0x89AB, vright_post_shift);
126     vacc0xCDEF = vrshlq_s32(vacc0xCDEF, vright_post_shift);
127 
128     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
129 #if XNN_ARCH_ARM64
130     int16x8_t vacc0x01234567 = vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567);
131     int16x8_t vacc0x89ABCDEF = vqmovn_high_s32(vqmovn_s32(vacc0x89AB), vacc0xCDEF);
132 
133     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
134     vacc0x89ABCDEF = vqaddq_s16(vacc0x89ABCDEF, voutput_zero_point);
135 
136     int8x16_t vout0x0123456789ABCDEF = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc0x89ABCDEF);
137 #else
138     int16x8_t vacc0x01234567 = vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567));
139     int16x8_t vacc0x89ABCDEF = vcombine_s16(vqmovn_s32(vacc0x89AB), vqmovn_s32(vacc0xCDEF));
140 
141     vacc0x01234567 = vqaddq_s16(vacc0x01234567, voutput_zero_point);
142     vacc0x89ABCDEF = vqaddq_s16(vacc0x89ABCDEF, voutput_zero_point);
143 
144     int8x16_t vout0x0123456789ABCDEF = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc0x89ABCDEF));
145 #endif
146 
147     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
148     vout0x0123456789ABCDEF = vmaxq_s8(vout0x0123456789ABCDEF, voutput_min);
149 
150     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
151     vout0x0123456789ABCDEF = vminq_s8(vout0x0123456789ABCDEF, voutput_max);
152 
153     if (nc >= 16) {
154       vst1q_s8(c0 + 0, vout0x0123456789ABCDEF);
155 
156       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
157 
158       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
159 
160       nc -= 16;
161     } else {
162       // Final case where not all of the 16 columns fit in the destination.
163       int8x8_t vout0x01234567 = vget_low_s8(vout0x0123456789ABCDEF);
164       if (nc & 8) {
165         vst1_s8(c0, vout0x01234567); c0 += 8;
166         vout0x01234567 = vget_high_s8(vout0x0123456789ABCDEF);
167       }
168       if (nc & 4) {
169         vst1_lane_u32((void*) c0, vreinterpret_u32_s8(vout0x01234567), 0); c0 += 4;
170         vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 4);
171       }
172       if (nc & 2) {
173         vst1_lane_u16((void*) c0, vreinterpret_u16_s8(vout0x01234567), 0); c0 += 2;
174         vout0x01234567 = vext_s8(vout0x01234567, vout0x01234567, 2);
175       }
176       if (nc & 1) {
177         vst1_lane_s8(c0, vout0x01234567, 0);
178       }
179 
180       nc = 0;
181     }
182   } while (nc != 0);
183 }
184