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