aboutsummaryrefslogtreecommitdiff
path: root/x86_64-w64-mingw32/include/fvec.h
blob: 120ff96feb5de4eb3b3d415f1617d2a883cace66 (plain)
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
/**
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is part of the mingw-w64 runtime package.
 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
 */
#ifndef _FVEC_H_INCLUDED
#define _FVEC_H_INCLUDED

#ifndef RC_INVOKED
#ifndef __cplusplus
#error ERROR: This file is only supported in C++ compilations!
#endif

#include <intrin.h>
#include <assert.h>
#include <ivec.h>
#include <crtdefs.h>

#if defined(_ENABLE_VEC_DEBUG)
#include <iostream>
#endif

#pragma pack(push,_CRT_PACKING)

#ifdef __SSE__

#pragma pack(push,16)

#define EXPLICIT explicit

class F32vec4 {
protected:
  __m128 vec;
public:
  F32vec4() {}
  F32vec4(__m128 m) { vec = m;}
  F32vec4(float f3,float f2,float f1,float f0) { vec= _mm_set_ps(f3,f2,f1,f0); }
  EXPLICIT F32vec4(float f) { vec = _mm_set_ps1(f); }
  EXPLICIT F32vec4(double d) { vec = _mm_set_ps1((float) d); }
  F32vec4& operator =(float f) { vec = _mm_set_ps1(f); return *this; }
  F32vec4& operator =(double d) { vec = _mm_set_ps1((float) d); return *this; }
  operator __m128() const { return vec; }
  friend F32vec4 operator &(const F32vec4 &a,const F32vec4 &b) { return _mm_and_ps(a,b); }
  friend F32vec4 operator |(const F32vec4 &a,const F32vec4 &b) { return _mm_or_ps(a,b); }
  friend F32vec4 operator ^(const F32vec4 &a,const F32vec4 &b) { return _mm_xor_ps(a,b); }
  friend F32vec4 operator +(const F32vec4 &a,const F32vec4 &b) { return _mm_add_ps(a,b); }
  friend F32vec4 operator -(const F32vec4 &a,const F32vec4 &b) { return _mm_sub_ps(a,b); }
  friend F32vec4 operator *(const F32vec4 &a,const F32vec4 &b) { return _mm_mul_ps(a,b); }
  friend F32vec4 operator /(const F32vec4 &a,const F32vec4 &b) { return _mm_div_ps(a,b); }
  F32vec4& operator =(const F32vec4 &a) { vec = a.vec; return *this; }
  F32vec4& operator =(const __m128 &avec) { vec = avec; return *this; }
  F32vec4& operator +=(F32vec4 &a) { return *this = _mm_add_ps(vec,a); }
  F32vec4& operator -=(F32vec4 &a) { return *this = _mm_sub_ps(vec,a); }
  F32vec4& operator *=(F32vec4 &a) { return *this = _mm_mul_ps(vec,a); }
  F32vec4& operator /=(F32vec4 &a) { return *this = _mm_div_ps(vec,a); }
  F32vec4& operator &=(F32vec4 &a) { return *this = _mm_and_ps(vec,a); }
  F32vec4& operator |=(F32vec4 &a) { return *this = _mm_or_ps(vec,a); }
  F32vec4& operator ^=(F32vec4 &a) { return *this = _mm_xor_ps(vec,a); }
  friend float add_horizontal(F32vec4 &a) {
    F32vec4 ftemp = _mm_add_ss(a,_mm_add_ss(_mm_shuffle_ps(a,a,1),_mm_add_ss(_mm_shuffle_ps(a,a,2),_mm_shuffle_ps(a,a,3))));
    return ftemp[0];
  }
  friend F32vec4 sqrt(const F32vec4 &a) { return _mm_sqrt_ps(a); }
  friend F32vec4 rcp(const F32vec4 &a) { return _mm_rcp_ps(a); }
  friend F32vec4 rsqrt(const F32vec4 &a) { return _mm_rsqrt_ps(a); }
  friend F32vec4 rcp_nr(const F32vec4 &a) {
    F32vec4 Ra0 = _mm_rcp_ps(a);
    return _mm_sub_ps(_mm_add_ps(Ra0,Ra0),_mm_mul_ps(_mm_mul_ps(Ra0,a),Ra0));
  }
  friend F32vec4 rsqrt_nr(const F32vec4 &a) {
    static const F32vec4 fvecf0pt5(0.5f);
    static const F32vec4 fvecf3pt0(3.0f);
    F32vec4 Ra0 = _mm_rsqrt_ps(a);
    return (fvecf0pt5 *Ra0) *(fvecf3pt0 - (a *Ra0) *Ra0);

  }
#define Fvec32s4_COMP(op) friend F32vec4 cmp##op (const F32vec4 &a,const F32vec4 &b) { return _mm_cmp##op##_ps(a,b); }
  Fvec32s4_COMP(eq)
    Fvec32s4_COMP(lt)
    Fvec32s4_COMP(le)
    Fvec32s4_COMP(gt)
    Fvec32s4_COMP(ge)
    Fvec32s4_COMP(neq)
    Fvec32s4_COMP(nlt)
    Fvec32s4_COMP(nle)
    Fvec32s4_COMP(ngt)
    Fvec32s4_COMP(nge)
#undef Fvec32s4_COMP

