sse_intrin.h 12.9 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

knopp's avatar
knopp committed
22 23 24 25 26 27 28
/*! \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.
 *
29 30 31 32 33
 * \author S. Held, Laurent THOMAS
 * \email sebastian.held@imst.de, laurent.thomas@open-cells.com	
 * \company IMST GmbH, Open Cells Project
 * \date 2019
 * \version 0.2
knopp's avatar
knopp committed
34 35 36 37 38 39
*/

#ifndef SSE_INTRIN_H
#define SSE_INTRIN_H


knopp's avatar
knopp committed
40
#if defined(__x86_64) || defined(__i386__)
knopp's avatar
knopp committed
41 42

#ifndef __SSE2__
43
  #  error SSE2 processor intrinsics disabled
knopp's avatar
knopp committed
44 45 46 47 48 49
#endif

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

#ifdef __SSE3__
50 51
  #include <pmmintrin.h>
  #include <tmmintrin.h>
knopp's avatar
knopp committed
52 53 54
#endif

#ifdef __SSE4_1__
55
  #include <smmintrin.h>
knopp's avatar
knopp committed
56 57
#endif

58
#ifdef __AVX2__
59
  #include <immintrin.h>
60
#endif
knopp's avatar
knopp committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74

// ------------------------------------------------
// 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
 */
75 76 77 78 79 80
typedef union {
  __m128  f;
  __m128d d;
  __m128i i;
  __m64       m64[ 2];
  signed char  s8[16];
knopp's avatar
knopp committed
81 82 83 84 85 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
} 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
 */
111
static inline __m128i ssp_comge_epi8_SSE2(__m128i a, __m128i b) {
112 113 114 115 116
  __m128i c;
  c = _mm_cmpgt_epi8( a, b );
  a = _mm_cmpeq_epi8( a, b );
  a = _mm_or_si128  ( a, c );
  return a;
knopp's avatar
knopp committed
117 118 119 120 121 122 123 124 125 126 127
}

/*! \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
 */
128
static inline __m128i ssp_shuffle_epi8_SSE2 (__m128i a, __m128i mask) {
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  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;
knopp's avatar
knopp committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
}
#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
 */
181
static inline __m128i ssp_insert_epi8_SSE2( __m128i a, int b, const int ndx ) {
182 183 184 185 186 187 188
  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 ) {
189 190 191
    case 0:
      Alo.i = _mm_insert_epi16( Alo.i, b, 0 );
      break;
192

193 194 195
    case 1:
      Alo.i = _mm_insert_epi16( Alo.i, b, 1 );
      break;
196

197 198 199
    case 2:
      Alo.i = _mm_insert_epi16( Alo.i, b, 2 );
      break;
200

201 202 203
    case 3:
      Alo.i = _mm_insert_epi16( Alo.i, b, 3 );
      break;
204

205 206 207
    case 4:
      Alo.i = _mm_insert_epi16( Alo.i, b, 4 );
      break;
208

209 210 211
    case 5:
      Alo.i = _mm_insert_epi16( Alo.i, b, 5 );
      break;
212

213 214 215
    case 6:
      Alo.i = _mm_insert_epi16( Alo.i, b, 6 );
      break;
216

217 218 219
    case 7:
      Alo.i = _mm_insert_epi16( Alo.i, b, 7 );
      break;
220

221 222 223
    case 8:
      Ahi.i = _mm_insert_epi16( Ahi.i, b, 0 );
      break;
224

225 226 227
    case 9:
      Ahi.i = _mm_insert_epi16( Ahi.i, b, 1 );
      break;
228

229 230 231
    case 10:
      Ahi.i = _mm_insert_epi16( Ahi.i, b, 2 );
      break;
232

233 234 235
    case 11:
      Ahi.i = _mm_insert_epi16( Ahi.i, b, 3 );
      break;
236

237 238 239
    case 12:
      Ahi.i = _mm_insert_epi16( Ahi.i, b, 4 );
      break;
240

241 242 243
    case 13:
      Ahi.i = _mm_insert_epi16( Ahi.i, b, 5 );
      break;
244

245 246 247
    case 14:
      Ahi.i = _mm_insert_epi16( Ahi.i, b, 6 );
      break;
248

249 250
    default:
      Ahi.i = _mm_insert_epi16( Ahi.i, b, 7 );
251 252 253 254 255 256 257 258 259 260 261 262
  }

  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;
knopp's avatar
knopp committed
263 264 265 266 267 268 269 270 271 272 273
}

