Robert Lopez / CMSIS5
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_abs_q15.c Source File

arm_abs_q15.c

00001 /* ----------------------------------------------------------------------
00002  * Project:      CMSIS DSP Library
00003  * Title:        arm_abs_q15.c
00004  * Description:  Q15 vector absolute value
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 groupMath
00033  */
00034 
00035 /**
00036  * @addtogroup BasicAbs
00037  * @{
00038  */
00039 
00040 /**
00041  * @brief Q15 vector absolute value.
00042  * @param[in]       *pSrc points to the input buffer
00043  * @param[out]      *pDst points to the output buffer
00044  * @param[in]       blockSize number of samples in each vector
00045  * @return none.
00046  *
00047  * <b>Scaling and Overflow Behavior:</b>
00048  * \par
00049  * The function uses saturating arithmetic.
00050  * The Q15 value -1 (0x8000) will be saturated to the maximum allowable positive value 0x7FFF.
00051  */
00052 
00053 void arm_abs_q15(
00054   q15_t * pSrc,
00055   q15_t * pDst,
00056   uint32_t blockSize)
00057 {
00058   uint32_t blkCnt;                               /* loop counter */
00059 
00060 #if defined (ARM_MATH_DSP)
00061   __SIMD32_TYPE *simd;
00062 
00063 /* Run the below code for Cortex-M4 and Cortex-M3 */
00064 
00065   q15_t in1;                                     /* Input value1 */
00066   q15_t in2;                                     /* Input value2 */
00067 
00068 
00069   /*loop Unrolling */
00070   blkCnt = blockSize >> 2U;
00071 
00072   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
00073    ** a second loop below computes the remaining 1 to 3 samples. */
00074   simd = __SIMD32_CONST(pDst);
00075   while (blkCnt > 0U)
00076   {
00077     /* C = |A| */
00078     /* Read two inputs */
00079     in1 = *pSrc++;
00080     in2 = *pSrc++;
00081 
00082 
00083     /* Store the Absolute result in the destination buffer by packing the two values, in a single cycle */
00084 #ifndef  ARM_MATH_BIG_ENDIAN
00085     *simd++ =
00086       __PKHBT(((in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1)),
00087               ((in2 > 0) ? in2 : (q15_t)__QSUB16(0, in2)), 16);
00088 
00089 #else
00090 
00091 
00092     *simd++ =
00093       __PKHBT(((in2 > 0) ? in2 : (q15_t)__QSUB16(0, in2)),
00094               ((in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1)), 16);
00095 
00096 #endif /* #ifndef  ARM_MATH_BIG_ENDIAN    */
00097 
00098     in1 = *pSrc++;
00099     in2 = *pSrc++;
00100 
00101 
00102 #ifndef  ARM_MATH_BIG_ENDIAN
00103 
00104     *simd++ =
00105       __PKHBT(((in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1)),
00106               ((in2 > 0) ? in2 : (q15_t)__QSUB16(0, in2)), 16);
00107 
00108 #else
00109 
00110 
00111     *simd++ =
00112       __PKHBT(((in2 > 0) ? in2 : (q15_t)__QSUB16(0, in2)),
00113               ((in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1)), 16);
00114 
00115 #endif /* #ifndef  ARM_MATH_BIG_ENDIAN    */
00116 
00117     /* Decrement the loop counter */
00118     blkCnt--;
00119   }
00120   pDst = (q15_t *)simd;
00121 
00122   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
00123    ** No loop unrolling is used. */
00124   blkCnt = blockSize % 0x4U;
00125 
00126   while (blkCnt > 0U)
00127   {
00128     /* C = |A| */
00129     /* Read the input */
00130     in1 = *pSrc++;
00131 
00132     /* Calculate absolute value of input and then store the result in the destination buffer. */
00133     *pDst++ = (in1 > 0) ? in1 : (q15_t)__QSUB16(0, in1);
00134 
00135     /* Decrement the loop counter */
00136     blkCnt--;
00137   }
00138 
00139 #else
00140 
00141   /* Run the below code for Cortex-M0 */
00142 
00143   q15_t in;                                      /* Temporary input variable */
00144 
00145   /* Initialize blkCnt with number of samples */
00146   blkCnt = blockSize;
00147 
00148   while (blkCnt > 0U)
00149   {
00150     /* C = |A| */
00151     /* Read the input */
00152     in = *pSrc++;
00153 
00154     /* Calculate absolute value of input and then store the result in the destination buffer. */
00155     *pDst++ = (in > 0) ? in : ((in == (q15_t) 0x8000) ? 0x7fff : -in);
00156 
00157     /* Decrement the loop counter */
00158     blkCnt--;
00159   }
00160 
00161 #endif /* #if defined (ARM_MATH_DSP) */
00162 
00163 }
00164 
00165 /**
00166  * @} end of BasicAbs group
00167  */
00168