Debian Codegraph @

src/f16-f32acc-igemm/gen/f16-f32acc-igemm-5x8-minmax-avx2-broadcast.c

raw
1 // Auto-generated file. Do not edit!
2 // Template: src/f16-igemm/avx2-broadcast.c.in
3 // Generator: tools/xngen
4 //
5 // Copyright 2022 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/igemm.h"
15 #include "xnnpack/intrinsics-polyfill.h"
16
17
18 void xnn_f16_f32acc_igemm_minmax_ukernel_5x8__avx2_broadcast(
19 size_t mr,
20 size_t nc,
21 size_t kc,
22 size_t ks,
23 const xnn_float16** restrict a,
24 const xnn_float16* restrict w,
25 xnn_float16* restrict c,
26 size_t cm_stride,
27 size_t cn_stride,
28 size_t a_offset,
29 const xnn_float16* zero,
30 const union xnn_f16_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
31 {
32 assert(mr != 0);
33 assert(mr <= 5);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(kc % sizeof(uint16_t) == 0);
37 assert(ks != 0);
38 assert(ks % (5 * sizeof(void*)) == 0);
39 assert(a_offset % sizeof(uint16_t) == 0);
40 assert(a != NULL);
41 assert(w != NULL);
42 assert(c != NULL);
43
44 const __m256 vmin = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.min));
45 const __m256 vmax = _mm256_cvtph_ps(_mm_set1_epi16(*(const uint16_t*) &params->scalar.max));
46 XNN_FORCE_REALIZATION(vmin);
47 XNN_FORCE_REALIZATION(vmax);
48
49 uint16_t* c0 = (uint16_t*) c;
50 uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride);
51 if XNN_UNPREDICTABLE(mr < 2) {
52 c1 = c0;
53 }
54 uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride);
55 if XNN_UNPREDICTABLE(mr <= 2) {
56 c2 = c1;
57 }
58 uint16_t* c3 = (uint16_t*) ((uintptr_t) c2 + cm_stride);
59 if XNN_UNPREDICTABLE(mr < 4) {
60 c3 = c2;
61 }
62 uint16_t* c4 = (uint16_t*) ((uintptr_t) c3 + cm_stride);
63 if XNN_UNPREDICTABLE(mr <= 4) {
64 c4 = c3;
65 }
66
67 do {
68 __m256 vacc0x0 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
69 __m256 vacc1x0 = vacc0x0;
70 __m256 vacc2x0 = vacc0x0;
71 __m256 vacc3x0 = vacc0x0;
72 __m256 vacc4x0 = vacc0x0;
73 w = (const xnn_float16*) w + 8;
74
75 size_t p = ks;
76 do {
77 const uint16_t* restrict a0 = (const uint16_t*) a[0];
78 assert(a0 != NULL);
79 if XNN_UNPREDICTABLE(a0 != (const uint16_t*) zero) {
80 a0 = (const uint16_t*) ((uintptr_t) a0 + a_offset);
81 }
82 const uint16_t* restrict a1 = (const uint16_t*) a[1];
83 assert(a1 != NULL);
84 if XNN_UNPREDICTABLE(a1 != (const uint16_t*) zero) {
85 a1 = (const uint16_t*) ((uintptr_t) a1 + a_offset);
86 }
87 const uint16_t* restrict a2 = (const uint16_t*) a[2];
88 assert(a2 != NULL);
89 if XNN_UNPREDICTABLE(a2 != (const uint16_t*) zero) {
90 a2 = (const uint16_t*) ((uintptr_t) a2 + a_offset);
91 }
92 const uint16_t* restrict a3 = (const uint16_t*) a[3];
93 assert(a3 != NULL);
94 if XNN_UNPREDICTABLE(a3 != (const uint16_t*) zero) {
95 a3 = (const uint16_t*) ((uintptr_t) a3 + a_offset);
96 }
97 const uint16_t* restrict a4 = (const uint16_t*) a[4];
98 assert(a4 != NULL);
99 if XNN_UNPREDICTABLE(a4 != (const uint16_t*) zero) {
100 a4 = (const uint16_t*) ((uintptr_t) a4 + a_offset);
101 }
102 a += 5;
103
104 size_t k = kc;
105 do {
106 const __m256 vb0 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
107 w = (const xnn_float16*) w + 8;
108
109 const __m256 va0 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a0));
110 a0 += 1;
111 const __m256 va1 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a1));
112 a1 += 1;
113 const __m256 va2 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a2));
114 a2 += 1;
115 const __m256 va3 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a3));
116 a3 += 1;
117 const __m256 va4 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a4));
118 a4 += 1;
119
120 vacc0x0 = _mm256_fmadd_ps(va0, vb0, vacc0x0);
121 vacc1x0 = _mm256_fmadd_ps(va1, vb0, vacc1x0);
122 vacc2x0 = _mm256_fmadd_ps(va2, vb0, vacc2x0);
123 vacc3x0 = _mm256_fmadd_ps(va3, vb0, vacc3x0);
124 vacc4x0 = _mm256_fmadd_ps(va4, vb0, vacc4x0);
125
126 k -= sizeof(uint16_t);
127 } while (k != 0);
128 p -= 5 * sizeof(void*);
129 } while (p != 0);
130
131 vacc0x0 = _mm256_max_ps(vacc0x0, vmin);
132 vacc1x0 = _mm256_max_ps(vacc1x0, vmin);
133 vacc2x0 = _mm256_max_ps(vacc2x0, vmin);
134 vacc3x0 = _mm256_max_ps(vacc3x0, vmin);
135 vacc4x0 = _mm256_max_ps(vacc4x0, vmin);
136
137 vacc0x0 = _mm256_min_ps(vacc0x0, vmax);
138 vacc1x0 = _mm256_min_ps(vacc1x0, vmax);
139 vacc2x0 = _mm256_min_ps(vacc2x0, vmax);
140 vacc3x0 = _mm256_min_ps(vacc3x0, vmax);
141 vacc4x0 = _mm256_min_ps(vacc4x0, vmax);
142
143 if XNN_LIKELY(nc >= 8) {
144 _mm_storeu_si128((__m128i*) c4, _mm256_cvtps_ph(vacc4x0, _MM_FROUND_TO_NEAREST_INT));
145 c4 = (uint16_t*) ((uintptr_t) c4 + cn_stride);
146 _mm_storeu_si128((__m128i*) c3, _mm256_cvtps_ph(vacc3x0, _MM_FROUND_TO_NEAREST_INT));
147 c3 = (uint16_t*) ((uintptr_t) c3 + cn_stride);
148 _mm_storeu_si128((__m128i*) c2, _mm256_cvtps_ph(vacc2x0, _MM_FROUND_TO_NEAREST_INT));
149 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
150 _mm_storeu_si128((__m128i*) c1, _mm256_cvtps_ph(vacc1x0, _MM_FROUND_TO_NEAREST_INT));
151 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
152 _mm_storeu_si128((__m128i*) c0, _mm256_cvtps_ph(vacc0x0, _MM_FROUND_TO_NEAREST_INT));
153 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
154
155 a = (const xnn_float16**restrict) ((uintptr_t) a - ks);
156 nc -= 8;
157 } else {
158 __m128i vh4x0 = _mm256_cvtps_ph(vacc4x0, _MM_FROUND_TO_NEAREST_INT);
159 __m128i vh3x0 = _mm256_cvtps_ph(vacc3x0, _MM_FROUND_TO_NEAREST_INT);
160 __m128i vh2x0 = _mm256_cvtps_ph(vacc2x0, _MM_FROUND_TO_NEAREST_INT);
161 __m128i vh1x0 = _mm256_cvtps_ph(vacc1x0, _MM_FROUND_TO_NEAREST_INT);
162 __m128i vh0x0 = _mm256_cvtps_ph(vacc0x0, _MM_FROUND_TO_NEAREST_INT);
163 if (nc & 4) {
164 _mm_storel_epi64((__m128i*) c4, vh4x0);
165 _mm_storel_epi64((__m128i*) c3, vh3x0);
166 _mm_storel_epi64((__m128i*) c2, vh2x0);
167 _mm_storel_epi64((__m128i*) c1, vh1x0);
168 _mm_storel_epi64((__m128i*) c0, vh0x0);
169
170 vh4x0 = _mm_unpackhi_epi64(vh4x0, vh4x0);
171 vh3x0 = _mm_unpackhi_epi64(vh3x0, vh3x0);
172 vh2x0 = _mm_unpackhi_epi64(vh2x0, vh2x0);
173 vh1x0 = _mm_unpackhi_epi64(vh1x0, vh1x0);
174 vh0x0 = _mm_unpackhi_epi64(vh0x0, vh0x0);
175
176 c4 += 4;
177 c3 += 4;
178 c2 += 4;
179 c1 += 4;
180 c0 += 4;
181 }
182 if (nc & 2) {
183 _mm_storeu_si32(c4, vh4x0);
184 _mm_storeu_si32(c3, vh3x0);
185 _mm_storeu_si32(c2, vh2x0);
186 _mm_storeu_si32(c1, vh1x0);
187 _mm_storeu_si32(c0, vh0x0);
188
189 vh4x0 = _mm_srli_epi64(vh4x0, 32);
190 vh3x0 = _mm_srli_epi64(vh3x0, 32);
191 vh2x0 = _mm_srli_epi64(vh2x0, 32);
192 vh1x0 = _mm_srli_epi64(vh1x0, 32);
193 vh0x0 = _mm_srli_epi64(vh0x0, 32);
194
195 c4 += 2;
196 c3 += 2;
197 c2 += 2;
198 c1 += 2;
199 c0 += 2;
200 }
201 if (nc & 1) {
202 *c4 = _mm_extract_epi16(vh4x0, 0);
203 *c3 = _mm_extract_epi16(vh3x0, 0);
204 *c2 = _mm_extract_epi16(vh2x0, 0);
205 *c1 = _mm_extract_epi16(vh1x0, 0);
206 *c0 = _mm_extract_epi16(vh0x0, 0);
207 }
208
209 nc = 0;
210 }
211 } while (nc != 0);
212 }