Aded CMSIS5 DSP and NN folder. Needs some work

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_power_q15.c Source File

arm_power_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_power_q15.c
00004  * Description:  Sum of the squares of the elements of a 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 groupStats
00033  */
00034 
00035 /**
00036  * @addtogroup power
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Sum of the squares of the elements of a Q15 vector.
00042  * @param[in]       *pSrc points to the input vector
00043  * @param[in]       blockSize length of the input vector
00044  * @param[out]      *pResult sum of the squares value returned here
00045  * @return none.
00046  *
00047  * @details
00048  * <b>Scaling and Overflow Behavior:</b>
00049  *
00050  * \par
00051  * The function is implemented using a 64-bit internal accumulator.
00052  * The input is represented in 1.15 format.
00053  * Intermediate multiplication yields a 2.30 format, and this
00054  * result is added without saturation to a 64-bit accumulator in 34.30 format.
00055  * With 33 guard bits in the accumulator, there is no risk of overflow, and the
00056  * full precision of the intermediate multiplication is preserved.
00057  * Finally, the return result is in 34.30 format.
00058  *
00059  */
00060 
00061 void arm_power_q15(
00062   q15_t * pSrc,
00063   uint32_t blockSize,
00064   q63_t * pResult)
00065 {
00066   q63_t sum = 0;                                 /* Temporary result storage */
00067 
00068 #if defined (ARM_MATH_DSP)
00069   /* Run the below code for Cortex-M4 and Cortex-M3 */
00070 
00071   q31_t in32;                                    /* Temporary variable to store input value */
00072   q15_t in16;                                    /* Temporary variable to store input value */
00073   uint32_t blkCnt;                               /* loop counter */
00074 
00075 
00076   /* loop Unrolling */
00077   blkCnt = blockSize >> 2U;
00078 
00079   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00080    ** a second loop below computes the remaining 1 to 3 samples. */
00081   while (blkCnt > 0U)
00082   {
00083     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
00084     /* Compute Power and then store the result in a temporary variable, sum. */
00085     in32 = *__SIMD32(pSrc)++;
00086     sum = __SMLALD(in32, in32, sum);
00087     in32 = *__SIMD32(pSrc)++;
00088     sum = __SMLALD(in32, in32, sum);
00089 
00090     /* Decrement the loop counter */
00091     blkCnt--;
00092   }
00093 
00094   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00095    ** No loop unrolling is used. */
00096   blkCnt = blockSize % 0x4U;
00097 
00098   while (blkCnt > 0U)
00099   {
00100     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
00101     /* Compute Power and then store the result in a temporary variable, sum. */
00102     in16 = *pSrc++;
00103     sum = __SMLALD(in16, in16, sum);
00104 
00105     /* Decrement the loop counter */
00106     blkCnt--;
00107   }
00108 
00109 #else
00110   /* Run the below code for Cortex-M0 */
00111 
00112   q15_t in;                                      /* Temporary variable to store input value */
00113   uint32_t blkCnt;                               /* loop counter */
00114 
00115 
00116   /* Loop over blockSize number of values */
00117   blkCnt = blockSize;
00118 
00119   while (blkCnt > 0U)
00120   {
00121     /* C = A[0] * A[0] + A[1] * A[1] + A[2] * A[2] + ... + A[blockSize-1] * A[blockSize-1] */
00122     /* Compute Power and then store the result in a temporary variable, sum. */
00123     in = *pSrc++;
00124     sum += ((q31_t) in * in);
00125 
00126     /* Decrement the loop counter */
00127     blkCnt--;
00128   }
00129 
00130 #endif /* #if defined (ARM_MATH_DSP) */
00131 
00132   /* Store the results in 34.30 format  */
00133   *pResult = sum;
00134 }
00135 
00136 /**
00137  * @} end of power group
00138  */
00139