arm_clip_f32.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. */
  50. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  51. #include "arm_helium_utils.h"
  52. void arm_clip_f32(const float32_t * pSrc,
  53. float32_t * pDst,
  54. float32_t low,
  55. float32_t high,
  56. uint32_t numSamples)
  57. {
  58. uint32_t blkCnt;
  59. f32x4_t curVec0, curVec1;
  60. f32x4_t vecLow, vecHigh;
  61. vecLow = vdupq_n_f32(low);
  62. vecHigh = vdupq_n_f32(high);
  63. curVec0 = vld1q(pSrc);
  64. pSrc += 4;
  65. /*
  66. * unrolled x 2 to allow
  67. * vldr/vstr/vmin/vmax
  68. * stall free interleaving
  69. */
  70. blkCnt = numSamples >> 3;
  71. while (blkCnt--)
  72. {
  73. curVec0 = vmaxnmq(curVec0, vecLow);
  74. curVec1 = vld1q(pSrc);
  75. pSrc += 4;
  76. curVec0 = vminnmq(curVec0, vecHigh);
  77. vst1q(pDst, curVec0);
  78. pDst += 4;
  79. curVec1 = vmaxnmq(curVec1, vecLow);
  80. curVec0 = vld1q(pSrc);
  81. pSrc += 4;
  82. curVec1 = vminnmq(curVec1, vecHigh);
  83. vst1q(pDst, curVec1);
  84. pDst += 4;
  85. }
  86. /*
  87. * Tail handling
  88. */
  89. blkCnt = numSamples - ((numSamples >> 3) << 3);
  90. if (blkCnt >= 4)
  91. {
  92. curVec0 = vmaxnmq(curVec0, vecLow);
  93. curVec0 = vminnmq(curVec0, vecHigh);
  94. vst1q(pDst, curVec0);
  95. pDst += 4;
  96. curVec0 = vld1q(pSrc);
  97. pSrc += 4;
  98. }
  99. if (blkCnt > 0)
  100. {
  101. mve_pred16_t p0 = vctp32q(blkCnt & 3);
  102. curVec0 = vmaxnmq(curVec0, vecLow);
  103. curVec0 = vminnmq(curVec0, vecHigh);
  104. vstrwq_p(pDst, curVec0, p0);
  105. }
  106. }
  107. #else
  108. void arm_clip_f32(const float32_t * pSrc,
  109. float32_t * pDst,
  110. float32_t low,
  111. float32_t high,
  112. uint32_t numSamples)
  113. {
  114. uint32_t i;
  115. for (i = 0; i < numSamples; i++)
  116. {
  117. if (pSrc[i] > high)
  118. pDst[i] = high;
  119. else if (pSrc[i] < low)
  120. pDst[i] = low;
  121. else
  122. pDst[i] = pSrc[i];
  123. }
  124. }
  125. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  126. /**
  127. @} end of BasicClip group
  128. */