1 /* ----------------------------------------------------------------------
2  * Project:      CMSIS DSP Library
3  * Title:        arm_scale_q15.c
4  * Description:  Multiplies a Q15 vector by a scalar
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 BasicScale
37   @{
38  */
39 
40 /**
41   @brief         Multiplies a Q15 vector by a scalar.
42   @param[in]     pSrc       points to the input vector
43   @param[in]     scaleFract fractional portion of the scale value
44   @param[in]     shift      number of bits to shift the result by
45   @param[out]    pDst       points to the output vector
46   @param[in]     blockSize  number of samples in each vector
47   @return        none
48 
49   @par           Scaling and Overflow Behavior
50                    The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.
51                    These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
52  */
53 
54 #if defined(ARM_MATH_MVEI) && !defined(ARM_MATH_AUTOVECTORIZE)
55 
56 #include "arm_helium_utils.h"
57 
arm_scale_q15(const q15_t * pSrc,q15_t scaleFract,int8_t shift,q15_t * pDst,uint32_t blockSize)58 void arm_scale_q15(
59     const q15_t * pSrc,
60     q15_t   scaleFract,
61     int8_t  shift,
62     q15_t * pDst,
63     uint32_t blockSize)
64 {
65     uint32_t  blkCnt;           /* loop counters */
66     q15x8_t vecSrc;
67     q15x8_t vecDst;
68 
69 
70     /* Compute 8 outputs at a time */
71     blkCnt = blockSize >> 3;
72 
73     while (blkCnt > 0U)
74     {
75         /*
76          * C = A * scale
77          * Scale the input and then store the result in the destination buffer.
78          */
79         vecSrc = vld1q(pSrc);
80         vecDst = vmulhq(vecSrc, vdupq_n_s16(scaleFract));
81         vecDst = vqshlq_r(vecDst, shift + 1);
82         vst1q(pDst, vecDst);
83         /*
84          * Decrement the blockSize loop counter
85          */
86         blkCnt--;
87         /*
88          * advance vector source and destination pointers
89          */
90         pSrc += 8;
91         pDst += 8;
92     }
93     /*
94      * tail
95      */
96     blkCnt = blockSize & 7;
97     if (blkCnt > 0U)
98     {
99         mve_pred16_t p0 = vctp16q(blkCnt);;
100         vecSrc = vld1q(pSrc);
101         vecDst = vmulhq(vecSrc, vdupq_n_s16(scaleFract));
102         vecDst = vqshlq_r(vecDst, shift + 1);
103         vstrhq_p(pDst, vecDst, p0);
104     }
105 
106 }
107 
108 
109 #else
arm_scale_q15(const q15_t * pSrc,q15_t scaleFract,int8_t shift,q15_t * pDst,uint32_t blockSize)110 void arm_scale_q15(
111   const q15_t *pSrc,
112         q15_t scaleFract,
113         int8_t shift,
114         q15_t *pDst,
115         uint32_t blockSize)
116 {
117         uint32_t blkCnt;                               /* Loop counter */
118         int8_t kShift = 15 - shift;                    /* Shift to apply after scaling */
119 
120 #if defined (ARM_MATH_LOOPUNROLL)
121 #if defined (ARM_MATH_DSP)
122   q31_t inA1, inA2;
123   q31_t out1, out2, out3, out4;                  /* Temporary output variables */
124   q15_t in1, in2, in3, in4;                      /* Temporary input variables */
125 #endif
126 #endif
127 
128 #if defined (ARM_MATH_LOOPUNROLL)
129 
130   /* Loop unrolling: Compute 4 outputs at a time */
131   blkCnt = blockSize >> 2U;
132 
133   while (blkCnt > 0U)
134   {
135     /* C = A * scale */
136 
137 #if defined (ARM_MATH_DSP)
138     /* read 2 times 2 samples at a time from source */
139     inA1 = read_q15x2_ia ((q15_t **) &pSrc);
140     inA2 = read_q15x2_ia ((q15_t **) &pSrc);
141 
142     /* Scale inputs and store result in temporary variables
143      * in single cycle by packing the outputs */
144     out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract);
145     out2 = (q31_t) ((q15_t) (inA1      ) * scaleFract);
146     out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract);
147     out4 = (q31_t) ((q15_t) (inA2      ) * scaleFract);
148 
149     /* apply shifting */
150     out1 = out1 >> kShift;
151     out2 = out2 >> kShift;
152     out3 = out3 >> kShift;
153     out4 = out4 >> kShift;
154 
155     /* saturate the output */
156     in1 = (q15_t) (__SSAT(out1, 16));
157     in2 = (q15_t) (__SSAT(out2, 16));
158     in3 = (q15_t) (__SSAT(out3, 16));
159     in4 = (q15_t) (__SSAT(out4, 16));
160 
161     /* store result to destination */
162     write_q15x2_ia (&pDst, __PKHBT(in2, in1, 16));
163     write_q15x2_ia (&pDst, __PKHBT(in4, in3, 16));
164 #else
165     *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
166     *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
167     *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
168     *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
169 #endif
170 
171     /* Decrement loop counter */
172     blkCnt--;
173   }
174 
175   /* Loop unrolling: Compute remaining outputs */
176   blkCnt = blockSize % 0x4U;
177 
178 #else
179 
180   /* Initialize blkCnt with number of samples */
181   blkCnt = blockSize;
182 
183 #endif /* #if defined (ARM_MATH_LOOPUNROLL) */
184 
185   while (blkCnt > 0U)
186   {
187     /* C = A * scale */
188 
189     /* Scale input and store result in destination buffer. */
190     *pDst++ = (q15_t) (__SSAT(((q31_t) *pSrc++ * scaleFract) >> kShift, 16));
191 
192     /* Decrement loop counter */
193     blkCnt--;
194   }
195 
196 }
197 #endif /* defined(ARM_MATH_MVEI) */
198 
199 /**
200   @} end of BasicScale group
201  */
202