• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-igemm/c4-neondot.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 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/igemm.h>
15 #include <xnnpack/math.h>
16 
17 
xnn_qs8_igemm_minmax_rndnu_ukernel_4x8c4__neondot(size_t mr,size_t nc,size_t kc,size_t ks,const int8_t ** restrict a,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const int8_t * zero,const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_qs8_igemm_minmax_rndnu_ukernel_4x8c4__neondot(
19     size_t mr,
20     size_t nc,
21     size_t kc,
22     size_t ks,
23     const int8_t** restrict a,
24     const void* restrict w,
25     int8_t* restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     size_t a_offset,
29     const int8_t* zero,
30     const union xnn_qs8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
31 {
32   assert(mr != 0);
33   assert(mr <= 4);
34   assert(nc != 0);
35   assert(kc != 0);
36   assert(ks != 0);
37   assert(ks % (4 * sizeof(void*)) == 0);
38   assert(a_offset % sizeof(int8_t) == 0);
39   assert(a != NULL);
40   assert(w != NULL);
41   assert(c != NULL);
42 
43   kc = round_up_po2(kc, 4 * sizeof(int8_t));
44   int8_t* c0 = c;
45   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
46   if XNN_UNPREDICTABLE(mr < 2) {
47     c1 = c0;
48   }
49   int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
50   if XNN_UNPREDICTABLE(mr <= 2) {
51     c2 = c1;
52   }
53   int8_t* c3 = (int8_t*) ((uintptr_t) c2 + cm_stride);
54   if XNN_UNPREDICTABLE(mr != 4) {
55     c3 = c2;
56   }
57 
58   do {
59     int32x4_t vacc0x0123 = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
60     int32x4_t vacc0x4567 = vld1q_s32(w); w = (const void*) ((const int32_t*) w + 4);
61     int32x4_t vacc1x0123 = vacc0x0123;
62     int32x4_t vacc1x4567 = vacc0x4567;
63     int32x4_t vacc2x0123 = vacc0x0123;
64     int32x4_t vacc2x4567 = vacc0x4567;
65     int32x4_t vacc3x0123 = vacc0x0123;
66     int32x4_t vacc3x4567 = vacc0x4567;
67 
68     size_t p = ks;
69     do {
70       const int8_t* restrict a0 = a[0];
71       if XNN_UNPREDICTABLE(a0 != zero) {
72         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
73       }
74       const int8_t* restrict a1 = a[1];
75       if XNN_UNPREDICTABLE(a1 != zero) {
76         a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
77       }
78       const int8_t* restrict a2 = a[2];
79       if XNN_UNPREDICTABLE(a2 != zero) {
80         a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
81       }
82       const int8_t* restrict a3 = a[3];
83       if XNN_UNPREDICTABLE(a3 != zero) {
84         a3 = (const int8_t*) ((uintptr_t) a3 + a_offset);
85       }
86       a += 4;
87 
88       // Inner accumulation loop along the 8 columns.
89       size_t k = kc;
90       // 2x partial unrolled loop to load 8 bytes at a time.
91       while (k >= 8 * sizeof(int8_t)) {
92         // Load a 4x8 block of activations.
93         const int8x8_t va0x01234567 = vld1_s8(a0); a0 += 8;
94         const int8x8_t va1x01234567 = vld1_s8(a1); a1 += 8;
95         const int8x8_t va2x01234567 = vld1_s8(a2); a2 += 8;
96         const int8x8_t va3x01234567 = vld1_s8(a3); a3 += 8;
97 
98         // Load a 8x8 block of weights.
99         const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
100         const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
101         const int8x16_t vb4567x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
102         const int8x16_t vb4567x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
103 
104         // Multiply-accumulate: 4x8 * 8x8 --> 4x8.
105         vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
106         vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
107         vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
108         vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
109         vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
110         vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
111         vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
112         vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
113         vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb4567x0123, va0x01234567, 1);
114         vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb4567x4567, va0x01234567, 1);
115         vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb4567x0123, va1x01234567, 1);
116         vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb4567x4567, va1x01234567, 1);
117         vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb4567x0123, va2x01234567, 1);
118         vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb4567x4567, va2x01234567, 1);
119         vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb4567x0123, va3x01234567, 1);
120         vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb4567x4567, va3x01234567, 1);
121 
122         k -= 8 * sizeof(int8_t);
123       }
124       // Handle up to 4 final positions of `k`
125       if XNN_UNLIKELY(k != 0) {
126         // Load a 4x4 block of activations.
127         const int8x8_t va0x01234567 = vld1_s8(a0);
128         const int8x8_t va1x01234567 = vld1_s8(a1);
129         const int8x8_t va2x01234567 = vld1_s8(a2);
130         const int8x8_t va3x01234567 = vld1_s8(a3);
131 
132         // Load a 4x8 block of weights.
133         const int8x16_t vb0123x0123 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
134         const int8x16_t vb0123x4567 = vld1q_s8(w); w = (const void*) ((const int8_t*) w + 16);
135 
136         // Multiply-accumulate: 4x4 * 4x8 --> 4x8.
137         vacc0x0123 = vdotq_lane_s32(vacc0x0123, vb0123x0123, va0x01234567, 0);
138         vacc0x4567 = vdotq_lane_s32(vacc0x4567, vb0123x4567, va0x01234567, 0);
139         vacc1x0123 = vdotq_lane_s32(vacc1x0123, vb0123x0123, va1x01234567, 0);
140         vacc1x4567 = vdotq_lane_s32(vacc1x4567, vb0123x4567, va1x01234567, 0);
141         vacc2x0123 = vdotq_lane_s32(vacc2x0123, vb0123x0123, va2x01234567, 0);
142         vacc2x4567 = vdotq_lane_s32(vacc2x4567, vb0123x4567, va2x01234567, 0);
143         vacc3x0123 = vdotq_lane_s32(vacc3x0123, vb0123x0123, va3x01234567, 0);
144         vacc3x4567 = vdotq_lane_s32(vacc3x4567, vb0123x4567, va3x01234567, 0);
145       }
146       p -= 4 * sizeof(void*);
147     } while (p != 0);
148 
149     const int32x4_t vright_pre_shift = vld1q_dup_s32(&params->rndnu_neon.right_pre_shift);
150     const int32x4_t vmultiplier = vld1q_dup_s32(&params->rndnu_neon.multiplier);
151     const int32x4_t vright_post_shift = vld1q_dup_s32(&params->rndnu_neon.right_post_shift);
152 
153     vacc0x0123 = vshlq_s32(vacc0x0123, vright_pre_shift);
154     vacc0x4567 = vshlq_s32(vacc0x4567, vright_pre_shift);
155     vacc1x0123 = vshlq_s32(vacc1x0123, vright_pre_shift);
156     vacc1x4567 = vshlq_s32(vacc1x4567, vright_pre_shift);
157     vacc2x0123 = vshlq_s32(vacc2x0123, vright_pre_shift);
158     vacc2x4567 = vshlq_s32(vacc2x4567, vright_pre_shift);
159     vacc3x0123 = vshlq_s32(vacc3x0123, vright_pre_shift);
160     vacc3x4567 = vshlq_s32(vacc3x4567, vright_pre_shift);
161 
162     vacc0x0123 = vqdmulhq_s32(vacc0x0123, vmultiplier);
163     vacc0x4567 = vqdmulhq_s32(vacc0x4567, vmultiplier);
164     vacc1x0123 = vqdmulhq_s32(vacc1x0123, vmultiplier);
165     vacc1x4567 = vqdmulhq_s32(vacc1x4567, vmultiplier);
166     vacc2x0123 = vqdmulhq_s32(vacc2x0123, vmultiplier);
167     vacc2x4567 = vqdmulhq_s32(vacc2x4567, vmultiplier);
168     vacc3x0123 = vqdmulhq_s32(vacc3x0123, vmultiplier);
169     vacc3x4567 = vqdmulhq_s32(vacc3x4567, vmultiplier);
170 
171     vacc0x0123 = vrshlq_s32(vacc0x0123, vright_post_shift);
172     vacc0x4567 = vrshlq_s32(vacc0x4567, vright_post_shift);
173     vacc1x0123 = vrshlq_s32(vacc1x0123, vright_post_shift);
174     vacc1x4567 = vrshlq_s32(vacc1x4567, vright_post_shift);
175     vacc2x0123 = vrshlq_s32(vacc2x0123, vright_post_shift);
176     vacc2x4567 = vrshlq_s32(vacc2x4567, vright_post_shift);
177     vacc3x0123 = vrshlq_s32(vacc3x0123, vright_post_shift);
178     vacc3x4567 = vrshlq_s32(vacc3x4567, vright_post_shift);
179 
180     const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->rndnu_neon.output_zero_point);
181 #if XNN_ARCH_ARM64
182     const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
183     const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
184     const int16x8_t vacc2x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc2x0123), vacc2x4567), voutput_zero_point);
185     const int16x8_t vacc3x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc3x0123), vacc3x4567), voutput_zero_point);
186 
187     int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
188     int8x16_t vout2x01234567_3x01234567 = vqmovn_high_s16(vqmovn_s16(vacc2x01234567), vacc3x01234567);
189 #else
190     const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
191     const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
192     const int16x8_t vacc2x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc2x0123), vqmovn_s32(vacc2x4567)), voutput_zero_point);
193     const int16x8_t vacc3x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc3x0123), vqmovn_s32(vacc3x4567)), voutput_zero_point);
194 
195     int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
196     int8x16_t vout2x01234567_3x01234567 = vcombine_s8(vqmovn_s16(vacc2x01234567), vqmovn_s16(vacc3x01234567));
197 #endif
198     const int8x16_t voutput_min = vld1q_dup_s8(&params->rndnu_neon.output_min);
199     const int8x16_t voutput_max = vld1q_dup_s8(&params->rndnu_neon.output_max);
200 
201     vout2x01234567_3x01234567 = vmaxq_s8(vout2x01234567_3x01234567, voutput_min);
202     vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
203 
204     vout2x01234567_3x01234567 = vminq_s8(vout2x01234567_3x01234567, voutput_max);
205     vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
206 
207     if (nc >= 8) {
208       vst1_s8(c3 + 0, vget_high_s8(vout2x01234567_3x01234567));
209       vst1_s8(c2 + 0, vget_low_s8(vout2x01234567_3x01234567));
210       vst1_s8(c1 + 0, vget_high_s8(vout0x01234567_1x01234567));
211       vst1_s8(c0 + 0, vget_low_s8(vout0x01234567_1x01234567));
212 
213       c3 = (int8_t*) ((uintptr_t) c3 + cn_stride);
214       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
215       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
216       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
217 
218       a = (const int8_t**restrict) ((uintptr_t) a - ks);
219 
220       nc -= 8;
221     } else {
222       if (nc & 4) {
223         vst1q_lane_u32((void*) c3, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 2); c3 += 4;
224         vst1q_lane_u32((void*) c2, vreinterpretq_u32_s8(vout2x01234567_3x01234567), 0); c2 += 4;
225         vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
226         vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
227         vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 4);
228         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
229       }
230       if (nc & 2) {
231         vst1q_lane_u16((void*) c3, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 4); c3 += 2;
232         vst1q_lane_u16((void*) c2, vreinterpretq_u16_s8(vout2x01234567_3x01234567), 0); c2 += 2;
233         vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
234         vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
235         vout2x01234567_3x01234567 = vextq_s8(vout2x01234567_3x01234567, vout2x01234567_3x01234567, 2);
236         vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
237       }
238       if (nc & 1) {
239         vst1q_lane_s8(c3, vout2x01234567_3x01234567, 8);
240         vst1q_lane_s8(c2, vout2x01234567_3x01234567, 0);
241         vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
242         vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
243       }
244 
245       nc = 0;
246     }
247   } while (nc != 0);
248 }
249