xref: /aosp_15_r20/external/XNNPACK/src/x32-transposec/gen/4x4-multi-multi-wasmsimd.c (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Auto-generated file. Do not edit!
2 //   Template: src/x32-transposec/wasmsimd.c.in
3 //   Generator: tools/xngen
4 //
5 // Copyright 2021 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 <wasm_simd128.h>
11 
12 #include <assert.h>
13 
14 #include <xnnpack/common.h>
15 #include <xnnpack/math.h>
16 #include <xnnpack/transpose.h>
17 
xnn_x32_transposec_ukernel__4x4_multi_multi_wasmsimd(const uint32_t * input,uint32_t * output,size_t input_stride,size_t output_stride,size_t block_width,size_t block_height)18 void xnn_x32_transposec_ukernel__4x4_multi_multi_wasmsimd(
19     const uint32_t* input,
20     uint32_t* output,
21     size_t input_stride,
22     size_t output_stride,
23     size_t block_width,
24     size_t block_height) XNN_OOB_READS
25 {
26   assert(output_stride >= block_height * sizeof(uint32_t));
27   assert(input_stride >= block_width * sizeof(uint32_t));
28 
29   const size_t tile_height = 4;
30   const size_t tile_width = 4;
31   const size_t tile_hbytes = tile_height * sizeof(uint32_t);
32   const size_t tile_wbytes = tile_width * sizeof(uint32_t);
33   const size_t input_reset = tile_wbytes - round_down_po2(block_height, tile_height) * input_stride;
34   const size_t input_offset = tile_height * input_stride;
35   const size_t output_reset = tile_width * output_stride - round_down_po2(block_height, 2) * sizeof(uint32_t);
36 
37   const uint32_t* i0 = input;
38   const uint32_t* i1 = (const uint32_t*) ((uintptr_t) i0 + input_stride);
39   const uint32_t* i2 = (const uint32_t*) ((uintptr_t) i1 + input_stride);
40   const uint32_t* i3 = (const uint32_t*) ((uintptr_t) i2 + input_stride);
41   uint32_t* o0 = (uint32_t*) output;
42   uint32_t* o1 = (uint32_t*) ((uintptr_t) o0 + output_stride);
43   uint32_t* o2 = (uint32_t*) ((uintptr_t) o1 + output_stride);
44   uint32_t* o3 = (uint32_t*) ((uintptr_t) o2 + output_stride);
45 
46   do {
47     if XNN_UNPREDICTABLE(block_width < 2) {
48       o1 = o0;
49     }
50     if XNN_UNPREDICTABLE(block_width <= 2) {
51       o2 = o0;
52     }
53     if XNN_UNPREDICTABLE(block_width < 4) {
54       o3 = o0;
55     }
56     size_t bh = block_height;
57     for (; bh >= 4; bh -= 4) {
58       const v128_t v2_0 = wasm_v128_load(i0);
59       i0 = (uint32_t*) ((uintptr_t) i0 + input_offset);
60       const v128_t v2_1 = wasm_v128_load(i1);
61       i1 = (uint32_t*) ((uintptr_t) i1 + input_offset);
62       const v128_t v2_2 = wasm_v128_load(i2);
63       i2 = (uint32_t*) ((uintptr_t) i2 + input_offset);
64       const v128_t v2_3 = wasm_v128_load(i3);
65       i3 = (uint32_t*) ((uintptr_t) i3 + input_offset);
66 
67       const v128_t v1_0 = wasm_v32x4_shuffle(v2_0, v2_2, 0, 4, 1, 5);
68       const v128_t v1_1 = wasm_v32x4_shuffle(v2_0, v2_2, 2, 6, 3, 7);
69       const v128_t v1_2 = wasm_v32x4_shuffle(v2_1, v2_3, 0, 4, 1, 5);
70       const v128_t v1_3 = wasm_v32x4_shuffle(v2_1, v2_3, 2, 6, 3, 7);
71       const v128_t v0_0 = wasm_v32x4_shuffle(v1_0, v1_2, 0, 4, 1, 5);
72       const v128_t v0_1 = wasm_v32x4_shuffle(v1_0, v1_2, 2, 6, 3, 7);
73       const v128_t v0_2 = wasm_v32x4_shuffle(v1_1, v1_3, 0, 4, 1, 5);
74       const v128_t v0_3 = wasm_v32x4_shuffle(v1_1, v1_3, 2, 6, 3, 7);
75 
76       wasm_v128_store(o3, v0_3);
77       o3 = (uint32_t*) ((uintptr_t) o3 + tile_hbytes);
78       wasm_v128_store(o2, v0_2);
79       o2 = (uint32_t*) ((uintptr_t) o2 + tile_hbytes);
80       wasm_v128_store(o1, v0_1);
81       o1 = (uint32_t*) ((uintptr_t) o1 + tile_hbytes);
82       wasm_v128_store(o0, v0_0);
83       o0 = (uint32_t*) ((uintptr_t) o0 + tile_hbytes);
84     }
85 
86     if (bh != 0) {
87       const v128_t v2_0 = wasm_v128_load(i0);
88       if XNN_UNPREDICTABLE(bh < 2) {
89         i1 = i0;
90       }
91       const v128_t v2_1 = wasm_v128_load(i1);
92       if XNN_UNPREDICTABLE(bh <= 2) {
93         i2 = i0;
94       }
95       const v128_t v2_2 = wasm_v128_load(i2);
96       const v128_t v2_3 = wasm_v128_xor(v2_0, v2_0);
97 
98       const v128_t v1_0 = wasm_v32x4_shuffle(v2_0, v2_2, 0, 4, 1, 5);
99       const v128_t v1_1 = wasm_v32x4_shuffle(v2_0, v2_2, 2, 6, 3, 7);
100       const v128_t v1_2 = wasm_v32x4_shuffle(v2_1, v2_3, 0, 4, 1, 5);
101       const v128_t v1_3 = wasm_v32x4_shuffle(v2_1, v2_3, 2, 6, 3, 7);
102 
103       v128_t v0_0 = wasm_v32x4_shuffle(v1_0, v1_2, 0, 4, 1, 5);
104       v128_t v0_1 = wasm_v32x4_shuffle(v1_0, v1_2, 2, 6, 3, 7);
105       v128_t v0_2 = wasm_v32x4_shuffle(v1_1, v1_3, 0, 4, 1, 5);
106       v128_t v0_3 = wasm_v32x4_shuffle(v1_1, v1_3, 2, 6, 3, 7);
107 
108       if (bh & 2) {
109         *((double*) o3) = wasm_f64x2_extract_lane(v0_3, 0);
110         o3 += 2;
111         *((double*) o2) = wasm_f64x2_extract_lane(v0_2, 0);
112         o2 += 2;
113         *((double*) o1) = wasm_f64x2_extract_lane(v0_1, 0);
114         o1 += 2;
115         *((double*) o0) = wasm_f64x2_extract_lane(v0_0, 0);
116         o0 += 2;
117         v0_0 = wasm_v64x2_shuffle(v0_0, v0_0, 1, 1);
118         v0_1 = wasm_v64x2_shuffle(v0_1, v0_1, 1, 1);
119         v0_2 = wasm_v64x2_shuffle(v0_2, v0_2, 1, 1);
120         v0_3 = wasm_v64x2_shuffle(v0_3, v0_3, 1, 1);
121       }
122 
123       if (bh & 1) {
124         *((float*) o3) = wasm_f32x4_extract_lane(v0_3, 0);
125         *((float*) o2) = wasm_f32x4_extract_lane(v0_2, 0);
126         *((float*) o1) = wasm_f32x4_extract_lane(v0_1, 0);
127         *((float*) o0) = wasm_f32x4_extract_lane(v0_0, 0);
128       }
129     }
130 
131     i0 = (const uint32_t*) ((uintptr_t) i0 + input_reset);
132     i1 = (const uint32_t*) ((uintptr_t) i0 + input_stride);
133     i2 = (const uint32_t*) ((uintptr_t) i1 + input_stride);
134     i3 = (const uint32_t*) ((uintptr_t) i2 + input_stride);
135     o0 = (uint32_t*) ((uintptr_t) o0 + output_reset);
136     o1 = (uint32_t*) ((uintptr_t) o1 + output_reset);
137     o2 = (uint32_t*) ((uintptr_t) o2 + output_reset);
138     o3 = (uint32_t*) ((uintptr_t) o3 + output_reset);
139     block_width = doz(block_width, tile_width);
140   } while (block_width != 0);
141 }
142