Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_float_to_q31.c Source File

arm_float_to_q31.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_float_to_q31.c
00004  * Description:  Converts the elements of the floating-point vector to Q31 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  * @defgroup float_to_x  Convert 32-bit floating point value
00037  */
00038 
00039 /**
00040  * @addtogroup float_to_x
00041  * @{
00042  */
00043 
00044 /**
00045  * @brief Converts the elements of the floating-point vector to Q31 vector.
00046  * @param[in]       *pSrc points to the floating-point input vector
00047  * @param[out]      *pDst points to the Q31 output vector
00048  * @param[in]       blockSize length of the input vector
00049  * @return none.
00050  *
00051  *\par Description:
00052  * \par
00053  * The equation used for the conversion process is:
00054  *
00055  * <pre>
00056  *  pDst[n] = (q31_t)(pSrc[n] * 2147483648);   0 <= n < blockSize.
00057  * </pre>
00058  * <b>Scaling and Overflow Behavior:</b>
00059  * \par
00060  * The function uses saturating arithmetic.
00061  * Results outside of the allowable Q31 range[0x80000000 0x7FFFFFFF] will be saturated.
00062  *
00063  * \note In order to apply rounding, the library should be rebuilt with the ROUNDING macro
00064  * defined in the preprocessor section of project options.
00065  */
00066 
00067 
00068 void arm_float_to_q31(
00069   float32_t * pSrc,
00070   q31_t * pDst,
00071   uint32_t blockSize)
00072 {
00073   float32_t *pIn = pSrc;                         /* Src pointer */
00074   uint32_t blkCnt;                               /* loop counter */
00075 
00076 #ifdef ARM_MATH_ROUNDING
00077 
00078   float32_t in;
00079 
00080 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00081 
00082 #if defined (ARM_MATH_DSP)
00083 
00084   /* Run the below code for Cortex-M4 and Cortex-M3 */
00085 
00086   /*loop Unrolling */
00087   blkCnt = blockSize >> 2U;
00088 
00089   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00090    ** a second loop below computes the remaining 1 to 3 samples. */
00091   while (blkCnt > 0U)
00092   {
00093 
00094 #ifdef ARM_MATH_ROUNDING
00095 
00096     /* C = A * 32768 */
00097     /* convert from float to Q31 and then store the results in the destination buffer */
00098     in = *pIn++;
00099     in = (in * 2147483648.0f);
00100     in += in > 0.0f ? 0.5f : -0.5f;
00101     *pDst++ = clip_q63_to_q31((q63_t) (in));
00102 
00103     in = *pIn++;
00104     in = (in * 2147483648.0f);
00105     in += in > 0.0f ? 0.5f : -0.5f;
00106     *pDst++ = clip_q63_to_q31((q63_t) (in));
00107 
00108     in = *pIn++;
00109     in = (in * 2147483648.0f);
00110     in += in > 0.0f ? 0.5f : -0.5f;
00111     *pDst++ = clip_q63_to_q31((q63_t) (in));
00112 
00113     in = *pIn++;
00114     in = (in * 2147483648.0f);
00115     in += in > 0.0f ? 0.5f : -0.5f;
00116     *pDst++ = clip_q63_to_q31((q63_t) (in));
00117 
00118 #else
00119 
00120     /* C = A * 2147483648 */
00121     /* convert from float to Q31 and then store the results in the destination buffer */
00122     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
00123     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
00124     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
00125     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
00126 
00127 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00128 
00129     /* Decrement the loop counter */
00130     blkCnt--;
00131   }
00132 
00133   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00134    ** No loop unrolling is used. */
00135   blkCnt = blockSize % 0x4U;
00136 
00137   while (blkCnt > 0U)
00138   {
00139 
00140 #ifdef ARM_MATH_ROUNDING
00141 
00142     /* C = A * 2147483648 */
00143     /* convert from float to Q31 and then store the results in the destination buffer */
00144     in = *pIn++;
00145     in = (in * 2147483648.0f);
00146     in += in > 0.0f ? 0.5f : -0.5f;
00147     *pDst++ = clip_q63_to_q31((q63_t) (in));
00148 
00149 #else
00150 
00151     /* C = A * 2147483648 */
00152     /* convert from float to Q31 and then store the results in the destination buffer */
00153     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
00154 
00155 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00156 
00157     /* Decrement the loop counter */
00158     blkCnt--;
00159   }
00160 
00161 
00162 #else
00163 
00164   /* Run the below code for Cortex-M0 */
00165 
00166   /* Loop over blockSize number of values */
00167   blkCnt = blockSize;
00168 
00169   while (blkCnt > 0U)
00170   {
00171 
00172 #ifdef ARM_MATH_ROUNDING
00173 
00174     /* C = A * 2147483648 */
00175     /* convert from float to Q31 and then store the results in the destination buffer */
00176     in = *pIn++;
00177     in = (in * 2147483648.0f);
00178     in += in > 0 ? 0.5f : -0.5f;
00179     *pDst++ = clip_q63_to_q31((q63_t) (in));
00180 
00181 #else
00182 
00183     /* C = A * 2147483648 */
00184     /* convert from float to Q31 and then store the results in the destination buffer */
00185     *pDst++ = clip_q63_to_q31((q63_t) (*pIn++ * 2147483648.0f));
00186 
00187 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00188 
00189     /* Decrement the loop counter */
00190     blkCnt--;
00191   }
00192 
00193 #endif /* #if defined (ARM_MATH_DSP) */
00194 
00195 }
00196 
00197 /**
00198  * @} end of float_to_x group
00199  */
00200