Debian Codegraph @

src/qs8-qc8w-igemm/gen/qs8-qc8w-igemm-1x8c8-minmax-fp32-avxvnni.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/qs8-igemm/MRx8c8-avxvnni.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 <immintrin.h>
13
14 #include "xnnpack/common.h"
15 #include "xnnpack/gemm.h"
16 #include "xnnpack/intrinsics-polyfill.h"
17 #include "xnnpack/math.h"
18 #include "xnnpack/unaligned.h"
19
20
21 void xnn_qs8_qc8w_igemm_minmax_fp32_ukernel_1x8c8__avxvnni(
22 size_t mr,
23 size_t nc,
24 size_t kc,
25 size_t ks,
26 const int8_t** restrict a,
27 const void* restrict w,
28 int8_t* restrict c,
29 size_t cm_stride,
30 size_t cn_stride,
31 size_t a_offset,
32 const int8_t* zero,
33 const union xnn_qs8_qc8w_conv_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_OOB_READS
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, 8 * sizeof(int8_t));
45 int8_t* c0 = c;
46
47 const __m256i vsign_mask = _mm256_set1_epi8(0x80);
48 XNN_FORCE_REALIZATION(vsign_mask);
49 const __m256 voutput_max_less_zero_point = _mm256_set1_ps((int32_t) params->fp32_scalar.output_max - (int32_t) params->fp32_scalar.output_zero_point);
50 const __m256i voutput_zero_point = _mm256_set1_epi32(params->fp32_scalar.output_zero_point);
51 const __m128i voutput_min = _mm_set1_epi8(params->fp32_scalar.output_min);
52 // XNN_FORCE_REALIZATION(voutput_max_less_zero_point);
53 // XNN_FORCE_REALIZATION(voutput_zero_point);
54 // XNN_FORCE_REALIZATION(voutput_min);
55 do {
56 __m256i vacc0x0123 = _mm256_cvtepu32_epi64(_mm_load_si128((const __m128i*) w));
57 __m256i vacc0x4567 = _mm256_cvtepu32_epi64(_mm_load_si128((const __m128i*) ((const int32_t*) w + 4)));
58 __m256i vacc1x0x0123 = _mm256_setzero_si256();
59 __m256i vacc1x0x4567 = _mm256_setzero_si256();
60 w = (const int32_t*) w + 8;
61
62 size_t p = ks;
63 do {
64 const int8_t* restrict a0 = a[0];
65 if XNN_UNPREDICTABLE(a0 != zero) {
66 a0 = (const int8_t*) ((uintptr_t) a0 + a_offset);
67 }
68 a += 1;
69
70 size_t k = kc;
71 while (k >= 16 * sizeof(int8_t)) {
72 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
73 const __m256i va0x89ABCDEF = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0 + 8)), vsign_mask);
74 a0 += 16;
75
76 const __m256i vb01234567x0123 = _mm256_load_si256(w);
77 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
78 const __m256i vb01234567x4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 64));
79 const __m256i vb89ABCDEFx4567 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 96));
80
81 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
82 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
83 vacc1x0x0123 = _mm256_dpbusd_avx_epi32(vacc1x0x0123, va0x89ABCDEF, vb01234567x4567);
84 vacc1x0x4567 = _mm256_dpbusd_avx_epi32(vacc1x0x4567, va0x89ABCDEF, vb89ABCDEFx4567);
85
86 w = (const int8_t*) w + 128;
87 k -= 16 * sizeof(int8_t);
88 }
89
90 if (k != 0) {
91 const __m256i va0x01234567 = _mm256_xor_si256(_mm256_set1_epi64x((int64_t) unaligned_load_u64(a0)), vsign_mask);
92 a0 += 8;
93
94 const __m256i vb01234567x0123 = _mm256_load_si256(w);
95 const __m256i vb89ABCDEFx0123 = _mm256_load_si256((const __m256i*) ((const int8_t*) w + 32));
96
97 vacc0x0123 = _mm256_dpbusd_avx_epi32(vacc0x0123, va0x01234567, vb01234567x0123);
98 vacc0x4567 = _mm256_dpbusd_avx_epi32(vacc0x4567, va0x01234567, vb89ABCDEFx0123);
99
100 w = (const int8_t*) w + 64;
101 k -= 8 * sizeof(int8_t);
102 }
103
104 p -= 1 * sizeof(void*);
105 } while (p != 0);
106
107 vacc0x0123 = _mm256_add_epi32(vacc0x0123, vacc1x0x0123);
108 vacc0x4567 = _mm256_add_epi32(vacc0x4567, vacc1x0x4567);
109
110 // Add adjacent pairs
111 const __m256i vsum0x02134657 = _mm256_hadd_epi32(vacc0x0123, vacc0x4567);
112 __m256i vacc0x01234567 = _mm256_permute4x64_epi64(vsum0x02134657, _MM_SHUFFLE(3, 1, 2, 0));
113
114 __m256 vout0x01234567 = _mm256_cvtepi32_ps(vacc0x01234567);
115
116 const __m256 vscale01234567 = _mm256_load_ps(w);
117 w = (const float*) w + 8;
118 vout0x01234567 = _mm256_mul_ps(vout0x01234567, vscale01234567);
119
120 vout0x01234567 = _mm256_min_ps(vout0x01234567, voutput_max_less_zero_point);
121
122 vacc0x01234567 = _mm256_cvtps_epi32(vout0x01234567);
123
124 vacc0x01234567 = _mm256_add_epi32(vacc0x01234567, voutput_zero_point);
125
126 vacc0x01234567 = _mm256_packs_epi32(vacc0x01234567, _mm256_castsi128_si256(_mm256_extracti128_si256(vacc0x01234567, 1)));
127 __m128i voutb0x01234567 = _mm256_castsi256_si128(_mm256_packs_epi16(vacc0x01234567, vacc0x01234567));
128
129 voutb0x01234567 = _mm_max_epi8(voutb0x01234567, voutput_min);
130
131 if (nc >= 8) {
132 _mm_storel_epi64((__m128i*) c0, voutb0x01234567);
133 c0 = (int8_t*) ((uintptr_t) c0 + cn_stride);
134 a = (const int8_t**restrict) ((uintptr_t) a - ks);
135 nc -= 8;
136 } else {
137 if (nc & 4) {
138 _mm_storeu_si32(c0, voutb0x01234567);
139 c0 += 4;
140 voutb0x01234567 = _mm_srli_epi64(voutb0x01234567, 32);
141 }
142 if (nc & 2) {
143 unaligned_store_u16(c0, (uint16_t) _mm_extract_epi16(voutb0x01234567, 0));
144 c0 += 2;
145 voutb0x01234567 = _mm_srli_epi32(voutb0x01234567, 16);
146 }
147 if (nc & 1) {
148 *c0 = (int8_t) _mm_extract_epi8(voutb0x01234567, 0);
149 }
150 nc = 0;
151 }
152 } while (nc != 0);
153 }