1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_mat_scale_q15.c
10 * Description: Multiplies a Q15 matrix by a scalar.
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
14 * Version 1.0.10 2011/7/15
15 * Big Endian support added and Merged M0 and M3/M4 Source code.
17 * Version 1.0.3 2010/11/29
18 * Re-organized the CMSIS folders and updated documentation.
20 * Version 1.0.2 2010/11/11
21 * Documentation updated.
23 * Version 1.0.1 2010/10/05
24 * Production release and review comments incorporated.
26 * Version 1.0.0 2010/09/20
27 * Production release and review comments incorporated.
29 * Version 0.0.5 2010/04/26
30 * incorporated review comments and updated with latest CMSIS layer
32 * Version 0.0.3 2010/03/10
34 * -------------------------------------------------------------------- */
39 * @ingroup groupMatrix
43 * @addtogroup MatrixScale
48 * @brief Q15 matrix scaling.
49 * @param[in] *pSrc points to input matrix
50 * @param[in] scaleFract fractional portion of the scale factor
51 * @param[in] shift number of bits to shift the result by
52 * @param[out] *pDst points to output matrix structure
53 * @return The function returns either
54 * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking.
57 * <b>Scaling and Overflow Behavior:</b>
59 * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format.
60 * These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format.
63 arm_status arm_mat_scale_q15(
64 const arm_matrix_instance_q15 * pSrc,
67 arm_matrix_instance_q15 * pDst)
69 q15_t *pIn = pSrc->pData; /* input data matrix pointer */
70 q15_t *pOut = pDst->pData; /* output data matrix pointer */
71 uint32_t numSamples; /* total number of elements in the matrix */
72 int32_t totShift = 15 - shift; /* total shift to apply after scaling */
73 uint32_t blkCnt; /* loop counters */
74 arm_status status; /* status of matrix scaling */
76 #ifdef ARM_MATH_MATRIX_CHECK
79 /* Check for matrix mismatch */
80 if((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
82 /* Set status as ARM_MATH_SIZE_MISMATCH */
83 status = ARM_MATH_SIZE_MISMATCH;
86 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
89 /* Total number of samples in the input matrix */
90 numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
94 /* Run the below code for Cortex-M4 and Cortex-M3 */
96 blkCnt = numSamples >> 2;
98 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
99 ** a second loop below computes the remaining 1 to 3 samples. */
102 /* C(m,n) = A(m,n) * k */
103 /* Scale, saturate and then store the results in the destination buffer. */
105 (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
107 (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
109 (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
111 (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
113 /* Decrement the numSamples loop counter */
117 /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
118 ** No loop unrolling is used. */
119 blkCnt = numSamples % 0x4u;
123 /* Run the below code for Cortex-M0 */
125 /* Initialize blkCnt with number of samples */
128 #endif /* #ifndef ARM_MATH_CM0 */
132 /* C(m,n) = A(m,n) * k */
133 /* Scale, saturate and then store the results in the destination buffer. */
135 (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16));
137 /* Decrement the numSamples loop counter */
140 /* Set status as ARM_MATH_SUCCESS */
141 status = ARM_MATH_SUCCESS;
144 /* Return to application */
149 * @} end of MatrixScale group