arm_float_to_q7.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_float_to_q7.c
  4. * Description: Converts the elements of the floating-point vector to Q7 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 Q7 vector.
  38. * @param[in] *pSrc points to the floating-point input vector
  39. * @param[out] *pDst points to the Q7 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] = (q7_t)(pSrc[n] * 128); 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 Q7 range [0x80 0x7F] 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. void arm_float_to_q7(
  58. float32_t * pSrc,
  59. q7_t * pDst,
  60. uint32_t blockSize)
  61. {
  62. float32_t *pIn = pSrc; /* Src pointer */
  63. uint32_t blkCnt; /* loop counter */
  64. #ifdef ARM_MATH_ROUNDING
  65. float32_t in;
  66. #endif /* #ifdef ARM_MATH_ROUNDING */
  67. #if defined (ARM_MATH_DSP)
  68. /* Run the below code for Cortex-M4 and Cortex-M3 */
  69. /*loop Unrolling */
  70. blkCnt = blockSize >> 2U;
  71. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  72. ** a second loop below computes the remaining 1 to 3 samples. */
  73. while (blkCnt > 0U)
  74. {
  75. #ifdef ARM_MATH_ROUNDING
  76. /* C = A * 128 */
  77. /* convert from float to q7 and then store the results in the destination buffer */
  78. in = *pIn++;
  79. in = (in * 128);
  80. in += in > 0.0f ? 0.5f : -0.5f;
  81. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  82. in = *pIn++;
  83. in = (in * 128);
  84. in += in > 0.0f ? 0.5f : -0.5f;
  85. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  86. in = *pIn++;
  87. in = (in * 128);
  88. in += in > 0.0f ? 0.5f : -0.5f;
  89. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  90. in = *pIn++;
  91. in = (in * 128);
  92. in += in > 0.0f ? 0.5f : -0.5f;
  93. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  94. #else
  95. /* C = A * 128 */
  96. /* convert from float to q7 and then store the results in the destination buffer */
  97. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  98. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  99. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  100. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  101. #endif /* #ifdef ARM_MATH_ROUNDING */
  102. /* Decrement the loop counter */
  103. blkCnt--;
  104. }
  105. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  106. ** No loop unrolling is used. */
  107. blkCnt = blockSize % 0x4U;
  108. while (blkCnt > 0U)
  109. {
  110. #ifdef ARM_MATH_ROUNDING
  111. /* C = A * 128 */
  112. /* convert from float to q7 and then store the results in the destination buffer */
  113. in = *pIn++;
  114. in = (in * 128);
  115. in += in > 0.0f ? 0.5f : -0.5f;
  116. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  117. #else
  118. /* C = A * 128 */
  119. /* convert from float to q7 and then store the results in the destination buffer */
  120. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  121. #endif /* #ifdef ARM_MATH_ROUNDING */
  122. /* Decrement the loop counter */
  123. blkCnt--;
  124. }
  125. #else
  126. /* Run the below code for Cortex-M0 */
  127. /* Loop over blockSize number of values */
  128. blkCnt = blockSize;
  129. while (blkCnt > 0U)
  130. {
  131. #ifdef ARM_MATH_ROUNDING
  132. /* C = A * 128 */
  133. /* convert from float to q7 and then store the results in the destination buffer */
  134. in = *pIn++;
  135. in = (in * 128.0f);
  136. in += in > 0 ? 0.5f : -0.5f;
  137. *pDst++ = (q7_t) (__SSAT((q31_t) (in), 8));
  138. #else
  139. /* C = A * 128 */
  140. /* convert from float to q7 and then store the results in the destination buffer */
  141. *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  142. #endif /* #ifdef ARM_MATH_ROUNDING */
  143. /* Decrement the loop counter */
  144. blkCnt--;
  145. }
  146. #endif /* #if defined (ARM_MATH_DSP) */
  147. }
  148. /**
  149. * @} end of float_to_x group
  150. */