arm_float_to_q7.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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: 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. @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. #if defined(ARM_MATH_NEON)
  58. void arm_float_to_q7(
  59. const float32_t * pSrc,
  60. q7_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 / 128.0f);
  70. float32x4_t mHalf = vdupq_n_f32(-0.5f / 128.0f);
  71. float32x4_t r;
  72. uint32x4_t cmp;
  73. #endif
  74. int32x4_t cvt;
  75. int16x4_t cvt1,cvt2;
  76. int8x8_t outV;
  77. blkCnt = blockSize >> 3U;
  78. /* Compute 8 outputs at a time.
  79. ** a second loop below computes the remaining 1 to 7 samples. */
  80. while (blkCnt > 0U)
  81. {
  82. #ifdef ARM_MATH_ROUNDING
  83. /* C = A * 128 */
  84. /* Convert from float to q7 and then store the results in the destination buffer */
  85. inV = vld1q_f32(pIn);
  86. cmp = vcgtq_f32(inV,zeroV);
  87. r = vbslq_f32(cmp,pHalf,mHalf);
  88. inV = vaddq_f32(inV, r);
  89. cvt1 = vqmovn_s32(vcvtq_n_s32_f32(inV,7));
  90. pIn += 4;
  91. inV = vld1q_f32(pIn);
  92. cmp = vcgtq_f32(inV,zeroV);
  93. r = vbslq_f32(cmp,pHalf,mHalf);
  94. inV = vaddq_f32(inV, r);
  95. cvt2 = vqmovn_s32(vcvtq_n_s32_f32(inV,7));
  96. pIn += 4;
  97. outV = vqmovn_s16(vcombine_s16(cvt1,cvt2));
  98. vst1_s8(pDst, outV);
  99. pDst += 8;
  100. #else
  101. /* C = A * 128 */
  102. /* Convert from float to q7 and then store the results in the destination buffer */
  103. inV = vld1q_f32(pIn);
  104. cvt1 = vqmovn_s32(vcvtq_n_s32_f32(inV,7));
  105. pIn += 4;
  106. inV = vld1q_f32(pIn);
  107. cvt2 = vqmovn_s32(vcvtq_n_s32_f32(inV,7));
  108. pIn += 4;
  109. outV = vqmovn_s16(vcombine_s16(cvt1,cvt2));
  110. vst1_s8(pDst, outV);
  111. pDst += 8;
  112. #endif /* #ifdef ARM_MATH_ROUNDING */
  113. /* Decrement the loop counter */
  114. blkCnt--;
  115. }
  116. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  117. ** No loop unrolling is used. */
  118. blkCnt = blockSize & 7;
  119. while (blkCnt > 0U)
  120. {
  121. #ifdef ARM_MATH_ROUNDING
  122. /* C = A * 128 */
  123. /* Convert from float to q7 and then store the results in the destination buffer */
  124. in = *pIn++;
  125. in = (in * 128);
  126. in += in > 0.0f ? 0.5f : -0.5f;
  127. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  128. #else
  129. /* C = A * 128 */
  130. /* Convert from float to q7 and then store the results in the destination buffer */
  131. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  132. #endif /* #ifdef ARM_MATH_ROUNDING */
  133. /* Decrement the loop counter */
  134. blkCnt--;
  135. }
  136. }
  137. #else
  138. void arm_float_to_q7(
  139. const float32_t * pSrc,
  140. q7_t * pDst,
  141. uint32_t blockSize)
  142. {
  143. uint32_t blkCnt; /* Loop counter */
  144. const float32_t *pIn = pSrc; /* Source pointer */
  145. #ifdef ARM_MATH_ROUNDING
  146. float32_t in;
  147. #endif /* #ifdef ARM_MATH_ROUNDING */
  148. #if defined (ARM_MATH_LOOPUNROLL)
  149. /* Loop unrolling: Compute 4 outputs at a time */
  150. blkCnt = blockSize >> 2U;
  151. while (blkCnt > 0U)
  152. {
  153. /* C = A * 128 */
  154. /* Convert from float to q7 and store result in destination buffer */
  155. #ifdef ARM_MATH_ROUNDING
  156. in = (*pIn++ * 128);
  157. in += in > 0.0f ? 0.5f : -0.5f;
  158. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  159. in = (*pIn++ * 128);
  160. in += in > 0.0f ? 0.5f : -0.5f;
  161. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  162. in = (*pIn++ * 128);
  163. in += in > 0.0f ? 0.5f : -0.5f;
  164. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  165. in = (*pIn++ * 128);
  166. in += in > 0.0f ? 0.5f : -0.5f;
  167. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  168. #else
  169. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  170. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  171. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  172. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  173. #endif /* #ifdef ARM_MATH_ROUNDING */
  174. /* Decrement loop counter */
  175. blkCnt--;
  176. }
  177. /* Loop unrolling: Compute remaining outputs */
  178. blkCnt = blockSize % 0x4U;
  179. #else
  180. /* Initialize blkCnt with number of samples */
  181. blkCnt = blockSize;
  182. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  183. while (blkCnt > 0U)
  184. {
  185. /* C = A * 128 */
  186. /* Convert from float to q7 and store result in destination buffer */
  187. #ifdef ARM_MATH_ROUNDING
  188. in = (*pIn++ * 128);
  189. in += in > 0.0f ? 0.5f : -0.5f;
  190. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  191. #else
  192. *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  193. #endif /* #ifdef ARM_MATH_ROUNDING */
  194. /* Decrement loop counter */
  195. blkCnt--;
  196. }
  197. }
  198. #endif /* #if defined(ARM_MATH_NEON) */
  199. /**
  200. @} end of float_to_x group
  201. */