1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
7 * Project: CMSIS DSP Library
8 * Title: arm_mat_scale_q31.c
10 * Description: Multiplies a Q31 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 Q31 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.31 format.
60 * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format.
63 arm_status arm_mat_scale_q31(
64 const arm_matrix_instance_q31 * pSrc,
67 arm_matrix_instance_q31 * pDst)
69 q31_t *pIn = pSrc->pData; /* input data matrix pointer */
70 q31_t *pOut = pDst->pData; /* output data matrix pointer */
71 q63_t out; /* temporary variable to hold output value */
72 uint32_t numSamples; /* total number of elements in the matrix */
73 int32_t totShift = 31 - shift; /* shift to apply after scaling */
74 uint32_t blkCnt; /* loop counters */
75 arm_status status; /* status of matrix scaling */
77 #ifdef ARM_MATH_MATRIX_CHECK
80 /* Check for matrix mismatch */
81 if((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols))
83 /* Set status as ARM_MATH_SIZE_MISMATCH */
84 status = ARM_MATH_SIZE_MISMATCH;
87 #endif /* #ifdef ARM_MATH_MATRIX_CHECK */
90 /* Total number of samples in the input matrix */
91 numSamples = (uint32_t) pSrc->numRows * pSrc->numCols;
95 /* Run the below code for Cortex-M4 and Cortex-M3 */
98 blkCnt = numSamples >> 2u;
100 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
101 ** a second loop below computes the remaining 1 to 3 samples. */
104 /* C(m,n) = A(m,n) * k */
105 /* Scale, saturate and then store the results in the destination buffer. */
106 out = ((q63_t) * pIn++ * scaleFract) >> totShift;
107 *pOut++ = clip_q63_to_q31(out);
108 out = ((q63_t) * pIn++ * scaleFract) >> totShift;
109 *pOut++ = clip_q63_to_q31(out);
110 out = ((q63_t) * pIn++ * scaleFract) >> totShift;
111 *pOut++ = clip_q63_to_q31(out);
112 out = ((q63_t) * pIn++ * scaleFract) >> totShift;
113 *pOut++ = clip_q63_to_q31(out);
115 /* Decrement the numSamples loop counter */
119 /* If the numSamples is not a multiple of 4, compute any remaining output samples here.
120 ** No loop unrolling is used. */
121 blkCnt = numSamples % 0x4u;
125 /* Run the below code for Cortex-M0 */
127 /* Initialize blkCnt with number of samples */
130 #endif /* #ifndef ARM_MATH_CM0 */
134 /* C(m,n) = A(m,n) * k */
135 /* Scale, saturate and then store the results in the destination buffer. */
136 out = ((q63_t) * pIn++ * scaleFract) >> totShift;
137 *pOut++ = clip_q63_to_q31(out);
139 /* Decrement the numSamples loop counter */
142 /* Set status as ARM_MATH_SUCCESS */
143 status = ARM_MATH_SUCCESS;
146 /* Return to application */
151 * @} end of MatrixScale group