You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

clip_arm.cpp 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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. #include "clip_arm.h"
  15. #ifdef __ARM_NEON
  16. #include <arm_neon.h>
  17. #endif // __ARM_NEON
  18. namespace ncnn {
  19. DEFINE_LAYER_CREATOR(Clip_arm)
  20. int Clip_arm::forward_inplace(Mat &bottom_top_blob, const Option &opt) const
  21. {
  22. int w = bottom_top_blob.w;
  23. int h = bottom_top_blob.h;
  24. int channels = bottom_top_blob.c;
  25. int size = w * h;
  26. #if __ARM_NEON
  27. int nn = size >> 2;
  28. int remain = size - (nn << 2);
  29. #else
  30. int remian = size;
  31. #endif
  32. #if __ARM_NEON
  33. float32x4_t maxf32 = vmovq_n_f32(max);
  34. float32x4_t minf32 = vmovq_n_f32(min);
  35. #endif
  36. #pragma omp parallel for num_threads(opt.num_threads)
  37. for (int i = 0; i < channels; ++i)
  38. {
  39. float *channel_ptr = bottom_top_blob.channel(i);
  40. #if __ARM_NEON
  41. #if __aarch64__
  42. for (; nn > 0; --nn)
  43. {
  44. float32x4_t clip_f32 = vld1q_f32(channel_ptr);
  45. float32x4_t clip_min_f32 = vmaxq_f32(minf32, clip_f32);
  46. float32x4_t clip_max_f32 = vminq_f32(maxf32, clip_min_f32);
  47. vst1q_f32(channel_ptr, clip_max_f32);
  48. channel_ptr += 4;
  49. }
  50. #else
  51. if (nn > 0)
  52. {
  53. asm volatile(
  54. "0:"
  55. "pld [%1, #128] \n"
  56. "vld1.f32 {d0-d1}, [%1:128] \n"
  57. "vmax.f32 q1, %q4, q0 \n"
  58. "vmin.f32 q2, %q5, q1 \n"
  59. "subs %0, #1 \n"
  60. "vst1.f32 {d4-d5}, [%1:128]! \n"
  61. "bne 0b \n"
  62. :"=r"(nn), //%0
  63. "=r"(channel_ptr) //%1
  64. :"0"(nn),
  65. "1"(channel_ptr),
  66. "w"(minf32), //%q4
  67. "w"(maxf32) //%q5
  68. :"cc", "memory", "q0", "q1", "q2"
  69. );
  70. }
  71. #endif // __aarch64__
  72. #endif // __ARM_NEON
  73. for (; remain > 0; --remain)
  74. {
  75. if (*channel_ptr < min)
  76. {
  77. *channel_ptr = min;
  78. }
  79. if (*channel_ptr > max)
  80. {
  81. *channel_ptr = max;
  82. }
  83. ++channel_ptr;
  84. }
  85. }
  86. return 0;
  87. }
  88. } // namespace ncnn