• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-igemm/MRx4c8-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 #include <xnnpack/unaligned.h>
17 
18 
xnn_qc8_igemm_minmax_fp32_ukernel_3x4c8__sse2_ld128(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_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])19 void xnn_qc8_igemm_minmax_fp32_ukernel_3x4c8__sse2_ld128(
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_qc8_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
32 {
33   assert(mr != 0);
34   assert(mr <= 3);
35   assert(nc != 0);
36   assert(kc != 0);
37   assert(ks != 0);
38   assert(ks % (3 * 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   int8_t* c1 = (int8_t*) ((uintptr_t) c0 + cm_stride);
47   if XNN_UNPREDICTABLE(mr < 2) {
48     c1 = c0;
49   }
50   int8_t* c2 = (int8_t*) ((uintptr_t) c1 + cm_stride);
51   if XNN_UNPREDICTABLE(mr <= 2) {
52     c2 = c1;
53   }
54 
55   do {
56     __m128i vacc0x0 = _mm_cvtsi32_si128(((const int*) w)[0]);
57     __m128i vacc0x1 = _mm_cvtsi32_si128(((const int*) w)[1]);
58     __m128i vacc0x2 = _mm_cvtsi32_si128(((const int*) w)[2]);
59     __m128i vacc0x3 = _mm_cvtsi32_si128(((const int*) w)[3]);
60     __m128i vacc1x0 = vacc0x0;
61     __m128i vacc1x1 = vacc0x1;
62     __m128i vacc1x2 = vacc0x2;
63     __m128i vacc1x3 = vacc0x3;
64     __m128i vacc2x0 = vacc0x0;
65     __m128i vacc2x1 = vacc0x1;
66     __m128i vacc2x2 = vacc0x2;
67     __m128i vacc2x3 = vacc0x3;
68     w = (const int32_t*) w + 4;
69 
70     size_t p = ks;
71     do {
72       const int8_t* restrict a0 = a[0];
73       if XNN_UNPREDICTABLE(a0 != zero) {
74         a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
75       }
76       const int8_t* restrict a1 = a[1];
77       if XNN_UNPREDICTABLE(a1 != zero) {
78         a1 = (const int8_t*) ((uintptr_t) a1 + a_offset);
79       }
80       const int8_t* restrict a2 = a[2];
81       if XNN_UNPREDICTABLE(a2 != zero) {
82         a2 = (const int8_t*) ((uintptr_t) a2 + a_offset);
83       }
84       a += 3;
85 
86       size_t k = 0;
87       while (k < kc) {
88         const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
89         const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
90         a0 += 8;
91         const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
92         const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
93         a1 += 8;
94         const __m128i va2 = _mm_loadl_epi64((const __m128i*) a2);
95         const __m128i vxa2 = _mm_srai_epi16(_mm_unpacklo_epi8(va2, va2), 8);
96         a2 += 8;
97 
98         const __m128i vb01 = _mm_load_si128((const __m128i*) w);
99         const __m128i vsb01 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb01);
100         const __m128i vxb0 = _mm_unpacklo_epi8(vb01, vsb01);
101         const __m128i vxb1 = _mm_unpackhi_epi8(vb01, vsb01);
102 
103         vacc0x0 = _mm_add_epi32(vacc0x0, _mm_madd_epi16(vxa0, vxb0));
104         vacc0x1 = _mm_add_epi32(vacc0x1, _mm_madd_epi16(vxa0, vxb1));
105         vacc1x0 = _mm_add_epi32(vacc1x0, _mm_madd_epi16(vxa1, vxb0));
106         vacc1x1 = _mm_add_epi32(vacc1x1, _mm_madd_epi16(vxa1, vxb1));
107         vacc2x0 = _mm_add_epi32(vacc2x0, _mm_madd_epi16(vxa2, vxb0));
108         vacc2x1 = _mm_add_epi32(vacc2x1, _mm_madd_epi16(vxa2, vxb1));
109         const __m128i vb23 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
110         const __m128i vsb23 = _mm_cmpgt_epi8(_mm_setzero_si128(), vb23);
111         const __m128i vxb2 = _mm_unpacklo_epi8(vb23, vsb23);
112         const __m128i vxb3 = _mm_unpackhi_epi8(vb23, vsb23);
113 
114         vacc0x2 = _mm_add_epi32(vacc0x2, _mm_madd_epi16(vxa0, vxb2));
115         vacc0x3 = _mm_add_epi32(vacc0x3, _mm_madd_epi16(vxa0, vxb3));
116         vacc1x2 = _mm_add_epi32(vacc1x2, _mm_madd_epi16(vxa1, vxb2));
117         vacc1x3 = _mm_add_epi32(vacc1x3, _mm_madd_epi16(vxa1, vxb3));
118         vacc2x2 = _mm_add_epi32(vacc2x2, _mm_madd_epi16(vxa2, vxb2));
119         vacc2x3 = _mm_add_epi32(vacc2x3, _mm_madd_epi16(vxa2, vxb3));
120 
121         w = (const void*) ((const int8_t*) w + 32);
122         k += 8 * sizeof(int8_t);
123       }
124       p -= 3 * sizeof(void*);
125     } while (p != 0);
126 
127     const __m128i vacc0x02 = _mm_add_epi32(_mm_unpacklo_epi32(vacc0x0, vacc0x2), _mm_unpackhi_epi32(vacc0x0, vacc0x2));
128     const __m128i vacc0x13 = _mm_add_epi32(_mm_unpacklo_epi32(vacc0x1, vacc0x3), _mm_unpackhi_epi32(vacc0x1, vacc0x3));
129     const __m128i vacc1x02 = _mm_add_epi32(_mm_unpacklo_epi32(vacc1x0, vacc1x2), _mm_unpackhi_epi32(vacc1x0, vacc1x2));
130     const __m128i vacc1x13 = _mm_add_epi32(_mm_unpacklo_epi32(vacc1x1, vacc1x3), _mm_unpackhi_epi32(vacc1x1, vacc1x3));
131     const __m128i vacc2x02 = _mm_add_epi32(_mm_unpacklo_epi32(vacc2x0, vacc2x2), _mm_unpackhi_epi32(vacc2x0, vacc2x2));
132     const __m128i vacc2x13 = _mm_add_epi32(_mm_unpacklo_epi32(vacc2x1, vacc2x3), _mm_unpackhi_epi32(vacc2x1, vacc2x3));
133 
134     __m128i vacc0x0123 = _mm_add_epi32(_mm_unpacklo_epi32(vacc0x02, vacc0x13), _mm_unpackhi_epi32(vacc0x02, vacc0x13));
135     __m128i vacc1x0123 = _mm_add_epi32(_mm_unpacklo_epi32(vacc1x02, vacc1x13), _mm_unpackhi_epi32(vacc1x02, vacc1x13));
136     __m128i vacc2x0123 = _mm_add_epi32(_mm_unpacklo_epi32(vacc2x02, vacc2x13), _mm_unpackhi_epi32(vacc2x02, vacc2x13));
137 
138     __m128 vscaled0x0123 = _mm_cvtepi32_ps(vacc0x0123);
139     __m128 vscaled1x0123 = _mm_cvtepi32_ps(vacc1x0123);
140     __m128 vscaled2x0123 = _mm_cvtepi32_ps(vacc2x0123);
141 
142     const __m128 vscale0123 = _mm_load_ps((const float*) w);
143     w = (const void*) ((const float*) w + 4);
144     vscaled0x0123 = _mm_mul_ps(vscaled0x0123, vscale0123);
145     vscaled1x0123 = _mm_mul_ps(vscaled1x0123, vscale0123);
146     vscaled2x0123 = _mm_mul_ps(vscaled2x0123, vscale0123);
147 
148     const __m128 voutput_max_less_zero_point = _mm_load_ps(params->fp32_sse2.output_max_less_zero_point);
149     vscaled0x0123 = _mm_min_ps(vscaled0x0123, voutput_max_less_zero_point);
150     vscaled1x0123 = _mm_min_ps(vscaled1x0123, voutput_max_less_zero_point);
151     vscaled2x0123 = _mm_min_ps(vscaled2x0123, voutput_max_less_zero_point);
152 
153     vacc0x0123 = _mm_cvtps_epi32(vscaled0x0123);
154     vacc1x0123 = _mm_cvtps_epi32(vscaled1x0123);
155     vacc2x0123 = _mm_cvtps_epi32(vscaled2x0123);
156 
157     const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->fp32_sse2.output_zero_point);
158     __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
159     __m128i vacc22x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc2x0123, vacc2x0123), voutput_zero_point);
160 
161     const __m128i voutput_min = _mm_load_si128((const __m128i*) params->fp32_sse2.output_min);
162     vacc01x0123 = _mm_max_epi16(vacc01x0123, voutput_min);
163     vacc22x0123 = _mm_max_epi16(vacc22x0123, voutput_min);
164 
165     __m128i vout = _mm_packs_epi16(vacc01x0123, vacc22x0123);
166 
167 
168     if (nc >= 4) {
169       unaligned_store_u32(c2, (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(2, 2, 2, 2))));
170       c2 = (int8_t*) ((uintptr_t) c2 + cn_stride);
171       unaligned_store_u32(c1, (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(1, 1, 1, 1))));
172       c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
173       unaligned_store_u32(c0, (uint32_t) _mm_cvtsi128_si32(vout));
174       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
175 
176       a = (const int8_t**restrict) ((uintptr_t) a - ks);
177 
178       nc -= 4;
179     } else {
180       if (nc & 2) {
181         unaligned_store_u16(c2, (uint16_t) _mm_extract_epi16(vout, 4));
182         c2 += 2;
183         unaligned_store_u16(c1, (uint16_t) _mm_extract_epi16(vout, 2));
184         c1 += 2;
185         unaligned_store_u16(c0, (uint16_t) _mm_extract_epi16(vout, 0));
186         c0 += 2;
187         vout = _mm_srli_epi32(vout, 16);
188       }
189       if (nc & 1) {
190         *c2 = (int8_t) _mm_extract_epi16(vout, 4);
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