arm_abs_f32.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_abs_f32.c
  4. * Description: Floating-point 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. #include <math.h>
  30. /**
  31. * @ingroup groupMath
  32. */
  33. /**
  34. * @defgroup BasicAbs Vector Absolute Value
  35. *
  36. * Computes the absolute value of a vector on an element-by-element basis.
  37. *
  38. * <pre>
  39. * pDst[n] = abs(pSrc[n]), 0 <= n < blockSize.
  40. * </pre>
  41. *
  42. * The functions support in-place computation allowing the source and
  43. * destination pointers to reference the same memory buffer.
  44. * There are separate functions for floating-point, Q7, Q15, and Q31 data types.
  45. */
  46. /**
  47. * @addtogroup BasicAbs
  48. * @{
  49. */
  50. /**
  51. * @brief Floating-point vector absolute value.
  52. * @param[in] *pSrc points to the input buffer
  53. * @param[out] *pDst points to the output buffer
  54. * @param[in] blockSize number of samples in each vector
  55. * @return none.
  56. */
  57. void arm_abs_f32(
  58. float32_t * pSrc,
  59. float32_t * pDst,
  60. uint32_t blockSize)
  61. {
  62. uint32_t blkCnt; /* loop counter */
  63. #if defined (ARM_MATH_DSP)
  64. /* Run the below code for Cortex-M4 and Cortex-M3 */
  65. float32_t in1, in2, in3, in4; /* temporary variables */
  66. /*loop Unrolling */
  67. blkCnt = blockSize >> 2U;
  68. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  69. ** a second loop below computes the remaining 1 to 3 samples. */
  70. while (blkCnt > 0U)
  71. {
  72. /* C = |A| */
  73. /* Calculate absolute and then store the results in the destination buffer. */
  74. /* read sample from source */
  75. in1 = *pSrc;
  76. in2 = *(pSrc + 1);
  77. in3 = *(pSrc + 2);
  78. /* find absolute value */
  79. in1 = fabsf(in1);
  80. /* read sample from source */
  81. in4 = *(pSrc + 3);
  82. /* find absolute value */
  83. in2 = fabsf(in2);
  84. /* read sample from source */
  85. *pDst = in1;
  86. /* find absolute value */
  87. in3 = fabsf(in3);
  88. /* find absolute value */
  89. in4 = fabsf(in4);
  90. /* store result to destination */
  91. *(pDst + 1) = in2;
  92. /* store result to destination */
  93. *(pDst + 2) = in3;
  94. /* store result to destination */
  95. *(pDst + 3) = in4;
  96. /* Update source pointer to process next sampels */
  97. pSrc += 4U;
  98. /* Update destination pointer to process next sampels */
  99. pDst += 4U;
  100. /* Decrement the loop counter */
  101. blkCnt--;
  102. }
  103. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  104. ** No loop unrolling is used. */
  105. blkCnt = blockSize % 0x4U;
  106. #else
  107. /* Run the below code for Cortex-M0 */
  108. /* Initialize blkCnt with number of samples */
  109. blkCnt = blockSize;
  110. #endif /* #if defined (ARM_MATH_DSP) */
  111. while (blkCnt > 0U)
  112. {
  113. /* C = |A| */
  114. /* Calculate absolute and then store the results in the destination buffer. */
  115. *pDst++ = fabsf(*pSrc++);
  116. /* Decrement the loop counter */
  117. blkCnt--;
  118. }
  119. }
  120. /**
  121. * @} end of BasicAbs group
  122. */