Rev 56 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 56 | mjames | 1 | /* ---------------------------------------------------------------------- |
| 2 | * Project: CMSIS DSP Library |
||
| 3 | * Title: arm_mat_scale_q15.c |
||
| 4 | * Description: Multiplies a Q15 matrix by a scalar |
||
| 5 | * |
||
| 6 | * $Date: 27. January 2017 |
||
| 7 | * $Revision: V.1.5.1 |
||
| 8 | * |
||
| 9 | * Target Processor: Cortex-M cores |
||
| 10 | * -------------------------------------------------------------------- */ |
||
| 11 | /* |
||
| 12 | * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved. |
||
| 13 | * |
||
| 14 | * SPDX-License-Identifier: Apache-2.0 |
||
| 15 | * |
||
| 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 |
||
| 19 | * |
||
| 20 | * www.apache.org/licenses/LICENSE-2.0 |
||
| 21 | * |
||
| 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. |
||
| 27 | */ |
||
| 28 | |||
| 29 | #include "arm_math.h" |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @ingroup groupMatrix |
||
| 33 | */ |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @addtogroup MatrixScale |
||
| 37 | * @{ |
||
| 38 | */ |
||
| 39 | |||
| 40 | /** |
||
| 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. |
||
| 48 | * |
||
| 49 | * @details |
||
| 50 | * <b>Scaling and Overflow Behavior:</b> |
||
| 51 | * \par |
||
| 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. |
||
| 54 | */ |
||
| 55 | |||
| 56 | arm_status arm_mat_scale_q15( |
||
| 57 | const arm_matrix_instance_q15 * pSrc, |
||
| 58 | q15_t scaleFract, |
||
| 59 | int32_t shift, |
||
| 60 | arm_matrix_instance_q15 * pDst) |
||
| 61 | { |
||
| 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 */ |
||
| 68 | |||
| 69 | #if defined (ARM_MATH_DSP) |
||
| 70 | |||
| 71 | q15_t in1, in2, in3, in4; |
||
| 72 | q31_t out1, out2, out3, out4; |
||
| 73 | q31_t inA1, inA2; |
||
| 74 | |||
| 75 | #endif // #if defined (ARM_MATH_DSP) |
||
| 76 | |||
| 77 | #ifdef ARM_MATH_MATRIX_CHECK |
||
| 78 | /* Check for matrix mismatch */ |
||
| 79 | if ((pSrc->numRows != pDst->numRows) || (pSrc->numCols != pDst->numCols)) |
||
| 80 | { |
||
| 81 | /* Set status as ARM_MATH_SIZE_MISMATCH */ |
||
| 82 | status = ARM_MATH_SIZE_MISMATCH; |
||
| 83 | } |
||
| 84 | else |
||
| 85 | #endif // #ifdef ARM_MATH_MATRIX_CHECK |
||
| 86 | { |
||
| 87 | /* Total number of samples in the input matrix */ |
||
| 88 | numSamples = (uint32_t) pSrc->numRows * pSrc->numCols; |
||
| 89 | |||
| 90 | #if defined (ARM_MATH_DSP) |
||
| 91 | |||
| 92 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
||
| 93 | /* Loop Unrolling */ |
||
| 94 | blkCnt = numSamples >> 2; |
||
| 95 | |||
| 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. */ |
||
| 98 | while (blkCnt > 0U) |
||
| 99 | { |
||
| 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); |
||
| 105 | |||
| 106 | /* C = A * scale */ |
||
| 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); |
||
| 113 | |||
| 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; |
||
| 120 | |||
| 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)); |
||
| 125 | |||
| 126 | _SIMD32_OFFSET(pOut) = __PKHBT(in2, in1, 16); |
||
| 127 | _SIMD32_OFFSET(pOut + 2) = __PKHBT(in4, in3, 16); |
||
| 128 | |||
| 129 | /* update pointers to process next sampels */ |
||
| 130 | pIn += 4U; |
||
| 131 | pOut += 4U; |
||
| 132 | |||
| 133 | |||
| 134 | /* Decrement the numSamples loop counter */ |
||
| 135 | blkCnt--; |
||
| 136 | } |
||
| 137 | |||
| 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; |
||
| 141 | |||
| 142 | #else |
||
| 143 | |||
| 144 | /* Run the below code for Cortex-M0 */ |
||
| 145 | |||
| 146 | /* Initialize blkCnt with number of samples */ |
||
| 147 | blkCnt = numSamples; |
||
| 148 | |||
| 149 | #endif /* #if defined (ARM_MATH_DSP) */ |
||
| 150 | |||
| 151 | while (blkCnt > 0U) |
||
| 152 | { |
||
| 153 | /* C(m,n) = A(m,n) * k */ |
||
| 154 | /* Scale, saturate and then store the results in the destination buffer. */ |
||
| 155 | *pOut++ = |
||
| 156 | (q15_t) (__SSAT(((q31_t) (*pIn++) * scaleFract) >> totShift, 16)); |
||
| 157 | |||
| 158 | /* Decrement the numSamples loop counter */ |
||
| 159 | blkCnt--; |
||
| 160 | } |
||
| 161 | /* Set status as ARM_MATH_SUCCESS */ |
||
| 162 | status = ARM_MATH_SUCCESS; |
||
| 163 | } |
||
| 164 | |||
| 165 | /* Return to application */ |
||
| 166 | return (status); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @} end of MatrixScale group |
||
| 171 | */ |