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