CMSIS DSP library

Dependents:   performance_timer Surfboard_ gps2rtty Capstone ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_float_to_q7.c Source File

arm_float_to_q7.c

00001 /* ----------------------------------------------------------------------------    
00002 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.    
00003 *    
00004 * $Date:        19. March 2015
00005 * $Revision:    V.1.4.5  
00006 *    
00007 * Project:      CMSIS DSP Library    
00008 * Title:        arm_float_to_q7.c    
00009 *    
00010 * Description:  Converts the elements of the floating-point vector to Q7 vector.   
00011 *    
00012 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
00013 *  
00014 * Redistribution and use in source and binary forms, with or without 
00015 * modification, are permitted provided that the following conditions
00016 * are met:
00017 *   - Redistributions of source code must retain the above copyright
00018 *     notice, this list of conditions and the following disclaimer.
00019 *   - Redistributions in binary form must reproduce the above copyright
00020 *     notice, this list of conditions and the following disclaimer in
00021 *     the documentation and/or other materials provided with the 
00022 *     distribution.
00023 *   - Neither the name of ARM LIMITED nor the names of its contributors
00024 *     may be used to endorse or promote products derived from this
00025 *     software without specific prior written permission.
00026 *
00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00028 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00029 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00030 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
00031 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00032 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00033 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00034 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00035 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00036 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00037 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00038 * POSSIBILITY OF SUCH DAMAGE.  
00039 * ---------------------------------------------------------------------------- */
00040 
00041 #include "arm_math.h"
00042 
00043 /**    
00044  * @ingroup groupSupport    
00045  */
00046 
00047 /**    
00048  * @addtogroup float_to_x    
00049  * @{    
00050  */
00051 
00052 /**    
00053  * @brief Converts the elements of the floating-point vector to Q7 vector.    
00054  * @param[in]       *pSrc points to the floating-point input vector    
00055  * @param[out]      *pDst points to the Q7 output vector   
00056  * @param[in]       blockSize length of the input vector    
00057  * @return none.    
00058  *    
00059  *\par Description:    
00060  * \par   
00061  * The equation used for the conversion process is:    
00062  * <pre>    
00063  *  pDst[n] = (q7_t)(pSrc[n] * 128);   0 <= n < blockSize.    
00064  * </pre>    
00065  * \par Scaling and Overflow Behavior:    
00066  * \par    
00067  * The function uses saturating arithmetic.    
00068  * Results outside of the allowable Q7 range [0x80 0x7F] will be saturated.    
00069  * \note   
00070  * In order to apply rounding, the library should be rebuilt with the ROUNDING macro     
00071  * defined in the preprocessor section of project options.     
00072  */
00073 
00074 
00075 void arm_float_to_q7(
00076   float32_t * pSrc,
00077   q7_t * pDst,
00078   uint32_t blockSize)
00079 {
00080   float32_t *pIn = pSrc;                         /* Src pointer */
00081   uint32_t blkCnt;                               /* loop counter */
00082 
00083 #ifdef ARM_MATH_ROUNDING
00084 
00085   float32_t in;
00086 
00087 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00088 
00089 #ifndef ARM_MATH_CM0_FAMILY
00090 
00091   /* Run the below code for Cortex-M4 and Cortex-M3 */
00092 
00093   /*loop Unrolling */
00094   blkCnt = blockSize >> 2u;
00095 
00096   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.    
00097    ** a second loop below computes the remaining 1 to 3 samples. */
00098   while(blkCnt > 0u)
00099   {
00100 
00101 #ifdef ARM_MATH_ROUNDING
00102     /* C = A * 128 */
00103     /* convert from float to q7 and then store the results in the destination buffer */
00104     in = *pIn++;
00105     in = (in * 128);
00106     in += in > 0.0f ? 0.5f : -0.5f;
00107     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00108 
00109     in = *pIn++;
00110     in = (in * 128);
00111     in += in > 0.0f ? 0.5f : -0.5f;
00112     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00113 
00114     in = *pIn++;
00115     in = (in * 128);
00116     in += in > 0.0f ? 0.5f : -0.5f;
00117     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00118 
00119     in = *pIn++;
00120     in = (in * 128);
00121     in += in > 0.0f ? 0.5f : -0.5f;
00122     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00123 
00124 #else
00125 
00126     /* C = A * 128 */
00127     /* convert from float to q7 and then store the results in the destination buffer */
00128     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00129     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00130     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00131     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00132 
00133 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00134 
00135     /* Decrement the loop counter */
00136     blkCnt--;
00137   }
00138 
00139   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.    
00140    ** No loop unrolling is used. */
00141   blkCnt = blockSize % 0x4u;
00142 
00143   while(blkCnt > 0u)
00144   {
00145 
00146 #ifdef ARM_MATH_ROUNDING
00147     /* C = A * 128 */
00148     /* convert from float to q7 and then store the results in the destination buffer */
00149     in = *pIn++;
00150     in = (in * 128);
00151     in += in > 0.0f ? 0.5f : -0.5f;
00152     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00153 
00154 #else
00155 
00156     /* C = A * 128 */
00157     /* convert from float to q7 and then store the results in the destination buffer */
00158     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00159 
00160 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00161 
00162     /* Decrement the loop counter */
00163     blkCnt--;
00164   }
00165 
00166 
00167 #else
00168 
00169   /* Run the below code for Cortex-M0 */
00170 
00171 
00172   /* Loop over blockSize number of values */
00173   blkCnt = blockSize;
00174 
00175   while(blkCnt > 0u)
00176   {
00177 #ifdef ARM_MATH_ROUNDING
00178     /* C = A * 128 */
00179     /* convert from float to q7 and then store the results in the destination buffer */
00180     in = *pIn++;
00181     in = (in * 128.0f);
00182     in += in > 0 ? 0.5f : -0.5f;
00183     *pDst++ = (q7_t) (__SSAT((q31_t) (in), 8));
00184 
00185 #else
00186 
00187     /* C = A * 128 */
00188     /* convert from float to q7 and then store the results in the destination buffer */
00189     *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00190 
00191 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00192 
00193     /* Decrement the loop counter */
00194     blkCnt--;
00195   }
00196 
00197 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
00198 
00199 }
00200 
00201 /**    
00202  * @} end of float_to_x group    
00203  */