Home | History | Annotate | Download | only in libspeex
      1 /* Copyright (C) 2007-2008 Jean-Marc Valin
      2  * Copyright (C) 2008 Thorvald Natvig
      3  */
      4 /**
      5    @file resample_sse.h
      6    @brief Resampler functions (SSE version)
      7 */
      8 /*
      9    Redistribution and use in source and binary forms, with or without
     10    modification, are permitted provided that the following conditions
     11    are met:
     12 
     13    - Redistributions of source code must retain the above copyright
     14    notice, this list of conditions and the following disclaimer.
     15 
     16    - Redistributions in binary form must reproduce the above copyright
     17    notice, this list of conditions and the following disclaimer in the
     18    documentation and/or other materials provided with the distribution.
     19 
     20    - Neither the name of the Xiph.org Foundation nor the names of its
     21    contributors may be used to endorse or promote products derived from
     22    this software without specific prior written permission.
     23 
     24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     25    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     26    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     27    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
     28    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     31    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     32    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35 */
     36 
     37 #include <xmmintrin.h>
     38 
     39 #define OVERRIDE_INNER_PRODUCT_SINGLE
     40 static inline float inner_product_single(const float *a, const float *b, unsigned int len)
     41 {
     42    int i;
     43    float ret;
     44    __m128 sum = _mm_setzero_ps();
     45    for (i=0;i<len;i+=8)
     46    {
     47       sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(a+i), _mm_loadu_ps(b+i)));
     48       sum = _mm_add_ps(sum, _mm_mul_ps(_mm_loadu_ps(a+i+4), _mm_loadu_ps(b+i+4)));
     49    }
     50    sum = _mm_add_ps(sum, _mm_movehl_ps(sum, sum));
     51    sum = _mm_add_ss(sum, _mm_shuffle_ps(sum, sum, 0x55));
     52    _mm_store_ss(&ret, sum);
     53    return ret;
     54 }
     55 
     56 #define OVERRIDE_INTERPOLATE_PRODUCT_SINGLE
     57 static inline float interpolate_product_single(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac) {
     58   int i;
     59   float ret;
     60   __m128 sum = _mm_setzero_ps();
     61   __m128 f = _mm_loadu_ps(frac);
     62   for(i=0;i<len;i+=2)
     63   {
     64     sum = _mm_add_ps(sum, _mm_mul_ps(_mm_load1_ps(a+i), _mm_loadu_ps(b+i*oversample)));
     65     sum = _mm_add_ps(sum, _mm_mul_ps(_mm_load1_ps(a+i+1), _mm_loadu_ps(b+(i+1)*oversample)));
     66   }
     67    sum = _mm_mul_ps(f, sum);
     68    sum = _mm_add_ps(sum, _mm_movehl_ps(sum, sum));
     69    sum = _mm_add_ss(sum, _mm_shuffle_ps(sum, sum, 0x55));
     70    _mm_store_ss(&ret, sum);
     71    return ret;
     72 }
     73 
     74 #ifdef _USE_SSE2
     75 #include <emmintrin.h>
     76 #define OVERRIDE_INNER_PRODUCT_DOUBLE
     77 
     78 static inline double inner_product_double(const float *a, const float *b, unsigned int len)
     79 {
     80    int i;
     81    double ret;
     82    __m128d sum = _mm_setzero_pd();
     83    __m128 t;
     84    for (i=0;i<len;i+=8)
     85    {
     86       t = _mm_mul_ps(_mm_loadu_ps(a+i), _mm_loadu_ps(b+i));
     87       sum = _mm_add_pd(sum, _mm_cvtps_pd(t));
     88       sum = _mm_add_pd(sum, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
     89 
     90       t = _mm_mul_ps(_mm_loadu_ps(a+i+4), _mm_loadu_ps(b+i+4));
     91       sum = _mm_add_pd(sum, _mm_cvtps_pd(t));
     92       sum = _mm_add_pd(sum, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
     93    }
     94    sum = _mm_add_sd(sum, (__m128d) _mm_movehl_ps((__m128) sum, (__m128) sum));
     95    _mm_store_sd(&ret, sum);
     96    return ret;
     97 }
     98 
     99 #define OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE
    100 static inline double interpolate_product_double(const float *a, const float *b, unsigned int len, const spx_uint32_t oversample, float *frac) {
    101   int i;
    102   double ret;
    103   __m128d sum;
    104   __m128d sum1 = _mm_setzero_pd();
    105   __m128d sum2 = _mm_setzero_pd();
    106   __m128 f = _mm_loadu_ps(frac);
    107   __m128d f1 = _mm_cvtps_pd(f);
    108   __m128d f2 = _mm_cvtps_pd(_mm_movehl_ps(f,f));
    109   __m128 t;
    110   for(i=0;i<len;i+=2)
    111   {
    112     t = _mm_mul_ps(_mm_load1_ps(a+i), _mm_loadu_ps(b+i*oversample));
    113     sum1 = _mm_add_pd(sum1, _mm_cvtps_pd(t));
    114     sum2 = _mm_add_pd(sum2, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
    115 
    116     t = _mm_mul_ps(_mm_load1_ps(a+i+1), _mm_loadu_ps(b+(i+1)*oversample));
    117     sum1 = _mm_add_pd(sum1, _mm_cvtps_pd(t));
    118     sum2 = _mm_add_pd(sum2, _mm_cvtps_pd(_mm_movehl_ps(t, t)));
    119   }
    120   sum1 = _mm_mul_pd(f1, sum1);
    121   sum2 = _mm_mul_pd(f2, sum2);
    122   sum = _mm_add_pd(sum1, sum2);
    123   sum = _mm_add_sd(sum, (__m128d) _mm_movehl_ps((__m128) sum, (__m128) sum));
    124   _mm_store_sd(&ret, sum);
    125   return ret;
    126 }
    127 
    128 #endif
    129