Debian Codegraph @

src/qs8-qc8w-igemm/gen/qs8-qc8w-igemm-2x8c8-minmax-fp32-neoni8mm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/c8-neoni8mm.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2023 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/intrinsics-polyfill.h"
16 #include "xnnpack/math.h"
17
18
19 void xnn_qs8_qc8w_igemm_minmax_fp32_ukernel_2x8c8__neoni8mm(
20 size_t mr,
21 size_t nc,
22 size_t kc,
23 size_t ks,
24 const int8_t** restrict a,
25 const void* restrict w,
26 int8_t* restrict c,
27 size_t cm_stride,
28 size_t cn_stride,
29 size_t a_offset,
30 const int8_t* zero,
31 const union xnn_qs8_qc8w_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
32 {
33 assert(mr != 0);
34 assert(mr <= 2);
35 assert(nc != 0);
36 assert(kc != 0);
37 assert(ks != 0);
38 assert(ks % (2 * sizeof(void*)) == 0);
39 assert(a_offset % sizeof(int8_t) == 0);
40 assert(a != NULL);
41 assert(w != NULL);
42 assert(c != NULL);
43
44 kc = round_up_po2(kc, 8 * sizeof(int8_t));
45 int8_t* c0 = c;
46 int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
47 if XNN_UNPREDICTABLE(mr != 2) {
48 c1 = c0;
49 }
50
51 do {
52 const uint64x2x2_t vbias01x0123 = vld2q_dup_u64(w); w = (const int32_t*) w + 4;
53 const uint64x2x2_t vbias01x4567 = vld2q_dup_u64(w); w = (const int32_t*) w + 4;
54 int32x4_t vacc01x01 = vreinterpretq_s32_u64(vbias01x0123.val[0]);
55 int32x4_t vacc01x23 = vreinterpretq_s32_u64(vbias01x0123.val[1]);
56 int32x4_t vacc01x45 = vreinterpretq_s32_u64(vbias01x4567.val[0]);
57 int32x4_t vacc01x67 = vreinterpretq_s32_u64(vbias01x4567.val[1]);
58
59 size_t p = ks;
60 do {
61 const int8_t* restrict a0 = a[0];
62 if XNN_UNPREDICTABLE(a0 != zero) {
63 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
64 }
65 const int8_t* restrict a1 = a[1];
66 if XNN_UNPREDICTABLE(a1 != zero) {
67 a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
68 }
69 a += 2;
70
71 uint64x2x2_t va01x0123456789ABCDEF;
72 va01x0123456789ABCDEF.val[0] = vdupq_n_u64(0);
73 va01x0123456789ABCDEF.val[1] = vdupq_n_u64(0);
74
75 // Inner accumulation loop along the 8 columns.
76 size_t k = kc;
77 // 2x partial unrolled loop to load 8 bytes at a time.
78 while (k >= 16 * sizeof(int8_t)) {
79 // Load a 2x16 block of activations.
80 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a0, va01x0123456789ABCDEF, 0); a0 += 16;
81 va01x0123456789ABCDEF = vld2q_lane_u64((const void*) a1, va01x0123456789ABCDEF, 1); a1 += 16;
82
83 // Load a 16x8 block of weights.
84 const int8x16_t vb01x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
85 const int8x16_t vb23x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
86 const int8x16_t vb45x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
87 const int8x16_t vb67x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
88 const int8x16_t vb01x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
89 const int8x16_t vb23x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
90 const int8x16_t vb45x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
91 const int8x16_t vb67x89ABCDEF = vld1q_s8(w); w = (const int8_t*) w + 16;
92
93 // Multiply-accumulate: 2x16 * 16x8 --> 2x8.
94 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb01x01234567);
95 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb23x01234567);
96 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb45x01234567);
97 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[0]), vb67x01234567);
98 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb01x89ABCDEF);
99 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb23x89ABCDEF);
100 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb45x89ABCDEF);
101 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x0123456789ABCDEF.val[1]), vb67x89ABCDEF);
102
103 k -= 16 * sizeof(int8_t);
104 }
105 // Handle up to 8 final positions of `k`
106 if XNN_UNLIKELY(k != 0) {
107 // Load a 2x8 block of activations.
108 uint64x2_t va01x01234567 = vld1q_dup_u64((const void*) a0); a0 += 8;
109 va01x01234567 = vld1q_lane_u64((const void*) a1, va01x01234567, 1); a1 += 8;
110
111 // Load a 16x8 block of weights.
112 const int8x16_t vb01x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
113 const int8x16_t vb23x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
114 const int8x16_t vb45x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
115 const int8x16_t vb67x01234567 = vld1q_s8(w); w = (const int8_t*) w + 16;
116
117 // Multiply-accumulate: 2x4 * 4x8 --> 2x8.
118 vacc01x01 = vmmlaq_s32(vacc01x01, vreinterpretq_s8_u64(va01x01234567), vb01x01234567);
119 vacc01x23 = vmmlaq_s32(vacc01x23, vreinterpretq_s8_u64(va01x01234567), vb23x01234567);
120 vacc01x45 = vmmlaq_s32(vacc01x45, vreinterpretq_s8_u64(va01x01234567), vb45x01234567);
121 vacc01x67 = vmmlaq_s32(vacc01x67, vreinterpretq_s8_u64(va01x01234567), vb67x01234567);
122 }
123
124 p -= 2 * sizeof(void*);
125 } while (p != 0);
126
127 int32x4_t vacc0x0123 = vcombine_s32(vget_low_s32(vacc01x01), vget_low_s32(vacc01x23));
128 int32x4_t vacc1x0123 = vcombine_s32(vget_high_s32(vacc01x01), vget_high_s32(vacc01x23));
129 int32x4_t vacc0x4567 = vcombine_s32(vget_low_s32(vacc01x45), vget_low_s32(vacc01x67));
130 int32x4_t vacc1x4567 = vcombine_s32(vget_high_s32(vacc01x45), vget_high_s32(vacc01x67));
131
132 float32x4_t vfpacc0x0123 = vcvtq_f32_s32(vacc0x0123);
133 float32x4_t vfpacc0x4567 = vcvtq_f32_s32(vacc0x4567);
134 float32x4_t vfpacc1x0123 = vcvtq_f32_s32(vacc1x0123);
135 float32x4_t vfpacc1x4567 = vcvtq_f32_s32(vacc1x4567);
136
137 const float32x4_t vscale0123 = vld1q_f32((const float*) w); w = (const float*) w + 4;
138 vfpacc0x0123 = vmulq_f32(vfpacc0x0123, vscale0123);
139 vfpacc1x0123 = vmulq_f32(vfpacc1x0123, vscale0123);
140 const float32x4_t vscale4567 = vld1q_f32((const float*) w); w = (const float*) w + 4;
141 vfpacc0x4567 = vmulq_f32(vfpacc0x4567, vscale4567);
142 vfpacc1x4567 = vmulq_f32(vfpacc1x4567, vscale4567);
143
144 vacc0x0123 = vcvtnq_s32_f32(vfpacc0x0123);
145 vacc0x4567 = vcvtnq_s32_f32(vfpacc0x4567);
146 vacc1x0123 = vcvtnq_s32_f32(vfpacc1x0123);
147 vacc1x4567 = vcvtnq_s32_f32(vfpacc1x4567);
148
149 const int16x8_t voutput_zero_point = vld1q_dup_s16(&params->fp32_neonv8.output_zero_point);
150 #if XNN_ARCH_ARM64
151 const int16x8_t vacc0x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc0x0123), vacc0x4567), voutput_zero_point);
152 const int16x8_t vacc1x01234567 = vqaddq_s16(vqmovn_high_s32(vqmovn_s32(vacc1x0123), vacc1x4567), voutput_zero_point);
153
154 int8x16_t vout0x01234567_1x01234567 = vqmovn_high_s16(vqmovn_s16(vacc0x01234567), vacc1x01234567);
155 #else
156 const int16x8_t vacc0x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc0x0123), vqmovn_s32(vacc0x4567)), voutput_zero_point);
157 const int16x8_t vacc1x01234567 = vqaddq_s16(vcombine_s16(vqmovn_s32(vacc1x0123), vqmovn_s32(vacc1x4567)), voutput_zero_point);
158
159 int8x16_t vout0x01234567_1x01234567 = vcombine_s8(vqmovn_s16(vacc0x01234567), vqmovn_s16(vacc1x01234567));
160 #endif
161 const int8x16_t voutput_min = vld1q_dup_s8(&params->fp32_neonv8.output_min);
162 const int8x16_t voutput_max = vld1q_dup_s8(&params->fp32_neonv8.output_max);
163
164 vout0x01234567_1x01234567 = vmaxq_s8(vout0x01234567_1x01234567, voutput_min);
165
166 vout0x01234567_1x01234567 = vminq_s8(vout0x01234567_1x01234567, voutput_max);
167
168 if (nc >= 8) {
169 vst1_s8(c1, vget_high_s8(vout0x01234567_1x01234567));
170 vst1_s8(c0, vget_low_s8(vout0x01234567_1x01234567));
171
172 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
173 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
174
175 a = (const int8_t**restrict) ((uintptr_t) a - ks);
176
177 nc -= 8;
178 } else {
179 if (nc & 4) {
180 vst1q_lane_u32((void*) c1, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 2); c1 += 4;
181 vst1q_lane_u32((void*) c0, vreinterpretq_u32_s8(vout0x01234567_1x01234567), 0); c0 += 4;
182 vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 4);
183 }
184 if (nc & 2) {
185 vst1q_lane_u16((void*) c1, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 4); c1 += 2;
186 vst1q_lane_u16((void*) c0, vreinterpretq_u16_s8(vout0x01234567_1x01234567), 0); c0 += 2;
187 vout0x01234567_1x01234567 = vextq_s8(vout0x01234567_1x01234567, vout0x01234567_1x01234567, 2);
188 }
189 if (nc & 1) {
190 vst1q_lane_s8(c1, vout0x01234567_1x01234567, 8);
191 vst1q_lane_s8(c0, vout0x01234567_1x01234567, 0);
192 }
193
194 nc = 0;
195 }
196 } while (nc != 0);
197 }