blob: aac5e19ebeb07ac8a68013bd5c327c27116b81c2 [file] [log] [blame]
Pablo Telloeb82fd22018-02-23 13:43:50 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2018 Arm Limited.
Pablo Telloeb82fd22018-02-23 13:43:50 +00003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#pragma once
25
26template <unsigned int IntBy, typename TIn, typename TOut>
Anthony Barbier5f707732018-07-03 16:22:02 +010027struct TransposeInterleaveCommon {
28 // Override the moveblock_1xY methods to improve performance
29 static inline void moveblock_1x1(const TIn *&in0, TOut *out) {
30 for (unsigned int i = 0; i < IntBy; i++) {
31 *out++ = static_cast<TOut>(*in0++);
32 }
33 }
34
35 static inline void moveblock_1x2(const TIn *&in0, const TIn *&in1, TOut *out) {
36 for (unsigned int i = 0; i < IntBy; i++) {
37 *out++ = static_cast<TOut>(*in0++);
38 }
39 for (unsigned int i = 0; i < IntBy; i++) {
40 *out++ = static_cast<TOut>(*in1++);
41 }
42 }
43
44 static inline void moveblock_1x4(const TIn *&in0, const TIn *&in1, const TIn *&in2, const TIn *&in3, TOut *out) {
45 for (unsigned int i = 0; i < IntBy; i++) {
46 *out++ = static_cast<TOut>(*in0++);
47 }
48 for (unsigned int i = 0; i < IntBy; i++) {
49 *out++ = static_cast<TOut>(*in1++);
50 }
51 for (unsigned int i = 0; i < IntBy; i++) {
52 *out++ = static_cast<TOut>(*in2++);
53 }
54 for (unsigned int i = 0; i < IntBy; i++) {
55 *out++ = static_cast<TOut>(*in3++);
56 }
57 }
58
59 static inline void Transform(TOut *out, const TIn *in, const int stride, const int x0, const int xmax, const int k0, const int kmax) {
60 const auto ldin = stride;
61
62 TOut *outarray = out;
63 const TIn *inarray = in;
64 TOut *outptr_base = outarray;
65 const TIn *inptr_base = inarray + x0 + (k0 * ldin);
66 int ldout = (kmax - k0) * IntBy;
67
68 int k=(kmax-k0);
69 for ( ; k>3; k-=4) {
70 TOut *outptr = outptr_base;
71 const TIn *inptr = inptr_base;
72 const TIn *inptr1 = inptr + ldin;
73 const TIn *inptr2 = inptr1 + ldin;
74 const TIn *inptr3 = inptr2 + ldin;
75
76 prefetch_3x(inptr);
77 prefetch_3x(inptr1);
78 prefetch_3x(inptr2);
79 prefetch_3x(inptr3);
80
81 outptr_base += IntBy * 4;
82 inptr_base += ldin * 4;
83
84 for (int x = (xmax-x0) / IntBy; x > 0 ; x--) {
85 moveblock_1x4(inptr, inptr1, inptr2, inptr3, outptr);
86 outptr += ldout;
Pablo Telloeb82fd22018-02-23 13:43:50 +000087 }
88 }
89
Anthony Barbier5f707732018-07-03 16:22:02 +010090 if (k) {
91 TOut *outptr = outptr_base;
92 const TIn *inptr = inptr_base;
93 const TIn *inptr1 = inptr + ldin;
94 const TIn *inptr2 = inptr1 + ldin;
Pablo Telloeb82fd22018-02-23 13:43:50 +000095
Anthony Barbier5f707732018-07-03 16:22:02 +010096 prefetch_3x(inptr);
97 prefetch_3x(inptr1);
98 prefetch_3x(inptr2);
Pablo Telloeb82fd22018-02-23 13:43:50 +000099
Anthony Barbier5f707732018-07-03 16:22:02 +0100100 for (int x = (xmax-x0) / IntBy; x > 0 ; x--) {
101 switch(k) {
102 case 3:
103 moveblock_1x2(inptr, inptr1, outptr);
104 moveblock_1x1(inptr2, outptr + IntBy * 2);
105 break;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000106
Anthony Barbier5f707732018-07-03 16:22:02 +0100107 case 2:
108 moveblock_1x2(inptr, inptr1, outptr);
109 break;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000110
Anthony Barbier5f707732018-07-03 16:22:02 +0100111 case 1:
112 moveblock_1x1(inptr, outptr);
113 break;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000114
Anthony Barbier5f707732018-07-03 16:22:02 +0100115 default:
116 UNREACHABLE("Impossible.");
Pablo Telloeb82fd22018-02-23 13:43:50 +0000117 }
Anthony Barbier5f707732018-07-03 16:22:02 +0100118
119 outptr += ldout;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000120 }
Anthony Barbier5f707732018-07-03 16:22:02 +0100121 }
Pablo Telloeb82fd22018-02-23 13:43:50 +0000122
Anthony Barbier5f707732018-07-03 16:22:02 +0100123 // Cope with ragged X cases
124 const unsigned int overflow = (xmax - x0) % IntBy;
125 if (overflow) {
126 const TIn *inptr_base = inarray + (xmax - overflow) + (k0 * ldin);
127 TOut *outptr = outarray + ((xmax - x0) / IntBy) * ldout;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000128
Anthony Barbier5f707732018-07-03 16:22:02 +0100129 for (int k=(kmax-k0); k>0; k--) {
130 const TIn *inptr = inptr_base;
131 inptr_base += ldin;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000132
Anthony Barbier5f707732018-07-03 16:22:02 +0100133 for (unsigned int x=0; x < IntBy; x++) {
134 TOut val = (x < overflow) ? static_cast<TOut>(*inptr++) : static_cast<TOut>(0);
135 *outptr++ = val;
Pablo Telloeb82fd22018-02-23 13:43:50 +0000136 }
137 }
138 }
Anthony Barbier5f707732018-07-03 16:22:02 +0100139}
Pablo Telloeb82fd22018-02-23 13:43:50 +0000140};