arm_f64_to_q15.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_f64_to_q15.c
  4. * Description: Converts the elements of the 64 bit floating-point vector to Q15 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 Q15 vector.
  38. @param[in] pSrc points to the 64 bit floating-point input vector
  39. @param[out] pDst points to the Q15 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] = (q15_t)(pSrc[n] * 32768); 0 <= n < blockSize.
  45. </pre>
  46. @par Scaling and Overflow Behavior
  47. The function uses saturating arithmetic.
  48. Results outside of the allowable Q15 range [0x8000 0x7FFF] 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_q15(
  54. const float64_t * pSrc,
  55. q15_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 * 32768 */
  69. /* convert from float to Q15 and store result in destination buffer */
  70. #ifdef ARM_MATH_ROUNDING
  71. in = (*pIn++ * 32768.0);
  72. in += in > 0.0 ? 0.5 : -0.5;
  73. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  74. in = (*pIn++ * 32768.0);
  75. in += in > 0.0 ? 0.5 : -0.5;
  76. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  77. in = (*pIn++ * 32768.0);
  78. in += in > 0.0 ? 0.5 : -0.5;
  79. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  80. in = (*pIn++ * 32768.0);
  81. in += in > 0.0 ? 0.5 : -0.5;
  82. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  83. #else
  84. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
  85. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
  86. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
  87. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
  88. #endif /* #ifdef ARM_MATH_ROUNDING */
  89. /* Decrement loop counter */
  90. blkCnt--;
  91. }
  92. /* Loop unrolling: Compute remaining outputs */
  93. blkCnt = blockSize % 0x4U;
  94. #else
  95. /* Initialize blkCnt with number of samples */
  96. blkCnt = blockSize;
  97. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  98. while (blkCnt > 0U)
  99. {
  100. /* C = A * 32768 */
  101. /* convert from float to Q15 and store result in destination buffer */
  102. #ifdef ARM_MATH_ROUNDING
  103. in = (*pIn++ * 32768.0);
  104. in += in > 0.0 ? 0.5 : -0.5;
  105. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  106. #else
  107. /* C = A * 32768 */
  108. /* Convert from float to q15 and then store the results in the destination buffer */
  109. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0), 16);
  110. #endif /* #ifdef ARM_MATH_ROUNDING */
  111. /* Decrement loop counter */
  112. blkCnt--;
  113. }
  114. }
  115. /**
  116. @} end of f64_to_x group
  117. */