Elffuss BraidHub

kikocis/ncnn public

master · 7.3 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 resize_bicubic_image_bf16s(const Mat& src, Mat& dst, float* alpha, int* xofs, float* beta, int* yofs)
16{
17 int w = dst.w;
18 int h = dst.h;
19
20 // loop body
21 Mat rowsbuf0(w);
22 Mat rowsbuf1(w);
23 Mat rowsbuf2(w);
24 Mat rowsbuf3(w);
25 float* rows0 = rowsbuf0;
26 float* rows1 = rowsbuf1;
27 float* rows2 = rowsbuf2;
28 float* rows3 = rowsbuf3;
29
30 int prev_sy1 = -3;
31
32 for (int dy = 0; dy < h; dy++)
33 {
34 int sy = yofs[dy];
35
36 if (sy == prev_sy1)
37 {
38 // reuse all rows
39 }
40 else if (sy == prev_sy1 + 1)
41 {
42 // hresize one row
43 float* rows0_old = rows0;
44 rows0 = rows1;
45 rows1 = rows2;
46 rows2 = rows3;
47 rows3 = rows0_old;
48 const unsigned short* S3 = src.row<const unsigned short>(sy + 2);
49
50 const float* alphap = alpha;
51 float* rows3p = rows3;
52 for (int dx = 0; dx < w; dx++)
53 {
54 int sx = xofs[dx];
55 const unsigned short* S3p = S3 + sx;
56
57 float a0 = alphap[0];
58 float a1 = alphap[1];
59 float a2 = alphap[2];
60 float a3 = alphap[3];
61 rows3p[dx] = bfloat16_to_float32(S3p[-1]) * a0 + bfloat16_to_float32(S3p[0]) * a1 + bfloat16_to_float32(S3p[1]) * a2 + bfloat16_to_float32(S3p[2]) * a3;
62
63 alphap += 4;
64 }
65 }
66 else if (sy == prev_sy1 + 2)
67 {
68 // hresize two rows
69 float* rows0_old = rows0;
70 float* rows1_old = rows1;
71 rows0 = rows2;
72 rows1 = rows3;
73 rows2 = rows0_old;
74 rows3 = rows1_old;
75 const unsigned short* S2 = src.row<const unsigned short>(sy + 1);
76 const unsigned short* S3 = src.row<const unsigned short>(sy + 2);
77
78 const float* alphap = alpha;
79 float* rows2p = rows2;
80 float* rows3p = rows3;
81 for (int dx = 0; dx < w; dx++)
82 {
83 int sx = xofs[dx];
84 const unsigned short* S2p = S2 + sx;
85 const unsigned short* S3p = S3 + sx;
86
87 float a0 = alphap[0];
88 float a1 = alphap[1];
89 float a2 = alphap[2];
90 float a3 = alphap[3];
91 rows2p[dx] = bfloat16_to_float32(S2p[-1]) * a0 + bfloat16_to_float32(S2p[0]) * a1 + bfloat16_to_float32(S2p[1]) * a2 + bfloat16_to_float32(S2p[2]) * a3;
92 rows3p[dx] = bfloat16_to_float32(S3p[-1]) * a0 + bfloat16_to_float32(S3p[0]) * a1 + bfloat16_to_float32(S3p[1]) * a2 + bfloat16_to_float32(S3p[2]) * a3;
93
94 alphap += 4;
95 }
96 }
97 else if (sy == prev_sy1 + 3)
98 {
99 // hresize three rows
100 float* rows0_old = rows0;
101 float* rows1_old = rows1;
102 float* rows2_old = rows2;
103 rows0 = rows3;
104 rows1 = rows0_old;
105 rows2 = rows1_old;
106 rows3 = rows2_old;
107 const unsigned short* S1 = src.row<const unsigned short>(sy);
108 const unsigned short* S2 = src.row<const unsigned short>(sy + 1);
109 const unsigned short* S3 = src.row<const unsigned short>(sy + 2);
110
111 const float* alphap = alpha;
112 float* rows1p = rows1;
113 float* rows2p = rows2;
114 float* rows3p = rows3;
115 for (int dx = 0; dx < w; dx++)
116 {
117 int sx = xofs[dx];
118 const unsigned short* S1p = S1 + sx;
119 const unsigned short* S2p = S2 + sx;
120 const unsigned short* S3p = S3 + sx;
121
122 float a0 = alphap[0];
123 float a1 = alphap[1];
124 float a2 = alphap[2];
125 float a3 = alphap[3];
126 rows1p[dx] = bfloat16_to_float32(S1p[-1]) * a0 + bfloat16_to_float32(S1p[0]) * a1 + bfloat16_to_float32(S1p[1]) * a2 + bfloat16_to_float32(S1p[2]) * a3;
127 rows2p[dx] = bfloat16_to_float32(S2p[-1]) * a0 + bfloat16_to_float32(S2p[0]) * a1 + bfloat16_to_float32(S2p[1]) * a2 + bfloat16_to_float32(S2p[2]) * a3;
128 rows3p[dx] = bfloat16_to_float32(S3p[-1]) * a0 + bfloat16_to_float32(S3p[0]) * a1 + bfloat16_to_float32(S3p[1]) * a2 + bfloat16_to_float32(S3p[2]) * a3;
129
130 alphap += 4;
131 }
132 }
133 else
134 {
135 // hresize four rows
136 const unsigned short* S0 = src.row<const unsigned short>(sy - 1);
137 const unsigned short* S1 = src.row<const unsigned short>(sy);
138 const unsigned short* S2 = src.row<const unsigned short>(sy + 1);
139 const unsigned short* S3 = src.row<const unsigned short>(sy + 2);
140
141 const float* alphap = alpha;
142 float* rows0p = rows0;
143 float* rows1p = rows1;
144 float* rows2p = rows2;
145 float* rows3p = rows3;
146 for (int dx = 0; dx < w; dx++)
147 {
148 int sx = xofs[dx];
149 const unsigned short* S0p = S0 + sx;
150 const unsigned short* S1p = S1 + sx;
151 const unsigned short* S2p = S2 + sx;
152 const unsigned short* S3p = S3 + sx;
153
154 float a0 = alphap[0];
155 float a1 = alphap[1];
156 float a2 = alphap[2];
157 float a3 = alphap[3];
158 rows0p[dx] = bfloat16_to_float32(S0p[-1]) * a0 + bfloat16_to_float32(S0p[0]) * a1 + bfloat16_to_float32(S0p[1]) * a2 + bfloat16_to_float32(S0p[2]) * a3;
159 rows1p[dx] = bfloat16_to_float32(S1p[-1]) * a0 + bfloat16_to_float32(S1p[0]) * a1 + bfloat16_to_float32(S1p[1]) * a2 + bfloat16_to_float32(S1p[2]) * a3;
160 rows2p[dx] = bfloat16_to_float32(S2p[-1]) * a0 + bfloat16_to_float32(S2p[0]) * a1 + bfloat16_to_float32(S2p[1]) * a2 + bfloat16_to_float32(S2p[2]) * a3;
161 rows3p[dx] = bfloat16_to_float32(S3p[-1]) * a0 + bfloat16_to_float32(S3p[0]) * a1 + bfloat16_to_float32(S3p[1]) * a2 + bfloat16_to_float32(S3p[2]) * a3;
162
163 alphap += 4;
164 }
165 }
166
167 prev_sy1 = sy;
168
169 // vresize
170 float b0 = beta[0];
171 float b1 = beta[1];
172 float b2 = beta[2];
173 float b3 = beta[3];
174
175 float* rows0p = rows0;
176 float* rows1p = rows1;
177 float* rows2p = rows2;
178 float* rows3p = rows3;
179 unsigned short* Dp = dst.row<unsigned short>(dy);
180 for (int dx = 0; dx < w; dx++)
181 {
182 // D[x] = rows0[x]*b0 + rows1[x]*b1 + rows2[x]*b2 + rows3[x]*b3;
183 *Dp++ = float32_to_bfloat16(*rows0p++ * b0 + *rows1p++ * b1 + *rows2p++ * b2 + *rows3p++ * b3);
184 }
185
186 beta += 4;
187 }
188}