File: sse_utils_cxx.h

package info (click to toggle)
healpix-cxx 3.50.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,008 kB
  • sloc: cpp: 13,882; ansic: 5,930; sh: 4,451; makefile: 216
file content (388 lines) | stat: -rw-r--r-- 12,947 bytes parent folder | download | duplicates (7)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 *  This file is part of libcxxsupport.
 *
 *  libcxxsupport is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  libcxxsupport is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with libcxxsupport; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
 *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
 *  (DLR).
 */

/*! \file sse_utils_cxx.h
 *  SSE/SSE2/SSE3-related functionality for C++
 *
 *  Copyright (C) 2011, 2012 Max-Planck-Society
 *  \author Martin Reinecke
 */

#ifndef PLANCK_SSE_UTILS_CXX_H
#define PLANCK_SSE_UTILS_CXX_H

template<typename T, int sz> class svec;

#if (defined(__SSE2__))

#include <xmmintrin.h>
#include <emmintrin.h>

template<> class svec<int, 4>
  {
  public:
    typedef int Ts;
    typedef __m128i Tv;
    typedef union { Tv v; Ts d[4]; } Tu;
    Tv v;

    svec () {}
    svec (const svec &b) : v(b.v) {}
    svec (const Tv &b) : v(b) {}
    svec (const Ts &val) : v(_mm_set1_epi32(val)) {}
    svec (const Ts &val1, const Ts &val2, const Ts &val3, const Ts &val4)
      : v(_mm_set_epi32(val4,val3,val2,val1)) {}

    const svec &operator= (const Ts &val)
      { v=_mm_set1_epi32(val); return *this; }
    const svec &operator= (const svec &b)
      { v=b.v; return *this; }

    Ts operator[] (int p) const
      { Tu u; u.v=v; return u.d[p]; }
    void set (int p, Ts val)
      { Tu u; u.v=v; u.d[p]=val; v=u.v; }

    const svec &operator+= (const svec &b)
      { v=_mm_add_epi32(v,b.v); return *this; }
    const svec &operator-= (const svec &b)
      { v=_mm_sub_epi32(v,b.v); return *this; }
    svec operator+ (const svec &b) const
      { return svec(_mm_add_epi32(v,b.v)); }
    svec operator- (const svec &b) const
      { return svec(_mm_sub_epi32(v,b.v)); }

    const svec &operator&= (const svec &b)
      { v=_mm_and_si128(v,b.v); return *this; }
    const svec &operator|= (const svec &b)
      { v=_mm_or_si128(v,b.v); return *this; }
    const svec &operator^= (const svec &b)
      { v=_mm_xor_si128(v,b.v); return *this; }
    svec operator& (const svec &b) const
      { return svec(_mm_and_si128(v,b.v)); }
    svec operator| (const svec &b) const
      { return svec(_mm_or_si128(v,b.v)); }
    svec operator^ (const svec &b) const
      { return svec(_mm_xor_si128(v,b.v)); }
    svec andnot (const svec &b) const
      { return svec(_mm_andnot_si128(v,b.v)); }

    const svec &operator<<= (int b)
      { v=_mm_slli_epi32(v,b); return *this; }
    svec operator<< (int b) const
      { return svec(_mm_slli_epi32(v,b)); }
    const svec &operator>>= (int b)
      { v=_mm_srai_epi32(v,b); return *this; }
    svec operator>> (int b) const
      { return svec(_mm_srai_epi32(v,b)); }

    svec eq (const svec &b) const
      { return svec(_mm_cmpeq_epi32(v,b.v)); }
    svec gt (const svec &b) const
      { return svec(_mm_cmpgt_epi32(v,b.v)); }
    svec lt (const svec &b) const
      { return svec(_mm_cmplt_epi32(v,b.v)); }
  };

typedef svec<int,4> V4si;

