arm_clip_q31.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_clip_q31.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. @addtogroup BasicClip
  34. @{
  35. */
  36. /**
  37. @brief Elementwise fixed-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. */
  44. #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
  45. #include "arm_helium_utils.h"
  46. void arm_clip_q31(const q31_t * pSrc,
  47. q31_t * pDst,
  48. q31_t low,
  49. q31_t high,
  50. uint32_t numSamples)
  51. {
  52. uint32_t blkCnt;
  53. q31x4_t curVec0, curVec1;
  54. q31x4_t vecLow, vecHigh;
  55. vecLow = vdupq_n_s32(low);
  56. vecHigh = vdupq_n_s32(high);
  57. curVec0 = vld1q(pSrc);
  58. pSrc += 4;
  59. /*
  60. * unrolled x 2 to allow
  61. * vldr/vstr/vmin/vmax
  62. * stall free interleaving
  63. */
  64. blkCnt = numSamples >> 3;
  65. while (blkCnt--)
  66. {
  67. curVec0 = vmaxq(curVec0, vecLow);
  68. curVec1 = vld1q(pSrc);
  69. pSrc += 4;
  70. curVec0 = vminq(curVec0, vecHigh);
  71. vst1q(pDst, curVec0);
  72. pDst += 4;
  73. curVec1 = vmaxq(curVec1, vecLow);
  74. curVec0 = vld1q(pSrc);
  75. pSrc += 4;
  76. curVec1 = vminq(curVec1, vecHigh);
  77. vst1q(pDst, curVec1);
  78. pDst += 4;
  79. }
  80. /*
  81. * Tail handling
  82. */
  83. blkCnt = numSamples - ((numSamples >> 3) << 3);
  84. if (blkCnt >= 4)
  85. {
  86. curVec0 = vmaxq(curVec0, vecLow);
  87. curVec0 = vminq(curVec0, vecHigh);
  88. vst1q(pDst, curVec0);
  89. pDst += 4;
  90. curVec0 = vld1q(pSrc);
  91. pSrc += 4;
  92. }
  93. if (blkCnt > 0)
  94. {
  95. mve_pred16_t p0 = vctp32q(blkCnt & 3);
  96. curVec0 = vmaxq(curVec0, vecLow);
  97. curVec0 = vminq(curVec0, vecHigh);
  98. vstrwq_p(pDst, curVec0, p0);
  99. }
  100. }
  101. #else
  102. void arm_clip_q31(const q31_t * pSrc,
  103. q31_t * pDst,
  104. q31_t low,
  105. q31_t high,
  106. uint32_t numSamples)
  107. {
  108. uint32_t i;
  109. for (i = 0; i < numSamples; i++)
  110. {
  111. if (pSrc[i] > high)
  112. pDst[i] = high;
  113. else if (pSrc[i] < low)
  114. pDst[i] = low;
  115. else
  116. pDst[i] = pSrc[i];
  117. }
  118. }
  119. #endif /* defined(ARM_MATH_MVEI) */
  120. /**
  121. @} end of BasicClip group
  122. */