| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "x86_usability.h" |
| 16 | |
| 17 | template<bool align_corner> |
| 18 | struct grid_sample_unormalize; |
| 19 | |
| 20 | template<> |
| 21 | struct grid_sample_unormalize</*align_corner*/ true> |
| 22 | { |
| 23 | #if __AVX__ |
| 24 | __m256 operator()(__m256 length, __m256 coord) |
| 25 | { |
| 26 | return _mm256_mul_ps(_mm256_div_ps(_mm256_add_ps(coord, _mm256_set1_ps(1)), _mm256_set1_ps(2)), _mm256_sub_ps(length, _mm256_set1_ps(1))); |
| 27 | } |
| 28 | #endif // __AVX__ |
| 29 | float operator()(int length, float coord) |
| 30 | { |
| 31 | return (coord + 1) / 2.f * (length - 1); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | template<> |
| 36 | struct grid_sample_unormalize</*align_corner*/ false> |
| 37 | { |
| 38 | #if __AVX__ |
| 39 | __m256 operator()(__m256 length, __m256 coord) |
| 40 | { |
| 41 | return _mm256_div_ps(_mm256_comp_fmsub_ps(_mm256_add_ps(coord, _mm256_set1_ps(1)), length, _mm256_set1_ps(1)), _mm256_set1_ps(2)); |
| 42 | } |
| 43 | #endif // __AVX__ |
| 44 | float operator()(int length, float coord) |
| 45 | { |
| 46 | return ((coord + 1) * length - 1) / 2.f; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | template<GridSample::PaddingMode pd, bool align_corner> |
| 51 | struct compute_coord |
| 52 | { |
| 53 | #if __AVX__ |
| 54 | __m256 operator()(__m256 /*length*/, __m256 coord) |
| 55 | { |
| 56 | return coord; |
| 57 | } |
| 58 | #endif // __AVX__ |
| 59 | float operator()(int /*length*/, float coord) |
| 60 | { |
| 61 | return coord; |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | template<bool align_corner> |
| 66 | struct compute_coord<GridSample::Padding_BORDER, align_corner> |
| 67 | { |
| 68 | #if __AVX__ |
| 69 | __m256 operator()(__m256 length, __m256 coord) |
| 70 | { |
| 71 | const __m256 border_x = _mm256_sub_ps(length, _mm256_set1_ps(1)); |
| 72 | |
| 73 | coord = _mm256_min_ps(border_x, _mm256_max_ps(coord, _mm256_setzero_ps())); |
| 74 | |
| 75 | return coord; |
| 76 | } |
| 77 | #endif // __AVX__ |
| 78 | float operator()(int length, float coord) |
| 79 | { |
| 80 | return std::min(length - 1.0f, std::max(coord, 0.0f)); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | template<> |
| 85 | struct compute_coord<GridSample::Padding_REFLECTION, /*align_corner*/ true> |
| 86 | { |
| 87 | #if __AVX__ |
| 88 | __m256 operator()(__m256 length, __m256 coord) |
| 89 | { |
| 90 | const __m256 border_x = _mm256_sub_ps(length, _mm256_set1_ps(1)); |
| 91 | |
| 92 | coord = abs256_ps(coord); |
| 93 | |
| 94 | __m256 reflectx_v = abs256_ps(_mm256_sub_ps(coord, border_x)); |
| 95 | coord = _mm256_sub_ps(border_x, reflectx_v); |
| 96 | |
| 97 | return coord; |
| 98 | } |
| 99 | #endif // __AVX__ |
| 100 | float operator()(int length, float coord) |
| 101 | { |
| 102 | coord = fabs(coord); |
| 103 | coord = (length - 1) - fabs(coord - (length - 1)); |
| 104 | |
| 105 | return std::min(length - 1.0f, std::max(coord, 0.0f)); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | template<> |
| 110 | struct compute_coord<GridSample::Padding_REFLECTION, /*align_corner*/ false> |
| 111 | { |
| 112 | #if __AVX__ |
| 113 | __m256 operator()(__m256 length, __m256 coord) |
| 114 | { |
| 115 | const __m256 border_x = _mm256_sub_ps(length, _mm256_set1_ps(1)); |
| 116 | |
| 117 | __m256 v0p5fp8 = _mm256_set1_ps(0.5f); |
| 118 | coord = _mm256_add_ps(coord, v0p5fp8); |
| 119 | |
| 120 | coord = abs256_ps(coord); |
| 121 | |
| 122 | __m256 reflectx_v = abs256_ps(_mm256_sub_ps(coord, length)); |
| 123 | coord = _mm256_sub_ps(length, reflectx_v); |
| 124 | |
| 125 | coord = _mm256_sub_ps(coord, v0p5fp8); |
| 126 | |
| 127 | _mm256_sub_ps(coord, v0p5fp8); |
| 128 | |
| 129 | coord = _mm256_min_ps(border_x, _mm256_max_ps(coord, _mm256_setzero_ps())); |
| 130 | |
| 131 | return coord; |
| 132 | } |
| 133 | #endif // __AVX__ |
| 134 | float operator()(int length, float coord) |
| 135 | { |
| 136 | coord = fabs(coord + 0.5f); |
| 137 | coord = length - fabs(coord - length) - 0.5; |
| 138 | |
| 139 | return std::min(length - 1.0f, std::max(coord, 0.0f)); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | #include "gridsample_bilinear_compute_blob.h" |
| 144 | #include "gridsample_bicubic_compute_blob.h" |
| 145 | #include "gridsample_nearest_compute_blob.h" |