1 /*
2 * Copyright (C) 2010-2018 Arm Limited or its affiliates. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 /* ----------------------------------------------------------------------
20 * Project: CMSIS NN Library
21 * Title: arm_nn_mult_q15.c
22 * Description: Q15 vector multiplication with variable output shifts
23 *
24 * $Date: 09. October 2020
25 * $Revision: V.1.0.2
26 *
27 * Target Processor: Cortex-M cores
28 *
29 * -------------------------------------------------------------------- */
30
31 #include "arm_nnsupportfunctions.h"
32
33 /**
34 * @ingroup groupSupport
35 */
36
37 /**
38 * @addtogroup NNBasicMath
39 * @{
40 */
41
42 /**
43 * @brief Q7 vector multiplication with variable output shifts
44 * @param[in] *pSrcA pointer to the first input vector
45 * @param[in] *pSrcB pointer to the second input vector
46 * @param[out] *pDst pointer to the output vector
47 * @param[in] out_shift amount of right-shift for output
48 * @param[in] blockSize number of samples in each vector
49 *
50 * <b>Scaling and Overflow Behavior:</b>
51 * \par
52 * The function uses saturating arithmetic.
53 * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
54 */
55
arm_nn_mult_q15(q15_t * pSrcA,q15_t * pSrcB,q15_t * pDst,const uint16_t out_shift,uint32_t blockSize)56 void arm_nn_mult_q15(q15_t *pSrcA, q15_t *pSrcB, q15_t *pDst, const uint16_t out_shift, uint32_t blockSize)
57 {
58 uint32_t blkCnt; /* loop counters */
59
60 #if defined(ARM_MATH_DSP)
61
62 /* Run the below code for Cortex-M4 and Cortex-M3 */
63 q31_t inA1, inA2, inB1, inB2; /* temporary input variables */
64 q15_t out1, out2, out3, out4; /* temporary output variables */
65 q31_t mul1, mul2, mul3, mul4; /* temporary variables */
66
67 /* loop Unrolling */
68 blkCnt = blockSize >> 2U;
69
70 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
71 ** a second loop below computes the remaining 1 to 3 samples. */
72 while (blkCnt > 0U)
73 {
74 /* read two samples at a time from sourceA */
75 inA1 = arm_nn_read_q15x2_ia((const q15_t **)&pSrcA);
76 /* read two samples at a time from sourceB */
77 inB1 = arm_nn_read_q15x2_ia((const q15_t **)&pSrcB);
78 /* read two samples at a time from sourceA */
79 inA2 = arm_nn_read_q15x2_ia((const q15_t **)&pSrcA);
80 /* read two samples at a time from sourceB */
81 inB2 = arm_nn_read_q15x2_ia((const q15_t **)&pSrcB);
82
83 /* multiply mul = sourceA * sourceB */
84 mul1 = (q31_t)((q15_t)(inA1 >> 16) * (q15_t)(inB1 >> 16));
85 mul2 = (q31_t)((q15_t)inA1 * (q15_t)inB1);
86 mul3 = (q31_t)((q15_t)(inA2 >> 16) * (q15_t)(inB2 >> 16));
87 mul4 = (q31_t)((q15_t)inA2 * (q15_t)inB2);
88
89 /* saturate result to 16 bit */
90 out1 = (q15_t)__SSAT((q31_t)(mul1 + NN_ROUND(out_shift)) >> out_shift, 16);
91 out2 = (q15_t)__SSAT((q31_t)(mul2 + NN_ROUND(out_shift)) >> out_shift, 16);
92 out3 = (q15_t)__SSAT((q31_t)(mul3 + NN_ROUND(out_shift)) >> out_shift, 16);
93 out4 = (q15_t)__SSAT((q31_t)(mul4 + NN_ROUND(out_shift)) >> out_shift, 16);
94
95 /* store the result */
96 #ifndef ARM_MATH_BIG_ENDIAN
97
98 *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
99 *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
100
101 #else
102
103 *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
104 *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
105
106 #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
107
108 /* Decrement the blockSize loop counter */
109 blkCnt--;
110 }
111
112 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
113 ** No loop unrolling is used. */
114 blkCnt = blockSize % 0x4U;
115
116 #else
117
118 /* Run the below code for Cortex-M0 */
119
120 /* Initialize blkCnt with number of samples */
121 blkCnt = blockSize;
122
123 #endif /* #if defined (ARM_MATH_DSP) */
124
125 while (blkCnt > 0U)
126 {
127 /* C = A * B */
128 /* Multiply the inputs and store the result in the destination buffer */
129 *pDst++ = (q15_t)__SSAT(((q31_t)((q31_t)(*pSrcA++) * (*pSrcB++) + NN_ROUND(out_shift)) >> out_shift), 16);
130
131 /* Decrement the blockSize loop counter */
132 blkCnt--;
133 }
134 }
135
136 /**
137 * @} end of NNBasicMath group
138 */
139