Subversion Repositories dashGPS

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* ----------------------------------------------------------------------
  2.  * Project:      CMSIS DSP Library
  3.  * Title:        arm_mult_q15.c
  4.  * Description:  Q15 vector multiplication
  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 groupMath
  33.  */
  34.  
  35. /**
  36.  * @addtogroup BasicMult
  37.  * @{
  38.  */
  39.  
  40.  
  41. /**
  42.  * @brief           Q15 vector multiplication
  43.  * @param[in]       *pSrcA points to the first input vector
  44.  * @param[in]       *pSrcB points to the second input vector
  45.  * @param[out]      *pDst points to the output vector
  46.  * @param[in]       blockSize number of samples in each vector
  47.  * @return none.
  48.  *
  49.  * <b>Scaling and Overflow Behavior:</b>
  50.  * \par
  51.  * The function uses saturating arithmetic.
  52.  * Results outside of the allowable Q15 range [0x8000 0x7FFF] will be saturated.
  53.  */
  54.  
  55. void arm_mult_q15(
  56.   q15_t * pSrcA,
  57.   q15_t * pSrcB,
  58.   q15_t * pDst,
  59.   uint32_t blockSize)
  60. {
  61.   uint32_t blkCnt;                               /* loop counters */
  62.  
  63. #if defined (ARM_MATH_DSP)
  64.  
  65. /* Run the below code for Cortex-M4 and Cortex-M3 */
  66.   q31_t inA1, inA2, inB1, inB2;                  /* temporary input variables */
  67.   q15_t out1, out2, out3, out4;                  /* temporary output variables */
  68.   q31_t mul1, mul2, mul3, mul4;                  /* temporary variables */
  69.  
  70.   /* loop Unrolling */
  71.   blkCnt = blockSize >> 2U;
  72.  
  73.   /* First part of the processing with loop unrolling.  Compute 4 outputs at a time.
  74.    ** a second loop below computes the remaining 1 to 3 samples. */
  75.   while (blkCnt > 0U)
  76.   {
  77.     /* read two samples at a time from sourceA */
  78.     inA1 = *__SIMD32(pSrcA)++;
  79.     /* read two samples at a time from sourceB */
  80.     inB1 = *__SIMD32(pSrcB)++;
  81.     /* read two samples at a time from sourceA */
  82.     inA2 = *__SIMD32(pSrcA)++;
  83.     /* read two samples at a time from sourceB */
  84.     inB2 = *__SIMD32(pSrcB)++;
  85.  
  86.     /* multiply mul = sourceA * sourceB */
  87.     mul1 = (q31_t) ((q15_t) (inA1 >> 16) * (q15_t) (inB1 >> 16));
  88.     mul2 = (q31_t) ((q15_t) inA1 * (q15_t) inB1);
  89.     mul3 = (q31_t) ((q15_t) (inA2 >> 16) * (q15_t) (inB2 >> 16));
  90.     mul4 = (q31_t) ((q15_t) inA2 * (q15_t) inB2);
  91.  
  92.     /* saturate result to 16 bit */
  93.     out1 = (q15_t) __SSAT(mul1 >> 15, 16);
  94.     out2 = (q15_t) __SSAT(mul2 >> 15, 16);
  95.     out3 = (q15_t) __SSAT(mul3 >> 15, 16);
  96.     out4 = (q15_t) __SSAT(mul4 >> 15, 16);
  97.  
  98.     /* store the result */
  99. #ifndef ARM_MATH_BIG_ENDIAN
  100.  
  101.     *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
  102.     *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
  103.  
  104. #else
  105.  
  106.     *__SIMD32(pDst)++ = __PKHBT(out2, out1, 16);
  107.     *__SIMD32(pDst)++ = __PKHBT(out4, out3, 16);
  108.  
  109. #endif /* #ifndef ARM_MATH_BIG_ENDIAN */
  110.  
  111.     /* Decrement the blockSize loop counter */
  112.     blkCnt--;
  113.   }
  114.  
  115.   /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  116.    ** No loop unrolling is used. */
  117.   blkCnt = blockSize % 0x4U;
  118.  
  119. #else
  120.  
  121.   /* Run the below code for Cortex-M0 */
  122.  
  123.   /* Initialize blkCnt with number of samples */
  124.   blkCnt = blockSize;
  125.  
  126. #endif /* #if defined (ARM_MATH_DSP) */
  127.  
  128.  
  129.   while (blkCnt > 0U)
  130.   {
  131.     /* C = A * B */
  132.     /* Multiply the inputs and store the result in the destination buffer */
  133.     *pDst++ = (q15_t) __SSAT((((q31_t) (*pSrcA++) * (*pSrcB++)) >> 15), 16);
  134.  
  135.     /* Decrement the blockSize loop counter */
  136.     blkCnt--;
  137.   }
  138. }
  139.  
  140. /**
  141.  * @} end of BasicMult group
  142.  */
  143.