xref: /aosp_15_r20/external/XNNPACK/src/f32-spmm/gen/4x1-minmax-wasmsimd-x86.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/f32-spmm/wasmsimd.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2020 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 <wasm_simd128.h>
13 
14 #include <xnnpack/spmm.h>
15 
16 
xnn_f32_spmm_minmax_ukernel_4x1__wasmsimd_x86(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__wasmsimd_x86(
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 v128_t vmin = wasm_v128_load64_splat(params->wasmsimd.min);
33   const v128_t vmax = wasm_v128_load64_splat(params->wasmsimd.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       v128_t vacc0123 = wasm_v128_load32_splat(w); w += 1;
43       if XNN_LIKELY(nnz != 0) {
44         do {
45           const intptr_t diff = *dmap++;
46           const v128_t vi0123 = wasm_v128_load(input);
47           input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
48           const v128_t vw = wasm_v128_load32_splat(w); w += 1;
49           vacc0123 = wasm_f32x4_add(vacc0123, wasm_f32x4_mul(vi0123, vw));
50         } while (--nnz != 0);
51       }
52       v128_t vout0123 = wasm_f32x4_pmin(vmax, vacc0123);
53       vout0123 = wasm_f32x4_pmax(vmin, vout0123);
54       wasm_v128_store(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         v128_t vacc01 = wasm_v128_load32_splat(w); w += 1;
71         if XNN_LIKELY(nnz != 0) {
72           do {
73             const intptr_t diff = *dmap++;
74             const v128_t vi01 = wasm_v128_load64_splat(input);
75             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
76             const v128_t vw = wasm_v128_load32_splat(w); w += 1;
77             vacc01 = wasm_f32x4_add(vacc01, wasm_f32x4_mul(vi01, vw));
78           } while (--nnz != 0);
79         }
80         v128_t vout01 = wasm_f32x4_pmin(vmax, vacc01);
81         vout01 = wasm_f32x4_pmax(vmin, vout01);
82         *((double*) output) = wasm_f64x2_extract_lane(vout01, 0);
83 
84         output = (float*restrict) ((uintptr_t) output + output_stride);
85       } while (--n != 0);
86       output = (float*restrict) ((uintptr_t) output - output_decrement);
87       input += 2;
88     }
89     output_decrement += 1 * sizeof(float);
90     if (mc & (1 * sizeof(float))) {
91       const float*restrict w = weights;
92       const int32_t* dmap = widx_dmap;
93       const uint32_t* nnzmap = nidx_nnzmap;
94       size_t n = nc;
95       do {
96         uint32_t nnz = *nnzmap++;
97         v128_t vacc0 = wasm_v128_load32_splat(w); w += 1;
98         if XNN_LIKELY(nnz != 0) {
99           do {
100             const intptr_t diff = *dmap++;
101             const v128_t vi0 = wasm_v128_load32_splat(input);
102             input = (const float*restrict) ((uintptr_t) input + (uintptr_t) diff);
103             const v128_t vw = wasm_v128_load32_splat(w); w += 1;
104             vacc0 = wasm_f32x4_add(vacc0, wasm_f32x4_mul(vi0, vw));
105           } while (--nnz != 0);
106         }
107         v128_t vout0 = wasm_f32x4_pmin(vmax, vacc0);
108         vout0 = wasm_f32x4_pmax(vmin, vout0);
109         *output = wasm_f32x4_extract_lane(vout0, 0);
110 
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