Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_float_to_q15.c Source File

arm_float_to_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_float_to_q15.c
00004  * Description:  Converts the elements of the floating-point vector to Q15 vector
00005  *
00006  * $Date:        27. January 2017
00007  * $Revision:    V.1.5.1
00008  *
00009  * Target Processor: Cortex-M cores
00010  * -------------------------------------------------------------------- */
00011 /*
00012  * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
00013  *
00014  * SPDX-License-Identifier: Apache-2.0
00015  *
00016  * Licensed under the Apache License, Version 2.0 (the License); you may
00017  * not use this file except in compliance with the License.
00018  * You may obtain a copy of the License at
00019  *
00020  * www.apache.org/licenses/LICENSE-2.0
00021  *
00022  * Unless required by applicable law or agreed to in writing, software
00023  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00024  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00025  * See the License for the specific language governing permissions and
00026  * limitations under the License.
00027  */
00028 
00029 #include "arm_math.h"
00030 
00031 /**
00032  * @ingroup groupSupport
00033  */
00034 
00035 /**
00036  * @addtogroup float_to_x
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Converts the elements of the floating-point vector to Q15 vector.
00042  * @param[in]       *pSrc points to the floating-point input vector
00043  * @param[out]      *pDst points to the Q15 output vector
00044  * @param[in]       blockSize length of the input vector
00045  * @return none.
00046  *
00047  * \par Description:
00048  * \par
00049  * The equation used for the conversion process is:
00050  * <pre>
00051  *  pDst[n] = (q15_t)(pSrc[n] * 32768);   0 <= n < blockSize.
00052  * </pre>
00053  * \par Scaling and Overflow Behavior:
00054  * \par
00055  * The function uses saturating arithmetic.
00056  * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
00057  * \note
00058  * In order to apply rounding, the library should be rebuilt with the ROUNDING macro
00059  * defined in the preprocessor section of project options.
00060  *
00061  */
00062 
00063 
00064 void arm_float_to_q15(
00065   float32_t * pSrc,
00066   q15_t * pDst,
00067   uint32_t blockSize)
00068 {
00069   float32_t *pIn = pSrc;                         /* Src pointer */
00070   uint32_t blkCnt;                               /* loop counter */
00071 
00072 #ifdef ARM_MATH_ROUNDING
00073 
00074   float32_t in;
00075 
00076 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00077 
00078 #if defined (ARM_MATH_DSP)
00079 
00080   /* Run the below code for Cortex-M4 and Cortex-M3 */
00081 
00082   /*loop Unrolling */
00083   blkCnt = blockSize >> 2U;
00084 
00085   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00086    ** a second loop below computes the remaining 1 to 3 samples. */
00087   while (blkCnt > 0U)
00088   {
00089 
00090 #ifdef ARM_MATH_ROUNDING
00091     /* C = A * 32768 */
00092     /* convert from float to q15 and then store the results in the destination buffer */
00093     in = *pIn++;
00094     in = (in * 32768.0f);
00095     in += in > 0.0f ? 0.5f : -0.5f;
00096     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
00097 
00098     in = *pIn++;
00099     in = (in * 32768.0f);
00100     in += in > 0.0f ? 0.5f : -0.5f;
00101     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
00102 
00103     in = *pIn++;
00104     in = (in * 32768.0f);
00105     in += in > 0.0f ? 0.5f : -0.5f;
00106     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
00107 
00108     in = *pIn++;
00109     in = (in * 32768.0f);
00110     in += in > 0.0f ? 0.5f : -0.5f;
00111     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
00112 
00113 #else
00114 
00115     /* C = A * 32768 */
00116     /* convert from float to q15 and then store the results in the destination buffer */
00117     *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
00118     *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
00119     *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
00120     *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
00121 
00122 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00123 
00124     /* Decrement the loop counter */
00125     blkCnt--;
00126   }
00127 
00128   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00129    ** No loop unrolling is used. */
00130   blkCnt = blockSize % 0x4U;
00131 
00132   while (blkCnt > 0U)
00133   {
00134 
00135 #ifdef ARM_MATH_ROUNDING
00136     /* C = A * 32768 */
00137     /* convert from float to q15 and then store the results in the destination buffer */
00138     in = *pIn++;
00139     in = (in * 32768.0f);
00140     in += in > 0.0f ? 0.5f : -0.5f;
00141     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
00142 
00143 #else
00144 
00145     /* C = A * 32768 */
00146     /* convert from float to q15 and then store the results in the destination buffer */
00147     *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
00148 
00149 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00150 
00151     /* Decrement the loop counter */
00152     blkCnt--;
00153   }
00154 
00155 
00156 #else
00157 
00158   /* Run the below code for Cortex-M0 */
00159 
00160   /* Loop over blockSize number of values */
00161   blkCnt = blockSize;
00162 
00163   while (blkCnt > 0U)
00164   {
00165 
00166 #ifdef ARM_MATH_ROUNDING
00167     /* C = A * 32768 */
00168     /* convert from float to q15 and then store the results in the destination buffer */
00169     in = *pIn++;
00170     in = (in * 32768.0f);
00171     in += in > 0 ? 0.5f : -0.5f;
00172     *pDst++ = (q15_t) (__SSAT((q31_t) (in), 16));
00173 
00174 #else
00175 
00176     /* C = A * 32768 */
00177     /* convert from float to q15 and then store the results in the destination buffer */
00178     *pDst++ = (q15_t) __SSAT((q31_t) (*pIn++ * 32768.0f), 16);
00179 
00180 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00181 
00182     /* Decrement the loop counter */
00183     blkCnt--;
00184   }
00185 
00186 #endif /* #if defined (ARM_MATH_DSP) */
00187 
00188 }
00189 
00190 /**
00191  * @} end of float_to_x group
00192  */
00193