#if 0
template<> class svec<long long , 2>
  {
  public:
    typedef long long Ts;
    typedef __m128i Tv;
    typedef union { Tv v; Ts d[2]; } Tu;
    Tv v;

    svec () {}
    svec (const svec &b) : v(b.v) {}
    svec (const Tv &b) : v(b) {}
    svec (const Ts &val) : v(_mm_set1_epi64x(val)) {}
    svec (const Ts &val1, const Ts &val2)
      : v(_mm_set_epi64x(val2,val1)) {}

    const svec &operator= (const Ts &val)
      { v=_mm_set1_epi64x(val); return *this; }
    const svec &operator= (const svec &b)
      { v=b.v; return *this; }

    int operator[] (int p) const
      { Tu u; u.v=v; return u.d[p]; }
    void set (int p, int val)
      { Tu u; u.v=v; u.d[p]=val; v=u.v; }

    const svec &operator+= (const svec &b)
      { v=_mm_add_epi64(v,b.v); return *this; }
    const svec &operator-= (const svec &b)
      { v=_mm_sub_epi64(v,b.v); return *this; }
    svec operator+ (const svec &b) const
      { return svec(_mm_add_epi64(v,b.v)); }
    svec operator- (const svec &b) const
      { return svec(_mm_sub_epi64(v,b.v)); }

    const svec &operator&= (const svec &b)
      { v=_mm_and_si128(v,b.v); return *this; }
    const svec &operator|= (const svec &b)
      { v=_mm_or_si128(v,b.v); return *this; }
    const svec &operator^= (const svec &b)
      { v=_mm_xor_si128(v,b.v); return *this; }
    svec operator& (const svec &b) const
      { return svec(_mm_and_si128(v,b.v)); }
    svec operator| (const svec &b) const
      { return svec(_mm_or_si128(v,b.v)); }
    svec operator^ (const svec &b) const
      { return svec(_mm_xor_si128(v,b.v)); }
    svec andnot (const svec &b) const
      { return svec(_mm_andnot_si128(v,b.v)); }

    const svec &operator<<= (int b)
      { v=_mm_slli_epi64(v,b); return *this; }
    svec operator<< (int b) const
      { return svec(_mm_slli_epi64(v,b)); }
  };

typedef svec<long long,2> V2di;
#endif

