home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.68.zip / src / resid-fp / convolve-sse.cpp next >
C/C++ Source or Header  |  2009-01-03  |  3KB  |  77 lines

  1. //  ---------------------------------------------------------------------------
  2. //  This file is part of reSID, a MOS6581 SID emulator engine.
  3. //  Copyright (C) 2004  Dag Lem <resid@nimrod.no>
  4. //
  5. //  This program is free software; you can redistribute it and/or modify
  6. //  it under the terms of the GNU General Public License as published by
  7. //  the Free Software Foundation; either version 2 of the License, or
  8. //  (at your option) any later version.
  9. //
  10. //  This program is distributed in the hope that it will be useful,
  11. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //  GNU General Public License for more details.
  14. //
  15. //  You should have received a copy of the GNU General Public License
  16. //  along with this program; if not, write to the Free Software
  17. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. //  ---------------------------------------------------------------------------
  19.  
  20. #include "sid.h"
  21.  
  22. #if (RESID_USE_SSE==1)
  23.  
  24. #include <xmmintrin.h>
  25.  
  26. float convolve_sse(const float *a, const float *b, int n)
  27. {
  28.     float out = 0.f;
  29.     __m128 out4 = { 0, 0, 0, 0 };
  30.  
  31.     /* examine if we can use aligned loads on both pointers */
  32.     int diff = (int) (a - b) & 0xf;
  33.     /* long cast is no-op for x86-32, but x86-64 gcc needs 64 bit intermediate
  34.      * to convince compiler we mean this. */
  35.     unsigned int a_align = (unsigned int) (unsigned long) a & 0xf;
  36.  
  37.     /* advance if necessary. We can't let n fall < 0, so no while (n --). */
  38.     while (n > 0 && a_align != 0 && a_align != 16) {
  39.         out += (*(a ++)) * (*(b ++));
  40.         --n;
  41.         a_align += 4;
  42.     }
  43.  
  44.     int n4 = n / 4;
  45.     if (diff == 0) {
  46.         for (int i = 0; i < n4; i ++) {
  47.             out4 = _mm_add_ps(out4, _mm_mul_ps(_mm_load_ps(a), _mm_load_ps(b)));
  48.             a += 4;
  49.             b += 4;
  50.         }
  51.     } else {
  52.         /* XXX loadu is 4x slower than load, at least. We could at 4x memory
  53.          * use prepare versions of b aligned for any a alignment. We could
  54.          * also issue aligned loads and shuffle the halves at each iteration.
  55.          * Initial results indicate only very small improvements. */
  56.         for (int i = 0; i < n4; i ++) {
  57.             out4 = _mm_add_ps(out4, _mm_mul_ps(_mm_load_ps(a), _mm_loadu_ps(b)));
  58.             a += 4;
  59.             b += 4;
  60.         }
  61.     }
  62.  
  63.     out4 = _mm_add_ps(_mm_movehl_ps(out4, out4), out4);
  64.     out4 = _mm_add_ss(_mm_shuffle_ps(out4, out4, 1), out4);
  65.     float out_tmp;
  66.     _mm_store_ss(&out_tmp, out4);
  67.     out += out_tmp;
  68.  
  69.     n &= 3;
  70.     
  71.     while (n --)
  72.         out += (*(a ++)) * (*(b ++));
  73.  
  74.     return out;
  75. }
  76. #endif
  77.