The CMSIS DSP 5 library
Dependents: Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more
functions/MatrixFunctions/arm_mat_scale_q31.c@3:4098b9d3d571, 2018-06-21 (annotated)
- Committer:
- xorjoep
- Date:
- Thu Jun 21 11:56:27 2018 +0000
- Revision:
- 3:4098b9d3d571
- Parent:
- 1:24714b45cd1b
headers is a folder not a library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xorjoep | 1:24714b45cd1b | 1 | /* ---------------------------------------------------------------------- |
xorjoep | 1:24714b45cd1b | 2 | * Project: CMSIS DSP Library |
xorjoep | 1:24714b45cd1b | 3 | * Title: arm_mat_scale_q31.c |
xorjoep | 1:24714b45cd1b | 4 | * Description: Multiplies a Q31 matrix by a scalar |
xorjoep | 1:24714b45cd1b | 5 | * |
xorjoep | 1:24714b45cd1b | 6 | * $Date: 27. January 2017 |
xorjoep | 1:24714b45cd1b | 7 | * $Revision: V.1.5.1 |
xorjoep | 1:24714b45cd1b | 8 | * |
xorjoep | 1:24714b45cd1b | 9 | * Target Processor: Cortex-M cores |
xorjoep | 1:24714b45cd1b | 10 | * -------------------------------------------------------------------- */ |
xorjoep | 1:24714b45cd1b | 11 | /* |
xorjoep | 1:24714b45cd1b | 12 | * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. |
xorjoep | 1:24714b45cd1b | 13 | * |
xorjoep | 1:24714b45cd1b | 14 | * SPDX-License-Identifier: Apache-2.0 |
xorjoep | 1:24714b45cd1b | 15 | * |
xorjoep | 1:24714b45cd1b | 16 | * Licensed under the Apache License, Version 2.0 (the License); you may |
xorjoep | 1:24714b45cd1b | 17 | * not use this file except in compliance with the License. |
xorjoep | 1:24714b45cd1b | 18 | * You may obtain a copy of the License at |
xorjoep | 1:24714b45cd1b | 19 | * |
xorjoep | 1:24714b45cd1b | 20 | * www.apache.org/licenses/LICENSE-2.0 |
xorjoep | 1:24714b45cd1b | 21 | * |
xorjoep | 1:24714b45cd1b | 22 | * Unless required by applicable law or agreed to in writing, software |
xorjoep | 1:24714b45cd1b | 23 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
xorjoep | 1:24714b45cd1b | 24 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
xorjoep | 1:24714b45cd1b | 25 | * See the License for the specific language governing permissions and |
xorjoep | 1:24714b45cd1b | 26 | * limitations under the License. |
xorjoep | 1:24714b45cd1b | 27 | */ |
xorjoep | 1:24714b45cd1b | 28 | |
xorjoep | 1:24714b45cd1b | 29 | #include "arm_math.h" |
xorjoep | 1:24714b45cd1b | 30 | |
xorjoep | 1:24714b45cd1b | 31 | /** |
xorjoep | 1:24714b45cd1b | 32 | * @ingroup groupMatrix |
xorjoep | 1:24714b45cd1b | 33 | */ |
xorjoep | 1:24714b45cd1b | 34 | |
xorjoep | 1:24714b45cd1b | 35 | /** |
xorjoep | 1:24714b45cd1b | 36 | * @addtogroup MatrixScale |
xorjoep | 1:24714b45cd1b | 37 | * @{ |
xorjoep | 1:24714b45cd1b | 38 | */ |
xorjoep | 1:24714b45cd1b | 39 | |
xorjoep | 1:24714b45cd1b | 40 | /** |
xorjoep | 1:24714b45cd1b | 41 | * @brief Q31 matrix scaling. |
xorjoep | 1:24714b45cd1b | 42 | * @param[in] *pSrc points to input matrix |
xorjoep | 1:24714b45cd1b | 43 | * @param[in] scaleFract fractional portion of the scale factor |
xorjoep | 1:24714b45cd1b | 44 | * @param[in] shift number of bits to shift the result by |
xorjoep | 1:24714b45cd1b | 45 | * @param[out] *pDst points to output matrix structure |
xorjoep | 1:24714b45cd1b | 46 | * @return The function returns either |
xorjoep | 1:24714b45cd1b | 47 | * <code>ARM_MATH_SIZE_MISMATCH</code> or <code>ARM_MATH_SUCCESS</code> based on the outcome of size checking. |
xorjoep | 1:24714b45cd1b | 48 | * |
xorjoep | 1:24714b45cd1b | 49 | * @details |
xorjoep | 1:24714b45cd1b | 50 | * <b>Scaling and Overflow Behavior:</b> |
xorjoep | 1:24714b45cd1b | 51 | * \par |
xorjoep | 1:24714b45cd1b | 52 | * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.31 format. |
xorjoep | 1:24714b45cd1b | 53 | * These are multiplied to yield a 2.62 intermediate result and this is shifted with saturation to 1.31 format. |
xorjoep | 1:24714b45cd1b | 54 | */ |
xorjoep | 1:24714b45cd1b | 55 | |
xorjoep | 1:24714b45cd1b | 56 | arm_status arm_mat_scale_q31( |
xorjoep | 1:24714b45cd1b | 57 | const arm_matrix_instance_q31 * pSrc, |
xorjoep | 1:24714b45cd1b | 58 | q31_t scaleFract, |
xorjoep | 1:24714b45cd1b | 59 | int32_t shift, |
xorjoep | 1:24714b45cd1b | 60 | arm_matrix_instance_q31 * pDst) |
xorjoep | 1:24714b45cd1b | 61 | { |
xorjoep | 1:24714b45cd1b | 62 | q31_t *pIn = pSrc->pData; /* input data matrix pointer */ |
xorjoep | 1:24714b45cd1b | 63 | q31_t *pOut = pDst->pData; /* output data matrix pointer */ |
xorjoep | 1:24714b45cd1b | 64 | uint32_t numSamples; /* total number of elements in the matrix */ |
xorjoep | 1:24714b45cd1b | 65 | int32_t totShift = shift + 1; /* shift to apply after scaling */ |
xorjoep | 1:24714b45cd1b | 66 | uint32_t blkCnt; /* loop counters */ |
xorjoep | 1:24714b45cd1b | 67 | arm_status status; /* status of matrix scaling */ |
xorjoep | 1:24714b45cd1b | 68 | q31_t in1, in2, out1; /* temporary variabels */ |
xorjoep | 1:24714b45cd1b | 69 | |
xorjoep | 1:24714b45cd1b | 70 | #if defined (ARM_MATH_DSP) |
xorjoep | 1:24714b45cd1b | 71 | |
xorjoep | 1:24714b45cd1b | 72 | q31_t in3, in4, out2, out3, out4; /* temporary variables */ |
xorjoep | 1:24714b45cd1b | 73 | |
xorjoep | 1:24714b45cd1b | 74 | #endif // #ifndef ARM_MAT_CM0 |
xorjoep | 1:24714b45cd1b | 75 | |
xorjoep | 1:24714b45cd1b | 76 | #ifdef ARM_MATH_MATRIX_CHECK |
xorjoep | 1:24714b45cd1b | 77 | /* Check for matrix mismatch */ |
xorjoep | 1:24714b45cd1b | 78 | if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols)) |
xorjoep | 1:24714b45cd1b | 79 | { |
xorjoep | 1:24714b45cd1b | 80 | /* Set status as ARM_MATH_SIZE_MISMATCH */ |
xorjoep | 1:24714b45cd1b | 81 | status = ARM_MATH_SIZE_MISMATCH; |
xorjoep | 1:24714b45cd1b | 82 | } |
xorjoep | 1:24714b45cd1b | 83 | else |
xorjoep | 1:24714b45cd1b | 84 | #endif // #ifdef ARM_MATH_MATRIX_CHECK |
xorjoep | 1:24714b45cd1b | 85 | { |
xorjoep | 1:24714b45cd1b | 86 | /* Total number of samples in the input matrix */ |
xorjoep | 1:24714b45cd1b | 87 | numSamples = (uint32_t) pSrc->numRows * pSrc->numCols; |
xorjoep | 1:24714b45cd1b | 88 | |
xorjoep | 1:24714b45cd1b | 89 | #if defined (ARM_MATH_DSP) |
xorjoep | 1:24714b45cd1b | 90 | |
xorjoep | 1:24714b45cd1b | 91 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
xorjoep | 1:24714b45cd1b | 92 | |
xorjoep | 1:24714b45cd1b | 93 | /* Loop Unrolling */ |
xorjoep | 1:24714b45cd1b | 94 | blkCnt = numSamples >> 2U; |
xorjoep | 1:24714b45cd1b | 95 | |
xorjoep | 1:24714b45cd1b | 96 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
xorjoep | 1:24714b45cd1b | 97 | ** a second loop below computes the remaining 1 to 3 samples. */ |
xorjoep | 1:24714b45cd1b | 98 | while (blkCnt > 0U) |
xorjoep | 1:24714b45cd1b | 99 | { |
xorjoep | 1:24714b45cd1b | 100 | /* C(m,n) = A(m,n) * k */ |
xorjoep | 1:24714b45cd1b | 101 | /* Read values from input */ |
xorjoep | 1:24714b45cd1b | 102 | in1 = *pIn; |
xorjoep | 1:24714b45cd1b | 103 | in2 = *(pIn + 1); |
xorjoep | 1:24714b45cd1b | 104 | in3 = *(pIn + 2); |
xorjoep | 1:24714b45cd1b | 105 | in4 = *(pIn + 3); |
xorjoep | 1:24714b45cd1b | 106 | |
xorjoep | 1:24714b45cd1b | 107 | /* multiply input with scaler value */ |
xorjoep | 1:24714b45cd1b | 108 | in1 = ((q63_t) in1 * scaleFract) >> 32; |
xorjoep | 1:24714b45cd1b | 109 | in2 = ((q63_t) in2 * scaleFract) >> 32; |
xorjoep | 1:24714b45cd1b | 110 | in3 = ((q63_t) in3 * scaleFract) >> 32; |
xorjoep | 1:24714b45cd1b | 111 | in4 = ((q63_t) in4 * scaleFract) >> 32; |
xorjoep | 1:24714b45cd1b | 112 | |
xorjoep | 1:24714b45cd1b | 113 | /* apply shifting */ |
xorjoep | 1:24714b45cd1b | 114 | out1 = in1 << totShift; |
xorjoep | 1:24714b45cd1b | 115 | out2 = in2 << totShift; |
xorjoep | 1:24714b45cd1b | 116 | |
xorjoep | 1:24714b45cd1b | 117 | /* saturate the results. */ |
xorjoep | 1:24714b45cd1b | 118 | if (in1 != (out1 >> totShift)) |
xorjoep | 1:24714b45cd1b | 119 | out1 = 0x7FFFFFFF ^ (in1 >> 31); |
xorjoep | 1:24714b45cd1b | 120 | |
xorjoep | 1:24714b45cd1b | 121 | if (in2 != (out2 >> totShift)) |
xorjoep | 1:24714b45cd1b | 122 | out2 = 0x7FFFFFFF ^ (in2 >> 31); |
xorjoep | 1:24714b45cd1b | 123 | |
xorjoep | 1:24714b45cd1b | 124 | out3 = in3 << totShift; |
xorjoep | 1:24714b45cd1b | 125 | out4 = in4 << totShift; |
xorjoep | 1:24714b45cd1b | 126 | |
xorjoep | 1:24714b45cd1b | 127 | *pOut = out1; |
xorjoep | 1:24714b45cd1b | 128 | *(pOut + 1) = out2; |
xorjoep | 1:24714b45cd1b | 129 | |
xorjoep | 1:24714b45cd1b | 130 | if (in3 != (out3 >> totShift)) |
xorjoep | 1:24714b45cd1b | 131 | out3 = 0x7FFFFFFF ^ (in3 >> 31); |
xorjoep | 1:24714b45cd1b | 132 | |
xorjoep | 1:24714b45cd1b | 133 | if (in4 != (out4 >> totShift)) |
xorjoep | 1:24714b45cd1b | 134 | out4 = 0x7FFFFFFF ^ (in4 >> 31); |
xorjoep | 1:24714b45cd1b | 135 | |
xorjoep | 1:24714b45cd1b | 136 | |
xorjoep | 1:24714b45cd1b | 137 | *(pOut + 2) = out3; |
xorjoep | 1:24714b45cd1b | 138 | *(pOut + 3) = out4; |
xorjoep | 1:24714b45cd1b | 139 | |
xorjoep | 1:24714b45cd1b | 140 | /* update pointers to process next sampels */ |
xorjoep | 1:24714b45cd1b | 141 | pIn += 4U; |
xorjoep | 1:24714b45cd1b | 142 | pOut += 4U; |
xorjoep | 1:24714b45cd1b | 143 | |
xorjoep | 1:24714b45cd1b | 144 | |
xorjoep | 1:24714b45cd1b | 145 | /* Decrement the numSamples loop counter */ |
xorjoep | 1:24714b45cd1b | 146 | blkCnt--; |
xorjoep | 1:24714b45cd1b | 147 | } |
xorjoep | 1:24714b45cd1b | 148 | |
xorjoep | 1:24714b45cd1b | 149 | /* If the numSamples is not a multiple of 4, compute any remaining output samples here. |
xorjoep | 1:24714b45cd1b | 150 | ** No loop unrolling is used. */ |
xorjoep | 1:24714b45cd1b | 151 | blkCnt = numSamples % 0x4U; |
xorjoep | 1:24714b45cd1b | 152 | |
xorjoep | 1:24714b45cd1b | 153 | #else |
xorjoep | 1:24714b45cd1b | 154 | |
xorjoep | 1:24714b45cd1b | 155 | /* Run the below code for Cortex-M0 */ |
xorjoep | 1:24714b45cd1b | 156 | |
xorjoep | 1:24714b45cd1b | 157 | /* Initialize blkCnt with number of samples */ |
xorjoep | 1:24714b45cd1b | 158 | blkCnt = numSamples; |
xorjoep | 1:24714b45cd1b | 159 | |
xorjoep | 1:24714b45cd1b | 160 | #endif /* #if defined (ARM_MATH_DSP) */ |
xorjoep | 1:24714b45cd1b | 161 | |
xorjoep | 1:24714b45cd1b | 162 | while (blkCnt > 0U) |
xorjoep | 1:24714b45cd1b | 163 | { |
xorjoep | 1:24714b45cd1b | 164 | /* C(m,n) = A(m,n) * k */ |
xorjoep | 1:24714b45cd1b | 165 | /* Scale, saturate and then store the results in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 166 | in1 = *pIn++; |
xorjoep | 1:24714b45cd1b | 167 | |
xorjoep | 1:24714b45cd1b | 168 | in2 = ((q63_t) in1 * scaleFract) >> 32; |
xorjoep | 1:24714b45cd1b | 169 | |
xorjoep | 1:24714b45cd1b | 170 | out1 = in2 << totShift; |
xorjoep | 1:24714b45cd1b | 171 | |
xorjoep | 1:24714b45cd1b | 172 | if (in2 != (out1 >> totShift)) |
xorjoep | 1:24714b45cd1b | 173 | out1 = 0x7FFFFFFF ^ (in2 >> 31); |
xorjoep | 1:24714b45cd1b | 174 | |
xorjoep | 1:24714b45cd1b | 175 | *pOut++ = out1; |
xorjoep | 1:24714b45cd1b | 176 | |
xorjoep | 1:24714b45cd1b | 177 | /* Decrement the numSamples loop counter */ |
xorjoep | 1:24714b45cd1b | 178 | blkCnt--; |
xorjoep | 1:24714b45cd1b | 179 | } |
xorjoep | 1:24714b45cd1b | 180 | |
xorjoep | 1:24714b45cd1b | 181 | /* Set status as ARM_MATH_SUCCESS */ |
xorjoep | 1:24714b45cd1b | 182 | status = ARM_MATH_SUCCESS; |
xorjoep | 1:24714b45cd1b | 183 | } |
xorjoep | 1:24714b45cd1b | 184 | |
xorjoep | 1:24714b45cd1b | 185 | /* Return to application */ |
xorjoep | 1:24714b45cd1b | 186 | return (status); |
xorjoep | 1:24714b45cd1b | 187 | } |
xorjoep | 1:24714b45cd1b | 188 | |
xorjoep | 1:24714b45cd1b | 189 | /** |
xorjoep | 1:24714b45cd1b | 190 | * @} end of MatrixScale group |
xorjoep | 1:24714b45cd1b | 191 | */ |