1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_offset_q31.c
4  * Description:  Q31 vector offset
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 BasicOffset
37   @{
38  */
39 
40 /**
41   @brief         Adds a constant offset to a Q31 vector.
42   @param[in]     pSrc       points to the input vector
43   @param[in]     offset     is the offset to be added
44   @param[out]    pDst       points to the output vector
45   @param[in]     blockSize  number of samples in each vector
46 
47   @par           Scaling and Overflow Behavior
48                    The function uses saturating arithmetic.
49                    Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] are saturated.
50  */
51 
52 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
53 
54 #include "arm_helium_utils.h"
55 
arm_offset_q31(const q31_t * pSrc,q31_t offset,q31_t * pDst,uint32_t blockSize)56 void arm_offset_q31(
57     const q31_t * pSrc,
58     q31_t   offset,
59     q31_t * pDst,
60     uint32_t blockSize)
61 {
62     uint32_t  blkCnt;           /* loop counters */
63     q31x4_t vecSrc;
64 
65     /* Compute 4 outputs at a time */
66     blkCnt = blockSize >> 2;
67     while (blkCnt > 0U)
68     {
69         /*
70          * C = A + offset
71          * Add offset and then store the result in the destination buffer.
72          */
73         vecSrc = vld1q(pSrc);
74         vst1q(pDst, vqaddq(vecSrc, offset));
75         /*
76          * Decrement the blockSize loop counter
77          */
78         blkCnt--;
79         /*
80          * advance vector source and destination pointers
81          */
82         pSrc += 4;
83         pDst += 4;
84     }
85     /*
86      * tail
87      */
88     blkCnt = blockSize & 3;
89     if (blkCnt > 0U)
90     {
91         mve_pred16_t p0 = vctp32q(blkCnt);
92         vecSrc = vld1q(pSrc);
93         vstrwq_p(pDst, vqaddq(vecSrc, offset), p0);
94     }
95 }
96 
97 #else
arm_offset_q31(const q31_t * pSrc,q31_t offset,q31_t * pDst,uint32_t blockSize)98 void arm_offset_q31(
99   const q31_t * pSrc,
100         q31_t offset,
101         q31_t * pDst,
102         uint32_t blockSize)
103 {
104         uint32_t blkCnt;                               /* Loop counter */
105 
106 #if defined (ARM_MATH_LOOPUNROLL)
107 
108   /* Loop unrolling: Compute 4 outputs at a time */
109   blkCnt = blockSize >> 2U;
110 
111   while (blkCnt > 0U)
112   {
113     /* C = A + offset */
114 
115     /* Add offset and store result in destination buffer. */
116     *pDst++ = __QADD(*pSrc++, offset);
117 
118     *pDst++ = __QADD(*pSrc++, offset);
119 
120     *pDst++ = __QADD(*pSrc++, offset);
121 
122     *pDst++ = __QADD(*pSrc++, offset);
123 
124     /* Decrement loop counter */
125     blkCnt--;
126   }
127 
128   /* Loop unrolling: Compute remaining outputs */
129   blkCnt = blockSize % 0x4U;
130 
131 #else
132 
133   /* Initialize blkCnt with number of samples */
134   blkCnt = blockSize;
135 
136 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
137 
138   while (blkCnt > 0U)
139   {
140     /* C = A + offset */
141 
142     /* Add offset and store result in destination buffer. */
143 #if defined (ARM_MATH_DSP)
144     *pDst++ = __QADD(*pSrc++, offset);
145 #else
146     *pDst++ = (q31_t) clip_q63_to_q31((q63_t) * pSrc++ + offset);
147 #endif
148 
149     /* Decrement loop counter */
150     blkCnt--;
151   }
152 
153 }
154 #endif /* defined(ARM_MATH_MVEI) */
155 
156 /**
157   @} end of BasicOffset group
158  */
159