| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "prelu_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #include "arm_usability.h" |
| 20 | #endif // __ARM_NEON |
| 21 | |
| 22 | namespace ncnn { |
| 23 | |
| 24 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 25 | int PReLU_arm::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 26 | { |
| 27 | int dims = bottom_top_blob.dims; |
| 28 | int elempack = bottom_top_blob.elempack; |
| 29 | |
| 30 | if (elempack == 4) |
| 31 | { |
| 32 | float32x4_t _zero = vdupq_n_f32(0.f); |
| 33 | |
| 34 | if (dims == 1) |
| 35 | { |
| 36 | int w = bottom_top_blob.w; |
| 37 | |
| 38 | if (num_slope > 1) |
| 39 | { |
| 40 | const float* slope = slope_data; |
| 41 | |
| 42 | #pragma omp parallel for num_threads(opt.num_threads) |
| 43 | for (int i = 0; i < w; i++) |
| 44 | { |
| 45 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 4; |
| 46 | |
| 47 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 48 | float32x4_t _slope = vld1q_f32(slope + i * 4); |
| 49 | uint32x4_t _lemask = vcleq_f32(_p, _zero); |
| 50 | float32x4_t _ps = vmulq_f32(_p, _slope); |
| 51 | _p = vbslq_f32(_lemask, _ps, _p); |
| 52 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 53 | } |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | float32x4_t _slope = vdupq_n_f32(slope_data[0]); |
| 58 | |
| 59 | #pragma omp parallel for num_threads(opt.num_threads) |
| 60 | for (int i = 0; i < w; i++) |
| 61 | { |
| 62 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 4; |
| 63 | |
| 64 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 65 | uint32x4_t _lemask = vcleq_f32(_p, _zero); |
| 66 | float32x4_t _ps = vmulq_f32(_p, _slope); |
| 67 | _p = vbslq_f32(_lemask, _ps, _p); |
| 68 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (dims == 2) |
| 74 | { |
| 75 | int w = bottom_top_blob.w; |
| 76 | int h = bottom_top_blob.h; |
| 77 | |
| 78 | #pragma omp parallel for num_threads(opt.num_threads) |
| 79 | for (int i = 0; i < h; i++) |
| 80 | { |
| 81 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 82 | float32x4_t _slope = num_slope > 1 ? vld1q_f32((const float*)slope_data + i * 4) : vdupq_n_f32(slope_data[0]); |
| 83 | |
| 84 | for (int j = 0; j < w; j++) |
| 85 | { |
| 86 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 87 | uint32x4_t _lemask = vcleq_f32(_p, _zero); |
| 88 | float32x4_t _ps = vmulq_f32(_p, _slope); |
| 89 | _p = vbslq_f32(_lemask, _ps, _p); |
| 90 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 91 | |
| 92 | ptr += 4; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (dims == 3) |
| 98 | { |
| 99 | int w = bottom_top_blob.w; |
| 100 | int h = bottom_top_blob.h; |
| 101 | int channels = bottom_top_blob.c; |
| 102 | int size = w * h; |
| 103 | |
| 104 | #pragma omp parallel for num_threads(opt.num_threads) |
| 105 | for (int q = 0; q < channels; q++) |
| 106 | { |
| 107 | __fp16* ptr = bottom_top_blob.channel(q); |
| 108 | float32x4_t _slope = num_slope > 1 ? vld1q_f32((const float*)slope_data + q * 4) : vdupq_n_f32(slope_data[0]); |
| 109 | |
| 110 | for (int i = 0; i < size; i++) |
| 111 | { |
| 112 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 113 | uint32x4_t _lemask = vcleq_f32(_p, _zero); |
| 114 | float32x4_t _ps = vmulq_f32(_p, _slope); |
| 115 | _p = vbslq_f32(_lemask, _ps, _p); |
| 116 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 117 | |
| 118 | ptr += 4; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | if (dims == 1) |
| 127 | { |
| 128 | int w = bottom_top_blob.w; |
| 129 | |
| 130 | __fp16* ptr = bottom_top_blob; |
| 131 | |
| 132 | if (num_slope > 1) |
| 133 | { |
| 134 | const float* slope = slope_data; |
| 135 | |
| 136 | #pragma omp parallel for num_threads(opt.num_threads) |
| 137 | for (int i = 0; i < w; i++) |
| 138 | { |
| 139 | float v = (float)ptr[i]; |
| 140 | if (v < 0.f) |
| 141 | ptr[i] = (__fp16)(v * slope[i]); |
| 142 | } |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | const float slope = slope_data[0]; |
| 147 | |
| 148 | #pragma omp parallel for num_threads(opt.num_threads) |
| 149 | for (int i = 0; i < w; i++) |
| 150 | { |
| 151 | float v = (float)ptr[i]; |
| 152 | if (v < 0.f) |
| 153 | ptr[i] = (__fp16)(v * slope); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (dims == 2) |
| 159 | { |
| 160 | int w = bottom_top_blob.w; |
| 161 | int h = bottom_top_blob.h; |
| 162 | |
| 163 | #pragma omp parallel for num_threads(opt.num_threads) |
| 164 | for (int i = 0; i < h; i++) |
| 165 | { |
| 166 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 167 | |
| 168 | const float slope = num_slope > 1 ? slope_data[i] : slope_data[0]; |
| 169 | |
| 170 | float32x4_t _zero = vdupq_n_f32(0.f); |
| 171 | float32x4_t _slope = vdupq_n_f32(slope); |
| 172 | |
| 173 | int j = 0; |
| 174 | for (; j + 3 < w; j += 4) |
| 175 | { |
| 176 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 177 | uint32x4_t _lemask = vcleq_f32(_p, _zero); |
| 178 | float32x4_t _ps = vmulq_f32(_p, _slope); |
| 179 | _p = vbslq_f32(_lemask, _ps, _p); |
| 180 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 181 | |
| 182 | ptr += 4; |
| 183 | } |
| 184 | for (; j < w; j++) |
| 185 | { |
| 186 | float v = (float)*ptr; |
| 187 | if (v < 0.f) |
| 188 | *ptr = (__fp16)(v * slope); |
| 189 | |
| 190 | ptr++; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (dims == 3) |
| 196 | { |
| 197 | int w = bottom_top_blob.w; |
| 198 | int h = bottom_top_blob.h; |
| 199 | int channels = bottom_top_blob.c; |
| 200 | int size = w * h; |
| 201 | |
| 202 | #pragma omp parallel for num_threads(opt.num_threads) |
| 203 | for (int q = 0; q < channels; q++) |
| 204 | { |
| 205 | __fp16* ptr = bottom_top_blob.channel(q); |
| 206 | |
| 207 | const float slope = num_slope > 1 ? slope_data[q] : slope_data[0]; |
| 208 | |
| 209 | float32x4_t _zero = vdupq_n_f32(0.f); |
| 210 | float32x4_t _slope = vdupq_n_f32(slope); |
| 211 | |
| 212 | int j = 0; |
| 213 | for (; j + 3 < size; j += 4) |
| 214 | { |
| 215 | float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr)); |
| 216 | uint32x4_t _lemask = vcleq_f32(_p, _zero); |
| 217 | float32x4_t _ps = vmulq_f32(_p, _slope); |
| 218 | _p = vbslq_f32(_lemask, _ps, _p); |
| 219 | vst1_f16(ptr, vcvt_f16_f32(_p)); |
| 220 | |
| 221 | ptr += 4; |
| 222 | } |
| 223 | for (; j < size; j++) |
| 224 | { |
| 225 | float v = (float)*ptr; |
| 226 | if (v < 0.f) |
| 227 | *ptr = (__fp16)(v * slope); |
| 228 | |
| 229 | ptr++; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | int PReLU_arm::forward_inplace_fp16sa(Mat& bottom_top_blob, const Option& opt) const |
| 238 | { |
| 239 | int dims = bottom_top_blob.dims; |
| 240 | int elempack = bottom_top_blob.elempack; |
| 241 | |
| 242 | if (elempack == 8) |
| 243 | { |
| 244 | float16x8_t _zero = vdupq_n_f16(0.f); |
| 245 | |
| 246 | if (dims == 1) |
| 247 | { |
| 248 | int w = bottom_top_blob.w; |
| 249 | |
| 250 | if (num_slope > 1) |
| 251 | { |
| 252 | const float* slope = slope_data; |
| 253 | |
| 254 | #pragma omp parallel for num_threads(opt.num_threads) |
| 255 | for (int i = 0; i < w; i++) |
| 256 | { |
| 257 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 8; |
| 258 | |
| 259 | float16x8_t _p = vld1q_f16(ptr); |
| 260 | float16x8_t _slope = vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)slope + i * 8)), vcvt_f16_f32(vld1q_f32((const float*)slope + i * 8 + 4))); |
| 261 | uint16x8_t _lemask = vcleq_f16(_p, _zero); |
| 262 | float16x8_t _ps = vmulq_f16(_p, _slope); |
| 263 | _p = vbslq_f16(_lemask, _ps, _p); |
| 264 | vst1q_f16(ptr, _p); |
| 265 | } |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | #if defined(_MSC_VER) && !defined(__clang__) |
| 270 | float16x4_t _slope0 = vcvt_f16_f32(vdupq_n_f32(slope_data[0])); |
| 271 | float16x8_t _slope = vcombine_f16(_slope0, _slope0); |
| 272 | #else |
| 273 | float16x8_t _slope = vdupq_n_f16((__fp16)slope_data[0]); |
| 274 | #endif |
| 275 | |
| 276 | #pragma omp parallel for num_threads(opt.num_threads) |
| 277 | for (int i = 0; i < w; i++) |
| 278 | { |
| 279 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 8; |
| 280 | |
| 281 | float16x8_t _p = vld1q_f16(ptr); |
| 282 | uint16x8_t _lemask = vcleq_f16(_p, _zero); |
| 283 | float16x8_t _ps = vmulq_f16(_p, _slope); |
| 284 | _p = vbslq_f16(_lemask, _ps, _p); |
| 285 | vst1q_f16(ptr, _p); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (dims == 2) |
| 291 | { |
| 292 | int w = bottom_top_blob.w; |
| 293 | int h = bottom_top_blob.h; |
| 294 | |
| 295 | #pragma omp parallel for num_threads(opt.num_threads) |
| 296 | for (int i = 0; i < h; i++) |
| 297 | { |
| 298 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 299 | float16x8_t _slope = num_slope > 1 ? vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)slope_data + i * 8)), vcvt_f16_f32(vld1q_f32((const float*)slope_data + i * 8 + 4))) : vdupq_n_f16((__fp16)slope_data[0]); |
| 300 | |
| 301 | for (int j = 0; j < w; j++) |
| 302 | { |
| 303 | float16x8_t _p = vld1q_f16(ptr); |
| 304 | uint16x8_t _lemask = vcleq_f16(_p, _zero); |
| 305 | float16x8_t _ps = vmulq_f16(_p, _slope); |
| 306 | _p = vbslq_f16(_lemask, _ps, _p); |
| 307 | vst1q_f16(ptr, _p); |
| 308 | |
| 309 | ptr += 8; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (dims == 3) |
| 315 | { |
| 316 | int w = bottom_top_blob.w; |
| 317 | int h = bottom_top_blob.h; |
| 318 | int channels = bottom_top_blob.c; |
| 319 | int size = w * h; |
| 320 | |
| 321 | #pragma omp parallel for num_threads(opt.num_threads) |
| 322 | for (int q = 0; q < channels; q++) |
| 323 | { |
| 324 | __fp16* ptr = bottom_top_blob.channel(q); |
| 325 | float16x8_t _slope = num_slope > 1 ? vcombine_f16(vcvt_f16_f32(vld1q_f32((const float*)slope_data + q * 8)), vcvt_f16_f32(vld1q_f32((const float*)slope_data + q * 8 + 4))) : vdupq_n_f16((__fp16)slope_data[0]); |
| 326 | |
| 327 | for (int i = 0; i < size; i++) |
| 328 | { |
| 329 | float16x8_t _p = vld1q_f16(ptr); |
| 330 | uint16x8_t _lemask = vcleq_f16(_p, _zero); |
| 331 | float16x8_t _ps = vmulq_f16(_p, _slope); |
| 332 | _p = vbslq_f16(_lemask, _ps, _p); |
| 333 | vst1q_f16(ptr, _p); |
| 334 | |
| 335 | ptr += 8; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | if (elempack == 4) |
| 344 | { |
| 345 | if (dims == 1) |
| 346 | { |
| 347 | #if defined(_MSC_VER) && !defined(__clang__) |
| 348 | float16x8_t _zero = vdupq_n_f16(0.f); |
| 349 | #else |
| 350 | float16x4_t _zero = vdup_n_f16(0.f); |
| 351 | #endif |
| 352 | |
| 353 | int w = bottom_top_blob.w; |
| 354 | |
| 355 | if (num_slope > 1) |
| 356 | { |
| 357 | const float* slope = slope_data; |
| 358 | |
| 359 | #pragma omp parallel for num_threads(opt.num_threads) |
| 360 | for (int i = 0; i < w; i++) |
| 361 | { |
| 362 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 4; |
| 363 | |
| 364 | float16x4_t _p = vld1_f16(ptr); |
| 365 | float16x4_t _slope = vcvt_f16_f32(vld1q_f32(slope + i * 4)); |
| 366 | #if defined(_MSC_VER) && !defined(__clang__) |
| 367 | uint16x4_t _lemask = vcle_f16(_p, vget_low_f16(_zero)); |
| 368 | #else |
| 369 | uint16x4_t _lemask = vcle_f16(_p, _zero); |
| 370 | #endif |
| 371 | float16x4_t _ps = vmul_f16(_p, _slope); |
| 372 | _p = vbsl_f16(_lemask, _ps, _p); |
| 373 | vst1_f16(ptr, _p); |
| 374 | } |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | #if defined(_MSC_VER) && !defined(__clang__) |
| 379 | float16x8_t _slope = vdupq_n_f16((__fp16)slope_data[0]); |
| 380 | #else |
| 381 | float16x4_t _slope = vdup_n_f16((__fp16)slope_data[0]); |
| 382 | #endif |
| 383 | |
| 384 | #pragma omp parallel for num_threads(opt.num_threads) |
| 385 | for (int i = 0; i < w; i++) |
| 386 | { |
| 387 | __fp16* ptr = (__fp16*)bottom_top_blob + i * 4; |
| 388 | |
| 389 | float16x4_t _p = vld1_f16(ptr); |
| 390 | #if defined(_MSC_VER) && !defined(__clang__) |
| 391 | uint16x4_t _lemask = vcle_f16(_p, vget_low_f16(_zero)); |
| 392 | float16x4_t _ps = vmul_f16(_p, vget_low_f16(_slope)); |
| 393 | #else |
| 394 | uint16x4_t _lemask = vcle_f16(_p, _zero); |
| 395 | float16x4_t _ps = vmul_f16(_p, _slope); |
| 396 | #endif |
| 397 | _p = vbsl_f16(_lemask, _ps, _p); |
| 398 | vst1_f16(ptr, _p); |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (dims == 2) |
| 404 | { |
| 405 | int w = bottom_top_blob.w; |
| 406 | int h = bottom_top_blob.h; |
| 407 | |
| 408 | #pragma omp parallel for num_threads(opt.num_threads) |
| 409 | for (int i = 0; i < h; i++) |
| 410 | { |
| 411 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 412 | float16x4_t _zero = vdup_n_f16(0.f); |
| 413 | float16x4_t _slope = num_slope > 1 ? vcvt_f16_f32(vld1q_f32((const float*)slope_data + i * 4)) : vdup_n_f16((__fp16)slope_data[0]); |
| 414 | |
| 415 | for (int j = 0; j < w; j++) |
| 416 | { |
| 417 | float16x4_t _p = vld1_f16(ptr); |
| 418 | uint16x4_t _lemask = vcle_f16(_p, _zero); |
| 419 | float16x4_t _ps = vmul_f16(_p, _slope); |
| 420 | _p = vbsl_f16(_lemask, _ps, _p); |
| 421 | vst1_f16(ptr, _p); |
| 422 | |
| 423 | ptr += 4; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if (dims == 3) |
| 429 | { |
| 430 | int w = bottom_top_blob.w; |
| 431 | int h = bottom_top_blob.h; |
| 432 | int channels = bottom_top_blob.c; |
| 433 | int size = w * h; |
| 434 | |
| 435 | #pragma omp parallel for num_threads(opt.num_threads) |
| 436 | for (int q = 0; q < channels; q++) |
| 437 | { |
| 438 | __fp16* ptr = bottom_top_blob.channel(q); |
| 439 | float16x4_t _zero = vdup_n_f16(0.f); |
| 440 | float16x4_t _slope = num_slope > 1 ? vcvt_f16_f32(vld1q_f32((const float*)slope_data + q * 4)) : vdup_n_f16((__fp16)slope_data[0]); |
| 441 | |
| 442 | for (int i = 0; i < size; i++) |
| 443 | { |
| 444 | float16x4_t _p = vld1_f16(ptr); |
| 445 | uint16x4_t _lemask = vcle_f16(_p, _zero); |
| 446 | float16x4_t _ps = vmul_f16(_p, _slope); |
| 447 | _p = vbsl_f16(_lemask, _ps, _p); |
| 448 | vst1_f16(ptr, _p); |
| 449 | |
| 450 | ptr += 4; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | if (dims == 1) |
| 459 | { |
| 460 | int w = bottom_top_blob.w; |
| 461 | |
| 462 | __fp16* ptr = bottom_top_blob; |
| 463 | |
| 464 | if (num_slope > 1) |
| 465 | { |
| 466 | const float* slope = slope_data; |
| 467 | |
| 468 | #pragma omp parallel for num_threads(opt.num_threads) |
| 469 | for (int i = 0; i < w; i++) |
| 470 | { |
| 471 | float v = (float)ptr[i]; |
| 472 | if (v < (__fp16)0.f) |
| 473 | ptr[i] = (__fp16)(v * slope[i]); |
| 474 | } |
| 475 | } |
| 476 | else |
| 477 | { |
| 478 | const float slope = slope_data[0]; |
| 479 | |
| 480 | #pragma omp parallel for num_threads(opt.num_threads) |
| 481 | for (int i = 0; i < w; i++) |
| 482 | { |
| 483 | float v = (float)ptr[i]; |
| 484 | if (v < (__fp16)0.f) |
| 485 | ptr[i] = (__fp16)(v * slope); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (dims == 2) |
| 491 | { |
| 492 | int w = bottom_top_blob.w; |
| 493 | int h = bottom_top_blob.h; |
| 494 | |
| 495 | #pragma omp parallel for num_threads(opt.num_threads) |
| 496 | for (int i = 0; i < h; i++) |
| 497 | { |
| 498 | __fp16* ptr = bottom_top_blob.row<__fp16>(i); |
| 499 | |
| 500 | const float slope = num_slope > 1 ? slope_data[i] : slope_data[0]; |
| 501 | |
| 502 | float16x4_t _zero = vdup_n_f16(0.f); |
| 503 | #if defined(_MSC_VER) && !defined(__clang__) |
| 504 | float16x4_t _slope = vcvt_f16_f32(vdupq_n_f32(slope)); |
| 505 | #else |
| 506 | float16x4_t _slope = vdup_n_f16((__fp16)slope); |
| 507 | #endif |
| 508 | |
| 509 | int j = 0; |
| 510 | for (; j + 3 < w; j += 4) |
| 511 | { |
| 512 | float16x4_t _p = vld1_f16(ptr); |
| 513 | uint16x4_t _lemask = vcle_f16(_p, _zero); |
| 514 | float16x4_t _ps = vmul_f16(_p, _slope); |
| 515 | _p = vbsl_f16(_lemask, _ps, _p); |
| 516 | vst1_f16(ptr, _p); |
| 517 | |
| 518 | ptr += 4; |
| 519 | } |
| 520 | for (; j < w; j++) |
| 521 | { |
| 522 | float v = (float)*ptr; |
| 523 | if (v < (__fp16)0.f) |
| 524 | *ptr = (__fp16)(v * slope); |
| 525 | |
| 526 | ptr++; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | if (dims == 3) |
| 532 | { |
| 533 | int w = bottom_top_blob.w; |
| 534 | int h = bottom_top_blob.h; |
| 535 | int channels = bottom_top_blob.c; |
| 536 | int size = w * h; |
| 537 | |
| 538 | #pragma omp parallel for num_threads(opt.num_threads) |
| 539 | for (int q = 0; q < channels; q++) |
| 540 | { |
| 541 | __fp16* ptr = bottom_top_blob.channel(q); |
| 542 | |
| 543 | const float slope = num_slope > 1 ? slope_data[q] : slope_data[0]; |
| 544 | |
| 545 | float16x4_t _zero = vdup_n_f16(0.f); |
| 546 | #if defined(_MSC_VER) && !defined(__clang__) |
| 547 | float16x4_t _slope = vcvt_f16_f32(vdupq_n_f32(slope)); |
| 548 | #else |
| 549 | float16x4_t _slope = vdup_n_f16((__fp16)slope); |
| 550 | #endif |
| 551 | |
| 552 | int j = 0; |
| 553 | for (; j + 3 < size; j += 4) |
| 554 | { |
| 555 | float16x4_t _p = vld1_f16(ptr); |
| 556 | uint16x4_t _lemask = vcle_f16(_p, _zero); |
| 557 | float16x4_t _ps = vmul_f16(_p, _slope); |
| 558 | _p = vbsl_f16(_lemask, _ps, _p); |
| 559 | vst1_f16(ptr, _p); |
| 560 | |
| 561 | ptr += 4; |
| 562 | } |
| 563 | for (; j < size; j++) |
| 564 | { |
| 565 | float v = (float)*ptr; |
| 566 | if (v < (__fp16)0.f) |
| 567 | *ptr = (__fp16)(v * slope); |
| 568 | |
| 569 | ptr++; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 577 | |
| 578 | } // namespace ncnn |