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_q7.c
22  * Description:  Q7 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 Q7 range [0x80 0x7F] will be saturated.
54  */
55 
arm_nn_mult_q7(q7_t * pSrcA,q7_t * pSrcB,q7_t * pDst,const uint16_t out_shift,uint32_t blockSize)56 void arm_nn_mult_q7(q7_t *pSrcA, q7_t *pSrcB, q7_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     q7_t out1, out2, out3, out4; /* Temporary variables to store the product */
64 
65     /* loop Unrolling */
66     blkCnt = blockSize >> 2U;
67 
68     /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
69      ** a second loop below computes the remaining 1 to 3 samples. */
70     while (blkCnt > 0U)
71     {
72         /* C = A * B */
73         /* Multiply the inputs and store the results in temporary variables */
74         out1 = (q7_t)__SSAT(((q15_t)((q15_t)(*pSrcA++) * (*pSrcB++) + NN_ROUND(out_shift)) >> out_shift), 8);
75         out2 = (q7_t)__SSAT(((q15_t)((q15_t)(*pSrcA++) * (*pSrcB++) + NN_ROUND(out_shift)) >> out_shift), 8);
76         out3 = (q7_t)__SSAT(((q15_t)((q15_t)(*pSrcA++) * (*pSrcB++) + NN_ROUND(out_shift)) >> out_shift), 8);
77         out4 = (q7_t)__SSAT(((q15_t)((q15_t)(*pSrcA++) * (*pSrcB++) + NN_ROUND(out_shift)) >> out_shift), 8);
78 
79         /* Store the results of 4 inputs in the destination buffer in single cycle by packing */
80         *__SIMD32(pDst)++ = __PACKq7(out1, out2, out3, out4);
81 
82         /* Decrement the blockSize loop counter */
83         blkCnt--;
84     }
85 
86     /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
87      ** No loop unrolling is used. */
88     blkCnt = blockSize % 0x4U;
89 
90 #else
91 
92     /* Run the below code for Cortex-M0 */
93 
94     /* Initialize blkCnt with number of samples */
95     blkCnt = blockSize;
96 
97 #endif /* #if defined (ARM_MATH_DSP) */
98 
99     while (blkCnt > 0U)
100     {
101         /* C = A * B */
102         /* Multiply the inputs and store the result in the destination buffer */
103         *pDst++ = (q7_t)__SSAT(((q15_t)((q15_t)(*pSrcA++) * (*pSrcB++) + NN_ROUND(out_shift)) >> out_shift), 8);
104 
105         /* Decrement the blockSize loop counter */
106         blkCnt--;
107     }
108 }
109 
110 /**
111  * @} end of NNBasicMath group
112  */
113