| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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 | static 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 = floor(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 | |
| 52 | static 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 | |
| 84 | #if __riscv_vector |
| 85 | const unsigned int* pxofs = (const unsigned int*)xofs; |
| 86 | int n = w; |
| 87 | while (n > 0) |
| 88 | { |
| 89 | size_t vl = vsetvl_e32m4(n); |
| 90 | |
| 91 | vuint32m4_t _sx = vmul_vx_u32m4(vle32_v_u32m4(pxofs, vl), sizeof(float), vl); |
| 92 | |
| 93 | vfloat32m4_t _S1p0; |
| 94 | vfloat32m4_t _S1p1; |
| 95 | vloxseg2ei32_v_f32m4(&_S1p0, &_S1p1, S1, _sx, vl); |
| 96 | |
| 97 | vfloat32m4_t _a0; |
| 98 | vfloat32m4_t _a1; |
| 99 | vlseg2e32_v_f32m4(&_a0, &_a1, alphap, vl); |
| 100 | |
| 101 | vfloat32m4_t _rows1 = vfmacc_vv_f32m4(vfmul_vv_f32m4(_S1p0, _a0, vl), _S1p1, _a1, vl); |
| 102 | |
| 103 | vse32_v_f32m4(rows1p, _rows1, vl); |
| 104 | |
| 105 | pxofs += vl; |
| 106 | alphap += vl * 2; |
| 107 | rows1p += vl; |
| 108 | n -= vl; |
| 109 | } |
| 110 | #else // __riscv_vector |
| 111 | for (int dx = 0; dx < w; dx++) |
| 112 | { |
| 113 | int sx = xofs[dx]; |
| 114 | const float* S1p = S1 + sx; |
| 115 | |
| 116 | float a0 = alphap[0]; |
| 117 | float a1 = alphap[1]; |
| 118 | rows1p[dx] = S1p[0] * a0 + S1p[1] * a1; |
| 119 | |
| 120 | alphap += 2; |
| 121 | } |
| 122 | #endif // __riscv_vector |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | // hresize two rows |
| 127 | const float* S0 = src.row(sy); |
| 128 | const float* S1 = src.row(sy + 1); |
| 129 | |
| 130 | const float* alphap = alpha; |
| 131 | float* rows0p = rows0; |
| 132 | float* rows1p = rows1; |
| 133 | |
| 134 | #if __riscv_vector |
| 135 | const unsigned int* pxofs = (const unsigned int*)xofs; |
| 136 | int n = w; |
| 137 | while (n > 0) |
| 138 | { |
| 139 | size_t vl = vsetvl_e32m4(n); |
| 140 | |
| 141 | vuint32m4_t _sx = vmul_vx_u32m4(vle32_v_u32m4(pxofs, vl), sizeof(float), vl); |
| 142 | |
| 143 | vfloat32m4_t _S0p0; |
| 144 | vfloat32m4_t _S0p1; |
| 145 | vfloat32m4_t _S1p0; |
| 146 | vfloat32m4_t _S1p1; |
| 147 | |
| 148 | vloxseg2ei32_v_f32m4(&_S0p0, &_S0p1, S0, _sx, vl); |
| 149 | vloxseg2ei32_v_f32m4(&_S1p0, &_S1p1, S1, _sx, vl); |
| 150 | |
| 151 | vfloat32m4_t _a0; |
| 152 | vfloat32m4_t _a1; |
| 153 | vlseg2e32_v_f32m4(&_a0, &_a1, alphap, vl); |
| 154 | |
| 155 | vfloat32m4_t _rows0 = vfmacc_vv_f32m4(vfmul_vv_f32m4(_S0p0, _a0, vl), _S0p1, _a1, vl); |
| 156 | vfloat32m4_t _rows1 = vfmacc_vv_f32m4(vfmul_vv_f32m4(_S1p0, _a0, vl), _S1p1, _a1, vl); |
| 157 | |
| 158 | vse32_v_f32m4(rows0p, _rows0, vl); |
| 159 | vse32_v_f32m4(rows1p, _rows1, vl); |
| 160 | |
| 161 | pxofs += vl; |
| 162 | alphap += vl * 2; |
| 163 | rows0p += vl; |
| 164 | rows1p += vl; |
| 165 | n -= vl; |
| 166 | } |
| 167 | #else // __riscv_vector |
| 168 | for (int dx = 0; dx < w; dx++) |
| 169 | { |
| 170 | int sx = xofs[dx]; |
| 171 | const float* S0p = S0 + sx; |
| 172 | const float* S1p = S1 + sx; |
| 173 | |
| 174 | float a0 = alphap[0]; |
| 175 | float a1 = alphap[1]; |
| 176 | rows0p[dx] = S0p[0] * a0 + S0p[1] * a1; |
| 177 | rows1p[dx] = S1p[0] * a0 + S1p[1] * a1; |
| 178 | |
| 179 | alphap += 2; |
| 180 | } |
| 181 | #endif // __riscv_vector |
| 182 | } |
| 183 | |
| 184 | prev_sy1 = sy; |
| 185 | |
| 186 | // vresize |
| 187 | float b0 = beta[0]; |
| 188 | float b1 = beta[1]; |
| 189 | |
| 190 | float* rows0p = rows0; |
| 191 | float* rows1p = rows1; |
| 192 | float* Dp = dst.row(dy); |
| 193 | |
| 194 | #if __riscv_vector |
| 195 | int n = w; |
| 196 | while (n > 0) |
| 197 | { |
| 198 | size_t vl = vsetvl_e32m8(n); |
| 199 | |
| 200 | vfloat32m8_t _rows0 = vle32_v_f32m8(rows0p, vl); |
| 201 | vfloat32m8_t _rows1 = vle32_v_f32m8(rows1p, vl); |
| 202 | |
| 203 | vfloat32m8_t _Dp = vfmacc_vf_f32m8(vfmul_vf_f32m8(_rows0, b0, vl), b1, _rows1, vl); |
| 204 | |
| 205 | vse32_v_f32m8(Dp, _Dp, vl); |
| 206 | |
| 207 | Dp += vl; |
| 208 | rows0p += vl; |
| 209 | rows1p += vl; |
| 210 | n -= vl; |
| 211 | } |
| 212 | #else // __riscv_vector |
| 213 | for (int i = 0; i < w; i++) |
| 214 | { |
| 215 | // D[x] = rows0[x]*b0 + rows1[x]*b1; |
| 216 | *Dp++ = *rows0p++ * b0 + *rows1p++ * b1; |
| 217 | } |
| 218 | #endif // __riscv_vector |
| 219 | |
| 220 | beta += 2; |
| 221 | } |
| 222 | } |