1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_negate_q7.c
4  * Description:  Negates Q7 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 Q7 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   @return        none
46 
47   @par           Scaling and Overflow Behavior
48                    The function uses saturating arithmetic.
49                    The Q7 value -1 (0x80) is saturated to the maximum allowable positive value 0x7F.
50  */
51 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
52 
53 #include "arm_helium_utils.h"
54 
arm_negate_q7(const q7_t * pSrc,q7_t * pDst,uint32_t blockSize)55 void arm_negate_q7(
56     const q7_t   * pSrc,
57     q7_t   * pDst,
58     uint32_t blockSize)
59 {
60     uint32_t  blkCnt;           /* loop counters */
61     q7x16_t vecSrc;
62 
63     /* Compute 16 outputs at a time */
64     blkCnt = blockSize >> 4;
65     while (blkCnt > 0U)
66     {
67         /*
68          * C = -A
69          * Negate and then store the results in the destination buffer.
70          */
71         vecSrc = vld1q(pSrc);
72         vst1q(pDst, vqnegq(vecSrc));
73         /*
74          * Decrement the blockSize loop counter
75          */
76         blkCnt--;
77         /*
78          * advance vector source and destination pointers
79          */
80         pSrc += 16;
81         pDst += 16;
82     }
83     /*
84      * tail
85      */
86     blkCnt = blockSize & 0xF;
87     if (blkCnt > 0U)
88     {
89         mve_pred16_t p0 = vctp8q(blkCnt);
90         vecSrc = vld1q(pSrc);
91         vstrbq_p(pDst, vqnegq(vecSrc), p0);
92     }
93 }
94 
95 #else
arm_negate_q7(const q7_t * pSrc,q7_t * pDst,uint32_t blockSize)96 void arm_negate_q7(
97   const q7_t * pSrc,
98         q7_t * pDst,
99         uint32_t blockSize)
100 {
101         uint32_t blkCnt;                               /* Loop counter */
102         q7_t in;                                       /* Temporary input variable */
103 
104 #if defined (ARM_MATH_LOOPUNROLL)
105 
106 #if defined (ARM_MATH_DSP)
107   q31_t in1;                                    /* Temporary input variable */
108 #endif
109 
110   /* Loop unrolling: Compute 4 outputs at a time */
111   blkCnt = blockSize >> 2U;
112 
113   while (blkCnt > 0U)
114   {
115     /* C = -A */
116 
117 #if defined (ARM_MATH_DSP)
118     /* Negate and store result in destination buffer (4 samples at a time). */
119     in1 = read_q7x4_ia ((q7_t **) &pSrc);
120     write_q7x4_ia (&pDst, __QSUB8(0, in1));
121 #else
122     in = *pSrc++;
123     *pDst++ = (in == (q7_t) 0x80) ? (q7_t) 0x7f : -in;
124 
125     in = *pSrc++;
126     *pDst++ = (in == (q7_t) 0x80) ? (q7_t) 0x7f : -in;
127 
128     in = *pSrc++;
129     *pDst++ = (in == (q7_t) 0x80) ? (q7_t) 0x7f : -in;
130 
131     in = *pSrc++;
132     *pDst++ = (in == (q7_t) 0x80) ? (q7_t) 0x7f : -in;
133 #endif
134 
135     /* Decrement loop counter */
136     blkCnt--;
137   }
138 
139   /* Loop unrolling: Compute remaining outputs */
140   blkCnt = blockSize % 0x4U;
141 
142 #else
143 
144   /* Initialize blkCnt with number of samples */
145   blkCnt = blockSize;
146 
147 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
148 
149   while (blkCnt > 0U)
150   {
151     /* C = -A */
152 
153     /* Negate and store result in destination buffer. */
154     in = *pSrc++;
155 
156 #if defined (ARM_MATH_DSP)
157     *pDst++ = (q7_t) __QSUB8(0, in);
158 #else
159     *pDst++ = (in == (q7_t) 0x80) ? (q7_t) 0x7f : -in;
160 #endif
161 
162     /* Decrement loop counter */
163     blkCnt--;
164   }
165 
166 }
167 #endif /* defined(ARM_MATH_MVEI) */
168 
169 /**
170   @} end of BasicNegate group
171  */
172