Elffuss BraidHub

kikocis/ncnn public

master · 4.0 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_bilinear_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 float* rows0 = rowsbuf0;
24 float* rows1 = rowsbuf1;
25
26 int prev_sy1 = -2;
27
28 for (int dy = 0; dy < h; dy++)
29 {
30 int sy = yofs[dy];
31
32 if (sy == prev_sy1)
33 {
34 // reuse all rows
35 }
36 else if (sy == prev_sy1 + 1)
37 {
38 // hresize one row
39 float* rows0_old = rows0;
40 rows0 = rows1;
41 rows1 = rows0_old;
42 const unsigned short* S1 = src.row<const unsigned short>(sy + 1);
43
44 const float* alphap = alpha;
45 float* rows1p = rows1;
46 int dx = 0;
47 for (; dx < w; dx++)
48 {
49 int sx = xofs[dx];
50 const unsigned short* S1p = S1 + sx;
51
52 float a0 = alphap[0];
53 float a1 = alphap[1];
54 rows1p[dx] = bfloat16_to_float32(S1p[0]) * a0 + bfloat16_to_float32(S1p[1]) * a1;
55
56 alphap += 2;
57 }
58 }
59 else
60 {
61 // hresize two rows
62 const unsigned short* S0 = src.row<const unsigned short>(sy);
63 const unsigned short* S1 = src.row<const unsigned short>(sy + 1);
64
65 const float* alphap = alpha;
66 float* rows0p = rows0;
67 float* rows1p = rows1;
68 int dx = 0;
69 for (; dx < w; dx++)
70 {
71 int sx = xofs[dx];
72 const unsigned short* S0p = S0 + sx;
73 const unsigned short* S1p = S1 + sx;
74
75 float a0 = alphap[0];
76 float a1 = alphap[1];
77 rows0p[dx] = bfloat16_to_float32(S0p[0]) * a0 + bfloat16_to_float32(S0p[1]) * a1;
78 rows1p[dx] = bfloat16_to_float32(S1p[0]) * a0 + bfloat16_to_float32(S1p[1]) * a1;
79
80 alphap += 2;
81 }
82 }
83
84 prev_sy1 = sy;
85
86 // vresize
87 float b0 = beta[0];
88 float b1 = beta[1];
89
90 float* rows0p = rows0;
91 float* rows1p = rows1;
92 unsigned short* Dp = dst.row<unsigned short>(dy);
93
94#if __ARM_NEON
95 int nn = w >> 3;
96#else
97 int nn = 0;
98#endif
99 int remain = w - (nn << 3);
100
101#if __ARM_NEON
102 float32x4_t _b0 = vdupq_n_f32(b0);
103 float32x4_t _b1 = vdupq_n_f32(b1);
104 for (; nn > 0; nn--)
105 {
106 float32x4_t _rows0 = vld1q_f32(rows0p);
107 float32x4_t _rows1 = vld1q_f32(rows1p);
108
109 float32x4_t _Dp = vmulq_f32(_rows0, _b0);
110 _Dp = vmlaq_f32(_Dp, _rows1, _b1);
111
112 vst1_u16(Dp, float2bfloat(_Dp));
113
114 float32x4_t _rows0n = vld1q_f32(rows0p + 4);
115 float32x4_t _rows1n = vld1q_f32(rows1p + 4);
116
117 float32x4_t _Dpn = vmulq_f32(_rows0n, _b0);
118 _Dpn = vmlaq_f32(_Dpn, _rows1n, _b1);
119
120 vst1_u16(Dp + 4, float2bfloat(_Dpn));
121
122 Dp += 8;
123 rows0p += 8;
124 rows1p += 8;
125 }
126#endif // __ARM_NEON
127 for (; remain; --remain)
128 {
129 // D[x] = rows0[x]*b0 + rows1[x]*b1;
130 *Dp++ = float32_to_bfloat16(*rows0p++ * b0 + *rows1p++ * b1);
131 }
132
133 beta += 2;
134 }
135}