xref: /aosp_15_r20/external/XNNPACK/src/f32-gemm/gen/1x8s4-minmax-sse.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-gemm/sse-shuffle.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_1x8s4__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_1x8s4__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_OOB_READS
28 {
29   assert(mr != 0);
30   assert(mr <= 1);
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 
41   do {
42     __m128 vacc0x0123 = _mm_load_ps(w + 0);
43     __m128 vacc0x4567 = _mm_load_ps(w + 4);
44     w += 8;
45 
46     size_t k = kc;
47     while (k >= 4 * sizeof(float)) {
48       __m128 va0 = _mm_loadu_ps(a0);
49       a0 += 4;
50 
51 
52       const __m128 vb0123c0 = _mm_load_ps(w + 0);
53       const __m128 vb4567c0 = _mm_load_ps(w + 4);
54 
55       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123c0));
56       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567c0));
57 
58       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
59 
60       const __m128 vb0123c1 = _mm_load_ps(w + 8);
61       const __m128 vb4567c1 = _mm_load_ps(w + 12);
62 
63       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123c1));
64       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567c1));
65 
66       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
67 
68       const __m128 vb0123c2 = _mm_load_ps(w + 16);
69       const __m128 vb4567c2 = _mm_load_ps(w + 20);
70 
71       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123c2));
72       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567c2));
73 
74       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
75 
76       const __m128 vb0123c3 = _mm_load_ps(w + 24);
77       const __m128 vb4567c3 = _mm_load_ps(w + 28);
78 
79       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(va0, vb0123c3));
80       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(va0, vb4567c3));
81 
82 
83       w += 32;
84       k -= 4 * sizeof(float);
85     }
86     if XNN_UNLIKELY(k != 0) {
87       __m128 va0 = _mm_loadu_ps(a0);
88       a0 = (const float*) ((uintptr_t) a0 + k);
89 
90 
91       const __m128 vb0123c0 = _mm_load_ps(w + 0);
92       const __m128 vb4567c0 = _mm_load_ps(w + 4);
93 
94       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(_mm_andnot_ps(_mm_cmpeq_ps(_mm_setzero_ps(), vb0123c0), va0), vb0123c0));
95       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(_mm_andnot_ps(_mm_cmpeq_ps(_mm_setzero_ps(), vb4567c0), va0), vb4567c0));
96 
97       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
98 
99       const __m128 vb0123c1 = _mm_load_ps(w + 8);
100       const __m128 vb4567c1 = _mm_load_ps(w + 12);
101 
102       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(_mm_andnot_ps(_mm_cmpeq_ps(_mm_setzero_ps(), vb0123c1), va0), vb0123c1));
103       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(_mm_andnot_ps(_mm_cmpeq_ps(_mm_setzero_ps(), vb4567c1), va0), vb4567c1));
104 
105       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
106 
107       const __m128 vb0123c2 = _mm_load_ps(w + 16);
108       const __m128 vb4567c2 = _mm_load_ps(w + 20);
109 
110       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(_mm_andnot_ps(_mm_cmpeq_ps(_mm_setzero_ps(), vb0123c2), va0), vb0123c2));
111       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(_mm_andnot_ps(_mm_cmpeq_ps(_mm_setzero_ps(), vb4567c2), va0), vb4567c2));
112 
113       va0 = _mm_shuffle_ps(va0, va0, _MM_SHUFFLE(0, 3, 2, 1));
114 
115       const __m128 vb0123c3 = _mm_load_ps(w + 24);
116       const __m128 vb4567c3 = _mm_load_ps(w + 28);
117 
118       vacc0x0123 = _mm_add_ps(vacc0x0123, _mm_mul_ps(_mm_andnot_ps(_mm_cmpeq_ps(_mm_setzero_ps(), vb0123c3), va0), vb0123c3));
119       vacc0x4567 = _mm_add_ps(vacc0x4567, _mm_mul_ps(_mm_andnot_ps(_mm_cmpeq_ps(_mm_setzero_ps(), vb4567c3), va0), vb4567c3));
120 
121 
122       w += 32;
123     }
124 
125     const __m128 vmax = _mm_load_ps(params->sse.max);
126     vacc0x0123 = _mm_min_ps(vacc0x0123, vmax);
127     vacc0x4567 = _mm_min_ps(vacc0x4567, vmax);
128 
129     const __m128 vmin = _mm_load_ps(params->sse.min);
130     vacc0x0123 = _mm_max_ps(vacc0x0123, vmin);
131     vacc0x4567 = _mm_max_ps(vacc0x4567, vmin);
132 
133     if XNN_LIKELY(nc >= 8) {
134       _mm_storeu_ps(c0, vacc0x0123);
135       _mm_storeu_ps(c0 + 4, vacc0x4567);
136       c0 = (float*) ((uintptr_t) c0 + cn_stride);
137 
138       a0 = (const float*) ((uintptr_t) a0 - kc);
139 
140       nc -= 8;
141     } else {
142       if (nc & 4) {
143         _mm_storeu_ps(c0, vacc0x0123);
144 
145         vacc0x0123 = vacc0x4567;
146 
147         c0 += 4;
148       }
149       if (nc & 2) {
150         _mm_storel_pi((__m64*) c0, vacc0x0123);
151 
152         vacc0x0123 = _mm_movehl_ps(vacc0x0123, vacc0x0123);
153 
154         c0 += 2;
155       }
156       if (nc & 1) {
157         _mm_store_ss(c0, vacc0x0123);
158       }
159 
160       nc = 0;
161     }
162   } while (nc != 0);
163 }
164