arm_clip_f16.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_clip_f16.c
  4. * Description: Floating-point vector addition
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
  13. *
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the License); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. #include "dsp/basic_math_functions_f16.h"
  29. /**
  30. @ingroup groupMath
  31. */
  32. /**
  33. @addtogroup BasicClip
  34. @{
  35. */
  36. /**
  37. @brief Elementwise floating-point clipping
  38. @param[in] pSrc points to input values
  39. @param[out] pDst points to output clipped values
  40. @param[in] low lower bound
  41. @param[in] high higher bound
  42. @param[in] numSamples number of samples to clip
  43. @return none
  44. */
  45. #if defined(ARM_MATH_MVE_FLOAT16) && !defined(ARM_MATH_AUTOVECTORIZE)
  46. #include "arm_helium_utils.h"
  47. void arm_clip_f16(const float16_t * pSrc,
  48. float16_t * pDst,
  49. float16_t low,
  50. float16_t high,
  51. uint32_t numSamples)
  52. {
  53. uint32_t blkCnt;
  54. f16x8_t curVec0, curVec1;
  55. f16x8_t vecLow, vecHigh;
  56. vecLow = vdupq_n_f16(low);
  57. vecHigh = vdupq_n_f16(high);
  58. curVec0 = vld1q(pSrc);
  59. pSrc += 8;
  60. /*
  61. * unrolled x 2 to allow
  62. * vldr/vstr/vmin/vmax
  63. * stall free interleaving
  64. */
  65. blkCnt = numSamples >> 4;
  66. while (blkCnt--)
  67. {
  68. curVec0 = vmaxnmq(curVec0, vecLow);
  69. curVec1 = vld1q(pSrc);
  70. pSrc += 8;
  71. curVec0 = vminnmq(curVec0, vecHigh);
  72. vst1q(pDst, curVec0);
  73. pDst += 8;
  74. curVec1 = vmaxnmq(curVec1, vecLow);
  75. curVec0 = vld1q(pSrc);
  76. pSrc += 8;
  77. curVec1 = vminnmq(curVec1, vecHigh);
  78. vst1q(pDst, curVec1);
  79. pDst += 8;
  80. }
  81. /*
  82. * Tail handling
  83. */
  84. blkCnt = numSamples - ((numSamples >> 4) << 4);
  85. if (blkCnt >= 8)
  86. {
  87. curVec0 = vmaxnmq(curVec0, vecLow);
  88. curVec0 = vminnmq(curVec0, vecHigh);
  89. vst1q(pDst, curVec0);
  90. pDst += 8;
  91. curVec0 = vld1q(pSrc);
  92. pSrc += 8;
  93. }
  94. if (blkCnt > 0)
  95. {
  96. mve_pred16_t p0 = vctp16q(blkCnt & 7);
  97. curVec0 = vmaxnmq(curVec0, vecLow);
  98. curVec0 = vminnmq(curVec0, vecHigh);
  99. vstrhq_p(pDst, curVec0, p0);
  100. }
  101. }
  102. #else
  103. #if defined(ARM_FLOAT16_SUPPORTED)
  104. void arm_clip_f16(const float16_t * pSrc,
  105. float16_t * pDst,
  106. float16_t low,
  107. float16_t high,
  108. uint32_t numSamples)
  109. {
  110. for (uint32_t i = 0; i < numSamples; i++)
  111. {
  112. if ((_Float16)pSrc[i] > (_Float16)high)
  113. pDst[i] = high;
  114. else if ((_Float16)pSrc[i] < (_Float16)low)
  115. pDst[i] = low;
  116. else
  117. pDst[i] = pSrc[i];
  118. }
  119. }
  120. #endif /* defined(ARM_FLOAT16_SUPPORTED */
  121. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  122. /**
  123. @} end of BasicClip group
  124. */