arm_float_to_q15.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. @return none
  42. @par Details
  43. The equation used for the conversion process is:
  44. <pre>
  45. pDst[n] = (q15_t)(pSrc[n] * 32768); 0 <= n < blockSize.
  46. </pre>
  47. @par Scaling and Overflow Behavior
  48. The function uses saturating arithmetic.
  49. Results outside of the allowable Q15 range [0x8000 0x7FFF] are saturated.
  50. @note
  51. In order to apply rounding, the library should be rebuilt with the ROUNDING macro
  52. defined in the preprocessor section of project options.
  53. */
  54. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  55. void arm_float_to_q15(
  56. const float32_t * pSrc,
  57. q15_t * pDst,
  58. uint32_t blockSize)
  59. {
  60. uint32_t blkCnt;
  61. float32_t maxQ = (float32_t) Q15_MAX;
  62. f32x4x2_t tmp;
  63. q15x8_t vecDst;
  64. #ifdef ARM_MATH_ROUNDING
  65. float32_t in;
  66. #endif
  67. blkCnt = blockSize >> 3;
  68. while (blkCnt > 0U)
  69. {
  70. /* C = A * 32768 */
  71. /* convert from float to q15 and then store the results in the destination buffer */
  72. tmp = vld2q(pSrc);
  73. tmp.val[0] = vmulq(tmp.val[0], maxQ);
  74. tmp.val[1] = vmulq(tmp.val[1], maxQ);
  75. vecDst = vqmovnbq(vecDst, vcvtaq_s32_f32(tmp.val[0]));
  76. vecDst = vqmovntq(vecDst, vcvtaq_s32_f32(tmp.val[1]));
  77. vst1q(pDst, vecDst);
  78. /*
  79. * Decrement the blockSize loop counter
  80. */
  81. blkCnt--;
  82. pDst += 8;
  83. pSrc += 8;
  84. }
  85. blkCnt = blockSize & 7;
  86. while (blkCnt > 0U)
  87. {
  88. /* C = A * 32768 */
  89. /* convert from float to Q15 and store result in destination buffer */
  90. #ifdef ARM_MATH_ROUNDING
  91. in = (*pSrc++ * 32768.0f);
  92. in += in > 0.0f ? 0.5f : -0.5f;
  93. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  94. #else
  95. /* C = A * 32768 */
  96. /* Convert from float to q15 and then store the results in the destination buffer */
  97. *pDst++ = (q15_t) __SSAT((q31_t) (*pSrc++ * 32768.0f), 16);
  98. #endif /* #ifdef ARM_MATH_ROUNDING */
  99. /* Decrement loop counter */
  100. blkCnt--;
  101. }
  102. }
  103. #else
  104. #if defined(ARM_MATH_NEON_EXPERIMENTAL)
  105. void arm_float_to_q15(
  106. const float32_t * pSrc,
  107. q15_t * pDst,
  108. uint32_t blockSize)
  109. {
  110. const float32_t *pIn = pSrc; /* Src pointer */
  111. uint32_t blkCnt; /* loop counter */
  112. float32x4_t inV;
  113. #ifdef ARM_MATH_ROUNDING
  114. float32x4_t zeroV = vdupq_n_f32(0.0f);
  115. float32x4_t pHalf = vdupq_n_f32(0.5f / 32768.0f);
  116. float32x4_t mHalf = vdupq_n_f32(-0.5f / 32768.0f);
  117. float32x4_t r;
  118. uint32x4_t cmp;
  119. float32_t in;
  120. #endif
  121. int32x4_t cvt;
  122. int16x4_t outV;
  123. blkCnt = blockSize >> 2U;
  124. /* Compute 4 outputs at a time.
  125. ** a second loop below computes the remaining 1 to 3 samples. */
  126. while (blkCnt > 0U)
  127. {
  128. #ifdef ARM_MATH_ROUNDING
  129. /* C = A * 32768 */
  130. /* Convert from float to q15 and then store the results in the destination buffer */
  131. inV = vld1q_f32(pIn);
  132. cmp = vcgtq_f32(inV,zeroV);
  133. r = vbslq_f32(cmp,pHalf,mHalf);
  134. inV = vaddq_f32(inV, r);
  135. pIn += 4;
  136. cvt = vcvtq_n_s32_f32(inV,15);
  137. outV = vqmovn_s32(cvt);
  138. vst1_s16(pDst, outV);
  139. pDst += 4;
  140. #else
  141. /* C = A * 32768 */
  142. /* Convert from float to q15 and then store the results in the destination buffer */
  143. inV = vld1q_f32(pIn);
  144. cvt = vcvtq_n_s32_f32(inV,15);
  145. outV = vqmovn_s32(cvt);
  146. vst1_s16(pDst, outV);
  147. pDst += 4;
  148. pIn += 4;
  149. #endif /* #ifdef ARM_MATH_ROUNDING */
  150. /* Decrement the loop counter */
  151. blkCnt--;
  152. }
  153. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  154. ** No loop unrolling is used. */
  155. blkCnt = blockSize & 3;
  156. while (blkCnt > 0U)
  157. {
  158. #ifdef ARM_MATH_ROUNDING
  159. /* C = A * 32768 */
  160. /* Convert from float to q15 and then store the results in the destination buffer */
  161. in = *pIn++;
  162. in = (in * 32768.0f);
  163. in += in > 0.0f ? 0.5f : -0.5f;
  164. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  165. #else
  166. /* C = A * 32768 */
  167. /* Convert from float to q15 and then store the results in the destination buffer */
  168. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  169. #endif /* #ifdef ARM_MATH_ROUNDING */
  170. /* Decrement the loop counter */
  171. blkCnt--;
  172. }
  173. }
  174. #else
  175. void arm_float_to_q15(
  176. const float32_t * pSrc,
  177. q15_t * pDst,
  178. uint32_t blockSize)
  179. {
  180. uint32_t blkCnt; /* Loop counter */
  181. const float32_t *pIn = pSrc; /* Source pointer */
  182. #ifdef ARM_MATH_ROUNDING
  183. float32_t in;
  184. #endif /* #ifdef ARM_MATH_ROUNDING */
  185. #if defined (ARM_MATH_LOOPUNROLL)
  186. /* Loop unrolling: Compute 4 outputs at a time */
  187. blkCnt = blockSize >> 2U;
  188. while (blkCnt > 0U)
  189. {
  190. /* C = A * 32768 */
  191. /* convert from float to Q15 and store result in destination buffer */
  192. #ifdef ARM_MATH_ROUNDING
  193. in = (*pIn++ * 32768.0f);
  194. in += in > 0.0f ? 0.5f : -0.5f;
  195. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  196. in = (*pIn++ * 32768.0f);
  197. in += in > 0.0f ? 0.5f : -0.5f;
  198. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  199. in = (*pIn++ * 32768.0f);
  200. in += in > 0.0f ? 0.5f : -0.5f;
  201. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  202. in = (*pIn++ * 32768.0f);
  203. in += in > 0.0f ? 0.5f : -0.5f;
  204. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  205. #else
  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. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  210. #endif /* #ifdef ARM_MATH_ROUNDING */
  211. /* Decrement loop counter */
  212. blkCnt--;
  213. }
  214. /* Loop unrolling: Compute remaining outputs */
  215. blkCnt = blockSize % 0x4U;
  216. #else
  217. /* Initialize blkCnt with number of samples */
  218. blkCnt = blockSize;
  219. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  220. while (blkCnt > 0U)
  221. {
  222. /* C = A * 32768 */
  223. /* convert from float to Q15 and store result in destination buffer */
  224. #ifdef ARM_MATH_ROUNDING
  225. in = (*pIn++ * 32768.0f);
  226. in += in > 0.0f ? 0.5f : -0.5f;
  227. *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
  228. #else
  229. /* C = A * 32768 */
  230. /* Convert from float to q15 and then store the results in the destination buffer */
  231. *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
  232. #endif /* #ifdef ARM_MATH_ROUNDING */
  233. /* Decrement loop counter */
  234. blkCnt--;
  235. }
  236. }
  237. #endif /* #if defined(ARM_MATH_NEON) */
  238. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  239. /**
  240. @} end of float_to_x group
  241. */