arm_float_to_q31.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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: 18. March 2019
  7. * $Revision: V1.6.0
  8. *
  9. * Target Processor: Cortex-M cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2019 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 number of samples in each vector
  44. @return none
  45. @par Details
  46. The equation used for the conversion process is:
  47. <pre>
  48. pDst[n] = (q31_t)(pSrc[n] * 2147483648); 0 <= n < blockSize.
  49. </pre>
  50. @par Scaling and Overflow Behavior
  51. The function uses saturating arithmetic.
  52. Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] are 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. #if defined(ARM_MATH_NEON)
  58. void arm_float_to_q31(
  59. const float32_t * pSrc,
  60. q31_t * pDst,
  61. uint32_t blockSize)
  62. {
  63. const float32_t *pIn = pSrc; /* Src pointer */
  64. uint32_t blkCnt; /* loop counter */
  65. float32_t in;
  66. float32x4_t inV;
  67. #ifdef ARM_MATH_ROUNDING
  68. float32x4_t zeroV = vdupq_n_f32(0.0f);
  69. float32x4_t pHalf = vdupq_n_f32(0.5f / 2147483648.0f);
  70. float32x4_t mHalf = vdupq_n_f32(-0.5f / 2147483648.0f);
  71. float32x4_t r;
  72. uint32x4_t cmp;
  73. #endif
  74. int32x4_t outV;
  75. blkCnt = blockSize >> 2U;
  76. /* Compute 4 outputs at a time.
  77. ** a second loop below computes the remaining 1 to 3 samples. */
  78. while (blkCnt > 0U)
  79. {
  80. #ifdef ARM_MATH_ROUNDING
  81. /* C = A * 32768 */
  82. /* Convert from float to Q31 and then store the results in the destination buffer */
  83. inV = vld1q_f32(pIn);
  84. cmp = vcgtq_f32(inV,zeroV);
  85. r = vbslq_f32(cmp,pHalf,mHalf);
  86. inV = vaddq_f32(inV, r);
  87. pIn += 4;
  88. outV = vcvtq_n_s32_f32(inV,31);
  89. vst1q_s32(pDst, outV);
  90. pDst += 4;
  91. #else
  92. /* C = A * 2147483648 */
  93. /* Convert from float to Q31 and then store the results in the destination buffer */
  94. inV = vld1q_f32(pIn);
  95. outV = vcvtq_n_s32_f32(inV,31);
  96. vst1q_s32(pDst, outV);
  97. pDst += 4;
  98. pIn += 4;
  99. #endif /* #ifdef ARM_MATH_ROUNDING */
  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 & 3;
  106. while (blkCnt > 0U)
  107. {
  108. #ifdef ARM_MATH_ROUNDING
  109. /* C = A * 2147483648 */
  110. /* Convert from float to Q31 and then store the results in the destination buffer */
  111. in = *pIn++;
  112. in = (in * 2147483648.0f);
  113. in += in > 0.0f ? 0.5f : -0.5f;
  114. *pDst++ = clip_q63_to_q31((q63_t) (in));
  115. #else
  116. /* C = A * 2147483648 */
  117. /* Convert from float to Q31 and then store the results in the destination buffer */
  118. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
  119. #endif /* #ifdef ARM_MATH_ROUNDING */
  120. /* Decrement the loop counter */
  121. blkCnt--;
  122. }
  123. }
  124. #else
  125. void arm_float_to_q31(
  126. const float32_t * pSrc,
  127. q31_t * pDst,
  128. uint32_t blockSize)
  129. {
  130. uint32_t blkCnt; /* Loop counter */
  131. const float32_t *pIn = pSrc; /* Source pointer */
  132. #ifdef ARM_MATH_ROUNDING
  133. float32_t in;
  134. #endif /* #ifdef ARM_MATH_ROUNDING */
  135. #if defined (ARM_MATH_LOOPUNROLL)
  136. /* Loop unrolling: Compute 4 outputs at a time */
  137. blkCnt = blockSize >> 2U;
  138. while (blkCnt > 0U)
  139. {
  140. /* C = A * 2147483648 */
  141. /* convert from float to Q31 and store result in destination buffer */
  142. #ifdef ARM_MATH_ROUNDING
  143. in = (*pIn++ * 2147483648.0f);
  144. in += in > 0.0f ? 0.5f : -0.5f;
  145. *pDst++ = clip_q63_to_q31((q63_t) (in));
  146. in = (*pIn++ * 2147483648.0f);
  147. in += in > 0.0f ? 0.5f : -0.5f;
  148. *pDst++ = clip_q63_to_q31((q63_t) (in));
  149. in = (*pIn++ * 2147483648.0f);
  150. in += in > 0.0f ? 0.5f : -0.5f;
  151. *pDst++ = clip_q63_to_q31((q63_t) (in));
  152. in = (*pIn++ * 2147483648.0f);
  153. in += in > 0.0f ? 0.5f : -0.5f;
  154. *pDst++ = clip_q63_to_q31((q63_t) (in));
  155. #else
  156. /* C = A * 2147483648 */
  157. /* Convert from float to Q31 and then store the results in the destination buffer */
  158. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
  159. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
  160. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
  161. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
  162. #endif /* #ifdef ARM_MATH_ROUNDING */
  163. /* Decrement loop counter */
  164. blkCnt--;
  165. }
  166. /* Loop unrolling: Compute remaining outputs */
  167. blkCnt = blockSize % 0x4U;
  168. #else
  169. /* Initialize blkCnt with number of samples */
  170. blkCnt = blockSize;
  171. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  172. while (blkCnt > 0U)
  173. {
  174. /* C = A * 2147483648 */
  175. /* convert from float to Q31 and store result in destination buffer */
  176. #ifdef ARM_MATH_ROUNDING
  177. in = (*pIn++ * 2147483648.0f);
  178. in += in > 0.0f ? 0.5f : -0.5f;
  179. *pDst++ = clip_q63_to_q31((q63_t) (in));
  180. #else
  181. /* C = A * 2147483648 */
  182. /* Convert from float to Q31 and then store the results in the destination buffer */
  183. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
  184. #endif /* #ifdef ARM_MATH_ROUNDING */
  185. /* Decrement loop counter */
  186. blkCnt--;
  187. }
  188. }
  189. #endif /* #if defined(ARM_MATH_NEON) */
  190. /**
  191. @} end of float_to_x group
  192. */