• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Google LLC
2 //
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree.
5 
6 #include <assert.h>
7 
8 #include <arm_neon.h>
9 
10 #include <xnnpack/common.h>
11 #include <xnnpack/math.h>
12 #include <xnnpack/transpose.h>
13 
14 static const uint8_t pos0[16] = {0, 1, 2, 3, 16, 17, 18, 19, 32, 33, 34, 35, 48, 49, 50, 51};
15 static const uint8_t pos1[16] = {4, 5, 6, 7, 20, 21, 22, 23, 36, 37, 38, 39, 52, 53, 54, 55};
16 static const uint8_t pos2[16] = {8, 9, 10, 11, 24, 25, 26, 27, 40, 41, 42, 43, 56, 57, 58, 59};
17 static const uint8_t pos3[16] = {12, 13, 14, 15, 28, 29, 30, 31, 44, 45, 46, 47, 60, 61, 62, 63};
18 
xnn_x32_transposec_ukernel__4x4_aarch64_neon_tbl(const uint32_t * input,uint32_t * output,size_t input_stride,size_t output_stride,size_t block_width,size_t block_height)19 void xnn_x32_transposec_ukernel__4x4_aarch64_neon_tbl(
20     const uint32_t* input,
21     uint32_t* output,
22     size_t input_stride,
23     size_t output_stride,
24     size_t block_width,
25     size_t block_height) XNN_OOB_READS
26 {
27   assert(output_stride >= block_height * sizeof(uint32_t));
28   assert(input_stride >= block_width * sizeof(uint32_t));
29 
30   const size_t tile_height = 4;
31   const size_t tile_width = 4;
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 output_reset = tile_height * output_stride - round_down_po2(block_height, 2) * sizeof(uint32_t);
35   const size_t tile_stride = tile_height * input_stride;
36 
37   const uint8_t* i0 = (const uint8_t*) input;
38   const uint8_t* i1 = (const uint8_t*) ((uintptr_t) i0 + input_stride);
39   const uint8_t* i2 = (const uint8_t*) ((uintptr_t) i1 + input_stride);
40   const uint8_t* i3 = (const uint8_t*) ((uintptr_t) i2 + input_stride);
41 
42   uint8_t* o0 = (uint8_t*) output;
43   uint8_t* o1 = (uint8_t*) ((uintptr_t) o0 + output_stride);
44   uint8_t* o2 = (uint8_t*) ((uintptr_t) o1 + output_stride);
45   uint8_t* o3 = (uint8_t*) ((uintptr_t) o2 + output_stride);
46 
47   const uint8x16_t vperm0 = vld1q_u8(pos0);
48   const uint8x16_t vperm1 = vld1q_u8(pos1);
49   const uint8x16_t vperm2 = vld1q_u8(pos2);
50   const uint8x16_t vperm3 = vld1q_u8(pos3);
51   do {
52     if XNN_UNPREDICTABLE(block_width < 2) {
53       o1 = o0;
54     }
55     if XNN_UNPREDICTABLE(block_width <= 2) {
56       o2 = o0;
57     }
58     if XNN_UNPREDICTABLE(block_width < 4) {
59       o3 = o0;
60     }
61     size_t bh = block_height;
62     for (; bh >= 4; bh -= 4) {
63       uint8x16x4_t v;
64       v.val[0] = vld1q_u8(i0); i0 = (const uint8_t*) ((uintptr_t) i0 + tile_stride);
65       v.val[1] = vld1q_u8(i1); i1 = (const uint8_t*) ((uintptr_t) i1 + tile_stride);
66       v.val[2] = vld1q_u8(i2); i2 = (const uint8_t*) ((uintptr_t) i2 + tile_stride);
67       v.val[3] = vld1q_u8(i3); i3 = (const uint8_t*) ((uintptr_t) i3 + tile_stride);
68 
69       uint8x16_t vres0 = vqtbl4q_u8(v, vperm0);
70       uint8x16_t vres1 = vqtbl4q_u8(v, vperm1);
71       uint8x16_t vres2 = vqtbl4q_u8(v, vperm2);
72       uint8x16_t vres3 = vqtbl4q_u8(v, vperm3);
73 
74       vst1q_u8(o3, vres3); o3 = (uint8_t*) ((uintptr_t) o3 + tile_wbytes);
75       vst1q_u8(o2, vres2); o2 = (uint8_t*) ((uintptr_t) o2 + tile_wbytes);
76       vst1q_u8(o1, vres1); o1 = (uint8_t*) ((uintptr_t) o1 + tile_wbytes);
77       vst1q_u8(o0, vres0); o0 = (uint8_t*) ((uintptr_t) o0 + tile_wbytes);
78     }
79 
80     if (bh != 0) {
81       if XNN_UNPREDICTABLE(bh <= 2) {
82         i2 = i0;
83       }
84       if XNN_UNPREDICTABLE(bh < 2) {
85         i1 = i0;
86       }
87       uint8x16x4_t v;
88       v.val[0] = vld1q_u8(i0);
89       v.val[1] = vld1q_u8(i1);
90       v.val[2] = vld1q_u8(i2);
91 
92       uint8x16_t vres0 = vqtbl4q_u8(v, vperm0);
93       uint8x16_t vres1 = vqtbl4q_u8(v, vperm1);
94       uint8x16_t vres2 = vqtbl4q_u8(v, vperm2);
95       uint8x16_t vres3 = vqtbl4q_u8(v, vperm3);
96 
97       uint8x8_t vres0_low = vget_low_u8(vres0);
98       uint8x8_t vres1_low = vget_low_u8(vres1);
99       uint8x8_t vres2_low = vget_low_u8(vres2);
100       uint8x8_t vres3_low = vget_low_u8(vres3);
101 
102       if (bh & 2) {
103         vst1_u8(o3, vres3_low); o3 += 8;
104         vst1_u8(o2, vres2_low); o2 += 8;
105         vst1_u8(o1, vres1_low); o1 += 8;
106         vst1_u8(o0, vres0_low); o0 += 8;
107         vres0_low = vget_high_u8(vres0);
108         vres1_low = vget_high_u8(vres1);
109         vres2_low = vget_high_u8(vres2);
110         vres3_low = vget_high_u8(vres3);
111       }
112       if (bh & 1) {
113         vst1_lane_u32((void*) o3, vreinterpret_u32_u8(vres3_low), 0);
114         vst1_lane_u32((void*) o2, vreinterpret_u32_u8(vres2_low), 0);
115         vst1_lane_u32((void*) o1, vreinterpret_u32_u8(vres1_low), 0);
116         vst1_lane_u32((void*) o0, vreinterpret_u32_u8(vres0_low), 0);
117       }
118     }
119     i0 = (const uint8_t*) ((uintptr_t) i0 + input_reset);
120     i1 = (const uint8_t*) ((uintptr_t) i0 + input_stride);
121     i2 = (const uint8_t*) ((uintptr_t) i1 + input_stride);
122     i3 = (const uint8_t*) ((uintptr_t) i2 + input_stride);
123     o0 = (uint8_t*) ((uintptr_t) o0 + output_reset);
124     o1 = (uint8_t*) ((uintptr_t) o1 + output_reset);
125     o2 = (uint8_t*) ((uintptr_t) o2 + output_reset);
126     o3 = (uint8_t*) ((uintptr_t) o3 + output_reset);
127     block_width = doz(block_width, tile_width);
128   } while (block_width != 0);
129 }
130