/*! \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
 */
274
static inline __m128i ssp_cvtepi8_epi16_SSE2 ( __m128i a) {
275 276 277 278 279 280 281
  __m128i b = _mm_setzero_si128 ();
  __m128i c = _mm_unpacklo_epi8(a, b);
  __m128i d = _mm_set1_epi16 (128);
  b = _mm_and_si128(d, c);
  d = _mm_set1_epi16(0x1FE);
  b = _mm_mullo_epi16(b, d);
  return _mm_add_epi16(c, b);
knopp's avatar
knopp committed
282 283 284 285 286 287 288 289 290 291 292
}

/*! \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
 */
293
static inline __m128i ssp_logical_bitwise_select_SSE2( __m128i a, __m128i b, __m128i mask ) { // Bitwise (mask ? a : b)
294 295 296 297
  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;
knopp's avatar
knopp committed
298 299 300 301 302 303 304 305 306 307 308
}

/*! \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
 */
309
static inline __m128i ssp_max_epi8_SSE2( __m128i a, __m128i b ) {
310 311 312
  __m128i mask  = _mm_cmpgt_epi8( a, b );                             // FFFFFFFF where a > b
  a = ssp_logical_bitwise_select_SSE2( a, b, mask );
  return a;
knopp's avatar
knopp committed
313 314 315 316 317 318 319 320 321 322 323
}

/*! \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
 */
324
static inline __m128i ssp_cvtepi16_epi32_SSE2 ( __m128i a) {
325 326 327 328 329 330
  __m128i b = _mm_set1_epi32 (-1);         //0xFFFFFFFF
  __m128i c = _mm_unpacklo_epi16(a, b);    //FFFFa0**FFFFa1**....
  __m128i d = _mm_set1_epi32 (0x8000);     //0x8000
  b = _mm_andnot_si128(c, d);              // 0x80 for positive, 0x00 for negative
  d = _mm_slli_epi32(b, 1);                // 0x100 for positive, 0x000 for negative
  return _mm_add_epi32(c, d);
knopp's avatar
knopp committed
331 332 333
}
#endif // __SSE4_1__

334 335 336
#elif defined(__arm__)
#include <arm_neon.h>

knopp's avatar
knopp committed
337
#endif // x86_64 || i386
knopp's avatar
knopp committed
338

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
#if defined(__x86_64__) || defined(__i386__)
  #define vect128 __m128i
#elif defined(__arm__)
  #define vect128 int16x8_t
#endif

static const short minusConjug128[8]__attribute__((aligned(16))) = {-1,1,-1,1,-1,1,-1,1};
static inline vect128 mulByConjugate128(vect128 *a, vect128 *b, int8_t output_shift) {

#if defined(__x86_64__) || defined(__i386__)
  vect128 realPart = _mm_madd_epi16(*a,*b);
  realPart = _mm_srai_epi32(realPart,output_shift);
  vect128 imagPart = _mm_shufflelo_epi16(*b,_MM_SHUFFLE(2,3,0,1));
  imagPart = _mm_shufflehi_epi16(imagPart,_MM_SHUFFLE(2,3,0,1));
  imagPart = _mm_sign_epi16(imagPart,*(vect128 *)minusConjug128);
  imagPart = _mm_madd_epi16(imagPart,*a);
  imagPart = _mm_srai_epi32(imagPart,output_shift);
  vect128 lowPart = _mm_unpacklo_epi32(realPart,imagPart);
  vect128 highPart = _mm_unpackhi_epi32(realPart,imagPart);
  return ( _mm_packs_epi32(lowPart,highPart));
#elif defined(__arm__)
  AssertFatal(false, "not developped\n");
#endif
}
knopp's avatar
knopp committed
363

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#if defined(__x86_64__) || defined(__i386__)
#define displaySamples128(vect)  {\
    __m128i x=vect;                                       \
    printf("vector: %s = (%hd,%hd) (%hd,%hd) (%hd,%hd) (%hd,%hd)\n", #vect, \
           _mm_extract_epi16(x,0),                                  \
           _mm_extract_epi16(x,1),\
           _mm_extract_epi16(x,2),\
           _mm_extract_epi16(x,3),\
           _mm_extract_epi16(x,4),\
           _mm_extract_epi16(x,5),\
           _mm_extract_epi16(x,6),\
           _mm_extract_epi16(x,7));\
  }
#elif defined(__arm__)
  displaySamples128(vect) {}
//TBD
#endif
#endif // SSE_INTRIN_H