1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_negate_q15.c
4  * Description:  Negates Q15 vectors
5  *
6  * $Date:        23 April 2021
7  * $Revision:    V1.9.0
8  *
9  * Target Processor: Cortex-M and Cortex-A cores
10  * -------------------------------------------------------------------- */
11 /*
12  * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
13  *
14  * SPDX-License-Identifier: Apache-2.0
15  *
16  * Licensed under the Apache License, Version 2.0 (the License); you may
17  * not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */
28 
29 #include "dsp/basic_math_functions.h"
30 
31 /**
32   @ingroup groupMath
33  */
34 
35 /**
36   @addtogroup BasicNegate
37   @{
38  */
39 
40 /**
41   @brief         Negates the elements of a Q15 vector.
42   @param[in]     pSrc       points to the input vector.
43   @param[out]    pDst       points to the output vector.
44   @param[in]     blockSize  number of samples in each vector.
45 
46   @par           Conditions for optimum performance
47                    Input and output buffers should be aligned by 32-bit
48   @par           Scaling and Overflow Behavior
49                    The function uses saturating arithmetic.
50                    The Q15 value -1 (0x8000) is saturated to the maximum allowable positive value 0x7FFF.
51  */
52 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
53 
54 #include "arm_helium_utils.h"
55 
arm_negate_q15(const q15_t * pSrc,q15_t * pDst,uint32_t blockSize)56 ARM_DSP_ATTRIBUTE void arm_negate_q15(
57     const q15_t  * pSrc,
58     q15_t  * pDst,
59     uint32_t blockSize)
60 {
61     uint32_t  blkCnt;           /* loop counters */
62     q15x8_t vecSrc;
63 
64     /* Compute 8 outputs at a time */
65     blkCnt = blockSize >> 3;
66     while (blkCnt > 0U)
67     {
68         /*
69          * C = -A
70          * Negate and then store the results in the destination buffer.
71          */
72         vecSrc = vld1q(pSrc);
73         vst1q(pDst, vqnegq(vecSrc));
74         /*
75          * Decrement the blockSize loop counter
76          */
77         blkCnt--;
78         /*
79          * advance vector source and destination pointers
80          */
81         pSrc += 8;
82         pDst += 8;
83     }
84     /*
85      * tail
86      */
87     blkCnt = blockSize & 7;
88     if (blkCnt > 0U)
89     {
90         mve_pred16_t p0 = vctp16q(blkCnt);
91         vecSrc = vld1q(pSrc);
92         vstrhq_p(pDst, vqnegq(vecSrc), p0);
93     }
94 }
95 
96 #else
arm_negate_q15(const q15_t * pSrc,q15_t * pDst,uint32_t blockSize)97 ARM_DSP_ATTRIBUTE void arm_negate_q15(
98   const q15_t * pSrc,
99         q15_t * pDst,
100         uint32_t blockSize)
101 {
102         uint32_t blkCnt;                               /* Loop counter */
103         q15_t in;                                      /* Temporary input variable */
104 
105 #if defined (ARM_MATH_LOOPUNROLL)
106 
107 #if defined (ARM_MATH_DSP)
108   q31_t in1;                                    /* Temporary input variables */
109 #endif
110 
111   /* Loop unrolling: Compute 4 outputs at a time */
112   blkCnt = blockSize >> 2U;
113 
114   while (blkCnt > 0U)
115   {
116     /* C = -A */
117 
118 #if defined (ARM_MATH_DSP)
119     /* Negate and store result in destination buffer (2 samples at a time). */
120     in1 = read_q15x2_ia (&pSrc);
121     write_q15x2_ia (&pDst, __QSUB16(0, in1));
122 
123     in1 = read_q15x2_ia (&pSrc);
124     write_q15x2_ia (&pDst, __QSUB16(0, in1));
125 #else
126     in = *pSrc++;
127     *pDst++ = (in == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in;
128 
129     in = *pSrc++;
130     *pDst++ = (in == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in;
131 
132     in = *pSrc++;
133     *pDst++ = (in == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in;
134 
135     in = *pSrc++;
136     *pDst++ = (in == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in;
137 #endif
138 
139     /* Decrement loop counter */
140     blkCnt--;
141   }
142 
143   /* Loop unrolling: Compute remaining outputs */
144   blkCnt = blockSize % 0x4U;
145 
146 #else
147 
148   /* Initialize blkCnt with number of samples */
149   blkCnt = blockSize;
150 
151 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
152 
153   while (blkCnt > 0U)
154   {
155     /* C = -A */
156 
157     /* Negate and store result in destination buffer. */
158     in = *pSrc++;
159     *pDst++ = (in == (q15_t) 0x8000) ? (q15_t) 0x7fff : -in;
160 
161     /* Decrement loop counter */
162     blkCnt--;
163   }
164 
165 }
166 #endif /* defined(ARM_MATH_MVEI) */
167 
168 /**
169   @} end of BasicNegate group
170  */
171