arm_negate_q15.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_negate_q15.c
  4. * Description: Negates Q15 vectors
  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 negate
  34. * @{
  35. */
  36. /**
  37. * @brief Negates the elements of a Q15 vector.
  38. * @param[in] *pSrc points to the input vector
  39. * @param[out] *pDst points to the output vector
  40. * @param[in] blockSize number of samples in the 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 Q15 value -1 (0x8000) will be saturated to the maximum allowable positive value 0x7FFF.
  51. */
  52. void arm_negate_q15(
  53. q15_t * pSrc,
  54. q15_t * pDst,
  55. uint32_t blockSize)
  56. {
  57. uint32_t blkCnt; /* loop counter */
  58. q15_t in;
  59. #if defined (ARM_MATH_DSP)
  60. /* Run the below code for Cortex-M4 and Cortex-M3 */
  61. q31_t in1, in2; /* Temporary variables */
  62. /*loop Unrolling */
  63. blkCnt = blockSize >> 2U;
  64. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  65. ** a second loop below computes the remaining 1 to 3 samples. */
  66. while (blkCnt > 0U)
  67. {
  68. /* C = -A */
  69. /* Read two inputs at a time */
  70. in1 = _SIMD32_OFFSET(pSrc);
  71. in2 = _SIMD32_OFFSET(pSrc + 2);
  72. /* negate two samples at a time */
  73. in1 = __QSUB16(0, in1);
  74. /* negate two samples at a time */
  75. in2 = __QSUB16(0, in2);
  76. /* store the result to destination 2 samples at a time */
  77. _SIMD32_OFFSET(pDst) = in1;
  78. /* store the result to destination 2 samples at a time */
  79. _SIMD32_OFFSET(pDst + 2) = in2;
  80. /* update pointers to process next samples */
  81. pSrc += 4U;
  82. pDst += 4U;
  83. /* Decrement the loop counter */
  84. blkCnt--;
  85. }
  86. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  87. ** No loop unrolling is used. */
  88. blkCnt = blockSize % 0x4U;
  89. #else
  90. /* Run the below code for Cortex-M0 */
  91. /* Initialize blkCnt with number of samples */
  92. blkCnt = blockSize;
  93. #endif /* #if defined (ARM_MATH_DSP) */
  94. while (blkCnt > 0U)
  95. {
  96. /* C = -A */
  97. /* Negate and then store the result in the destination buffer. */
  98. in = *pSrc++;
  99. *pDst++ = (in == (q15_t) 0x8000) ? 0x7fff : -in;
  100. /* Decrement the loop counter */
  101. blkCnt--;
  102. }
  103. }
  104. /**
  105. * @} end of negate group
  106. */