1 // Auto-generated file. Do not edit!
2 //   Template: src/qs8-gemm/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 #ifdef __GNUC__
13   #include <x86intrin.h>
14 #else
15   #include <immintrin.h>
16   #include <ammintrin.h>
17 #endif
18 
19 #include <xnnpack/gemm.h>
20 #include <xnnpack/math.h>
21 
22 
xnn_qs8_gemm_xw_minmax_ukernel_1x4c2__xop(size_t mr,size_t nc,size_t kc,const int8_t * restrict a,size_t a_stride,const void * restrict w,int8_t * restrict c,size_t cm_stride,size_t cn_stride,const union xnn_qs8_gemm_xw_params params[restrict XNN_MIN_ELEMENTS (1)])23 void xnn_qs8_gemm_xw_minmax_ukernel_1x4c2__xop(
24     size_t mr,
25     size_t nc,
26     size_t kc,
27     const int8_t* restrict a,
28     size_t a_stride,
29     const void* restrict w,
30     int8_t* restrict c,
31     size_t cm_stride,
32     size_t cn_stride,
33     const union xnn_qs8_gemm_xw_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
34 {
35   assert(mr != 0);
36   assert(mr <= 1);
37   assert(nc != 0);
38   assert(kc != 0);
39   assert(kc % sizeof(int8_t) == 0);
40   assert(a != NULL);
41   assert(w != NULL);
42   assert(c != NULL);
43 
44   kc = round_up_po2(kc, 2);
45   const int8_t* a0 = a;
46   int8_t* c0 = c;
47 
48   do {
49     __m128i vacc0x0123 = _mm_loadu_si128((const __m128i*) w);
50     w = (const void*) ((uintptr_t) w + 4 * sizeof(int32_t));
51 
52     size_t k = kc;
53     while (k >= 8 * sizeof(int8_t)) {
54       const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
55       const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
56       a0 += 8;
57 
58       const __m128i vxb0 = _mm_load_si128((const __m128i*) w);
59 
60       vacc0x0123 = _mm_maddd_epi16(
61         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc0x0123);
62       const __m128i vxb1 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 8 * sizeof(int16_t)));
63 
64       vacc0x0123 = _mm_maddd_epi16(
65         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc0x0123);
66       const __m128i vxb2 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 16 * sizeof(int16_t)));
67 
68       vacc0x0123 = _mm_maddd_epi16(
69         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc0x0123);
70       const __m128i vxb3 = _mm_load_si128((const __m128i*) ((uintptr_t) w + 24 * sizeof(int16_t)));
71 
72       vacc0x0123 = _mm_maddd_epi16(
73         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(3, 3, 3, 3)), vxb3, vacc0x0123);
74 
75       w = (const void*) ((uintptr_t) w + 32 * sizeof(int16_t));
76       k -= 8 * sizeof(int8_t);
77     }
78     if (k != 0) {
79       const __m128i va0 = _mm_loadl_epi64((const __m128i*) a0);
80       const __m128i vxa0 = _mm_cvtepi8_epi16(va0);
81       a0 = (const int8_t*) ((uintptr_t) a0 + k);
82 
83       const __m128i vxb0 = _mm_load_si128((const __m128i*) w);
84       w = (const void*) ((uintptr_t) w + 8 * sizeof(int16_t));
85 
86       vacc0x0123 = _mm_maddd_epi16(
87         _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(0, 0, 0, 0)), vxb0, vacc0x0123);
88 
89       if (k > 2 * sizeof(int8_t)) {
90         const __m128i vxb1 = _mm_load_si128((const __m128i*) w);
91         w = (const void*) ((uintptr_t) w + 8 * sizeof(int16_t));
92 
93         vacc0x0123 = _mm_maddd_epi16(
94           _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(1, 1, 1, 1)), vxb1, vacc0x0123);
95 
96         if (k > 4 * sizeof(int8_t)) {
97           const __m128i vxb2 = _mm_load_si128((const __m128i*) w);
98           w = (const void*) ((uintptr_t) w + 8 * sizeof(int16_t));
99 
100           vacc0x0123 = _mm_maddd_epi16(
101             _mm_shuffle_epi32(vxa0, _MM_SHUFFLE(2, 2, 2, 2)), vxb2, vacc0x0123);
102         }
103       }
104     }
105 
106     const __m128i vmultiplier = _mm_load_si128((const __m128i*) params->sse2.multiplier);
107     const __m128i vrounding = _mm_load_si128((const __m128i*) params->sse2.rounding);
108 
109     const __m128i vacc0x1133 = _mm_shuffle_epi32(vacc0x0123, _MM_SHUFFLE(3, 3, 1, 1));
110 
111     const __m128i vprod0x02 = _mm_add_epi64(_mm_mul_epi32(vacc0x0123, vmultiplier), vrounding);
112 
113     const __m128i vprod0x13 = _mm_add_epi64(_mm_mul_epi32(vacc0x1133, vmultiplier), vrounding);
114 
115     const __m128i vq31prod0x02 = _mm_srli_epi64(vprod0x02, 31);
116     const __m128i vq31prod0x13 = _mm_add_epi64(vprod0x13, vprod0x13);
117 
118     const __m128i vq31prod0x0123 = _mm_blend_epi16(vq31prod0x02, vq31prod0x13, 0xCC);
119 
120     const __m128i vremainder_mask = _mm_load_si128((const __m128i*) params->sse2.remainder_mask);
121     const __m128i vrem0x0123 =
122       _mm_add_epi32(_mm_and_si128(vq31prod0x0123, vremainder_mask), _mm_cmpgt_epi32(_mm_setzero_si128(), vq31prod0x0123));
123 
124     const __m128i vremainder_threshold = _mm_load_si128((const __m128i*) params->sse2.remainder_threshold);
125     const __m128i vshift = _mm_load_si128((const __m128i*) params->sse2.shift);
126     vacc0x0123 =
127       _mm_sub_epi32(_mm_sra_epi32(vq31prod0x0123, vshift), _mm_cmpgt_epi32(vrem0x0123, vremainder_threshold));
128 
129     const __m128i voutput_zero_point = _mm_load_si128((const __m128i*) params->sse2.output_zero_point);
130     __m128i vacc00x0123 = _mm_adds_epi16(_mm_packs_epi32(vacc0x0123, vacc0x0123), voutput_zero_point);
131 
132     const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.output_min);
133     const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.output_max);
134     vacc00x0123 = _mm_min_epi16(_mm_max_epi16(vacc00x0123, voutput_min), voutput_max);
135 
136     __m128i vout = _mm_packs_epi16(vacc00x0123, vacc00x0123);
137 
138     if (nc >= 4) {
139       *((uint32_t*) c0) = (uint32_t) _mm_cvtsi128_si32(vout);
140 
141       c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
142 
143       a0 = (const int8_t*) ((uintptr_t) a0 - kc);
144 
145       nc -= 4;
146     } else {
147       if (nc & 2) {
148         *((uint16_t*) c0) = (uint16_t) _mm_extract_epi16(vout, 0);
149         c0 += 2;
150         vout = _mm_srli_epi32(vout, 16);
151       }
152       if (nc & 1) {
153         *((int8_t*) c0) = (int8_t) _mm_extract_epi8(vout, 0);
154       }
155 
156       nc = 0;
157     }
158   } while (nc != 0);
159 }
160