arm_float_to_q15.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_float_to_q15.c
  4. * Description: Converts the elements of the floating-point vector to Q15 vector
  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 groupSupport
  31. */
  32. /**
  33. * @addtogroup float_to_x
  34. * @{
  35. */
  36. /**
  37. * @brief Converts the elements of the floating-point vector to Q15 vector.
  38. * @param[in] *pSrc points to the floating-point input vector
  39. * @param[out] *pDst points to the Q15 output vector
  40. * @param[in] blockSize length of the input vector
  41. * @return none.
  42. *
  43. * \par Description:
  44. * \par
  45. * The equation used for the conversion process is:
  46. * <pre>
  47. * pDst[n] = (q15_t)(pSrc[n] * 32768); 0 <= n < blockSize.
  48. * </pre>
  49. * \par Scaling and Overflow Behavior:
  50. * \par
  51. * The function uses saturating arithmetic.
  52. * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
  53. * \note
  54. * In order to apply rounding, the library should be rebuilt with the ROUNDING macro
  55. * defined in the preprocessor section of project options.
  56. *
  57. */
  58. void arm_float_to_q15(
  59. float32_t * pSrc,
  60. q15_t * pDst,
  61. uint32_t blockSize)
  62. {
  63. float32_t *pIn = pSrc; /* Src pointer */
  64. uint32_t blkCnt; /* loop counter */
  65. #ifdef ARM_MATH_ROUNDING
  66. float32_t in;
  67. #endif /* #ifdef ARM_MATH_ROUNDING */
  68. #if defined (ARM_MATH_DSP)
  69. /* Run the below code for Cortex-M4 and Cortex-M3 */
  70. /*loop Unrolling */
  71. blkCnt = blockSize >> 2U;
  72. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  73. ** a second loop below computes the remaining 1 to 3 samples. */
  74. while (blkCnt > 0U)
  75. {
  76. #ifdef ARM_MATH_ROUNDING
  77. /* C = A * 32768 */
  78. /* convert from float to q15 and then store the results in the destination buffer */
  79. in = *pIn++;
  80. in = (in * 32768.0f);
  81. in += in > 0.0f ? 0.5f : -0.5f;
  82. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  83. in = *pIn++;
  84. in = (in * 32768.0f);
  85. in += in > 0.0f ? 0.5f : -0.5f;
  86. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  87. in = *pIn++;
  88. in = (in * 32768.0f);
  89. in += in > 0.0f ? 0.5f : -0.5f;
  90. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  91. in = *pIn++;
  92. in = (in * 32768.0f);
  93. in += in > 0.0f ? 0.5f : -0.5f;
  94. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  95. #else
  96. /* C = A * 32768 */
  97. /* convert from float to q15 and then store the results in the destination buffer */
  98. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  99. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  100. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  101. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  102. #endif /* #ifdef ARM_MATH_ROUNDING */
  103. /* Decrement the loop counter */
  104. blkCnt--;
  105. }
  106. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  107. ** No loop unrolling is used. */
  108. blkCnt = blockSize % 0x4U;
  109. while (blkCnt > 0U)
  110. {
  111. #ifdef ARM_MATH_ROUNDING
  112. /* C = A * 32768 */
  113. /* convert from float to q15 and then store the results in the destination buffer */
  114. in = *pIn++;
  115. in = (in * 32768.0f);
  116. in += in > 0.0f ? 0.5f : -0.5f;
  117. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  118. #else
  119. /* C = A * 32768 */
  120. /* convert from float to q15 and then store the results in the destination buffer */
  121. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  122. #endif /* #ifdef ARM_MATH_ROUNDING */
  123. /* Decrement the loop counter */
  124. blkCnt--;
  125. }
  126. #else
  127. /* Run the below code for Cortex-M0 */
  128. /* Loop over blockSize number of values */
  129. blkCnt = blockSize;
  130. while (blkCnt > 0U)
  131. {
  132. #ifdef ARM_MATH_ROUNDING
  133. /* C = A * 32768 */
  134. /* convert from float to q15 and then store the results in the destination buffer */
  135. in = *pIn++;
  136. in = (in * 32768.0f);
  137. in += in > 0 ? 0.5f : -0.5f;
  138. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  139. #else
  140. /* C = A * 32768 */
  141. /* convert from float to q15 and then store the results in the destination buffer */
  142. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  143. #endif /* #ifdef ARM_MATH_ROUNDING */
  144. /* Decrement the loop counter */
  145. blkCnt--;
  146. }
  147. #endif /* #if defined (ARM_MATH_DSP) */
  148. }
  149. /**
  150. * @} end of float_to_x group
  151. */