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