arm_float_to_q31.c 5.4 KB

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