Debian Codegraph @

src/f16-f32acc-igemm/gen/f16-f32acc-igemm-4x8-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_4x8__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 <= 4);
34 assert(nc != 0);
35 assert(kc != 0);
36 assert(kc % sizeof(uint16_t) == 0);
37 assert(ks != 0);
38 assert(ks % (4 * 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
63 do {
64 __m256 vacc0x0 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
65 __m256 vacc1x0 = vacc0x0;
66 __m256 vacc2x0 = vacc0x0;
67 __m256 vacc3x0 = vacc0x0;
68 w = (const xnn_float16*) w + 8;
69
70 size_t p = ks;
71 do {
72 const uint16_t* restrict a0 = (const uint16_t*) a[0];
73 assert(a0 != NULL);
74 if XNN_UNPREDICTABLE(a0 != (const uint16_t*) zero) {
75 a0 = (const uint16_t*) ((uintptr_t) a0 + a_offset);
76 }
77 const uint16_t* restrict a1 = (const uint16_t*) a[1];
78 assert(a1 != NULL);
79 if XNN_UNPREDICTABLE(a1 != (const uint16_t*) zero) {
80 a1 = (const uint16_t*) ((uintptr_t) a1 + a_offset);
81 }
82 const uint16_t* restrict a2 = (const uint16_t*) a[2];
83 assert(a2 != NULL);
84 if XNN_UNPREDICTABLE(a2 != (const uint16_t*) zero) {
85 a2 = (const uint16_t*) ((uintptr_t) a2 + a_offset);
86 }
87 const uint16_t* restrict a3 = (const uint16_t*) a[3];
88 assert(a3 != NULL);
89 if XNN_UNPREDICTABLE(a3 != (const uint16_t*) zero) {
90 a3 = (const uint16_t*) ((uintptr_t) a3 + a_offset);
91 }
92 a += 4;
93
94 size_t k = kc;
95 do {
96 const __m256 vb0 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
97 w = (const xnn_float16*) w + 8;
98
99 const __m256 va0 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a0));
100 a0 += 1;
101 const __m256 va1 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a1));
102 a1 += 1;
103 const __m256 va2 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a2));
104 a2 += 1;
105 const __m256 va3 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a3));
106 a3 += 1;
107
108 vacc0x0 = _mm256_fmadd_ps(va0, vb0, vacc0x0);
109 vacc1x0 = _mm256_fmadd_ps(va1, vb0, vacc1x0);
110 vacc2x0 = _mm256_fmadd_ps(va2, vb0, vacc2x0);
111 vacc3x0 = _mm256_fmadd_ps(va3, vb0, vacc3x0);
112
113 k -= sizeof(uint16_t);
114 } while (k != 0);
115 p -= 4 * sizeof(void*);
116 } while (p != 0);
117
118 vacc0x0 = _mm256_max_ps(vacc0x0, vmin);
119 vacc1x0 = _mm256_max_ps(vacc1x0, vmin);
120 vacc2x0 = _mm256_max_ps(vacc2x0, vmin);
121 vacc3x0 = _mm256_max_ps(vacc3x0, vmin);
122
123 vacc0x0 = _mm256_min_ps(vacc0x0, vmax);
124 vacc1x0 = _mm256_min_ps(vacc1x0, vmax);
125 vacc2x0 = _mm256_min_ps(vacc2x0, vmax);
126 vacc3x0 = _mm256_min_ps(vacc3x0, vmax);
127
128 if XNN_LIKELY(nc >= 8) {
129 _mm_storeu_si128((__m128i*) c3, _mm256_cvtps_ph(vacc3x0, _MM_FROUND_TO_NEAREST_INT));
130 c3 = (uint16_t*) ((uintptr_t) c3 + cn_stride);
131 _mm_storeu_si128((__m128i*) c2, _mm256_cvtps_ph(vacc2x0, _MM_FROUND_TO_NEAREST_INT));
132 c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
133 _mm_storeu_si128((__m128i*) c1, _mm256_cvtps_ph(vacc1x0, _MM_FROUND_TO_NEAREST_INT));
134 c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
135 _mm_storeu_si128((__m128i*) c0, _mm256_cvtps_ph(vacc0x0, _MM_FROUND_TO_NEAREST_INT));
136 c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
137
138 a = (const xnn_float16**restrict) ((uintptr_t) a - ks);
139 nc -= 8;
140 } else {
141 __m128i vh3x0 = _mm256_cvtps_ph(vacc3x0, _MM_FROUND_TO_NEAREST_INT);
142 __m128i vh2x0 = _mm256_cvtps_ph(vacc2x0, _MM_FROUND_TO_NEAREST_INT);
143 __m128i vh1x0 = _mm256_cvtps_ph(vacc1x0, _MM_FROUND_TO_NEAREST_INT);
144 __m128i vh0x0 = _mm256_cvtps_ph(vacc0x0, _MM_FROUND_TO_NEAREST_INT);
145 if (nc & 4) {
146 _mm_storel_epi64((__m128i*) c3, vh3x0);
147 _mm_storel_epi64((__m128i*) c2, vh2x0);
148 _mm_storel_epi64((__m128i*) c1, vh1x0);
149 _mm_storel_epi64((__m128i*) c0, vh0x0);
150
151 vh3x0 = _mm_unpackhi_epi64(vh3x0, vh3x0);
152 vh2x0 = _mm_unpackhi_epi64(vh2x0, vh2x0);
153 vh1x0 = _mm_unpackhi_epi64(vh1x0, vh1x0);
154 vh0x0 = _mm_unpackhi_epi64(vh0x0, vh0x0);
155
156 c3 += 4;
157 c2 += 4;
158 c1 += 4;
159 c0 += 4;
160 }
161 if (nc & 2) {
162 _mm_storeu_si32(c3, vh3x0);
163 _mm_storeu_si32(c2, vh2x0);
164 _mm_storeu_si32(c1, vh1x0);
165 _mm_storeu_si32(c0, vh0x0);
166
167 vh3x0 = _mm_srli_epi64(vh3x0, 32);
168 vh2x0 = _mm_srli_epi64(vh2x0, 32);
169 vh1x0 = _mm_srli_epi64(vh1x0, 32);
170 vh0x0 = _mm_srli_epi64(vh0x0, 32);
171
172 c3 += 2;
173 c2 += 2;
174 c1 += 2;
175 c0 += 2;
176 }
177 if (nc & 1) {
178 *c3 = _mm_extract_epi16(vh3x0, 0);
179 *c2 = _mm_extract_epi16(vh2x0, 0);
180 *c1 = _mm_extract_epi16(vh1x0, 0);
181 *c0 = _mm_extract_epi16(vh0x0, 0);
182 }
183
184 nc = 0;
185 }
186 } while (nc != 0);
187 }