blob: 6c6dbd7d3f09bc03c5eed3cb6208901f0efcc613 [file] [log] [blame]
Marat Dukhanfc188ed2021-06-03 12:21:22 -07001// 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
18void xnn_qc8_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_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN XNN_DISABLE_MSAN
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;
Marat Dukhane5eee462021-07-01 19:34:39 -070053 w = (const void*) ((const int32_t*) w + 4);
Marat Dukhanfc188ed2021-06-03 12:21:22 -070054
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);
Marat Dukhane5eee462021-07-01 19:34:39 -070070 const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
Marat Dukhanfc188ed2021-06-03 12:21:22 -070071 a0 += 8;
72 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
Marat Dukhane5eee462021-07-01 19:34:39 -070073 const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
Marat Dukhanfc188ed2021-06-03 12:21:22 -070074 a1 += 8;
75
76 const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
Marat Dukhane5eee462021-07-01 19:34:39 -070077 const __m128i vxb0 = _mm_srai_epi16(_mm_unpacklo_epi8(vb0, vb0), 8);
Marat Dukhanfc188ed2021-06-03 12:21:22 -070078
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));
Marat Dukhane5eee462021-07-01 19:34:39 -070083 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);
Marat Dukhanfc188ed2021-06-03 12:21:22 -070085
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));
Marat Dukhane5eee462021-07-01 19:34:39 -070090 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);
Marat Dukhanfc188ed2021-06-03 12:21:22 -070092
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));
Marat Dukhane5eee462021-07-01 19:34:39 -070097 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);
Marat Dukhanfc188ed2021-06-03 12:21:22 -070099
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
Marat Dukhane5eee462021-07-01 19:34:39 -0700105 w = (const void*) ((const int8_t*) w + 32);
Marat Dukhanfc188ed2021-06-03 12:21:22 -0700106 k -= 8 * sizeof(int8_t);
107 }
108 if (k != 0) {
109 const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
Marat Dukhane5eee462021-07-01 19:34:39 -0700110 const __m128i vxa0 = _mm_srai_epi16(_mm_unpacklo_epi8(va0, va0), 8);
Marat Dukhanfc188ed2021-06-03 12:21:22 -0700111 a0 = (const int8_t*) ((uintptr_t) a0 + k);
112 const __m128i va1 = _mm_loadl_epi64((const __m128i*) a1);
Marat Dukhane5eee462021-07-01 19:34:39 -0700113 const __m128i vxa1 = _mm_srai_epi16(_mm_unpacklo_epi8(va1, va1), 8);
Marat Dukhanfc188ed2021-06-03 12:21:22 -0700114 a1 = (const int8_t*) ((uintptr_t) a1 + k);
115
116 const __m128i vb0 = _mm_loadl_epi64((const __m128i*) w);
Marat Dukhane5eee462021-07-01 19:34:39 -0700117 w = (const void*) ((const int8_t*) w + 8);
118 const __m128i vxb0 = _mm_srai_epi16(_mm_unpacklo_epi8(vb0, vb0), 8);
Marat Dukhanfc188ed2021-06-03 12:21:22 -0700119
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);
Marat Dukhane5eee462021-07-01 19:34:39 -0700127 w = (const void*) ((const int8_t*) w + 8);
128 const __m128i vxb1 = _mm_srai_epi16(_mm_unpacklo_epi8(vb1, vb1), 8);
Marat Dukhanfc188ed2021-06-03 12:21:22 -0700129
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);
Marat Dukhane5eee462021-07-01 19:34:39 -0700137 w = (const void*) ((const int8_t*) w + 8);
138 const __m128i vxb2 = _mm_srai_epi16(_mm_unpacklo_epi8(vb2, vb2), 8);
Marat Dukhanfc188ed2021-06-03 12:21:22 -0700139
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 vscale0123 = _mm_loadu_ps((const float*) w);
Marat Dukhane5eee462021-07-01 19:34:39 -0700154 w = (const void*) ((const float*) w + 4);
Marat Dukhanfc188ed2021-06-03 12:21:22 -0700155 vscaled0x0123 = _mm_mul_ps(vscaled0x0123, vscale0123);
156 vscaled1x0123 = _mm_mul_ps(vscaled1x0123, vscale0123);
157
158 vacc0x0123 = _mm_cvtps_epi32(vscaled0x0123);
159 vacc1x0123 = _mm_cvtps_epi32(vscaled1x0123);
160
161 const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
162 __m128i vacc01x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc1x0123), voutput_zero_point);
163
164 const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
165 const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
166 vacc01x0123 = _mm_min_epi16(_mm_max_epi16(vacc01x0123, voutput_min), voutput_max);
167
168 __m128i vout = _mm_packs_epi16(vacc01x0123, vacc01x0123);
169
170
171 if (nc >= 4) {
172 *((uint32_t*) c1) = (uint32_t) _mm_cvtsi128_si32(_mm_shuffle_epi32(vout, _MM_SHUFFLE(1, 1, 1, 1)));
173 c1 = (int8_t*) ((uintptr_t) c1 + cn_stride);
174 *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
175 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
176
177 a = (const int8_t**restrict) ((uintptr_t) a - ks);
178
179 nc -= 4;
180 } else {
181 if (nc & 2) {
182 *((uint16_t*) c1) = (uint16_t) _mm_extract_epi16(vout, 2);
183 c1 += 2;
184 *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
185 c0 += 2;
186 vout = _mm_srli_epi32(vout, 16);
187 }
188 if (nc & 1) {
Marat Dukhancdbe9a32021-07-01 23:52:04 -0700189 *c1 = (int8_t) _mm_extract_epi16(vout, 2);
190 *c0 = (int8_t) _mm_cvtsi128_si32(vout);
Marat Dukhanfc188ed2021-06-03 12:21:22 -0700191 }
192
193 nc = 0;
194 }
195 } while (nc != 0);
196}