1/** 2 * This file has no copyright assigned and is placed in the Public Domain. 3 * This file is part of the mingw-w64 runtime package. 4 * No warranty is given; refer to the file DISCLAIMER.PD 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 <crtdefs.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 31class F32vec4 { 32protected: 33 __m128 vec; 34public: 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 115inline F32vec4 unpack_low(const F32vec4 &a,const F32vec4 &b) { return _mm_unpacklo_ps(a,b); } 116inline F32vec4 unpack_high(const F32vec4 &a,const F32vec4 &b) { return _mm_unpackhi_ps(a,b); } 117inline int move_mask(const F32vec4 &a) { return _mm_movemask_ps(a); } 118inline void loadu(F32vec4 &a,float *p) { a = _mm_loadu_ps(p); } 119inline void storeu(float *p,const F32vec4 &a) { _mm_storeu_ps(p,a); } 120inline 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)))); } 123Fvec32s4_SELECT(eq) 124Fvec32s4_SELECT(lt) 125Fvec32s4_SELECT(le) 126Fvec32s4_SELECT(gt) 127Fvec32s4_SELECT(ge) 128Fvec32s4_SELECT(neq) 129Fvec32s4_SELECT(nlt) 130Fvec32s4_SELECT(nle) 131Fvec32s4_SELECT(ngt) 132Fvec32s4_SELECT(nge) 133#undef Fvec32s4_SELECT 134 135#if 0 /* Commented until required types are defined */ 136inline Is16vec4 simd_max(const Is16vec4 &a,const Is16vec4 &b) { return _m_pmaxsw(a,b); } 137inline Is16vec4 simd_min(const Is16vec4 &a,const Is16vec4 &b) { return _m_pminsw(a,b); } 138inline Iu8vec8 simd_max(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pmaxub(a,b); } 139inline Iu8vec8 simd_min(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pminub(a,b); } 140inline Iu16vec4 simd_avg(const Iu16vec4 &a,const Iu16vec4 &b) { return _m_pavgw(a,b); } 141inline Iu8vec8 simd_avg(const Iu8vec8 &a,const Iu8vec8 &b) { return _m_pavgb(a,b); } 142inline int move_mask(const I8vec8 &a) { return _m_pmovmskb(a); } 143inline Iu16vec4 mul_high(const Iu16vec4 &a,const Iu16vec4 &b) { return _m_pmulhuw(a,b); } 144inline void mask_move(const I8vec8 &a,const I8vec8 &b,char *addr) { _m_maskmovq(a,b,addr); } 145inline void store_nta(__m64 *p,M64 &a) { _mm_stream_pi(p,a); } 146inline int F32vec4ToInt(const F32vec4 &a) { return _mm_cvtt_ss2si(a); } 147inline Is32vec2 F32vec4ToIs32vec2 (const F32vec4 &a) { 148 __m64 result; 149 result = _mm_cvtt_ps2pi(a); 150 return Is32vec2(result); 151} 152#endif 153 154inline F32vec4 IntToF32vec4(const F32vec4 &a,int i) { 155 __m128 result; 156 result = _mm_cvt_si2ss(a,i); 157 return F32vec4(result); 158} 159 160#if 0 /* Commented until required types are defined */ 161inline F32vec4 Is32vec2ToF32vec4(const F32vec4 &a,const Is32vec2 &b) { 162 __m128 result; 163 result = _mm_cvt_pi2ps(a,b); 164 return F32vec4(result); 165} 166#endif 167 168class F32vec1 { 169protected: 170 __m128 vec; 171public: 172 F32vec1() {} 173 F32vec1(int i) { vec = _mm_cvt_si2ss(vec,i);}; 174 EXPLICIT F32vec1(float f) { vec = _mm_set_ss(f); } 175 EXPLICIT F32vec1(double d) { vec = _mm_set_ss((float) d); } 176 F32vec1(__m128 m) { vec = m; } 177 operator __m128() const { return vec; } 178 friend F32vec1 operator &(const F32vec1 &a,const F32vec1 &b) { return _mm_and_ps(a,b); } 179 friend F32vec1 operator |(const F32vec1 &a,const F32vec1 &b) { return _mm_or_ps(a,b); } 180 friend F32vec1 operator ^(const F32vec1 &a,const F32vec1 &b) { return _mm_xor_ps(a,b); } 181 friend F32vec1 operator +(const F32vec1 &a,const F32vec1 &b) { return _mm_add_ss(a,b); } 182 friend F32vec1 operator -(const F32vec1 &a,const F32vec1 &b) { return _mm_sub_ss(a,b); } 183 friend F32vec1 operator *(const F32vec1 &a,const F32vec1 &b) { return _mm_mul_ss(a,b); } 184 friend F32vec1 operator /(const F32vec1 &a,const F32vec1 &b) { return _mm_div_ss(a,b); } 185 F32vec1& operator +=(F32vec1 &a) { return *this = _mm_add_ss(vec,a); } 186 F32vec1& operator -=(F32vec1 &a) { return *this = _mm_sub_ss(vec,a); } 187 F32vec1& operator *=(F32vec1 &a) { return *this = _mm_mul_ss(vec,a); } 188 F32vec1& operator /=(F32vec1 &a) { return *this = _mm_div_ss(vec,a); } 189 F32vec1& operator &=(F32vec1 &a) { return *this = _mm_and_ps(vec,a); } 190 F32vec1& operator |=(F32vec1 &a) { return *this = _mm_or_ps(vec,a); } 191 F32vec1& operator ^=(F32vec1 &a) { return *this = _mm_xor_ps(vec,a); } 192 friend F32vec1 sqrt(const F32vec1 &a) { return _mm_sqrt_ss(a); } 193 friend F32vec1 rcp(const F32vec1 &a) { return _mm_rcp_ss(a); } 194 friend F32vec1 rsqrt(const F32vec1 &a) { return _mm_rsqrt_ss(a); } 195 friend F32vec1 rcp_nr(const F32vec1 &a) { 196 F32vec1 Ra0 = _mm_rcp_ss(a); 197 return _mm_sub_ss(_mm_add_ss(Ra0,Ra0),_mm_mul_ss(_mm_mul_ss(Ra0,a),Ra0)); 198 } 199 friend F32vec1 rsqrt_nr(const F32vec1 &a) { 200 static const F32vec1 fvecf0pt5(0.5f); 201 static const F32vec1 fvecf3pt0(3.0f); 202 F32vec1 Ra0 = _mm_rsqrt_ss(a); 203 return (fvecf0pt5 *Ra0) *(fvecf3pt0 - (a *Ra0) *Ra0); 204 } 205#define Fvec32s1_COMP(op) friend F32vec1 cmp##op (const F32vec1 &a,const F32vec1 &b) { return _mm_cmp##op##_ss(a,b); } 206 Fvec32s1_COMP(eq) 207 Fvec32s1_COMP(lt) 208 Fvec32s1_COMP(le) 209 Fvec32s1_COMP(gt) 210 Fvec32s1_COMP(ge) 211 Fvec32s1_COMP(neq) 212 Fvec32s1_COMP(nlt) 213 Fvec32s1_COMP(nle) 214 Fvec32s1_COMP(ngt) 215 Fvec32s1_COMP(nge) 216#undef Fvec32s1_COMP 217 218 friend F32vec1 simd_min(const F32vec1 &a,const F32vec1 &b) { return _mm_min_ss(a,b); } 219 friend F32vec1 simd_max(const F32vec1 &a,const F32vec1 &b) { return _mm_max_ss(a,b); } 220 221#if defined(_ENABLE_VEC_DEBUG) 222 friend std::ostream & operator<<(std::ostream & os,const F32vec1 &a) { 223 float *fp = (float*)&a; 224 os << "float:" << *fp; 225 return os; 226 } 227#endif 228}; 229 230#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)))); } 231Fvec32s1_SELECT(eq) 232Fvec32s1_SELECT(lt) 233Fvec32s1_SELECT(le) 234Fvec32s1_SELECT(gt) 235Fvec32s1_SELECT(ge) 236Fvec32s1_SELECT(neq) 237Fvec32s1_SELECT(nlt) 238Fvec32s1_SELECT(nle) 239Fvec32s1_SELECT(ngt) 240Fvec32s1_SELECT(nge) 241#undef Fvec32s1_SELECT 242 243inline int F32vec1ToInt(const F32vec1 &a) 244{ 245 return _mm_cvtt_ss2si(a); 246} 247 248#pragma pack(pop) 249 250#endif /* #ifdef __SSE__ */ 251#pragma pack(pop) 252#endif 253#endif 254