    friend F32vec4 simd_min(const F32vec4 &a,const F32vec4 &b) { return _mm_min_ps(a,b); }
  friend F32vec4 simd_max(const F32vec4 &a,const F32vec4 &b) { return _mm_max_ps(a,b); }

#if defined(_ENABLE_VEC_DEBUG)
  friend std::ostream & operator<<(std::ostream & os,const F32vec4 &a) {
    float *fp = (float*)&a;
    os << "[3]:" << *(fp+3)
      << " [2]:" << *(fp+2)
      << " [1]:" << *(fp+1)
      << " [0]:" << *fp;
    return os;
  }
#endif
  const float& operator[](int i) const {
    assert((0 <= i) && (i <= 3));
    float *fp = (float*)&vec;
    return *(fp+i);
  }
  float& operator[](int i) {
    assert((0 <= i) && (i <= 3));
    float *fp = (float*)&vec;
    return *(fp+i);
  }
};

inline F32vec4 unpack_low(const F32vec4 &a,const F32vec4 &b) { return _mm_unpacklo_ps(a,b); }
inline F32vec4 unpack_high(const F32vec4 &a,const F32vec4 &b) { return _mm_unpackhi_ps(a,b); }
inline int move_mask(const F32vec4 &a) { return _mm_movemask_ps(a); }
inline void loadu(F32vec4 &a,float *p) { a = _mm_loadu_ps(p); }
inline void storeu(float *p,const F32vec4 &a) { _mm_storeu_ps(p,a); }
inline void store_nta(float *p,F32vec4 &a) { _mm_stream_ps(p,a); }

#define Fvec32s4_SELECT(op) inline F32vec4 select_##op (const F32vec4 &a,const F32vec4 &b,const F32vec4 &c,const F32vec4 &d) { F32vec4 mask = _mm_cmp##op##_ps(a,b); return((mask & c) | F32vec4((_mm_andnot_ps(mask,d)))); }
Fvec32s4_SELECT(eq)
Fvec32s4_SELECT(lt)
Fvec32s4_SELECT(le)
Fvec32s4_SELECT(gt)
Fvec32s4_SELECT(ge)
Fvec32s4_SELECT(neq)
Fvec32s4_SELECT(nlt)
Fvec32s4_SELECT(nle)
Fvec32s4_SELECT(ngt)
Fvec32s4_SELECT(nge)
#undef Fvec32s4_SELECT

#if 0 /* Commented until required types are defined */
inline Is16vec4 simd_max(const Is16vec4 &a,const Is16vec4 &b) { return _m_pmaxsw(a,b); }
inline Is16vec4 simd_min(const Is16vec4 &a,const Is16vec4 &b) { return _m_pminsw(a,b); }
inline Iu8vec8 simd_max(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pmaxub(a,b); }
inline Iu8vec8 simd_min(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pminub(a,b); }
inline Iu16vec4 simd_avg(const Iu16vec4 &a,const Iu16vec4 &b) { return _m_pavgw(a,b); }
inline Iu8vec8 simd_avg(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pavgb(a,b); }
inline int move_mask(const I8vec8 &a) { return _m_pmovmskb(a); }
inline Iu16vec4 mul_high(const Iu16vec4 &a,const Iu16vec4 &b) { return _m_pmulhuw(a,b); }
inline void mask_move(const I8vec8 &a,const I8vec8 &b,char *addr) { _m_maskmovq(a,b,addr); }
inline void store_nta(__m64 *p,M64 &a) { _mm_stream_pi(p,a); }
inline int F32vec4ToInt(const F32vec4 &a) { return _mm_cvtt_ss2si(a); }
inline Is32vec2 F32vec4ToIs32vec2 (const F32vec4 &a) {
  __m64 result;
  result = _mm_cvtt_ps2pi(a);
  return Is32vec2(result);
}
#endif

inline F32vec4 IntToF32vec4(const F32vec4 &a,int i) {
  __m128 result;
  result = _mm_cvt_si2ss(a,i);
  return F32vec4(result);
}

#if 0 /* Commented until required types are defined */
inline F32vec4 Is32vec2ToF32vec4(const F32vec4 &a,const Is32vec2 &b) {
  __m128 result;
  result = _mm_cvt_pi2ps(a,b);
  return F32vec4(result);
}
#endif

