V4.0.1 of the ARM CMSIS DSP libraries. Note that arm_bitreversal2.s, arm_cfft_f32.c and arm_rfft_fast_f32.c had to be removed. arm_bitreversal2.s will not assemble with the online tools. So, the fast f32 FFT functions are not yet available. All the other FFT functions are available.
Dependents: MPU9150_Example fir_f32 fir_f32 MPU9150_nucleo_noni2cdev ... more
FilteringFunctions/arm_correlate_f32.c@0:3d9c67d97d6f, 2014-07-28 (annotated)
- Committer:
- emh203
- Date:
- Mon Jul 28 15:03:15 2014 +0000
- Revision:
- 0:3d9c67d97d6f
1st working commit. Had to remove arm_bitreversal2.s arm_cfft_f32.c and arm_rfft_fast_f32.c. The .s will not assemble. For now I removed these functions so we could at least have a library for the other functions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emh203 | 0:3d9c67d97d6f | 1 | /* ---------------------------------------------------------------------------- |
emh203 | 0:3d9c67d97d6f | 2 | * Copyright (C) 2010-2014 ARM Limited. All rights reserved. |
emh203 | 0:3d9c67d97d6f | 3 | * |
emh203 | 0:3d9c67d97d6f | 4 | * $Date: 12. March 2014 |
emh203 | 0:3d9c67d97d6f | 5 | * $Revision: V1.4.3 |
emh203 | 0:3d9c67d97d6f | 6 | * |
emh203 | 0:3d9c67d97d6f | 7 | * Project: CMSIS DSP Library |
emh203 | 0:3d9c67d97d6f | 8 | * Title: arm_correlate_f32.c |
emh203 | 0:3d9c67d97d6f | 9 | * |
emh203 | 0:3d9c67d97d6f | 10 | * Description: Correlation of floating-point sequences. |
emh203 | 0:3d9c67d97d6f | 11 | * |
emh203 | 0:3d9c67d97d6f | 12 | * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0 |
emh203 | 0:3d9c67d97d6f | 13 | * |
emh203 | 0:3d9c67d97d6f | 14 | * Redistribution and use in source and binary forms, with or without |
emh203 | 0:3d9c67d97d6f | 15 | * modification, are permitted provided that the following conditions |
emh203 | 0:3d9c67d97d6f | 16 | * are met: |
emh203 | 0:3d9c67d97d6f | 17 | * - Redistributions of source code must retain the above copyright |
emh203 | 0:3d9c67d97d6f | 18 | * notice, this list of conditions and the following disclaimer. |
emh203 | 0:3d9c67d97d6f | 19 | * - Redistributions in binary form must reproduce the above copyright |
emh203 | 0:3d9c67d97d6f | 20 | * notice, this list of conditions and the following disclaimer in |
emh203 | 0:3d9c67d97d6f | 21 | * the documentation and/or other materials provided with the |
emh203 | 0:3d9c67d97d6f | 22 | * distribution. |
emh203 | 0:3d9c67d97d6f | 23 | * - Neither the name of ARM LIMITED nor the names of its contributors |
emh203 | 0:3d9c67d97d6f | 24 | * may be used to endorse or promote products derived from this |
emh203 | 0:3d9c67d97d6f | 25 | * software without specific prior written permission. |
emh203 | 0:3d9c67d97d6f | 26 | * |
emh203 | 0:3d9c67d97d6f | 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
emh203 | 0:3d9c67d97d6f | 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
emh203 | 0:3d9c67d97d6f | 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
emh203 | 0:3d9c67d97d6f | 30 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
emh203 | 0:3d9c67d97d6f | 31 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
emh203 | 0:3d9c67d97d6f | 32 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
emh203 | 0:3d9c67d97d6f | 33 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
emh203 | 0:3d9c67d97d6f | 34 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
emh203 | 0:3d9c67d97d6f | 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
emh203 | 0:3d9c67d97d6f | 36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
emh203 | 0:3d9c67d97d6f | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
emh203 | 0:3d9c67d97d6f | 38 | * POSSIBILITY OF SUCH DAMAGE. |
emh203 | 0:3d9c67d97d6f | 39 | * -------------------------------------------------------------------------- */ |
emh203 | 0:3d9c67d97d6f | 40 | |
emh203 | 0:3d9c67d97d6f | 41 | #include "arm_math.h" |
emh203 | 0:3d9c67d97d6f | 42 | |
emh203 | 0:3d9c67d97d6f | 43 | /** |
emh203 | 0:3d9c67d97d6f | 44 | * @ingroup groupFilters |
emh203 | 0:3d9c67d97d6f | 45 | */ |
emh203 | 0:3d9c67d97d6f | 46 | |
emh203 | 0:3d9c67d97d6f | 47 | /** |
emh203 | 0:3d9c67d97d6f | 48 | * @defgroup Corr Correlation |
emh203 | 0:3d9c67d97d6f | 49 | * |
emh203 | 0:3d9c67d97d6f | 50 | * Correlation is a mathematical operation that is similar to convolution. |
emh203 | 0:3d9c67d97d6f | 51 | * As with convolution, correlation uses two signals to produce a third signal. |
emh203 | 0:3d9c67d97d6f | 52 | * The underlying algorithms in correlation and convolution are identical except that one of the inputs is flipped in convolution. |
emh203 | 0:3d9c67d97d6f | 53 | * Correlation is commonly used to measure the similarity between two signals. |
emh203 | 0:3d9c67d97d6f | 54 | * It has applications in pattern recognition, cryptanalysis, and searching. |
emh203 | 0:3d9c67d97d6f | 55 | * The CMSIS library provides correlation functions for Q7, Q15, Q31 and floating-point data types. |
emh203 | 0:3d9c67d97d6f | 56 | * Fast versions of the Q15 and Q31 functions are also provided. |
emh203 | 0:3d9c67d97d6f | 57 | * |
emh203 | 0:3d9c67d97d6f | 58 | * \par Algorithm |
emh203 | 0:3d9c67d97d6f | 59 | * Let <code>a[n]</code> and <code>b[n]</code> be sequences of length <code>srcALen</code> and <code>srcBLen</code> samples respectively. |
emh203 | 0:3d9c67d97d6f | 60 | * The convolution of the two signals is denoted by |
emh203 | 0:3d9c67d97d6f | 61 | * <pre> |
emh203 | 0:3d9c67d97d6f | 62 | * c[n] = a[n] * b[n] |
emh203 | 0:3d9c67d97d6f | 63 | * </pre> |
emh203 | 0:3d9c67d97d6f | 64 | * In correlation, one of the signals is flipped in time |
emh203 | 0:3d9c67d97d6f | 65 | * <pre> |
emh203 | 0:3d9c67d97d6f | 66 | * c[n] = a[n] * b[-n] |
emh203 | 0:3d9c67d97d6f | 67 | * </pre> |
emh203 | 0:3d9c67d97d6f | 68 | * |
emh203 | 0:3d9c67d97d6f | 69 | * \par |
emh203 | 0:3d9c67d97d6f | 70 | * and this is mathematically defined as |
emh203 | 0:3d9c67d97d6f | 71 | * \image html CorrelateEquation.gif |
emh203 | 0:3d9c67d97d6f | 72 | * \par |
emh203 | 0:3d9c67d97d6f | 73 | * The <code>pSrcA</code> points to the first input vector of length <code>srcALen</code> and <code>pSrcB</code> points to the second input vector of length <code>srcBLen</code>. |
emh203 | 0:3d9c67d97d6f | 74 | * The result <code>c[n]</code> is of length <code>2 * max(srcALen, srcBLen) - 1</code> and is defined over the interval <code>n=0, 1, 2, ..., (2 * max(srcALen, srcBLen) - 2)</code>. |
emh203 | 0:3d9c67d97d6f | 75 | * The output result is written to <code>pDst</code> and the calling function must allocate <code>2 * max(srcALen, srcBLen) - 1</code> words for the result. |
emh203 | 0:3d9c67d97d6f | 76 | * |
emh203 | 0:3d9c67d97d6f | 77 | * <b>Note</b> |
emh203 | 0:3d9c67d97d6f | 78 | * \par |
emh203 | 0:3d9c67d97d6f | 79 | * The <code>pDst</code> should be initialized to all zeros before being used. |
emh203 | 0:3d9c67d97d6f | 80 | * |
emh203 | 0:3d9c67d97d6f | 81 | * <b>Fixed-Point Behavior</b> |
emh203 | 0:3d9c67d97d6f | 82 | * \par |
emh203 | 0:3d9c67d97d6f | 83 | * Correlation requires summing up a large number of intermediate products. |
emh203 | 0:3d9c67d97d6f | 84 | * As such, the Q7, Q15, and Q31 functions run a risk of overflow and saturation. |
emh203 | 0:3d9c67d97d6f | 85 | * Refer to the function specific documentation below for further details of the particular algorithm used. |
emh203 | 0:3d9c67d97d6f | 86 | * |
emh203 | 0:3d9c67d97d6f | 87 | * |
emh203 | 0:3d9c67d97d6f | 88 | * <b>Fast Versions</b> |
emh203 | 0:3d9c67d97d6f | 89 | * |
emh203 | 0:3d9c67d97d6f | 90 | * \par |
emh203 | 0:3d9c67d97d6f | 91 | * Fast versions are supported for Q31 and Q15. Cycles for Fast versions are less compared to Q31 and Q15 of correlate and the design requires |
emh203 | 0:3d9c67d97d6f | 92 | * the input signals should be scaled down to avoid intermediate overflows. |
emh203 | 0:3d9c67d97d6f | 93 | * |
emh203 | 0:3d9c67d97d6f | 94 | * |
emh203 | 0:3d9c67d97d6f | 95 | * <b>Opt Versions</b> |
emh203 | 0:3d9c67d97d6f | 96 | * |
emh203 | 0:3d9c67d97d6f | 97 | * \par |
emh203 | 0:3d9c67d97d6f | 98 | * Opt versions are supported for Q15 and Q7. Design uses internal scratch buffer for getting good optimisation. |
emh203 | 0:3d9c67d97d6f | 99 | * These versions are optimised in cycles and consumes more memory(Scratch memory) compared to Q15 and Q7 versions of correlate |
emh203 | 0:3d9c67d97d6f | 100 | */ |
emh203 | 0:3d9c67d97d6f | 101 | |
emh203 | 0:3d9c67d97d6f | 102 | /** |
emh203 | 0:3d9c67d97d6f | 103 | * @addtogroup Corr |
emh203 | 0:3d9c67d97d6f | 104 | * @{ |
emh203 | 0:3d9c67d97d6f | 105 | */ |
emh203 | 0:3d9c67d97d6f | 106 | /** |
emh203 | 0:3d9c67d97d6f | 107 | * @brief Correlation of floating-point sequences. |
emh203 | 0:3d9c67d97d6f | 108 | * @param[in] *pSrcA points to the first input sequence. |
emh203 | 0:3d9c67d97d6f | 109 | * @param[in] srcALen length of the first input sequence. |
emh203 | 0:3d9c67d97d6f | 110 | * @param[in] *pSrcB points to the second input sequence. |
emh203 | 0:3d9c67d97d6f | 111 | * @param[in] srcBLen length of the second input sequence. |
emh203 | 0:3d9c67d97d6f | 112 | * @param[out] *pDst points to the location where the output result is written. Length 2 * max(srcALen, srcBLen) - 1. |
emh203 | 0:3d9c67d97d6f | 113 | * @return none. |
emh203 | 0:3d9c67d97d6f | 114 | */ |
emh203 | 0:3d9c67d97d6f | 115 | |
emh203 | 0:3d9c67d97d6f | 116 | void arm_correlate_f32( |
emh203 | 0:3d9c67d97d6f | 117 | float32_t * pSrcA, |
emh203 | 0:3d9c67d97d6f | 118 | uint32_t srcALen, |
emh203 | 0:3d9c67d97d6f | 119 | float32_t * pSrcB, |
emh203 | 0:3d9c67d97d6f | 120 | uint32_t srcBLen, |
emh203 | 0:3d9c67d97d6f | 121 | float32_t * pDst) |
emh203 | 0:3d9c67d97d6f | 122 | { |
emh203 | 0:3d9c67d97d6f | 123 | |
emh203 | 0:3d9c67d97d6f | 124 | |
emh203 | 0:3d9c67d97d6f | 125 | #ifndef ARM_MATH_CM0_FAMILY |
emh203 | 0:3d9c67d97d6f | 126 | |
emh203 | 0:3d9c67d97d6f | 127 | /* Run the below code for Cortex-M4 and Cortex-M3 */ |
emh203 | 0:3d9c67d97d6f | 128 | |
emh203 | 0:3d9c67d97d6f | 129 | float32_t *pIn1; /* inputA pointer */ |
emh203 | 0:3d9c67d97d6f | 130 | float32_t *pIn2; /* inputB pointer */ |
emh203 | 0:3d9c67d97d6f | 131 | float32_t *pOut = pDst; /* output pointer */ |
emh203 | 0:3d9c67d97d6f | 132 | float32_t *px; /* Intermediate inputA pointer */ |
emh203 | 0:3d9c67d97d6f | 133 | float32_t *py; /* Intermediate inputB pointer */ |
emh203 | 0:3d9c67d97d6f | 134 | float32_t *pSrc1; /* Intermediate pointers */ |
emh203 | 0:3d9c67d97d6f | 135 | float32_t sum, acc0, acc1, acc2, acc3; /* Accumulators */ |
emh203 | 0:3d9c67d97d6f | 136 | float32_t x0, x1, x2, x3, c0; /* temporary variables for holding input and coefficient values */ |
emh203 | 0:3d9c67d97d6f | 137 | uint32_t j, k = 0u, count, blkCnt, outBlockSize, blockSize1, blockSize2, blockSize3; /* loop counters */ |
emh203 | 0:3d9c67d97d6f | 138 | int32_t inc = 1; /* Destination address modifier */ |
emh203 | 0:3d9c67d97d6f | 139 | |
emh203 | 0:3d9c67d97d6f | 140 | |
emh203 | 0:3d9c67d97d6f | 141 | /* The algorithm implementation is based on the lengths of the inputs. */ |
emh203 | 0:3d9c67d97d6f | 142 | /* srcB is always made to slide across srcA. */ |
emh203 | 0:3d9c67d97d6f | 143 | /* So srcBLen is always considered as shorter or equal to srcALen */ |
emh203 | 0:3d9c67d97d6f | 144 | /* But CORR(x, y) is reverse of CORR(y, x) */ |
emh203 | 0:3d9c67d97d6f | 145 | /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */ |
emh203 | 0:3d9c67d97d6f | 146 | /* and the destination pointer modifier, inc is set to -1 */ |
emh203 | 0:3d9c67d97d6f | 147 | /* If srcALen > srcBLen, zero pad has to be done to srcB to make the two inputs of same length */ |
emh203 | 0:3d9c67d97d6f | 148 | /* But to improve the performance, |
emh203 | 0:3d9c67d97d6f | 149 | * we assume zeroes in the output instead of zero padding either of the the inputs*/ |
emh203 | 0:3d9c67d97d6f | 150 | /* If srcALen > srcBLen, |
emh203 | 0:3d9c67d97d6f | 151 | * (srcALen - srcBLen) zeroes has to included in the starting of the output buffer */ |
emh203 | 0:3d9c67d97d6f | 152 | /* If srcALen < srcBLen, |
emh203 | 0:3d9c67d97d6f | 153 | * (srcALen - srcBLen) zeroes has to included in the ending of the output buffer */ |
emh203 | 0:3d9c67d97d6f | 154 | if(srcALen >= srcBLen) |
emh203 | 0:3d9c67d97d6f | 155 | { |
emh203 | 0:3d9c67d97d6f | 156 | /* Initialization of inputA pointer */ |
emh203 | 0:3d9c67d97d6f | 157 | pIn1 = pSrcA; |
emh203 | 0:3d9c67d97d6f | 158 | |
emh203 | 0:3d9c67d97d6f | 159 | /* Initialization of inputB pointer */ |
emh203 | 0:3d9c67d97d6f | 160 | pIn2 = pSrcB; |
emh203 | 0:3d9c67d97d6f | 161 | |
emh203 | 0:3d9c67d97d6f | 162 | /* Number of output samples is calculated */ |
emh203 | 0:3d9c67d97d6f | 163 | outBlockSize = (2u * srcALen) - 1u; |
emh203 | 0:3d9c67d97d6f | 164 | |
emh203 | 0:3d9c67d97d6f | 165 | /* When srcALen > srcBLen, zero padding has to be done to srcB |
emh203 | 0:3d9c67d97d6f | 166 | * to make their lengths equal. |
emh203 | 0:3d9c67d97d6f | 167 | * Instead, (outBlockSize - (srcALen + srcBLen - 1)) |
emh203 | 0:3d9c67d97d6f | 168 | * number of output samples are made zero */ |
emh203 | 0:3d9c67d97d6f | 169 | j = outBlockSize - (srcALen + (srcBLen - 1u)); |
emh203 | 0:3d9c67d97d6f | 170 | |
emh203 | 0:3d9c67d97d6f | 171 | /* Updating the pointer position to non zero value */ |
emh203 | 0:3d9c67d97d6f | 172 | pOut += j; |
emh203 | 0:3d9c67d97d6f | 173 | |
emh203 | 0:3d9c67d97d6f | 174 | //while(j > 0u) |
emh203 | 0:3d9c67d97d6f | 175 | //{ |
emh203 | 0:3d9c67d97d6f | 176 | // /* Zero is stored in the destination buffer */ |
emh203 | 0:3d9c67d97d6f | 177 | // *pOut++ = 0.0f; |
emh203 | 0:3d9c67d97d6f | 178 | |
emh203 | 0:3d9c67d97d6f | 179 | // /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 180 | // j--; |
emh203 | 0:3d9c67d97d6f | 181 | //} |
emh203 | 0:3d9c67d97d6f | 182 | |
emh203 | 0:3d9c67d97d6f | 183 | } |
emh203 | 0:3d9c67d97d6f | 184 | else |
emh203 | 0:3d9c67d97d6f | 185 | { |
emh203 | 0:3d9c67d97d6f | 186 | /* Initialization of inputA pointer */ |
emh203 | 0:3d9c67d97d6f | 187 | pIn1 = pSrcB; |
emh203 | 0:3d9c67d97d6f | 188 | |
emh203 | 0:3d9c67d97d6f | 189 | /* Initialization of inputB pointer */ |
emh203 | 0:3d9c67d97d6f | 190 | pIn2 = pSrcA; |
emh203 | 0:3d9c67d97d6f | 191 | |
emh203 | 0:3d9c67d97d6f | 192 | /* srcBLen is always considered as shorter or equal to srcALen */ |
emh203 | 0:3d9c67d97d6f | 193 | j = srcBLen; |
emh203 | 0:3d9c67d97d6f | 194 | srcBLen = srcALen; |
emh203 | 0:3d9c67d97d6f | 195 | srcALen = j; |
emh203 | 0:3d9c67d97d6f | 196 | |
emh203 | 0:3d9c67d97d6f | 197 | /* CORR(x, y) = Reverse order(CORR(y, x)) */ |
emh203 | 0:3d9c67d97d6f | 198 | /* Hence set the destination pointer to point to the last output sample */ |
emh203 | 0:3d9c67d97d6f | 199 | pOut = pDst + ((srcALen + srcBLen) - 2u); |
emh203 | 0:3d9c67d97d6f | 200 | |
emh203 | 0:3d9c67d97d6f | 201 | /* Destination address modifier is set to -1 */ |
emh203 | 0:3d9c67d97d6f | 202 | inc = -1; |
emh203 | 0:3d9c67d97d6f | 203 | |
emh203 | 0:3d9c67d97d6f | 204 | } |
emh203 | 0:3d9c67d97d6f | 205 | |
emh203 | 0:3d9c67d97d6f | 206 | /* The function is internally |
emh203 | 0:3d9c67d97d6f | 207 | * divided into three parts according to the number of multiplications that has to be |
emh203 | 0:3d9c67d97d6f | 208 | * taken place between inputA samples and inputB samples. In the first part of the |
emh203 | 0:3d9c67d97d6f | 209 | * algorithm, the multiplications increase by one for every iteration. |
emh203 | 0:3d9c67d97d6f | 210 | * In the second part of the algorithm, srcBLen number of multiplications are done. |
emh203 | 0:3d9c67d97d6f | 211 | * In the third part of the algorithm, the multiplications decrease by one |
emh203 | 0:3d9c67d97d6f | 212 | * for every iteration.*/ |
emh203 | 0:3d9c67d97d6f | 213 | /* The algorithm is implemented in three stages. |
emh203 | 0:3d9c67d97d6f | 214 | * The loop counters of each stage is initiated here. */ |
emh203 | 0:3d9c67d97d6f | 215 | blockSize1 = srcBLen - 1u; |
emh203 | 0:3d9c67d97d6f | 216 | blockSize2 = srcALen - (srcBLen - 1u); |
emh203 | 0:3d9c67d97d6f | 217 | blockSize3 = blockSize1; |
emh203 | 0:3d9c67d97d6f | 218 | |
emh203 | 0:3d9c67d97d6f | 219 | /* -------------------------- |
emh203 | 0:3d9c67d97d6f | 220 | * Initializations of stage1 |
emh203 | 0:3d9c67d97d6f | 221 | * -------------------------*/ |
emh203 | 0:3d9c67d97d6f | 222 | |
emh203 | 0:3d9c67d97d6f | 223 | /* sum = x[0] * y[srcBlen - 1] |
emh203 | 0:3d9c67d97d6f | 224 | * sum = x[0] * y[srcBlen-2] + x[1] * y[srcBlen - 1] |
emh203 | 0:3d9c67d97d6f | 225 | * .... |
emh203 | 0:3d9c67d97d6f | 226 | * sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen - 1] * y[srcBLen - 1] |
emh203 | 0:3d9c67d97d6f | 227 | */ |
emh203 | 0:3d9c67d97d6f | 228 | |
emh203 | 0:3d9c67d97d6f | 229 | /* In this stage the MAC operations are increased by 1 for every iteration. |
emh203 | 0:3d9c67d97d6f | 230 | The count variable holds the number of MAC operations performed */ |
emh203 | 0:3d9c67d97d6f | 231 | count = 1u; |
emh203 | 0:3d9c67d97d6f | 232 | |
emh203 | 0:3d9c67d97d6f | 233 | /* Working pointer of inputA */ |
emh203 | 0:3d9c67d97d6f | 234 | px = pIn1; |
emh203 | 0:3d9c67d97d6f | 235 | |
emh203 | 0:3d9c67d97d6f | 236 | /* Working pointer of inputB */ |
emh203 | 0:3d9c67d97d6f | 237 | pSrc1 = pIn2 + (srcBLen - 1u); |
emh203 | 0:3d9c67d97d6f | 238 | py = pSrc1; |
emh203 | 0:3d9c67d97d6f | 239 | |
emh203 | 0:3d9c67d97d6f | 240 | /* ------------------------ |
emh203 | 0:3d9c67d97d6f | 241 | * Stage1 process |
emh203 | 0:3d9c67d97d6f | 242 | * ----------------------*/ |
emh203 | 0:3d9c67d97d6f | 243 | |
emh203 | 0:3d9c67d97d6f | 244 | /* The first stage starts here */ |
emh203 | 0:3d9c67d97d6f | 245 | while(blockSize1 > 0u) |
emh203 | 0:3d9c67d97d6f | 246 | { |
emh203 | 0:3d9c67d97d6f | 247 | /* Accumulator is made zero for every iteration */ |
emh203 | 0:3d9c67d97d6f | 248 | sum = 0.0f; |
emh203 | 0:3d9c67d97d6f | 249 | |
emh203 | 0:3d9c67d97d6f | 250 | /* Apply loop unrolling and compute 4 MACs simultaneously. */ |
emh203 | 0:3d9c67d97d6f | 251 | k = count >> 2u; |
emh203 | 0:3d9c67d97d6f | 252 | |
emh203 | 0:3d9c67d97d6f | 253 | /* First part of the processing with loop unrolling. Compute 4 MACs at a time. |
emh203 | 0:3d9c67d97d6f | 254 | ** a second loop below computes MACs for the remaining 1 to 3 samples. */ |
emh203 | 0:3d9c67d97d6f | 255 | while(k > 0u) |
emh203 | 0:3d9c67d97d6f | 256 | { |
emh203 | 0:3d9c67d97d6f | 257 | /* x[0] * y[srcBLen - 4] */ |
emh203 | 0:3d9c67d97d6f | 258 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 259 | /* x[1] * y[srcBLen - 3] */ |
emh203 | 0:3d9c67d97d6f | 260 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 261 | /* x[2] * y[srcBLen - 2] */ |
emh203 | 0:3d9c67d97d6f | 262 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 263 | /* x[3] * y[srcBLen - 1] */ |
emh203 | 0:3d9c67d97d6f | 264 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 265 | |
emh203 | 0:3d9c67d97d6f | 266 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 267 | k--; |
emh203 | 0:3d9c67d97d6f | 268 | } |
emh203 | 0:3d9c67d97d6f | 269 | |
emh203 | 0:3d9c67d97d6f | 270 | /* If the count is not a multiple of 4, compute any remaining MACs here. |
emh203 | 0:3d9c67d97d6f | 271 | ** No loop unrolling is used. */ |
emh203 | 0:3d9c67d97d6f | 272 | k = count % 0x4u; |
emh203 | 0:3d9c67d97d6f | 273 | |
emh203 | 0:3d9c67d97d6f | 274 | while(k > 0u) |
emh203 | 0:3d9c67d97d6f | 275 | { |
emh203 | 0:3d9c67d97d6f | 276 | /* Perform the multiply-accumulate */ |
emh203 | 0:3d9c67d97d6f | 277 | /* x[0] * y[srcBLen - 1] */ |
emh203 | 0:3d9c67d97d6f | 278 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 279 | |
emh203 | 0:3d9c67d97d6f | 280 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 281 | k--; |
emh203 | 0:3d9c67d97d6f | 282 | } |
emh203 | 0:3d9c67d97d6f | 283 | |
emh203 | 0:3d9c67d97d6f | 284 | /* Store the result in the accumulator in the destination buffer. */ |
emh203 | 0:3d9c67d97d6f | 285 | *pOut = sum; |
emh203 | 0:3d9c67d97d6f | 286 | /* Destination pointer is updated according to the address modifier, inc */ |
emh203 | 0:3d9c67d97d6f | 287 | pOut += inc; |
emh203 | 0:3d9c67d97d6f | 288 | |
emh203 | 0:3d9c67d97d6f | 289 | /* Update the inputA and inputB pointers for next MAC calculation */ |
emh203 | 0:3d9c67d97d6f | 290 | py = pSrc1 - count; |
emh203 | 0:3d9c67d97d6f | 291 | px = pIn1; |
emh203 | 0:3d9c67d97d6f | 292 | |
emh203 | 0:3d9c67d97d6f | 293 | /* Increment the MAC count */ |
emh203 | 0:3d9c67d97d6f | 294 | count++; |
emh203 | 0:3d9c67d97d6f | 295 | |
emh203 | 0:3d9c67d97d6f | 296 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 297 | blockSize1--; |
emh203 | 0:3d9c67d97d6f | 298 | } |
emh203 | 0:3d9c67d97d6f | 299 | |
emh203 | 0:3d9c67d97d6f | 300 | /* -------------------------- |
emh203 | 0:3d9c67d97d6f | 301 | * Initializations of stage2 |
emh203 | 0:3d9c67d97d6f | 302 | * ------------------------*/ |
emh203 | 0:3d9c67d97d6f | 303 | |
emh203 | 0:3d9c67d97d6f | 304 | /* sum = x[0] * y[0] + x[1] * y[1] +...+ x[srcBLen-1] * y[srcBLen-1] |
emh203 | 0:3d9c67d97d6f | 305 | * sum = x[1] * y[0] + x[2] * y[1] +...+ x[srcBLen] * y[srcBLen-1] |
emh203 | 0:3d9c67d97d6f | 306 | * .... |
emh203 | 0:3d9c67d97d6f | 307 | * sum = x[srcALen-srcBLen-2] * y[0] + x[srcALen-srcBLen-1] * y[1] +...+ x[srcALen-1] * y[srcBLen-1] |
emh203 | 0:3d9c67d97d6f | 308 | */ |
emh203 | 0:3d9c67d97d6f | 309 | |
emh203 | 0:3d9c67d97d6f | 310 | /* Working pointer of inputA */ |
emh203 | 0:3d9c67d97d6f | 311 | px = pIn1; |
emh203 | 0:3d9c67d97d6f | 312 | |
emh203 | 0:3d9c67d97d6f | 313 | /* Working pointer of inputB */ |
emh203 | 0:3d9c67d97d6f | 314 | py = pIn2; |
emh203 | 0:3d9c67d97d6f | 315 | |
emh203 | 0:3d9c67d97d6f | 316 | /* count is index by which the pointer pIn1 to be incremented */ |
emh203 | 0:3d9c67d97d6f | 317 | count = 0u; |
emh203 | 0:3d9c67d97d6f | 318 | |
emh203 | 0:3d9c67d97d6f | 319 | /* ------------------- |
emh203 | 0:3d9c67d97d6f | 320 | * Stage2 process |
emh203 | 0:3d9c67d97d6f | 321 | * ------------------*/ |
emh203 | 0:3d9c67d97d6f | 322 | |
emh203 | 0:3d9c67d97d6f | 323 | /* Stage2 depends on srcBLen as in this stage srcBLen number of MACS are performed. |
emh203 | 0:3d9c67d97d6f | 324 | * So, to loop unroll over blockSize2, |
emh203 | 0:3d9c67d97d6f | 325 | * srcBLen should be greater than or equal to 4, to loop unroll the srcBLen loop */ |
emh203 | 0:3d9c67d97d6f | 326 | if(srcBLen >= 4u) |
emh203 | 0:3d9c67d97d6f | 327 | { |
emh203 | 0:3d9c67d97d6f | 328 | /* Loop unroll over blockSize2, by 4 */ |
emh203 | 0:3d9c67d97d6f | 329 | blkCnt = blockSize2 >> 2u; |
emh203 | 0:3d9c67d97d6f | 330 | |
emh203 | 0:3d9c67d97d6f | 331 | while(blkCnt > 0u) |
emh203 | 0:3d9c67d97d6f | 332 | { |
emh203 | 0:3d9c67d97d6f | 333 | /* Set all accumulators to zero */ |
emh203 | 0:3d9c67d97d6f | 334 | acc0 = 0.0f; |
emh203 | 0:3d9c67d97d6f | 335 | acc1 = 0.0f; |
emh203 | 0:3d9c67d97d6f | 336 | acc2 = 0.0f; |
emh203 | 0:3d9c67d97d6f | 337 | acc3 = 0.0f; |
emh203 | 0:3d9c67d97d6f | 338 | |
emh203 | 0:3d9c67d97d6f | 339 | /* read x[0], x[1], x[2] samples */ |
emh203 | 0:3d9c67d97d6f | 340 | x0 = *(px++); |
emh203 | 0:3d9c67d97d6f | 341 | x1 = *(px++); |
emh203 | 0:3d9c67d97d6f | 342 | x2 = *(px++); |
emh203 | 0:3d9c67d97d6f | 343 | |
emh203 | 0:3d9c67d97d6f | 344 | /* Apply loop unrolling and compute 4 MACs simultaneously. */ |
emh203 | 0:3d9c67d97d6f | 345 | k = srcBLen >> 2u; |
emh203 | 0:3d9c67d97d6f | 346 | |
emh203 | 0:3d9c67d97d6f | 347 | /* First part of the processing with loop unrolling. Compute 4 MACs at a time. |
emh203 | 0:3d9c67d97d6f | 348 | ** a second loop below computes MACs for the remaining 1 to 3 samples. */ |
emh203 | 0:3d9c67d97d6f | 349 | do |
emh203 | 0:3d9c67d97d6f | 350 | { |
emh203 | 0:3d9c67d97d6f | 351 | /* Read y[0] sample */ |
emh203 | 0:3d9c67d97d6f | 352 | c0 = *(py++); |
emh203 | 0:3d9c67d97d6f | 353 | |
emh203 | 0:3d9c67d97d6f | 354 | /* Read x[3] sample */ |
emh203 | 0:3d9c67d97d6f | 355 | x3 = *(px++); |
emh203 | 0:3d9c67d97d6f | 356 | |
emh203 | 0:3d9c67d97d6f | 357 | /* Perform the multiply-accumulate */ |
emh203 | 0:3d9c67d97d6f | 358 | /* acc0 += x[0] * y[0] */ |
emh203 | 0:3d9c67d97d6f | 359 | acc0 += x0 * c0; |
emh203 | 0:3d9c67d97d6f | 360 | /* acc1 += x[1] * y[0] */ |
emh203 | 0:3d9c67d97d6f | 361 | acc1 += x1 * c0; |
emh203 | 0:3d9c67d97d6f | 362 | /* acc2 += x[2] * y[0] */ |
emh203 | 0:3d9c67d97d6f | 363 | acc2 += x2 * c0; |
emh203 | 0:3d9c67d97d6f | 364 | /* acc3 += x[3] * y[0] */ |
emh203 | 0:3d9c67d97d6f | 365 | acc3 += x3 * c0; |
emh203 | 0:3d9c67d97d6f | 366 | |
emh203 | 0:3d9c67d97d6f | 367 | /* Read y[1] sample */ |
emh203 | 0:3d9c67d97d6f | 368 | c0 = *(py++); |
emh203 | 0:3d9c67d97d6f | 369 | |
emh203 | 0:3d9c67d97d6f | 370 | /* Read x[4] sample */ |
emh203 | 0:3d9c67d97d6f | 371 | x0 = *(px++); |
emh203 | 0:3d9c67d97d6f | 372 | |
emh203 | 0:3d9c67d97d6f | 373 | /* Perform the multiply-accumulate */ |
emh203 | 0:3d9c67d97d6f | 374 | /* acc0 += x[1] * y[1] */ |
emh203 | 0:3d9c67d97d6f | 375 | acc0 += x1 * c0; |
emh203 | 0:3d9c67d97d6f | 376 | /* acc1 += x[2] * y[1] */ |
emh203 | 0:3d9c67d97d6f | 377 | acc1 += x2 * c0; |
emh203 | 0:3d9c67d97d6f | 378 | /* acc2 += x[3] * y[1] */ |
emh203 | 0:3d9c67d97d6f | 379 | acc2 += x3 * c0; |
emh203 | 0:3d9c67d97d6f | 380 | /* acc3 += x[4] * y[1] */ |
emh203 | 0:3d9c67d97d6f | 381 | acc3 += x0 * c0; |
emh203 | 0:3d9c67d97d6f | 382 | |
emh203 | 0:3d9c67d97d6f | 383 | /* Read y[2] sample */ |
emh203 | 0:3d9c67d97d6f | 384 | c0 = *(py++); |
emh203 | 0:3d9c67d97d6f | 385 | |
emh203 | 0:3d9c67d97d6f | 386 | /* Read x[5] sample */ |
emh203 | 0:3d9c67d97d6f | 387 | x1 = *(px++); |
emh203 | 0:3d9c67d97d6f | 388 | |
emh203 | 0:3d9c67d97d6f | 389 | /* Perform the multiply-accumulates */ |
emh203 | 0:3d9c67d97d6f | 390 | /* acc0 += x[2] * y[2] */ |
emh203 | 0:3d9c67d97d6f | 391 | acc0 += x2 * c0; |
emh203 | 0:3d9c67d97d6f | 392 | /* acc1 += x[3] * y[2] */ |
emh203 | 0:3d9c67d97d6f | 393 | acc1 += x3 * c0; |
emh203 | 0:3d9c67d97d6f | 394 | /* acc2 += x[4] * y[2] */ |
emh203 | 0:3d9c67d97d6f | 395 | acc2 += x0 * c0; |
emh203 | 0:3d9c67d97d6f | 396 | /* acc3 += x[5] * y[2] */ |
emh203 | 0:3d9c67d97d6f | 397 | acc3 += x1 * c0; |
emh203 | 0:3d9c67d97d6f | 398 | |
emh203 | 0:3d9c67d97d6f | 399 | /* Read y[3] sample */ |
emh203 | 0:3d9c67d97d6f | 400 | c0 = *(py++); |
emh203 | 0:3d9c67d97d6f | 401 | |
emh203 | 0:3d9c67d97d6f | 402 | /* Read x[6] sample */ |
emh203 | 0:3d9c67d97d6f | 403 | x2 = *(px++); |
emh203 | 0:3d9c67d97d6f | 404 | |
emh203 | 0:3d9c67d97d6f | 405 | /* Perform the multiply-accumulates */ |
emh203 | 0:3d9c67d97d6f | 406 | /* acc0 += x[3] * y[3] */ |
emh203 | 0:3d9c67d97d6f | 407 | acc0 += x3 * c0; |
emh203 | 0:3d9c67d97d6f | 408 | /* acc1 += x[4] * y[3] */ |
emh203 | 0:3d9c67d97d6f | 409 | acc1 += x0 * c0; |
emh203 | 0:3d9c67d97d6f | 410 | /* acc2 += x[5] * y[3] */ |
emh203 | 0:3d9c67d97d6f | 411 | acc2 += x1 * c0; |
emh203 | 0:3d9c67d97d6f | 412 | /* acc3 += x[6] * y[3] */ |
emh203 | 0:3d9c67d97d6f | 413 | acc3 += x2 * c0; |
emh203 | 0:3d9c67d97d6f | 414 | |
emh203 | 0:3d9c67d97d6f | 415 | |
emh203 | 0:3d9c67d97d6f | 416 | } while(--k); |
emh203 | 0:3d9c67d97d6f | 417 | |
emh203 | 0:3d9c67d97d6f | 418 | /* If the srcBLen is not a multiple of 4, compute any remaining MACs here. |
emh203 | 0:3d9c67d97d6f | 419 | ** No loop unrolling is used. */ |
emh203 | 0:3d9c67d97d6f | 420 | k = srcBLen % 0x4u; |
emh203 | 0:3d9c67d97d6f | 421 | |
emh203 | 0:3d9c67d97d6f | 422 | while(k > 0u) |
emh203 | 0:3d9c67d97d6f | 423 | { |
emh203 | 0:3d9c67d97d6f | 424 | /* Read y[4] sample */ |
emh203 | 0:3d9c67d97d6f | 425 | c0 = *(py++); |
emh203 | 0:3d9c67d97d6f | 426 | |
emh203 | 0:3d9c67d97d6f | 427 | /* Read x[7] sample */ |
emh203 | 0:3d9c67d97d6f | 428 | x3 = *(px++); |
emh203 | 0:3d9c67d97d6f | 429 | |
emh203 | 0:3d9c67d97d6f | 430 | /* Perform the multiply-accumulates */ |
emh203 | 0:3d9c67d97d6f | 431 | /* acc0 += x[4] * y[4] */ |
emh203 | 0:3d9c67d97d6f | 432 | acc0 += x0 * c0; |
emh203 | 0:3d9c67d97d6f | 433 | /* acc1 += x[5] * y[4] */ |
emh203 | 0:3d9c67d97d6f | 434 | acc1 += x1 * c0; |
emh203 | 0:3d9c67d97d6f | 435 | /* acc2 += x[6] * y[4] */ |
emh203 | 0:3d9c67d97d6f | 436 | acc2 += x2 * c0; |
emh203 | 0:3d9c67d97d6f | 437 | /* acc3 += x[7] * y[4] */ |
emh203 | 0:3d9c67d97d6f | 438 | acc3 += x3 * c0; |
emh203 | 0:3d9c67d97d6f | 439 | |
emh203 | 0:3d9c67d97d6f | 440 | /* Reuse the present samples for the next MAC */ |
emh203 | 0:3d9c67d97d6f | 441 | x0 = x1; |
emh203 | 0:3d9c67d97d6f | 442 | x1 = x2; |
emh203 | 0:3d9c67d97d6f | 443 | x2 = x3; |
emh203 | 0:3d9c67d97d6f | 444 | |
emh203 | 0:3d9c67d97d6f | 445 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 446 | k--; |
emh203 | 0:3d9c67d97d6f | 447 | } |
emh203 | 0:3d9c67d97d6f | 448 | |
emh203 | 0:3d9c67d97d6f | 449 | /* Store the result in the accumulator in the destination buffer. */ |
emh203 | 0:3d9c67d97d6f | 450 | *pOut = acc0; |
emh203 | 0:3d9c67d97d6f | 451 | /* Destination pointer is updated according to the address modifier, inc */ |
emh203 | 0:3d9c67d97d6f | 452 | pOut += inc; |
emh203 | 0:3d9c67d97d6f | 453 | |
emh203 | 0:3d9c67d97d6f | 454 | *pOut = acc1; |
emh203 | 0:3d9c67d97d6f | 455 | pOut += inc; |
emh203 | 0:3d9c67d97d6f | 456 | |
emh203 | 0:3d9c67d97d6f | 457 | *pOut = acc2; |
emh203 | 0:3d9c67d97d6f | 458 | pOut += inc; |
emh203 | 0:3d9c67d97d6f | 459 | |
emh203 | 0:3d9c67d97d6f | 460 | *pOut = acc3; |
emh203 | 0:3d9c67d97d6f | 461 | pOut += inc; |
emh203 | 0:3d9c67d97d6f | 462 | |
emh203 | 0:3d9c67d97d6f | 463 | /* Increment the pointer pIn1 index, count by 4 */ |
emh203 | 0:3d9c67d97d6f | 464 | count += 4u; |
emh203 | 0:3d9c67d97d6f | 465 | |
emh203 | 0:3d9c67d97d6f | 466 | /* Update the inputA and inputB pointers for next MAC calculation */ |
emh203 | 0:3d9c67d97d6f | 467 | px = pIn1 + count; |
emh203 | 0:3d9c67d97d6f | 468 | py = pIn2; |
emh203 | 0:3d9c67d97d6f | 469 | |
emh203 | 0:3d9c67d97d6f | 470 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 471 | blkCnt--; |
emh203 | 0:3d9c67d97d6f | 472 | } |
emh203 | 0:3d9c67d97d6f | 473 | |
emh203 | 0:3d9c67d97d6f | 474 | /* If the blockSize2 is not a multiple of 4, compute any remaining output samples here. |
emh203 | 0:3d9c67d97d6f | 475 | ** No loop unrolling is used. */ |
emh203 | 0:3d9c67d97d6f | 476 | blkCnt = blockSize2 % 0x4u; |
emh203 | 0:3d9c67d97d6f | 477 | |
emh203 | 0:3d9c67d97d6f | 478 | while(blkCnt > 0u) |
emh203 | 0:3d9c67d97d6f | 479 | { |
emh203 | 0:3d9c67d97d6f | 480 | /* Accumulator is made zero for every iteration */ |
emh203 | 0:3d9c67d97d6f | 481 | sum = 0.0f; |
emh203 | 0:3d9c67d97d6f | 482 | |
emh203 | 0:3d9c67d97d6f | 483 | /* Apply loop unrolling and compute 4 MACs simultaneously. */ |
emh203 | 0:3d9c67d97d6f | 484 | k = srcBLen >> 2u; |
emh203 | 0:3d9c67d97d6f | 485 | |
emh203 | 0:3d9c67d97d6f | 486 | /* First part of the processing with loop unrolling. Compute 4 MACs at a time. |
emh203 | 0:3d9c67d97d6f | 487 | ** a second loop below computes MACs for the remaining 1 to 3 samples. */ |
emh203 | 0:3d9c67d97d6f | 488 | while(k > 0u) |
emh203 | 0:3d9c67d97d6f | 489 | { |
emh203 | 0:3d9c67d97d6f | 490 | /* Perform the multiply-accumulates */ |
emh203 | 0:3d9c67d97d6f | 491 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 492 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 493 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 494 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 495 | |
emh203 | 0:3d9c67d97d6f | 496 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 497 | k--; |
emh203 | 0:3d9c67d97d6f | 498 | } |
emh203 | 0:3d9c67d97d6f | 499 | |
emh203 | 0:3d9c67d97d6f | 500 | /* If the srcBLen is not a multiple of 4, compute any remaining MACs here. |
emh203 | 0:3d9c67d97d6f | 501 | ** No loop unrolling is used. */ |
emh203 | 0:3d9c67d97d6f | 502 | k = srcBLen % 0x4u; |
emh203 | 0:3d9c67d97d6f | 503 | |
emh203 | 0:3d9c67d97d6f | 504 | while(k > 0u) |
emh203 | 0:3d9c67d97d6f | 505 | { |
emh203 | 0:3d9c67d97d6f | 506 | /* Perform the multiply-accumulate */ |
emh203 | 0:3d9c67d97d6f | 507 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 508 | |
emh203 | 0:3d9c67d97d6f | 509 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 510 | k--; |
emh203 | 0:3d9c67d97d6f | 511 | } |
emh203 | 0:3d9c67d97d6f | 512 | |
emh203 | 0:3d9c67d97d6f | 513 | /* Store the result in the accumulator in the destination buffer. */ |
emh203 | 0:3d9c67d97d6f | 514 | *pOut = sum; |
emh203 | 0:3d9c67d97d6f | 515 | /* Destination pointer is updated according to the address modifier, inc */ |
emh203 | 0:3d9c67d97d6f | 516 | pOut += inc; |
emh203 | 0:3d9c67d97d6f | 517 | |
emh203 | 0:3d9c67d97d6f | 518 | /* Increment the pointer pIn1 index, count by 1 */ |
emh203 | 0:3d9c67d97d6f | 519 | count++; |
emh203 | 0:3d9c67d97d6f | 520 | |
emh203 | 0:3d9c67d97d6f | 521 | /* Update the inputA and inputB pointers for next MAC calculation */ |
emh203 | 0:3d9c67d97d6f | 522 | px = pIn1 + count; |
emh203 | 0:3d9c67d97d6f | 523 | py = pIn2; |
emh203 | 0:3d9c67d97d6f | 524 | |
emh203 | 0:3d9c67d97d6f | 525 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 526 | blkCnt--; |
emh203 | 0:3d9c67d97d6f | 527 | } |
emh203 | 0:3d9c67d97d6f | 528 | } |
emh203 | 0:3d9c67d97d6f | 529 | else |
emh203 | 0:3d9c67d97d6f | 530 | { |
emh203 | 0:3d9c67d97d6f | 531 | /* If the srcBLen is not a multiple of 4, |
emh203 | 0:3d9c67d97d6f | 532 | * the blockSize2 loop cannot be unrolled by 4 */ |
emh203 | 0:3d9c67d97d6f | 533 | blkCnt = blockSize2; |
emh203 | 0:3d9c67d97d6f | 534 | |
emh203 | 0:3d9c67d97d6f | 535 | while(blkCnt > 0u) |
emh203 | 0:3d9c67d97d6f | 536 | { |
emh203 | 0:3d9c67d97d6f | 537 | /* Accumulator is made zero for every iteration */ |
emh203 | 0:3d9c67d97d6f | 538 | sum = 0.0f; |
emh203 | 0:3d9c67d97d6f | 539 | |
emh203 | 0:3d9c67d97d6f | 540 | /* Loop over srcBLen */ |
emh203 | 0:3d9c67d97d6f | 541 | k = srcBLen; |
emh203 | 0:3d9c67d97d6f | 542 | |
emh203 | 0:3d9c67d97d6f | 543 | while(k > 0u) |
emh203 | 0:3d9c67d97d6f | 544 | { |
emh203 | 0:3d9c67d97d6f | 545 | /* Perform the multiply-accumulate */ |
emh203 | 0:3d9c67d97d6f | 546 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 547 | |
emh203 | 0:3d9c67d97d6f | 548 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 549 | k--; |
emh203 | 0:3d9c67d97d6f | 550 | } |
emh203 | 0:3d9c67d97d6f | 551 | |
emh203 | 0:3d9c67d97d6f | 552 | /* Store the result in the accumulator in the destination buffer. */ |
emh203 | 0:3d9c67d97d6f | 553 | *pOut = sum; |
emh203 | 0:3d9c67d97d6f | 554 | /* Destination pointer is updated according to the address modifier, inc */ |
emh203 | 0:3d9c67d97d6f | 555 | pOut += inc; |
emh203 | 0:3d9c67d97d6f | 556 | |
emh203 | 0:3d9c67d97d6f | 557 | /* Increment the pointer pIn1 index, count by 1 */ |
emh203 | 0:3d9c67d97d6f | 558 | count++; |
emh203 | 0:3d9c67d97d6f | 559 | |
emh203 | 0:3d9c67d97d6f | 560 | /* Update the inputA and inputB pointers for next MAC calculation */ |
emh203 | 0:3d9c67d97d6f | 561 | px = pIn1 + count; |
emh203 | 0:3d9c67d97d6f | 562 | py = pIn2; |
emh203 | 0:3d9c67d97d6f | 563 | |
emh203 | 0:3d9c67d97d6f | 564 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 565 | blkCnt--; |
emh203 | 0:3d9c67d97d6f | 566 | } |
emh203 | 0:3d9c67d97d6f | 567 | } |
emh203 | 0:3d9c67d97d6f | 568 | |
emh203 | 0:3d9c67d97d6f | 569 | /* -------------------------- |
emh203 | 0:3d9c67d97d6f | 570 | * Initializations of stage3 |
emh203 | 0:3d9c67d97d6f | 571 | * -------------------------*/ |
emh203 | 0:3d9c67d97d6f | 572 | |
emh203 | 0:3d9c67d97d6f | 573 | /* sum += x[srcALen-srcBLen+1] * y[0] + x[srcALen-srcBLen+2] * y[1] +...+ x[srcALen-1] * y[srcBLen-1] |
emh203 | 0:3d9c67d97d6f | 574 | * sum += x[srcALen-srcBLen+2] * y[0] + x[srcALen-srcBLen+3] * y[1] +...+ x[srcALen-1] * y[srcBLen-1] |
emh203 | 0:3d9c67d97d6f | 575 | * .... |
emh203 | 0:3d9c67d97d6f | 576 | * sum += x[srcALen-2] * y[0] + x[srcALen-1] * y[1] |
emh203 | 0:3d9c67d97d6f | 577 | * sum += x[srcALen-1] * y[0] |
emh203 | 0:3d9c67d97d6f | 578 | */ |
emh203 | 0:3d9c67d97d6f | 579 | |
emh203 | 0:3d9c67d97d6f | 580 | /* In this stage the MAC operations are decreased by 1 for every iteration. |
emh203 | 0:3d9c67d97d6f | 581 | The count variable holds the number of MAC operations performed */ |
emh203 | 0:3d9c67d97d6f | 582 | count = srcBLen - 1u; |
emh203 | 0:3d9c67d97d6f | 583 | |
emh203 | 0:3d9c67d97d6f | 584 | /* Working pointer of inputA */ |
emh203 | 0:3d9c67d97d6f | 585 | pSrc1 = pIn1 + (srcALen - (srcBLen - 1u)); |
emh203 | 0:3d9c67d97d6f | 586 | px = pSrc1; |
emh203 | 0:3d9c67d97d6f | 587 | |
emh203 | 0:3d9c67d97d6f | 588 | /* Working pointer of inputB */ |
emh203 | 0:3d9c67d97d6f | 589 | py = pIn2; |
emh203 | 0:3d9c67d97d6f | 590 | |
emh203 | 0:3d9c67d97d6f | 591 | /* ------------------- |
emh203 | 0:3d9c67d97d6f | 592 | * Stage3 process |
emh203 | 0:3d9c67d97d6f | 593 | * ------------------*/ |
emh203 | 0:3d9c67d97d6f | 594 | |
emh203 | 0:3d9c67d97d6f | 595 | while(blockSize3 > 0u) |
emh203 | 0:3d9c67d97d6f | 596 | { |
emh203 | 0:3d9c67d97d6f | 597 | /* Accumulator is made zero for every iteration */ |
emh203 | 0:3d9c67d97d6f | 598 | sum = 0.0f; |
emh203 | 0:3d9c67d97d6f | 599 | |
emh203 | 0:3d9c67d97d6f | 600 | /* Apply loop unrolling and compute 4 MACs simultaneously. */ |
emh203 | 0:3d9c67d97d6f | 601 | k = count >> 2u; |
emh203 | 0:3d9c67d97d6f | 602 | |
emh203 | 0:3d9c67d97d6f | 603 | /* First part of the processing with loop unrolling. Compute 4 MACs at a time. |
emh203 | 0:3d9c67d97d6f | 604 | ** a second loop below computes MACs for the remaining 1 to 3 samples. */ |
emh203 | 0:3d9c67d97d6f | 605 | while(k > 0u) |
emh203 | 0:3d9c67d97d6f | 606 | { |
emh203 | 0:3d9c67d97d6f | 607 | /* Perform the multiply-accumulates */ |
emh203 | 0:3d9c67d97d6f | 608 | /* sum += x[srcALen - srcBLen + 4] * y[3] */ |
emh203 | 0:3d9c67d97d6f | 609 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 610 | /* sum += x[srcALen - srcBLen + 3] * y[2] */ |
emh203 | 0:3d9c67d97d6f | 611 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 612 | /* sum += x[srcALen - srcBLen + 2] * y[1] */ |
emh203 | 0:3d9c67d97d6f | 613 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 614 | /* sum += x[srcALen - srcBLen + 1] * y[0] */ |
emh203 | 0:3d9c67d97d6f | 615 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 616 | |
emh203 | 0:3d9c67d97d6f | 617 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 618 | k--; |
emh203 | 0:3d9c67d97d6f | 619 | } |
emh203 | 0:3d9c67d97d6f | 620 | |
emh203 | 0:3d9c67d97d6f | 621 | /* If the count is not a multiple of 4, compute any remaining MACs here. |
emh203 | 0:3d9c67d97d6f | 622 | ** No loop unrolling is used. */ |
emh203 | 0:3d9c67d97d6f | 623 | k = count % 0x4u; |
emh203 | 0:3d9c67d97d6f | 624 | |
emh203 | 0:3d9c67d97d6f | 625 | while(k > 0u) |
emh203 | 0:3d9c67d97d6f | 626 | { |
emh203 | 0:3d9c67d97d6f | 627 | /* Perform the multiply-accumulates */ |
emh203 | 0:3d9c67d97d6f | 628 | sum += *px++ * *py++; |
emh203 | 0:3d9c67d97d6f | 629 | |
emh203 | 0:3d9c67d97d6f | 630 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 631 | k--; |
emh203 | 0:3d9c67d97d6f | 632 | } |
emh203 | 0:3d9c67d97d6f | 633 | |
emh203 | 0:3d9c67d97d6f | 634 | /* Store the result in the accumulator in the destination buffer. */ |
emh203 | 0:3d9c67d97d6f | 635 | *pOut = sum; |
emh203 | 0:3d9c67d97d6f | 636 | /* Destination pointer is updated according to the address modifier, inc */ |
emh203 | 0:3d9c67d97d6f | 637 | pOut += inc; |
emh203 | 0:3d9c67d97d6f | 638 | |
emh203 | 0:3d9c67d97d6f | 639 | /* Update the inputA and inputB pointers for next MAC calculation */ |
emh203 | 0:3d9c67d97d6f | 640 | px = ++pSrc1; |
emh203 | 0:3d9c67d97d6f | 641 | py = pIn2; |
emh203 | 0:3d9c67d97d6f | 642 | |
emh203 | 0:3d9c67d97d6f | 643 | /* Decrement the MAC count */ |
emh203 | 0:3d9c67d97d6f | 644 | count--; |
emh203 | 0:3d9c67d97d6f | 645 | |
emh203 | 0:3d9c67d97d6f | 646 | /* Decrement the loop counter */ |
emh203 | 0:3d9c67d97d6f | 647 | blockSize3--; |
emh203 | 0:3d9c67d97d6f | 648 | } |
emh203 | 0:3d9c67d97d6f | 649 | |
emh203 | 0:3d9c67d97d6f | 650 | #else |
emh203 | 0:3d9c67d97d6f | 651 | |
emh203 | 0:3d9c67d97d6f | 652 | /* Run the below code for Cortex-M0 */ |
emh203 | 0:3d9c67d97d6f | 653 | |
emh203 | 0:3d9c67d97d6f | 654 | float32_t *pIn1 = pSrcA; /* inputA pointer */ |
emh203 | 0:3d9c67d97d6f | 655 | float32_t *pIn2 = pSrcB + (srcBLen - 1u); /* inputB pointer */ |
emh203 | 0:3d9c67d97d6f | 656 | float32_t sum; /* Accumulator */ |
emh203 | 0:3d9c67d97d6f | 657 | uint32_t i = 0u, j; /* loop counters */ |
emh203 | 0:3d9c67d97d6f | 658 | uint32_t inv = 0u; /* Reverse order flag */ |
emh203 | 0:3d9c67d97d6f | 659 | uint32_t tot = 0u; /* Length */ |
emh203 | 0:3d9c67d97d6f | 660 | |
emh203 | 0:3d9c67d97d6f | 661 | /* The algorithm implementation is based on the lengths of the inputs. */ |
emh203 | 0:3d9c67d97d6f | 662 | /* srcB is always made to slide across srcA. */ |
emh203 | 0:3d9c67d97d6f | 663 | /* So srcBLen is always considered as shorter or equal to srcALen */ |
emh203 | 0:3d9c67d97d6f | 664 | /* But CORR(x, y) is reverse of CORR(y, x) */ |
emh203 | 0:3d9c67d97d6f | 665 | /* So, when srcBLen > srcALen, output pointer is made to point to the end of the output buffer */ |
emh203 | 0:3d9c67d97d6f | 666 | /* and a varaible, inv is set to 1 */ |
emh203 | 0:3d9c67d97d6f | 667 | /* If lengths are not equal then zero pad has to be done to make the two |
emh203 | 0:3d9c67d97d6f | 668 | * inputs of same length. But to improve the performance, we assume zeroes |
emh203 | 0:3d9c67d97d6f | 669 | * in the output instead of zero padding either of the the inputs*/ |
emh203 | 0:3d9c67d97d6f | 670 | /* If srcALen > srcBLen, (srcALen - srcBLen) zeroes has to included in the |
emh203 | 0:3d9c67d97d6f | 671 | * starting of the output buffer */ |
emh203 | 0:3d9c67d97d6f | 672 | /* If srcALen < srcBLen, (srcALen - srcBLen) zeroes has to included in the |
emh203 | 0:3d9c67d97d6f | 673 | * ending of the output buffer */ |
emh203 | 0:3d9c67d97d6f | 674 | /* Once the zero padding is done the remaining of the output is calcualted |
emh203 | 0:3d9c67d97d6f | 675 | * using convolution but with the shorter signal time shifted. */ |
emh203 | 0:3d9c67d97d6f | 676 | |
emh203 | 0:3d9c67d97d6f | 677 | /* Calculate the length of the remaining sequence */ |
emh203 | 0:3d9c67d97d6f | 678 | tot = ((srcALen + srcBLen) - 2u); |
emh203 | 0:3d9c67d97d6f | 679 | |
emh203 | 0:3d9c67d97d6f | 680 | if(srcALen > srcBLen) |
emh203 | 0:3d9c67d97d6f | 681 | { |
emh203 | 0:3d9c67d97d6f | 682 | /* Calculating the number of zeros to be padded to the output */ |
emh203 | 0:3d9c67d97d6f | 683 | j = srcALen - srcBLen; |
emh203 | 0:3d9c67d97d6f | 684 | |
emh203 | 0:3d9c67d97d6f | 685 | /* Initialise the pointer after zero padding */ |
emh203 | 0:3d9c67d97d6f | 686 | pDst += j; |
emh203 | 0:3d9c67d97d6f | 687 | } |
emh203 | 0:3d9c67d97d6f | 688 | |
emh203 | 0:3d9c67d97d6f | 689 | else if(srcALen < srcBLen) |
emh203 | 0:3d9c67d97d6f | 690 | { |
emh203 | 0:3d9c67d97d6f | 691 | /* Initialization to inputB pointer */ |
emh203 | 0:3d9c67d97d6f | 692 | pIn1 = pSrcB; |
emh203 | 0:3d9c67d97d6f | 693 | |
emh203 | 0:3d9c67d97d6f | 694 | /* Initialization to the end of inputA pointer */ |
emh203 | 0:3d9c67d97d6f | 695 | pIn2 = pSrcA + (srcALen - 1u); |
emh203 | 0:3d9c67d97d6f | 696 | |
emh203 | 0:3d9c67d97d6f | 697 | /* Initialisation of the pointer after zero padding */ |
emh203 | 0:3d9c67d97d6f | 698 | pDst = pDst + tot; |
emh203 | 0:3d9c67d97d6f | 699 | |
emh203 | 0:3d9c67d97d6f | 700 | /* Swapping the lengths */ |
emh203 | 0:3d9c67d97d6f | 701 | j = srcALen; |
emh203 | 0:3d9c67d97d6f | 702 | srcALen = srcBLen; |
emh203 | 0:3d9c67d97d6f | 703 | srcBLen = j; |
emh203 | 0:3d9c67d97d6f | 704 | |
emh203 | 0:3d9c67d97d6f | 705 | /* Setting the reverse flag */ |
emh203 | 0:3d9c67d97d6f | 706 | inv = 1; |
emh203 | 0:3d9c67d97d6f | 707 | |
emh203 | 0:3d9c67d97d6f | 708 | } |
emh203 | 0:3d9c67d97d6f | 709 | |
emh203 | 0:3d9c67d97d6f | 710 | /* Loop to calculate convolution for output length number of times */ |
emh203 | 0:3d9c67d97d6f | 711 | for (i = 0u; i <= tot; i++) |
emh203 | 0:3d9c67d97d6f | 712 | { |
emh203 | 0:3d9c67d97d6f | 713 | /* Initialize sum with zero to carry on MAC operations */ |
emh203 | 0:3d9c67d97d6f | 714 | sum = 0.0f; |
emh203 | 0:3d9c67d97d6f | 715 | |
emh203 | 0:3d9c67d97d6f | 716 | /* Loop to perform MAC operations according to convolution equation */ |
emh203 | 0:3d9c67d97d6f | 717 | for (j = 0u; j <= i; j++) |
emh203 | 0:3d9c67d97d6f | 718 | { |
emh203 | 0:3d9c67d97d6f | 719 | /* Check the array limitations */ |
emh203 | 0:3d9c67d97d6f | 720 | if((((i - j) < srcBLen) && (j < srcALen))) |
emh203 | 0:3d9c67d97d6f | 721 | { |
emh203 | 0:3d9c67d97d6f | 722 | /* z[i] += x[i-j] * y[j] */ |
emh203 | 0:3d9c67d97d6f | 723 | sum += pIn1[j] * pIn2[-((int32_t) i - j)]; |
emh203 | 0:3d9c67d97d6f | 724 | } |
emh203 | 0:3d9c67d97d6f | 725 | } |
emh203 | 0:3d9c67d97d6f | 726 | /* Store the output in the destination buffer */ |
emh203 | 0:3d9c67d97d6f | 727 | if(inv == 1) |
emh203 | 0:3d9c67d97d6f | 728 | *pDst-- = sum; |
emh203 | 0:3d9c67d97d6f | 729 | else |
emh203 | 0:3d9c67d97d6f | 730 | *pDst++ = sum; |
emh203 | 0:3d9c67d97d6f | 731 | } |
emh203 | 0:3d9c67d97d6f | 732 | |
emh203 | 0:3d9c67d97d6f | 733 | #endif /* #ifndef ARM_MATH_CM0_FAMILY */ |
emh203 | 0:3d9c67d97d6f | 734 | |
emh203 | 0:3d9c67d97d6f | 735 | } |
emh203 | 0:3d9c67d97d6f | 736 | |
emh203 | 0:3d9c67d97d6f | 737 | /** |
emh203 | 0:3d9c67d97d6f | 738 | * @} end of Corr group |
emh203 | 0:3d9c67d97d6f | 739 | */ |