comparison fuhtark_test/include/fvec.h @ 1500:91c8c3b7cbf0

add: futhark tests for generating vulkan api
author sam <sam@basx.dev>
date Wed, 26 Nov 2025 21:36:48 +0700
parents
children
comparison
equal deleted inserted replaced
1499:1f58458b7ef7 1500:91c8c3b7cbf0
1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the w64 mingw-runtime package.
4 * No warranty is given; refer to the file DISCLAIMER within this package.
5 */
6 #ifndef _FVEC_H_INCLUDED
7 #define _FVEC_H_INCLUDED
8
9 #ifndef RC_INVOKED
10 #ifndef __cplusplus
11 #error ERROR: This file is only supported in C++ compilations!
12 #endif
13
14 #include <intrin.h>
15 #include <assert.h>
16 #include <ivec.h>
17 #include <_mingw.h>
18
19 #if defined(_ENABLE_VEC_DEBUG)
20 #include <iostream>
21 #endif
22
23 #pragma pack(push,_CRT_PACKING)
24
25 #ifdef __SSE__
26
27 #pragma pack(push,16)
28
29 #define EXPLICIT explicit
30
31 class F32vec4 {
32 protected:
33 __m128 vec;
34 public:
35 F32vec4() {}
36 F32vec4(__m128 m) { vec = m;}
37 F32vec4(float f3,float f2,float f1,float f0) { vec= _mm_set_ps(f3,f2,f1,f0); }
38 EXPLICIT F32vec4(float f) { vec = _mm_set_ps1(f); }
39 EXPLICIT F32vec4(double d) { vec = _mm_set_ps1((float) d); }
40 F32vec4& operator =(float f) { vec = _mm_set_ps1(f); return *this; }
41 F32vec4& operator =(double d) { vec = _mm_set_ps1((float) d); return *this; }
42 operator __m128() const { return vec; }
43 friend F32vec4 operator &(const F32vec4 &a,const F32vec4 &b) { return _mm_and_ps(a,b); }
44 friend F32vec4 operator |(const F32vec4 &a,const F32vec4 &b) { return _mm_or_ps(a,b); }
45 friend F32vec4 operator ^(const F32vec4 &a,const F32vec4 &b) { return _mm_xor_ps(a,b); }
46 friend F32vec4 operator +(const F32vec4 &a,const F32vec4 &b) { return _mm_add_ps(a,b); }
47 friend F32vec4 operator -(const F32vec4 &a,const F32vec4 &b) { return _mm_sub_ps(a,b); }
48 friend F32vec4 operator *(const F32vec4 &a,const F32vec4 &b) { return _mm_mul_ps(a,b); }
49 friend F32vec4 operator /(const F32vec4 &a,const F32vec4 &b) { return _mm_div_ps(a,b); }
50 F32vec4& operator =(const F32vec4 &a) { vec = a.vec; return *this; }
51 F32vec4& operator =(const __m128 &avec) { vec = avec; return *this; }
52 F32vec4& operator +=(F32vec4 &a) { return *this = _mm_add_ps(vec,a); }
53 F32vec4& operator -=(F32vec4 &a) { return *this = _mm_sub_ps(vec,a); }
54 F32vec4& operator *=(F32vec4 &a) { return *this = _mm_mul_ps(vec,a); }
55 F32vec4& operator /=(F32vec4 &a) { return *this = _mm_div_ps(vec,a); }
56 F32vec4& operator &=(F32vec4 &a) { return *this = _mm_and_ps(vec,a); }
57 F32vec4& operator |=(F32vec4 &a) { return *this = _mm_or_ps(vec,a); }
58 F32vec4& operator ^=(F32vec4 &a) { return *this = _mm_xor_ps(vec,a); }
59 friend float add_horizontal(F32vec4 &a) {
60 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))));
61 return ftemp[0];
62 }
63 friend F32vec4 sqrt(const F32vec4 &a) { return _mm_sqrt_ps(a); }
64 friend F32vec4 rcp(const F32vec4 &a) { return _mm_rcp_ps(a); }
65 friend F32vec4 rsqrt(const F32vec4 &a) { return _mm_rsqrt_ps(a); }
66 friend F32vec4 rcp_nr(const F32vec4 &a) {
67 F32vec4 Ra0 = _mm_rcp_ps(a);
68 return _mm_sub_ps(_mm_add_ps(Ra0,Ra0),_mm_mul_ps(_mm_mul_ps(Ra0,a),Ra0));
69 }
70 friend F32vec4 rsqrt_nr(const F32vec4 &a) {
71 static const F32vec4 fvecf0pt5(0.5f);
72 static const F32vec4 fvecf3pt0(3.0f);
73 F32vec4 Ra0 = _mm_rsqrt_ps(a);
74 return (fvecf0pt5 *Ra0) *(fvecf3pt0 - (a *Ra0) *Ra0);
75
76 }
77 #define Fvec32s4_COMP(op) friend F32vec4 cmp##op (const F32vec4 &a,const F32vec4 &b) { return _mm_cmp##op##_ps(a,b); }
78 Fvec32s4_COMP(eq)
79 Fvec32s4_COMP(lt)
80 Fvec32s4_COMP(le)
81 Fvec32s4_COMP(gt)
82 Fvec32s4_COMP(ge)
83 Fvec32s4_COMP(neq)
84 Fvec32s4_COMP(nlt)
85 Fvec32s4_COMP(nle)
86 Fvec32s4_COMP(ngt)
87 Fvec32s4_COMP(nge)
88 #undef Fvec32s4_COMP
89
90 friend F32vec4 simd_min(const F32vec4 &a,const F32vec4 &b) { return _mm_min_ps(a,b); }
91 friend F32vec4 simd_max(const F32vec4 &a,const F32vec4 &b) { return _mm_max_ps(a,b); }
92
93 #if defined(_ENABLE_VEC_DEBUG)
94 friend std::ostream & operator<<(std::ostream & os,const F32vec4 &a) {
95 float *fp = (float*)&a;
96 os << "[3]:" << *(fp+3)
97 << " [2]:" << *(fp+2)
98 << " [1]:" << *(fp+1)
99 << " [0]:" << *fp;
100 return os;
101 }
102 #endif
103 const float& operator[](int i) const {
104 assert((0 <= i) && (i <= 3));
105 float *fp = (float*)&vec;
106 return *(fp+i);
107 }
108 float& operator[](int i) {
109 assert((0 <= i) && (i <= 3));
110 float *fp = (float*)&vec;
111 return *(fp+i);
112 }
113 };
114
115 inline F32vec4 unpack_low(const F32vec4 &a,const F32vec4 &b) { return _mm_unpacklo_ps(a,b); }
116 inline F32vec4 unpack_high(const F32vec4 &a,const F32vec4 &b) { return _mm_unpackhi_ps(a,b); }
117 inline int move_mask(const F32vec4 &a) { return _mm_movemask_ps(a); }
118 inline void loadu(F32vec4 &a,float *p) { a = _mm_loadu_ps(p); }
119 inline void storeu(float *p,const F32vec4 &a) { _mm_storeu_ps(p,a); }
120 inline void store_nta(float *p,F32vec4 &a) { _mm_stream_ps(p,a); }
121
122 #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)))); }
123 Fvec32s4_SELECT(eq)
124 Fvec32s4_SELECT(lt)
125 Fvec32s4_SELECT(le)
126 Fvec32s4_SELECT(gt)
127 Fvec32s4_SELECT(ge)
128 Fvec32s4_SELECT(neq)
129 Fvec32s4_SELECT(nlt)
130 Fvec32s4_SELECT(nle)
131 Fvec32s4_SELECT(ngt)
132 Fvec32s4_SELECT(nge)
133 #undef Fvec32s4_SELECT
134
135 inline Is16vec4 simd_max(const Is16vec4 &a,const Is16vec4 &b) { return _m_pmaxsw(a,b); }
136 inline Is16vec4 simd_min(const Is16vec4 &a,const Is16vec4 &b) { return _m_pminsw(a,b); }
137 inline Iu8vec8 simd_max(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pmaxub(a,b); }
138 inline Iu8vec8 simd_min(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pminub(a,b); }
139 inline Iu16vec4 simd_avg(const Iu16vec4 &a,const Iu16vec4 &b) { return _m_pavgw(a,b); }
140 inline Iu8vec8 simd_avg(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pavgb(a,b); }
141 inline int move_mask(const I8vec8 &a) { return _m_pmovmskb(a); }
142 inline Iu16vec4 mul_high(const Iu16vec4 &a,const Iu16vec4 &b) { return _m_pmulhuw(a,b); }
143 inline void mask_move(const I8vec8 &a,const I8vec8 &b,char *addr) { _m_maskmovq(a,b,addr); }
144 inline void store_nta(__m64 *p,M64 &a) { _mm_stream_pi(p,a); }
145 inline int F32vec4ToInt(const F32vec4 &a) { return _mm_cvtt_ss2si(a); }
146 inline Is32vec2 F32vec4ToIs32vec2 (const F32vec4 &a) {
147 __m64 result;
148 result = _mm_cvtt_ps2pi(a);
149 return Is32vec2(result);
150 }
151
152 inline F32vec4 IntToF32vec4(const F32vec4 &a,int i) {
153 __m128 result;
154 result = _mm_cvt_si2ss(a,i);
155 return F32vec4(result);
156 }
157
158 inline F32vec4 Is32vec2ToF32vec4(const F32vec4 &a,const Is32vec2 &b) {
159 __m128 result;
160 result = _mm_cvt_pi2ps(a,b);
161 return F32vec4(result);
162 }
163
164 class F32vec1 {
165 protected:
166 __m128 vec;
167 public:
168 F32vec1() {}
169 F32vec1(int i) { vec = _mm_cvt_si2ss(vec,i);};
170 EXPLICIT F32vec1(float f) { vec = _mm_set_ss(f); }
171 EXPLICIT F32vec1(double d) { vec = _mm_set_ss((float) d); }
172 F32vec1(__m128 m) { vec = m; }
173 operator __m128() const { return vec; }
174 friend F32vec1 operator &(const F32vec1 &a,const F32vec1 &b) { return _mm_and_ps(a,b); }
175 friend F32vec1 operator |(const F32vec1 &a,const F32vec1 &b) { return _mm_or_ps(a,b); }
176 friend F32vec1 operator ^(const F32vec1 &a,const F32vec1 &b) { return _mm_xor_ps(a,b); }
177 friend F32vec1 operator +(const F32vec1 &a,const F32vec1 &b) { return _mm_add_ss(a,b); }
178 friend F32vec1 operator -(const F32vec1 &a,const F32vec1 &b) { return _mm_sub_ss(a,b); }
179 friend F32vec1 operator *(const F32vec1 &a,const F32vec1 &b) { return _mm_mul_ss(a,b); }
180 friend F32vec1 operator /(const F32vec1 &a,const F32vec1 &b) { return _mm_div_ss(a,b); }
181 F32vec1& operator +=(F32vec1 &a) { return *this = _mm_add_ss(vec,a); }
182 F32vec1& operator -=(F32vec1 &a) { return *this = _mm_sub_ss(vec,a); }
183 F32vec1& operator *=(F32vec1 &a) { return *this = _mm_mul_ss(vec,a); }
184 F32vec1& operator /=(F32vec1 &a) { return *this = _mm_div_ss(vec,a); }
185 F32vec1& operator &=(F32vec1 &a) { return *this = _mm_and_ps(vec,a); }
186 F32vec1& operator |=(F32vec1 &a) { return *this = _mm_or_ps(vec,a); }
187 F32vec1& operator ^=(F32vec1 &a) { return *this = _mm_xor_ps(vec,a); }
188 friend F32vec1 sqrt(const F32vec1 &a) { return _mm_sqrt_ss(a); }
189 friend F32vec1 rcp(const F32vec1 &a) { return _mm_rcp_ss(a); }
190 friend F32vec1 rsqrt(const F32vec1 &a) { return _mm_rsqrt_ss(a); }
191 friend F32vec1 rcp_nr(const F32vec1 &a) {
192 F32vec1 Ra0 = _mm_rcp_ss(a);
193 return _mm_sub_ss(_mm_add_ss(Ra0,Ra0),_mm_mul_ss(_mm_mul_ss(Ra0,a),Ra0));
194 }
195 friend F32vec1 rsqrt_nr(const F32vec1 &a) {
196 static const F32vec1 fvecf0pt5(0.5f);
197 static const F32vec1 fvecf3pt0(3.0f);
198 F32vec1 Ra0 = _mm_rsqrt_ss(a);
199 return (fvecf0pt5 *Ra0) *(fvecf3pt0 - (a *Ra0) *Ra0);
200 }
201 #define Fvec32s1_COMP(op) friend F32vec1 cmp##op (const F32vec1 &a,const F32vec1 &b) { return _mm_cmp##op##_ss(a,b); }
202 Fvec32s1_COMP(eq)
203 Fvec32s1_COMP(lt)
204 Fvec32s1_COMP(le)
205 Fvec32s1_COMP(gt)
206 Fvec32s1_COMP(ge)
207 Fvec32s1_COMP(neq)
208 Fvec32s1_COMP(nlt)
209 Fvec32s1_COMP(nle)
210 Fvec32s1_COMP(ngt)
211 Fvec32s1_COMP(nge)
212 #undef Fvec32s1_COMP
213
214 friend F32vec1 simd_min(const F32vec1 &a,const F32vec1 &b) { return _mm_min_ss(a,b); }
215 friend F32vec1 simd_max(const F32vec1 &a,const F32vec1 &b) { return _mm_max_ss(a,b); }
216
217 #if defined(_ENABLE_VEC_DEBUG)
218 friend std::ostream & operator<<(std::ostream & os,const F32vec1 &a) {
219 float *fp = (float*)&a;
220 os << "float:" << *fp;
221 return os;
222 }
223 #endif
224 };
225
226 #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)))); }
227 Fvec32s1_SELECT(eq)
228 Fvec32s1_SELECT(lt)
229 Fvec32s1_SELECT(le)
230 Fvec32s1_SELECT(gt)
231 Fvec32s1_SELECT(ge)
232 Fvec32s1_SELECT(neq)
233 Fvec32s1_SELECT(nlt)
234 Fvec32s1_SELECT(nle)
235 Fvec32s1_SELECT(ngt)
236 Fvec32s1_SELECT(nge)
237 #undef Fvec32s1_SELECT
238
239 inline int F32vec1ToInt(const F32vec1 &a)
240 {
241 return _mm_cvtt_ss2si(a);
242 }
243
244 #pragma pack(pop)
245
246 #endif /* #ifdef __SSE__ */
247 #pragma pack(pop)
248 #endif
249 #endif