arm_abs_q7.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_abs_q7.c
  4. * Description: Q7 vector absolute value
  5. *
  6. * $Date: 27. January 2017
  7. * $Revision: V.1.5.1
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2017 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 "arm_math.h"
  29. /**
  30. * @ingroup groupMath
  31. */
  32. /**
  33. * @addtogroup BasicAbs
  34. * @{
  35. */
  36. /**
  37. * @brief Q7 vector absolute value.
  38. * @param[in] *pSrc points to the input buffer
  39. * @param[out] *pDst points to the output buffer
  40. * @param[in] blockSize number of samples in each vector
  41. * @return none.
  42. *
  43. * \par Conditions for optimum performance
  44. * Input and output buffers should be aligned by 32-bit
  45. *
  46. *
  47. * <b>Scaling and Overflow Behavior:</b>
  48. * \par
  49. * The function uses saturating arithmetic.
  50. * The Q7 value -1 (0x80) will be saturated to the maximum allowable positive value 0x7F.
  51. */
  52. void arm_abs_q7(
  53. q7_t * pSrc,
  54. q7_t * pDst,
  55. uint32_t blockSize)
  56. {
  57. uint32_t blkCnt; /* loop counter */
  58. q7_t in; /* Input value1 */
  59. #if defined (ARM_MATH_DSP)
  60. /* Run the below code for Cortex-M4 and Cortex-M3 */
  61. q31_t in1, in2, in3, in4; /* temporary input variables */
  62. q31_t out1, out2, out3, out4; /* temporary output variables */
  63. /*loop Unrolling */
  64. blkCnt = blockSize >> 2U;
  65. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  66. ** a second loop below computes the remaining 1 to 3 samples. */
  67. while (blkCnt > 0U)
  68. {
  69. /* C = |A| */
  70. /* Read inputs */
  71. in1 = (q31_t) * pSrc;
  72. in2 = (q31_t) * (pSrc + 1);
  73. in3 = (q31_t) * (pSrc + 2);
  74. /* find absolute value */
  75. out1 = (in1 > 0) ? in1 : (q31_t)__QSUB8(0, in1);
  76. /* read input */
  77. in4 = (q31_t) * (pSrc + 3);
  78. /* find absolute value */
  79. out2 = (in2 > 0) ? in2 : (q31_t)__QSUB8(0, in2);
  80. /* store result to destination */
  81. *pDst = (q7_t) out1;
  82. /* find absolute value */
  83. out3 = (in3 > 0) ? in3 : (q31_t)__QSUB8(0, in3);
  84. /* find absolute value */
  85. out4 = (in4 > 0) ? in4 : (q31_t)__QSUB8(0, in4);
  86. /* store result to destination */
  87. *(pDst + 1) = (q7_t) out2;
  88. /* store result to destination */
  89. *(pDst + 2) = (q7_t) out3;
  90. /* store result to destination */
  91. *(pDst + 3) = (q7_t) out4;
  92. /* update pointers to process next samples */
  93. pSrc += 4U;
  94. pDst += 4U;
  95. /* Decrement the loop counter */
  96. blkCnt--;
  97. }
  98. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  99. ** No loop unrolling is used. */
  100. blkCnt = blockSize % 0x4U;
  101. #else
  102. /* Run the below code for Cortex-M0 */
  103. blkCnt = blockSize;
  104. #endif /* #define ARM_MATH_CM0_FAMILY */
  105. while (blkCnt > 0U)
  106. {
  107. /* C = |A| */
  108. /* Read the input */
  109. in = *pSrc++;
  110. /* Store the Absolute result in the destination buffer */
  111. *pDst++ = (in > 0) ? in : ((in == (q7_t) 0x80) ? 0x7f : -in);
  112. /* Decrement the loop counter */
  113. blkCnt--;
  114. }
  115. }
  116. /**
  117. * @} end of BasicAbs group
  118. */