fvec.h 9.7 KB

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