1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_offset_q7.c
4  * Description:  Q7 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 Q7 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 Q7 range [0x80 0x7F] are saturated.
50  */
51 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
52 
53 #include "arm_helium_utils.h"
54 
arm_offset_q7(const q7_t * pSrc,q7_t offset,q7_t * pDst,uint32_t blockSize)55 ARM_DSP_ATTRIBUTE void arm_offset_q7(
56     const q7_t * pSrc,
57     q7_t   offset,
58     q7_t * pDst,
59     uint32_t blockSize)
60 {
61     uint32_t  blkCnt;           /* loop counters */
62     q7x16_t vecSrc;
63 
64     /* Compute 16 outputs at a time */
65     blkCnt = blockSize >> 4;
66     while (blkCnt > 0U)
67     {
68         /*
69          * C = A + offset
70          * Add offset and then store the result in the destination buffer.
71          */
72         vecSrc = vld1q(pSrc);
73         vst1q(pDst, vqaddq(vecSrc, offset));
74         /*
75          * Decrement the blockSize loop counter
76          */
77         blkCnt--;
78         /*
79          * advance vector source and destination pointers
80          */
81         pSrc += 16;
82         pDst += 16;
83     }
84     /*
85      * tail
86      */
87     blkCnt = blockSize & 0xF;
88     if (blkCnt > 0U)
89     {
90         mve_pred16_t p0 = vctp8q(blkCnt);
91         vecSrc = vld1q(pSrc);
92         vstrbq_p(pDst, vqaddq(vecSrc, offset), p0);
93     }
94 }
95 
96 #else
arm_offset_q7(const q7_t * pSrc,q7_t offset,q7_t * pDst,uint32_t blockSize)97 ARM_DSP_ATTRIBUTE void arm_offset_q7(
98   const q7_t * pSrc,
99         q7_t offset,
100         q7_t * pDst,
101         uint32_t blockSize)
102 {
103         uint32_t blkCnt;                               /* Loop counter */
104 
105 #if defined (ARM_MATH_LOOPUNROLL)
106 
107 #if defined (ARM_MATH_DSP)
108   q31_t offset_packed;                           /* Offset packed to 32 bit */
109 
110   /* Offset is packed to 32 bit in order to use SIMD32 for addition */
111   offset_packed = __PACKq7(offset, offset, offset, offset);
112 #endif
113 
114   /* Loop unrolling: Compute 4 outputs at a time */
115   blkCnt = blockSize >> 2U;
116 
117   while (blkCnt > 0U)
118   {
119     /* C = A + offset */
120 
121 #if defined (ARM_MATH_DSP)
122     /* Add offset and store result in destination buffer (4 samples at a time). */
123     write_q7x4_ia (&pDst, __QADD8(read_q7x4_ia (&pSrc), offset_packed));
124 #else
125     *pDst++ = (q7_t) __SSAT((q15_t) *pSrc++ + offset, 8);
126     *pDst++ = (q7_t) __SSAT((q15_t) *pSrc++ + offset, 8);
127     *pDst++ = (q7_t) __SSAT((q15_t) *pSrc++ + offset, 8);
128     *pDst++ = (q7_t) __SSAT((q15_t) *pSrc++ + offset, 8);
129 #endif
130 
131     /* Decrement loop counter */
132     blkCnt--;
133   }
134 
135   /* Loop unrolling: Compute remaining outputs */
136   blkCnt = blockSize % 0x4U;
137 
138 #else
139 
140   /* Initialize blkCnt with number of samples */
141   blkCnt = blockSize;
142 
143 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
144 
145   while (blkCnt > 0U)
146   {
147     /* C = A + offset */
148 
149     /* Add offset and store result in destination buffer. */
150     *pDst++ = (q7_t) __SSAT((q15_t) *pSrc++ + offset, 8);
151 
152     /* Decrement loop counter */
153     blkCnt--;
154   }
155 
156 }
157 #endif /* defined(ARM_MATH_MVEI) */
158 
159 /**
160   @} end of BasicOffset group
161  */
162