| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2025 Arm Limited and/or its affiliates <open-source-office@arm.com> | ||
| 2 | // | ||
| 3 | // SPDX-License-Identifier: Apache-2.0 | ||
| 4 | |||
| 5 | #ifndef KLEIDICV_WORKSPACE_BORDER_GENERIC_NEON_H | ||
| 6 | #define KLEIDICV_WORKSPACE_BORDER_GENERIC_NEON_H | ||
| 7 | |||
| 8 | #include <algorithm> | ||
| 9 | #include <cstddef> | ||
| 10 | |||
| 11 | #include "kleidicv/neon.h" | ||
| 12 | #include "kleidicv/types.h" | ||
| 13 | #include "kleidicv/workspace/border_types.h" | ||
| 14 | |||
| 15 | namespace KLEIDICV_TARGET_NAMESPACE { | ||
| 16 | |||
| 17 | // Border offsets for generic filters. | ||
| 18 | template <kleidicv::FixedBorderType BorderType> | ||
| 19 | class GenericBorderHorizontal final { | ||
| 20 | public: | ||
| 21 | 52 | GenericBorderHorizontal(size_t width, size_t channels) | |
| 22 | 52 | : width_(static_cast<ptrdiff_t>(width)), | |
| 23 | 52 | channels_{static_cast<ptrdiff_t>(channels)}, | |
| 24 | 52 | data_indices_{0ULL | (1ULL << 8) | (2ULL << 16) | (3ULL << 24) | | |
| 25 | (4ULL << 32) | (5ULL << 40) | (6ULL << 48) | | ||
| 26 | (7ULL << 56)}, | ||
| 27 | 52 | border_indices_left_{0}, | |
| 28 | 52 | border_indices_right_{0} { | |
| 29 | // The result will take some elements from the image (data), and the | ||
| 30 | // remaining parts from the border. | ||
| 31 | // An index vector is prepared here to help the process, e.g. for replicated | ||
| 32 | // borders and 3 channels, the constructed index vector will look like this: | ||
| 33 | // [1, 2, 0, 1, 2, 3, 4, 5] | ||
| 34 | // (0,1,2 is repeated until index 0 is reached, when the image data begins) | ||
| 35 | // Right side is similar, but it is the [5,6,7] that repeats after. | ||
| 36 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 416 times.
|
468 | for (ptrdiff_t i = 0; i < 8; ++i) { |
| 37 | // channels_*8 - 1 - i: 23, 22, 21, 20, 19, 18, 17, 16 | ||
| 38 | // % channels: 2, 1, 0, 2, 1, 0, 2, 1 | ||
| 39 | 416 | border_indices_left_ = | |
| 40 | 416 | (border_indices_left_ << 8) | ((channels_ * 8 - 1 - i) % channels_); | |
| 41 | // (7 - i): 7, 6, 5, 4, 3, 2, 1, 0 | ||
| 42 | // % channels: 1, 0, 2, 1 0, 2, 1, 0 | ||
| 43 | 416 | border_indices_right_ = | |
| 44 | 416 | (border_indices_right_ << 8) | (((7 - i) % channels) + 8 - channels_); | |
| 45 | 416 | } | |
| 46 | 52 | } | |
| 47 | |||
| 48 | // Raw column can be bigger than width-1 or less than 0 | ||
| 49 | 3240 | ptrdiff_t get_column(ptrdiff_t raw_column) const { | |
| 50 | // TODO more border types, this is only the Replicated | ||
| 51 | 6480 | return std::max<ptrdiff_t>(std::min<ptrdiff_t>(raw_column, width_ - 1), | |
| 52 | 3240 | ptrdiff_t{0}); | |
| 53 | } | ||
| 54 | |||
| 55 | // Assuming that start_offset is <= 0 | ||
| 56 | 3760 | uint16x8_t load_left(Rows<const uint8_t> src_rows, | |
| 57 | ptrdiff_t start_offset) const { | ||
| 58 | if constexpr (BorderType == FixedBorderType::REPLICATE) { | ||
| 59 | 3760 | uint8x8_t data = vld1_u8(&src_rows[0]); | |
| 60 | 3760 | uint64_t indices{}; | |
| 61 |
2/2✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 400 times.
|
3760 | if (start_offset > -8) { |
| 62 | 3360 | ptrdiff_t shift = -8 * start_offset; | |
| 63 | 3360 | indices = | |
| 64 | 3360 | ((border_indices_left_ >> (64 - shift)) | (data_indices_ << shift)); | |
| 65 | 3360 | } else { | |
| 66 | 400 | ptrdiff_t shift = ((-start_offset - 8) % channels_) * 8; | |
| 67 | 1200 | indices = (((border_indices_left_ >> (8 * channels_ - shift)) & | |
| 68 | 800 | ((1 << shift) - 1)) | | |
| 69 | 400 | (border_indices_left_ << shift)); | |
| 70 | 400 | } | |
| 71 | 7520 | return vmovl_u8(vtbl1_u8(data, vreinterpret_u8_u64(uint64x1_t{indices}))); | |
| 72 | 3760 | } | |
| 73 | } | ||
| 74 | |||
| 75 | // Assuming that start_offset is >= width - 8 | ||
| 76 | 3760 | uint16x8_t load_right(Rows<const uint8_t> src_rows, | |
| 77 | ptrdiff_t start_offset) const { | ||
| 78 | if constexpr (BorderType == FixedBorderType::REPLICATE) { | ||
| 79 | 3760 | uint8x8_t data = vld1_u8(&src_rows[width_ * channels_ - 8]); | |
| 80 | 3760 | uint64_t indices{}; | |
| 81 | 3760 | ptrdiff_t shift = 8 * (start_offset - (width_ * channels_ - 8)); | |
| 82 |
2/2✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 400 times.
|
3760 | if (shift < 64) { |
| 83 | 3360 | indices = | |
| 84 | 3360 | (data_indices_ >> shift) | (border_indices_right_ << (64 - shift)); | |
| 85 | 3360 | } else { | |
| 86 | 400 | shift = ((start_offset - width_ * channels_) % channels_) * 8; | |
| 87 |
2/2✓ Branch 0 taken 240 times.
✓ Branch 1 taken 160 times.
|
400 | indices = shift == 0 |
| 88 | 160 | ? border_indices_right_ | |
| 89 | 240 | : (((border_indices_right_ >> (8 * channels_ - shift)) | |
| 90 | 480 | << (64 - shift)) | | |
| 91 | 240 | (border_indices_right_ >> shift)); | |
| 92 | } | ||
| 93 | 7520 | return vmovl_u8(vtbl1_u8(data, vreinterpret_u8_u64(uint64x1_t{indices}))); | |
| 94 | 3760 | } | |
| 95 | } | ||
| 96 | |||
| 97 | private: | ||
| 98 | ptrdiff_t width_; | ||
| 99 | ptrdiff_t channels_; | ||
| 100 | uint64_t data_indices_, border_indices_left_, border_indices_right_; | ||
| 101 | }; // end of class GenericBorderHorizontal<BorderType> | ||
| 102 | |||
| 103 | // Border offsets for generic filters. | ||
| 104 | template <kleidicv::FixedBorderType BorderType> | ||
| 105 | class GenericBorderVertical final { | ||
| 106 | public: | ||
| 107 | 52 | explicit GenericBorderVertical(size_t height) | |
| 108 | 52 | : height_(static_cast<ptrdiff_t>(height)) {} | |
| 109 | |||
| 110 | // Raw column can be bigger than width-1 or less than 0 | ||
| 111 | 27200 | ptrdiff_t get_row(ptrdiff_t raw_row) const { | |
| 112 | // TODO more border types, this is only the Replicated | ||
| 113 | 54400 | return std::max<ptrdiff_t>(std::min<ptrdiff_t>(raw_row, height_ - 1), | |
| 114 | 27200 | ptrdiff_t{0}); | |
| 115 | } | ||
| 116 | |||
| 117 | private: | ||
| 118 | ptrdiff_t height_; | ||
| 119 | }; // end of class GenericBorderVertical<BorderType> | ||
| 120 | |||
| 121 | } // namespace KLEIDICV_TARGET_NAMESPACE | ||
| 122 | |||
| 123 | #endif // KLEIDICV_WORKSPACE_BORDER_GENERIC_NEON_H | ||
| 124 |