xref: /aosp_15_r20/external/XNNPACK/src/f16-igemm/gen/4x8-minmax-avx2-broadcast.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
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_4x8__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_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])18 void xnn_f16_igemm_minmax_ukernel_4x8__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_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   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 
58   do {
59     __m256 vacc0x01234567 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
60     __m256 vacc1x01234567 = vacc0x01234567;
61     __m256 vacc2x01234567 = vacc0x01234567;
62     __m256 vacc3x01234567 = vacc0x01234567;
63     w = (const uint16_t*) w + 8;
64 
65     size_t p = ks;
66     do {
67       const uint16_t* restrict a0 = (const uint16_t*) a[0];
68       assert(a0 != NULL);
69       if XNN_UNPREDICTABLE(a0 != zero) {
70         a0 = (const uint16_t*) ((uintptr_t) a0 + a_offset);
71       }
72       const uint16_t* restrict a1 = (const uint16_t*) a[1];
73       assert(a1 != NULL);
74       if XNN_UNPREDICTABLE(a1 != zero) {
75         a1 = (const uint16_t*) ((uintptr_t) a1 + a_offset);
76       }
77       const uint16_t* restrict a2 = (const uint16_t*) a[2];
78       assert(a2 != NULL);
79       if XNN_UNPREDICTABLE(a2 != zero) {
80         a2 = (const uint16_t*) ((uintptr_t) a2 + a_offset);
81       }
82       const uint16_t* restrict a3 = (const uint16_t*) a[3];
83       assert(a3 != NULL);
84       if XNN_UNPREDICTABLE(a3 != zero) {
85         a3 = (const uint16_t*) ((uintptr_t) a3 + a_offset);
86       }
87       a += 4;
88 
89       size_t k = kc;
90       do {
91         const __m256 vb01234567 = _mm256_cvtph_ps(_mm_load_si128((const __m128i*) w));
92         w = (const uint16_t*) w + 8;
93 
94         const __m256 va0 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a0));
95         a0 += 1;
96         const __m256 va1 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a1));
97         a1 += 1;
98         const __m256 va2 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a2));
99         a2 += 1;
100         const __m256 va3 = _mm256_cvtph_ps(_mm_set1_epi16((short) *a3));
101         a3 += 1;
102 
103         vacc0x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va0, vb01234567, vacc0x01234567), _MM_FROUND_NO_EXC));
104         vacc1x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va1, vb01234567, vacc1x01234567), _MM_FROUND_NO_EXC));
105         vacc2x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va2, vb01234567, vacc2x01234567), _MM_FROUND_NO_EXC));
106         vacc3x01234567 = _mm256_cvtph_ps(_mm256_cvtps_ph(_mm256_fmadd_ps(va3, vb01234567, vacc3x01234567), _MM_FROUND_NO_EXC));
107 
108         k -= sizeof(uint16_t);
109       } while (k != 0);
110       p -= 4 * sizeof(void*);
111     } while (p != 0);
112 
113     const __m256 vmin = _mm256_load_ps(params->avx.min);
114     vacc0x01234567 = _mm256_max_ps(vacc0x01234567, vmin);
115     vacc1x01234567 = _mm256_max_ps(vacc1x01234567, vmin);
116     vacc2x01234567 = _mm256_max_ps(vacc2x01234567, vmin);
117     vacc3x01234567 = _mm256_max_ps(vacc3x01234567, vmin);
118 
119     const __m256 vmax = _mm256_load_ps(params->avx.max);
120     vacc0x01234567 = _mm256_min_ps(vacc0x01234567, vmax);
121     vacc1x01234567 = _mm256_min_ps(vacc1x01234567, vmax);
122     vacc2x01234567 = _mm256_min_ps(vacc2x01234567, vmax);
123     vacc3x01234567 = _mm256_min_ps(vacc3x01234567, vmax);
124 
125     if XNN_LIKELY(nc >= 8) {
126       _mm_storeu_si128((__m128i*) c3, _mm256_cvtps_ph(vacc3x01234567, _MM_FROUND_NO_EXC));
127       c3 = (uint16_t*) ((uintptr_t) c3 + cn_stride);
128       _mm_storeu_si128((__m128i*) c2, _mm256_cvtps_ph(vacc2x01234567, _MM_FROUND_NO_EXC));
129       c2 = (uint16_t*) ((uintptr_t) c2 + cn_stride);
130       _mm_storeu_si128((__m128i*) c1, _mm256_cvtps_ph(vacc1x01234567, _MM_FROUND_NO_EXC));
131       c1 = (uint16_t*) ((uintptr_t) c1 + cn_stride);
132       _mm_storeu_si128((__m128i*) c0, _mm256_cvtps_ph(vacc0x01234567, _MM_FROUND_NO_EXC));
133       c0 = (uint16_t*) ((uintptr_t) c0 + cn_stride);
134 
135       a = (const void**restrict) ((uintptr_t) a - ks);
136       nc -= 8;
137     } else {
138       __m128i vh3x01234567 = _mm256_cvtps_ph(vacc3x01234567, _MM_FROUND_NO_EXC);
139       __m128i vh2x01234567 = _mm256_cvtps_ph(vacc2x01234567, _MM_FROUND_NO_EXC);
140       __m128i vh1x01234567 = _mm256_cvtps_ph(vacc1x01234567, _MM_FROUND_NO_EXC);
141       __m128i vh0x01234567 = _mm256_cvtps_ph(vacc0x01234567, _MM_FROUND_NO_EXC);
142       if (nc & 4) {
143         _mm_storel_epi64((__m128i*) c3, vh3x01234567);
144         _mm_storel_epi64((__m128i*) c2, vh2x01234567);
145         _mm_storel_epi64((__m128i*) c1, vh1x01234567);
146         _mm_storel_epi64((__m128i*) c0, vh0x01234567);
147 
148         vh3x01234567 = _mm_unpackhi_epi64(vh3x01234567, vh3x01234567);
149         vh2x01234567 = _mm_unpackhi_epi64(vh2x01234567, vh2x01234567);
150         vh1x01234567 = _mm_unpackhi_epi64(vh1x01234567, vh1x01234567);
151         vh0x01234567 = _mm_unpackhi_epi64(vh0x01234567, vh0x01234567);
152 
153         c3 += 4;
154         c2 += 4;
155         c1 += 4;
156         c0 += 4;
157       }
158       if (nc & 2) {
159         _mm_storeu_si32(c3, vh3x01234567);
160         _mm_storeu_si32(c2, vh2x01234567);
161         _mm_storeu_si32(c1, vh1x01234567);
162         _mm_storeu_si32(c0, vh0x01234567);
163 
164         vh3x01234567 = _mm_srli_epi64(vh3x01234567, 32);
165         vh2x01234567 = _mm_srli_epi64(vh2x01234567, 32);
166         vh1x01234567 = _mm_srli_epi64(vh1x01234567, 32);
167         vh0x01234567 = _mm_srli_epi64(vh0x01234567, 32);
168 
169         c3 += 2;
170         c2 += 2;
171         c1 += 2;
172         c0 += 2;
173       }
174       if (nc & 1) {
175         *c3 = _mm_extract_epi16(vh3x01234567, 0);
176         *c2 = _mm_extract_epi16(vh2x01234567, 0);
177         *c1 = _mm_extract_epi16(vh1x01234567, 0);
178         *c0 = _mm_extract_epi16(vh0x01234567, 0);
179       }
180 
181       nc = 0;
182     }
183   } while (nc != 0);
184 }
185