Subversion Repositories AFRtranscoder

Rev

Blame | Last modification | View Log | Download | RSS feed

  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:        27. January 2017
  7.  * $Revision:    V.1.5.1
  8.  *
  9.  * Target Processor: Cortex-M cores
  10.  * -------------------------------------------------------------------- */
  11. /*
  12.  * Copyright (C) 2010-2017 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.  
  29. #include "arm_math.h"
  30.  
  31. /**
  32.  * @ingroup groupSupport
  33.  */
  34.  
  35. /**
  36.  * @addtogroup float_to_x
  37.  * @{
  38.  */
  39.  
  40. /**
  41.  * @brief Converts the elements of the floating-point vector to Q7 vector.
  42.  * @param[in]       *pSrc points to the floating-point input vector
  43.  * @param[out]      *pDst points to the Q7 output vector
  44.  * @param[in]       blockSize length of the input vector
  45.  * @return none.
  46.  *
  47.  *\par Description:
  48.  * \par
  49.  * The equation used for the conversion process is:
  50.  * <pre>
  51.  *      pDst[n] = (q7_t)(pSrc[n] * 128);   0 <= n < blockSize.
  52.  * </pre>
  53.  * \par Scaling and Overflow Behavior:
  54.  * \par
  55.  * The function uses saturating arithmetic.
  56.  * Results outside of the allowable Q7 range [0x80 0x7F] will be saturated.
  57.  * \note
  58.  * In order to apply rounding, the library should be rebuilt with the ROUNDING macro
  59.  * defined in the preprocessor section of project options.
  60.  */
  61.  
  62.  
  63. void arm_float_to_q7(
  64.   float32_t * pSrc,
  65.   q7_t * pDst,
  66.   uint32_t blockSize)
  67. {
  68.   float32_t *pIn = pSrc;                         /* Src pointer */
  69.   uint32_t blkCnt;                               /* loop counter */
  70.  
  71. #ifdef ARM_MATH_ROUNDING
  72.  
  73.   float32_t in;
  74.  
  75. #endif /*      #ifdef ARM_MATH_ROUNDING        */
  76.  
  77. #if defined (ARM_MATH_DSP)
  78.  
  79.   /* Run the below code for Cortex-M4 and Cortex-M3 */
  80.  
  81.   /*loop Unrolling */
  82.   blkCnt = blockSize >> 2U;
  83.  
  84.   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  85.    ** a second loop below computes the remaining 1 to 3 samples. */
  86.   while (blkCnt > 0U)
  87.   {
  88.  
  89. #ifdef ARM_MATH_ROUNDING
  90.     /* C = A * 128 */
  91.     /* convert from float to q7 and then store the results in the destination buffer */
  92.     in = *pIn++;
  93.     in = (in * 128);
  94.     in += in > 0.0f ? 0.5f : -0.5f;
  95.     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  96.  
  97.     in = *pIn++;
  98.     in = (in * 128);
  99.     in += in > 0.0f ? 0.5f : -0.5f;
  100.     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  101.  
  102.     in = *pIn++;
  103.     in = (in * 128);
  104.     in += in > 0.0f ? 0.5f : -0.5f;
  105.     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  106.  
  107.     in = *pIn++;
  108.     in = (in * 128);
  109.     in += in > 0.0f ? 0.5f : -0.5f;
  110.     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  111.  
  112. #else
  113.  
  114.     /* C = A * 128 */
  115.     /* convert from float to q7 and then store the results in the destination buffer */
  116.     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  117.     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  118.     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  119.     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  120.  
  121. #endif /*      #ifdef ARM_MATH_ROUNDING        */
  122.  
  123.     /* Decrement the loop counter */
  124.     blkCnt--;
  125.   }
  126.  
  127.   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  128.    ** No loop unrolling is used. */
  129.   blkCnt = blockSize % 0x4U;
  130.  
  131.   while (blkCnt > 0U)
  132.   {
  133.  
  134. #ifdef ARM_MATH_ROUNDING
  135.     /* C = A * 128 */
  136.     /* convert from float to q7 and then store the results in the destination buffer */
  137.     in = *pIn++;
  138.     in = (in * 128);
  139.     in += in > 0.0f ? 0.5f : -0.5f;
  140.     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
  141.  
  142. #else
  143.  
  144.     /* C = A * 128 */
  145.     /* convert from float to q7 and then store the results in the destination buffer */
  146.     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  147.  
  148. #endif /*      #ifdef ARM_MATH_ROUNDING        */
  149.  
  150.     /* Decrement the loop counter */
  151.     blkCnt--;
  152.   }
  153.  
  154.  
  155. #else
  156.  
  157.   /* Run the below code for Cortex-M0 */
  158.  
  159.  
  160.   /* Loop over blockSize number of values */
  161.   blkCnt = blockSize;
  162.  
  163.   while (blkCnt > 0U)
  164.   {
  165. #ifdef ARM_MATH_ROUNDING
  166.     /* C = A * 128 */
  167.     /* convert from float to q7 and then store the results in the destination buffer */
  168.     in = *pIn++;
  169.     in = (in * 128.0f);
  170.     in += in > 0 ? 0.5f : -0.5f;
  171.     *pDst++ = (q7_t) (__SSAT((q31_t) (in), 8));
  172.  
  173. #else
  174.  
  175.     /* C = A * 128 */
  176.     /* convert from float to q7 and then store the results in the destination buffer */
  177.     *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0f), 8);
  178.  
  179. #endif /*      #ifdef ARM_MATH_ROUNDING        */
  180.  
  181.     /* Decrement the loop counter */
  182.     blkCnt--;
  183.   }
  184.  
  185. #endif /* #if defined (ARM_MATH_DSP) */
  186.  
  187. }
  188.  
  189. /**
  190.  * @} end of float_to_x group
  191.  */
  192.