arm_float_to_q15.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* ----------------------------------------------------------------------
  2. * Project: CMSIS DSP Library
  3. * Title: arm_float_to_q15.c
  4. * Description: Converts the elements of the floating-point vector to Q15 vector
  5. *
  6. * $Date: 23 April 2021
  7. * $Revision: V1.9.0
  8. *
  9. * Target Processor: Cortex-M and Cortex-A cores
  10. * -------------------------------------------------------------------- */
  11. /*
  12. * Copyright (C) 2010-2021 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 float_to_x
  34. @{
  35. */
  36. /**
  37. @brief Converts the elements of the floating-point vector to Q15 vector.
  38. @param[in] pSrc points to the 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. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  54. void arm_float_to_q15(
  55. const float32_t * pSrc,
  56. q15_t * pDst,
  57. uint32_t blockSize)
  58. {
  59. uint32_t blkCnt;
  60. float32_t maxQ = (float32_t) Q15_MAX;
  61. f32x4x2_t tmp;
  62. q15x8_t vecDst = { 0 };
  63. #ifdef ARM_MATH_ROUNDING
  64. float32_t in;
  65. #endif
  66. blkCnt = blockSize >> 3;
  67. while (blkCnt > 0U)
  68. {
  69. /* C = A * 32768 */
  70. /* convert from float to q15 and then store the results in the destination buffer */
  71. tmp = vld2q(pSrc);
  72. tmp.val[0] = vmulq(tmp.val[0], maxQ);
  73. tmp.val[1] = vmulq(tmp.val[1], maxQ);
  74. vecDst = vqmovnbq(vecDst, vcvtaq_s32_f32(tmp.val[0]));
  75. vecDst = vqmovntq(vecDst, vcvtaq_s32_f32(tmp.val[1]));
  76. vst1q(pDst, vecDst);
  77. /*
  78. * Decrement the blockSize loop counter
  79. */
  80. blkCnt--;
  81. pDst += 8;
  82. pSrc += 8;
  83. }
  84. blkCnt = blockSize & 7;
  85. while (blkCnt > 0U)
  86. {
  87. /* C = A * 32768 */
  88. /* convert from float to Q15 and store result in destination buffer */
  89. #ifdef ARM_MATH_ROUNDING
  90. in = (*pSrc++ * 32768.0f);
  91. in += in > 0.0f ? 0.5f : -0.5f;
  92. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  93. #else
  94. /* C = A * 32768 */
  95. /* Convert from float to q15 and then store the results in the destination buffer */
  96. *pDst++ = (q15_t) __SSAT((q31_t) (*pSrc++ * 32768.0f), 16);
  97. #endif /* #ifdef ARM_MATH_ROUNDING */
  98. /* Decrement loop counter */
  99. blkCnt--;
  100. }
  101. }
  102. #else
  103. #if defined(ARM_MATH_NEON_EXPERIMENTAL)
  104. void arm_float_to_q15(
  105. const float32_t * pSrc,
  106. q15_t * pDst,
  107. uint32_t blockSize)
  108. {
  109. const float32_t *pIn = pSrc; /* Src pointer */
  110. uint32_t blkCnt; /* loop counter */
  111. float32x4_t inV;
  112. #ifdef ARM_MATH_ROUNDING
  113. float32x4_t zeroV = vdupq_n_f32(0.0f);
  114. float32x4_t pHalf = vdupq_n_f32(0.5f / 32768.0f);
  115. float32x4_t mHalf = vdupq_n_f32(-0.5f / 32768.0f);
  116. float32x4_t r;
  117. uint32x4_t cmp;
  118. float32_t in;
  119. #endif
  120. int32x4_t cvt;
  121. int16x4_t outV;
  122. blkCnt = blockSize >> 2U;
  123. /* Compute 4 outputs at a time.
  124. ** a second loop below computes the remaining 1 to 3 samples. */
  125. while (blkCnt > 0U)
  126. {
  127. #ifdef ARM_MATH_ROUNDING
  128. /* C = A * 32768 */
  129. /* Convert from float to q15 and then store the results in the destination buffer */
  130. inV = vld1q_f32(pIn);
  131. cmp = vcgtq_f32(inV,zeroV);
  132. r = vbslq_f32(cmp,pHalf,mHalf);
  133. inV = vaddq_f32(inV, r);
  134. pIn += 4;
  135. cvt = vcvtq_n_s32_f32(inV,15);
  136. outV = vqmovn_s32(cvt);
  137. vst1_s16(pDst, outV);
  138. pDst += 4;
  139. #else
  140. /* C = A * 32768 */
  141. /* Convert from float to q15 and then store the results in the destination buffer */
  142. inV = vld1q_f32(pIn);
  143. cvt = vcvtq_n_s32_f32(inV,15);
  144. outV = vqmovn_s32(cvt);
  145. vst1_s16(pDst, outV);
  146. pDst += 4;
  147. pIn += 4;
  148. #endif /* #ifdef ARM_MATH_ROUNDING */
  149. /* Decrement the loop counter */
  150. blkCnt--;
  151. }
  152. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  153. ** No loop unrolling is used. */
  154. blkCnt = blockSize & 3;
  155. while (blkCnt > 0U)
  156. {
  157. #ifdef ARM_MATH_ROUNDING
  158. /* C = A * 32768 */
  159. /* Convert from float to q15 and then store the results in the destination buffer */
  160. in = *pIn++;
  161. in = (in * 32768.0f);
  162. in += in > 0.0f ? 0.5f : -0.5f;
  163. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  164. #else
  165. /* C = A * 32768 */
  166. /* Convert from float to q15 and then store the results in the destination buffer */
  167. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  168. #endif /* #ifdef ARM_MATH_ROUNDING */
  169. /* Decrement the loop counter */
  170. blkCnt--;
  171. }
  172. }
  173. #else
  174. void arm_float_to_q15(
  175. const float32_t * pSrc,
  176. q15_t * pDst,
  177. uint32_t blockSize)
  178. {
  179. uint32_t blkCnt; /* Loop counter */
  180. const float32_t *pIn = pSrc; /* Source pointer */
  181. #ifdef ARM_MATH_ROUNDING
  182. float32_t in;
  183. #endif /* #ifdef ARM_MATH_ROUNDING */
  184. #if defined (ARM_MATH_LOOPUNROLL)
  185. /* Loop unrolling: Compute 4 outputs at a time */
  186. blkCnt = blockSize >> 2U;
  187. while (blkCnt > 0U)
  188. {
  189. /* C = A * 32768 */
  190. /* convert from float to Q15 and store result in destination buffer */
  191. #ifdef ARM_MATH_ROUNDING
  192. in = (*pIn++ * 32768.0f);
  193. in += in > 0.0f ? 0.5f : -0.5f;
  194. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  195. in = (*pIn++ * 32768.0f);
  196. in += in > 0.0f ? 0.5f : -0.5f;
  197. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  198. in = (*pIn++ * 32768.0f);
  199. in += in > 0.0f ? 0.5f : -0.5f;
  200. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  201. in = (*pIn++ * 32768.0f);
  202. in += in > 0.0f ? 0.5f : -0.5f;
  203. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  204. #else
  205. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  206. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  207. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  208. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  209. #endif /* #ifdef ARM_MATH_ROUNDING */
  210. /* Decrement loop counter */
  211. blkCnt--;
  212. }
  213. /* Loop unrolling: Compute remaining outputs */
  214. blkCnt = blockSize % 0x4U;
  215. #else
  216. /* Initialize blkCnt with number of samples */
  217. blkCnt = blockSize;
  218. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  219. while (blkCnt > 0U)
  220. {
  221. /* C = A * 32768 */
  222. /* convert from float to Q15 and store result in destination buffer */
  223. #ifdef ARM_MATH_ROUNDING
  224. in = (*pIn++ * 32768.0f);
  225. in += in > 0.0f ? 0.5f : -0.5f;
  226. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  227. #else
  228. /* C = A * 32768 */
  229. /* Convert from float to q15 and then store the results in the destination buffer */
  230. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  231. #endif /* #ifdef ARM_MATH_ROUNDING */
  232. /* Decrement loop counter */
  233. blkCnt--;
  234. }
  235. }
  236. #endif /* #if defined(ARM_MATH_NEON) */
  237. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  238. /**
  239. @} end of float_to_x group
  240. */