sse_intrin.h 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 2015 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE

 *******************************************************************************/

/*! \file PHY/sse_intrin.h
 * \brief SSE includes and compatibility functions.
 *
 * This header collects all SSE compatibility functions. To use SSE inside a source file, include only sse_intrin.h.
 * The host CPU needs to have support for SSE2 at least. SSE3 and SSE4.1 functions are emulated if the CPU lacks support for them.
 * This will slow down the softmodem, but may be valuable if only offline signal processing is required.
 *
 * \author S. Held
 * \email sebastian.held@imst.de
 * \company IMST GmbH
 * \date 2015
 * \version 0.1
*/

#ifndef SSE_INTRIN_H
#define SSE_INTRIN_H


knopp's avatar
knopp committed
48
#if defined(__x86_64) || defined(__i386__)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

#ifndef __SSE2__
#  error SSE2 processor intrinsics disabled
#endif

#include <emmintrin.h>
#include <xmmintrin.h>

#ifdef __SSE3__
#  include <pmmintrin.h>
#  include <tmmintrin.h>
#endif

#ifdef __SSE4_1__
#  include <smmintrin.h>
#endif


// ------------------------------------------------
// compatibility functions if SSE3 or SSE4 is not available
// ------------------------------------------------
#if !defined(__SSE3__) || !defined(__SSE4_1__)
/*! \brief SSE vector type.
 *
 * this source code fragment is Copyright (c) 2006-2008 Advanced Micro Devices, Inc.
 * It is licensed under Apache License 2.0 (compatible to our GPL3).
 * see http://sourceforge.net/projects/sseplus
 * \author Advanced Micro Devices, Inc.
 * \date 2006-2008
 * \copyright Apache License 2.0
 */
80 81 82 83 84 85
typedef union {
  __m128  f;
  __m128d d;
  __m128i i;
  __m64       m64[ 2];
  signed char  s8[16];
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
} ssp_m128;
#endif


// ------------------------------------------------
// compatibility functions if SSE3 is not available
// ------------------------------------------------
#ifndef __SSE3__
#warning SSE3 instruction set not preset
#define _mm_abs_epi16(xmmx) _mm_xor_si128((xmmx),_mm_cmpgt_epi16(_mm_setzero_si128(),(xmmx)))
#define _mm_sign_epi16(xmmx,xmmy) _mm_xor_si128((xmmx),_mm_cmpgt_epi16(_mm_setzero_si128(),(xmmy)))
#define _mm_hadd_epi32(xmmx,xmmy) _mm_unpacklo_epi64(_mm_add_epi32(_mm_shuffle_epi32((xmmx),_MM_SHUFFLE(0,2,0,2)),_mm_shuffle_epi32((xmmx),_MM_SHUFFLE(1,3,1,3))),_mm_add_epi32(_mm_shuffle_epi32((xmmy),_MM_SHUFFLE(0,2,0,2)),_mm_shuffle_epi32((xmmy),_MM_SHUFFLE(1,3,1,3))))

// variant from lte_ul_channel_estimation.c and dlsch_demodulation.c and pmch.c
//#define _mm_abs_epi16(xmmx) _mm_add_epi16(_mm_xor_si128((xmmx),_mm_cmpgt_epi16(_mm_setzero_si128(),(xmmx))),_mm_srli_epi16(_mm_cmpgt_epi16(_mm_setzero_si128(),(xmmx)),15))

// variant from cdot_prod.c
//#define _mm_abs_epi16(xmmx) xmmx=_mm_xor_si128((xmmx),_mm_cmpgt_epi16(_mm_setzero_si128(),(xmmx)))

#define _mm_shuffle_epi8(xmmx,xmmy) ssp_shuffle_epi8_SSE2(xmmx,xmmy)

/*! \brief Helper function.
 *
 * this source code fragment is Copyright (c) 2006-2008 Advanced Micro Devices, Inc.
 * It is licensed under Apache License 2.0 (compatible to our GPL3).
 * see http://sourceforge.net/projects/sseplus
 * \author Advanced Micro Devices, Inc.
 * \date 2006-2008
 * \copyright Apache License 2.0
 */
static inline __m128i ssp_comge_epi8_SSE2(__m128i a, __m128i b)
{
118 119 120 121 122
  __m128i c;
  c = _mm_cmpgt_epi8( a, b );
  a = _mm_cmpeq_epi8( a, b );
  a = _mm_or_si128  ( a, c );
  return a;
123 124 125 126 127 128 129 130 131 132 133 134 135
}

/*! \brief SSE2 emulation of SSE3 _mm_shuffle_epi8().
 *
 * this source code fragment is Copyright (c) 2006-2008 Advanced Micro Devices, Inc.
 * It is licensed under Apache License 2.0 (compatible to our GPL3).
 * see http://sourceforge.net/projects/sseplus
 * \author Advanced Micro Devices, Inc.
 * \date 2006-2008
 * \copyright Apache License 2.0
 */
