EigenRand  0.4.0-alpha
arch/SSE/PacketFilter.h
Go to the documentation of this file.
1 
12 #ifndef EIGENRAND_PACKET_FILTER_SSE_H
13 #define EIGENRAND_PACKET_FILTER_SSE_H
14 
15 #include <xmmintrin.h>
16 
17 namespace Eigen
18 {
19  namespace Rand
20  {
21  namespace detail
22  {
23  template<>
24  class CompressMask<16>
25  {
26  std::array<std::array<uint8_t, 16>, 7> idx;
27  std::array<internal::Packet4f, 4> selector;
28  std::array<uint8_t, 16> cnt;
29 
30  static uint8_t make_compress(int mask, int offset = 0)
31  {
32  uint8_t ret = 0;
33  int n = offset;
34  for (int i = 0; i < 4; ++i)
35  {
36  int l = mask & 1;
37  mask >>= 1;
38  if (l)
39  {
40  if (n >= 0) ret |= (i & 3) << (2 * n);
41  if (++n >= 4) break;
42  }
43  }
44  return ret;
45  }
46 
47  static uint8_t count(int mask)
48  {
49  uint8_t ret = 0;
50  for (int i = 0; i < 4; ++i)
51  {
52  ret += mask & 1;
53  mask >>= 1;
54  }
55  return ret;
56  }
57 
58  CompressMask()
59  {
60  for (int i = 0; i < 16; ++i)
61  {
62  for (int o = 0; o < 7; ++o)
63  {
64  idx[o][i] = make_compress(i, o < 4 ? o : o - 7);
65  }
66 
67  cnt[i] = count(i);
68  }
69 
70  selector[0] = _mm_castsi128_ps(_mm_setr_epi32(0, 0, 0, 0));
71  selector[1] = _mm_castsi128_ps(_mm_setr_epi32(-1, 0, 0, 0));
72  selector[2] = _mm_castsi128_ps(_mm_setr_epi32(-1, -1, 0, 0));
73  selector[3] = _mm_castsi128_ps(_mm_setr_epi32(-1, -1, -1, 0));
74  }
75 
76  static EIGEN_STRONG_INLINE internal::Packet4f permute(const internal::Packet4f& p, uint8_t i)
77  {
78  float u[4];
79  _mm_storeu_ps(u, p);
80  return _mm_setr_ps(u[i & 3], u[(i >> 2) & 3], u[(i >> 4) & 3], u[(i >> 6) & 3]);
81  }
82 
83  public:
84 
85  enum { full_size = 4 };
86 
87  static const CompressMask& get_inst()
88  {
89  static CompressMask cm;
90  return cm;
91  }
92 
93  template<typename Packet>
94  EIGEN_STRONG_INLINE int compress_append(Packet& _value, const Packet& _mask,
95  Packet& _rest, int rest_cnt, bool& full) const
96  {
97  auto& value = reinterpret_cast<internal::Packet4f&>(_value);
98  auto& mask = reinterpret_cast<const internal::Packet4f&>(_mask);
99  auto& rest = reinterpret_cast<internal::Packet4f&>(_rest);
100 
101  int m = _mm_movemask_ps(mask);
102  if (cnt[m] == full_size)
103  {
104  full = true;
105  return rest_cnt;
106  }
107 
108  auto p1 = permute(value, idx[rest_cnt][m]);
109  p1 = internal::pblendv(selector[rest_cnt], rest, p1);
110 
111  auto new_cnt = rest_cnt + cnt[m];
112  if (new_cnt >= full_size)
113  {
114  if (new_cnt > full_size)
115  {
116  rest = permute(value, idx[new_cnt - cnt[m] + full_size - 1][m]);
117  }
118  value = p1;
119  full = true;
120  return new_cnt - full_size;
121  }
122  else
123  {
124  rest = p1;
125  full = false;
126  return new_cnt;
127  }
128  }
129  };
130  }
131  }
132 }
133 #endif