The CMSIS DSP 5 library
Dependents: Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more
functions/BasicMathFunctions/arm_shift_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_shift_q31.c |
xorjoep | 1:24714b45cd1b | 4 | * Description: Shifts the elements of a Q31 vector by a specified number of bits |
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 | * @defgroup shift Vector Shift |
xorjoep | 1:24714b45cd1b | 36 | * |
xorjoep | 1:24714b45cd1b | 37 | * Shifts the elements of a fixed-point vector by a specified number of bits. |
xorjoep | 1:24714b45cd1b | 38 | * There are separate functions for Q7, Q15, and Q31 data types. |
xorjoep | 1:24714b45cd1b | 39 | * The underlying algorithm used is: |
xorjoep | 1:24714b45cd1b | 40 | * |
xorjoep | 1:24714b45cd1b | 41 | * <pre> |
xorjoep | 1:24714b45cd1b | 42 | * pDst[n] = pSrc[n] << shift, 0 <= n < blockSize. |
xorjoep | 1:24714b45cd1b | 43 | * </pre> |
xorjoep | 1:24714b45cd1b | 44 | * |
xorjoep | 1:24714b45cd1b | 45 | * If <code>shift</code> is positive then the elements of the vector are shifted to the left. |
xorjoep | 1:24714b45cd1b | 46 | * If <code>shift</code> is negative then the elements of the vector are shifted to the right. |
xorjoep | 1:24714b45cd1b | 47 | * |
xorjoep | 1:24714b45cd1b | 48 | * The functions support in-place computation allowing the source and destination |
xorjoep | 1:24714b45cd1b | 49 | * pointers to reference the same memory buffer. |
xorjoep | 1:24714b45cd1b | 50 | */ |
xorjoep | 1:24714b45cd1b | 51 | |
xorjoep | 1:24714b45cd1b | 52 | /** |
xorjoep | 1:24714b45cd1b | 53 | * @addtogroup shift |
xorjoep | 1:24714b45cd1b | 54 | * @{ |
xorjoep | 1:24714b45cd1b | 55 | */ |
xorjoep | 1:24714b45cd1b | 56 | |
xorjoep | 1:24714b45cd1b | 57 | /** |
xorjoep | 1:24714b45cd1b | 58 | * @brief Shifts the elements of a Q31 vector a specified number of bits. |
xorjoep | 1:24714b45cd1b | 59 | * @param[in] *pSrc points to the input vector |
xorjoep | 1:24714b45cd1b | 60 | * @param[in] shiftBits number of bits to shift. A positive value shifts left; a negative value shifts right. |
xorjoep | 1:24714b45cd1b | 61 | * @param[out] *pDst points to the output vector |
xorjoep | 1:24714b45cd1b | 62 | * @param[in] blockSize number of samples in the vector |
xorjoep | 1:24714b45cd1b | 63 | * @return none. |
xorjoep | 1:24714b45cd1b | 64 | * |
xorjoep | 1:24714b45cd1b | 65 | * |
xorjoep | 1:24714b45cd1b | 66 | * <b>Scaling and Overflow Behavior:</b> |
xorjoep | 1:24714b45cd1b | 67 | * \par |
xorjoep | 1:24714b45cd1b | 68 | * The function uses saturating arithmetic. |
xorjoep | 1:24714b45cd1b | 69 | * Results outside of the allowable Q31 range [0x80000000 0x7FFFFFFF] will be saturated. |
xorjoep | 1:24714b45cd1b | 70 | */ |
xorjoep | 1:24714b45cd1b | 71 | |
xorjoep | 1:24714b45cd1b | 72 | void arm_shift_q31( |
xorjoep | 1:24714b45cd1b | 73 | q31_t * pSrc, |
xorjoep | 1:24714b45cd1b | 74 | int8_t shiftBits, |
xorjoep | 1:24714b45cd1b | 75 | q31_t * pDst, |
xorjoep | 1:24714b45cd1b | 76 | uint32_t blockSize) |
xorjoep | 1:24714b45cd1b | 77 | { |
xorjoep | 1:24714b45cd1b | 78 | uint32_t blkCnt; /* loop counter */ |
xorjoep | 1:24714b45cd1b | 79 | uint8_t sign = (shiftBits & 0x80); /* Sign of shiftBits */ |
xorjoep | 1:24714b45cd1b | 80 | |
xorjoep | 1:24714b45cd1b | 81 | #if defined (ARM_MATH_DSP) |
xorjoep | 1:24714b45cd1b | 82 | |
xorjoep | 1:24714b45cd1b | 83 | q31_t in1, in2, in3, in4; /* Temporary input variables */ |
xorjoep | 1:24714b45cd1b | 84 | q31_t out1, out2, out3, out4; /* Temporary output variables */ |
xorjoep | 1:24714b45cd1b | 85 | |
xorjoep | 1:24714b45cd1b | 86 | /*loop Unrolling */ |
xorjoep | 1:24714b45cd1b | 87 | blkCnt = blockSize >> 2U; |
xorjoep | 1:24714b45cd1b | 88 | |
xorjoep | 1:24714b45cd1b | 89 | |
xorjoep | 1:24714b45cd1b | 90 | if (sign == 0U) |
xorjoep | 1:24714b45cd1b | 91 | { |
xorjoep | 1:24714b45cd1b | 92 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
xorjoep | 1:24714b45cd1b | 93 | ** a second loop below computes the remaining 1 to 3 samples. */ |
xorjoep | 1:24714b45cd1b | 94 | while (blkCnt > 0U) |
xorjoep | 1:24714b45cd1b | 95 | { |
xorjoep | 1:24714b45cd1b | 96 | /* C = A << shiftBits */ |
xorjoep | 1:24714b45cd1b | 97 | /* Shift the input and then store the results in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 98 | in1 = *pSrc; |
xorjoep | 1:24714b45cd1b | 99 | in2 = *(pSrc + 1); |
xorjoep | 1:24714b45cd1b | 100 | out1 = in1 << shiftBits; |
xorjoep | 1:24714b45cd1b | 101 | in3 = *(pSrc + 2); |
xorjoep | 1:24714b45cd1b | 102 | out2 = in2 << shiftBits; |
xorjoep | 1:24714b45cd1b | 103 | in4 = *(pSrc + 3); |
xorjoep | 1:24714b45cd1b | 104 | if (in1 != (out1 >> shiftBits)) |
xorjoep | 1:24714b45cd1b | 105 | out1 = 0x7FFFFFFF ^ (in1 >> 31); |
xorjoep | 1:24714b45cd1b | 106 | |
xorjoep | 1:24714b45cd1b | 107 | if (in2 != (out2 >> shiftBits)) |
xorjoep | 1:24714b45cd1b | 108 | out2 = 0x7FFFFFFF ^ (in2 >> 31); |
xorjoep | 1:24714b45cd1b | 109 | |
xorjoep | 1:24714b45cd1b | 110 | *pDst = out1; |
xorjoep | 1:24714b45cd1b | 111 | out3 = in3 << shiftBits; |
xorjoep | 1:24714b45cd1b | 112 | *(pDst + 1) = out2; |
xorjoep | 1:24714b45cd1b | 113 | out4 = in4 << shiftBits; |
xorjoep | 1:24714b45cd1b | 114 | |
xorjoep | 1:24714b45cd1b | 115 | if (in3 != (out3 >> shiftBits)) |
xorjoep | 1:24714b45cd1b | 116 | out3 = 0x7FFFFFFF ^ (in3 >> 31); |
xorjoep | 1:24714b45cd1b | 117 | |
xorjoep | 1:24714b45cd1b | 118 | if (in4 != (out4 >> shiftBits)) |
xorjoep | 1:24714b45cd1b | 119 | out4 = 0x7FFFFFFF ^ (in4 >> 31); |
xorjoep | 1:24714b45cd1b | 120 | |
xorjoep | 1:24714b45cd1b | 121 | *(pDst + 2) = out3; |
xorjoep | 1:24714b45cd1b | 122 | *(pDst + 3) = out4; |
xorjoep | 1:24714b45cd1b | 123 | |
xorjoep | 1:24714b45cd1b | 124 | /* Update destination pointer to process next sampels */ |
xorjoep | 1:24714b45cd1b | 125 | pSrc += 4U; |
xorjoep | 1:24714b45cd1b | 126 | pDst += 4U; |
xorjoep | 1:24714b45cd1b | 127 | |
xorjoep | 1:24714b45cd1b | 128 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 129 | blkCnt--; |
xorjoep | 1:24714b45cd1b | 130 | } |
xorjoep | 1:24714b45cd1b | 131 | } |
xorjoep | 1:24714b45cd1b | 132 | else |
xorjoep | 1:24714b45cd1b | 133 | { |
xorjoep | 1:24714b45cd1b | 134 | |
xorjoep | 1:24714b45cd1b | 135 | /* First part of the processing with loop unrolling. Compute 4 outputs at a time. |
xorjoep | 1:24714b45cd1b | 136 | ** a second loop below computes the remaining 1 to 3 samples. */ |
xorjoep | 1:24714b45cd1b | 137 | while (blkCnt > 0U) |
xorjoep | 1:24714b45cd1b | 138 | { |
xorjoep | 1:24714b45cd1b | 139 | /* C = A >> shiftBits */ |
xorjoep | 1:24714b45cd1b | 140 | /* Shift the input and then store the results in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 141 | in1 = *pSrc; |
xorjoep | 1:24714b45cd1b | 142 | in2 = *(pSrc + 1); |
xorjoep | 1:24714b45cd1b | 143 | in3 = *(pSrc + 2); |
xorjoep | 1:24714b45cd1b | 144 | in4 = *(pSrc + 3); |
xorjoep | 1:24714b45cd1b | 145 | |
xorjoep | 1:24714b45cd1b | 146 | *pDst = (in1 >> -shiftBits); |
xorjoep | 1:24714b45cd1b | 147 | *(pDst + 1) = (in2 >> -shiftBits); |
xorjoep | 1:24714b45cd1b | 148 | *(pDst + 2) = (in3 >> -shiftBits); |
xorjoep | 1:24714b45cd1b | 149 | *(pDst + 3) = (in4 >> -shiftBits); |
xorjoep | 1:24714b45cd1b | 150 | |
xorjoep | 1:24714b45cd1b | 151 | |
xorjoep | 1:24714b45cd1b | 152 | pSrc += 4U; |
xorjoep | 1:24714b45cd1b | 153 | pDst += 4U; |
xorjoep | 1:24714b45cd1b | 154 | |
xorjoep | 1:24714b45cd1b | 155 | blkCnt--; |
xorjoep | 1:24714b45cd1b | 156 | } |
xorjoep | 1:24714b45cd1b | 157 | |
xorjoep | 1:24714b45cd1b | 158 | } |
xorjoep | 1:24714b45cd1b | 159 | |
xorjoep | 1:24714b45cd1b | 160 | /* If the blockSize is not a multiple of 4, compute any remaining output samples here. |
xorjoep | 1:24714b45cd1b | 161 | ** No loop unrolling is used. */ |
xorjoep | 1:24714b45cd1b | 162 | blkCnt = blockSize % 0x4U; |
xorjoep | 1:24714b45cd1b | 163 | |
xorjoep | 1:24714b45cd1b | 164 | #else |
xorjoep | 1:24714b45cd1b | 165 | |
xorjoep | 1:24714b45cd1b | 166 | /* Run the below code for Cortex-M0 */ |
xorjoep | 1:24714b45cd1b | 167 | |
xorjoep | 1:24714b45cd1b | 168 | |
xorjoep | 1:24714b45cd1b | 169 | /* Initialize blkCnt with number of samples */ |
xorjoep | 1:24714b45cd1b | 170 | blkCnt = blockSize; |
xorjoep | 1:24714b45cd1b | 171 | |
xorjoep | 1:24714b45cd1b | 172 | #endif /* #if defined (ARM_MATH_DSP) */ |
xorjoep | 1:24714b45cd1b | 173 | |
xorjoep | 1:24714b45cd1b | 174 | |
xorjoep | 1:24714b45cd1b | 175 | while (blkCnt > 0U) |
xorjoep | 1:24714b45cd1b | 176 | { |
xorjoep | 1:24714b45cd1b | 177 | /* C = A (>> or <<) shiftBits */ |
xorjoep | 1:24714b45cd1b | 178 | /* Shift the input and then store the result in the destination buffer. */ |
xorjoep | 1:24714b45cd1b | 179 | *pDst++ = (sign == 0U) ? clip_q63_to_q31((q63_t) * pSrc++ << shiftBits) : |
xorjoep | 1:24714b45cd1b | 180 | (*pSrc++ >> -shiftBits); |
xorjoep | 1:24714b45cd1b | 181 | |
xorjoep | 1:24714b45cd1b | 182 | /* Decrement the loop counter */ |
xorjoep | 1:24714b45cd1b | 183 | blkCnt--; |
xorjoep | 1:24714b45cd1b | 184 | } |
xorjoep | 1:24714b45cd1b | 185 | |
xorjoep | 1:24714b45cd1b | 186 | |
xorjoep | 1:24714b45cd1b | 187 | } |
xorjoep | 1:24714b45cd1b | 188 | |
xorjoep | 1:24714b45cd1b | 189 | /** |
xorjoep | 1:24714b45cd1b | 190 | * @} end of shift group |
xorjoep | 1:24714b45cd1b | 191 | */ |