1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_negate_q31.c
4 * Description: Negates Q31 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 Q31 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 Q31 value -1 (0x80000000) is saturated to the maximum allowable positive value 0x7FFFFFFF.
50 */
51
52 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
53
54 #include "arm_helium_utils.h"
55
arm_negate_q31(const q31_t * pSrc,q31_t * pDst,uint32_t blockSize)56 void arm_negate_q31(
57 const q31_t * pSrc,
58 q31_t * pDst,
59 uint32_t blockSize)
60 {
61 uint32_t blkCnt; /* loop counters */
62 q31x4_t vecSrc;
63
64 /* Compute 4 outputs at a time */
65 blkCnt = blockSize >> 2;
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 += 4;
82 pDst += 4;
83 }
84 /*
85 * tail
86 */
87 blkCnt = blockSize & 3;
88 if (blkCnt > 0U)
89 {
90 mve_pred16_t p0 = vctp32q(blkCnt);
91 vecSrc = vld1q(pSrc);
92 vstrwq_p(pDst, vqnegq(vecSrc), p0);
93 }
94 }
95
96 #else
arm_negate_q31(const q31_t * pSrc,q31_t * pDst,uint32_t blockSize)97 void arm_negate_q31(
98 const q31_t * pSrc,
99 q31_t * pDst,
100 uint32_t blockSize)
101 {
102 uint32_t blkCnt; /* Loop counter */
103 q31_t in; /* Temporary input variable */
104
105 #if defined (ARM_MATH_LOOPUNROLL)
106
107 /* Loop unrolling: Compute 4 outputs at a time */
108 blkCnt = blockSize >> 2U;
109
110 while (blkCnt > 0U)
111 {
112 /* C = -A */
113
114 /* Negate and store result in destination buffer. */
115 in = *pSrc++;
116 #if defined (ARM_MATH_DSP)
117 *pDst++ = __QSUB(0, in);
118 #else
119 *pDst++ = (in == INT32_MIN) ? INT32_MAX : -in;
120 #endif
121
122 in = *pSrc++;
123 #if defined (ARM_MATH_DSP)
124 *pDst++ = __QSUB(0, in);
125 #else
126 *pDst++ = (in == INT32_MIN) ? INT32_MAX : -in;
127 #endif
128
129 in = *pSrc++;
130 #if defined (ARM_MATH_DSP)
131 *pDst++ = __QSUB(0, in);
132 #else
133 *pDst++ = (in == INT32_MIN) ? INT32_MAX : -in;
134 #endif
135
136 in = *pSrc++;
137 #if defined (ARM_MATH_DSP)
138 *pDst++ = __QSUB(0, in);
139 #else
140 *pDst++ = (in == INT32_MIN) ? INT32_MAX : -in;
141 #endif
142
143 /* Decrement loop counter */
144 blkCnt--;
145 }
146
147 /* Loop unrolling: Compute remaining outputs */
148 blkCnt = blockSize % 0x4U;
149
150 #else
151
152 /* Initialize blkCnt with number of samples */
153 blkCnt = blockSize;
154
155 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
156
157 while (blkCnt > 0U)
158 {
159 /* C = -A */
160
161 /* Negate and store result in destination buffer. */
162 in = *pSrc++;
163 #if defined (ARM_MATH_DSP)
164 *pDst++ = __QSUB(0, in);
165 #else
166 *pDst++ = (in == INT32_MIN) ? INT32_MAX : -in;
167 #endif
168
169 /* Decrement loop counter */
170 blkCnt--;
171 }
172
173 }
174 #endif /* defined(ARM_MATH_MVEI) */
175
176 /**
177 @} end of BasicNegate group
178 */
179