1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-gemm/MRx2c4-sse.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 <xmmintrin.h>
13 
14 #include <xnnpack/gemm.h>
15 
16 
xnn_f32_gemm_minmax_ukernel_4x2c4__sse(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_4x2c4__sse(
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)]) XNN_DISABLE_TSAN
28 {
29   assert(mr != 0);
30   assert(mr <= 4);
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 
59   do {
60     __m128 vacc0x0c4 = _mm_load_ss(w);
61     __m128 vacc0x1c4 = _mm_load_ss(w + 1);
62     __m128 vacc1x0c4 = vacc0x0c4;
63     __m128 vacc1x1c4 = vacc0x1c4;
64     __m128 vacc2x0c4 = vacc0x0c4;
65     __m128 vacc2x1c4 = vacc0x1c4;
66     __m128 vacc3x0c4 = vacc0x0c4;
67     __m128 vacc3x1c4 = vacc0x1c4;
68     w += 2;
69 
70     size_t k = kc;
71     for (; k >= 4 * sizeof(float); k -= 4 * sizeof(float)) {
72       const __m128 va0 = _mm_loadu_ps(a0);
73       a0 += 4;
74       const __m128 va1 = _mm_loadu_ps(a1);
75       a1 += 4;
76       const __m128 va2 = _mm_loadu_ps(a2);
77       a2 += 4;
78       const __m128 va3 = _mm_loadu_ps(a3);
79       a3 += 4;
80 
81       const __m128 vb0 = _mm_loadu_ps(w);
82       const __m128 vb1 = _mm_loadu_ps(w + 4);
83       w += 8;
84 
85       vacc0x0c4 = _mm_add_ps(vacc0x0c4, _mm_mul_ps(va0, vb0));
86       vacc0x1c4 = _mm_add_ps(vacc0x1c4, _mm_mul_ps(va0, vb1));
87       vacc1x0c4 = _mm_add_ps(vacc1x0c4, _mm_mul_ps(va1, vb0));
88       vacc1x1c4 = _mm_add_ps(vacc1x1c4, _mm_mul_ps(va1, vb1));
89       vacc2x0c4 = _mm_add_ps(vacc2x0c4, _mm_mul_ps(va2, vb0));
90       vacc2x1c4 = _mm_add_ps(vacc2x1c4, _mm_mul_ps(va2, vb1));
91       vacc3x0c4 = _mm_add_ps(vacc3x0c4, _mm_mul_ps(va3, vb0));
92       vacc3x1c4 = _mm_add_ps(vacc3x1c4, _mm_mul_ps(va3, vb1));
93     }
94     if XNN_UNLIKELY(k != 0) {
95       const __m128 va0 = _mm_loadu_ps(a0);
96       a0 = (const float*) ((uintptr_t) a0 + k);
97       const __m128 va1 = _mm_loadu_ps(a1);
98       a1 = (const float*) ((uintptr_t) a1 + k);
99       const __m128 va2 = _mm_loadu_ps(a2);
100       a2 = (const float*) ((uintptr_t) a2 + k);
101       const __m128 va3 = _mm_loadu_ps(a3);
102       a3 = (const float*) ((uintptr_t) a3 + k);
103 
104       const __m128 vb0 = _mm_loadu_ps(w);
105       const __m128 vb1 = _mm_loadu_ps(w + 4);
106       w += 8;
107 
108       const __m128 vmask0 = _mm_cmpeq_ps(_mm_setzero_ps(), vb0);
109       const __m128 vmask1 = _mm_cmpeq_ps(_mm_setzero_ps(), vb1);
110 
111       vacc0x0c4 = _mm_add_ps(vacc0x0c4, _mm_mul_ps(_mm_andnot_ps(vmask0, va0), vb0));
112       vacc0x1c4 = _mm_add_ps(vacc0x1c4, _mm_mul_ps(_mm_andnot_ps(vmask1, va0), vb1));
113       vacc1x0c4 = _mm_add_ps(vacc1x0c4, _mm_mul_ps(_mm_andnot_ps(vmask0, va1), vb0));
114       vacc1x1c4 = _mm_add_ps(vacc1x1c4, _mm_mul_ps(_mm_andnot_ps(vmask1, va1), vb1));
115       vacc2x0c4 = _mm_add_ps(vacc2x0c4, _mm_mul_ps(_mm_andnot_ps(vmask0, va2), vb0));
116       vacc2x1c4 = _mm_add_ps(vacc2x1c4, _mm_mul_ps(_mm_andnot_ps(vmask1, va2), vb1));
117       vacc3x0c4 = _mm_add_ps(vacc3x0c4, _mm_mul_ps(_mm_andnot_ps(vmask0, va3), vb0));
118       vacc3x1c4 = _mm_add_ps(vacc3x1c4, _mm_mul_ps(_mm_andnot_ps(vmask1, va3), vb1));
119     }
120 
121     const __m128 vacc0x01c2 = _mm_add_ps(_mm_unpacklo_ps(vacc0x0c4, vacc0x1c4), _mm_unpackhi_ps(vacc0x0c4, vacc0x1c4));
122     const __m128 vacc1x01c2 = _mm_add_ps(_mm_unpacklo_ps(vacc1x0c4, vacc1x1c4), _mm_unpackhi_ps(vacc1x0c4, vacc1x1c4));
123     const __m128 vacc2x01c2 = _mm_add_ps(_mm_unpacklo_ps(vacc2x0c4, vacc2x1c4), _mm_unpackhi_ps(vacc2x0c4, vacc2x1c4));
124     const __m128 vacc3x01c2 = _mm_add_ps(_mm_unpacklo_ps(vacc3x0c4, vacc3x1c4), _mm_unpackhi_ps(vacc3x0c4, vacc3x1c4));
125 
126     __m128 vacc01x01 = _mm_add_ps(_mm_movelh_ps(vacc0x01c2, vacc1x01c2), _mm_movehl_ps(vacc1x01c2, vacc0x01c2));
127     __m128 vacc23x01 = _mm_add_ps(_mm_movelh_ps(vacc2x01c2, vacc3x01c2), _mm_movehl_ps(vacc3x01c2, vacc2x01c2));
128 
129     const __m128 vmax = _mm_load_ps(params->sse.max);
130     vacc01x01 = _mm_min_ps(vacc01x01, vmax);
131     vacc23x01 = _mm_min_ps(vacc23x01, vmax);
132 
133     const __m128 vmin = _mm_load_ps(params->sse.min);
134     vacc01x01 = _mm_max_ps(vacc01x01, vmin);
135     vacc23x01 = _mm_max_ps(vacc23x01, vmin);
136 
137     if XNN_LIKELY(nc >= 2) {
138       _mm_storel_pi((__m64*) c2, vacc23x01);
139       c2 = (float*) ((uintptr_t) c2 + cn_stride);
140       a2 = (const float*) ((uintptr_t) a2 - kc);
141       _mm_storeh_pi((__m64*) c3, vacc23x01);
142       c3 = (float*) ((uintptr_t) c3 + cn_stride);
143       a3 = (const float*) ((uintptr_t) a3 - kc);
144       _mm_storel_pi((__m64*) c0, vacc01x01);
145       c0 = (float*) ((uintptr_t) c0 + cn_stride);
146       a0 = (const float*) ((uintptr_t) a0 - kc);
147       _mm_storeh_pi((__m64*) c1, vacc01x01);
148       c1 = (float*) ((uintptr_t) c1 + cn_stride);
149       a1 = (const float*) ((uintptr_t) a1 - kc);
150 
151       nc -= 2;
152     } else {
153       assert(nc == 1);
154       _mm_store_ss(c2, vacc23x01);
155       _mm_store_ss(c3, _mm_movehl_ps(vacc23x01, vacc23x01));
156       _mm_store_ss(c0, vacc01x01);
157       _mm_store_ss(c1, _mm_movehl_ps(vacc01x01, vacc01x01));
158 
159       nc = 0;
160     }
161   } while (nc != 0);
162 }
163