The CMSIS DSP 5 library
Dependents: Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more
functions/BasicMathFunctions/arm_scale_q15.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_scale_q15.c |
xorjoep | 1:24714b45cd1b | 4 | * Description: Multiplies a Q15 vector 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 groupMath |
xorjoep | 1:24714b45cd1b | 33 | */ |
xorjoep | 1:24714b45cd1b | 34 | |
xorjoep | 1:24714b45cd1b | 35 | /** |
xorjoep | 1:24714b45cd1b | 36 | * @addtogroup scale |
xorjoep | 1:24714b45cd1b | 37 | * @{ |
xorjoep | 1:24714b45cd1b | 38 | */ |
xorjoep | 1:24714b45cd1b | 39 | |
xorjoep | 1:24714b45cd1b | 40 | /** |
xorjoep | 1:24714b45cd1b | 41 | * @brief Multiplies a Q15 vector by a scalar. |
xorjoep | 1:24714b45cd1b | 42 | * @param[in] *pSrc points to the input vector |
xorjoep | 1:24714b45cd1b | 43 | * @param[in] scaleFract fractional portion of the scale value |
xorjoep | 1:24714b45cd1b | 44 | * @param[in] shift number of bits to shift the result by |
xorjoep | 1:24714b45cd1b | 45 | * @param[out] *pDst points to the output vector |
xorjoep | 1:24714b45cd1b | 46 | * @param[in] blockSize number of samples in the vector |
xorjoep | 1:24714b45cd1b | 47 | * @return none. |
xorjoep | 1:24714b45cd1b | 48 | * |
xorjoep | 1:24714b45cd1b | 49 | * <b>Scaling and Overflow Behavior:</b> |
xorjoep | 1:24714b45cd1b | 50 | * \par |
xorjoep | 1:24714b45cd1b | 51 | * The input data <code>*pSrc</code> and <code>scaleFract</code> are in 1.15 format. |
xorjoep | 1:24714b45cd1b | 52 | * These are multiplied to yield a 2.30 intermediate result and this is shifted with saturation to 1.15 format. |
xorjoep | 1:24714b45cd1b | 53 | */ |
xorjoep | 1:24714b45cd1b | 54 | |
xorjoep | 1:24714b45cd1b | 55 | |
xorjoep | 1:24714b45cd1b | 56 | void arm_scale_q15( |
xorjoep | 1:24714b45cd1b | 57 | q15_t * pSrc, |
xorjoep | 1:24714b45cd1b | 58 | q15_t scaleFract, |
xorjoep | 1:24714b45cd1b | 59 | int8_t shift, |
xorjoep | 1:24714b45cd1b | 60 | q15_t * pDst, |
xorjoep | 1:24714b45cd1b | 61 | uint32_t blockSize) |
xorjoep | 1:24714b45cd1b | 62 | { |
xorjoep | 1:24714b45cd1b | 63 | int8_t kShift = 15 - shift; /* shift to apply after scaling */ |
xorjoep | 1:24714b45cd1b | 64 | uint32_t blkCnt; /* loop counter */ |
xorjoep | 1:24714b45cd1b | 65 | |
xorjoep | 1:24714b45cd1b | 66 | #if defined (ARM_MATH_DSP) |
xorjoep | 1:24714b45cd1b | 67 | |
xorjoep | 1:24714b45cd1b | 68 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
xorjoep | 1:24714b45cd1b | 69 | q15_t in1, in2, in3, in4; |
xorjoep | 1:24714b45cd1b | 70 | q31_t inA1, inA2; /* Temporary variables */ |
xorjoep | 1:24714b45cd1b | 71 | q31_t out1, out2, out3, out4; |
xorjoep | 1:24714b45cd1b | 72 | |
xorjoep | 1:24714b45cd1b | 73 | |
xorjoep | 1:24714b45cd1b | 74 | /*loop Unrolling */ |
xorjoep | 1:24714b45cd1b | 75 | blkCnt = blockSize >> 2U; |
xorjoep | 1:24714b45cd1b | 76 | |
xorjoep | 1:24714b45cd1b | 77 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
xorjoep | 1:24714b45cd1b | 78 | ** a second loop below computes the remaining 1 to 3 samples. */ |
xorjoep | 1:24714b45cd1b | 79 | while (blkCnt > 0U) |
xorjoep | 1:24714b45cd1b | 80 | { |
xorjoep | 1:24714b45cd1b | 81 | /* Reading 2 inputs from memory */ |
xorjoep | 1:24714b45cd1b | 82 | inA1 = *__SIMD32(pSrc)++; |
xorjoep | 1:24714b45cd1b | 83 | inA2 = *__SIMD32(pSrc)++; |
xorjoep | 1:24714b45cd1b | 84 | |
xorjoep | 1:24714b45cd1b | 85 | /* C = A * scale */ |
xorjoep | 1:24714b45cd1b | 86 | /* Scale the inputs and then store the 2 results in the destination buffer |
xorjoep | 1:24714b45cd1b | 87 | * in single cycle by packing the outputs */ |
xorjoep | 1:24714b45cd1b | 88 | out1 = (q31_t) ((q15_t) (inA1 >> 16) * scaleFract); |
xorjoep | 1:24714b45cd1b | 89 | out2 = (q31_t) ((q15_t) inA1 * scaleFract); |
xorjoep | 1:24714b45cd1b | 90 | out3 = (q31_t) ((q15_t) (inA2 >> 16) * scaleFract); |
xorjoep | 1:24714b45cd1b | 91 | out4 = (q31_t) ((q15_t) inA2 * scaleFract); |
xorjoep | 1:24714b45cd1b | 92 | |
xorjoep | 1:24714b45cd1b | 93 | /* apply shifting */ |
xorjoep | 1:24714b45cd1b | 94 | out1 = out1 >> kShift; |
xorjoep | 1:24714b45cd1b | 95 | out2 = out2 >> kShift; |
xorjoep | 1:24714b45cd1b | 96 | out3 = out3 >> kShift; |
xorjoep | 1:24714b45cd1b | 97 | out4 = out4 >> kShift; |
xorjoep | 1:24714b45cd1b | 98 | |
xorjoep | 1:24714b45cd1b | 99 | /* saturate the output */ |
xorjoep | 1:24714b45cd1b | 100 | in1 = (q15_t) (__SSAT(out1, 16)); |
xorjoep | 1:24714b45cd1b | 101 | in2 = (q15_t) (__SSAT(out2, 16)); |
xorjoep | 1:24714b45cd1b | 102 | in3 = (q15_t) (__SSAT(out3, 16)); |
xorjoep | 1:24714b45cd1b | 103 | in4 = (q15_t) (__SSAT(out4, 16)); |
xorjoep | 1:24714b45cd1b | 104 | |
xorjoep | 1:24714b45cd1b | 105 | /* store the result to destination */ |
xorjoep | 1:24714b45cd1b | 106 | *__SIMD32(pDst)++ = __PKHBT(in2, in1, 16); |
xorjoep | 1:24714b45cd1b | 107 | *__SIMD32(pDst)++ = __PKHBT(in4, in3, 16); |
xorjoep | 1:24714b45cd1b | 108 | |
xorjoep | 1:24714b45cd1b | 109 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 110 | blkCnt--; |
xorjoep | 1:24714b45cd1b | 111 | } |
xorjoep | 1:24714b45cd1b | 112 | |
xorjoep | 1:24714b45cd1b | 113 | /* If the blockSize is not a multiple of 4, compute any remaining output samples here. |
xorjoep | 1:24714b45cd1b | 114 | ** No loop unrolling is used. */ |
xorjoep | 1:24714b45cd1b | 115 | blkCnt = blockSize % 0x4U; |
xorjoep | 1:24714b45cd1b | 116 | |
xorjoep | 1:24714b45cd1b | 117 | while (blkCnt > 0U) |
xorjoep | 1:24714b45cd1b | 118 | { |
xorjoep | 1:24714b45cd1b | 119 | /* C = A * scale */ |
xorjoep | 1:24714b45cd1b | 120 | /* Scale the input and then store the result in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 121 | *pDst++ = (q15_t) (__SSAT(((*pSrc++) * scaleFract) >> kShift, 16)); |
xorjoep | 1:24714b45cd1b | 122 | |
xorjoep | 1:24714b45cd1b | 123 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 124 | blkCnt--; |
xorjoep | 1:24714b45cd1b | 125 | } |
xorjoep | 1:24714b45cd1b | 126 | |
xorjoep | 1:24714b45cd1b | 127 | #else |
xorjoep | 1:24714b45cd1b | 128 | |
xorjoep | 1:24714b45cd1b | 129 | /* Run the below code for Cortex-M0 */ |
xorjoep | 1:24714b45cd1b | 130 | |
xorjoep | 1:24714b45cd1b | 131 | /* Initialize blkCnt with number of samples */ |
xorjoep | 1:24714b45cd1b | 132 | blkCnt = blockSize; |
xorjoep | 1:24714b45cd1b | 133 | |
xorjoep | 1:24714b45cd1b | 134 | while (blkCnt > 0U) |
xorjoep | 1:24714b45cd1b | 135 | { |
xorjoep | 1:24714b45cd1b | 136 | /* C = A * scale */ |
xorjoep | 1:24714b45cd1b | 137 | /* Scale the input and then store the result in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 138 | *pDst++ = (q15_t) (__SSAT(((q31_t) * pSrc++ * scaleFract) >> kShift, 16)); |
xorjoep | 1:24714b45cd1b | 139 | |
xorjoep | 1:24714b45cd1b | 140 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 141 | blkCnt--; |
xorjoep | 1:24714b45cd1b | 142 | } |
xorjoep | 1:24714b45cd1b | 143 | |
xorjoep | 1:24714b45cd1b | 144 | #endif /* #if defined (ARM_MATH_DSP) */ |
xorjoep | 1:24714b45cd1b | 145 | |
xorjoep | 1:24714b45cd1b | 146 | } |
xorjoep | 1:24714b45cd1b | 147 | |
xorjoep | 1:24714b45cd1b | 148 | /** |
xorjoep | 1:24714b45cd1b | 149 | * @} end of scale group |
xorjoep | 1:24714b45cd1b | 150 | */ |