template<> class svec<float, 4>
  {
  public:
    typedef float Ts;
    typedef __m128 Tv;
    typedef union { Tv v; Ts d[4]; } Tu;
    Tv v;

    svec () {}
    svec (const svec &b) : v(b.v) {}
    svec (const Tv &b) : v(b) {}
    svec (const Ts &val) : v(_mm_set1_ps(val)) {}
    svec (Ts val1, Ts val2, Ts val3, Ts val4)
      : v(_mm_set_ps(val4,val3,val2,val1)) {}
    explicit svec (const svec<int,4> &b) : v(_mm_cvtepi32_ps(b.v)) {}

    operator svec<int,4>() const
      { return svec<int,4> (_mm_cvtps_epi32(v)); }
    const svec &operator= (const Ts &val)
      { v=_mm_set1_ps(val); return *this; }
    const svec &operator= (const svec &b)
      { v=b.v; return *this; }

    Ts operator[] (int p) const
      { Tu u; u.v=v; return u.d[p]; }
    void set (int p, Ts val)
      { Tu u; u.v=v; u.d[p]=val; v=u.v; }

    const svec &operator+= (const svec &b)
      { v=_mm_add_ps(v,b.v); return *this; }
    const svec &operator-= (const svec &b)
      { v=_mm_sub_ps(v,b.v); return *this; }
    const svec &operator*= (const svec &b)
      { v=_mm_mul_ps(v,b.v); return *this; }
    const svec &operator/= (const svec &b)
      { v=_mm_div_ps(v,b.v); return *this; }

    svec operator+ (const svec &b) const
      { return svec(_mm_add_ps(v,b.v)); }
    svec operator- (const svec &b) const
      { return svec(_mm_sub_ps(v,b.v)); }
    svec operator* (const svec &b) const
      { return svec(_mm_mul_ps(v,b.v)); }
    svec operator/ (const svec &b) const
      { return svec(_mm_div_ps(v,b.v)); }

    const svec &operator&= (const svec &b)
      { v=_mm_and_ps(v,b.v); return *this; }
    const svec &operator|= (const svec &b)
      { v=_mm_or_ps(v,b.v); return *this; }
    const svec &operator^= (const svec &b)
      { v=_mm_xor_ps(v,b.v); return *this; }
    svec operator& (const svec &b) const
      { return svec(_mm_and_ps(v,b.v)); }
    svec andnot (const svec &b) const
      { return svec(_mm_andnot_ps(v,b.v)); }
    svec operator| (const svec &b) const
      { return svec(_mm_or_ps(v,b.v)); }
    svec operator^ (const svec &b) const
      { return svec(_mm_xor_ps(v,b.v)); }

    svec operator- () const
      { return svec(_mm_xor_ps(_mm_set1_ps(-0.),v)); }

    svec eq (const svec &b) const
      { return svec(_mm_cmpeq_ps(v,b.v)); }
    svec neq (const svec &b) const
      { return svec(_mm_cmpneq_ps(v,b.v)); }
    svec lt (const svec &b) const
      { return svec(_mm_cmplt_ps(v,b.v)); }
    svec le (const svec &b) const
      { return svec(_mm_cmple_ps(v,b.v)); }
    svec gt (const svec &b) const
      { return svec(_mm_cmpgt_ps(v,b.v)); }
    svec ge (const svec &b) const
      { return svec(_mm_cmpge_ps(v,b.v)); }

    void writeTo (Ts *val) const
      { _mm_storeu_ps (val, v); }
    void writeTo (Ts &a, Ts &b, Ts &c, Ts &d) const
      { Tu u; u.v=v; a=u.d[0]; b=u.d[1]; c=u.d[2]; d=u.d[3]; }
    void readFrom (const Ts *val)
      { v=_mm_loadu_ps(val); }
    void readFrom (Ts a, Ts b, Ts c, Ts d)
      { v=_mm_set_ps(d,c,b,a); }
  };

typedef svec<float,4> V4sf;

inline V4sf sqrt(const V4sf &v)
  { return V4sf(_mm_sqrt_ps(v.v)); }
inline V4sf abs(const V4sf &v)
  { return V4sf(_mm_andnot_ps(_mm_set1_ps(-0.),v.v)); }
inline V4sf blend(const V4sf &mask, const V4sf &a, const V4sf &b)
  { return (mask&a)|(mask.andnot(b)); }
inline bool any (const V4sf &a)
  { return _mm_movemask_ps(a.v)!=0; }
inline bool all (const V4sf &a)
  { return _mm_movemask_ps(a.v)==15; }
inline bool none (const V4sf &a)
  { return _mm_movemask_ps(a.v)==0; }
inline V4sf min (const V4sf &a, const V4sf &b)
  { return _mm_min_ps(a.v,b.v); }
inline V4sf max (const V4sf &a, const V4sf &b)
  { return _mm_max_ps(a.v,b.v); }