class F32vec1 {
protected:
  __m128 vec;
public:
  F32vec1() {}
  F32vec1(int i) { vec = _mm_cvt_si2ss(vec,i);};
  EXPLICIT F32vec1(float f) { vec = _mm_set_ss(f); }
  EXPLICIT F32vec1(double d) { vec = _mm_set_ss((float) d); }
  F32vec1(__m128 m) { vec = m; }
  operator __m128() const { return vec; }
  friend F32vec1 operator &(const F32vec1 &a,const F32vec1 &b) { return _mm_and_ps(a,b); }
  friend F32vec1 operator |(const F32vec1 &a,const F32vec1 &b) { return _mm_or_ps(a,b); }
  friend F32vec1 operator ^(const F32vec1 &a,const F32vec1 &b) { return _mm_xor_ps(a,b); }
  friend F32vec1 operator +(const F32vec1 &a,const F32vec1 &b) { return _mm_add_ss(a,b); }
  friend F32vec1 operator -(const F32vec1 &a,const F32vec1 &b) { return _mm_sub_ss(a,b); }
  friend F32vec1 operator *(const F32vec1 &a,const F32vec1 &b) { return _mm_mul_ss(a,b); }
  friend F32vec1 operator /(const F32vec1 &a,const F32vec1 &b) { return _mm_div_ss(a,b); }
  F32vec1& operator +=(F32vec1 &a) { return *this = _mm_add_ss(vec,a); }
  F32vec1& operator -=(F32vec1 &a) { return *this = _mm_sub_ss(vec,a); }
  F32vec1& operator *=(F32vec1 &a) { return *this = _mm_mul_ss(vec,a); }
  F32vec1& operator /=(F32vec1 &a) { return *this = _mm_div_ss(vec,a); }
  F32vec1& operator &=(F32vec1 &a) { return *this = _mm_and_ps(vec,a); }
  F32vec1& operator |=(F32vec1 &a) { return *this = _mm_or_ps(vec,a); }
  F32vec1& operator ^=(F32vec1 &a) { return *this = _mm_xor_ps(vec,a); }
  friend F32vec1 sqrt(const F32vec1 &a) { return _mm_sqrt_ss(a); }
  friend F32vec1 rcp(const F32vec1 &a) { return _mm_rcp_ss(a); }
  friend F32vec1 rsqrt(const F32vec1 &a) { return _mm_rsqrt_ss(a); }
  friend F32vec1 rcp_nr(const F32vec1 &a) {
    F32vec1 Ra0 = _mm_rcp_ss(a);
    return _mm_sub_ss(_mm_add_ss(Ra0,Ra0),_mm_mul_ss(_mm_mul_ss(Ra0,a),Ra0));
  }
  friend F32vec1 rsqrt_nr(const F32vec1 &a) {
    static const F32vec1 fvecf0pt5(0.5f);
    static const F32vec1 fvecf3pt0(3.0f);
    F32vec1 Ra0 = _mm_rsqrt_ss(a);
    return (fvecf0pt5 *Ra0) *(fvecf3pt0 - (a *Ra0) *Ra0);
  }
#define Fvec32s1_COMP(op) friend F32vec1 cmp##op (const F32vec1 &a,const F32vec1 &b) { return _mm_cmp##op##_ss(a,b); }
  Fvec32s1_COMP(eq)
    Fvec32s1_COMP(lt)
    Fvec32s1_COMP(le)
    Fvec32s1_COMP(gt)
    Fvec32s1_COMP(ge)
    Fvec32s1_COMP(neq)
    Fvec32s1_COMP(nlt)
    Fvec32s1_COMP(nle)
    Fvec32s1_COMP(ngt)
    Fvec32s1_COMP(nge)
#undef Fvec32s1_COMP

    friend F32vec1 simd_min(const F32vec1 &a,const F32vec1 &b) { return _mm_min_ss(a,b); }
  friend F32vec1 simd_max(const F32vec1 &a,const F32vec1 &b) { return _mm_max_ss(a,b); }

#if defined(_ENABLE_VEC_DEBUG)
  friend std::ostream & operator<<(std::ostream & os,const F32vec1 &a) {
    float *fp = (float*)&a;
    os << "float:" << *fp;
    return os;
  }
#endif
};

#define Fvec32s1_SELECT(op) inline F32vec1 select_##op (const F32vec1 &a,const F32vec1 &b,const F32vec1 &c,const F32vec1 &d) { F32vec1 mask = _mm_cmp##op##_ss(a,b); return((mask & c) | F32vec1((_mm_andnot_ps(mask,d)))); }
Fvec32s1_SELECT(eq)
Fvec32s1_SELECT(lt)
Fvec32s1_SELECT(le)
Fvec32s1_SELECT(gt)
Fvec32s1_SELECT(ge)
Fvec32s1_SELECT(neq)
Fvec32s1_SELECT(nlt)
Fvec32s1_SELECT(nle)
Fvec32s1_SELECT(ngt)
Fvec32s1_SELECT(nge)
#undef Fvec32s1_SELECT

inline int F32vec1ToInt(const F32vec1 &a)
{
  return _mm_cvtt_ss2si(a);
}

#pragma pack(pop)

#endif /* #ifdef __SSE__ */
#pragma pack(pop)
#endif
#endif