Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 Arm Limited. |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 3 | * |
| 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 | |
| 26 | template <unsigned int IntBy, typename TIn, typename TOut> |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 27 | struct 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 Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 90 | 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 Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 95 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 96 | prefetch_3x(inptr); |
| 97 | prefetch_3x(inptr1); |
| 98 | prefetch_3x(inptr2); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 99 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 100 | 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 Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 106 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 107 | case 2: |
| 108 | moveblock_1x2(inptr, inptr1, outptr); |
| 109 | break; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 110 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 111 | case 1: |
| 112 | moveblock_1x1(inptr, outptr); |
| 113 | break; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 114 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 115 | default: |
| 116 | UNREACHABLE("Impossible."); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 117 | } |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 118 | |
| 119 | outptr += ldout; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 120 | } |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 121 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 122 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 123 | // 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 Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 128 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 129 | for (int k=(kmax-k0); k>0; k--) { |
| 130 | const TIn *inptr = inptr_base; |
| 131 | inptr_base += ldin; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 132 | |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 133 | 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 Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
Anthony Barbier | 5f70773 | 2018-07-03 16:22:02 +0100 | [diff] [blame] | 139 | } |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 140 | }; |