template<> class svec<double, 2>
  {
  public:
    typedef double Ts;
    typedef __m128d Tv;
    typedef union { Tv v; Ts d[2]; } Tu;
    Tv v;

    svec () {}
    svec (const svec &b) : v(b.v) {}
    svec (const Tv &b) : v(b) {}
    svec (const Ts &val) : v(_mm_set1_pd(val)) {}
    svec (const Ts &val1, const Ts &val2)
      : v(_mm_set_pd(val2,val1)) {}
    explicit svec (const svec<int,4> &b) : v(_mm_cvtepi32_pd(b.v)) {}

    operator svec<int,4>() const
      { return svec<int,4> (_mm_cvtpd_epi32(v)); }
    const svec &operator= (const Ts &val)
      { v=_mm_set1_pd(val); return *this; }
    const svec &operator= (const svec &b)
      { v=b.v; return *this; }

    Ts operator[] (int p) const
      { Tu u; u.v=v; return u.d[p]; }
    void set (int p, Ts val)
      { Tu u; u.v=v; u.d[p]=val; v=u.v; }

    const svec &operator+= (const svec &b)
      { v=_mm_add_pd(v,b.v); return *this; }
    const svec &operator-= (const svec &b)
      { v=_mm_sub_pd(v,b.v); return *this; }
    const svec &operator*= (const svec &b)
      { v=_mm_mul_pd(v,b.v); return *this; }
    const svec &operator/= (const svec &b)
      { v=_mm_div_pd(v,b.v); return *this; }

    svec operator+ (const svec &b) const
      { return svec(_mm_add_pd(v,b.v)); }
    svec operator- (const svec &b) const
      { return svec(_mm_sub_pd(v,b.v)); }
    svec operator* (const svec &b) const
      { return svec(_mm_mul_pd(v,b.v)); }
    svec operator/ (const svec &b) const
      { return svec(_mm_div_pd(v,b.v)); }

    const svec &operator&= (const svec &b)
      { v=_mm_and_pd(v,b.v); return *this; }
    const svec &operator|= (const svec &b)
      { v=_mm_or_pd(v,b.v); return *this; }
    const svec &operator^= (const svec &b)
      { v=_mm_xor_pd(v,b.v); return *this; }
    svec operator& (const svec &b) const
      { return svec(_mm_and_pd(v,b.v)); }
    svec operator| (const svec &b) const
      { return svec(_mm_or_pd(v,b.v)); }
    svec operator^ (const svec &b) const
      { return svec(_mm_xor_pd(v,b.v)); }

    svec operator- () const
      { return svec(_mm_xor_pd(_mm_set1_pd(-0.),v)); }

    svec eq (const svec &b) const
      { return svec(_mm_cmpeq_pd(v,b.v)); }
    svec neq (const svec &b) const
      { return svec(_mm_cmpneq_pd(v,b.v)); }
    svec lt (const svec &b) const
      { return svec(_mm_cmplt_pd(v,b.v)); }
    svec le (const svec &b) const
      { return svec(_mm_cmple_pd(v,b.v)); }
    svec gt (const svec &b) const
      { return svec(_mm_cmpgt_pd(v,b.v)); }
    svec ge (const svec &b) const
      { return svec(_mm_cmpge_pd(v,b.v)); }

    void writeTo (Ts *val) const
      { _mm_storeu_pd (val, v); }
    void writeTo (Ts &a, Ts &b) const
      { _mm_store_sd(&a,v); _mm_storeh_pd(&b,v); }
    void readFrom (const Ts *val)
      { v=_mm_loadu_pd(val); }
    void readFrom (const Ts &a, const Ts &b)
      { v=_mm_set_pd(b,a); }
  };

typedef svec<double,2> V2df;

inline V2df sqrt(const V2df &v)
  { return V2df(_mm_sqrt_pd(v.v)); }
inline V2df abs(const V2df &v)
  { return V2df(_mm_andnot_pd(_mm_set1_pd(-0.),v.v)); }
inline V2df blend(const V2df &mask, const V2df &a, const V2df &b)
  { return V2df(_mm_or_pd(_mm_and_pd(a.v,mask.v),_mm_andnot_pd(mask.v,b.v))); }
inline bool any (const V2df &a)
  { return _mm_movemask_pd(a.v)!=0; }
inline bool all (const V2df &a)
  { return _mm_movemask_pd(a.v)==3; }
inline bool none (const V2df &a)
  { return _mm_movemask_pd(a.v)==0; }

template<typename T> inline T vcast(const V4si &a);
template<typename T> inline T vcast(const V4sf &a);
template<typename T> inline T vcast(const V2df &a);

template<> inline V4si vcast (const V4sf &a)
  { return V4si (_mm_castps_si128(a.v)); }
template<> inline V4sf vcast (const V4si &a)
  { return V4sf (_mm_castsi128_ps(a.v)); }
template<> inline V2df vcast (const V4si &a)
  { return V2df (_mm_castsi128_pd(a.v)); }

#endif

#endif