arm_f64_to_q7.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_f64_to_q7.c
  4. * Description: Converts the elements of the 64 bit floating-point vector to Q7 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 Q7 vector.
  38. * @param[in] *pSrc points to the 64 bit floating-point input vector
  39. * @param[out] *pDst points to the Q7 output vector
  40. * @param[in] blockSize length of the input vector
  41. *
  42. *\par Description:
  43. * \par
  44. * The equation used for the conversion process is:
  45. * <pre>
  46. * pDst[n] = (q7_t)(pSrc[n] * 128); 0 <= n < blockSize.
  47. * </pre>
  48. * \par Scaling and Overflow Behavior:
  49. * \par
  50. * The function uses saturating arithmetic.
  51. * Results outside of the allowable Q7 range [0x80 0x7F] will be saturated.
  52. * \note
  53. * In order to apply rounding, the library should be rebuilt with the ROUNDING macro
  54. * defined in the preprocessor section of project options.
  55. */
  56. void arm_f64_to_q7(
  57. const float64_t * pSrc,
  58. q7_t * pDst,
  59. uint32_t blockSize)
  60. {
  61. uint32_t blkCnt; /* Loop counter */
  62. const float64_t *pIn = pSrc; /* Source pointer */
  63. #ifdef ARM_MATH_ROUNDING
  64. float64_t in;
  65. #endif /* #ifdef ARM_MATH_ROUNDING */
  66. #if defined (ARM_MATH_LOOPUNROLL)
  67. /* Loop unrolling: Compute 4 outputs at a time */
  68. blkCnt = blockSize >> 2U;
  69. while (blkCnt > 0U)
  70. {
  71. /* C = A * 128 */
  72. /* Convert from float to q7 and store result in destination buffer */
  73. #ifdef ARM_MATH_ROUNDING
  74. in = (*pIn++ * 128);
  75. in += in > 0.0 ? 0.5 : -0.5;
  76. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  77. in = (*pIn++ * 128);
  78. in += in > 0.0 ? 0.5 : -0.5;
  79. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  80. in = (*pIn++ * 128);
  81. in += in > 0.0 ? 0.5 : -0.5;
  82. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  83. in = (*pIn++ * 128);
  84. in += in > 0.0 ? 0.5 : -0.5;
  85. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  86. #else
  87. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
  88. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
  89. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
  90. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0), 8);
  91. #endif /* #ifdef ARM_MATH_ROUNDING */
  92. /* Decrement loop counter */
  93. blkCnt--;
  94. }
  95. /* Loop unrolling: Compute remaining outputs */
  96. blkCnt = blockSize % 0x4U;
  97. #else
  98. /* Initialize blkCnt with number of samples */
  99. blkCnt = blockSize;
  100. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  101. while (blkCnt > 0U)
  102. {
  103. /* C = A * 128 */
  104. /* Convert from float to q7 and store result in destination buffer */
  105. #ifdef ARM_MATH_ROUNDING
  106. in = (*pIn++ * 128);
  107. in += in > 0.0 ? 0.5 : -0.5;
  108. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  109. #else
  110. *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0), 8);
  111. #endif /* #ifdef ARM_MATH_ROUNDING */
  112. /* Decrement loop counter */
  113. blkCnt--;
  114. }
  115. }
  116. /**
  117. @} end of f64_to_x group
  118. */