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