Elffuss BraidHub

kikocis/ncnn public

master · 4.4 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 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
15static void linear_coeffs(int w, int outw, int* xofs, float* alpha, int align_corner)
16{
17 double scale = (double)w / outw;
18 if (align_corner)
19 {
20 scale = (double)(w - 1) / (outw - 1);
21 }
22
23 for (int dx = 0; dx < outw; dx++)
24 {
25 float fx = (float)((dx + 0.5) * scale - 0.5);
26 if (align_corner)
27 {
28 fx = (float)(dx * scale);
29 }
30
31 int sx = (int)floorf(fx);
32 fx -= sx;
33
34 if (sx < 0)
35 {
36 sx = 0;
37 fx = 0.f;
38 }
39 if (sx >= w - 1)
40 {
41 sx = w - 2;
42 fx = 1.f;
43 }
44
45 xofs[dx] = sx;
46
47 alpha[dx * 2] = 1.f - fx;
48 alpha[dx * 2 + 1] = fx;
49 }
50}
51
52static void resize_bilinear_image(const Mat& src, Mat& dst, float* alpha, int* xofs, float* beta, int* yofs)
53{
54 int w = dst.w;
55 int h = dst.h;
56
57 // loop body
58 Mat rowsbuf0(w);
59 Mat rowsbuf1(w);
60 float* rows0 = rowsbuf0;
61 float* rows1 = rowsbuf1;
62
63 int prev_sy1 = -2;
64
65 for (int dy = 0; dy < h; dy++)
66 {
67 int sy = yofs[dy];
68
69 if (sy == prev_sy1)
70 {
71 // reuse all rows
72 }
73 else if (sy == prev_sy1 + 1)
74 {
75 // hresize one row
76 float* rows0_old = rows0;
77 rows0 = rows1;
78 rows1 = rows0_old;
79 const float* S1 = src.row(sy + 1);
80
81 const float* alphap = alpha;
82 float* rows1p = rows1;
83 int dx = 0;
84 for (; dx < w; dx++)
85 {
86 int sx = xofs[dx];
87 const float* S1p = S1 + sx;
88
89 float a0 = alphap[0];
90 float a1 = alphap[1];
91 rows1p[dx] = S1p[0] * a0 + S1p[1] * a1;
92
93 alphap += 2;
94 }
95 }
96 else
97 {
98 // hresize two rows
99 const float* S0 = src.row(sy);
100 const float* S1 = src.row(sy + 1);
101
102 const float* alphap = alpha;
103 float* rows0p = rows0;
104 float* rows1p = rows1;
105 int dx = 0;
106 for (; dx < w; dx++)
107 {
108 int sx = xofs[dx];
109 const float* S0p = S0 + sx;
110 const float* S1p = S1 + sx;
111
112 float a0 = alphap[0];
113 float a1 = alphap[1];
114 rows0p[dx] = S0p[0] * a0 + S0p[1] * a1;
115 rows1p[dx] = S1p[0] * a0 + S1p[1] * a1;
116
117 alphap += 2;
118 }
119 }
120
121 prev_sy1 = sy;
122
123 // vresize
124 float b0 = beta[0];
125 float b1 = beta[1];
126
127 float* rows0p = rows0;
128 float* rows1p = rows1;
129 float* Dp = dst.row(dy);
130
131 int dx = 0;
132#if __SSE2__
133#if __AVX__
134 __m256 _b0_256 = _mm256_set1_ps(b0);
135 __m256 _b1_256 = _mm256_set1_ps(b1);
136 for (; dx + 7 < w; dx += 8)
137 {
138 __m256 _rows0 = _mm256_loadu_ps(rows0p);
139 __m256 _rows1 = _mm256_loadu_ps(rows1p);
140 __m256 _Dp = _mm256_mul_ps(_rows0, _b0_256);
141 _Dp = _mm256_comp_fmadd_ps(_rows1, _b1_256, _Dp);
142 _mm256_storeu_ps(Dp, _Dp);
143
144 Dp += 8;
145 rows0p += 8;
146 rows1p += 8;
147 }
148#endif // __AVX__
149 __m128 _b0_128 = _mm_set1_ps(b0);
150 __m128 _b1_128 = _mm_set1_ps(b1);
151 for (; dx + 3 < w; dx += 4)
152 {
153 __m128 _rows0 = _mm_loadu_ps(rows0p);
154 __m128 _rows1 = _mm_loadu_ps(rows1p);
155 __m128 _Dp = _mm_mul_ps(_rows0, _b0_128);
156 _Dp = _mm_comp_fmadd_ps(_rows1, _b1_128, _Dp);
157 _mm_storeu_ps(Dp, _Dp);
158
159 Dp += 4;
160 rows0p += 4;
161 rows1p += 4;
162 }
163#endif // __SSE2__
164 for (; dx < w; dx++)
165 {
166 *Dp++ = *rows0p++ * b0 + *rows1p++ * b1;
167 }
168
169 beta += 2;
170 }
171}