Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [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 | /* |
| 27 | * Generic transform. |
| 28 | * |
| 29 | * Assuming the untransposed case, this works by first reading <BlockBy> |
| 30 | * consecutive values from the first input row. This same number of values |
| 31 | * are then read from the next <IntBy-1> rows. Now return to the first |
| 32 | * input row and repeat. |
| 33 | * |
| 34 | * Need to cope with the work requested in either dimension not actually |
| 35 | * being a multiple of the block sizes. |
| 36 | */ |
| 37 | template <unsigned IntBy, unsigned int BlockBy, bool Transposed, size_t TOutSize, size_t TInSize> |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 38 | struct TransformImpl |
| 39 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 40 | template <typename TOut, typename TIn> |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 41 | static void Transform(TOut *out, const TIn *const in, const int stride, |
| 42 | const int y0, const int ymax, const int x0, const int xmax) |
| 43 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 44 | const int n_whole_y_blocks = (ymax - y0) / IntBy; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 45 | const int y_remainders = (ymax - y0) % IntBy; |
| 46 | const int n_y_blocks = n_whole_y_blocks + (y_remainders ? 1 : 0); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 47 | |
| 48 | const int n_whole_x_blocks = (xmax - x0) / BlockBy; |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 49 | const int x_remainders = (xmax - x0) % BlockBy; |
| 50 | const int n_x_blocks = n_whole_x_blocks + (x_remainders ? 1 : 0); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 51 | |
| 52 | // "Y" loop: advance down the rows of the source IntBy rows at a time. |
| 53 | // Set up fill_rows to show the number rows to copy from, and blank_rows |
| 54 | // for the number of blank rows to add. |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 55 | for(int y_block = 0; y_block < n_y_blocks; y_block++) |
| 56 | { |
| 57 | int fill_rows = (y_block < n_whole_y_blocks) ? IntBy : y_remainders; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 58 | int blank_rows = IntBy - fill_rows; |
| 59 | |
| 60 | int y_base = y0 + (y_block * IntBy); |
| 61 | |
| 62 | // So now advance along this block of rows, BlockBy columns at a time. |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 63 | for(int x_block = 0; x_block < n_x_blocks; x_block++) |
| 64 | { |
| 65 | int fill_cols = (x_block < n_whole_x_blocks) ? BlockBy : x_remainders; |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 66 | int blank_cols = BlockBy - fill_cols; |
| 67 | |
| 68 | int x_base = x0 + (x_block * BlockBy); |
| 69 | |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 70 | for(int row = 0; row < fill_rows; row++) |
| 71 | { |
| 72 | for(int col = 0; col < fill_cols; col++) |
| 73 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 74 | // In-range copy. If it's transposed, we reverse the sense of rows and columns here. |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 75 | if(Transposed) |
| 76 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 77 | *out++ = static_cast<TOut>(in[(x_base + col) * stride + y_base + row]); |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 78 | } |
| 79 | else |
| 80 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 81 | *out++ = static_cast<TOut>(in[(y_base + row) * stride + x_base + col]); |
| 82 | } |
| 83 | } |
| 84 | // "col" tail - row is in range but column is out of range. |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 85 | for(int col = 0; col < blank_cols; col++) |
| 86 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 87 | *out++ = static_cast<TOut>(0); |
| 88 | } |
| 89 | } |
| 90 | // "row" tail - row is out of range so fill with zeros always. |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 91 | for(int row = 0; row < blank_rows; row++) |
| 92 | { |
| 93 | for(int col = 0; col < (fill_cols + blank_cols); col++) |
| 94 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 95 | *out++ = static_cast<TOut>(0); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | template <typename T> |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 103 | static inline void Transform(T *out, const T *const in, const int stride, |
| 104 | const int k0, const int kmax, const int x0, const int xmax) |
| 105 | { |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 106 | Transform<T, T>(out, in, stride, k0, kmax, x0, xmax); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | /*****************************************************************************/ |
| 111 | template <unsigned int IntBy, unsigned int BlockBy, bool Transposed, typename TOut, typename TIn> |
| 112 | void Transform( |
Pablo Tello | eb82fd2 | 2018-02-23 13:43:50 +0000 | [diff] [blame] | 113 | TOut *out, const TIn *const in, const int stride, |
| 114 | const int k0, const int kmax, const int x0, const int xmax) |
| 115 | { |
| 116 | // Redirect to a specialised implementation predicated on argument size. |
| 117 | TransformImpl<IntBy, BlockBy, Transposed, sizeof(TOut), sizeof(TIn)>::Transform( |
| 118 | out, in, stride, k0, kmax, x0, xmax); |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 119 | } |
| 120 | /*****************************************************************************/ |
| 121 | |
| 122 | #include "transforms/list.hpp" |