| 1 | /* Vectorized routines for x86 Advanced Vector Extensions (AVX). |
| 2 | * |
| 3 | * This header file, unusually, provides many complete function |
| 4 | * implementations so they can be inlined by the compiler. |
| 5 | * |
| 6 | * Contents: |
| 7 | * 1. Function declarations for esl_avx.c |
| 8 | * 2. Inlined functions: horizontal max, sum |
| 9 | * 3. Inlined functions: left and right shifts |
| 10 | * 4. Inlined functions: any_gt |
| 11 | */ |
| 12 | #ifndef eslAVX_INCLUDED |
| 13 | #define eslAVX_INCLUDED |
| 14 | #include <esl_config.h> |
| 15 | #ifdef eslENABLE_AVX |
| 16 | |
| 17 | #include "easel.h" |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <x86intrin.h> |
| 21 | |
| 22 | |
| 23 | /***************************************************************** |
| 24 | * 1. Function declarations for esl_avx.c |
| 25 | *****************************************************************/ |
| 26 | |
| 27 | extern void esl_avx_dump_256i_hex4(__m256i v); |
| 28 | |
| 29 | |
| 30 | |
| 31 | /***************************************************************** |
| 32 | * 2. Inlined functions: horizontal max, sum |
| 33 | *****************************************************************/ |
| 34 | |
| 35 | /* Function: esl_avx_hmax_epu8() |
| 36 | * Synopsis: Return max of 32 uint8_t elements in epu8 vector. |
| 37 | * |
| 38 | * Note: benchmark on wumpus, 0.8s (200M) => 4.0 ns/call |
| 39 | */ |
| 40 | static inline uint8_t |
| 41 | esl_avx_hmax_epu8(__m256i a) |
| 42 | { |
| 43 | a = _mm256_max_epu8(a, _mm256_permute2x128_si256(a, a, 0x01)); |
| 44 | a = _mm256_max_epu8(a, _mm256_shuffle_epi32 (a, 0x4e)); |
| 45 | a = _mm256_max_epu8(a, _mm256_shuffle_epi32 (a, 0xb1)); |
| 46 | a = _mm256_max_epu8(a, _mm256_shufflelo_epi16 (a, 0xb1)); |
| 47 | a = _mm256_max_epu8(a, _mm256_srli_si256 (a, 1)); |
| 48 | return _mm256_extract_epi8(a, 0); // epi8 is fine here. gets cast properly to uint8_t on return. |
| 49 | } |
| 50 | |
| 51 | /* Function: esl_avx_hmax_epi8() |
| 52 | * Synopsis: Return max of the 32 int8_t elements in epi8 vector. |
| 53 | * Incept: SRE, Tue May 23 09:42:02 2017 |
| 54 | * |
| 55 | * Note: benchmark on wumpus, ~0.6s (200M) => 3.0 ns/call |
| 56 | */ |
| 57 | static inline int8_t |
| 58 | esl_avx_hmax_epi8(__m256i a) |
| 59 | { |
| 60 | a = _mm256_max_epi8(a, _mm256_permute2x128_si256(a, a, 0x01)); |
| 61 | a = _mm256_max_epi8(a, _mm256_shuffle_epi32 (a, 0x4e)); |
| 62 | a = _mm256_max_epi8(a, _mm256_shuffle_epi32 (a, 0xb1)); |
| 63 | a = _mm256_max_epi8(a, _mm256_shufflelo_epi16 (a, 0xb1)); |
| 64 | a = _mm256_max_epi8(a, _mm256_srli_si256 (a, 1)); |
| 65 | return _mm256_extract_epi8(a, 0); |
| 66 | } |
| 67 | |
| 68 | /* Function: esl_avx_hmax_epi16() |
| 69 | * Synopsis: Return max of 16 int16_t elements in epi16 vector. |
| 70 | * |
| 71 | * Note: benchmark on wumpus, 0.6s (200M) => 3.0 ns/call |
| 72 | */ |
| 73 | static inline int16_t |
| 74 | esl_avx_hmax_epi16(__m256i a) |
| 75 | { |
| 76 | a = _mm256_max_epi16(a, _mm256_permute2x128_si256(a, a, 0x01)); |
| 77 | a = _mm256_max_epi16(a, _mm256_shuffle_epi32 (a, 0x4e)); |
| 78 | a = _mm256_max_epi16(a, _mm256_shuffle_epi32 (a, 0xb1)); |
| 79 | a = _mm256_max_epi16(a, _mm256_shufflelo_epi16 (a, 0xb1)); |
| 80 | return _mm256_extract_epi16(a, 0); |
| 81 | } |
| 82 | |
| 83 | /* Function: esl_avx_hsum_ps() |
| 84 | * Synopsis: Takes the horizontal sum of elements in a vector. |
| 85 | * |
| 86 | * Purpose: Add the four float elements in vector <a>; return |
| 87 | * that sum in <*ret_sum>. |
| 88 | */ |
| 89 | static inline void |
| 90 | esl_avx_hsum_ps(__m256 a, float *ret_sum) |
| 91 | { |
| 92 | __m256 temp1_AVX = (__m256) _mm256_permute2x128_si256((__m256i) a, (__m256i) a, 0x01); |
| 93 | // Swap the 128-bit halves from a into temp1 |
| 94 | |
| 95 | __m256 temp2_AVX = _mm256_add_ps(a, temp1_AVX); |
| 96 | // low 128 bits of temp2_AVX have the sum of the corresponding floats from the high, low |
| 97 | // 128 bits of a |
| 98 | |
| 99 | temp1_AVX = (__m256) _mm256_shuffle_epi32((__m256i) temp2_AVX, 0x4e); // Swap the 64-bit halves of each 128-bit half of a |
| 100 | temp2_AVX = _mm256_add_ps(temp1_AVX, temp2_AVX); // low 64 bits of temp2_AVX now have the sums of the |
| 101 | // corresponding floats from the quarters of a |
| 102 | |
| 103 | temp1_AVX = (__m256) _mm256_shuffle_epi32((__m256i) temp2_AVX, 0xb1); // Swap the 32-bit halves of each 64-bit quarter of temp2_AVX |
| 104 | temp2_AVX = _mm256_add_ps(temp1_AVX, temp2_AVX); // low 32 bits of temp2_AVX now have the sum of the floats in a |
| 105 | |
| 106 | int *retint_ptr = (int *) ret_sum; // This is a horrible hack because there isn't an intrinsic to extract a float from |
| 107 | // an __m256. Do this to avoid casting an int back to a float and screwing it up |
| 108 | *retint_ptr = _mm256_extract_epi32((__m256i) temp2_AVX, 0); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /****************************************************************** |
| 113 | * 3. Inlined functions: left and right shift |
| 114 | ******************************************************************/ |
| 115 | |
| 116 | /* Function: esl_avx_rightshift_int8() |
| 117 | * Synopsis: Shift int8 vector elements to the right, shifting a -inf on. |
| 118 | * Incept: SRE, Sun Jun 4 17:12:07 2017 |
| 119 | * See: esl_sse.h::esl_sse_rightshift_int8() |
| 120 | */ |
| 121 | static inline __m256i |
| 122 | esl_avx_rightshift_int8(__m256i v, __m256i neginfmask) |
| 123 | { |
| 124 | return _mm256_or_si256(_mm256_alignr_epi8(v, _mm256_permute2x128_si256(v, v, _MM_SHUFFLE(0,0,3,0)), 15), neginfmask); |
| 125 | } |
| 126 | |
| 127 | /* Function: esl_avx_rightshift_int16() |
| 128 | * Synopsis: Shift int16 vector elements to the right, shifting a -inf on. |
| 129 | * Incept: SRE, Sun Jun 4 17:13:58 2017 |
| 130 | * See: esl_sse.h::esl_sse_rightshift_int16() |
| 131 | */ |
| 132 | static inline __m256i |
| 133 | esl_avx_rightshift_int16(__m256i v, __m256i neginfmask) |
| 134 | { |
| 135 | return _mm256_or_si256(_mm256_alignr_epi8(v, _mm256_permute2x128_si256(v, v, _MM_SHUFFLE(0,0,3,0)), 14), neginfmask); |
| 136 | } |
| 137 | |
| 138 | /* Function: esl_avx_rightshiftz_float() |
| 139 | * Synopsis: Shift float vector elements to the right, shifting zero on. |
| 140 | * Incept: SRE, Sun Jun 4 17:16:42 2017 |
| 141 | * See: esl_sse.h::esl_sse_rightshiftz_float() |
| 142 | */ |
| 143 | static inline __m256 |
| 144 | esl_avx_rightshiftz_float(__m256 v) |
| 145 | { |
| 146 | return ((__m256) _mm256_alignr_epi8((__m256i) v, _mm256_permute2x128_si256((__m256i) v, (__m256i) v, _MM_SHUFFLE(0,0,3,0) ), 12)); |
| 147 | } |
| 148 | |
| 149 | /* Function: esl_avx_leftshiftz_float() |
| 150 | * Synopsis: Shift float vector elements to the left, shifting zero on. |
| 151 | * Incept: SRE, Sun Jun 4 17:27:52 2017 |
| 152 | * See: esl_sse.h::esl_sse_leftshiftz_float() |
| 153 | */ |
| 154 | static inline __m256 |
| 155 | esl_avx_leftshiftz_float(__m256 v) |
| 156 | { |
| 157 | //permute result has vector[255:128] in low 128 bits, 0 in high 128 |
| 158 | return ((__m256) _mm256_alignr_epi8(_mm256_permute2x128_si256((__m256i) v, (__m256i) v, 0x81), (__m256i) v, 4)); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /****************************************************************** |
| 163 | * 4. Inlined functions: any_gt |
| 164 | ******************************************************************/ |
| 165 | |
| 166 | /* Function: esl_avx_any_gt_epi16() |
| 167 | * Synopsis: Return >0 if any a[z] > b[z] |
| 168 | */ |
| 169 | static inline int |
| 170 | esl_avx_any_gt_epi16(__m256i a, __m256i b) |
| 171 | { |
| 172 | return (_mm256_movemask_epi8(_mm256_cmpgt_epi16(a,b)) != 0); |
| 173 | } |
| 174 | |
| 175 | #endif /*eslAVX_INCLUDED*/ |
| 176 | #endif // eslENABLE_AVX |