1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_mat_scale_q15.c
4 * Description: Multiplies a Q15 matrix by a scalar
6 * $Date: 27. January 2017
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
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
20 * www.apache.org/licenses/LICENSE-2.0
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.
32 * @ingroup groupMatrix
36 * @addtogroup MatrixScale
41 * @brief Q15 matrix scaling.
42 * @param[in] *pSrc points to input matrix
43 * @param[in] scaleFract fractional portion of the scale factor
44 * @param[in] shift number of bits to shift the result by
45 * @param[out] *pDst points to output matrix structure
46 * @return The function returns either
47 * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
50 * <b>Scaling and Overflow Behavior:</b>
52 * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.
53 * These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
56 arm_status
arm_mat_scale_q15(
57 const arm_matrix_instance_q15
* pSrc
,
60 arm_matrix_instance_q15
* pDst
)
62 q15_t
*pIn
= pSrc
->pData
; /* input data matrix pointer */
63 q15_t
*pOut
= pDst
->pData
; /* output data matrix pointer */
64 uint32_t numSamples
; /* total number of elements in the matrix */
65 int32_t totShift
= 15 - shift
; /* total shift to apply after scaling */
66 uint32_t blkCnt
; /* loop counters */
67 arm_status status
; /* status of matrix scaling */
69 #if defined (ARM_MATH_DSP)
71 q15_t in1
, in2
, in3
, in4
;
72 q31_t out1
, out2
, out3
, out4
;
75 #endif // #if defined (ARM_MATH_DSP)
77 #ifdef ARM_MATH_MATRIX_CHECK
78 /* Check for matrix mismatch */
79 if ((pSrc
->numRows
!= pDst
->numRows
) || (pSrc
->numCols
!= pDst
->numCols
))
81 /* Set status as ARM_MATH_SIZE_MISMATCH */
82 status
= ARM_MATH_SIZE_MISMATCH
;
85 #endif // #ifdef ARM_MATH_MATRIX_CHECK
87 /* Total number of samples in the input matrix */
88 numSamples
= (uint32_t) pSrc
->numRows
* pSrc
->numCols
;
90 #if defined (ARM_MATH_DSP)
92 /* Run the below code for Cortex-M4 and Cortex-M3 */
94 blkCnt
= numSamples
>> 2;
96 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
97 ** a second loop below computes the remaining 1 to 3 samples. */
100 /* C(m,n) = A(m,n) * k */
101 /* Scale, saturate and then store the results in the destination buffer. */
102 /* Reading 2 inputs from memory */
103 inA1
= _SIMD32_OFFSET(pIn
);
104 inA2
= _SIMD32_OFFSET(pIn
+ 2);
107 /* Scale the inputs and then store the 2 results in the destination buffer
108 * in single cycle by packing the outputs */
109 out1
= (q31_t
) ((q15_t
) (inA1
>> 16) * scaleFract
);
110 out2
= (q31_t
) ((q15_t
) inA1
* scaleFract
);
111 out3
= (q31_t
) ((q15_t
) (inA2
>> 16) * scaleFract
);
112 out4
= (q31_t
) ((q15_t
) inA2
* scaleFract
);
114 out1
= out1
>> totShift
;
115 inA1
= _SIMD32_OFFSET(pIn
+ 4);
116 out2
= out2
>> totShift
;
117 inA2
= _SIMD32_OFFSET(pIn
+ 6);
118 out3
= out3
>> totShift
;
119 out4
= out4
>> totShift
;
121 in1
= (q15_t
) (__SSAT(out1
, 16));
122 in2
= (q15_t
) (__SSAT(out2
, 16));
123 in3
= (q15_t
) (__SSAT(out3
, 16));
124 in4
= (q15_t
) (__SSAT(out4
, 16));
126 _SIMD32_OFFSET(pOut
) = __PKHBT(in2
, in1
, 16);
127 _SIMD32_OFFSET(pOut
+ 2) = __PKHBT(in4
, in3
, 16);
129 /* update pointers to process next sampels */
134 /* Decrement the numSamples loop counter */
138 /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
139 ** No loop unrolling is used. */
140 blkCnt
= numSamples
% 0x4U
;
144 /* Run the below code for Cortex-M0 */
146 /* Initialize blkCnt with number of samples */
149 #endif /* #if defined (ARM_MATH_DSP) */
153 /* C(m,n) = A(m,n) * k */
154 /* Scale, saturate and then store the results in the destination buffer. */
156 (q15_t
) (__SSAT(((q31_t
) (*pIn
++) * scaleFract
) >> totShift
, 16));
158 /* Decrement the numSamples loop counter */
161 /* Set status as ARM_MATH_SUCCESS */
162 status
= ARM_MATH_SUCCESS
;
165 /* Return to application */
170 * @} end of MatrixScale group