arm_f64_to_q31.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_f64_to_q31.c
  4. * Description: Converts the elements of the 64 bit floating-point vector to Q31 vector
  5. *
  6. * $Date: 18 August 2022
  7. * $Revision: V1.0.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2022 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 "dsp/support_functions.h"
  29. /**
  30. @ingroup groupSupport
  31. */
  32. /**
  33. @addtogroup f64_to_x
  34. @{
  35. */
  36. /**
  37. @brief Converts the elements of the 64 bit floating-point vector to Q31 vector.
  38. @param[in] pSrc points to the 64 bit floating-point input vector
  39. @param[out] pDst points to the Q31 output vector
  40. @param[in] blockSize number of samples in each vector
  41. @par Details
  42. The equation used for the conversion process is:
  43. <pre>
  44. pDst[n] = (q31_t)(pSrc[n] * 2147483648); 0 <= n < blockSize.
  45. </pre>
  46. @par Scaling and Overflow Behavior
  47. The function uses saturating arithmetic.
  48. Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] are saturated.
  49. @note
  50. In order to apply rounding, the library should be rebuilt with the ROUNDING macro
  51. defined in the preprocessor section of project options.
  52. */
  53. void arm_f64_to_q31(
  54. const float64_t * pSrc,
  55. q31_t * pDst,
  56. uint32_t blockSize)
  57. {
  58. uint32_t blkCnt; /* Loop counter */
  59. const float64_t *pIn = pSrc; /* Source pointer */
  60. #ifdef ARM_MATH_ROUNDING
  61. float64_t in;
  62. #endif /* #ifdef ARM_MATH_ROUNDING */
  63. #if defined (ARM_MATH_LOOPUNROLL)
  64. /* Loop unrolling: Compute 4 outputs at a time */
  65. blkCnt = blockSize >> 2U;
  66. while (blkCnt > 0U)
  67. {
  68. /* C = A * 2147483648 */
  69. /* convert from float to Q31 and store result in destination buffer */
  70. #ifdef ARM_MATH_ROUNDING
  71. in = (*pIn++ * 2147483648.0);
  72. in += in > 0.0 ? 0.5 : -0.5;
  73. *pDst++ = clip_q63_to_q31((q63_t) (in));
  74. in = (*pIn++ * 2147483648.0);
  75. in += in > 0.0 ? 0.5 : -0.5;
  76. *pDst++ = clip_q63_to_q31((q63_t) (in));
  77. in = (*pIn++ * 2147483648.0);
  78. in += in > 0.0 ? 0.5 : -0.5;
  79. *pDst++ = clip_q63_to_q31((q63_t) (in));
  80. in = (*pIn++ * 2147483648.0);
  81. in += in > 0.0 ? 0.5 : -0.5;
  82. *pDst++ = clip_q63_to_q31((q63_t) (in));
  83. #else
  84. /* C = A * 2147483648 */
  85. /* Convert from float to Q31 and then store the results in the destination buffer */
  86. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0));
  87. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0));
  88. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0));
  89. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0));
  90. #endif /* #ifdef ARM_MATH_ROUNDING */
  91. /* Decrement loop counter */
  92. blkCnt--;
  93. }
  94. /* Loop unrolling: Compute remaining outputs */
  95. blkCnt = blockSize % 0x4U;
  96. #else
  97. /* Initialize blkCnt with number of samples */
  98. blkCnt = blockSize;
  99. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  100. while (blkCnt > 0U)
  101. {
  102. /* C = A * 2147483648 */
  103. /* convert from float to Q31 and store result in destination buffer */
  104. #ifdef ARM_MATH_ROUNDING
  105. in = (*pIn++ * 2147483648.0);
  106. in += in > 0.0 ? 0.5 : -0.5;
  107. *pDst++ = clip_q63_to_q31((q63_t) (in));
  108. #else
  109. /* C = A * 2147483648 */
  110. /* Convert from float to Q31 and then store the results in the destination buffer */
  111. *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0));
  112. #endif /* #ifdef ARM_MATH_ROUNDING */
  113. /* Decrement loop counter */
  114. blkCnt--;
  115. }
  116. }
  117. /**
  118. @} end of f64_to_x group
  119. */