• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
xnn_f16_igemm_minmax_ukernel_5x8__avx2_broadcast(size_t mr,size_t nc,size_t kc,size_t ks,const void ** restrict a,const void * restrict w,void * restrict c,size_t cm_stride,size_t cn_stride,size_t a_offset,const void * zero,const union xnn_f16_scaleminmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f16_igemm_minmax_ukernel_5x8__avx2_broadcast(
19     size_t mr,
20     size_t nc,
21     size_t kc,
22     size_t ks,
23     const void**restrict a,
24     const void*restrict w,
25     void*restrict c,
26     size_t cm_stride,
27     size_t cn_stride,
28     size_t a_offset,
29     const void* zero,
30     const union xnn_f16_scaleminmax_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   uint16_t* c0 = c;
45   uint16_t* c1 = (uint16_t*) ((uintptr_t) c0 + cm_stride);
46   if XNN_UNPREDICTABLE(mr < 2) {
47     c1 = c0;
48   }
49   uint16_t* c2 = (uint16_t*) ((uintptr_t) c1 + cm_stride);
50   if XNN_UNPREDICTABLE(mr <= 2) {
51     c2 = c1;
52   }
53   uint16_t* c3 = (uint16_t*) ((uintptr_t) c2 + cm_stride);
54   if XNN_UNPREDICTABLE(mr < 4) {
55     c3 = c2;
56   }
57   uint16_t* c4 = (uint16_t*) ((uintptr_t) c3 + cm_stride);
58   if XNN_UNPREDICTABLE(mr <= 4) {
59     c4 = c3;
60   }
61 
62   do {
63     __m256 vacc0x01234567 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
64     __m256 vacc1x01234567 = vacc0x01234567;
65     __m256 vacc2x01234567 = vacc0x01234567;
66     __m256 vacc3x01234567 = vacc0x01234567;
67     __m256 vacc4x01234567 = vacc0x01234567;
68     w = (const uint16_t*) 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 != 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 != 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 != 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 != zero) {
90         a3 = (const uint16_t*) ((uintptr_t) a3 + a_offset);
91       }
92       const uint16_t* restrict a4 = (const uint16_t*) a[4];
93       assert(a4 != NULL);
94       if XNN_UNPREDICTABLE(a4 != zero) {
95         a4 = (const uint16_t*) ((uintptr_t) a4 + a_offset);
96       }
97       a += 5;
98 
99       size_t k = kc;
100       do {
101         const __m256 vb01234567 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
102         w = (const uint16_t*) w + 8;
103 
104         const __m256 va0 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a0));
105         a0 += 1;
106         const __m256 va1 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a1));
107         a1 += 1;
108         const __m256 va2 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a2));
109         a2 += 1;
110         const __m256 va3 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a3));
111         a3 += 1;
112         const __m256 va4 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a4));
113         a4 += 1;
114 
115         vacc0x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va0, vb01234567, vacc0x01234567), _MM_FROUND_NO_EXC));
116         vacc1x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va1, vb01234567, vacc1x01234567), _MM_FROUND_NO_EXC));
117         vacc2x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va2, vb01234567, vacc2x01234567), _MM_FROUND_NO_EXC));
118         vacc3x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va3, vb01234567, vacc3x01234567), _MM_FROUND_NO_EXC));
119         vacc4x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va4, vb01234567, vacc4x01234567), _MM_FROUND_NO_EXC));
120 
121         k -= sizeof(uint16_t);
122       } while (k != 0);
123       p -= 5 * sizeof(void*);
124     } while (p != 0);
125 
126     const __m256 vscale = _mm256_load_ps(params->avx.scale);
127     vacc0x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_mul_ps(vacc0x01234567, vscale), _MM_FROUND_NO_EXC));
128     vacc1x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_mul_ps(vacc1x01234567, vscale), _MM_FROUND_NO_EXC));
129     vacc2x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_mul_ps(vacc2x01234567, vscale), _MM_FROUND_NO_EXC));
130     vacc3x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_mul_ps(vacc3x01234567, vscale), _MM_FROUND_NO_EXC));
131     vacc4x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_mul_ps(vacc4x01234567, vscale), _MM_FROUND_NO_EXC));
132 
133     const __m256 vmin = _mm256_load_ps(params->avx.min);
134     vacc0x01234567 = _mm256_max_ps(vacc0x01234567, vmin);
135     vacc1x01234567 = _mm256_max_ps(vacc1x01234567, vmin);
136     vacc2x01234567 = _mm256_max_ps(vacc2x01234567, vmin);
137     vacc3x01234567 = _mm256_max_ps(vacc3x01234567, vmin);
138     vacc4x01234567 = _mm256_max_ps(vacc4x01234567, vmin);
139 
140     const __m256 vmax = _mm256_load_ps(params->avx.max);
141     vacc0x01234567 = _mm256_min_ps(vacc0x01234567, vmax);
142     vacc1x01234567 = _mm256_min_ps(vacc1x01234567, vmax);
143     vacc2x01234567 = _mm256_min_ps(vacc2x01234567, vmax);
144     vacc3x01234567 = _mm256_min_ps(vacc3x01234567, vmax);
145     vacc4x01234567 = _mm256_min_ps(vacc4x01234567, vmax);
146 
147     if XNN_LIKELY(nc >= 8) {
148       _mm_storeu_si128((__m128i*) c4, _mm256_cvtps_ph(vacc4x01234567, _MM_FROUND_NO_EXC));
149       c4 = (uint16_t*) ((uintptr_t) c4 + cn_stride);
150       _mm_storeu_si128((__m128i*) c3, _mm256_cvtps_ph(vacc3x01234567, _MM_FROUND_NO_EXC));
151       c3 = (uint16_t*) ((uintptr_t) c3 + cn_stride);
152       _mm_storeu_si128((__m128i*) c2, _mm256_cvtps_ph(vacc2x01234567, _MM_FROUND_NO_EXC));
153       c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
154       _mm_storeu_si128((__m128i*) c1, _mm256_cvtps_ph(vacc1x01234567, _MM_FROUND_NO_EXC));
155       c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
156       _mm_storeu_si128((__m128i*) c0, _mm256_cvtps_ph(vacc0x01234567, _MM_FROUND_NO_EXC));
157       c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
158 
159       a = (const void**restrict) ((uintptr_t) a - ks);
160       nc -= 8;
161     } else {
162       __m128i vh4x01234567 = _mm256_cvtps_ph(vacc4x01234567, _MM_FROUND_NO_EXC);
163       __m128i vh3x01234567 = _mm256_cvtps_ph(vacc3x01234567, _MM_FROUND_NO_EXC);
164       __m128i vh2x01234567 = _mm256_cvtps_ph(vacc2x01234567, _MM_FROUND_NO_EXC);
165       __m128i vh1x01234567 = _mm256_cvtps_ph(vacc1x01234567, _MM_FROUND_NO_EXC);
166       __m128i vh0x01234567 = _mm256_cvtps_ph(vacc0x01234567, _MM_FROUND_NO_EXC);
167       if (nc & 4) {
168         _mm_storel_epi64((__m128i*) c4, vh4x01234567);
169         _mm_storel_epi64((__m128i*) c3, vh3x01234567);
170         _mm_storel_epi64((__m128i*) c2, vh2x01234567);
171         _mm_storel_epi64((__m128i*) c1, vh1x01234567);
172         _mm_storel_epi64((__m128i*) c0, vh0x01234567);
173 
174         vh4x01234567 = _mm_unpackhi_epi64(vh4x01234567, vh4x01234567);
175         vh3x01234567 = _mm_unpackhi_epi64(vh3x01234567, vh3x01234567);
176         vh2x01234567 = _mm_unpackhi_epi64(vh2x01234567, vh2x01234567);
177         vh1x01234567 = _mm_unpackhi_epi64(vh1x01234567, vh1x01234567);
178         vh0x01234567 = _mm_unpackhi_epi64(vh0x01234567, vh0x01234567);
179 
180         c4 += 4;
181         c3 += 4;
182         c2 += 4;
183         c1 += 4;
184         c0 += 4;
185       }
186       if (nc & 2) {
187         _mm_storeu_si32(c4, vh4x01234567);
188         _mm_storeu_si32(c3, vh3x01234567);
189         _mm_storeu_si32(c2, vh2x01234567);
190         _mm_storeu_si32(c1, vh1x01234567);
191         _mm_storeu_si32(c0, vh0x01234567);
192 
193         vh4x01234567 = _mm_srli_epi64(vh4x01234567, 32);
194         vh3x01234567 = _mm_srli_epi64(vh3x01234567, 32);
195         vh2x01234567 = _mm_srli_epi64(vh2x01234567, 32);
196         vh1x01234567 = _mm_srli_epi64(vh1x01234567, 32);
197         vh0x01234567 = _mm_srli_epi64(vh0x01234567, 32);
198 
199         c4 += 2;
200         c3 += 2;
201         c2 += 2;
202         c1 += 2;
203         c0 += 2;
204       }
205       if (nc & 1) {
206         *c4 = _mm_extract_epi16(vh4x01234567, 0);
207         *c3 = _mm_extract_epi16(vh3x01234567, 0);
208         *c2 = _mm_extract_epi16(vh2x01234567, 0);
209         *c1 = _mm_extract_epi16(vh1x01234567, 0);
210         *c0 = _mm_extract_epi16(vh0x01234567, 0);
211       }
212 
213       nc = 0;
214     }
215   } while (nc != 0);
216 }
217