static inline __m128i ssp_shuffle_epi8_SSE2 (__m128i a, __m128i mask)
{
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  ssp_m128 A,B, MASK, maskZero;
  A.i        = a;
  maskZero.i = ssp_comge_epi8_SSE2( mask, _mm_setzero_si128()        );
  MASK.i     = _mm_and_si128      ( mask, _mm_set1_epi8( (char)0x0F) );

  B.s8[ 0] = A.s8[ (MASK.s8[ 0]) ];
  B.s8[ 1] = A.s8[ (MASK.s8[ 1]) ];
  B.s8[ 2] = A.s8[ (MASK.s8[ 2]) ];
  B.s8[ 3] = A.s8[ (MASK.s8[ 3]) ];
  B.s8[ 4] = A.s8[ (MASK.s8[ 4]) ];
  B.s8[ 5] = A.s8[ (MASK.s8[ 5]) ];
  B.s8[ 6] = A.s8[ (MASK.s8[ 6]) ];
  B.s8[ 7] = A.s8[ (MASK.s8[ 7]) ];
  B.s8[ 8] = A.s8[ (MASK.s8[ 8]) ];
  B.s8[ 9] = A.s8[ (MASK.s8[ 9]) ];
  B.s8[10] = A.s8[ (MASK.s8[10]) ];
  B.s8[11] = A.s8[ (MASK.s8[11]) ];
  B.s8[12] = A.s8[ (MASK.s8[12]) ];
  B.s8[13] = A.s8[ (MASK.s8[13]) ];
  B.s8[14] = A.s8[ (MASK.s8[14]) ];
  B.s8[15] = A.s8[ (MASK.s8[15]) ];

  B.i = _mm_and_si128( B.i, maskZero.i );
  return B.i;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
}
#endif // __SSE3__





// ------------------------------------------------
// compatibility functions if SSE4 is not available
// ------------------------------------------------
#ifndef __SSE4_1__
#warning SSE4_1 instruction set not preset

// https://gitorious.org/vc/vc/commit/ee49857ffe5b74c74bc57d501b05519443fc609a license LGPL3
#define _mm_extract_epi32(xmmx,index) _mm_cvtsi128_si32(_mm_srli_si128(xmmx, (index) * 4))

#define _mm_insert_epi8(x,y,z) ssp_insert_epi8_SSE2(x,y,z)
#define _mm_cvtepi8_epi16(x) ssp_cvtepi8_epi16_SSE2(x)
#define _mm_max_epi8(x,y) ssp_max_epi8_SSE2(x,y)
#define _mm_cvtepi16_epi32(x) ssp_cvtepi16_epi32_SSE2(x)

/*! \brief SSE2 emulation of SSE4 _mm_insert_epi8().
 *
 * this source code fragment is Copyright (c) 2006-2008 Advanced Micro Devices, Inc.
 * It is licensed under Apache License 2.0 (compatible to our GPL3).
 * see http://sourceforge.net/projects/sseplus
 * \author Advanced Micro Devices, Inc.
 * \date 2006-2008
 * \copyright Apache License 2.0
 */
static inline __m128i ssp_insert_epi8_SSE2( __m128i a, int b, const int ndx )
{
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  ssp_m128 Ahi, Alo;
  b = b & 0xFF;                                           /* Convert to 8-bit integer */
  Ahi.i = _mm_unpackhi_epi8( a, _mm_setzero_si128() );    /* Ahi = a_8[8:15]  Simulate 8bit integers as 16-bit integers */
  Alo.i = _mm_unpacklo_epi8( a, _mm_setzero_si128() );    /* Alo = a_8[0:7]   Simulate 8bit integers as 16-bit integers */

  /* Insert b as a 16-bit integer to upper or lower half of a */
  switch( ndx & 0xF ) {
  case 0:
    Alo.i = _mm_insert_epi16( Alo.i, b, 0 );
    break;

  case 1:
    Alo.i = _mm_insert_epi16( Alo.i, b, 1 );
    break;

  case 2:
    Alo.i = _mm_insert_epi16( Alo.i, b, 2 );
    break;

  case 3:
    Alo.i = _mm_insert_epi16( Alo.i, b, 3 );
    break;

  case 4:
    Alo.i = _mm_insert_epi16( Alo.i, b, 4 );
    break;

  case 5:
    Alo.i = _mm_insert_epi16( Alo.i, b, 5 );
    break;

  case 6:
    Alo.i = _mm_insert_epi16( Alo.i, b, 6 );
    break;

  case 7:
    Alo.i = _mm_insert_epi16( Alo.i, b, 7 );
    break;

  case 8:
    Ahi.i = _mm_insert_epi16( Ahi.i, b, 0 );
    break;

  case 9:
    Ahi.i = _mm_insert_epi16( Ahi.i, b, 1 );
    break;

  case 10:
    Ahi.i = _mm_insert_epi16( Ahi.i, b, 2 );
    break;

  case 11:
    Ahi.i = _mm_insert_epi16( Ahi.i, b, 3 );
    break;

  case 12:
    Ahi.i = _mm_insert_epi16( Ahi.i, b, 4 );
    break;

  case 13:
    Ahi.i = _mm_insert_epi16( Ahi.i, b, 5 );
    break;

  case 14:
    Ahi.i = _mm_insert_epi16( Ahi.i, b, 6 );
    break;

  default:
    Ahi.i = _mm_insert_epi16( Ahi.i, b, 7 );
  }

  return _mm_packus_epi16( Alo.i, Ahi.i ); // Pack the 16-bit integers to 8bit again.

  ///* Another implementation, but slower: */
  //ssp_m128 A, B, mask;
  //mask.i = _mm_setzero_si128();
  //mask.s8[ ndx & 0x0F ] = (ssp_s8)0xFF;
  //B.i    = _mm_set1_epi8( (ssp_s8)b );
  //A.i    = _mm_andnot_si128( mask.i, a );
  //mask.i = _mm_and_si128( mask.i, B.i );
  //A.i = _mm_or_si128( A.i, mask.i );
  //return A.i;
274 275 276 277 278 279 280 281 282 283 284 285 286
}

