SPH
Indices.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "objects/Object.h"
10 
12 
13 #define SPH_NO_ROUNDING_MODE
14 
16 class Indices {
17 private:
18  union {
19  __m128i i;
20  int a[4];
21  } data;
22 
23 
24 public:
25  INLINE Indices() = default;
26 
27  INLINE Indices(__m128i i) {
28  data.i = i;
29  }
30 
32  INLINE explicit Indices(const int value) {
33  data.i = _mm_set1_epi32(value);
34  }
35 
37  INLINE Indices(const int i, const int j, const int k, const int l = 0) {
38  data.i = _mm_set_epi32(l, k, j, i);
39  }
40 
42 #ifndef SPH_NO_ROUNDING_MODE
43  INLINE explicit Indices(const BasicVector<float>& v) {
44  data = _mm_cvtps_epi32(v.sse());
45  }
46 #else
47  INLINE explicit Indices(const BasicVector<float>& v)
48  : Indices(int(v[X]), int(v[Y]), int(v[Z]), int(v[H])) {}
49 #endif
50 
51  INLINE explicit Indices(const BasicVector<double>& v) {
53  *this = Indices(int(v[0]), int(v[1]), int(v[2]), int(v[3]));
54  }
55 
56  INLINE Indices(const Indices& other)
57  : data(other.data) {}
58 
60  INLINE static void init() {
61 #ifndef SPH_NO_ROUNDING_MODE
62  _MM_SET_ROUNDING_MODE(_MM_ROUND_DOWN);
63 #endif
64  }
65 
66  INLINE Indices& operator=(const Indices& other) {
67  data = other.data;
68  return *this;
69  }
70 
71 #ifndef SPH_NO_ROUNDING_MODE
72  INLINE operator BasicVector<float>() const {
73  return BasicVector<float>(_mm_cvtepi32_ps(data));
74  }
75 #else
76  INLINE operator BasicVector<float>() const {
77  return BasicVector<float>((*this)[0], (*this)[1], (*this)[2], (*this)[3]);
78  }
79 #endif
80 
81  INLINE operator BasicVector<double>() const {
83  return BasicVector<double>((*this)[0], (*this)[1], (*this)[2], (*this)[3]);
84  }
85 
86  INLINE int& operator[](const int idx) {
87  SPH_ASSERT(unsigned(idx) < 4);
88  return data.a[idx];
89  }
90 
91  INLINE int operator[](const int idx) const {
92  SPH_ASSERT(unsigned(idx) < 4);
93  return data.a[idx];
94  }
95 
96  INLINE Indices operator==(const Indices& other) const {
97  return _mm_cmpeq_epi32(data.i, other.data.i);
98  }
99 
101  return _mm_xor_si128(_mm_cmpeq_epi32(data.i, other.data.i), _mm_set1_epi32(-1));
102  }
103 
104  INLINE Indices operator>(const Indices& other) const {
105  return _mm_cmpgt_epi32(data.i, other.data.i);
106  }
107 
108  INLINE Indices operator<(const Indices& other) const {
109  return _mm_cmplt_epi32(data.i, other.data.i);
110  }
111 
112  INLINE Indices operator+(const Indices& other) const {
113  return _mm_add_epi32(data.i, other.data.i);
114  }
115 
116  INLINE Indices operator-(const Indices& other) const {
117  return _mm_sub_epi32(data.i, other.data.i);
118  }
119 
120  INLINE Indices max(const Indices& other) const {
121  return _mm_max_epi32(data.i, other.data.i);
122  }
123 
124  INLINE Indices min(const Indices& other) const {
125  return _mm_min_epi32(data.i, other.data.i);
126  }
127 
128  friend std::ostream& operator<<(std::ostream& stream, const Indices& idxs) {
129  for (int i = 0; i < 3; ++i) {
130  stream << std::setw(20) << idxs[i];
131  }
132  return stream;
133  }
134 };
135 
136 INLINE Indices max(const Indices i1, const Indices i2) {
137  return i1.max(i2);
138 }
139 
140 INLINE Indices min(const Indices i1, const Indices i2) {
141  return i1.min(i2);
142 }
143 
144 INLINE bool all(const Indices& i) {
145  return i[0] && i[1] && i[2];
146 }
147 
148 INLINE bool any(const Indices& i) {
149  return i[0] || i[1] || i[2];
150 }
151 
152 template <>
153 INLINE auto floor(const Vector& v) {
154  return Indices(int(std::floor(v[X])), int(std::floor(v[Y])), int(std::floor(v[Z])));
155 }
156 
157 struct IndicesEqual {
158  INLINE bool operator()(const Indices& i1, const Indices& i2) const {
159  return all(i1 == i2);
160  }
161 };
162 
164 
165 template <>
166 class std::hash<Sph::Indices> {
167 public:
168  INLINE size_t operator()(const Sph::Indices& idxs) const {
169  return (idxs[0] * 73856093ull) ^ (idxs[1] * 19349663ull) ^ (idxs[2] * 83492791ull);
170  }
171 };
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
INLINE auto floor(const Vector &v)
Definition: Indices.h:153
INLINE bool any(const Indices &i)
Definition: Indices.h:148
INLINE bool all(const Indices &i)
Definition: Indices.h:144
INLINE Indices max(const Indices i1, const Indices i2)
Definition: Indices.h:136
INLINE Indices min(const Indices i1, const Indices i2)
Definition: Indices.h:140
Common macros and basic objects.
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Basic vector algebra. Computations are accelerated using SIMD.
@ H
Definition: Vector.h:25
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
@ Z
Definition: Vector.h:24
specialization for doubles or units of double precision
Definition: Vector.h:371
3-dimensional vector, float precision
Definition: Vector.h:57
INLINE const __m128 & sse() const
Returns the data as SSE vector.
Definition: Vector.h:107
Helper object for storing three (possibly four) int or bool values.
Definition: Indices.h:16
INLINE Indices & operator=(const Indices &other)
Definition: Indices.h:66
INLINE Indices(__m128i i)
Definition: Indices.h:27
INLINE Indices max(const Indices &other) const
Definition: Indices.h:120
friend std::ostream & operator<<(std::ostream &stream, const Indices &idxs)
Definition: Indices.h:128
INLINE int & operator[](const int idx)
Definition: Indices.h:86
INLINE Indices operator>(const Indices &other) const
Definition: Indices.h:104
int a[4]
Definition: Indices.h:20
INLINE Indices()=default
INLINE Indices(const BasicVector< double > &v)
Definition: Indices.h:51
INLINE Indices(const Indices &other)
Definition: Indices.h:56
__m128i i
Definition: Indices.h:19
INLINE Indices operator<(const Indices &other) const
Definition: Indices.h:108
INLINE Indices operator!=(const Indices &other)
Definition: Indices.h:100
INLINE Indices operator==(const Indices &other) const
Definition: Indices.h:96
INLINE Indices(const int value)
Constructs indices from single value by copying it to all components.
Definition: Indices.h:32
INLINE Indices operator+(const Indices &other) const
Definition: Indices.h:112
static INLINE void init()
Must be called once before Indices are used.
Definition: Indices.h:60
INLINE Indices operator-(const Indices &other) const
Definition: Indices.h:116
INLINE Indices(const BasicVector< float > &v)
Constructs indices by casting components of vectors to ints.
Definition: Indices.h:47
INLINE Indices min(const Indices &other) const
Definition: Indices.h:124
INLINE int operator[](const int idx) const
Definition: Indices.h:91
INLINE Indices(const int i, const int j, const int k, const int l=0)
Constructs indices from values. Fourth component is optional.
Definition: Indices.h:37
INLINE size_t operator()(const Sph::Indices &idxs) const
Definition: Indices.h:168
Definition: MemoryPool.h:5
INLINE bool operator()(const Indices &i1, const Indices &i2) const
Definition: Indices.h:158