SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
simd_algorithm_sse4.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <array>
16 
20 
21 //-----------------------------------------------------------------------------
22 // forward declare sse4 simd algorithms that use sse4 intrinsics
23 //-----------------------------------------------------------------------------
24 
25 namespace seqan3::detail
26 {
30 template <simd::simd_concept simd_t>
31 constexpr simd_t load_sse4(void const * mem_addr);
32 
36 template <simd::simd_concept simd_t>
37 inline void transpose_matrix_sse4(std::array<simd_t, simd_traits<simd_t>::length> & matrix);
38 
42 template <simd::simd_concept target_simd_t, simd::simd_concept source_simd_t>
43 constexpr target_simd_t upcast_signed_sse4(source_simd_t const & src);
44 
48 template <simd::simd_concept target_simd_t, simd::simd_concept source_simd_t>
49 constexpr target_simd_t upcast_unsigned_sse4(source_simd_t const & src);
50 
54 template <uint8_t index, simd::simd_concept simd_t>
55 constexpr simd_t extract_half_sse4(simd_t const & src);
56 
60 template <uint8_t index, simd::simd_concept simd_t>
61 constexpr simd_t extract_quarter_sse4(simd_t const & src);
62 
66 template <uint8_t index, simd::simd_concept simd_t>
67 constexpr simd_t extract_eighth_sse4(simd_t const & src);
68 
69 }
70 
71 //-----------------------------------------------------------------------------
72 // implementation
73 //-----------------------------------------------------------------------------
74 
75 #ifdef __SSE4_2__
76 
77 namespace seqan3::detail
78 {
79 
80 template <simd::simd_concept simd_t>
81 constexpr simd_t load_sse4(void const * mem_addr)
82 {
83  return reinterpret_cast<simd_t>(_mm_loadu_si128(reinterpret_cast<__m128i const *>(mem_addr)));
84 }
85 
86 template <simd::simd_concept simd_t>
87 inline void transpose_matrix_sse4(std::array<simd_t, simd_traits<simd_t>::length> & matrix)
88 {
89  static_assert(simd_traits<simd_t>::length == simd_traits<simd_t>::max_length, "Expects byte scalar type.");
90  static_assert(is_native_builtin_simd_v<simd_t>, "The passed simd vector is not a native SSE4 simd vector type.");
91  static_assert(is_builtin_simd_v<simd_t>, "The passed simd vector is not a builtin vector type.");
92 
93  // we need a look-up table to reverse the lowest 4 bits
94  // in order to place the permute the transposed rows
95  constexpr std::array<char, 16> bit_reverse{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
96 
97  // transpose a 16x16 byte matrix
98  //
99  // matrix =
100  // A0 A1 A2 ... Ae Af
101  // B0 B1 B2 ... Be Bf
102  // ...
103  // P0 P1 P2 ... Pe Pf
104  __m128i tmp1[16];
105  for (int i = 0; i < 8; ++i)
106  {
107  tmp1[i] = _mm_unpacklo_epi8(reinterpret_cast<__m128i &>(matrix[2*i]),
108  reinterpret_cast<__m128i &>(matrix[2*i+1]));
109  tmp1[i+8] = _mm_unpackhi_epi8(reinterpret_cast<__m128i &>(matrix[2*i]),
110  reinterpret_cast<__m128i &>(matrix[2*i+1]));
111  }
112  // tmp1[0] = A0 B0 A1 B1 ... A7 B7
113  // tmp1[1] = C0 D0 C1 D1 ... C7 D7
114  // ...
115  // tmp1[7] = O0 P0 O1 P1 ... O7 P7
116  // tmp1[8] = A8 B8 A9 B9 ... Af Bf
117  // ...
118  // tmp1[15] = O8 P8 O9 P9 ... Of Pf
119  __m128i tmp2[16];
120  for (int i = 0; i < 8; ++i)
121  {
122  tmp2[i] = _mm_unpacklo_epi16(tmp1[2*i], tmp1[2*i+1]);
123  tmp2[i+8] = _mm_unpackhi_epi16(tmp1[2*i], tmp1[2*i+1]);
124  }
125  // tmp2[0] = A0 B0 C0 D0 ... A3 B3 C3 D3
126  // tmp2[1] = E0 F0 G0 H0 ... E3 F3 G3 H3
127  // ...
128  // tmp2[3] = M0 N0 O0 P0 ... M3 N3 O3 P3
129  // tmp2[4] = A8 B8 C8 D8 ... Ab Bb Cb Db
130  // ...
131  // tmp2[7] = M8 N8 O8 P8 ... Mb Nb Ob Pb
132  // tmp2[8] = A4 B4 C4 D4 ... A7 B7 C7 D7
133  // ..
134  // tmp2[12] = Ac Bc Cc Dc ... Af Bf Cf Df
135  // ...
136  // tmp2[15] = Mc Nc Oc Pc ... Mf Nf Of Pf
137  for (int i = 0; i < 8; ++i)
138  {
139  tmp1[i] = _mm_unpacklo_epi32(tmp2[2*i], tmp2[2*i+1]);
140  tmp1[i+8] = _mm_unpackhi_epi32(tmp2[2*i], tmp2[2*i+1]);
141  }
142  // tmp1[0] = A0 B0 .... H0 A1 B1 .... H1
143  // tmp1[1] = I0 J0 .... P0 I1 J1 .... P1
144  // ...
145  // tmp1[4] = A0 B0 .... H0 A1 B1 .... H1
146  // tmp1[1] = I0 J0 .... P0 I1 J1 .... P1
147  for (int i = 0; i < 8; ++i)
148  {
149  matrix[bit_reverse[i]] = reinterpret_cast<simd_t>(_mm_unpacklo_epi64(tmp1[2*i], tmp1[2*i+1]));
150  matrix[bit_reverse[i+8]] = reinterpret_cast<simd_t>(_mm_unpackhi_epi64(tmp1[2*i], tmp1[2*i+1]));
151  }
152 }
153 
154 template <simd::simd_concept target_simd_t, simd::simd_concept source_simd_t>
155 constexpr target_simd_t upcast_signed_sse4(source_simd_t const & src)
156 {
157  if constexpr (simd_traits<source_simd_t>::length == 16) // cast from epi8 ...
158  {
159  if constexpr (simd_traits<target_simd_t>::length == 8) // to epi16
160  return reinterpret_cast<target_simd_t>(_mm_cvtepi8_epi16(reinterpret_cast<__m128i const &>(src)));
161  if constexpr (simd_traits<target_simd_t>::length == 4) // to epi32
162  return reinterpret_cast<target_simd_t>(_mm_cvtepi8_epi32(reinterpret_cast<__m128i const &>(src)));
163  if constexpr (simd_traits<target_simd_t>::length == 2) // to epi64
164  return reinterpret_cast<target_simd_t>(_mm_cvtepi8_epi64(reinterpret_cast<__m128i const &>(src)));
165  }
166  else if constexpr (simd_traits<source_simd_t>::length == 8) // cast from epi16 ...
167  {
168  if constexpr (simd_traits<target_simd_t>::length == 4) // to epi32
169  return reinterpret_cast<target_simd_t>(_mm_cvtepi16_epi32(reinterpret_cast<__m128i const &>(src)));
170  if constexpr (simd_traits<target_simd_t>::length == 2) // to epi64
171  return reinterpret_cast<target_simd_t>(_mm_cvtepi16_epi64(reinterpret_cast<__m128i const &>(src)));
172  }
173  else // cast from epi32 to epi64
174  {
175  static_assert(simd_traits<source_simd_t>::length == 4, "Expected 32 bit scalar type.");
176  return reinterpret_cast<target_simd_t>(_mm_cvtepi32_epi64(reinterpret_cast<__m128i const &>(src)));
177  }
178 }
179 
180 template <simd::simd_concept target_simd_t, simd::simd_concept source_simd_t>
181 constexpr target_simd_t upcast_unsigned_sse4(source_simd_t const & src)
182 {
183  if constexpr (simd_traits<source_simd_t>::length == 16) // cast from epi8 ...
184  {
185  if constexpr (simd_traits<target_simd_t>::length == 8) // to epi16
186  return reinterpret_cast<target_simd_t>(_mm_cvtepu8_epi16(reinterpret_cast<__m128i const &>(src)));
187  if constexpr (simd_traits<target_simd_t>::length == 4) // to epi32
188  return reinterpret_cast<target_simd_t>(_mm_cvtepu8_epi32(reinterpret_cast<__m128i const &>(src)));
189  if constexpr (simd_traits<target_simd_t>::length == 2) // to epi64
190  return reinterpret_cast<target_simd_t>(_mm_cvtepu8_epi64(reinterpret_cast<__m128i const &>(src)));
191  }
192  else if constexpr (simd_traits<source_simd_t>::length == 8) // cast from epi16 ...
193  {
194  if constexpr (simd_traits<target_simd_t>::length == 4) // to epi32
195  return reinterpret_cast<target_simd_t>(_mm_cvtepu16_epi32(reinterpret_cast<__m128i const &>(src)));
196  if constexpr (simd_traits<target_simd_t>::length == 2) // to epi64
197  return reinterpret_cast<target_simd_t>(_mm_cvtepu16_epi64(reinterpret_cast<__m128i const &>(src)));
198  }
199  else // cast from epi32 to epi64
200  {
201  static_assert(simd_traits<source_simd_t>::length == 4, "Expected 32 bit scalar type.");
202  return reinterpret_cast<target_simd_t>(_mm_cvtepu32_epi64(reinterpret_cast<__m128i const &>(src)));
203  }
204 }
205 
206 template <uint8_t index, simd::simd_concept simd_t>
207 constexpr simd_t extract_half_sse4(simd_t const & src)
208 {
209  return reinterpret_cast<simd_t>(_mm_srli_si128(reinterpret_cast<__m128i const &>(src), (index) << 3));
210 }
211 
212 template <uint8_t index, simd::simd_concept simd_t>
213 constexpr simd_t extract_quarter_sse4(simd_t const & src)
214 {
215  return reinterpret_cast<simd_t>(_mm_srli_si128(reinterpret_cast<__m128i const &>(src), index << 2));
216 }
217 
218 template <uint8_t index, simd::simd_concept simd_t>
219 constexpr simd_t extract_eighth_sse4(simd_t const & src)
220 {
221  return reinterpret_cast<simd_t>(_mm_srli_si128(reinterpret_cast<__m128i const &>(src), index << 1));
222 }
223 
224 } // namespace seqan3::detail
225 
226 #endif // __SSE4_2__
concept.hpp
Provides seqan3::simd::simd_concept.
simd_traits.hpp
Provides seqan3::simd::simd_traits.
builtin_simd_intrinsics.hpp
Provides intrinsics include for builtin simd.
array