Debian Codegraph @

src/qd8-f32-qc4w-gemm/gen/qd8-f32-qc4w-gemm-1x4c8-minmax-ssse3-madd-prfm.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-gemm/MRx4c8-ssevnni.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2024 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 <tmmintrin.h>
13
14 #include "xnnpack/gemm.h"
15 #include "xnnpack/intrinsics-polyfill.h"
16 #include "xnnpack/math.h"
17 #include "xnnpack/unaligned.h"
18 #include "xnnpack/prefetch.h"
19
20
21 void xnn_qd8_f32_qc4w_gemm_minmax_ukernel_1x4c8__ssse3_madd_prfm(
22 size_t mr,
23 size_t nc,
24 size_t kc,
25 const int8_t* restrict a,
26 size_t a_stride,
27 const void* restrict w,
28 float* restrict c,
29 size_t cm_stride,
30 size_t cn_stride,
31 const struct xnn_f32_qc4w_minmax_params params[restrict XNN_MIN_ELEMENTS(1)],
32 const struct xnn_qd8_quantization_params quantization_params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
33 {
34 assert(mr != 0);
35 assert(mr <= 1);
36 assert(nc != 0);
37 assert(kc != 0);
38 assert(kc % sizeof(int8_t) == 0);
39 assert(a != NULL);
40 assert(w != NULL);
41 assert(c != NULL);
42
43 kc = round_up_po2(kc, 8 * sizeof(int8_t));
44 const int8_t* a0 = a;
45 float* c0 = c;
46
47 const __m128i vsign_mask = _mm_set1_epi8(0x80);
48 XNN_FORCE_REALIZATION(vsign_mask);
49 const __m128i vinput_zero_point0 = _mm_set1_epi32((int) quantization_params[0].zero_point + 128);
50 const __m128 voutput_min = _mm_set1_ps(params->scalar.min);
51 const __m128 voutput_max = _mm_set1_ps(params->scalar.max);
52 const __m128i vmask = _mm_set1_epi8(0x0F);
53 XNN_FORCE_REALIZATION(vmask);
54 do {
55 const __m128i vksum0123 = _mm_load_si128(w);
56 const __m128i vksum13 = _mm_shuffle_epi32(vksum0123, 0xF5);
57 const __m128i vsum0x02 = _mm_mul_epu32(vksum0123, vinput_zero_point0);
58 const __m128i vsum0x13 = _mm_mul_epu32(vksum13, vinput_zero_point0);
59 const __m128i vsum0x01 = _mm_unpacklo_epi32(vsum0x02, vsum0x13);
60 const __m128i vsum0x23 = _mm_unpackhi_epi32(vsum0x02, vsum0x13);
61 __m128i vacc0x01 = _mm_unpacklo_epi32(vsum0x01, _mm_setzero_si128());
62 __m128i vacc0x23 = _mm_unpacklo_epi32(vsum0x23, _mm_setzero_si128());
63 __m128i vacc1x0x01 = _mm_setzero_si128();
64 __m128i vacc1x0x23 = _mm_setzero_si128();
65 w = (const int32_t*) w + 4;
66
67 size_t k = kc;
68 while (k >= 16 * sizeof(int8_t)) {
69 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
70 const __m128i va0x89ABCDEF = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
71 a0 += 16;
72
73 const __m128i vbb01234567x0123 = _mm_load_si128(w);
74 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
75 const __m128i vbs01234567x01 = _mm_srli_epi32(vbb01234567x0123, 4);
76 const __m128i vbs89ABCDEFx01 = _mm_srli_epi32(vbb89ABCDEFx0123, 4);
77 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
78 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
79 const __m128i vb01234567x23 = _mm_and_si128(vbs01234567x01, vmask);
80 const __m128i vb89ABCDEFx23 = _mm_and_si128(vbs89ABCDEFx01, vmask);
81
82 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
83 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
84 xnn_prefetch_to_l1((const int8_t*) w + 960);
85 vacc1x0x01 = _mm_dpbusd_epi32_madd(vacc1x0x01, va0x89ABCDEF, vb01234567x23);
86 vacc1x0x23 = _mm_dpbusd_epi32_madd(vacc1x0x23, va0x89ABCDEF, vb89ABCDEFx23);
87
88 w = (const int8_t*) w + 32;
89 k -= 16 * sizeof(int8_t);
90 }
91
92 if (k != 0) {
93 const __m128i va0x01234567 = _mm_xor_si128(_mm_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
94 a0 += 8;
95
96 const __m128i vbb01234567x0123 = _mm_load_si128(w);
97 const __m128i vbb89ABCDEFx0123 = _mm_load_si128((const __m128i*) ((const int8_t*) w + 16));
98 const __m128i vb01234567x01 = _mm_and_si128(vbb01234567x0123, vmask);
99 const __m128i vb89ABCDEFx01 = _mm_and_si128(vbb89ABCDEFx0123, vmask);
100
101 vacc0x01 = _mm_dpbusd_epi32_madd(vacc0x01, va0x01234567, vb01234567x01);
102 vacc0x23 = _mm_dpbusd_epi32_madd(vacc0x23, va0x01234567, vb89ABCDEFx01);
103 xnn_prefetch_to_l1((const int8_t*) w + 960);
104
105 w = (const int8_t*) w + 32;
106 k -= 8 * sizeof(int8_t);
107 }
108 vacc0x01 = _mm_add_epi32(vacc0x01, vacc1x0x01);
109 vacc0x23 = _mm_add_epi32(vacc0x23, vacc1x0x23);
110
111 // Add adjacent pairs
112 __m128i vacc0x0123 = _mm_hadd_epi32(vacc0x01, vacc0x23);
113
114 __m128 vout0x0123 = _mm_cvtepi32_ps(vacc0x0123);
115
116 vout0x0123 = _mm_mul_ps(vout0x0123, _mm_set1_ps(quantization_params[0].inv_scale));
117
118 const __m128 vfilter_output_scale0123 = _mm_load_ps((const float*) w);
119 const __m128 vbias0123 = _mm_load_ps((const float*) w + 4);
120 w = (const float*) w + 8;
121
122 vout0x0123 = _mm_add_ps(_mm_mul_ps(vout0x0123, vfilter_output_scale0123), vbias0123);
123
124 vout0x0123 = _mm_max_ps(vout0x0123, voutput_min);
125
126 vout0x0123 = _mm_min_ps(vout0x0123, voutput_max);
127
128 if XNN_LIKELY(nc >= 4) {
129 _mm_storeu_ps(c0, vout0x0123);
130 a0 = (const int8_t*) ((uintptr_t) a0 - kc);
131 c0 = (float*) ((uintptr_t) c0 + cn_stride);
132 nc -= 4;
133 } else {
134 if (nc & 2) {
135 _mm_storel_pi((__m64*) c0, vout0x0123);
136 c0 += 2;
137 vout0x0123 = _mm_movehl_ps(vout0x0123, vout0x0123);
138 }
139 if (nc & 1) {
140 _mm_store_ss(c0, vout0x0123);
141 }
142 nc = 0;
143 }
144 } while (nc != 0);
145 }