• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-igemm/MRx4c2-sse.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 <emmintrin.h>
13 
14 #include <xnnpack/igemm.h>
15 #include <xnnpack/math.h>
16 
17 
xnn_qs8_igemm_minmax_fp32_ukernel_2x4c2__sse2_ld64(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_fp32_ukernel_2x4c2__sse2_ld64(
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 <= 2);
34   assert(nc != 0);
35   assert(kc != 0);
36   assert(ks != 0);
37   assert(ks % (2 * 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, 2);
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 
50   do {
51     __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w);
52     __m128i vacc1x0123 = vacc0x0123;
53     w = (const void*) ((const int32_t*) w + 4);
54 
55     size_t p = ks;
56     do {
57       const int8_t* restrict a0 = a[0];
58       if XNN_UNPREDICTABLE(a0 != zero) {
59         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
60       }
61       const int8_t* restrict a1 = a[1];
62       if XNN_UNPREDICTABLE(a1 != zero) {
63         a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
64       }
65       a += 2;
66 
67       size_t k = kc;
68       while (k >= 8 * sizeof(int8_t)) {
69         const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
70         const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
71         a0 += 8;
72         const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
73         const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
74         a1 += 8;
75 
76         const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
77         const __m128i vxb0 = _mm_srai_epi16(_mm_unpacklo_epi8(vb0, vb0), 8);
78 
79         vacc0x0123 = _mm_add_epi32(vacc0x0123,
80           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
81         vacc1x0123 = _mm_add_epi32(vacc1x0123,
82           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
83         const __m128i vb1 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 8));
84         const __m128i vxb1 = _mm_srai_epi16(_mm_unpacklo_epi8(vb1, vb1), 8);
85 
86         vacc0x0123 = _mm_add_epi32(vacc0x0123,
87           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
88         vacc1x0123 = _mm_add_epi32(vacc1x0123,
89           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
90         const __m128i vb2 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 16));
91         const __m128i vxb2 = _mm_srai_epi16(_mm_unpacklo_epi8(vb2, vb2), 8);
92 
93         vacc0x0123 = _mm_add_epi32(vacc0x0123,
94           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
95         vacc1x0123 = _mm_add_epi32(vacc1x0123,
96           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
97         const __m128i vb3 = _mm_loadl_epi64((const __m128i*) ((const int8_t*) w + 24));
98         const __m128i vxb3 = _mm_srai_epi16(_mm_unpacklo_epi8(vb3, vb3), 8);
99 
100         vacc0x0123 = _mm_add_epi32(vacc0x0123,
101           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
102         vacc1x0123 = _mm_add_epi32(vacc1x0123,
103           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(3, 3, 3, 3)), vxb3));
104 
105         w = (const void*) ((const int8_t*) w + 32);
106         k -= 8 * sizeof(int8_t);
107       }
108       if (k != 0) {
109         const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
110         const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
111         a0 = (const int8_t*) ((uintptr_t) a0 + k);
112         const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
113         const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
114         a1 = (const int8_t*) ((uintptr_t) a1 + k);
115 
116         const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
117         w = (const void*) ((const int8_t*) w + 8);
118         const __m128i vxb0 = _mm_srai_epi16(_mm_unpacklo_epi8(vb0, vb0), 8);
119 
120         vacc0x0123 = _mm_add_epi32(vacc0x0123,
121           _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
122         vacc1x0123 = _mm_add_epi32(vacc1x0123,
123           _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(0, 0, 0, 0)), vxb0));
124 
125         if (k > 2 * sizeof(int8_t)) {
126           const __m128i vb1 = _mm_loadl_epi64((const __m128i*) w);
127           w = (const void*) ((const int8_t*) w + 8);
128           const __m128i vxb1 = _mm_srai_epi16(_mm_unpacklo_epi8(vb1, vb1), 8);
129 
130           vacc0x0123 = _mm_add_epi32(vacc0x0123,
131             _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
132           vacc1x0123 = _mm_add_epi32(vacc1x0123,
133             _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(1, 1, 1, 1)), vxb1));
134 
135           if (k > 4 * sizeof(int8_t)) {
136             const __m128i vb2 = _mm_loadl_epi64((const __m128i*) w);
137             w = (const void*) ((const int8_t*) w + 8);
138             const __m128i vxb2 = _mm_srai_epi16(_mm_unpacklo_epi8(vb2, vb2), 8);
139 
140             vacc0x0123 = _mm_add_epi32(vacc0x0123,
141               _mm_madd_epi16(_mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
142             vacc1x0123 = _mm_add_epi32(vacc1x0123,
143               _mm_madd_epi16(_mm_shuffle_epi32(vxa1, _MM_SHUFFLE(2, 2, 2, 2)), vxb2));
144           }
145         }
146       }
147       p -= 2 * sizeof(void*);
148     } while (p != 0);
149 
150     __m128 vscaled0x0123 = _mm_cvtepi32_ps(vacc0x0123);
151     __m128 vscaled1x0123 = _mm_cvtepi32_ps(vacc1x0123);
152 
153     const __m128 vscale = _mm_load_ps(params->fp32_sse2.scale);
154     vscaled0x0123 = _mm_mul_ps(vscaled0x0123, vscale);
155     vscaled1x0123 = _mm_mul_ps(vscaled1x0123, vscale);
156 
157     const __m128 voutput_max_less_zero_point = _mm_load_ps(params->fp32_sse2.output_max_less_zero_point);
158     vscaled0x0123 = _mm_min_ps(vscaled0x0123, voutput_max_less_zero_point);
159     vscaled1x0123 = _mm_min_ps(vscaled1x0123, voutput_max_less_zero_point);
160 
161     vacc0x0123 = _mm_cvtps_epi32(vscaled0x0123);
162     vacc1x0123 = _mm_cvtps_epi32(vscaled1x0123);
163 
164     const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->fp32_sse2.output_zero_point);
165     __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
166 
167     const __m128i voutput_min = _mm_load_si128((const __m128i*) params->fp32_sse2.output_min);
168     vacc01x0123 = _mm_max_epi16(vacc01x0123, voutput_min);
169 
170     __m128i vout = _mm_packs_epi16(vacc01x0123, vacc01x0123);
171 
172 
173     if (nc >= 4) {
174       *((uint32_t*) c1) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(1, 1, 1, 1)));
175       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
176       *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
177       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
178 
179       a = (const int8_t**restrict) ((uintptr_t) a - ks);
180 
181       nc -= 4;
182     } else {
183       if (nc & 2) {
184         *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2);
185         c1 += 2;
186         *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
187         c0 += 2;
188         vout = _mm_srli_epi32(vout, 16);
189       }
190       if (nc & 1) {
191         *c1 = (int8_t) _mm_extract_epi16(vout, 2);
192         *c0 = (int8_t) _mm_cvtsi128_si32(vout);
193       }
194 
195       nc = 0;
196     }
197   } while (nc != 0);
198 }
199