The Gaudi Framework  v33r2 (a6f0ec87)
instrset.h
Go to the documentation of this file.
1 /**************************** instrset.h **********************************
2  * Author: Agner Fog
3  * Date created: 2012-05-30
4  * Last modified: 2020-04-01
5  * Version: 2.01.02
6  * Project: vector class library
7  * Description:
8  * Header file for various compiler-specific tasks as well as common
9  * macros and templates. This file contains:
10  *
11  * > Selection of the supported instruction set
12  * > Defines compiler version macros
13  * > Undefines certain macros that prevent function overloading
14  * > Helper functions that depend on instruction set, compiler, or platform
15  * > Common templates for permute, blend, etc.
16  *
17  * For instructions, see vcl_manual.pdf
18  *
19  * (c) Copyright 2012-2020 Agner Fog.
20  * Apache License version 2.0 or later.
21  ******************************************************************************/
22 
23 #ifndef INSTRSET_H
24 #define INSTRSET_H 20102
25 
26 // Allow the use of floating point permute instructions on integer vectors.
27 // Some CPU's have an extra latency of 1 or 2 clock cycles for this, but
28 // it may still be faster than alternative implementations:
29 #define ALLOW_FP_PERMUTE true
30 
31 // Macro to indicate 64 bit mode
32 #if ( defined( _M_AMD64 ) || defined( _M_X64 ) || defined( __amd64 ) ) && !defined( __x86_64__ )
33 # define __x86_64__ 1 // There are many different macros for this, decide on only one
34 #endif
35 
36 // The following values of INSTRSET are currently defined:
37 // 2: SSE2
38 // 3: SSE3
39 // 4: SSSE3
40 // 5: SSE4.1
41 // 6: SSE4.2
42 // 7: AVX
43 // 8: AVX2
44 // 9: AVX512F
45 // 10: AVX512BW/DQ/VL
46 // In the future, INSTRSET = 11 may include AVX512VBMI and AVX512VBMI2, but this
47 // decision cannot be made before the market situation for CPUs with these
48 // instruction sets is known (these future instruction set extensions are already
49 // used in some VCL functions and tested with an emulator)
50 
51 // Find instruction set from compiler macros if INSTRSET is not defined.
52 // Note: Most of these macros are not defined in Microsoft compilers
53 #ifndef INSTRSET
54 # if defined( __AVX512VL__ ) && defined( __AVX512BW__ ) && defined( __AVX512DQ__ )
55 # define INSTRSET 10
56 # elif defined( __AVX512F__ ) || defined( __AVX512__ )
57 # define INSTRSET 9
58 # elif defined( __AVX2__ )
59 # define INSTRSET 8
60 # elif defined( __AVX__ )
61 # define INSTRSET 7
62 # elif defined( __SSE4_2__ )
63 # define INSTRSET 6
64 # elif defined( __SSE4_1__ )
65 # define INSTRSET 5
66 # elif defined( __SSSE3__ )
67 # define INSTRSET 4
68 # elif defined( __SSE3__ )
69 # define INSTRSET 3
70 # elif defined( __SSE2__ ) || defined( __x86_64__ )
71 # define INSTRSET 2
72 # elif defined( __SSE__ )
73 # define INSTRSET 1
74 # elif defined( _M_IX86_FP ) // Defined in MS compiler. 1: SSE, 2: SSE2
75 # define INSTRSET _M_IX86_FP
76 # else
77 # define INSTRSET 0
78 # endif // instruction set defines
79 #endif // INSTRSET
80 
81 // Include the appropriate header file for intrinsic functions
82 #if INSTRSET > 7 // AVX2 and later
83 # if defined( __GNUC__ ) && !defined( __INTEL_COMPILER )
84 # include <x86intrin.h> // x86intrin.h includes header files for whatever instruction
85  // sets are specified on the compiler command line, such as:
86  // xopintrin.h, fma4intrin.h
87 # else
88 # include <immintrin.h> // MS/Intel version of immintrin.h covers AVX and later
89 # endif // __GNUC__
90 #elif INSTRSET == 7
91 # include <immintrin.h> // AVX
92 #elif INSTRSET == 6
93 # include <nmmintrin.h> // SSE4.2
94 #elif INSTRSET == 5
95 # include <smmintrin.h> // SSE4.1
96 #elif INSTRSET == 4
97 # include <tmmintrin.h> // SSSE3
98 #elif INSTRSET == 3
99 # include <pmmintrin.h> // SSE3
100 #elif INSTRSET == 2
101 # include <emmintrin.h> // SSE2
102 #elif INSTRSET == 1
103 # include <xmmintrin.h> // SSE
104 #endif // INSTRSET
105 
106 #if INSTRSET >= 8 && !defined( __FMA__ )
107 // Assume that all processors that have AVX2 also have FMA3
108 # if defined( __GNUC__ ) && !defined( __INTEL_COMPILER )
109 // Prevent error message in g++ and Clang when using FMA intrinsics with avx2:
110 # if !defined( DISABLE_WARNING_AVX2_WITHOUT_FMA )
111 # pragma message "It is recommended to specify also option -mfma when using -mavx2 or higher"
112 # endif
113 # elif !defined( __clang__ )
114 # define __FMA__ 1
115 # endif
116 #endif
117 
118 // AMD instruction sets
119 #if defined( __XOP__ ) || defined( __FMA4__ )
120 # ifdef __GNUC__
121 # include <x86intrin.h> // AMD XOP (Gnu)
122 # else
123 # include <ammintrin.h> // AMD XOP (Microsoft)
124 # endif // __GNUC__
125 #elif defined( __SSE4A__ ) // AMD SSE4A
126 # include <ammintrin.h>
127 #endif // __XOP__
128 
129 // FMA3 instruction set
130 #if defined( __FMA__ ) && ( defined( __GNUC__ ) || defined( __clang__ ) ) && !defined( __INTEL_COMPILER )
131 # include <fmaintrin.h>
132 #endif // __FMA__
133 
134 // FMA4 instruction set
135 #if defined( __FMA4__ ) && ( defined( __GNUC__ ) || defined( __clang__ ) )
136 # include <fma4intrin.h> // must have both x86intrin.h and fma4intrin.h, don't know why
137 #endif // __FMA4__
138 
139 #include <stdint.h> // Define integer types with known size
140 #include <stdlib.h> // define abs(int)
141 
142 #ifdef _MSC_VER // Microsoft compiler or compatible Intel compiler
143 # include <intrin.h> // define _BitScanReverse(int), __cpuid(int[4],int), _xgetbv(int)
144 #endif // _MSC_VER
145 
146 // functions in instrset_detect.cpp:
147 #ifdef VCL_NAMESPACE
148 namespace VCL_NAMESPACE {
149 #endif
150  int instrset_detect( void ); // tells which instruction sets are supported
151  bool hasFMA3( void ); // true if FMA3 instructions supported
152  bool hasFMA4( void ); // true if FMA4 instructions supported
153  bool hasXOP( void ); // true if XOP instructions supported
154  bool hasAVX512ER( void ); // true if AVX512ER instructions supported
155  bool hasAVX512VBMI( void ); // true if AVX512VBMI instructions supported
156  bool hasAVX512VBMI2( void ); // true if AVX512VBMI2 instructions supported
157 #ifdef VCL_NAMESPACE
158 }
159 #endif
160 
161 // functions in physical_processors.cpp:
162 int physicalProcessors( int* logical_processors = 0 );
163 
164 // GCC version
165 #if defined( __GNUC__ ) && !defined( GCC_VERSION ) && !defined( __clang__ )
166 # define GCC_VERSION ( (__GNUC__)*10000 + (__GNUC_MINOR__)*100 + ( __GNUC_PATCHLEVEL__ ) )
167 #endif
168 
169 // Clang version
170 #if defined( __clang__ )
171 # define CLANG_VERSION ( (__clang_major__)*10000 + (__clang_minor__)*100 + ( __clang_patchlevel__ ) )
172 // Problem: The version number is not consistent across platforms
173 // http://llvm.org/bugs/show_bug.cgi?id=12643
174 // Apple bug 18746972
175 #endif
176 
177 // Fix problem with non-overloadable macros named min and max in WinDef.h
178 #ifdef _MSC_VER
179 # if defined( _WINDEF_ ) && defined( min ) && defined( max )
180 # undef min
181 # undef max
182 # endif
183 # ifndef NOMINMAX
184 # define NOMINMAX
185 # endif
186 #endif
187 
188 /* Intel compiler problem:
189 The Intel compiler currently cannot compile version 2.00 of VCL. It seems to have
190 a problem with constexpr function returns not being constant enough.
191 */
192 #if defined( __INTEL_COMPILER ) && __INTEL_COMPILER < 9999
193 # error The Intel compiler version 19.00 cannot compile VCL version 2. Use Version 1.xx of VCL instead
194 #endif
195 
196 /* Clang problem:
197 The Clang compiler treats the intrinsic vector types __m128, __m128i, and __m128d as identical.
198 See the bug report at https://bugs.llvm.org/show_bug.cgi?id=17164
199 Additional problem: The version number is not consistent across platforms. The Apple build has
200 different version numbers. We have to rely on __apple_build_version__ on the Mac platform:
201 http://llvm.org/bugs/show_bug.cgi?id=12643
202 We have to make switches here when - hopefully - the error some day has been fixed.
203 We need different version checks with and whithout __apple_build_version__
204 */
205 #if ( defined( __clang__ ) || defined( __apple_build_version__ ) ) && !defined( __INTEL_COMPILER )
206 # define FIX_CLANG_VECTOR_ALIAS_AMBIGUITY
207 #endif
208 
209 #if defined( GCC_VERSION ) && GCC_VERSION < 99999 && !defined( __clang__ )
210 # define ZEXT_MISSING // Gcc 7.4.0 does not have _mm256_zextsi128_si256 and similar functions
211 #endif
212 
213 #ifdef VCL_NAMESPACE
214 namespace VCL_NAMESPACE {
215 #endif
216 
217  // Constant for indicating don't care in permute and blend functions.
218  // V_DC is -256 in Vector class library version 1.xx
219  // V_DC can be any value less than -1 in Vector class library version 2.00
220  constexpr int V_DC = -256;
221 
222  /*****************************************************************************
223  *
224  * Helper functions that depend on instruction set, compiler, or platform
225  *
226  *****************************************************************************/
227 
228  // Define interface to cpuid instruction.
229  // input: functionnumber = leaf (eax), ecxleaf = subleaf(ecx)
230  // output: output[0] = eax, output[1] = ebx, output[2] = ecx, output[3] = edx
231  static inline void cpuid( int output[4], int functionnumber, int ecxleaf = 0 ) {
232 #if defined( __GNUC__ ) || defined( __clang__ ) // use inline assembly, Gnu/AT&T syntax
233  int a, b, c, d;
234  __asm( "cpuid" : "=a"( a ), "=b"( b ), "=c"( c ), "=d"( d ) : "a"( functionnumber ), "c"( ecxleaf ) : );
235  output[0] = a;
236  output[1] = b;
237  output[2] = c;
238  output[3] = d;
239 
240 #elif defined( _MSC_VER ) // Microsoft compiler, intrin.h included
241  __cpuidex( output, functionnumber, ecxleaf ); // intrinsic function for CPUID
242 
243 #else // unknown platform. try inline assembly with masm/intel syntax
244  __asm {
245  mov eax, functionnumber
246  mov ecx, ecxleaf
247  cpuid;
248  mov esi, output
249  mov[esi], eax
250  mov[esi + 4], ebx
251  mov[esi + 8], ecx
252  mov[esi + 12], edx
253  }
254 #endif
255  }
256 
257 // Define popcount function. Gives sum of bits
258 #if INSTRSET >= 6 // SSE4.2
259  // popcnt instruction is not officially part of the SSE4.2 instruction set,
260  // but available in all known processors with SSE4.2
261  static inline uint32_t vml_popcnt( uint32_t a ) {
262  return (uint32_t)_mm_popcnt_u32( a ); // Intel intrinsic. Supported by gcc and clang
263  }
264 # ifdef __x86_64__
265  static inline int64_t vml_popcnt( uint64_t a ) {
266  return _mm_popcnt_u64( a ); // Intel intrinsic.
267  }
268 # else // 32 bit mode
269  static inline int64_t vml_popcnt( uint64_t a ) {
270  return _mm_popcnt_u32( uint32_t( a >> 32 ) ) + _mm_popcnt_u32( uint32_t( a ) );
271  }
272 # endif
273 #else // no SSE4.2
274 static inline uint32_t vml_popcnt( uint32_t a ) {
275  // popcnt instruction not available
276  uint32_t b = a - ( ( a >> 1 ) & 0x55555555 );
277  uint32_t c = ( b & 0x33333333 ) + ( ( b >> 2 ) & 0x33333333 );
278  uint32_t d = ( c + ( c >> 4 ) ) & 0x0F0F0F0F;
279  uint32_t e = d * 0x01010101;
280  return e >> 24;
281 }
282 
283 static inline int32_t vml_popcnt( uint64_t a ) {
284  return vml_popcnt( uint32_t( a >> 32 ) ) + vml_popcnt( uint32_t( a ) );
285 }
286 
287 #endif
288 
289 // Define bit-scan-forward function. Gives index to lowest set bit
290 #if defined( __GNUC__ ) || defined( __clang__ )
291  // gcc and Clang have no bit_scan_forward intrinsic
292 # if defined( __clang__ ) // fix clang bug
293  // Clang uses a k register as parameter a when inlined from horizontal_find_first
294  __attribute__( ( noinline ) )
295 # endif
296  static uint32_t
297  bit_scan_forward( uint32_t a ) {
298  uint32_t r;
299  __asm( "bsfl %1, %0" : "=r"( r ) : "r"( a ) : );
300  return r;
301  }
302  static inline uint32_t bit_scan_forward( uint64_t a ) {
303  uint32_t lo = uint32_t( a );
304  if ( lo ) return bit_scan_forward( lo );
305  uint32_t hi = uint32_t( a >> 32 );
306  return bit_scan_forward( hi ) + 32;
307  }
308 
309 #else // other compilers
310 static inline uint32_t bit_scan_forward( uint32_t a ) {
311  unsigned long r;
312  _BitScanForward( &r, a ); // defined in intrin.h for MS and Intel compilers
313  return r;
314 }
315 # ifdef __x86_64__
316 static inline uint32_t bit_scan_forward( uint64_t a ) {
317  unsigned long r;
318  _BitScanForward64( &r, a ); // defined in intrin.h for MS and Intel compilers
319  return (uint32_t)r;
320 }
321 # else
322 static inline uint32_t bit_scan_forward( uint64_t a ) {
323  uint32_t lo = uint32_t( a );
324  if ( lo ) return bit_scan_forward( lo );
325  uint32_t hi = uint32_t( a >> 32 );
326  return bit_scan_forward( hi ) + 32;
327 }
328 # endif
329 #endif
330 
331 // Define bit-scan-reverse function. Gives index to highest set bit = floor(log2(a))
332 #if defined( __GNUC__ ) || defined( __clang__ )
333  static inline uint32_t bit_scan_reverse( uint32_t a ) __attribute__( ( pure ) );
334  static inline uint32_t bit_scan_reverse( uint32_t a ) {
335  uint32_t r;
336  __asm( "bsrl %1, %0" : "=r"( r ) : "r"( a ) : );
337  return r;
338  }
339 # ifdef __x86_64__
340  static inline uint32_t bit_scan_reverse( uint64_t a ) {
341  uint64_t r;
342  __asm( "bsrq %1, %0" : "=r"( r ) : "r"( a ) : );
343  return r;
344  }
345 # else // 32 bit mode
346  static inline uint32_t bit_scan_reverse( uint64_t a ) {
347  uint64_t ahi = a >> 32;
348  if ( ahi == 0 )
349  return bit_scan_reverse( uint32_t( a ) );
350  else
351  return bit_scan_reverse( uint32_t( ahi ) ) + 32;
352  }
353 # endif
354 #else
355 static inline uint32_t bit_scan_reverse( uint32_t a ) {
356  unsigned long r;
357  _BitScanReverse( &r, a ); // defined in intrin.h for MS and Intel compilers
358  return r;
359 }
360 # ifdef __x86_64__
361 static inline uint32_t bit_scan_reverse( uint64_t a ) {
362  unsigned long r;
363  _BitScanReverse64( &r, a ); // defined in intrin.h for MS and Intel compilers
364  return r;
365 }
366 # else // 32 bit mode
367 static inline uint32_t bit_scan_reverse( uint64_t a ) {
368  uint64_t ahi = a >> 32;
369  if ( ahi == 0 )
370  return bit_scan_reverse( uint32_t( a ) );
371  else
372  return bit_scan_reverse( uint32_t( ahi ) ) + 32;
373 }
374 # endif
375 #endif
376 
377  // Same function, for compile-time constants
378  constexpr int bit_scan_reverse_const( uint64_t const n ) {
379  if ( n == 0 ) return -1;
380  uint64_t a = n, b = 0, j = 64, k = 0;
381  do {
382  j >>= 1;
383  k = (uint64_t)1 << j;
384  if ( a >= k ) {
385  a >>= j;
386  b += j;
387  }
388  } while ( j > 0 );
389  return int( b );
390  }
391 
392  /*****************************************************************************
393  *
394  * Common templates
395  *
396  *****************************************************************************/
397 
398  // Template class to represent compile-time integer constant
399  template <int32_t n>
400  class Const_int_t {}; // represent compile-time signed integer constant
401  template <uint32_t n>
402  class Const_uint_t {}; // represent compile-time unsigned integer constant
403 #define const_int( n ) ( Const_int_t<n>() ) // n must be compile-time integer constant
404 #define const_uint( n ) ( Const_uint_t<n>() ) // n must be compile-time unsigned integer constant
405 
406  // template for producing quiet NAN
407  template <class VTYPE>
408  static inline VTYPE nan_vec( uint32_t payload = 0x100 ) {
409  if constexpr ( ( VTYPE::elementtype() & 1 ) != 0 ) { // double
410  union {
411  uint64_t q;
412  double f;
413  } ud;
414  // n is left justified to avoid loss of NAN payload when converting to float
415  ud.q = 0x7FF8000000000000 | uint64_t( payload ) << 29;
416  return VTYPE( ud.f );
417  }
418  // float will be converted to double if necessary
419  union {
420  uint32_t i;
421  float f;
422  } uf;
423  uf.i = 0x7FC00000 | ( payload & 0x003FFFFF );
424  return VTYPE( uf.f );
425  }
426 
427  // Test if a parameter is a compile-time constant
428  /* Unfortunately, this works only for macro parameters, not for inline function parameters.
429  I hope that some solution will appear in the future, but for now it appears to be
430  impossible to check if a function parameter is a compile-time constant.
431  This would be useful in operator / and in function pow:
432  #if defined(__GNUC__) || defined (__clang__)
433  #define is_constant(a) __builtin_constant_p(a)
434  #else
435  #define is_constant(a) false
436  #endif
437  */
438 
439  /*****************************************************************************
440  *
441  * Helper functions for permute and blend functions
442  *
443  ******************************************************************************
444  Rules for constexpr functions:
445 
446  > All variable declarations must include initialization
447 
448  > Do not put variable declarations inside a for-clause, e.g. avoid: for (int i=0; ..
449  Instead, you have to declare the loop counter before the for-loop.
450 
451  > Do not make constexpr functions that return vector types. This requires type
452  punning with a union, which is not allowed in constexpr functions under C++17.
453  It may be possible under C++20
454 
455  *****************************************************************************/
456 
457  // Define type for Encapsulated array to use as return type:
458  template <typename T, int N>
459  struct EList {
460  T a[N];
461  };
462 
463  // get_inttype: get an integer of a size that matches the element size
464  // of vector class V with the value -1
465  template <typename V>
466  constexpr auto get_inttype() {
467  constexpr int elementsize = sizeof( V ) / V::size(); // size of vector elements
468 
469  if constexpr ( elementsize >= 8 ) {
470  return -int64_t( 1 );
471  } else if constexpr ( elementsize >= 4 ) {
472  return int32_t( -1 );
473  } else if constexpr ( elementsize >= 2 ) {
474  return int16_t( -1 );
475  } else {
476  return int8_t( -1 );
477  }
478  }
479 
480  // zero_mask: return a compact bit mask mask for zeroing using AVX512 mask.
481  // Parameter a is a reference to a constexpr int array of permutation indexes
482  template <int N>
483  constexpr auto zero_mask( int const ( &a )[N] ) {
484  uint64_t mask = 0;
485  int i = 0;
486 
487  for ( i = 0; i < N; i++ ) {
488  if ( a[i] >= 0 ) mask |= uint64_t( 1 ) << i;
489  }
490  if constexpr ( N <= 8 )
491  return uint8_t( mask );
492  else if constexpr ( N <= 16 )
493  return uint16_t( mask );
494  else if constexpr ( N <= 32 )
495  return uint32_t( mask );
496  else
497  return mask;
498  }
499 
500  // zero_mask_broad: return a broad byte mask for zeroing.
501  // Parameter a is a reference to a constexpr int array of permutation indexes
502  template <typename V>
503  constexpr auto zero_mask_broad( int const ( &A )[V::size()] ) {
504  constexpr int N = V::size(); // number of vector elements
505  typedef decltype( get_inttype<V>() ) Etype; // element type
506  EList<Etype, N> u = {{0}}; // list for return
507  int i = 0;
508  for ( i = 0; i < N; i++ ) { u.a[i] = A[i] >= 0 ? get_inttype<V>() : 0; }
509  return u; // return encapsulated array
510  }
511 
512  // make_bit_mask: return a compact mask of bits from a list of N indexes:
513  // B contains options indicating how to gather the mask
514  // bit 0-7 in B indicates which bit in each index to collect
515  // bit 8 = 0x100: set 1 in the lower half of the bit mask if the indicated bit is 1.
516  // bit 8 = 0 : set 1 in the lower half of the bit mask if the indicated bit is 0.
517  // bit 9 = 0x200: set 1 in the upper half of the bit mask if the indicated bit is 1.
518  // bit 9 = 0 : set 1 in the upper half of the bit mask if the indicated bit is 0.
519  // bit 10 = 0x400: set 1 in the bit mask if the corresponding index is -1 or V_DC
520  // Parameter a is a reference to a constexpr int array of permutation indexes
521  template <int N, int B>
522  constexpr uint64_t make_bit_mask( int const ( &a )[N] ) {
523  uint64_t r = 0; // return value
524  uint8_t j = uint8_t( B & 0xFF ); // index to selected bit
525  uint64_t s = 0; // bit number i in r
526  uint64_t f = 0; // 1 if bit not flipped
527  int i = 0;
528  for ( i = 0; i < N; i++ ) {
529  int ix = a[i];
530  if ( ix < 0 ) { // -1 or V_DC
531  s = ( B >> 10 ) & 1;
532  } else {
533  s = ( (uint32_t)ix >> j ) & 1; // extract selected bit
534  if ( i < N / 2 ) {
535  f = ( B >> 8 ) & 1; // lower half
536  } else {
537  f = ( B >> 9 ) & 1; // upper half
538  }
539  s ^= f ^ 1; // flip bit if needed
540  }
541  r |= uint64_t( s ) << i; // set bit in return value
542  }
543  return r;
544  }
545 
546  // make_broad_mask: Convert a bit mask m to a broad mask
547  // The return value will be a broad boolean mask with elementsize matching vector class V
548  template <typename V>
549  constexpr auto make_broad_mask( uint64_t const m ) {
550  constexpr int N = V::size(); // number of vector elements
551  typedef decltype( get_inttype<V>() ) Etype; // element type
552  EList<Etype, N> u = {{0}}; // list for returning
553  int i = 0;
554  for ( i = 0; i < N; i++ ) { u.a[i] = ( ( m >> i ) & 1 ) != 0 ? get_inttype<V>() : 0; }
555  return u; // return encapsulated array
556  }
557 
558  // perm_mask_broad: return a mask for permutation by a vector register index.
559  // Parameter A is a reference to a constexpr int array of permutation indexes
560  template <typename V>
561  constexpr auto perm_mask_broad( int const ( &A )[V::size()] ) {
562  constexpr int N = V::size(); // number of vector elements
563  typedef decltype( get_inttype<V>() ) Etype; // vector element type
564  EList<Etype, N> u = {{0}}; // list for returning
565  int i = 0;
566  for ( i = 0; i < N; i++ ) { u.a[i] = Etype( A[i] ); }
567  return u; // return encapsulated array
568  }
569 
570  // perm_flags: returns information about how a permute can be implemented.
571  // The return value is composed of these flag bits:
572  const int perm_zeroing = 1; // needs zeroing
573  const int perm_perm = 2; // permutation needed
574  const int perm_allzero = 4; // all is zero or don't care
575  const int perm_largeblock = 8; // fits permute with a larger block size (e.g permute Vec2q instead of Vec4i)
576  const int perm_addz = 0x10; // additional zeroing needed after permute with larger block size or shift
577  const int perm_addz2 = 0x20; // additional zeroing needed after perm_zext, perm_compress, or perm_expand
578  const int perm_cross_lane = 0x40; // permutation crossing 128-bit lanes
579  const int perm_same_pattern = 0x80; // same permute pattern in all 128-bit lanes
580  const int perm_punpckh = 0x100; // permutation pattern fits punpckh instruction
581  const int perm_punpckl = 0x200; // permutation pattern fits punpckl instruction
582  const int perm_rotate =
583  0x400; // permutation pattern fits rotation within lanes. 4 bit count returned in bit perm_rot_count
584  const int perm_shright =
585  0x1000; // permutation pattern fits shift right within lanes. 4 bit count returned in bit perm_rot_count
586  const int perm_shleft =
587  0x2000; // permutation pattern fits shift left within lanes. negative count returned in bit perm_rot_count
588  const int perm_rotate_big =
589  0x4000; // permutation pattern fits rotation across lanes. 6 bit count returned in bit perm_rot_count
590  const int perm_broadcast = 0x8000; // permutation pattern fits broadcast of a single element.
591  const int perm_zext = 0x10000; // permutation pattern fits zero extension
592  const int perm_compress = 0x20000; // permutation pattern fits vpcompress instruction
593  const int perm_expand = 0x40000; // permutation pattern fits vpexpand instruction
594  const int perm_outofrange = 0x10000000; // index out of range
595  const int perm_rot_count = 32; // rotate or shift count is in bits perm_rot_count to perm_rot_count+3
596  const int perm_ipattern =
597  40; // pattern for pshufd is in bit perm_ipattern to perm_ipattern + 7 if perm_same_pattern and elementsize >= 4
598 
599  template <typename V>
600  constexpr uint64_t perm_flags( int const ( &a )[V::size()] ) {
601  // a is a reference to a constexpr array of permutation indexes
602  // V is a vector class
603  constexpr int N = V::size(); // number of elements
604  uint64_t r = perm_largeblock | perm_same_pattern | perm_allzero; // return value
605  uint32_t i = 0; // loop counter
606  int j = 0; // loop counter
607  int ix = 0; // index number i
608  const uint32_t nlanes = sizeof( V ) / 16; // number of 128-bit lanes
609  const uint32_t lanesize = N / nlanes; // elements per lane
610  const uint32_t elementsize = sizeof( V ) / N; // size of each vector element
611  uint32_t lane = 0; // current lane
612  uint32_t rot = 999; // rotate left count
613  int32_t broadc = 999; // index to broadcasted element
614  uint32_t patfail = 0; // remember certain patterns that do not fit
615  uint32_t addz2 = 0; // remember certain patterns need extra zeroing
616  int32_t compresslasti = -1; // last index in perm_compress fit
617  int32_t compresslastp = -1; // last position in perm_compress fit
618  int32_t expandlasti = -1; // last index in perm_expand fit
619  int32_t expandlastp = -1; // last position in perm_expand fit
620 
621  int lanepattern[lanesize] = {0}; // pattern in each lane
622 
623  for ( i = 0; i < N; i++ ) { // loop through indexes
624  ix = a[i]; // current index
625  // meaning of ix: -1 = set to zero, V_DC = don't care, non-negative value = permute.
626  if ( ix == -1 ) {
627  r |= perm_zeroing; // zeroing requested
628  } else if ( ix != V_DC && uint32_t( ix ) >= N ) {
629  r |= perm_outofrange; // index out of range
630  }
631  if ( ix >= 0 ) {
632  r &= ~perm_allzero; // not all zero
633  if ( ix != (int)i ) r |= perm_perm; // needs permutation
634  if ( broadc == 999 )
635  broadc = ix; // remember broadcast index
636  else if ( broadc != ix )
637  broadc = 1000; // does not fit broadcast
638  }
639  // check if pattern fits a larger block size:
640  // even indexes must be even, odd indexes must fit the preceding even index + 1
641  if ( ( i & 1 ) == 0 ) { // even index
642  if ( ix >= 0 && ( ix & 1 ) ) r &= ~perm_largeblock; // not even. does not fit larger block size
643  int iy = a[i + 1]; // next odd index
644  if ( iy >= 0 && ( iy & 1 ) == 0 ) r &= ~perm_largeblock; // not odd. does not fit larger block size
645  if ( ix >= 0 && iy >= 0 && iy != ix + 1 ) r &= ~perm_largeblock; // does not fit preceding index + 1
646  if ( ix == -1 && iy >= 0 ) r |= perm_addz; // needs additional zeroing at current block size
647  if ( iy == -1 && ix >= 0 ) r |= perm_addz; // needs additional zeroing at current block size
648  }
649  lane = i / lanesize; // current lane
650  if ( lane == 0 ) { // first lane, or no pattern yet
651  lanepattern[i] = ix; // save pattern
652  }
653  // check if crossing lanes
654  if ( ix >= 0 ) {
655  uint32_t lanei = (uint32_t)ix / lanesize; // source lane
656  if ( lanei != lane ) r |= perm_cross_lane; // crossing lane
657  }
658  // check if same pattern in all lanes
659  if ( lane != 0 && ix >= 0 ) { // not first lane
660  int j1 = i - int( lane * lanesize ); // index into lanepattern
661  int jx = ix - int( lane * lanesize ); // pattern within lane
662  if ( jx < 0 || jx >= (int)lanesize ) r &= ~perm_same_pattern; // source is in another lane
663  if ( lanepattern[j1] < 0 ) {
664  lanepattern[j1] = jx; // pattern not known from previous lane
665  } else {
666  if ( lanepattern[j1] != jx ) r &= ~perm_same_pattern; // not same pattern
667  }
668  }
669  if ( ix >= 0 ) {
670  // check if pattern fits zero extension (perm_zext)
671  if ( uint32_t( ix * 2 ) != i ) {
672  patfail |= 1; // does not fit zero extension
673  }
674  // check if pattern fits compress (perm_compress)
675  if ( ix > compresslasti && ix - compresslasti >= (int)i - compresslastp ) {
676  if ( (int)i - compresslastp > 1 ) addz2 |= 2; // perm_compress may need additional zeroing
677  compresslasti = ix;
678  compresslastp = i;
679  } else {
680  patfail |= 2; // does not fit perm_compress
681  }
682  // check if pattern fits expand (perm_expand)
683  if ( ix > expandlasti && ix - expandlasti <= (int)i - expandlastp ) {
684  if ( ix - expandlasti > 1 ) addz2 |= 4; // perm_expand may need additional zeroing
685  expandlasti = ix;
686  expandlastp = i;
687  } else {
688  patfail |= 4; // does not fit perm_compress
689  }
690  } else if ( ix == -1 ) {
691  if ( ( i & 1 ) == 0 ) addz2 |= 1; // zero extension needs additional zeroing
692  }
693  }
694  if ( !( r & perm_perm ) ) return r; // more checks are superfluous
695 
696  if ( !( r & perm_largeblock ) ) r &= ~perm_addz; // remove irrelevant flag
697  if ( r & perm_cross_lane ) r &= ~perm_same_pattern; // remove irrelevant flag
698  if ( ( patfail & 1 ) == 0 ) {
699  r |= perm_zext; // fits zero extension
700  if ( ( addz2 & 1 ) != 0 ) r |= perm_addz2;
701  } else if ( ( patfail & 2 ) == 0 ) {
702  r |= perm_compress; // fits compression
703  if ( ( addz2 & 2 ) != 0 ) { // check if additional zeroing needed
704  for ( j = 0; j < compresslastp; j++ ) {
705  if ( a[j] == -1 ) r |= perm_addz2;
706  }
707  }
708  } else if ( ( patfail & 4 ) == 0 ) {
709  r |= perm_expand; // fits expansion
710  if ( ( addz2 & 4 ) != 0 ) { // check if additional zeroing needed
711  for ( j = 0; j < expandlastp; j++ ) {
712  if ( a[j] == -1 ) r |= perm_addz2;
713  }
714  }
715  }
716 
717  if ( r & perm_same_pattern ) {
718  // same pattern in all lanes. check if it fits specific patterns
719  bool fit = true;
720  // fit shift or rotate
721  for ( i = 0; i < lanesize; i++ ) {
722  if ( lanepattern[i] >= 0 ) {
723  uint32_t rot1 = uint32_t( lanepattern[i] + lanesize - i ) % lanesize;
724  if ( rot == 999 ) {
725  rot = rot1;
726  } else { // check if fit
727  if ( rot != rot1 ) fit = false;
728  }
729  }
730  }
731  rot &= lanesize - 1; // prevent out of range values
732  if ( fit ) { // fits rotate, and possibly shift
733  uint64_t rot2 = ( rot * elementsize ) & 0xF; // rotate right count in bytes
734  r |= rot2 << perm_rot_count; // put shift/rotate count in output bit 16-19
735 #if INSTRSET >= 4 // SSSE3
736  r |= perm_rotate; // allow palignr
737 #endif
738  // fit shift left
739  fit = true;
740  for ( i = 0; i < lanesize - rot; i++ ) { // check if first rot elements are zero or don't care
741  if ( lanepattern[i] >= 0 ) fit = false;
742  }
743  if ( fit ) {
744  r |= perm_shleft;
745  for ( ; i < lanesize; i++ )
746  if ( lanepattern[i] == -1 ) r |= perm_addz; // additional zeroing needed
747  }
748  // fit shift right
749  fit = true;
750  for ( i = lanesize - (uint32_t)rot; i < lanesize;
751  i++ ) { // check if last (lanesize-rot) elements are zero or don't care
752  if ( lanepattern[i] >= 0 ) fit = false;
753  }
754  if ( fit ) {
755  r |= perm_shright;
756  for ( i = 0; i < lanesize - rot; i++ ) {
757  if ( lanepattern[i] == -1 ) r |= perm_addz; // additional zeroing needed
758  }
759  }
760  }
761  // fit punpckhi
762  fit = true;
763  uint32_t j2 = lanesize / 2;
764  for ( i = 0; i < lanesize; i++ ) {
765  if ( lanepattern[i] >= 0 && lanepattern[i] != (int)j2 ) fit = false;
766  if ( ( i & 1 ) != 0 ) j2++;
767  }
768  if ( fit ) r |= perm_punpckh;
769  // fit punpcklo
770  fit = true;
771  j2 = 0;
772  for ( i = 0; i < lanesize; i++ ) {
773  if ( lanepattern[i] >= 0 && lanepattern[i] != (int)j2 ) fit = false;
774  if ( ( i & 1 ) != 0 ) j2++;
775  }
776  if ( fit ) r |= perm_punpckl;
777  // fit pshufd
778  if ( elementsize >= 4 ) {
779  uint64_t p = 0;
780  for ( i = 0; i < lanesize; i++ ) {
781  if ( lanesize == 4 ) {
782  p |= ( lanepattern[i] & 3 ) << 2 * i;
783  } else { // lanesize = 2
784  p |= ( ( lanepattern[i] & 1 ) * 10 + 4 ) << 4 * i;
785  }
786  }
787  r |= p << perm_ipattern;
788  }
789  }
790 #if INSTRSET >= 7
791  else { // not same pattern in all lanes
792  if constexpr ( nlanes > 1 ) { // Try if it fits big rotate
793  for ( i = 0; i < N; i++ ) {
794  ix = a[i];
795  if ( ix >= 0 ) {
796  uint32_t rot2 = ( ix + N - i ) % N; // rotate count
797  if ( rot == 999 ) {
798  rot = rot2; // save rotate count
799  } else if ( rot != rot2 ) {
800  rot = 1000;
801  break; // does not fit big rotate
802  }
803  }
804  }
805  if ( rot < N ) { // fits big rotate
806  r |= perm_rotate_big | (uint64_t)rot << perm_rot_count;
807  }
808  }
809  }
810 #endif
811  if ( broadc < 999 && ( r & ( perm_rotate | perm_shright | perm_shleft | perm_rotate_big ) ) == 0 ) {
812  r |= perm_broadcast | (uint64_t)broadc << perm_rot_count; // fits broadcast
813  }
814  return r;
815  }
816 
817  // compress_mask: returns a bit mask to use for compression instruction.
818  // It is presupposed that perm_flags indicates perm_compress.
819  // Additional zeroing is needed if perm_flags indicates perm_addz2
820  template <int N>
821  constexpr uint64_t compress_mask( int const ( &a )[N] ) {
822  // a is a reference to a constexpr array of permutation indexes
823  int ix = 0, lasti = -1, lastp = -1;
824  uint64_t m = 0;
825  int i = 0;
826  int j = 1; // loop counters
827  for ( i = 0; i < N; i++ ) {
828  ix = a[i]; // permutation index
829  if ( ix >= 0 ) {
830  m |= (uint64_t)1 << ix; // mask for compression source
831  for ( j = 1; j < i - lastp; j++ ) {
832  m |= (uint64_t)1 << ( lasti + j ); // dummy filling source
833  }
834  lastp = i;
835  lasti = ix;
836  }
837  }
838  return m;
839  }
840 
841  // expand_mask: returns a bit mask to use for expansion instruction.
842  // It is presupposed that perm_flags indicates perm_expand.
843  // Additional zeroing is needed if perm_flags indicates perm_addz2
844  template <int N>
845  constexpr uint64_t expand_mask( int const ( &a )[N] ) {
846  // a is a reference to a constexpr array of permutation indexes
847  int ix = 0, lasti = -1, lastp = -1;
848  uint64_t m = 0;
849  int i = 0;
850  int j = 1;
851  for ( i = 0; i < N; i++ ) {
852  ix = a[i]; // permutation index
853  if ( ix >= 0 ) {
854  m |= (uint64_t)1 << i; // mask for expansion destination
855  for ( j = 1; j < ix - lasti; j++ ) {
856  m |= (uint64_t)1 << ( lastp + j ); // dummy filling destination
857  }
858  lastp = i;
859  lasti = ix;
860  }
861  }
862  return m;
863  }
864 
865  // perm16_flags: returns information about how to permute a vector of 16-bit integers
866  // Note: It is presupposed that perm_flags reports perm_same_pattern
867  // The return value is composed of these bits:
868  // 1: data from low 64 bits to low 64 bits. pattern in bit 32-39
869  // 2: data from high 64 bits to high 64 bits. pattern in bit 40-47
870  // 4: data from high 64 bits to low 64 bits. pattern in bit 48-55
871  // 8: data from low 64 bits to high 64 bits. pattern in bit 56-63
872  template <typename V>
873  constexpr uint64_t perm16_flags( int const ( &a )[V::size()] ) {
874  // a is a reference to a constexpr array of permutation indexes
875  // V is a vector class
876  constexpr int N = V::size(); // number of elements
877 
878  uint64_t retval = 0; // return value
879  uint32_t pat[4] = {0, 0, 0, 0}; // permute patterns
880  uint32_t i = 0; // loop counter
881  int ix = 0; // index number i
882  const uint32_t lanesize = 8; // elements per lane
883  uint32_t lane = 0; // current lane
884  int lanepattern[lanesize] = {0}; // pattern in each lane
885 
886  for ( i = 0; i < N; i++ ) {
887  ix = a[i];
888  lane = i / lanesize; // current lane
889  if ( lane == 0 ) {
890  lanepattern[i] = ix; // save pattern
891  } else if ( ix >= 0 ) { // not first lane
892  uint32_t j = i - lane * lanesize; // index into lanepattern
893  int jx = ix - lane * lanesize; // pattern within lane
894  if ( lanepattern[j] < 0 ) {
895  lanepattern[j] = jx; // pattern not known from previous lane
896  }
897  }
898  }
899  // four patterns: low2low, high2high, high2low, low2high
900  for ( i = 0; i < 4; i++ ) {
901  // loop through low pattern
902  if ( lanepattern[i] >= 0 ) {
903  if ( lanepattern[i] < 4 ) { // low2low
904  retval |= 1;
905  pat[0] |= uint32_t( lanepattern[i] & 3 ) << ( 2 * i );
906  } else { // high2low
907  retval |= 4;
908  pat[2] |= uint32_t( lanepattern[i] & 3 ) << ( 2 * i );
909  }
910  }
911  // loop through high pattern
912  if ( lanepattern[i + 4] >= 0 ) {
913  if ( lanepattern[i + 4] < 4 ) { // low2high
914  retval |= 8;
915  pat[3] |= uint32_t( lanepattern[i + 4] & 3 ) << ( 2 * i );
916  } else { // high2high
917  retval |= 2;
918  pat[1] |= uint32_t( lanepattern[i + 4] & 3 ) << ( 2 * i );
919  }
920  }
921  }
922  // join return data
923  for ( i = 0; i < 4; i++ ) { retval |= (uint64_t)pat[i] << ( 32 + i * 8 ); }
924  return retval;
925  }
926 
927  // pshufb_mask: return a broad byte mask for permutation within lanes
928  // for use with the pshufb instruction (_mm..._shuffle_epi8).
929  // The pshufb instruction provides fast permutation and zeroing,
930  // allowing different patterns in each lane but no crossing of lane boundaries
931  template <typename V, int oppos = 0>
932  constexpr auto pshufb_mask( int const ( &A )[V::size()] ) {
933  // Parameter a is a reference to a constexpr array of permutation indexes
934  // V is a vector class
935  // oppos = 1 for data from the opposite 128-bit lane in 256-bit vectors
936  constexpr uint32_t N = V::size(); // number of vector elements
937  constexpr uint32_t elementsize = sizeof( V ) / N; // size of each vector element
938  constexpr uint32_t nlanes = sizeof( V ) / 16; // number of 128 bit lanes in vector
939  constexpr uint32_t elements_per_lane = N / nlanes; // number of vector elements per lane
940 
941  EList<int8_t, sizeof( V )> u = {{0}}; // list for returning
942 
943  uint32_t i = 0; // loop counters
944  uint32_t j = 0;
945  int m = 0;
946  int k = 0;
947  uint32_t lane = 0;
948 
949  for ( lane = 0; lane < nlanes; lane++ ) { // loop through lanes
950  for ( i = 0; i < elements_per_lane; i++ ) { // loop through elements in lane
951  // permutation index for element within lane
952  int8_t p = -1;
953  int ix = A[m];
954  if ( ix >= 0 ) {
955  ix ^= oppos * elements_per_lane; // flip bit if opposite lane
956  }
957  ix -= int( lane * elements_per_lane ); // index relative to lane
958  if ( ix >= 0 && ix < (int)elements_per_lane ) { // index points to desired lane
959  p = ix * elementsize;
960  }
961  for ( j = 0; j < elementsize; j++ ) { // loop through bytes in element
962  u.a[k++] = p < 0 ? -1 : p + j; // store byte permutation index
963  }
964  m++;
965  }
966  }
967  return u; // return encapsulated array
968  }
969 
970  // largeblock_perm: return indexes for replacing a permute or blend with
971  // a certain block size by a permute or blend with the double block size.
972  // Note: it is presupposed that perm_flags() indicates perm_largeblock
973  // It is required that additional zeroing is added if perm_flags() indicates perm_addz
974  template <int N>
975  constexpr EList<int, N / 2> largeblock_perm( int const ( &a )[N] ) {
976  // Parameter a is a reference to a constexpr array of permutation indexes
977  EList<int, N / 2> list = {{0}}; // result indexes
978  int ix = 0; // even index
979  int iy = 0; // odd index
980  int iz = 0; // combined index
981  bool fit_addz = false; // additional zeroing needed at the lower block level
982  int i = 0; // loop counter
983 
984  // check if additional zeroing is needed at current block size
985  for ( i = 0; i < N; i += 2 ) {
986  ix = a[i]; // even index
987  iy = a[i + 1]; // odd index
988  if ( ( ix == -1 && iy >= 0 ) || ( iy == -1 && ix >= 0 ) ) { fit_addz = true; }
989  }
990 
991  // loop through indexes
992  for ( i = 0; i < N; i += 2 ) {
993  ix = a[i]; // even index
994  iy = a[i + 1]; // odd index
995  if ( ix >= 0 ) {
996  iz = ix / 2; // half index
997  } else if ( iy >= 0 ) {
998  iz = iy / 2;
999  } else {
1000  iz = ix | iy; // -1 or V_DC. -1 takes precedence
1001  if ( fit_addz ) iz = V_DC; // V_DC, because result will be zeroed later
1002  }
1003  list.a[i / 2] = iz; // save to list
1004  }
1005  return list;
1006  }
1007 
1008  // blend_flags: returns information about how a blend function can be implemented
1009  // The return value is composed of these flag bits:
1010  const int blend_zeroing = 1; // needs zeroing
1011  const int blend_allzero = 2; // all is zero or don't care
1012  const int blend_largeblock = 4; // fits blend with a larger block size (e.g permute Vec2q instead of Vec4i)
1013  const int blend_addz = 8; // additional zeroing needed after blend with larger block size or shift
1014  const int blend_a = 0x10; // has data from a
1015  const int blend_b = 0x20; // has data from b
1016  const int blend_perma = 0x40; // permutation of a needed
1017  const int blend_permb = 0x80; // permutation of b needed
1018  const int blend_cross_lane = 0x100; // permutation crossing 128-bit lanes
1019  const int blend_same_pattern = 0x200; // same permute/blend pattern in all 128-bit lanes
1020  const int blend_punpckhab = 0x1000; // pattern fits punpckh(a,b)
1021  const int blend_punpckhba = 0x2000; // pattern fits punpckh(b,a)
1022  const int blend_punpcklab = 0x4000; // pattern fits punpckl(a,b)
1023  const int blend_punpcklba = 0x8000; // pattern fits punpckl(b,a)
1024  const int blend_rotateab = 0x10000; // pattern fits palignr(a,b)
1025  const int blend_rotateba = 0x20000; // pattern fits palignr(b,a)
1026  const int blend_shufab = 0x40000; // pattern fits shufps/shufpd(a,b)
1027  const int blend_shufba = 0x80000; // pattern fits shufps/shufpd(b,a)
1028  const int blend_rotate_big = 0x100000; // pattern fits rotation across lanes. count returned in bits blend_rotpattern
1029  const int blend_outofrange = 0x10000000; // index out of range
1030  const int blend_shufpattern = 32; // pattern for shufps/shufpd is in bit blend_shufpattern to blend_shufpattern + 7
1031  const int blend_rotpattern = 40; // pattern for palignr is in bit blend_rotpattern to blend_rotpattern + 7
1032 
1033  template <typename V>
1034  constexpr uint64_t blend_flags( int const ( &a )[V::size()] ) {
1035  // a is a reference to a constexpr array of permutation indexes
1036  // V is a vector class
1037  constexpr int N = V::size(); // number of elements
1038  uint64_t r = blend_largeblock | blend_same_pattern | blend_allzero; // return value
1039  uint32_t iu = 0; // loop counter
1040  int32_t ii = 0; // loop counter
1041  int ix = 0; // index number i
1042  const uint32_t nlanes = sizeof( V ) / 16; // number of 128-bit lanes
1043  const uint32_t lanesize = N / nlanes; // elements per lane
1044  uint32_t lane = 0; // current lane
1045  uint32_t rot = 999; // rotate left count
1046  int lanepattern[lanesize] = {0}; // pattern in each lane
1047  if ( lanesize == 2 && N <= 8 ) {
1048  r |= blend_shufab | blend_shufba; // check if it fits shufpd
1049  }
1050 
1051  for ( ii = 0; ii < N; ii++ ) { // loop through indexes
1052  ix = a[ii]; // index
1053  if ( ix < 0 ) {
1054  if ( ix == -1 )
1055  r |= blend_zeroing; // set to zero
1056  else if ( ix != V_DC ) {
1057  r = blend_outofrange;
1058  break; // illegal index
1059  }
1060  } else { // ix >= 0
1061  r &= ~blend_allzero;
1062  if ( ix < N ) {
1063  r |= blend_a; // data from a
1064  if ( ix != ii ) r |= blend_perma; // permutation of a
1065  } else if ( ix < 2 * N ) {
1066  r |= blend_b; // data from b
1067  if ( ix != ii + N ) r |= blend_permb; // permutation of b
1068  } else {
1069  r = blend_outofrange;
1070  break; // illegal index
1071  }
1072  }
1073  // check if pattern fits a larger block size:
1074  // even indexes must be even, odd indexes must fit the preceding even index + 1
1075  if ( ( ii & 1 ) == 0 ) { // even index
1076  if ( ix >= 0 && ( ix & 1 ) ) r &= ~blend_largeblock; // not even. does not fit larger block size
1077  int iy = a[ii + 1]; // next odd index
1078  if ( iy >= 0 && ( iy & 1 ) == 0 ) r &= ~blend_largeblock; // not odd. does not fit larger block size
1079  if ( ix >= 0 && iy >= 0 && iy != ix + 1 ) r &= ~blend_largeblock; // does not fit preceding index + 1
1080  if ( ix == -1 && iy >= 0 ) r |= blend_addz; // needs additional zeroing at current block size
1081  if ( iy == -1 && ix >= 0 ) r |= blend_addz; // needs additional zeroing at current block size
1082  }
1083  lane = (uint32_t)ii / lanesize; // current lane
1084  if ( lane == 0 ) { // first lane, or no pattern yet
1085  lanepattern[ii] = ix; // save pattern
1086  }
1087  // check if crossing lanes
1088  if ( ix >= 0 ) {
1089  uint32_t lanei = uint32_t( ix & ~N ) / lanesize; // source lane
1090  if ( lanei != lane ) {
1091  r |= blend_cross_lane; // crossing lane
1092  }
1093  if ( lanesize == 2 ) { // check if it fits pshufd
1094  if ( lanei != lane ) r &= ~( blend_shufab | blend_shufba );
1095  if ( ( ( ( ix & N ) != 0 ) ^ ii ) & 1 )
1096  r &= ~blend_shufab;
1097  else
1098  r &= ~blend_shufba;
1099  }
1100  }
1101  // check if same pattern in all lanes
1102  if ( lane != 0 && ix >= 0 ) { // not first lane
1103  int j = ii - int( lane * lanesize ); // index into lanepattern
1104  int jx = ix - int( lane * lanesize ); // pattern within lane
1105  if ( jx < 0 || ( jx & ~N ) >= (int)lanesize ) r &= ~blend_same_pattern; // source is in another lane
1106  if ( lanepattern[j] < 0 ) {
1107  lanepattern[j] = jx; // pattern not known from previous lane
1108  } else {
1109  if ( lanepattern[j] != jx ) r &= ~blend_same_pattern; // not same pattern
1110  }
1111  }
1112  }
1113  if ( !( r & blend_largeblock ) ) r &= ~blend_addz; // remove irrelevant flag
1114  if ( r & blend_cross_lane ) r &= ~blend_same_pattern; // remove irrelevant flag
1115  if ( !( r & ( blend_perma | blend_permb ) ) ) {
1116  return r; // no permutation. more checks are superfluous
1117  }
1118  if ( r & blend_same_pattern ) {
1119  // same pattern in all lanes. check if it fits unpack patterns
1121  for ( iu = 0; iu < lanesize; iu++ ) { // loop through lanepattern
1122  ix = lanepattern[iu];
1123  if ( ix >= 0 ) {
1124  if ( (uint32_t)ix != iu / 2 + ( iu & 1 ) * N ) r &= ~blend_punpcklab;
1125  if ( (uint32_t)ix != iu / 2 + ( ( iu & 1 ) ^ 1 ) * N ) r &= ~blend_punpcklba;
1126  if ( (uint32_t)ix != ( iu + lanesize ) / 2 + ( iu & 1 ) * N ) r &= ~blend_punpckhab;
1127  if ( (uint32_t)ix != ( iu + lanesize ) / 2 + ( ( iu & 1 ) ^ 1 ) * N ) r &= ~blend_punpckhba;
1128  }
1129  }
1130 #if INSTRSET >= 4 // SSSE3. check if it fits palignr
1131  for ( iu = 0; iu < lanesize; iu++ ) {
1132  ix = lanepattern[iu];
1133  if ( ix >= 0 ) {
1134  uint32_t t = ix & ~N;
1135  if ( ix & N ) t += lanesize;
1136  uint32_t tb = ( t + 2 * lanesize - iu ) % ( lanesize * 2 );
1137  if ( rot == 999 ) {
1138  rot = tb;
1139  } else { // check if fit
1140  if ( rot != tb ) rot = 1000;
1141  }
1142  }
1143  }
1144  if ( rot < 999 ) { // firs palignr
1145  if ( rot < lanesize ) {
1146  r |= blend_rotateba;
1147  } else {
1148  r |= blend_rotateab;
1149  }
1150  const uint32_t elementsize = sizeof( V ) / N;
1151  r |= uint64_t( ( rot & ( lanesize - 1 ) ) * elementsize ) << blend_rotpattern;
1152  }
1153 #endif
1154  if ( lanesize == 4 ) {
1155  // check if it fits shufps
1156  r |= blend_shufab | blend_shufba;
1157  for ( ii = 0; ii < 2; ii++ ) {
1158  ix = lanepattern[ii];
1159  if ( ix >= 0 ) {
1160  if ( ix & N )
1161  r &= ~blend_shufab;
1162  else
1163  r &= ~blend_shufba;
1164  }
1165  }
1166  for ( ; ii < 4; ii++ ) {
1167  ix = lanepattern[ii];
1168  if ( ix >= 0 ) {
1169  if ( ix & N )
1170  r &= ~blend_shufba;
1171  else
1172  r &= ~blend_shufab;
1173  }
1174  }
1175  if ( r & ( blend_shufab | blend_shufba ) ) { // fits shufps/shufpd
1176  uint8_t shufpattern = 0; // get pattern
1177  for ( iu = 0; iu < lanesize; iu++ ) { shufpattern |= ( lanepattern[iu] & 3 ) << iu * 2; }
1178  r |= (uint64_t)shufpattern << blend_shufpattern; // return pattern
1179  }
1180  }
1181  } else if ( nlanes > 1 ) { // not same pattern in all lanes
1182  rot = 999; // check if it fits big rotate
1183  for ( ii = 0; ii < N; ii++ ) {
1184  ix = a[ii];
1185  if ( ix >= 0 ) {
1186  uint32_t rot2 = ( ix + 2 * N - ii ) % ( 2 * N ); // rotate count
1187  if ( rot == 999 ) {
1188  rot = rot2; // save rotate count
1189  } else if ( rot != rot2 ) {
1190  rot = 1000;
1191  break; // does not fit big rotate
1192  }
1193  }
1194  }
1195  if ( rot < 2 * N ) { // fits big rotate
1196  r |= blend_rotate_big | (uint64_t)rot << blend_rotpattern;
1197  }
1198  }
1199  if ( lanesize == 2 && ( r & ( blend_shufab | blend_shufba ) ) ) { // fits shufpd. Get pattern
1200  for ( ii = 0; ii < N; ii++ ) { r |= uint64_t( a[ii] & 1 ) << ( blend_shufpattern + ii ); }
1201  }
1202  return r;
1203  }
1204 
1205  // blend_perm_indexes: return an Indexlist for implementing a blend function as
1206  // two permutations. N = vector size.
1207  // dozero = 0: let unused elements be don't care. The two permutation results must be blended
1208  // dozero = 1: zero unused elements in each permuation. The two permutation results can be OR'ed
1209  // dozero = 2: indexes that are -1 or V_DC are preserved
1210  template <int N, int dozero>
1211  constexpr EList<int, 2 * N> blend_perm_indexes( int const ( &a )[N] ) {
1212  // a is a reference to a constexpr array of permutation indexes
1213  EList<int, 2 * N> list = {{0}}; // list to return
1214  int u = dozero ? -1 : V_DC; // value to use for unused entries
1215  int j = 0;
1216 
1217  for ( j = 0; j < N; j++ ) { // loop through indexes
1218  int ix = a[j]; // current index
1219  if ( ix < 0 ) { // zero or don't care
1220  if ( dozero == 2 ) {
1221  // list.a[j] = list.a[j + N] = ix; // fails in gcc in complicated cases
1222  list.a[j] = ix;
1223  list.a[j + N] = ix;
1224  } else {
1225  // list.a[j] = list.a[j + N] = u;
1226  list.a[j] = u;
1227  list.a[j + N] = u;
1228  }
1229  } else if ( ix < N ) { // value from a
1230  list.a[j] = ix;
1231  list.a[j + N] = u;
1232  } else {
1233  list.a[j] = u; // value from b
1234  list.a[j + N] = ix - N;
1235  }
1236  }
1237  return list;
1238  }
1239 
1240  // largeblock_indexes: return indexes for replacing a permute or blend with a
1241  // certain block size by a permute or blend with the double block size.
1242  // Note: it is presupposed that perm_flags or blend_flags indicates _largeblock
1243  // It is required that additional zeroing is added if perm_flags or blend_flags
1244  // indicates _addz
1245  template <int N>
1246  constexpr EList<int, N / 2> largeblock_indexes( int const ( &a )[N] ) {
1247  // Parameter a is a reference to a constexpr array of N permutation indexes
1248  EList<int, N / 2> list = {{0}}; // list to return
1249 
1250  bool fit_addz = false; // additional zeroing needed at the lower block level
1251  int ix = 0; // even index
1252  int iy = 0; // odd index
1253  int iz = 0; // combined index
1254  int i = 0; // loop counter
1255 
1256  for ( i = 0; i < N; i += 2 ) {
1257  ix = a[i]; // even index
1258  iy = a[i + 1]; // odd index
1259  if ( ix >= 0 ) {
1260  iz = ix / 2; // half index
1261  } else if ( iy >= 0 ) {
1262  iz = iy / 2; // half index
1263  } else
1264  iz = ix | iy; // -1 or V_DC. -1 takes precedence
1265  list.a[i / 2] = iz; // save to list
1266  // check if additional zeroing is needed at current block size
1267  if ( ( ix == -1 && iy >= 0 ) || ( iy == -1 && ix >= 0 ) ) { fit_addz = true; }
1268  }
1269  // replace -1 by V_DC if fit_addz
1270  if ( fit_addz ) {
1271  for ( i = 0; i < N / 2; i++ ) {
1272  if ( list.a[i] < 0 ) list.a[i] = V_DC;
1273  }
1274  }
1275  return list;
1276  }
1277 
1278  /****************************************************************************************
1279  *
1280  * Vector blend helper function templates
1281  *
1282  * These templates are for emulating a blend with a vector size that is not supported by
1283  * the instruction set, using multiple blends or permutations of half the vector size
1284  *
1285  ****************************************************************************************/
1286 
1287  // Make dummy blend function templates to avoid error messages when the blend funtions are not yet defined
1288  template <typename dummy>
1289  void blend2() {}
1290  template <typename dummy>
1291  void blend4() {}
1292  template <typename dummy>
1293  void blend8() {}
1294  template <typename dummy>
1295  void blend16() {}
1296  template <typename dummy>
1297  void blend32() {}
1298 
1299  // blend_half_indexes: return an Indexlist for emulating a blend function as
1300  // blends or permutations from multiple sources
1301  // dozero = 0: let unused elements be don't care. Multiple permutation results must be blended
1302  // dozero = 1: zero unused elements in each permuation. Multiple permutation results can be OR'ed
1303  // dozero = 2: indexes that are -1 or V_DC are preserved
1304  // src1, src2: sources to blend in a partial implementation
1305  template <int N, int dozero, int src1, int src2>
1306  constexpr EList<int, N> blend_half_indexes( int const ( &a )[N] ) {
1307  // a is a reference to a constexpr array of permutation indexes
1308  EList<int, N> list = {{0}}; // list to return
1309  int u = dozero ? -1 : V_DC; // value to use for unused entries
1310  int j = 0; // loop counter
1311 
1312  for ( j = 0; j < N; j++ ) { // loop through indexes
1313  int ix = a[j]; // current index
1314  if ( ix < 0 ) { // zero or don't care
1315  list.a[j] = ( dozero == 2 ) ? ix : u;
1316  } else {
1317  int src = ix / N; // source
1318  if ( src == src1 ) {
1319  list.a[j] = ix & ( N - 1 );
1320  } else if ( src == src2 ) {
1321  list.a[j] = ( ix & ( N - 1 ) ) + N;
1322  } else
1323  list.a[j] = u;
1324  }
1325  }
1326  return list;
1327  }
1328 
1329  // selectblend: select one of four sources for blending
1330  template <typename W, int s>
1331  static inline auto selectblend( W const a, W const b ) {
1332  if constexpr ( s == 0 )
1333  return a.get_low();
1334  else if constexpr ( s == 1 )
1335  return a.get_high();
1336  else if constexpr ( s == 2 )
1337  return b.get_low();
1338  else
1339  return b.get_high();
1340  }
1341 
1342  // blend_half: Emulate a blend with a vector size that is not supported
1343  // by multiple blends with half the vector size.
1344  // blend_half is called twice, to give the low and high half of the result
1345  // Parameters: W: type of full-size vector
1346  // i0...: indexes for low or high half
1347  // a, b: full size input vectors
1348  // return value: half-size vector for lower or upper part
1349  template <typename W, int... i0>
1350  auto blend_half( W const& a, W const& b ) {
1351  typedef decltype( a.get_low() ) V; // type for half-size vector
1352  constexpr int N = V::size(); // size of half-size vector
1353  static_assert( sizeof...( i0 ) == N, "wrong number of indexes in blend_half" );
1354  constexpr int ind[N] = {i0...}; // array of indexes
1355 
1356  // lambda to find which of the four possible sources are used
1357  // return: EList<int, 5> containing a list of up to 4 sources. The last element is the number of sources used
1358  auto listsources = []( int const n, int const( &ind )[N] ) constexpr {
1359  bool source_used[4] = {false, false, false, false}; // list of sources used
1360  int i = 0;
1361  for ( i = 0; i < n; i++ ) {
1362  int ix = ind[i]; // index
1363  if ( ix >= 0 ) {
1364  int src = ix / n; // source used
1365  source_used[src & 3] = true;
1366  }
1367  }
1368  // return a list of sources used. The last element is the number of sources used
1369  EList<int, 5> sources = {{0}};
1370  int nsrc = 0; // number of sources
1371  for ( i = 0; i < 4; i++ ) {
1372  if ( source_used[i] ) { sources.a[nsrc++] = i; }
1373  }
1374  sources.a[4] = nsrc;
1375  return sources;
1376  };
1377  // list of sources used
1378  constexpr EList<int, 5> sources = listsources( N, ind );
1379  constexpr int nsrc = sources.a[4]; // number of sources used
1380 
1381  if constexpr ( nsrc == 0 ) { // no sources
1382  return V( 0 );
1383  }
1384  // get indexes for the first one or two sources
1385  constexpr int uindex = ( nsrc > 2 ) ? 1 : 2; // unused elements set to zero if two blends are combined
1386  constexpr EList<int, N> L = blend_half_indexes<N, uindex, sources.a[0], sources.a[1]>( ind );
1387  V x0;
1388  V src0 = selectblend<W, sources.a[0]>( a, b ); // first source
1389  V src1 = selectblend<W, sources.a[1]>( a, b ); // second source
1390  if constexpr ( N == 2 ) {
1391  x0 = blend2<L.a[0], L.a[1]>( src0, src1 );
1392  } else if constexpr ( N == 4 ) {
1393  x0 = blend4<L.a[0], L.a[1], L.a[2], L.a[3]>( src0, src1 );
1394  } else if constexpr ( N == 8 ) {
1395  x0 = blend8<L.a[0], L.a[1], L.a[2], L.a[3], L.a[4], L.a[5], L.a[6], L.a[7]>( src0, src1 );
1396  } else if constexpr ( N == 16 ) {
1397  x0 = blend16<L.a[0], L.a[1], L.a[2], L.a[3], L.a[4], L.a[5], L.a[6], L.a[7], L.a[8], L.a[9], L.a[10], L.a[11],
1398  L.a[12], L.a[13], L.a[14], L.a[15]>( src0, src1 );
1399  } else if constexpr ( N == 32 ) {
1400  x0 = blend32<L.a[0], L.a[1], L.a[2], L.a[3], L.a[4], L.a[5], L.a[6], L.a[7], L.a[8], L.a[9], L.a[10], L.a[11],
1401  L.a[12], L.a[13], L.a[14], L.a[15], L.a[16], L.a[17], L.a[18], L.a[19], L.a[20], L.a[21], L.a[22],
1402  L.a[23], L.a[24], L.a[25], L.a[26], L.a[27], L.a[28], L.a[29], L.a[30], L.a[31]>( src0, src1 );
1403  }
1404  if constexpr ( nsrc > 2 ) { // get last one or two sources
1405  constexpr EList<int, N> M = blend_half_indexes<N, 1, sources.a[2], sources.a[3]>( ind );
1406  V x1;
1407  V src2 = selectblend<W, sources.a[2]>( a, b ); // third source
1408  V src3 = selectblend<W, sources.a[3]>( a, b ); // fourth source
1409  if constexpr ( N == 2 ) {
1410  x1 = blend2<M.a[0], M.a[1]>( src0, src1 );
1411  } else if constexpr ( N == 4 ) {
1412  x1 = blend4<M.a[0], M.a[1], M.a[2], M.a[3]>( src2, src3 );
1413  } else if constexpr ( N == 8 ) {
1414  x1 = blend8<M.a[0], M.a[1], M.a[2], M.a[3], M.a[4], M.a[5], M.a[6], M.a[7]>( src2, src3 );
1415  } else if constexpr ( N == 16 ) {
1416  x1 = blend16<M.a[0], M.a[1], M.a[2], M.a[3], M.a[4], M.a[5], M.a[6], M.a[7], M.a[8], M.a[9], M.a[10], M.a[11],
1417  M.a[12], M.a[13], M.a[14], M.a[15]>( src2, src3 );
1418  } else if constexpr ( N == 32 ) {
1419  x1 = blend32<M.a[0], M.a[1], M.a[2], M.a[3], M.a[4], M.a[5], M.a[6], M.a[7], M.a[8], M.a[9], M.a[10], M.a[11],
1420  M.a[12], M.a[13], M.a[14], M.a[15], M.a[16], M.a[17], M.a[18], M.a[19], M.a[20], M.a[21], M.a[22],
1421  M.a[23], M.a[24], M.a[25], M.a[26], M.a[27], M.a[28], M.a[29], M.a[30], M.a[31]>( src2, src3 );
1422  }
1423  x0 |= x1; // combine result of two blends. Unused elements are zero
1424  }
1425  return x0;
1426  }
1427 
1428 #ifdef VCL_NAMESPACE
1429 }
1430 #endif
1431 
1432 #endif // INSTRSET_H
const int perm_rot_count
Definition: instrset.h:595
#define VCL_NAMESPACE
Definition: System.cpp:47
constexpr auto size(const T &, Args &&...) noexcept
const int blend_permb
Definition: instrset.h:1017
constexpr EList< int, N/2 > largeblock_indexes(int const (&a)[N])
Definition: instrset.h:1246
const int perm_largeblock
Definition: instrset.h:575
constexpr auto make_broad_mask(uint64_t const m)
Definition: instrset.h:549
void blend8()
Definition: instrset.h:1293
bool hasAVX512ER(void)
void blend16()
Definition: instrset.h:1295
constexpr auto zero_mask(int const (&a)[N])
Definition: instrset.h:483
const int blend_shufab
Definition: instrset.h:1026
const int perm_perm
Definition: instrset.h:573
constexpr uint64_t expand_mask(int const (&a)[N])
Definition: instrset.h:845
const int perm_outofrange
Definition: instrset.h:594
bool hasAVX512VBMI2(void)
#define cpuid(func, eax, ebx, ecx, edx)
const int perm_ipattern
Definition: instrset.h:596
const int perm_punpckl
Definition: instrset.h:581
bool hasFMA4(void)
constexpr uint64_t make_bit_mask(int const (&a)[N])
Definition: instrset.h:522
const int perm_allzero
Definition: instrset.h:574
const int perm_compress
Definition: instrset.h:592
bool hasXOP(void)
const int blend_largeblock
Definition: instrset.h:1012
constexpr uint64_t perm_flags(int const (&a)[V::size()])
Definition: instrset.h:600
void blend32()
Definition: instrset.h:1297
const int blend_allzero
Definition: instrset.h:1011
int N
Definition: IOTest.py:110
const int perm_punpckh
Definition: instrset.h:580
const int blend_rotateba
Definition: instrset.h:1025
constexpr EList< int, N > blend_half_indexes(int const (&a)[N])
Definition: instrset.h:1306
const int blend_shufpattern
Definition: instrset.h:1030
constexpr uint64_t blend_flags(int const (&a)[V::size()])
Definition: instrset.h:1034
const int perm_addz2
Definition: instrset.h:577
const int blend_rotate_big
Definition: instrset.h:1028
const int blend_a
Definition: instrset.h:1014
auto blend_half(W const &a, W const &b)
Definition: instrset.h:1350
constexpr uint64_t compress_mask(int const (&a)[N])
Definition: instrset.h:821
constexpr double m
const int blend_shufba
Definition: instrset.h:1027
bool hasFMA3(void)
int instrset_detect(void)
const int perm_shright
Definition: instrset.h:584
const int perm_addz
Definition: instrset.h:576
const int blend_b
Definition: instrset.h:1015
const int perm_zext
Definition: instrset.h:591
const int perm_broadcast
Definition: instrset.h:590
const int perm_expand
Definition: instrset.h:593
const int blend_outofrange
Definition: instrset.h:1029
constexpr int V_DC
Definition: instrset.h:220
const int blend_cross_lane
Definition: instrset.h:1018
T a[N]
Definition: instrset.h:460
const int blend_punpckhba
Definition: instrset.h:1021
void blend2()
Definition: instrset.h:1289
const int blend_same_pattern
Definition: instrset.h:1019
const int blend_punpckhab
Definition: instrset.h:1020
constexpr auto pshufb_mask(int const (&A)[V::size()])
Definition: instrset.h:932
constexpr uint64_t perm16_flags(int const (&a)[V::size()])
Definition: instrset.h:873
const int perm_shleft
Definition: instrset.h:586
const int perm_zeroing
Definition: instrset.h:572
const int perm_rotate
Definition: instrset.h:582
void blend4()
Definition: instrset.h:1291
constexpr auto zero_mask_broad(int const (&A)[V::size()])
Definition: instrset.h:503
const int perm_same_pattern
Definition: instrset.h:579
constexpr EList< int, N/2 > largeblock_perm(int const (&a)[N])
Definition: instrset.h:975
string s
Definition: gaudirun.py:328
const int blend_perma
Definition: instrset.h:1016
const int perm_cross_lane
Definition: instrset.h:578
const int blend_addz
Definition: instrset.h:1013
constexpr EList< int, 2 *N > blend_perm_indexes(int const (&a)[N])
Definition: instrset.h:1211
const int blend_zeroing
Definition: instrset.h:1010
const int perm_rotate_big
Definition: instrset.h:588
const int blend_punpcklba
Definition: instrset.h:1023
bool hasAVX512VBMI(void)
constexpr auto perm_mask_broad(int const (&A)[V::size()])
Definition: instrset.h:561
const int blend_rotpattern
Definition: instrset.h:1031
const int blend_punpcklab
Definition: instrset.h:1022
constexpr int bit_scan_reverse_const(uint64_t const n)
Definition: instrset.h:378
const int blend_rotateab
Definition: instrset.h:1024
constexpr auto get_inttype()
Definition: instrset.h:466
int physicalProcessors(int *logical_processors=0)