-
Notifications
You must be signed in to change notification settings - Fork 63
/
utuple.h
364 lines (331 loc) · 14.2 KB
/
utuple.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// This file is part of the uSTL library, an STL implementation.
//
// Copyright (c) 2005 by Mike Sharov <[email protected]>
// This file is free software, distributed under the MIT License.
#pragma once
#include "ualgo.h"
#include "metamac.h"
namespace ustl {
/// \class tuple utuple.h ustl.h
/// \ingroup Sequences
///
/// \brief A fixed-size array of \p N \p Ts.
///
template <size_t N, typename T>
class tuple {
public:
typedef T value_type;
typedef size_t size_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef pointer iterator;
typedef const_pointer const_iterator;
typedef ::ustl::reverse_iterator<iterator> reverse_iterator;
typedef ::ustl::reverse_iterator<const_iterator> const_reverse_iterator;
typedef pair<iterator,iterator> range_t;
typedef pair<const_iterator,const_iterator> const_range_t;
typedef std::initializer_list<value_type> initlist_t;
public:
template <typename T2>
inline tuple (const tuple<N,T2>& t);
inline tuple (const tuple<N,T>& t);
inline tuple (const_pointer v);
inline tuple (void);
inline explicit tuple (const_reference v0);
inline tuple (const_reference v0, const_reference v1);
inline tuple (const_reference v0, const_reference v1, const_reference v2);
inline tuple (const_reference v0, const_reference v1, const_reference v2, const_reference v3);
inline iterator begin (void) { return (m_v); }
inline const_iterator begin (void) const { return (m_v); }
inline iterator end (void) { return (begin() + N); }
inline const_iterator end (void) const { return (begin() + N); }
inline size_type size (void) const { return (N); }
inline size_type max_size (void) const { return (N); }
inline bool empty (void) const { return (N == 0); }
inline const_reference at (size_type i) const { return (m_v[i]); }
inline reference at (size_type i) { return (m_v[i]); }
inline const_reference operator[] (size_type i) const { return (m_v[i]); }
inline reference operator[] (size_type i) { return (m_v[i]); }
template <typename T2>
inline tuple& operator= (const tuple<N,T2>& src);
inline tuple& operator= (const tuple<N,T>& src);
inline tuple& operator+= (const_reference v)
{ for (uoff_t i = 0; i < N; ++ i) m_v[i] += v; return (*this); }
inline tuple& operator-= (const_reference v)
{ for (uoff_t i = 0; i < N; ++ i) m_v[i] -= v; return (*this); }
inline tuple& operator*= (const_reference v)
{ for (uoff_t i = 0; i < N; ++ i) m_v[i] *= v; return (*this); }
inline tuple& operator/= (const_reference v)
{ for (uoff_t i = 0; i < N; ++ i) m_v[i] /= v; return (*this); }
inline tuple operator+ (const_reference v) const
{ tuple result; for (uoff_t i = 0; i < N; ++ i) result[i] = m_v[i] + v; return (result); }
inline tuple operator- (const_reference v) const
{ tuple result; for (uoff_t i = 0; i < N; ++ i) result[i] = m_v[i] - v; return (result); }
inline tuple operator* (const_reference v) const
{ tuple result; for (uoff_t i = 0; i < N; ++ i) result[i] = m_v[i] * v; return (result); }
inline tuple operator/ (const_reference v) const
{ tuple result; for (uoff_t i = 0; i < N; ++ i) result[i] = m_v[i] / v; return (result); }
inline void swap (tuple<N,T>& v)
{ for (uoff_t i = 0; i < N; ++ i) ::ustl::swap (m_v[i], v.m_v[i]); }
inline void read (istream& is) { nr_container_read (is, *this); }
inline void write (ostream& os) const { nr_container_write (os, *this); }
inline void text_write (ostringstream& os) const { container_text_write (os, *this); }
inline size_t stream_size (void) const { return (nr_container_stream_size (*this)); }
#if HAVE_CPP11
inline tuple (initlist_t v) { assign (v); }
inline tuple& assign (initlist_t v);
inline tuple& operator= (initlist_t v) { return (assign(v)); }
inline tuple& operator+= (initlist_t v)
{ for (uoff_t i = 0; i < min(N,v.size()); ++ i) m_v[i] += v.begin()[i]; return (*this); }
inline tuple& operator-= (initlist_t v)
{ for (uoff_t i = 0; i < min(N,v.size()); ++ i) m_v[i] -= v.begin()[i]; return (*this); }
inline tuple& operator*= (initlist_t v)
{ for (uoff_t i = 0; i < min(N,v.size()); ++ i) m_v[i] *= v.begin()[i]; return (*this); }
inline tuple& operator/= (initlist_t v)
{ for (uoff_t i = 0; i < min(N,v.size()); ++ i) m_v[i] /= v.begin()[i]; return (*this); }
#endif
private:
T m_v [N];
};
} // namespace ustl
#include "simd.h"
namespace ustl {
template <size_t N, typename T>
template <typename T2>
inline tuple<N,T>::tuple (const tuple<N,T2>& t)
{ simd::pconvert (t, *this, simd::fcast<T2,T>()); }
template <size_t N, typename T>
inline tuple<N,T>::tuple (const tuple<N,T>& t)
{ simd::passign (t, *this); }
template <size_t N, typename T>
inline tuple<N,T>::tuple (const_pointer v)
{ simd::ipassign (v, *this); }
template <size_t N, typename T>
inline tuple<N,T>::tuple (void)
{ fill_n (m_v, N, T()); }
template <size_t N, typename T>
inline tuple<N,T>::tuple (const_reference v0)
{ fill_n (m_v, N, v0); }
template <size_t N, typename T>
inline tuple<N,T>::tuple (const_reference v0, const_reference v1)
{
m_v[0] = v0;
fill_n (m_v+1, N-1, v1);
}
template <size_t N, typename T>
inline tuple<N,T>::tuple (const_reference v0, const_reference v1, const_reference v2)
{
m_v[0] = v0;
m_v[1] = v1;
fill_n (m_v+2, N-2, v2);
}
template <size_t N, typename T>
inline tuple<N,T>::tuple (const_reference v0, const_reference v1, const_reference v2, const_reference v3)
{
m_v[0] = v0;
m_v[1] = v1;
m_v[2] = v2;
fill_n (m_v+3, N-3, v3);
}
#if HAVE_CPP11
template <size_t N, typename T>
inline tuple<N,T>& tuple<N,T>::assign (initlist_t v)
{
const size_t isz = min (v.size(), N);
copy_n (v.begin(), isz, begin());
fill_n (begin()+isz, N-isz, T());
return (*this);
}
#endif
template <size_t N, typename T>
template <typename T2>
inline tuple<N,T>& tuple<N,T>::operator= (const tuple<N,T2>& src)
{ simd::pconvert (src, *this, simd::fcast<T2,T>()); return (*this); }
template <size_t N, typename T>
inline tuple<N,T>& tuple<N,T>::operator= (const tuple<N,T>& src)
{ simd::passign (src, *this); return (*this); }
template <size_t N, typename T1, typename T2>
inline bool operator== (const tuple<N,T1>& t1, const tuple<N,T2>& t2)
{
for (uoff_t i = 0; i < N; ++ i)
if (t1[i] != t2[i])
return (false);
return (true);
}
template <size_t N, typename T1, typename T2>
inline bool operator< (const tuple<N,T1>& t1, const tuple<N,T2>& t2)
{
for (uoff_t i = 0; i < N && t1[i] <= t2[i]; ++ i)
if (t1[i] < t2[i])
return (true);
return (false);
}
template <size_t N, typename T1, typename T2>
inline tuple<N,T1>& operator+= (tuple<N,T1>& t1, const tuple<N,T2>& t2)
{ for (uoff_t i = 0; i < N; ++ i) t1[i] = T1(t1[i] + t2[i]); return (t1); }
template <size_t N, typename T1, typename T2>
inline tuple<N,T1>& operator-= (tuple<N,T1>& t1, const tuple<N,T2>& t2)
{ for (uoff_t i = 0; i < N; ++ i) t1[i] = T1(t1[i] - t2[i]); return (t1); }
template <size_t N, typename T1, typename T2>
inline tuple<N,T1>& operator*= (tuple<N,T1>& t1, const tuple<N,T2>& t2)
{ for (uoff_t i = 0; i < N; ++ i) t1[i] = T1(t1[i] * t2[i]); return (t1); }
template <size_t N, typename T1, typename T2>
inline tuple<N,T1>& operator/= (tuple<N,T1>& t1, const tuple<N,T2>& t2)
{ for (uoff_t i = 0; i < N; ++ i) t1[i] = T1(t1[i] / t2[i]); return (t1); }
template <size_t N, typename T1, typename T2>
inline tuple<N,T1> operator+ (const tuple<N,T1>& t1, const tuple<N,T2>& t2)
{
tuple<N,T1> result;
for (uoff_t i = 0; i < N; ++ i) result[i] = T1(t1[i] + t2[i]);
return (result);
}
template <size_t N, typename T1, typename T2>
inline tuple<N,T1> operator- (const tuple<N,T1>& t1, const tuple<N,T2>& t2)
{
tuple<N,T1> result;
for (uoff_t i = 0; i < N; ++ i) result[i] = T1(t1[i] - t2[i]);
return (result);
}
template <size_t N, typename T1, typename T2>
inline tuple<N,T1> operator* (const tuple<N,T1>& t1, const tuple<N,T2>& t2)
{
tuple<N,T1> result;
for (uoff_t i = 0; i < N; ++ i) result[i] = T1(t1[i] * t2[i]);
return (result);
}
template <size_t N, typename T1, typename T2>
inline tuple<N,T1> operator/ (const tuple<N,T1>& t1, const tuple<N,T2>& t2)
{
tuple<N,T1> result;
for (uoff_t i = 0; i < N; ++ i) result[i] = T1(t1[i] / t2[i]);
return (result);
}
//----------------------------------------------------------------------
// Define SIMD specializations for member functions.
#if CPU_HAS_SSE
#define SSE_TUPLE_SPECS(n,type) \
template <> inline tuple<n,type>::tuple (void) \
{ asm("xorps %%xmm0, %%xmm0\n\tmovups %%xmm0, %0":"+m"(m_v[0])::"xmm0","memory"); } \
template<> inline void tuple<n,type>::swap (tuple<n,type>& v) \
{ \
asm ("movups %0,%%xmm0\n\tmovups %1,%%xmm1\n\t" \
"movups %%xmm0,%1\n\tmovups %%xmm1,%0" \
: "+m"(m_v[0]), "+m"(v.m_v[0]) :: "xmm0","xmm1","memory"); \
}
SSE_TUPLE_SPECS(4,float)
SSE_TUPLE_SPECS(4,int32_t)
SSE_TUPLE_SPECS(4,uint32_t)
#undef SSE_TUPLE_SPECS
#endif
#if SIZE_OF_LONG == 8 && __GNUC__
#define LONG_TUPLE_SPECS(n,type) \
template <> inline tuple<n,type>::tuple (void) \
{ asm("":"+m"(m_v[0])::"memory"); \
*noalias_cast<long*>(m_v) = 0; } \
template<> inline void tuple<n,type>::swap (tuple<n,type>& v) \
{ asm("":"+m"(m_v[0]),"+m"(v.m_v[0])::"memory"); \
iter_swap (noalias_cast<long*>(m_v), noalias_cast<long*>(v.m_v)); \
asm("":"+m"(m_v[0]),"+m"(v.m_v[0])::"memory"); \
}
LONG_TUPLE_SPECS(2,float)
LONG_TUPLE_SPECS(4,int16_t)
LONG_TUPLE_SPECS(4,uint16_t)
LONG_TUPLE_SPECS(2,int32_t)
LONG_TUPLE_SPECS(2,uint32_t)
LONG_TUPLE_SPECS(8,int8_t)
LONG_TUPLE_SPECS(8,uint8_t)
#undef LONG_TUPLE_SPECS
#elif CPU_HAS_MMX
#define MMX_TUPLE_SPECS(n,type) \
template <> inline tuple<n,type>::tuple (void) \
{ asm ("pxor %%mm0, %%mm0\n\tmovq %%mm0, %0" \
:"=m"(m_v[0])::"mm0","st","memory"); simd::reset_mmx(); } \
template<> inline void tuple<n,type>::swap (tuple<n,type>& v) \
{ asm ("movq %2,%%mm0\n\tmovq %3,%%mm1\n\t" \
"movq %%mm0,%1\n\tmovq %%mm1,%0" \
:"=m"(m_v[0]),"=m"(v.m_v[0]):"m"(m_v[0]),"m"(v.m_v[0]):"mm0","mm1","st","st(1)","memory"); \
simd::reset_mmx(); \
}
MMX_TUPLE_SPECS(2,float)
MMX_TUPLE_SPECS(4,int16_t)
MMX_TUPLE_SPECS(4,uint16_t)
MMX_TUPLE_SPECS(2,int32_t)
MMX_TUPLE_SPECS(2,uint32_t)
MMX_TUPLE_SPECS(8,int8_t)
MMX_TUPLE_SPECS(8,uint8_t)
#undef MMX_TUPLE_SPECS
#endif
#if __i386__ || __x86_64__
#define UINT32_TUPLE_SPECS(type,otype) \
template <> inline tuple<2,type>::tuple (void) \
{ asm("":"+m"(m_v[0]),"+m"(m_v[1])::"memory"); \
*noalias_cast<uint32_t*>(m_v) = 0; \
asm("":"+m"(m_v[0]),"+m"(m_v[1])::"memory"); }\
template <> inline tuple<2,type>& tuple<2,type>::operator= (const tuple<2,type>& v)\
{ asm ("mov %3, %0" \
:"=m"(*noalias_cast<uint32_t*>(m_v)),"=m"(m_v[0]),"=m"(m_v[1]) \
:"r"(*noalias_cast<const uint32_t*>(v.begin())),"m"(v[0]),"m"(v[1]):"memory"); \
return (*this); } \
template <> template <> \
inline tuple<2,type>& tuple<2,type>::operator= (const tuple<2,otype>& v)\
{ asm ("mov %3, %0" \
:"=m"(*noalias_cast<uint32_t*>(m_v)),"=m"(m_v[0]),"=m"(m_v[1]) \
:"r"(*noalias_cast<const uint32_t*>(v.begin())),"m"(v[0]),"m"(v[1]):"memory"); \
return (*this); } \
template <> inline tuple<2,type>::tuple (const tuple<2,type>& v) \
{ operator= (v); } \
template <> template <> \
inline tuple<2,type>::tuple (const tuple<2,otype>& v) \
{ operator= (v); } \
template<> inline void tuple<2,type>::swap (tuple<2,type>& v) \
{ asm(""::"m"(m_v[0]),"m"(m_v[1]),"m"(v.m_v[0]),"m"(v.m_v[1]):"memory");\
iter_swap (noalias_cast<uint32_t*>(m_v), noalias_cast<uint32_t*>(v.m_v)); \
asm("":"=m"(m_v[0]),"=m"(m_v[1]),"=m"(v.m_v[0]),"=m"(v.m_v[1])::"memory"); } \
template <> inline tuple<2,type>& operator+= (tuple<2,type>& t1, const tuple<2,type>& t2) \
{ t1[0] += t2[0]; t1[1] += t2[1]; return (t1); } \
template <> inline tuple<2,type>& operator-= (tuple<2,type>& t1, const tuple<2,type>& t2) \
{ t1[0] -= t2[0]; t1[1] -= t2[1]; return (t1); } \
template <> inline tuple<2,type> operator+ (const tuple<2,type>& t1, const tuple<2,type>& t2) \
{ return (tuple<2,type> (t1[0] + t2[0], t1[1] + t2[1])); } \
template <> inline tuple<2,type> operator- (const tuple<2,type>& t1, const tuple<2,type>& t2) \
{ return (tuple<2,type> (t1[0] - t2[0], t1[1] - t2[1])); }
UINT32_TUPLE_SPECS(int16_t,uint16_t)
UINT32_TUPLE_SPECS(uint16_t,int16_t)
#undef UINT32_TUPLE_SPECS
#endif
#undef TUPLEV_R1
#undef TUPLEV_R2
#undef TUPLEV_W1
#undef TUPLEV_W2
#define SIMD_TUPLE_PACKOP(N,T) \
template <> inline tuple<N,T>& operator+= (tuple<N,T>& t1, const tuple<N,T>& t2) \
{ simd::padd (t2, t1); return (t1); } \
template <> inline tuple<N,T>& operator-= (tuple<N,T>& t1, const tuple<N,T>& t2) \
{ simd::psub (t2, t1); return (t1); } \
template <> inline tuple<N,T>& operator*= (tuple<N,T>& t1, const tuple<N,T>& t2) \
{ simd::pmul (t2, t1); return (t1); } \
template <> inline tuple<N,T>& operator/= (tuple<N,T>& t1, const tuple<N,T>& t2) \
{ simd::pdiv (t2, t1); return (t1); } \
template <> inline tuple<N,T> operator+ (const tuple<N,T>& t1, const tuple<N,T>& t2) \
{ tuple<N,T> result (t1); simd::padd (t2, result); return (result); } \
template <> inline tuple<N,T> operator- (const tuple<N,T>& t1, const tuple<N,T>& t2) \
{ tuple<N,T> result (t1); simd::psub (t2, result); return (result); } \
template <> inline tuple<N,T> operator* (const tuple<N,T>& t1, const tuple<N,T>& t2) \
{ tuple<N,T> result (t1); simd::pmul (t2, result); return (result); } \
template <> inline tuple<N,T> operator/ (const tuple<N,T>& t1, const tuple<N,T>& t2) \
{ tuple<N,T> result (t1); simd::pdiv (t2, result); return (result); }
SIMD_TUPLE_PACKOP(4,float)
SIMD_TUPLE_PACKOP(2,float)
SIMD_TUPLE_PACKOP(2,double)
SIMD_TUPLE_PACKOP(4,int32_t)
SIMD_TUPLE_PACKOP(4,uint32_t)
SIMD_TUPLE_PACKOP(4,int16_t)
SIMD_TUPLE_PACKOP(4,uint16_t)
SIMD_TUPLE_PACKOP(2,int32_t)
SIMD_TUPLE_PACKOP(2,uint32_t)
SIMD_TUPLE_PACKOP(8,int8_t)
SIMD_TUPLE_PACKOP(8,uint8_t)
#undef SIMD_TUPLE_PACKOP
} // namespace ustl