/*! \brief SSE2 emulation of SSE4 _mm_cvtepi8_epi16().
 *
 * this source code fragment is Copyright (c) 2006-2008 Advanced Micro Devices, Inc.
 * It is licensed under Apache License 2.0 (compatible to our GPL3).
 * see http://sourceforge.net/projects/sseplus
 * \author Advanced Micro Devices, Inc.
 * \date 2006-2008
 * \copyright Apache License 2.0
 */
static inline __m128i ssp_cvtepi8_epi16_SSE2 ( __m128i a)
{
287 288 289
  __m128i b = _mm_setzero_si128 ();
  __m128i c = _mm_unpacklo_epi8(a, b);
  __m128i d = _mm_set1_epi16 (128);
290

291 292 293
  b = _mm_and_si128(d, c);
  d = _mm_set1_epi16(0x1FE);
  b = _mm_mullo_epi16(b, d);
294

295
  return _mm_add_epi16(c, b);
296 297 298 299 300 301 302 303 304 305 306 307 308
}

/*! \brief Helper function.
 *
 * this source code fragment is Copyright (c) 2006-2008 Advanced Micro Devices, Inc.
 * It is licensed under Apache License 2.0 (compatible to our GPL3).
 * see http://sourceforge.net/projects/sseplus
 * \author Advanced Micro Devices, Inc.
 * \date 2006-2008
 * \copyright Apache License 2.0
 */
static inline __m128i ssp_logical_bitwise_select_SSE2( __m128i a, __m128i b, __m128i mask )   // Bitwise (mask ? a : b)
{
309 310 311 312
  a = _mm_and_si128   ( a,    mask );                                 // clear a where mask = 0
  b = _mm_andnot_si128( mask, b    );                                 // clear b where mask = 1
  a = _mm_or_si128    ( a,    b    );                                 // a = a OR b
  return a;
313 314 315 316 317 318 319 320 321 322 323 324 325
}

/*! \brief SSE2 emulation of SSE4 _mm_max_epi8().
 *
 * this source code fragment is Copyright (c) 2006-2008 Advanced Micro Devices, Inc.
 * It is licensed under Apache License 2.0 (compatible to our GPL3).
 * see http://sourceforge.net/projects/sseplus
 * \author Advanced Micro Devices, Inc.
 * \date 2006-2008
 * \copyright Apache License 2.0
 */
static inline __m128i ssp_max_epi8_SSE2( __m128i a, __m128i b )
{
326 327 328
  __m128i mask  = _mm_cmpgt_epi8( a, b );                             // FFFFFFFF where a > b
  a = ssp_logical_bitwise_select_SSE2( a, b, mask );
  return a;
329 330 331 332 333 334 335 336 337 338 339 340 341
}

/*! \brief SSE2 emulation of SSE4 _mm_cvtepi16_epi32().
 *
 * this source code fragment is Copyright (c) 2006-2008 Advanced Micro Devices, Inc.
 * It is licensed under Apache License 2.0 (compatible to our GPL3).
 * see http://sourceforge.net/projects/sseplus
 * \author Advanced Micro Devices, Inc.
 * \date 2006-2008
 * \copyright Apache License 2.0
 */
static inline __m128i ssp_cvtepi16_epi32_SSE2 ( __m128i a)
{
342 343 344
  __m128i b = _mm_set1_epi32 (-1);         //0xFFFFFFFF
  __m128i c = _mm_unpacklo_epi16(a, b);    //FFFFa0**FFFFa1**....
  __m128i d = _mm_set1_epi32 (0x8000);     //0x8000
345

346 347
  b = _mm_andnot_si128(c, d);              // 0x80 for positive, 0x00 for negative
  d = _mm_slli_epi32(b, 1);                // 0x100 for positive, 0x000 for negative
348

349
  return _mm_add_epi32(c, d);
350 351 352
}
#endif // __SSE4_1__

knopp's avatar
knopp committed
353
#endif // x86_64 || i386
354 355 356

#endif // SSE_INTRIN_H