Aded CMSIS5 DSP and NN folder. Needs some work

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  * Project:      CMSIS DSP Library
00003  * Title:        arm_float_to_q7.c
00004  * Description:  Converts the elements of the floating-point vector to Q7 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 Q7 vector.
00042  * @param[in]       *pSrc points to the floating-point input vector
00043  * @param[out]      *pDst points to the Q7 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] = (q7_t)(pSrc[n] * 128);   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 Q7 range [0x80 0x7F] 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 void arm_float_to_q7(
00064   float32_t * pSrc,
00065   q7_t * pDst,
00066   uint32_t blockSize)
00067 {
00068   float32_t *pIn = pSrc;                         /* Src pointer */
00069   uint32_t blkCnt;                               /* loop counter */
00070 
00071 #ifdef ARM_MATH_ROUNDING
00072 
00073   float32_t in;
00074 
00075 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00076 
00077 #if defined (ARM_MATH_DSP)
00078 
00079   /* Run the below code for Cortex-M4 and Cortex-M3 */
00080 
00081   /*loop Unrolling */
00082   blkCnt = blockSize >> 2U;
00083 
00084   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00085    ** a second loop below computes the remaining 1 to 3 samples. */
00086   while (blkCnt > 0U)
00087   {
00088 
00089 #ifdef ARM_MATH_ROUNDING
00090     /* C = A * 128 */
00091     /* convert from float to q7 and then store the results in the destination buffer */
00092     in = *pIn++;
00093     in = (in * 128);
00094     in += in > 0.0f ? 0.5f : -0.5f;
00095     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00096 
00097     in = *pIn++;
00098     in = (in * 128);
00099     in += in > 0.0f ? 0.5f : -0.5f;
00100     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00101 
00102     in = *pIn++;
00103     in = (in * 128);
00104     in += in > 0.0f ? 0.5f : -0.5f;
00105     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00106 
00107     in = *pIn++;
00108     in = (in * 128);
00109     in += in > 0.0f ? 0.5f : -0.5f;
00110     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00111 
00112 #else
00113 
00114     /* C = A * 128 */
00115     /* convert from float to q7 and then store the results in the destination buffer */
00116     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00117     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00118     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00119     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00120 
00121 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00122 
00123     /* Decrement the loop counter */
00124     blkCnt--;
00125   }
00126 
00127   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00128    ** No loop unrolling is used. */
00129   blkCnt = blockSize % 0x4U;
00130 
00131   while (blkCnt > 0U)
00132   {
00133 
00134 #ifdef ARM_MATH_ROUNDING
00135     /* C = A * 128 */
00136     /* convert from float to q7 and then store the results in the destination buffer */
00137     in = *pIn++;
00138     in = (in * 128);
00139     in += in > 0.0f ? 0.5f : -0.5f;
00140     *pDst++ = (q7_t) (__SSAT((q15_t) (in), 8));
00141 
00142 #else
00143 
00144     /* C = A * 128 */
00145     /* convert from float to q7 and then store the results in the destination buffer */
00146     *pDst++ = __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00147 
00148 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00149 
00150     /* Decrement the loop counter */
00151     blkCnt--;
00152   }
00153 
00154 
00155 #else
00156 
00157   /* Run the below code for Cortex-M0 */
00158 
00159 
00160   /* Loop over blockSize number of values */
00161   blkCnt = blockSize;
00162 
00163   while (blkCnt > 0U)
00164   {
00165 #ifdef ARM_MATH_ROUNDING
00166     /* C = A * 128 */
00167     /* convert from float to q7 and then store the results in the destination buffer */
00168     in = *pIn++;
00169     in = (in * 128.0f);
00170     in += in > 0 ? 0.5f : -0.5f;
00171     *pDst++ = (q7_t) (__SSAT((q31_t) (in), 8));
00172 
00173 #else
00174 
00175     /* C = A * 128 */
00176     /* convert from float to q7 and then store the results in the destination buffer */
00177     *pDst++ = (q7_t) __SSAT((q31_t) (*pIn++ * 128.0f), 8);
00178 
00179 #endif /*      #ifdef ARM_MATH_ROUNDING        */
00180 
00181     /* Decrement the loop counter */
00182     blkCnt--;
00183   }
00184 
00185 #endif /* #if defined (ARM_MATH_DSP) */
00186 
00187 }
00188 
00189 /**
00190  * @} end of float_to_x group
00191  */
00192