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