Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 ARM Limited. |
| 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 | #include <cstring> |
| 25 | #include <cstdint> |
| 26 | |
| 27 | #include "padding.hpp" |
| 28 | |
| 29 | namespace padding |
| 30 | { |
| 31 | |
| 32 | template <typename T> |
| 33 | void copy_and_pad_tile( |
| 34 | const unsigned int tile_rows, |
| 35 | const unsigned int tile_cols, |
| 36 | const unsigned int n_channels, |
| 37 | const T* const inptr, |
| 38 | const unsigned int in_row_stride, |
| 39 | const unsigned int in_col_stride, |
| 40 | T* const outptr, |
| 41 | const unsigned int out_row_stride, |
| 42 | const unsigned int out_col_stride, |
| 43 | const unsigned int pad_top, |
| 44 | const unsigned int pad_left, |
| 45 | const unsigned int pad_bottom, |
| 46 | const unsigned int pad_right, |
| 47 | const T pad_value |
| 48 | ) |
| 49 | { |
| 50 | for (unsigned int out_i = 0; out_i < tile_rows; out_i++) |
| 51 | { |
| 52 | for (unsigned int out_j = 0; out_j < tile_cols; out_j++) |
| 53 | { |
| 54 | T* const output = outptr + out_i*out_row_stride + out_j*out_col_stride; |
| 55 | |
| 56 | if (out_i < pad_top || tile_rows - pad_bottom <= out_i || |
| 57 | out_j < pad_left || tile_cols - pad_right <= out_j) |
| 58 | { |
| 59 | for (unsigned int n = 0; n < n_channels; n++) |
| 60 | { |
| 61 | output[n] = pad_value; |
| 62 | } |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | const auto in_i = out_i - pad_top, in_j = out_j - pad_left; |
| 67 | const T* const input = inptr + in_i*in_row_stride + in_j*in_col_stride; |
| 68 | std::memcpy(output, input, n_channels * sizeof(T)); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | template void copy_and_pad_tile( |
| 75 | unsigned int, unsigned int, unsigned int, |
| 76 | const uint8_t *, unsigned int, unsigned int, |
| 77 | uint8_t *, unsigned int, unsigned int, |
| 78 | unsigned int, unsigned int, unsigned int, unsigned int, uint8_t |
| 79 | ); |
| 80 | |
| 81 | template void copy_and_pad_tile( |
| 82 | unsigned int, unsigned int, unsigned int, |
| 83 | const float *, unsigned int, unsigned int, |
| 84 | float *, unsigned int, unsigned int, |
| 85 | unsigned int, unsigned int, unsigned int, unsigned int, float |
| 86 | ); |
| 87 | |
Georgios Pinitas | 5ce897f | 2020-04-29 11:44:10 +0100 | [diff] [blame] | 88 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 89 | template void copy_and_pad_tile( |
| 90 | unsigned int, unsigned int, unsigned int, |
| 91 | const __fp16 *, unsigned int, unsigned int, |
| 92 | __fp16 *, unsigned int, unsigned int, |
| 93 | unsigned int, unsigned int, unsigned int, unsigned int, __fp16 |
| 94 | ); |
| 95 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 96 | |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 97 | template <unsigned int TileRows, unsigned int TileCols> |
| 98 | void CopyCropped<TileRows, TileCols>::execute( |
| 99 | const size_t size, |
| 100 | const void * const inptr, |
| 101 | const size_t in_row_stride, |
| 102 | const size_t in_col_stride, |
| 103 | void * const outptr, |
| 104 | const size_t out_row_stride, |
| 105 | const size_t out_col_stride, |
| 106 | const unsigned int pad_top, |
| 107 | const unsigned int pad_left, |
| 108 | const unsigned int pad_bottom, |
| 109 | const unsigned int pad_right |
| 110 | ) |
| 111 | { |
| 112 | for (unsigned int out_i = 0, in_i = pad_top; in_i < TileRows - pad_bottom; out_i++, in_i++) |
| 113 | { |
| 114 | for (unsigned int out_j = 0, in_j = pad_left; in_j < TileCols - pad_right; out_j++, in_j++) |
| 115 | { |
| 116 | std::memcpy( |
| 117 | static_cast<uint8_t *>(outptr) + out_i*out_row_stride + out_j*out_col_stride, |
| 118 | static_cast<const uint8_t *>(inptr) + in_i*in_row_stride + in_j*in_col_stride, |
| 119 | size |
| 120 | ); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | template class CopyCropped<2, 2>; |
| 126 | template class CopyCropped<3, 3>; |
| 127 | template class CopyCropped<4, 4>; |
| 128 | |
| 129 | template <typename T> |
| 130 | void crop_and_copy_tile( |
| 131 | unsigned int tile_rows, |
| 132 | unsigned int tile_cols, |
| 133 | unsigned int n_channels, |
| 134 | const T *inptr, |
| 135 | unsigned int in_row_stride, |
| 136 | unsigned int in_col_stride, |
| 137 | T *outptr, |
| 138 | unsigned int out_row_stride, |
| 139 | unsigned int out_col_stride, |
| 140 | unsigned int crop_top, |
| 141 | unsigned int crop_left, |
| 142 | unsigned int crop_bottom, |
| 143 | unsigned int crop_right |
| 144 | ) |
| 145 | { |
| 146 | for (unsigned int out_i = 0, in_i = crop_top; in_i < tile_rows - crop_bottom; out_i++, in_i++) |
| 147 | { |
| 148 | for (unsigned int out_j = 0, in_j = crop_left; in_j < tile_cols - crop_right; out_j++, in_j++) |
| 149 | { |
| 150 | std::memcpy( |
| 151 | outptr + out_i*out_row_stride + out_j*out_col_stride, |
| 152 | inptr + in_i*in_row_stride + in_j*in_col_stride, |
| 153 | sizeof(T) * n_channels |
| 154 | ); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | template void crop_and_copy_tile( |
| 160 | unsigned int tile_rows, |
| 161 | unsigned int tile_cols, |
| 162 | unsigned int n_channels, |
| 163 | const float *inptr, |
| 164 | unsigned int in_row_stride, |
| 165 | unsigned int in_col_stride, |
| 166 | float *outptr, |
| 167 | unsigned int out_row_stride, |
| 168 | unsigned int out_col_stride, |
| 169 | unsigned int crop_top, |
| 170 | unsigned int crop_left, |
| 171 | unsigned int crop_bottom, |
| 172 | unsigned int crop_right |
| 173 | ); |
| 174 | |
Georgios Pinitas | 5ce897f | 2020-04-29 11:44:10 +0100 | [diff] [blame] | 175 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 176 | template void crop_and_copy_tile( |
| 177 | unsigned int tile_rows, |
| 178 | unsigned int tile_cols, |
| 179 | unsigned int n_channels, |
| 180 | const __fp16 *inptr, |
| 181 | unsigned int in_row_stride, |
| 182 | unsigned int in_col_stride, |
| 183 | __fp16 *outptr, |
| 184 | unsigned int out_row_stride, |
| 185 | unsigned int out_col_stride, |
| 186 | unsigned int crop_top, |
| 187 | unsigned int crop_left, |
| 188 | unsigned int crop_bottom, |
| 189 | unsigned int crop_right |
| 190 | ); |
| 191 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 8f43d74 | 2019-03-27 09:28:32 +0000 | [diff] [blame] | 192 | } // namespace padding |