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