arm_clip_f32.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_clip_f32.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.h"
  29. /**
  30. @ingroup groupMath
  31. */
  32. /**
  33. @defgroup BasicClip Elementwise clipping
  34. Element-by-element clipping of a value.
  35. The value is constrained between 2 bounds.
  36. There are separate functions for floating-point, Q7, Q15, and Q31 data types.
  37. */
  38. /**
  39. @addtogroup BasicClip
  40. @{
  41. */
  42. /**
  43. @brief Elementwise floating-point clipping
  44. @param[in] pSrc points to input values
  45. @param[out] pDst points to output clipped values
  46. @param[in] low lower bound
  47. @param[in] high higher bound
  48. @param[in] numSamples number of samples to clip
  49. @return none
  50. */
  51. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  52. #include "arm_helium_utils.h"
  53. void arm_clip_f32(const float32_t * pSrc,
  54. float32_t * pDst,
  55. float32_t low,
  56. float32_t high,
  57. uint32_t numSamples)
  58. {
  59. uint32_t blkCnt;
  60. f32x4_t curVec0, curVec1;
  61. f32x4_t vecLow, vecHigh;
  62. vecLow = vdupq_n_f32(low);
  63. vecHigh = vdupq_n_f32(high);
  64. curVec0 = vld1q(pSrc);
  65. pSrc += 4;
  66. /*
  67. * unrolled x 2 to allow
  68. * vldr/vstr/vmin/vmax
  69. * stall free interleaving
  70. */
  71. blkCnt = numSamples >> 3;
  72. while (blkCnt--)
  73. {
  74. curVec0 = vmaxnmq(curVec0, vecLow);
  75. curVec1 = vld1q(pSrc);
  76. pSrc += 4;
  77. curVec0 = vminnmq(curVec0, vecHigh);
  78. vst1q(pDst, curVec0);
  79. pDst += 4;
  80. curVec1 = vmaxnmq(curVec1, vecLow);
  81. curVec0 = vld1q(pSrc);
  82. pSrc += 4;
  83. curVec1 = vminnmq(curVec1, vecHigh);
  84. vst1q(pDst, curVec1);
  85. pDst += 4;
  86. }
  87. /*
  88. * Tail handling
  89. */
  90. blkCnt = numSamples - ((numSamples >> 3) << 3);
  91. if (blkCnt >= 4)
  92. {
  93. curVec0 = vmaxnmq(curVec0, vecLow);
  94. curVec0 = vminnmq(curVec0, vecHigh);
  95. vst1q(pDst, curVec0);
  96. pDst += 4;
  97. curVec0 = vld1q(pSrc);
  98. pSrc += 4;
  99. }
  100. if (blkCnt > 0)
  101. {
  102. mve_pred16_t p0 = vctp32q(blkCnt & 3);
  103. curVec0 = vmaxnmq(curVec0, vecLow);
  104. curVec0 = vminnmq(curVec0, vecHigh);
  105. vstrwq_p(pDst, curVec0, p0);
  106. }
  107. }
  108. #else
  109. void arm_clip_f32(const float32_t * pSrc,
  110. float32_t * pDst,
  111. float32_t low,
  112. float32_t high,
  113. uint32_t numSamples)
  114. {
  115. uint32_t i;
  116. for (i = 0; i < numSamples; i++)
  117. {
  118. if (pSrc[i] > high)
  119. pDst[i] = high;
  120. else if (pSrc[i] < low)
  121. pDst[i] = low;
  122. else
  123. pDst[i] = pSrc[i];
  124. }
  125. }
  126. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  127. /**
  128. @} end of BasicClip group
  129. */