1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-gemm/avx-broadcast.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2019 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/gemm.h>
15 
16 
xnn_f32_gemminc_minmax_ukernel_4x16__avx_broadcast(size_t mr,size_t nc,size_t kc,const float * restrict a,size_t a_stride,const float * restrict w,float * restrict c,size_t cm_stride,size_t cn_stride,const float * restrict acc,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_gemminc_minmax_ukernel_4x16__avx_broadcast(
18     size_t mr,
19     size_t nc,
20     size_t kc,
21     const float*restrict a,
22     size_t a_stride,
23     const float*restrict w,
24     float*restrict c,
25     size_t cm_stride,
26     size_t cn_stride,
27     const float*restrict acc,
28     const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
29 {
30   assert(mr != 0);
31   assert(mr <= 4);
32   assert(nc != 0);
33   assert(kc != 0);
34   assert(kc % sizeof(float) == 0);
35   assert(a != NULL);
36   assert(w != NULL);
37   assert(c != NULL);
38   assert(acc != NULL);
39 
40   const float* a0 = a;
41   float* c0 = c;
42   const float* a1 = (const float*) ((uintptr_t) a0 + a_stride);
43   float* c1 = (float*) ((uintptr_t) c0 + cm_stride);
44   if XNN_UNPREDICTABLE(mr < 2) {
45     a1 = a0;
46     c1 = c0;
47   }
48   const float* a2 = (const float*) ((uintptr_t) a1 + a_stride);
49   float* c2 = (float*) ((uintptr_t) c1 + cm_stride);
50   if XNN_UNPREDICTABLE(mr <= 2) {
51     a2 = a1;
52     c2 = c1;
53   }
54   const float* a3 = (const float*) ((uintptr_t) a2 + a_stride);
55   float* c3 = (float*) ((uintptr_t) c2 + cm_stride);
56   if XNN_UNPREDICTABLE(mr != 4) {
57     a3 = a2;
58     c3 = c2;
59   }
60 
61   do {
62     __m256 vacc0x01234567 = _mm256_load_ps(acc + 0);
63     __m256 vacc0x89ABCDEF = _mm256_load_ps(acc + 8);
64     __m256 vacc1x01234567 = _mm256_load_ps(acc + 16);
65     __m256 vacc1x89ABCDEF = _mm256_load_ps(acc + 24);
66     __m256 vacc2x01234567 = _mm256_load_ps(acc + 32);
67     __m256 vacc2x89ABCDEF = _mm256_load_ps(acc + 40);
68     __m256 vacc3x01234567 = _mm256_load_ps(acc + 48);
69     __m256 vacc3x89ABCDEF = _mm256_load_ps(acc + 56);
70     acc += 64;
71 
72     size_t k = kc;
73     do {
74       const __m256 va0 = _mm256_broadcast_ss(a0);
75       a0 += 1;
76       const __m256 va1 = _mm256_broadcast_ss(a1);
77       a1 += 1;
78       const __m256 va2 = _mm256_broadcast_ss(a2);
79       a2 += 1;
80       const __m256 va3 = _mm256_broadcast_ss(a3);
81       a3 += 1;
82 
83       const __m256 vb01234567 = _mm256_load_ps(w);
84       const __m256 vb89ABCDEF = _mm256_load_ps(w + 8);
85       w += 16;
86 
87       vacc0x01234567 = _mm256_add_ps(vacc0x01234567, _mm256_mul_ps(va0, vb01234567));
88       vacc1x01234567 = _mm256_add_ps(vacc1x01234567, _mm256_mul_ps(va1, vb01234567));
89       vacc2x01234567 = _mm256_add_ps(vacc2x01234567, _mm256_mul_ps(va2, vb01234567));
90       vacc3x01234567 = _mm256_add_ps(vacc3x01234567, _mm256_mul_ps(va3, vb01234567));
91       vacc0x89ABCDEF = _mm256_add_ps(vacc0x89ABCDEF, _mm256_mul_ps(va0, vb89ABCDEF));
92       vacc1x89ABCDEF = _mm256_add_ps(vacc1x89ABCDEF, _mm256_mul_ps(va1, vb89ABCDEF));
93       vacc2x89ABCDEF = _mm256_add_ps(vacc2x89ABCDEF, _mm256_mul_ps(va2, vb89ABCDEF));
94       vacc3x89ABCDEF = _mm256_add_ps(vacc3x89ABCDEF, _mm256_mul_ps(va3, vb89ABCDEF));
95 
96       k -= sizeof(float);
97     } while (k != 0);
98 
99     const __m256 vmax = _mm256_broadcast_ps((const __m128*) params->sse.max);
100     vacc0x01234567 = _mm256_min_ps(vacc0x01234567, vmax);
101     vacc1x01234567 = _mm256_min_ps(vacc1x01234567, vmax);
102     vacc2x01234567 = _mm256_min_ps(vacc2x01234567, vmax);
103     vacc3x01234567 = _mm256_min_ps(vacc3x01234567, vmax);
104     vacc0x89ABCDEF = _mm256_min_ps(vacc0x89ABCDEF, vmax);
105     vacc1x89ABCDEF = _mm256_min_ps(vacc1x89ABCDEF, vmax);
106     vacc2x89ABCDEF = _mm256_min_ps(vacc2x89ABCDEF, vmax);
107     vacc3x89ABCDEF = _mm256_min_ps(vacc3x89ABCDEF, vmax);
108 
109     const __m256 vmin = _mm256_broadcast_ps((const __m128*) params->sse.min);
110     vacc0x01234567 = _mm256_max_ps(vacc0x01234567, vmin);
111     vacc1x01234567 = _mm256_max_ps(vacc1x01234567, vmin);
112     vacc2x01234567 = _mm256_max_ps(vacc2x01234567, vmin);
113     vacc3x01234567 = _mm256_max_ps(vacc3x01234567, vmin);
114     vacc0x89ABCDEF = _mm256_max_ps(vacc0x89ABCDEF, vmin);
115     vacc1x89ABCDEF = _mm256_max_ps(vacc1x89ABCDEF, vmin);
116     vacc2x89ABCDEF = _mm256_max_ps(vacc2x89ABCDEF, vmin);
117     vacc3x89ABCDEF = _mm256_max_ps(vacc3x89ABCDEF, vmin);
118 
119     if XNN_LIKELY(nc >= 16) {
120       _mm256_storeu_ps(c3, vacc3x01234567);
121       _mm256_storeu_ps(c3 + 8, vacc3x89ABCDEF);
122       c3 = (float*) ((uintptr_t) c3 + cn_stride);
123       _mm256_storeu_ps(c2, vacc2x01234567);
124       _mm256_storeu_ps(c2 + 8, vacc2x89ABCDEF);
125       c2 = (float*) ((uintptr_t) c2 + cn_stride);
126       _mm256_storeu_ps(c1, vacc1x01234567);
127       _mm256_storeu_ps(c1 + 8, vacc1x89ABCDEF);
128       c1 = (float*) ((uintptr_t) c1 + cn_stride);
129       _mm256_storeu_ps(c0, vacc0x01234567);
130       _mm256_storeu_ps(c0 + 8, vacc0x89ABCDEF);
131       c0 = (float*) ((uintptr_t) c0 + cn_stride);
132 
133       a3 = (const float*) ((uintptr_t) a3 - kc);
134       a2 = (const float*) ((uintptr_t) a2 - kc);
135       a1 = (const float*) ((uintptr_t) a1 - kc);
136       a0 = (const float*) ((uintptr_t) a0 - kc);
137 
138       nc -= 16;
139     } else {
140       if (nc & 8) {
141         _mm256_storeu_ps(c3, vacc3x01234567);
142         _mm256_storeu_ps(c2, vacc2x01234567);
143         _mm256_storeu_ps(c1, vacc1x01234567);
144         _mm256_storeu_ps(c0, vacc0x01234567);
145 
146         vacc3x01234567 = vacc3x89ABCDEF;
147         vacc2x01234567 = vacc2x89ABCDEF;
148         vacc1x01234567 = vacc1x89ABCDEF;
149         vacc0x01234567 = vacc0x89ABCDEF;
150 
151         c3 += 8;
152         c2 += 8;
153         c1 += 8;
154         c0 += 8;
155       }
156       __m128 vacc3x0123 = _mm256_castps256_ps128(vacc3x01234567);
157       __m128 vacc2x0123 = _mm256_castps256_ps128(vacc2x01234567);
158       __m128 vacc1x0123 = _mm256_castps256_ps128(vacc1x01234567);
159       __m128 vacc0x0123 = _mm256_castps256_ps128(vacc0x01234567);
160       if (nc & 4) {
161         _mm_storeu_ps(c3, vacc3x0123);
162         _mm_storeu_ps(c2, vacc2x0123);
163         _mm_storeu_ps(c1, vacc1x0123);
164         _mm_storeu_ps(c0, vacc0x0123);
165 
166         vacc3x0123 = _mm256_extractf128_ps(vacc3x01234567, 1);
167         vacc2x0123 = _mm256_extractf128_ps(vacc2x01234567, 1);
168         vacc1x0123 = _mm256_extractf128_ps(vacc1x01234567, 1);
169         vacc0x0123 = _mm256_extractf128_ps(vacc0x01234567, 1);
170 
171         c3 += 4;
172         c2 += 4;
173         c1 += 4;
174         c0 += 4;
175       }
176       if (nc & 2) {
177         _mm_storel_pi((__m64*) c3, vacc3x0123);
178         _mm_storel_pi((__m64*) c2, vacc2x0123);
179         _mm_storel_pi((__m64*) c1, vacc1x0123);
180         _mm_storel_pi((__m64*) c0, vacc0x0123);
181 
182         vacc3x0123 = _mm_movehl_ps(vacc3x0123, vacc3x0123);
183         vacc2x0123 = _mm_movehl_ps(vacc2x0123, vacc2x0123);
184         vacc1x0123 = _mm_movehl_ps(vacc1x0123, vacc1x0123);
185         vacc0x0123 = _mm_movehl_ps(vacc0x0123, vacc0x0123);
186 
187         c3 += 2;
188         c2 += 2;
189         c1 += 2;
190         c0 += 2;
191       }
192       if (nc & 1) {
193         _mm_store_ss(c3, vacc3x0123);
194         _mm_store_ss(c2, vacc2x0123);
195         _mm_store_ss(c1, vacc1x0123);
196         _mm_store_ss(c0, vacc0x0123);
197       }
198 
199       nc = 0;
200     }
201   } while (nc != 0);
202 }
203