• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-igemm/MRx8c8-avx2.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 <immintrin.h>
13 
14 #include <xnnpack/igemm.h>
15 #include <xnnpack/intrinsics-polyfill.h>
16 #include <xnnpack/math.h>
17 
18 
xnn_qs8_igemm_minmax_ukernel_1x8c8__avx2(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_gemm_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qs8_igemm_minmax_ukernel_1x8c8__avx2(
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_gemm_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
32 {
33   assert(mr != 0);
34   assert(mr <= 1);
35   assert(nc != 0);
36   assert(kc != 0);
37   assert(ks != 0);
38   assert(ks % (1 * 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);
45   int8_t* c0 = c;
46 
47   do {
48     const __m128i vbias0x0 = _mm_loadu_si32(w);
49     const __m128i vbias0x1 = _mm_loadu_si32((const void*) ((uintptr_t) w + sizeof(int32_t)));
50     __m256i vacc0x01 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x0), vbias0x1, 1);
51     const __m128i vbias0x2 = _mm_loadu_si32((const void*) ((uintptr_t) w + 2 * sizeof(int32_t)));
52     const __m128i vbias0x3 = _mm_loadu_si32((const void*) ((uintptr_t) w + 3 * sizeof(int32_t)));
53     __m256i vacc0x23 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x2), vbias0x3, 1);
54     const __m128i vbias0x4 = _mm_loadu_si32((const void*) ((uintptr_t) w + 4 * sizeof(int32_t)));
55     const __m128i vbias0x5 = _mm_loadu_si32((const void*) ((uintptr_t) w + 5 * sizeof(int32_t)));
56     __m256i vacc0x45 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x4), vbias0x5, 1);
57     const __m128i vbias0x6 = _mm_loadu_si32((const void*) ((uintptr_t) w + 6 * sizeof(int32_t)));
58     const __m128i vbias0x7 = _mm_loadu_si32((const void*) ((uintptr_t) w + 7 * sizeof(int32_t)));
59     __m256i vacc0x67 = _mm256_inserti128_si256(_mm256_castsi128_si256(vbias0x6), vbias0x7, 1);
60     w = (const void*) ((uintptr_t) w + 8 * sizeof(int32_t));
61 
62     size_t p = ks;
63     do {
64       const int8_t* restrict a0 = a[0];
65       if XNN_UNPREDICTABLE(a0 != zero) {
66         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
67       }
68       a += 1;
69 
70       size_t k = 0;
71       while (k < kc) {
72         const __m128i va0 = _mm_broadcastq_epi64(_mm_loadl_epi64((const __m128i*) a0));
73         const __m256i vxa0 = _mm256_cvtepi8_epi16(va0);
74         a0 += 8;
75 
76         const __m128i vb01 = _mm_load_si128((const __m128i*) w);
77         const __m256i vxb01 = _mm256_cvtepi8_epi16(vb01);
78 
79         vacc0x01 = _mm256_add_epi32(vacc0x01, _mm256_madd_epi16(vxa0, vxb01));
80         const __m128i vb23 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 16 * sizeof(int8_t)));
81         const __m256i vxb23 = _mm256_cvtepi8_epi16(vb23);
82 
83         vacc0x23 = _mm256_add_epi32(vacc0x23, _mm256_madd_epi16(vxa0, vxb23));
84         const __m128i vb45 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 32 * sizeof(int8_t)));
85         const __m256i vxb45 = _mm256_cvtepi8_epi16(vb45);
86 
87         vacc0x45 = _mm256_add_epi32(vacc0x45, _mm256_madd_epi16(vxa0, vxb45));
88         const __m128i vb67 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 48 * sizeof(int8_t)));
89         const __m256i vxb67 = _mm256_cvtepi8_epi16(vb67);
90 
91         vacc0x67 = _mm256_add_epi32(vacc0x67, _mm256_madd_epi16(vxa0, vxb67));
92 
93         w = (const void*) ((uintptr_t) w + 64 * sizeof(int8_t));
94         k += 8 * sizeof(int8_t);
95       }
96       p -= 1 * sizeof(void*);
97     } while (p != 0);
98 
99     const __m256i vacc0x0213 = _mm256_hadd_epi32(vacc0x01, vacc0x23);
100     const __m256i vacc0x4657 = _mm256_hadd_epi32(vacc0x45, vacc0x67);
101 
102     const __m256i vacc0x02461357 = _mm256_hadd_epi32(vacc0x0213, vacc0x4657);
103 
104     const __m256i vpermute_mask = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
105     __m256i vacc0x01234567 = _mm256_permutevar8x32_epi32(vacc0x02461357, vpermute_mask);
106 
107     const __m256i vmultiplier = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.multiplier));
108     const __m256i vrounding = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.rounding));
109 
110     const __m256i vacc0x11335577 = _mm256_shuffle_epi32(vacc0x01234567, _MM_SHUFFLE(3, 3, 1, 1));
111 
112     const __m256i vprod0x0246 = _mm256_add_epi64(_mm256_mul_epi32(vacc0x01234567, vmultiplier), vrounding);
113 
114     const __m256i vprod0x1357 = _mm256_add_epi64(_mm256_mul_epi32(vacc0x11335577, vmultiplier), vrounding);
115 
116     const __m256i vq31prod0x0246 = _mm256_srli_epi64(vprod0x0246, 31);
117     const __m256i vq31prod0x1357 = _mm256_add_epi64(vprod0x1357, vprod0x1357);
118 
119     const __m256i vq31prod0x01234567 = _mm256_blend_epi16(vq31prod0x0246, vq31prod0x1357, 0xCC);
120 
121     const __m256i vremainder_mask = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.remainder_mask));
122     const __m256i vrem0x01234567 =
123       _mm256_add_epi32(_mm256_and_si256(vq31prod0x01234567, vremainder_mask), _mm256_cmpgt_epi32(_mm256_setzero_si256(), vq31prod0x01234567));
124 
125     const __m256i vremainder_threshold = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.remainder_threshold));
126     const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift);
127     vacc0x01234567 =
128       _mm256_sub_epi32(_mm256_sra_epi32(vq31prod0x01234567, vshift), _mm256_cmpgt_epi32(vrem0x01234567, vremainder_threshold));
129 
130     const __m256i voutput_zero_point = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_zero_point));
131     __m256i vacc00x01234567 = _mm256_adds_epi16(_mm256_packs_epi32(vacc0x01234567, vacc0x01234567), voutput_zero_point);
132 
133     vacc00x01234567 = _mm256_permute4x64_epi64(vacc00x01234567, _MM_SHUFFLE(3, 1, 2, 0));
134 
135     const __m256i voutput_min = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_min));
136     const __m256i voutput_max = _mm256_broadcastsi128_si256(_mm_load_si128((const __m128i*) params->sse2.output_max));
137     vacc00x01234567 = _mm256_min_epi16(_mm256_max_epi16(vacc00x01234567, voutput_min), voutput_max);
138 
139     __m256i vout = _mm256_packs_epi16(vacc00x01234567, vacc00x01234567);
140     __m128i vout_lo = _mm256_castsi256_si128(vout);
141     __m128i vout_hi = _mm256_extracti128_si256(vout, 1);
142 
143     if (nc >= 8) {
144       _mm_storel_epi64((__m128i*) c0, vout_lo);
145 
146       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
147 
148       a = (const int8_t**restrict) ((uintptr_t) a - ks);
149 
150       nc -= 8;
151     } else {
152       if (nc & 4) {
153         _mm_storeu_si32(c0, vout_lo);
154 
155         c0 += 4;
156 
157         vout_lo = _mm_srli_epi64(vout_lo, 32);
158         vout_hi = _mm_srli_epi64(vout_hi, 32);
159       }
160       if (nc & 2) {
161         *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout_lo, 0);
162 
163         c0 += 2;
164 
165         vout_lo = _mm_srli_epi32(vout_lo, 16);
166         vout_hi = _mm_srli_epi32(vout_hi, 16);
167       }
168       if (nc & 1) {
169         *c0 = (int8_t) _mm_extract_epi8(vout_lo, 0);
170       }
171 
172       nc = 0;
173     }
174   } while (nc != 0);
175 }
176