arm_float_to_q7.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  58. void arm_float_to_q7(
  59. const float32_t * pSrc,
  60. q7_t * pDst,
  61. uint32_t blockSize)
  62. {
  63. uint32_t blkCnt; /* loop counters */
  64. float32_t maxQ = powf(2.0, 7);
  65. f32x4x4_t tmp;
  66. q15x8_t evVec, oddVec;
  67. q7x16_t vecDst;
  68. float32_t const *pSrcVec;
  69. pSrcVec = (float32_t const *) pSrc;
  70. blkCnt = blockSize >> 4;
  71. while (blkCnt > 0U) {
  72. tmp = vld4q(pSrcVec);
  73. pSrcVec += 16;
  74. /*
  75. * C = A * 128.0
  76. * convert from float to q7 and then store the results in the destination buffer
  77. */
  78. tmp.val[0] = vmulq(tmp.val[0], maxQ);
  79. tmp.val[1] = vmulq(tmp.val[1], maxQ);
  80. tmp.val[2] = vmulq(tmp.val[2], maxQ);
  81. tmp.val[3] = vmulq(tmp.val[3], maxQ);
  82. /*
  83. * convert and pack evens
  84. */
  85. evVec = vqmovnbq(evVec, vcvtaq_s32_f32(tmp.val[0]));
  86. evVec = vqmovntq(evVec, vcvtaq_s32_f32(tmp.val[2]));
  87. /*
  88. * convert and pack odds
  89. */
  90. oddVec = vqmovnbq(oddVec, vcvtaq_s32_f32(tmp.val[1]));
  91. oddVec = vqmovntq(oddVec, vcvtaq_s32_f32(tmp.val[3]));
  92. /*
  93. * merge
  94. */
  95. vecDst = vqmovnbq(vecDst, evVec);
  96. vecDst = vqmovntq(vecDst, oddVec);
  97. vst1q(pDst, vecDst);
  98. pDst += 16;
  99. /*
  100. * Decrement the blockSize loop counter
  101. */
  102. blkCnt--;
  103. }
  104. blkCnt = blockSize & 0xF;
  105. while (blkCnt > 0U)
  106. {
  107. /* C = A * 128 */
  108. /* Convert from float to q7 and store result in destination buffer */
  109. #ifdef ARM_MATH_ROUNDING
  110. in = (*pSrcVec++ * 128);
  111. in += in > 0.0f ? 0.5f : -0.5f;
  112. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  113. #else
  114. *pDst++ = (q7_t) __SSAT((q31_t) (*pSrcVec++ * 128.0f), 8);
  115. #endif /* #ifdef ARM_MATH_ROUNDING */
  116. /* Decrement loop counter */
  117. blkCnt--;
  118. }
  119. }
  120. #else
  121. #if defined(ARM_MATH_NEON)
  122. void arm_float_to_q7(
  123. const float32_t * pSrc,
  124. q7_t * pDst,
  125. uint32_t blockSize)
  126. {
  127. const float32_t *pIn = pSrc; /* Src pointer */
  128. uint32_t blkCnt; /* loop counter */
  129. float32x4_t inV;
  130. #ifdef ARM_MATH_ROUNDING
  131. float32_t in;
  132. float32x4_t zeroV = vdupq_n_f32(0.0f);
  133. float32x4_t pHalf = vdupq_n_f32(0.5f / 128.0f);
  134. float32x4_t mHalf = vdupq_n_f32(-0.5f / 128.0f);
  135. float32x4_t r;
  136. uint32x4_t cmp;
  137. #endif
  138. int16x4_t cvt1,cvt2;
  139. int8x8_t outV;
  140. blkCnt = blockSize >> 3U;
  141. /* Compute 8 outputs at a time.
  142. ** a second loop below computes the remaining 1 to 7 samples. */
  143. while (blkCnt > 0U)
  144. {
  145. #ifdef ARM_MATH_ROUNDING
  146. /* C = A * 128 */
  147. /* Convert from float to q7 and then store the results in the destination buffer */
  148. inV = vld1q_f32(pIn);
  149. cmp = vcgtq_f32(inV,zeroV);
  150. r = vbslq_f32(cmp,pHalf,mHalf);
  151. inV = vaddq_f32(inV, r);
  152. cvt1 = vqmovn_s32(vcvtq_n_s32_f32(inV,7));
  153. pIn += 4;
  154. inV = vld1q_f32(pIn);
  155. cmp = vcgtq_f32(inV,zeroV);
  156. r = vbslq_f32(cmp,pHalf,mHalf);
  157. inV = vaddq_f32(inV, r);
  158. cvt2 = vqmovn_s32(vcvtq_n_s32_f32(inV,7));
  159. pIn += 4;
  160. outV = vqmovn_s16(vcombine_s16(cvt1,cvt2));
  161. vst1_s8(pDst, outV);
  162. pDst += 8;
  163. #else
  164. /* C = A * 128 */
  165. /* Convert from float to q7 and then store the results in the destination buffer */
  166. inV = vld1q_f32(pIn);
  167. cvt1 = vqmovn_s32(vcvtq_n_s32_f32(inV,7));
  168. pIn += 4;
  169. inV = vld1q_f32(pIn);
  170. cvt2 = vqmovn_s32(vcvtq_n_s32_f32(inV,7));
  171. pIn += 4;
  172. outV = vqmovn_s16(vcombine_s16(cvt1,cvt2));
  173. vst1_s8(pDst, outV);
  174. pDst += 8;
  175. #endif /* #ifdef ARM_MATH_ROUNDING */
  176. /* Decrement the loop counter */
  177. blkCnt--;
  178. }
  179. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  180. ** No loop unrolling is used. */
  181. blkCnt = blockSize & 7;
  182. while (blkCnt > 0U)
  183. {
  184. #ifdef ARM_MATH_ROUNDING
  185. /* C = A * 128 */
  186. /* Convert from float to q7 and then store the results in the destination buffer */
  187. in = *pIn++;
  188. in = (in * 128);
  189. in += in > 0.0f ? 0.5f : -0.5f;
  190. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  191. #else
  192. /* C = A * 128 */
  193. /* Convert from float to q7 and then store the results in the destination buffer */
  194. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  195. #endif /* #ifdef ARM_MATH_ROUNDING */
  196. /* Decrement the loop counter */
  197. blkCnt--;
  198. }
  199. }
  200. #else
  201. void arm_float_to_q7(
  202. const float32_t * pSrc,
  203. q7_t * pDst,
  204. uint32_t blockSize)
  205. {
  206. uint32_t blkCnt; /* Loop counter */
  207. const float32_t *pIn = pSrc; /* Source pointer */
  208. #ifdef ARM_MATH_ROUNDING
  209. float32_t in;
  210. #endif /* #ifdef ARM_MATH_ROUNDING */
  211. #if defined (ARM_MATH_LOOPUNROLL)
  212. /* Loop unrolling: Compute 4 outputs at a time */
  213. blkCnt = blockSize >> 2U;
  214. while (blkCnt > 0U)
  215. {
  216. /* C = A * 128 */
  217. /* Convert from float to q7 and store result in destination buffer */
  218. #ifdef ARM_MATH_ROUNDING
  219. in = (*pIn++ * 128);
  220. in += in > 0.0f ? 0.5f : -0.5f;
  221. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  222. in = (*pIn++ * 128);
  223. in += in > 0.0f ? 0.5f : -0.5f;
  224. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  225. in = (*pIn++ * 128);
  226. in += in > 0.0f ? 0.5f : -0.5f;
  227. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  228. in = (*pIn++ * 128);
  229. in += in > 0.0f ? 0.5f : -0.5f;
  230. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  231. #else
  232. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  233. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  234. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  235. *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  236. #endif /* #ifdef ARM_MATH_ROUNDING */
  237. /* Decrement loop counter */
  238. blkCnt--;
  239. }
  240. /* Loop unrolling: Compute remaining outputs */
  241. blkCnt = blockSize % 0x4U;
  242. #else
  243. /* Initialize blkCnt with number of samples */
  244. blkCnt = blockSize;
  245. #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
  246. while (blkCnt > 0U)
  247. {
  248. /* C = A * 128 */
  249. /* Convert from float to q7 and store result in destination buffer */
  250. #ifdef ARM_MATH_ROUNDING
  251. in = (*pIn++ * 128);
  252. in += in > 0.0f ? 0.5f : -0.5f;
  253. *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  254. #else
  255. *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  256. #endif /* #ifdef ARM_MATH_ROUNDING */
  257. /* Decrement loop counter */
  258. blkCnt--;
  259. }
  260. }
  261. #endif /* #if defined(ARM_MATH_NEON) */
  262. #endif /* defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE) */
  263. /**
  264. @} end of float_to_x group
  265. */