xref: /aosp_15_r20/external/XNNPACK/src/f32-spmm/gen/4x1-minmax-sse.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-spmm/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 <immintrin.h>
13 
14 #include <xnnpack/spmm.h>
15 
16 
xnn_f32_spmm_minmax_ukernel_4x1__sse(size_t mc,size_t nc,const float * restrict input,const float * restrict weights,const int32_t * restrict widx_dmap,const uint32_t * restrict nidx_nnzmap,float * restrict output,size_t output_stride,const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])17 void xnn_f32_spmm_minmax_ukernel_4x1__sse(
18     size_t mc,
19     size_t nc,
20     const float*restrict input,
21     const float*restrict weights,
22     const int32_t*restrict widx_dmap,
23     const uint32_t*restrict nidx_nnzmap,
24     float*restrict output,
25     size_t output_stride,
26     const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)])
27 {
28   assert(mc != 0);
29   assert(mc % sizeof(float) == 0);
30   assert(nc != 0);
31 
32   const __m128 vmin = _mm_load_ps(params->sse.min);
33   const __m128 vmax = _mm_load_ps(params->sse.max);
34   size_t output_decrement = output_stride * nc - 4 * sizeof(float);
35   while XNN_LIKELY(mc >= 4 * sizeof(float)) {
36     const float*restrict w = weights;
37     const int32_t* dmap = widx_dmap;
38     const uint32_t* nnzmap = nidx_nnzmap;
39     size_t n = nc;
40     do {
41       uint32_t nnz = *nnzmap++;
42       __m128 vacc0123 = _mm_load1_ps(w); w += 1;
43       if XNN_LIKELY(nnz != 0) {
44         do {
45           const intptr_t diff = *dmap++;
46           const __m128 vi0123 = _mm_loadu_ps(input);
47           input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
48           const __m128 vw = _mm_load1_ps(w); w += 1;
49           vacc0123 = _mm_add_ps(vacc0123, _mm_mul_ps(vi0123, vw));
50         } while (--nnz != 0);
51       }
52       __m128 vout0123 = _mm_min_ps(vacc0123, vmax);
53       vout0123 = _mm_max_ps(vout0123, vmin);
54       _mm_storeu_ps(output, vout0123);
55       output = (float*restrict) ((uintptr_t) output + output_stride);
56     } while (--n != 0);
57     output = (float*restrict) ((uintptr_t) output - output_decrement);
58     input += 4;
59     mc -= 4 * sizeof(float);
60   }
61   if XNN_UNLIKELY(mc != 0) {
62     output_decrement += 2 * sizeof(float);
63     if (mc & (2 * sizeof(float))) {
64       const float*restrict w = weights;
65       const int32_t* dmap = widx_dmap;
66       const uint32_t* nnzmap = nidx_nnzmap;
67       size_t n = nc;
68       do {
69         uint32_t nnz = *nnzmap++;
70         __m128 vacc01 = _mm_load_ss(w); w += 1;
71         vacc01 = _mm_unpacklo_ps(vacc01, vacc01);
72         if XNN_LIKELY(nnz != 0) {
73           do {
74             const intptr_t diff = *dmap++;
75             const __m128 vi01 = _mm_loadl_pi(_mm_undefined_ps(), (const __m64*) input);
76             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
77             __m128 vw = _mm_load_ss(w); w += 1;
78             vw = _mm_unpacklo_ps(vw, vw);
79             vacc01 = _mm_add_ps(vacc01, _mm_mul_ps(vi01, vw));
80           } while (--nnz != 0);
81         }
82         __m128 vout01 = _mm_min_ps(vacc01, vmax);
83         vout01 = _mm_max_ps(vout01, vmin);
84         _mm_storel_pi((__m64*) output, vout01);
85         output = (float*restrict) ((uintptr_t) output + output_stride);
86       } while (--n != 0);
87       output = (float*restrict) ((uintptr_t) output - output_decrement);
88       input += 2;
89     }
90     output_decrement += 1 * sizeof(float);
91     if (mc & (1 * sizeof(float))) {
92       const float*restrict w = weights;
93       const int32_t* dmap = widx_dmap;
94       const uint32_t* nnzmap = nidx_nnzmap;
95       size_t n = nc;
96       do {
97         uint32_t nnz = *nnzmap++;
98         __m128 vacc0 = _mm_load_ss(w); w += 1;
99         if XNN_LIKELY(nnz != 0) {
100           do {
101             const intptr_t diff = *dmap++;
102             const __m128 vi0 = _mm_load_ss(input);
103             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
104             const __m128 vw = _mm_load_ss(w); w += 1;
105             vacc0 = _mm_add_ss(vacc0, _mm_mul_ss(vi0, vw));
106           } while (--nnz != 0);
107         }
108         __m128 vout0 = _mm_min_ss(vacc0, vmax);
109         vout0 = _mm_max_ss(vout0, vmin);
110         _mm_store_ss(output, vout0);
111         output = (float*restrict) ((uintptr_t) output + output_stride);
112       } while (--n != 0);
113       output = (float*restrict) ((uintptr_t) output - output_decrement);
114       input += 1;
115     }
116   }
117 }
118