The CMSIS DSP 5 library

Dependents:   Nucleo-Heart-Rate ejercicioVrms2 PROYECTOFINAL ejercicioVrms ... more

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?

UserRevisionLine numberNew contents of line
xorjoep 1:24714b45cd1b 1 /* ----------------------------------------------------------------------
xorjoep 1:24714b45cd1b 2 * Project: CMSIS DSP Library
xorjoep 1:24714b45cd1b 3 * Title: arm_shift_q7.c
xorjoep 1:24714b45cd1b 4 * Description: Processing function for the Q7 Shifting
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 shift
xorjoep 1:24714b45cd1b 37 * @{
xorjoep 1:24714b45cd1b 38 */
xorjoep 1:24714b45cd1b 39
xorjoep 1:24714b45cd1b 40
xorjoep 1:24714b45cd1b 41 /**
xorjoep 1:24714b45cd1b 42 * @brief Shifts the elements of a Q7 vector a specified number of bits.
xorjoep 1:24714b45cd1b 43 * @param[in] *pSrc points to the input vector
xorjoep 1:24714b45cd1b 44 * @param[in] shiftBits number of bits to shift. A positive value shifts left; a negative value shifts right.
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 * \par Conditions for optimum performance
xorjoep 1:24714b45cd1b 50 * Input and output buffers should be aligned by 32-bit
xorjoep 1:24714b45cd1b 51 *
xorjoep 1:24714b45cd1b 52 *
xorjoep 1:24714b45cd1b 53 * <b>Scaling and Overflow Behavior:</b>
xorjoep 1:24714b45cd1b 54 * \par
xorjoep 1:24714b45cd1b 55 * The function uses saturating arithmetic.
xorjoep 1:24714b45cd1b 56 * Results outside of the allowable Q7 range [0x8 0x7F] will be saturated.
xorjoep 1:24714b45cd1b 57 */
xorjoep 1:24714b45cd1b 58
xorjoep 1:24714b45cd1b 59 void arm_shift_q7(
xorjoep 1:24714b45cd1b 60 q7_t * pSrc,
xorjoep 1:24714b45cd1b 61 int8_t shiftBits,
xorjoep 1:24714b45cd1b 62 q7_t * pDst,
xorjoep 1:24714b45cd1b 63 uint32_t blockSize)
xorjoep 1:24714b45cd1b 64 {
xorjoep 1:24714b45cd1b 65 uint32_t blkCnt; /* loop counter */
xorjoep 1:24714b45cd1b 66 uint8_t sign; /* Sign of shiftBits */
xorjoep 1:24714b45cd1b 67
xorjoep 1:24714b45cd1b 68 #if defined (ARM_MATH_DSP)
xorjoep 1:24714b45cd1b 69
xorjoep 1:24714b45cd1b 70 /* Run the below code for Cortex-M4 and Cortex-M3 */
xorjoep 1:24714b45cd1b 71 q7_t in1; /* Input value1 */
xorjoep 1:24714b45cd1b 72 q7_t in2; /* Input value2 */
xorjoep 1:24714b45cd1b 73 q7_t in3; /* Input value3 */
xorjoep 1:24714b45cd1b 74 q7_t in4; /* Input value4 */
xorjoep 1:24714b45cd1b 75
xorjoep 1:24714b45cd1b 76
xorjoep 1:24714b45cd1b 77 /*loop Unrolling */
xorjoep 1:24714b45cd1b 78 blkCnt = blockSize >> 2U;
xorjoep 1:24714b45cd1b 79
xorjoep 1:24714b45cd1b 80 /* Getting the sign of shiftBits */
xorjoep 1:24714b45cd1b 81 sign = (shiftBits & 0x80);
xorjoep 1:24714b45cd1b 82
xorjoep 1:24714b45cd1b 83 /* If the shift value is positive then do right shift else left shift */
xorjoep 1:24714b45cd1b 84 if (sign == 0U)
xorjoep 1:24714b45cd1b 85 {
xorjoep 1:24714b45cd1b 86 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
xorjoep 1:24714b45cd1b 87 ** a second loop below computes the remaining 1 to 3 samples. */
xorjoep 1:24714b45cd1b 88 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 89 {
xorjoep 1:24714b45cd1b 90 /* C = A << shiftBits */
xorjoep 1:24714b45cd1b 91 /* Read 4 inputs */
xorjoep 1:24714b45cd1b 92 in1 = *pSrc;
xorjoep 1:24714b45cd1b 93 in2 = *(pSrc + 1);
xorjoep 1:24714b45cd1b 94 in3 = *(pSrc + 2);
xorjoep 1:24714b45cd1b 95 in4 = *(pSrc + 3);
xorjoep 1:24714b45cd1b 96
xorjoep 1:24714b45cd1b 97 /* Store the Shifted result in the destination buffer in single cycle by packing the outputs */
xorjoep 1:24714b45cd1b 98 *__SIMD32(pDst)++ = __PACKq7(__SSAT((in1 << shiftBits), 8),
xorjoep 1:24714b45cd1b 99 __SSAT((in2 << shiftBits), 8),
xorjoep 1:24714b45cd1b 100 __SSAT((in3 << shiftBits), 8),
xorjoep 1:24714b45cd1b 101 __SSAT((in4 << shiftBits), 8));
xorjoep 1:24714b45cd1b 102 /* Update source pointer to process next sampels */
xorjoep 1:24714b45cd1b 103 pSrc += 4U;
xorjoep 1:24714b45cd1b 104
xorjoep 1:24714b45cd1b 105 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 106 blkCnt--;
xorjoep 1:24714b45cd1b 107 }
xorjoep 1:24714b45cd1b 108
xorjoep 1:24714b45cd1b 109 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
xorjoep 1:24714b45cd1b 110 ** No loop unrolling is used. */
xorjoep 1:24714b45cd1b 111 blkCnt = blockSize % 0x4U;
xorjoep 1:24714b45cd1b 112
xorjoep 1:24714b45cd1b 113 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 114 {
xorjoep 1:24714b45cd1b 115 /* C = A << shiftBits */
xorjoep 1:24714b45cd1b 116 /* Shift the input and then store the result in the destination buffer. */
xorjoep 1:24714b45cd1b 117 *pDst++ = (q7_t) __SSAT((*pSrc++ << shiftBits), 8);
xorjoep 1:24714b45cd1b 118
xorjoep 1:24714b45cd1b 119 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 120 blkCnt--;
xorjoep 1:24714b45cd1b 121 }
xorjoep 1:24714b45cd1b 122 }
xorjoep 1:24714b45cd1b 123 else
xorjoep 1:24714b45cd1b 124 {
xorjoep 1:24714b45cd1b 125 shiftBits = -shiftBits;
xorjoep 1:24714b45cd1b 126 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
xorjoep 1:24714b45cd1b 127 ** a second loop below computes the remaining 1 to 3 samples. */
xorjoep 1:24714b45cd1b 128 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 129 {
xorjoep 1:24714b45cd1b 130 /* C = A >> shiftBits */
xorjoep 1:24714b45cd1b 131 /* Read 4 inputs */
xorjoep 1:24714b45cd1b 132 in1 = *pSrc;
xorjoep 1:24714b45cd1b 133 in2 = *(pSrc + 1);
xorjoep 1:24714b45cd1b 134 in3 = *(pSrc + 2);
xorjoep 1:24714b45cd1b 135 in4 = *(pSrc + 3);
xorjoep 1:24714b45cd1b 136
xorjoep 1:24714b45cd1b 137 /* Store the Shifted result in the destination buffer in single cycle by packing the outputs */
xorjoep 1:24714b45cd1b 138 *__SIMD32(pDst)++ = __PACKq7((in1 >> shiftBits), (in2 >> shiftBits),
xorjoep 1:24714b45cd1b 139 (in3 >> shiftBits), (in4 >> shiftBits));
xorjoep 1:24714b45cd1b 140
xorjoep 1:24714b45cd1b 141
xorjoep 1:24714b45cd1b 142 pSrc += 4U;
xorjoep 1:24714b45cd1b 143
xorjoep 1:24714b45cd1b 144 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 145 blkCnt--;
xorjoep 1:24714b45cd1b 146 }
xorjoep 1:24714b45cd1b 147
xorjoep 1:24714b45cd1b 148 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
xorjoep 1:24714b45cd1b 149 ** No loop unrolling is used. */
xorjoep 1:24714b45cd1b 150 blkCnt = blockSize % 0x4U;
xorjoep 1:24714b45cd1b 151
xorjoep 1:24714b45cd1b 152 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 153 {
xorjoep 1:24714b45cd1b 154 /* C = A >> shiftBits */
xorjoep 1:24714b45cd1b 155 /* Shift the input and then store the result in the destination buffer. */
xorjoep 1:24714b45cd1b 156 in1 = *pSrc++;
xorjoep 1:24714b45cd1b 157 *pDst++ = (in1 >> shiftBits);
xorjoep 1:24714b45cd1b 158
xorjoep 1:24714b45cd1b 159 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 160 blkCnt--;
xorjoep 1:24714b45cd1b 161 }
xorjoep 1:24714b45cd1b 162 }
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 /* Getting the sign of shiftBits */
xorjoep 1:24714b45cd1b 169 sign = (shiftBits & 0x80);
xorjoep 1:24714b45cd1b 170
xorjoep 1:24714b45cd1b 171 /* If the shift value is positive then do right shift else left shift */
xorjoep 1:24714b45cd1b 172 if (sign == 0U)
xorjoep 1:24714b45cd1b 173 {
xorjoep 1:24714b45cd1b 174 /* Initialize blkCnt with number of samples */
xorjoep 1:24714b45cd1b 175 blkCnt = blockSize;
xorjoep 1:24714b45cd1b 176
xorjoep 1:24714b45cd1b 177 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 178 {
xorjoep 1:24714b45cd1b 179 /* C = A << shiftBits */
xorjoep 1:24714b45cd1b 180 /* Shift the input and then store the result in the destination buffer. */
xorjoep 1:24714b45cd1b 181 *pDst++ = (q7_t) __SSAT(((q15_t) * pSrc++ << shiftBits), 8);
xorjoep 1:24714b45cd1b 182
xorjoep 1:24714b45cd1b 183 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 184 blkCnt--;
xorjoep 1:24714b45cd1b 185 }
xorjoep 1:24714b45cd1b 186 }
xorjoep 1:24714b45cd1b 187 else
xorjoep 1:24714b45cd1b 188 {
xorjoep 1:24714b45cd1b 189 /* Initialize blkCnt with number of samples */
xorjoep 1:24714b45cd1b 190 blkCnt = blockSize;
xorjoep 1:24714b45cd1b 191
xorjoep 1:24714b45cd1b 192 while (blkCnt > 0U)
xorjoep 1:24714b45cd1b 193 {
xorjoep 1:24714b45cd1b 194 /* C = A >> shiftBits */
xorjoep 1:24714b45cd1b 195 /* Shift the input and then store the result in the destination buffer. */
xorjoep 1:24714b45cd1b 196 *pDst++ = (*pSrc++ >> -shiftBits);
xorjoep 1:24714b45cd1b 197
xorjoep 1:24714b45cd1b 198 /* Decrement the loop counter */
xorjoep 1:24714b45cd1b 199 blkCnt--;
xorjoep 1:24714b45cd1b 200 }
xorjoep 1:24714b45cd1b 201 }
xorjoep 1:24714b45cd1b 202
xorjoep 1:24714b45cd1b 203 #endif /* #if defined (ARM_MATH_DSP) */
xorjoep 1:24714b45cd1b 204 }
xorjoep 1:24714b45cd1b 205
xorjoep 1:24714b45cd1b 206 /**
xorjoep 1:24714b45cd1b 207 * @} end of shift group
xorjoep 1:24714b45cd1b 208 */