Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1081 lines
41 KiB

  1. /**
  2. *** Copyright (C) 1985-1999 Intel Corporation. All rights reserved.
  3. ***
  4. *** The information and source code contained herein is the exclusive
  5. *** property of Intel Corporation and may not be disclosed, examined
  6. *** or reproduced in whole or in part without explicit written authorization
  7. *** from the company.
  8. ***
  9. **/
  10. /*
  11. * Definition of a C++ class interface to Willamette New Instruction intrinsics.
  12. *
  13. * File name : dvec.h class definitions
  14. *
  15. * Concept: A C++ abstraction of Willamette new intructions designed to improve
  16. * programmer productivity. Speed and accuracy are sacrificed for utility.
  17. * Facilitates an easy transition to compiler intrinsics
  18. * or assembly language.
  19. *
  20. */
  21. #ifndef DVEC_H_INCLUDED
  22. #define DVEC_H_INCLUDED
  23. #if !defined __cplusplus
  24. #error ERROR: This file is only supported in C++ compilations!
  25. #endif /* !__cplusplus */
  26. #include <emmintrin.h> /* Willamette New Instructions Intrinsics include file */
  27. #include <assert.h>
  28. #include <fvec.h>
  29. /* Define _ENABLE_VEC_DEBUG to enable std::ostream inserters for debug output */
  30. #if defined(_ENABLE_VEC_DEBUG)
  31. #include <iostream>
  32. #endif
  33. #pragma pack(push,16) /* Must ensure class & union 16-B aligned */
  34. /* If using MSVC5.0, explicit keyword should be used */
  35. #if (_MSC_VER >= 1100)
  36. #define EXPLICIT explicit
  37. #else
  38. #if (__ICL)
  39. #define EXPLICIT __explicit /* If MSVC4.x & ICL, use __explicit */
  40. #else
  41. #define EXPLICIT /* nothing */
  42. #pragma message( "explicit keyword not recognized")
  43. #endif
  44. #endif
  45. /* EMM Functionality Intrinsics */
  46. class I8vec16; /* 16 elements, each element a signed or unsigned char data type */
  47. class Is8vec16; /* 16 elements, each element a signed char data type */
  48. class Iu8vec16; /* 16 elements, each element an unsigned char data type */
  49. class I16vec8; /* 8 elements, each element a signed or unsigned short */
  50. class Is16vec8; /* 8 elements, each element a signed short */
  51. class Iu16vec8; /* 8 elements, each element an unsigned short */
  52. class I32vec4; /* 4 elements, each element a signed or unsigned long */
  53. class Is32vec4; /* 4 elements, each element a signed long */
  54. class Iu32vec4; /* 4 elements, each element a unsigned long */
  55. class I64vec2; /* 2 element, each a __m64 data type */
  56. class I128vec1; /* 1 element, a __m128i data type */
  57. #define _MM_16UB(element,vector) (*((unsigned char*)&##vector + ##element))
  58. #define _MM_16B(element,vector) (*((signed char*)&##vector + ##element))
  59. #define _MM_8UW(element,vector) (*((unsigned short*)&##vector + ##element))
  60. #define _MM_8W(element,vector) (*((short*)&##vector + ##element))
  61. #define _MM_4UDW(element,vector) (*((unsigned int*)&##vector + ##element))
  62. #define _MM_4DW(element,vector) (*((int*)&##vector + ##element))
  63. #define _MM_2QW(element,vector) (*((__int64*)&##vector + ##element))
  64. /* We need a m128i constant, keeping performance in mind*/
  65. inline const __m128i get_mask128()
  66. {
  67. static const __m128i mask128 = _mm_set1_epi64(M64(0xffffffffffffffffi64));
  68. return mask128;
  69. }
  70. /* M128 Class:
  71. * 1 element, a __m128i data type
  72. * Contructors & Logical Operations
  73. */
  74. class M128
  75. {
  76. protected:
  77. __m128i vec;
  78. public:
  79. M128() { }
  80. M128(__m128i mm) { vec = mm; }
  81. operator __m128i() const { return vec; }
  82. /* Logical Operations */
  83. M128& operator&=(const M128 &a) { return *this = (M128) _mm_and_si128(vec,a); }
  84. M128& operator|=(const M128 &a) { return *this = (M128) _mm_or_si128(vec,a); }
  85. M128& operator^=(const M128 &a) { return *this = (M128) _mm_xor_si128(vec,a); }
  86. };
  87. inline M128 operator&(const M128 &a, const M128 &b) { return _mm_and_si128(a,b); }
  88. inline M128 operator|(const M128 &a, const M128 &b) { return _mm_or_si128(a,b); }
  89. inline M128 operator^(const M128 &a, const M128 &b) { return _mm_xor_si128(a,b); }
  90. inline M128 andnot(const M128 &a, const M128 &b) { return _mm_andnot_si128(a,b); }
  91. /* I128vec1 Class:
  92. * 1 element, a __m128i data type
  93. * Contains Operations which can operate on any __m6128i data type
  94. */
  95. class I128vec1 : public M128
  96. {
  97. public:
  98. I128vec1() { }
  99. I128vec1(__m128i mm) : M128(mm) { }
  100. I128vec1& operator= (const M128 &a) { return *this = (I128vec1) a; }
  101. I128vec1& operator&=(const M128 &a) { return *this = (I128vec1) _mm_and_si128(vec,a); }
  102. I128vec1& operator|=(const M128 &a) { return *this = (I128vec1) _mm_or_si128(vec,a); }
  103. I128vec1& operator^=(const M128 &a) { return *this = (I128vec1) _mm_xor_si128(vec,a); }
  104. };
  105. /* I64vec2 Class:
  106. * 2 elements, each element signed or unsigned 64-bit integer
  107. */
  108. class I64vec2 : public M128
  109. {
  110. public:
  111. I64vec2() { }
  112. I64vec2(__m128i mm) : M128(mm) { }
  113. I64vec2(__m64 q1, __m64 q0)
  114. {
  115. _MM_2QW(0,vec) = *(__int64*)&q0;
  116. _MM_2QW(1,vec) = *(__int64*)&q1;
  117. }
  118. /* Assignment Operator */
  119. I64vec2& operator= (const M128 &a) { return *this = (I64vec2) a; }
  120. /* Logical Assignment Operators */
  121. I64vec2& operator&=(const M128 &a) { return *this = (I64vec2) _mm_and_si128(vec,a); }
  122. I64vec2& operator|=(const M128 &a) { return *this = (I64vec2) _mm_or_si128(vec,a); }
  123. I64vec2& operator^=(const M128 &a) { return *this = (I64vec2) _mm_xor_si128(vec,a); }
  124. /* Addition & Subtraction Assignment Operators */
  125. I64vec2& operator +=(const I64vec2 &a) { return *this = (I64vec2) _mm_add_epi64(vec,a); }
  126. I64vec2& operator -=(const I64vec2 &a) { return *this = (I64vec2) _mm_sub_epi64(vec,a); }
  127. /* Shift Logical Operators */
  128. I64vec2 operator<<(const I64vec2 &a) { return _mm_sll_epi64(vec,a); }
  129. I64vec2 operator<<(int count) { return _mm_slli_epi64(vec,count); }
  130. I64vec2& operator<<=(const I64vec2 &a) { return *this = (I64vec2) _mm_sll_epi64(vec,a); }
  131. I64vec2& operator<<=(int count) { return *this = (I64vec2) _mm_slli_epi64(vec,count); }
  132. I64vec2 operator>>(const I64vec2 &a) { return _mm_srl_epi64(vec,a); }
  133. I64vec2 operator>>(int count) { return _mm_srli_epi64(vec,count); }
  134. I64vec2& operator>>=(const I64vec2 &a) { return *this = (I64vec2) _mm_srl_epi64(vec,a); }
  135. I64vec2& operator>>=(int count) { return *this = (I64vec2) _mm_srli_epi64(vec,count); }
  136. /* Element Access for Debug, No data modified */
  137. const __int64& operator[](int i)const
  138. {
  139. assert(static_cast<unsigned int>(i) < 2); /* Only 2 elements to access */
  140. return _MM_2QW(i,vec);
  141. }
  142. /* Element Access and Assignment for Debug */
  143. __int64& operator[](int i)
  144. {
  145. assert(static_cast<unsigned int>(i) < 2); /* Only 2 elements to access */
  146. return _MM_2QW(i,vec);
  147. }
  148. };
  149. /* Unpacks */
  150. inline I64vec2 unpack_low(const I64vec2 &a, const I64vec2 &b) {return _mm_unpacklo_epi64(a,b); }
  151. inline I64vec2 unpack_high(const I64vec2 &a, const I64vec2 &b) {return _mm_unpackhi_epi64(a,b); }
  152. /* I32vec4 Class:
  153. * 4 elements, each element either a signed or unsigned int
  154. */
  155. class I32vec4 : public M128
  156. {
  157. public:
  158. I32vec4() { }
  159. I32vec4(__m128i mm) : M128(mm) { }
  160. /* Assignment Operator */
  161. I32vec4& operator= (const M128 &a) { return *this = (I32vec4) a; }
  162. /* Logicals Operators */
  163. I32vec4& operator&=(const M128 &a) { return *this = (I32vec4) _mm_and_si128(vec,a); }
  164. I32vec4& operator|=(const M128 &a) { return *this = (I32vec4) _mm_or_si128(vec,a); }
  165. I32vec4& operator^=(const M128 &a) { return *this = (I32vec4) _mm_xor_si128(vec,a); }
  166. /* Addition & Subtraction Assignment Operators */
  167. I32vec4& operator +=(const I32vec4 &a) { return *this = (I32vec4)_mm_add_epi32(vec,a); }
  168. I32vec4& operator -=(const I32vec4 &a) { return *this = (I32vec4)_mm_sub_epi32(vec,a); }
  169. /* Shift Logical Operators */
  170. I32vec4 operator<<(const I32vec4 &a) { return _mm_sll_epi32(vec,a); }
  171. I32vec4 operator<<(int count) { return _mm_slli_epi32(vec,count); }
  172. I32vec4& operator<<=(const I32vec4 &a) { return *this = (I32vec4)_mm_sll_epi32(vec,a); }
  173. I32vec4& operator<<=(int count) { return *this = (I32vec4)_mm_slli_epi32(vec,count); }
  174. };
  175. inline I32vec4 cmpeq(const I32vec4 &a, const I32vec4 &b) { return _mm_cmpeq_epi32(a,b); }
  176. inline I32vec4 cmpneq(const I32vec4 &a, const I32vec4 &b) { return _mm_andnot_si128(_mm_cmpeq_epi32(a,b), get_mask128()); }
  177. inline I32vec4 unpack_low(const I32vec4 &a, const I32vec4 &b) { return _mm_unpacklo_epi32(a,b); }
  178. inline I32vec4 unpack_high(const I32vec4 &a, const I32vec4 &b) { return _mm_unpackhi_epi32(a,b); }
  179. /* Is32vec4 Class:
  180. * 4 elements, each element signed integer
  181. */
  182. class Is32vec4 : public I32vec4
  183. {
  184. public:
  185. Is32vec4() { }
  186. Is32vec4(__m128i mm) : I32vec4(mm) { }
  187. Is32vec4(int i3, int i2, int i1, int i0)
  188. {
  189. _MM_4DW(0,vec) = i0;
  190. _MM_4DW(1,vec) = i1;
  191. _MM_4DW(2,vec) = i2;
  192. _MM_4DW(3,vec) = i3;
  193. }
  194. /* Assignment Operator */
  195. Is32vec4& operator= (const M128 &a) { return *this = (Is32vec4) a; }
  196. /* Logical Operators */
  197. Is32vec4& operator&=(const M128 &a) { return *this = (Is32vec4) _mm_and_si128(vec,a); }
  198. Is32vec4& operator|=(const M128 &a) { return *this = (Is32vec4) _mm_or_si128(vec,a); }
  199. Is32vec4& operator^=(const M128 &a) { return *this = (Is32vec4) _mm_xor_si128(vec,a); }
  200. /* Addition & Subtraction Assignment Operators */
  201. Is32vec4& operator +=(const I32vec4 &a) { return *this = (Is32vec4)_mm_add_epi32(vec,a); }
  202. Is32vec4& operator -=(const I32vec4 &a) { return *this = (Is32vec4)_mm_sub_epi32(vec,a); }
  203. /* Shift Logical Operators */
  204. Is32vec4 operator<<(const M128 &a) { return _mm_sll_epi32(vec,a); }
  205. Is32vec4 operator<<(int count) { return _mm_slli_epi32(vec,count); }
  206. Is32vec4& operator<<=(const M128 &a) { return *this = (Is32vec4)_mm_sll_epi32(vec,a); }
  207. Is32vec4& operator<<=(int count) { return *this = (Is32vec4)_mm_slli_epi32(vec,count); }
  208. /* Shift Arithmetic Operations */
  209. Is32vec4 operator>>(const M128 &a) { return _mm_sra_epi32(vec,a); }
  210. Is32vec4 operator>>(int count) { return _mm_srai_epi32(vec,count); }
  211. Is32vec4& operator>>=(const M128 &a) { return *this = (Is32vec4) _mm_sra_epi32(vec,a); }
  212. Is32vec4& operator>>=(int count) { return *this = (Is32vec4) _mm_srai_epi32(vec,count); }
  213. #if defined(_ENABLE_VEC_DEBUG)
  214. /* Output for Debug */
  215. friend std::ostream& operator<< (std::ostream &os, const Is32vec4 &a)
  216. {
  217. os << "[3]:" << _MM_4DW(3,a)
  218. << " [2]:" << _MM_4DW(2,a)
  219. << " [1]:" << _MM_4DW(1,a)
  220. << " [0]:" << _MM_4DW(0,a);
  221. return os;
  222. }
  223. #endif
  224. /* Element Access for Debug, No data modified */
  225. const int& operator[](int i)const
  226. {
  227. assert(static_cast<unsigned int>(i) < 4); /* Only 4 elements to access */
  228. return _MM_4DW(i,vec);
  229. }
  230. /* Element Access for Debug */
  231. int& operator[](int i)
  232. {
  233. assert(static_cast<unsigned int>(i) < 4); /* Only 4 elements to access */
  234. return _MM_4DW(i,vec);
  235. }
  236. };
  237. /* Compares */
  238. inline Is32vec4 cmpeq(const Is32vec4 &a, const Is32vec4 &b) { return _mm_cmpeq_epi32(a,b); }
  239. inline Is32vec4 cmpneq(const Is32vec4 &a, const Is32vec4 &b) { return _mm_andnot_si128(_mm_cmpeq_epi32(a,b), get_mask128()); }
  240. inline Is32vec4 cmpgt(const Is32vec4 &a, const Is32vec4 &b) { return _mm_cmpgt_epi32(a,b); }
  241. inline Is32vec4 cmplt(const Is32vec4 &a, const Is32vec4 &b) { return _mm_cmpgt_epi32(b,a); }
  242. /* Unpacks */
  243. inline Is32vec4 unpack_low(const Is32vec4 &a, const Is32vec4 &b) { return _mm_unpacklo_epi32(a,b); }
  244. inline Is32vec4 unpack_high(const Is32vec4 &a, const Is32vec4 &b) { return _mm_unpackhi_epi32(a,b); }
  245. /* Iu32vec4 Class:
  246. * 4 elements, each element unsigned int
  247. */
  248. class Iu32vec4 : public I32vec4
  249. {
  250. public:
  251. Iu32vec4() { }
  252. Iu32vec4(__m128i mm) : I32vec4(mm) { }
  253. Iu32vec4(unsigned int ui3, unsigned int ui2, unsigned int ui1, unsigned int ui0)
  254. {
  255. _MM_4UDW(0,vec) = ui0;
  256. _MM_4UDW(1,vec) = ui1;
  257. _MM_4UDW(2,vec) = ui2;
  258. _MM_4UDW(3,vec) = ui3;
  259. }
  260. /* Assignment Operator */
  261. Iu32vec4& operator= (const M128 &a) { return *this = (Iu32vec4) a; }
  262. /* Logical Assignment Operators */
  263. Iu32vec4& operator&=(const M128 &a) { return *this = (Iu32vec4) _mm_and_si128(vec,a); }
  264. Iu32vec4& operator|=(const M128 &a) { return *this = (Iu32vec4) _mm_or_si128(vec,a); }
  265. Iu32vec4& operator^=(const M128 &a) { return *this = (Iu32vec4) _mm_xor_si128(vec,a); }
  266. /* Addition & Subtraction Assignment Operators */
  267. Iu32vec4& operator +=(const I32vec4 &a) { return *this = (Iu32vec4)_mm_add_epi32(vec,a); }
  268. Iu32vec4& operator -=(const I32vec4 &a) { return *this = (Iu32vec4)_mm_sub_epi32(vec,a); }
  269. /* Shift Logical Operators */
  270. Iu32vec4 operator<<(const M128 &a) { return _mm_sll_epi32(vec,a); }
  271. Iu32vec4 operator<<(int count) { return _mm_slli_epi32(vec,count); }
  272. Iu32vec4& operator<<=(const M128 &a) { return *this = (Iu32vec4)_mm_sll_epi32(vec,a); }
  273. Iu32vec4& operator<<=(int count) { return *this = (Iu32vec4)_mm_slli_epi32(vec,count); }
  274. Iu32vec4 operator>>(const M128 &a) { return _mm_srl_epi32(vec,a); }
  275. Iu32vec4 operator>>(int count) { return _mm_srli_epi32(vec,count); }
  276. Iu32vec4& operator>>=(const M128 &a) { return *this = (Iu32vec4) _mm_srl_epi32(vec,a); }
  277. Iu32vec4& operator>>=(int count) { return *this = (Iu32vec4) _mm_srli_epi32(vec,count); }
  278. #if defined(_ENABLE_VEC_DEBUG)
  279. /* Output for Debug */
  280. friend std::ostream& operator<< (std::ostream &os, const Iu32vec4 &a)
  281. {
  282. os << "[3]:" << _MM_4UDW(3,a)
  283. << " [2]:" << _MM_4UDW(2,a)
  284. << " [1]:" << _MM_4UDW(1,a)
  285. << " [0]:" << _MM_4UDW(0,a);
  286. return os;
  287. }
  288. #endif
  289. /* Element Access for Debug, No data modified */
  290. const unsigned int& operator[](int i)const
  291. {
  292. assert(static_cast<unsigned int>(i) < 4); /* Only 4 elements to access */
  293. return _MM_4UDW(i,vec);
  294. }
  295. /* Element Access and Assignment for Debug */
  296. unsigned int& operator[](int i)
  297. {
  298. assert(static_cast<unsigned int>(i) < 4); /* Only 4 elements to access */
  299. return _MM_4UDW(i,vec);
  300. }
  301. };
  302. inline I64vec2 operator*(const Iu32vec4 &a, const Iu32vec4 &b) { return _mm_mul_epu32(a,b); }
  303. inline Iu32vec4 cmpeq(const Iu32vec4 &a, const Iu32vec4 &b) { return _mm_cmpeq_epi32(a,b); }
  304. inline Iu32vec4 cmpneq(const Iu32vec4 &a, const Iu32vec4 &b) { return _mm_andnot_si128(_mm_cmpeq_epi32(a,b), get_mask128()); }
  305. inline Iu32vec4 unpack_low(const Iu32vec4 &a, const Iu32vec4 &b) { return _mm_unpacklo_epi32(a,b); }
  306. inline Iu32vec4 unpack_high(const Iu32vec4 &a, const Iu32vec4 &b) { return _mm_unpackhi_epi32(a,b); }
  307. /* I16vec8 Class:
  308. * 8 elements, each element either unsigned or signed short
  309. */
  310. class I16vec8 : public M128
  311. {
  312. public:
  313. I16vec8() { }
  314. I16vec8(__m128i mm) : M128(mm) { }
  315. /* Assignment Operator */
  316. I16vec8& operator= (const M128 &a) { return *this = (I16vec8) a; }
  317. /* Logical Assignment Operators */
  318. I16vec8& operator&=(const M128 &a) { return *this = (I16vec8) _mm_and_si128(vec,a); }
  319. I16vec8& operator|=(const M128 &a) { return *this = (I16vec8) _mm_or_si128(vec,a); }
  320. I16vec8& operator^=(const M128 &a) { return *this = (I16vec8) _mm_xor_si128(vec,a); }
  321. /* Addition & Subtraction Assignment Operators */
  322. I16vec8& operator +=(const I16vec8 &a) { return *this = (I16vec8) _mm_add_epi16(vec,a); }
  323. I16vec8& operator -=(const I16vec8 &a) { return *this = (I16vec8) _mm_sub_epi16(vec,a); }
  324. I16vec8& operator *=(const I16vec8 &a) { return *this = (I16vec8) _mm_mullo_epi16(vec,a); }
  325. /* Shift Logical Operators */
  326. I16vec8 operator<<(const M128 &a) { return _mm_sll_epi16(vec,a); }
  327. I16vec8 operator<<(int count) { return _mm_slli_epi16(vec,count); }
  328. I16vec8& operator<<=(const M128 &a) { return *this = (I16vec8)_mm_sll_epi16(vec,a); }
  329. I16vec8& operator<<=(int count) { return *this = (I16vec8)_mm_slli_epi16(vec,count); }
  330. };
  331. inline I16vec8 operator*(const I16vec8 &a, const I16vec8 &b) { return _mm_mullo_epi16(a,b); }
  332. inline I16vec8 cmpeq(const I16vec8 &a, const I16vec8 &b) { return _mm_cmpeq_epi16(a,b); }
  333. inline I16vec8 cmpneq(const I16vec8 &a, const I16vec8 &b) { return _mm_andnot_si128(_mm_cmpeq_epi16(a,b), get_mask128()); }
  334. inline I16vec8 unpack_low(const I16vec8 &a, const I16vec8 &b) { return _mm_unpacklo_epi16(a,b); }
  335. inline I16vec8 unpack_high(const I16vec8 &a, const I16vec8 &b) { return _mm_unpackhi_epi16(a,b); }
  336. /* Is16vec8 Class:
  337. * 8 elements, each element signed short
  338. */
  339. class Is16vec8 : public I16vec8
  340. {
  341. public:
  342. Is16vec8() { }
  343. Is16vec8(__m128i mm) : I16vec8(mm) { }
  344. Is16vec8(signed short s7,signed short s6,signed short s5,signed short s4,signed short s3,signed short s2,signed short s1,signed short s0)
  345. {
  346. _MM_8W(0,vec) = s0;
  347. _MM_8W(1,vec) = s1;
  348. _MM_8W(2,vec) = s2;
  349. _MM_8W(3,vec) = s3;
  350. _MM_8W(4,vec) = s4;
  351. _MM_8W(5,vec) = s5;
  352. _MM_8W(6,vec) = s6;
  353. _MM_8W(7,vec) = s7;
  354. }
  355. /* Assignment Operator */
  356. Is16vec8& operator= (const M128 &a) { return *this = (Is16vec8) a; }
  357. /* Logical Assignment Operators */
  358. Is16vec8& operator&=(const M128 &a) { return *this = (Is16vec8) _mm_and_si128(vec,a); }
  359. Is16vec8& operator|=(const M128 &a) { return *this = (Is16vec8) _mm_or_si128(vec,a); }
  360. Is16vec8& operator^=(const M128 &a) { return *this = (Is16vec8) _mm_xor_si128(vec,a); }
  361. /* Addition & Subtraction Assignment Operators */
  362. Is16vec8& operator +=(const I16vec8 &a) { return *this = (Is16vec8) _mm_add_epi16(vec,a); }
  363. Is16vec8& operator -=(const I16vec8 &a) { return *this = (Is16vec8) _mm_sub_epi16(vec,a); }
  364. Is16vec8& operator *=(const I16vec8 &a) { return *this = (Is16vec8) _mm_mullo_epi16(vec,a); }
  365. /* Shift Logical Operators */
  366. Is16vec8 operator<<(const M128 &a) { return _mm_sll_epi16(vec,a); }
  367. Is16vec8 operator<<(int count) { return _mm_slli_epi16(vec,count); }
  368. Is16vec8& operator<<=(const M128 &a) { return *this = (Is16vec8)_mm_sll_epi16(vec,a); }
  369. Is16vec8& operator<<=(int count) { return *this = (Is16vec8)_mm_slli_epi16(vec,count); }
  370. /* Shift Arithmetic Operators */
  371. Is16vec8 operator>>(const M128 &a) { return _mm_sra_epi16(vec,a); }
  372. Is16vec8 operator>>(int count) { return _mm_srai_epi16(vec,count); }
  373. Is16vec8& operator>>=(const M128 &a) { return *this = (Is16vec8)_mm_sra_epi16(vec,a); }
  374. Is16vec8& operator>>=(int count) { return *this = (Is16vec8)_mm_srai_epi16(vec,count); }
  375. #if defined(_ENABLE_VEC_DEBUG)
  376. /* Output for Debug */
  377. friend std::ostream& operator<< (std::ostream &os, const Is16vec8 &a)
  378. {
  379. os << "[7]:" << _MM_8W(7,a)
  380. << " [6]:" << _MM_8W(6,a)
  381. << " [5]:" << _MM_8W(5,a)
  382. << " [4]:" << _MM_8W(4,a)
  383. << " [3]:" << _MM_8W(3,a)
  384. << " [2]:" << _MM_8W(2,a)
  385. << " [1]:" << _MM_8W(1,a)
  386. << " [0]:" << _MM_8W(0,a);
  387. return os;
  388. }
  389. #endif
  390. /* Element Access for Debug, No data modified */
  391. const signed short& operator[](int i)const
  392. {
  393. assert(static_cast<unsigned int>(i) < 8); /* Only 8 elements to access */
  394. return _MM_8W(i,vec);
  395. }
  396. /* Element Access and Assignment for Debug */
  397. signed short& operator[](int i)
  398. {
  399. assert(static_cast<unsigned int>(i) < 8); /* Only 8 elements to access */
  400. return _MM_8W(i,vec);
  401. }
  402. };
  403. inline Is16vec8 operator*(const Is16vec8 &a, const Is16vec8 &b) { return _mm_mullo_epi16(a,b); }
  404. /* Additional Is16vec8 functions: compares, unpacks, sat add/sub */
  405. inline Is16vec8 cmpeq(const Is16vec8 &a, const Is16vec8 &b) { return _mm_cmpeq_epi16(a,b); }
  406. inline Is16vec8 cmpneq(const Is16vec8 &a, const Is16vec8 &b) { return _mm_andnot_si128(_mm_cmpeq_epi16(a,b), get_mask128()); }
  407. inline Is16vec8 cmpgt(const Is16vec8 &a, const Is16vec8 &b) { return _mm_cmpgt_epi16(a,b); }
  408. inline Is16vec8 cmplt(const Is16vec8 &a, const Is16vec8 &b) { return _mm_cmpgt_epi16(b,a); }
  409. inline Is16vec8 unpack_low(const Is16vec8 &a, const Is16vec8 &b) { return _mm_unpacklo_epi16(a,b); }
  410. inline Is16vec8 unpack_high(const Is16vec8 &a, const Is16vec8 &b) { return _mm_unpackhi_epi16(a,b); }
  411. inline Is16vec8 mul_high(const Is16vec8 &a, const Is16vec8 &b) { return _mm_mulhi_epi16(a,b); }
  412. inline Is32vec4 mul_add(const Is16vec8 &a, const Is16vec8 &b) { return _mm_madd_epi16(a,b);}
  413. inline Is16vec8 sat_add(const Is16vec8 &a, const Is16vec8 &b) { return _mm_adds_epi16(a,b); }
  414. inline Is16vec8 sat_sub(const Is16vec8 &a, const Is16vec8 &b) { return _mm_subs_epi16(a,b); }
  415. inline Is16vec8 simd_max(const Is16vec8 &a, const Is16vec8 &b) { return _mm_max_epi16(a,b); }
  416. inline Is16vec8 simd_min(const Is16vec8 &a, const Is16vec8 &b) { return _mm_min_epi16(a,b); }
  417. /* Iu16vec8 Class:
  418. * 8 elements, each element unsigned short
  419. */
  420. class Iu16vec8 : public I16vec8
  421. {
  422. public:
  423. Iu16vec8() { }
  424. Iu16vec8(__m128i mm) : I16vec8(mm) { }
  425. Iu16vec8(unsigned short s7,unsigned short s6,unsigned short s5,unsigned short s4, unsigned short s3,unsigned short s2,unsigned short s1,unsigned short s0)
  426. {
  427. _MM_8UW(0,vec) = s0;
  428. _MM_8UW(1,vec) = s1;
  429. _MM_8UW(2,vec) = s2;
  430. _MM_8UW(3,vec) = s3;
  431. _MM_8UW(4,vec) = s4;
  432. _MM_8UW(5,vec) = s5;
  433. _MM_8UW(6,vec) = s6;
  434. _MM_8UW(7,vec) = s7;
  435. }
  436. /* Assignment Operator */
  437. Iu16vec8& operator= (const M128 &a) { return *this = (Iu16vec8) a; }
  438. /* Logical Assignment Operators */
  439. Iu16vec8& operator&=(const M128 &a) { return *this = (Iu16vec8) _mm_and_si128(vec,a); }
  440. Iu16vec8& operator|=(const M128 &a) { return *this = (Iu16vec8) _mm_or_si128(vec,a); }
  441. Iu16vec8& operator^=(const M128 &a) { return *this = (Iu16vec8) _mm_xor_si128(vec,a); }
  442. /* Addition & Subtraction Assignment Operators */
  443. Iu16vec8& operator +=(const I16vec8 &a) { return *this = (Iu16vec8) _mm_add_epi16(vec,a); }
  444. Iu16vec8& operator -=(const I16vec8 &a) { return *this = (Iu16vec8) _mm_sub_epi16(vec,a); }
  445. Iu16vec8& operator *=(const I16vec8 &a) { return *this = (Iu16vec8) _mm_mullo_epi16(vec,a); }
  446. /* Shift Logical Operators */
  447. Iu16vec8 operator<<(const M128 &a) { return _mm_sll_epi16(vec,a); }
  448. Iu16vec8 operator<<(int count) { return _mm_slli_epi16(vec,count); }
  449. Iu16vec8& operator<<=(const M128 &a) { return *this = (Iu16vec8)_mm_sll_epi16(vec,a); }
  450. Iu16vec8& operator<<=(int count) { return *this = (Iu16vec8)_mm_slli_epi16(vec,count); }
  451. Iu16vec8 operator>>(const M128 &a) { return _mm_srl_epi16(vec,a); }
  452. Iu16vec8 operator>>(int count) { return _mm_srli_epi16(vec,count); }
  453. Iu16vec8& operator>>=(const M128 &a) { return *this = (Iu16vec8) _mm_srl_epi16(vec,a); }
  454. Iu16vec8& operator>>=(int count) { return *this = (Iu16vec8) _mm_srli_epi16(vec,count); }
  455. #if defined(_ENABLE_VEC_DEBUG)
  456. /* Output for Debug */
  457. friend std::ostream& operator << (std::ostream &os, const Iu16vec8 &a)
  458. {
  459. os << "[7]:" << unsigned short(_MM_8UW(7,a))
  460. << " [6]:" << unsigned short(_MM_8UW(6,a))
  461. << " [5]:" << unsigned short(_MM_8UW(5,a))
  462. << " [4]:" << unsigned short(_MM_8UW(4,a))
  463. << " [3]:" << unsigned short(_MM_8UW(3,a))
  464. << " [2]:" << unsigned short(_MM_8UW(2,a))
  465. << " [1]:" << unsigned short(_MM_8UW(1,a))
  466. << " [0]:" << unsigned short(_MM_8UW(0,a));
  467. return os;
  468. }
  469. #endif
  470. /* Element Access for Debug, No data modified */
  471. const unsigned short& operator[](int i)const
  472. {
  473. assert(static_cast<unsigned int>(i) < 8); /* Only 8 elements to access */
  474. return _MM_8UW(i,vec);
  475. }
  476. /* Element Access for Debug */
  477. unsigned short& operator[](int i)
  478. {
  479. assert(static_cast<unsigned int>(i) < 8); /* Only 8 elements to access */
  480. return _MM_8UW(i,vec);
  481. }
  482. };
  483. inline Iu16vec8 operator*(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_mullo_epi16(a,b); }
  484. /* Additional Iu16vec8 functions: cmpeq,cmpneq, unpacks, sat add/sub */
  485. inline Iu16vec8 cmpeq(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_cmpeq_epi16(a,b); }
  486. inline Iu16vec8 cmpneq(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_andnot_si128(_mm_cmpeq_epi16(a,b), get_mask128()); }
  487. inline Iu16vec8 unpack_low(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_unpacklo_epi16(a,b); }
  488. inline Iu16vec8 unpack_high(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_unpackhi_epi16(a,b); }
  489. inline Iu16vec8 sat_add(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_adds_epu16(a,b); }
  490. inline Iu16vec8 sat_sub(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_subs_epu16(a,b); }
  491. inline Iu16vec8 simd_avg(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_avg_epu16(a,b); }
  492. inline I16vec8 mul_high(const Iu16vec8 &a, const Iu16vec8 &b) { return _mm_mulhi_epu16(a,b); }
  493. /* I8vec16 Class:
  494. * 16 elements, each element either unsigned or signed char
  495. */
  496. class I8vec16 : public M128
  497. {
  498. public:
  499. I8vec16() { }
  500. I8vec16(__m128i mm) : M128(mm) { }
  501. /* Assignment Operator */
  502. I8vec16& operator= (const M128 &a) { return *this = (I8vec16) a; }
  503. /* Logical Assignment Operators */
  504. I8vec16& operator&=(const M128 &a) { return *this = (I8vec16) _mm_and_si128(vec,a); }
  505. I8vec16& operator|=(const M128 &a) { return *this = (I8vec16) _mm_or_si128(vec,a); }
  506. I8vec16& operator^=(const M128 &a) { return *this = (I8vec16) _mm_xor_si128(vec,a); }
  507. /* Addition & Subtraction Assignment Operators */
  508. I8vec16& operator +=(const I8vec16 &a) { return *this = (I8vec16) _mm_add_epi8(vec,a); }
  509. I8vec16& operator -=(const I8vec16 &a) { return *this = (I8vec16) _mm_sub_epi8(vec,a); }
  510. };
  511. inline I8vec16 cmpeq(const I8vec16 &a, const I8vec16 &b) { return _mm_cmpeq_epi8(a,b); }
  512. inline I8vec16 cmpneq(const I8vec16 &a, const I8vec16 &b) { return _mm_andnot_si128(_mm_cmpeq_epi8(a,b), get_mask128()); }
  513. inline I8vec16 unpack_low(const I8vec16 &a, const I8vec16 &b) { return _mm_unpacklo_epi8(a,b); }
  514. inline I8vec16 unpack_high(const I8vec16 &a, const I8vec16 &b) { return _mm_unpackhi_epi8(a,b); }
  515. /* Is8vec16 Class:
  516. * 16 elements, each element a signed char
  517. */
  518. class Is8vec16 : public I8vec16
  519. {
  520. public:
  521. Is8vec16() { }
  522. Is8vec16(__m128i mm) : I8vec16(mm) { }
  523. /* Assignment Operator */
  524. Is8vec16& operator= (const M128 &a) { return *this = (Is8vec16) a; }
  525. /* Logical Assignment Operators */
  526. Is8vec16& operator&=(const M128 &a) { return *this = (Is8vec16) _mm_and_si128(vec,a); }
  527. Is8vec16& operator|=(const M128 &a) { return *this = (Is8vec16) _mm_or_si128(vec,a); }
  528. Is8vec16& operator^=(const M128 &a) { return *this = (Is8vec16) _mm_xor_si128(vec,a); }
  529. /* Addition & Subtraction Assignment Operators */
  530. Is8vec16& operator +=(const I8vec16 &a) { return *this = (Is8vec16) _mm_add_epi8(vec,a); }
  531. Is8vec16& operator -=(const I8vec16 &a) { return *this = (Is8vec16) _mm_sub_epi8(vec,a); }
  532. #if defined(_ENABLE_VEC_DEBUG)
  533. /* Output for Debug */
  534. friend std::ostream& operator << (std::ostream &os, const Is8vec16 &a)
  535. {
  536. os << "[15]:" << short(_MM_16B(15,a))
  537. << " [14]:" << short(_MM_16B(14,a))
  538. << " [13]:" << short(_MM_16B(13,a))
  539. << " [12]:" << short(_MM_16B(12,a))
  540. << " [11]:" << short(_MM_16B(11,a))
  541. << " [10]:" << short(_MM_16B(10,a))
  542. << " [9]:" << short(_MM_16B(9,a))
  543. << " [8]:" << short(_MM_16B(8,a))
  544. << " [7]:" << short(_MM_16B(7,a))
  545. << " [6]:" << short(_MM_16B(6,a))
  546. << " [5]:" << short(_MM_16B(5,a))
  547. << " [4]:" << short(_MM_16B(4,a))
  548. << " [3]:" << short(_MM_16B(3,a))
  549. << " [2]:" << short(_MM_16B(2,a))
  550. << " [1]:" << short(_MM_16B(1,a))
  551. << " [0]:" << short(_MM_16B(0,a));
  552. return os;
  553. }
  554. #endif
  555. /* Element Access for Debug, No data modified */
  556. const signed char& operator[](int i)const
  557. {
  558. assert(static_cast<unsigned int>(i) < 16); /* Only 16 elements to access */
  559. return _MM_16B(i,vec);
  560. }
  561. /* Element Access for Debug */
  562. signed char& operator[](int i)
  563. {
  564. assert(static_cast<unsigned int>(i) < 16); /* Only 16 elements to access */
  565. return _MM_16B(i,vec);
  566. }
  567. };
  568. inline Is8vec16 cmpeq(const Is8vec16 &a, const Is8vec16 &b) { return _mm_cmpeq_epi8(a,b); }
  569. inline Is8vec16 cmpneq(const Is8vec16 &a, const Is8vec16 &b) { return _mm_andnot_si128(_mm_cmpeq_epi8(a,b), get_mask128()); }
  570. inline Is8vec16 cmpgt(const Is8vec16 &a, const Is8vec16 &b) { return _mm_cmpgt_epi8(a,b); }
  571. inline Is8vec16 cmplt(const Is8vec16 &a, const Is8vec16 &b) { return _mm_cmplt_epi8(a,b); }
  572. inline Is8vec16 unpack_low(const Is8vec16 &a, const Is8vec16 &b) { return _mm_unpacklo_epi8(a,b); }
  573. inline Is8vec16 unpack_high(const Is8vec16 &a, const Is8vec16 &b) { return _mm_unpackhi_epi8(a,b); }
  574. inline Is8vec16 sat_add(const Is8vec16 &a, const Is8vec16 &b) { return _mm_adds_epi8(a,b); }
  575. inline Is8vec16 sat_sub(const Is8vec16 &a, const Is8vec16 &b) { return _mm_subs_epi8(a,b); }
  576. /* Iu8vec16 Class:
  577. * 16 elements, each element a unsigned char
  578. */
  579. class Iu8vec16 : public I8vec16
  580. {
  581. public:
  582. Iu8vec16() { }
  583. Iu8vec16(__m128i mm) : I8vec16(mm) { }
  584. /* Assignment Operator */
  585. Iu8vec16& operator= (const M128 &a) { return *this = (Iu8vec16) a; }
  586. /* Logical Assignment Operators */
  587. Iu8vec16& operator&=(const M128 &a) { return *this = (Iu8vec16) _mm_and_si128(vec,a); }
  588. Iu8vec16& operator|=(const M128 &a) { return *this = (Iu8vec16) _mm_or_si128(vec,a); }
  589. Iu8vec16& operator^=(const M128 &a) { return *this = (Iu8vec16) _mm_xor_si128(vec,a); }
  590. /* Addition & Subtraction Assignment Operators */
  591. Iu8vec16& operator +=(const I8vec16 &a) { return *this = (Iu8vec16) _mm_add_epi8(vec,a); }
  592. Iu8vec16& operator -=(const I8vec16 &a) { return *this = (Iu8vec16) _mm_sub_epi8(vec,a); }
  593. #if defined(_ENABLE_VEC_DEBUG)
  594. /* Output for Debug */
  595. friend std::ostream& operator << (std::ostream &os, const Iu8vec16 &a)
  596. {
  597. os << "[15]:" << unsigned short(_MM_16UB(15,a))
  598. << " [14]:" << unsigned short(_MM_16UB(14,a))
  599. << " [13]:" << unsigned short(_MM_16UB(13,a))
  600. << " [12]:" << unsigned short(_MM_16UB(12,a))
  601. << " [11]:" << unsigned short(_MM_16UB(11,a))
  602. << " [10]:" << unsigned short(_MM_16UB(10,a))
  603. << " [9]:" << unsigned short(_MM_16UB(9,a))
  604. << " [8]:" << unsigned short(_MM_16UB(8,a))
  605. << " [7]:" << unsigned short(_MM_16UB(7,a))
  606. << " [6]:" << unsigned short(_MM_16UB(6,a))
  607. << " [5]:" << unsigned short(_MM_16UB(5,a))
  608. << " [4]:" << unsigned short(_MM_16UB(4,a))
  609. << " [3]:" << unsigned short(_MM_16UB(3,a))
  610. << " [2]:" << unsigned short(_MM_16UB(2,a))
  611. << " [1]:" << unsigned short(_MM_16UB(1,a))
  612. << " [0]:" << unsigned short(_MM_16UB(0,a));
  613. return os;
  614. }
  615. #endif
  616. /* Element Access for Debug, No data modified */
  617. const unsigned char& operator[](int i)const
  618. {
  619. assert(static_cast<unsigned int>(i) < 16); /* Only 16 elements to access */
  620. return _MM_16UB(i,vec);
  621. }
  622. /* Element Access for Debug */
  623. unsigned char& operator[](int i)
  624. {
  625. assert(static_cast<unsigned int>(i) < 16); /* Only 16 elements to access */
  626. return _MM_16UB(i,vec);
  627. }
  628. };
  629. inline Iu8vec16 cmpeq(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_cmpeq_epi8(a,b); }
  630. inline Iu8vec16 cmpneq(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_andnot_si128(_mm_cmpeq_epi8(a,b), get_mask128()); }
  631. inline Iu8vec16 unpack_low(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_unpacklo_epi8(a,b); }
  632. inline Iu8vec16 unpack_high(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_unpackhi_epi8(a,b); }
  633. inline Iu8vec16 sat_add(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_adds_epu8(a,b); }
  634. inline Iu8vec16 sat_sub(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_subs_epu8(a,b); }
  635. inline I64vec2 sum_abs(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_sad_epu8(a,b); }
  636. inline Iu8vec16 simd_avg(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_avg_epu8(a,b); }
  637. inline Iu8vec16 simd_max(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_max_epu8(a,b); }
  638. inline Iu8vec16 simd_min(const Iu8vec16 &a, const Iu8vec16 &b) { return _mm_min_epu8(a,b); }
  639. /* Pack & Saturates */
  640. inline Is16vec8 pack_sat(const Is32vec4 &a, const Is32vec4 &b) { return _mm_packs_epi32(a,b); }
  641. inline Is8vec16 pack_sat(const Is16vec8 &a, const Is16vec8 &b) { return _mm_packs_epi16(a,b); }
  642. inline Iu8vec16 packu_sat(const Is16vec8 &a, const Is16vec8 &b) { return _mm_packus_epi16(a,b);}
  643. /********************************* Logicals ****************************************/
  644. #define IVEC128_LOGICALS(vect,element) \
  645. inline I##vect##vec##element operator& (const I##vect##vec##element &a, const I##vect##vec##element &b) \
  646. { return _mm_and_si128( a,b); } \
  647. inline I##vect##vec##element operator| (const I##vect##vec##element &a, const I##vect##vec##element &b) \
  648. { return _mm_or_si128( a,b); } \
  649. inline I##vect##vec##element operator^ (const I##vect##vec##element &a, const I##vect##vec##element &b) \
  650. { return _mm_xor_si128( a,b); } \
  651. inline I##vect##vec##element andnot (const I##vect##vec##element &a, const I##vect##vec##element &b) \
  652. { return _mm_andnot_si128( a,b); }
  653. IVEC128_LOGICALS(8,16)
  654. IVEC128_LOGICALS(u8,16)
  655. IVEC128_LOGICALS(s8,16)
  656. IVEC128_LOGICALS(16,8)
  657. IVEC128_LOGICALS(u16,8)
  658. IVEC128_LOGICALS(s16,8)
  659. IVEC128_LOGICALS(32,4)
  660. IVEC128_LOGICALS(u32,4)
  661. IVEC128_LOGICALS(s32,4)
  662. IVEC128_LOGICALS(64,2)
  663. IVEC128_LOGICALS(128,1)
  664. #undef IVEC128_LOGICALS
  665. /********************************* Add & Sub ****************************************/
  666. #define IVEC128_ADD_SUB(vect,element,opsize) \
  667. inline I##vect##vec##element operator+ (const I##vect##vec##element &a, const I##vect##vec##element &b) \
  668. { return _mm_add_##opsize( a,b); } \
  669. inline I##vect##vec##element operator- (const I##vect##vec##element &a, const I##vect##vec##element &b) \
  670. { return _mm_sub_##opsize( a,b); }
  671. IVEC128_ADD_SUB(8,16, epi8)
  672. IVEC128_ADD_SUB(u8,16, epi8)
  673. IVEC128_ADD_SUB(s8,16, epi8)
  674. IVEC128_ADD_SUB(16,8, epi16)
  675. IVEC128_ADD_SUB(u16,8, epi16)
  676. IVEC128_ADD_SUB(s16,8, epi16)
  677. IVEC128_ADD_SUB(32,4, epi32)
  678. IVEC128_ADD_SUB(u32,4, epi32)
  679. IVEC128_ADD_SUB(s32,4, epi32)
  680. IVEC128_ADD_SUB(64,2, epi64)
  681. #undef IVEC128_ADD_SUB
  682. /********************************* Conditional Select ****************************************/
  683. /* version of: retval = (a OP b)? c : d; *
  684. * Where OP is one of the possible comparision operators. *
  685. * Example: r = select_eq(a,b,c,d); *
  686. * if "member at position x of the vector a" == "member at position x of vector b" *
  687. * assign the corresponding member in r from c, else assign from d. *
  688. ********************************* Conditional Select ****************************************/
  689. #define IVEC128_SELECT(vect12,vect34,element,selop,arg1,arg2) \
  690. inline I##vect34##vec##element select_##selop (const I##vect12##vec##element &a, const I##vect12##vec##element &b, const I##vect34##vec##element &c, const I##vect34##vec##element &d) \
  691. { \
  692. I##vect12##vec##element mask = cmp##selop(a,b); \
  693. return( I##vect34##vec##element ((mask & arg1 ) | I##vect12##vec##element ((_mm_andnot_si128(mask, arg2 ))))); \
  694. }
  695. IVEC128_SELECT(8,s8,16,eq,c,d)
  696. IVEC128_SELECT(8,u8,16,eq,c,d)
  697. IVEC128_SELECT(8,8,16,eq,c,d)
  698. IVEC128_SELECT(8,s8,16,neq,c,d)
  699. IVEC128_SELECT(8,u8,16,neq,c,d)
  700. IVEC128_SELECT(8,8,16,neq,c,d)
  701. IVEC128_SELECT(16,s16,8,eq,c,d)
  702. IVEC128_SELECT(16,u16,8,eq,c,d)
  703. IVEC128_SELECT(16,16,8,eq,c,d)
  704. IVEC128_SELECT(16,s16,8,neq,c,d)
  705. IVEC128_SELECT(16,u16,8,neq,c,d)
  706. IVEC128_SELECT(16,16,8,neq,c,d)
  707. IVEC128_SELECT(32,s32,4,eq,c,d)
  708. IVEC128_SELECT(32,u32,4,eq,c,d)
  709. IVEC128_SELECT(32,32,4,eq,c,d)
  710. IVEC128_SELECT(32,s32,4,neq,c,d)
  711. IVEC128_SELECT(32,u32,4,neq,c,d)
  712. IVEC128_SELECT(32,32,4,neq,c,d)
  713. IVEC128_SELECT(s8,s8,16,gt,c,d)
  714. IVEC128_SELECT(s8,u8,16,gt,c,d)
  715. IVEC128_SELECT(s8,8,16,gt,c,d)
  716. IVEC128_SELECT(s8,s8,16,lt,c,d)
  717. IVEC128_SELECT(s8,u8,16,lt,c,d)
  718. IVEC128_SELECT(s8,8,16,lt,c,d)
  719. IVEC128_SELECT(s16,s16,8,gt,c,d)
  720. IVEC128_SELECT(s16,u16,8,gt,c,d)
  721. IVEC128_SELECT(s16,16,8,gt,c,d)
  722. IVEC128_SELECT(s16,s16,8,lt,c,d)
  723. IVEC128_SELECT(s16,u16,8,lt,c,d)
  724. IVEC128_SELECT(s16,16,8,lt,c,d)
  725. #undef IVEC128_SELECT
  726. class F64vec2
  727. {
  728. protected:
  729. __m128d vec;
  730. public:
  731. /* Constructors: __m128d, 2 doubles */
  732. F64vec2() {}
  733. /* initialize 2 DP FP with __m128d data type */
  734. F64vec2(__m128d m) { vec = m;}
  735. /* initialize 2 DP FPs with 2 doubles */
  736. F64vec2(double d1, double d0) { vec= _mm_set_pd(d1,d0); }
  737. /* Explicitly initialize each of 2 DP FPs with same double */
  738. EXPLICIT F64vec2(double d) { vec = _mm_set1_pd(d); }
  739. /* Conversion functions */
  740. operator __m128d() const { return vec; } /* Convert to __m128d */
  741. /* Logical Operators */
  742. friend F64vec2 operator &(const F64vec2 &a, const F64vec2 &b) { return _mm_and_pd(a,b); }
  743. friend F64vec2 operator |(const F64vec2 &a, const F64vec2 &b) { return _mm_or_pd(a,b); }
  744. friend F64vec2 operator ^(const F64vec2 &a, const F64vec2 &b) { return _mm_xor_pd(a,b); }
  745. /* Arithmetic Operators */
  746. friend F64vec2 operator +(const F64vec2 &a, const F64vec2 &b) { return _mm_add_pd(a,b); }
  747. friend F64vec2 operator -(const F64vec2 &a, const F64vec2 &b) { return _mm_sub_pd(a,b); }
  748. friend F64vec2 operator *(const F64vec2 &a, const F64vec2 &b) { return _mm_mul_pd(a,b); }
  749. friend F64vec2 operator /(const F64vec2 &a, const F64vec2 &b) { return _mm_div_pd(a,b); }
  750. F64vec2& operator +=(F64vec2 &a) { return *this = _mm_add_pd(vec,a); }
  751. F64vec2& operator -=(F64vec2 &a) { return *this = _mm_sub_pd(vec,a); }
  752. F64vec2& operator *=(F64vec2 &a) { return *this = _mm_mul_pd(vec,a); }
  753. F64vec2& operator /=(F64vec2 &a) { return *this = _mm_div_pd(vec,a); }
  754. F64vec2& operator &=(F64vec2 &a) { return *this = _mm_and_pd(vec,a); }
  755. F64vec2& operator |=(F64vec2 &a) { return *this = _mm_or_pd(vec,a); }
  756. F64vec2& operator ^=(F64vec2 &a) { return *this = _mm_xor_pd(vec,a); }
  757. /* Horizontal Add */
  758. friend double add_horizontal(F64vec2 &a)
  759. {
  760. F64vec2 ftemp = _mm_add_sd(a,_mm_shuffle_pd(a, a, 1));
  761. return ftemp[0];
  762. }
  763. /* And Not */
  764. friend F64vec2 andnot(const F64vec2 &a, const F64vec2 &b) { return _mm_andnot_pd(a,b); }
  765. /* Square Root */
  766. friend F64vec2 sqrt(const F64vec2 &a) { return _mm_sqrt_pd(a); }
  767. /* Compares: Mask is returned */
  768. /* Macros expand to all compare intrinsics. Example:
  769. friend F64vec2 cmpeq(const F64vec2 &a, const F64vec2 &b)
  770. { return _mm_cmpeq_ps(a,b);} */
  771. #define F64vec2_COMP(op) \
  772. friend F64vec2 cmp##op (const F64vec2 &a, const F64vec2 &b) { return _mm_cmp##op##_pd(a,b); }
  773. F64vec2_COMP(eq) // expanded to cmpeq(a,b)
  774. F64vec2_COMP(lt) // expanded to cmplt(a,b)
  775. F64vec2_COMP(le) // expanded to cmple(a,b)
  776. F64vec2_COMP(gt) // expanded to cmpgt(a,b)
  777. F64vec2_COMP(ge) // expanded to cmpge(a,b)
  778. F64vec2_COMP(ngt) // expanded to cmpngt(a,b)
  779. F64vec2_COMP(nge) // expanded to cmpnge(a,b)
  780. F64vec2_COMP(neq) // expanded to cmpneq(a,b)
  781. F64vec2_COMP(nlt) // expanded to cmpnlt(a,b)
  782. F64vec2_COMP(nle) // expanded to cmpnle(a,b)
  783. #undef F64vec2_COMP
  784. /* Min and Max */
  785. friend F64vec2 simd_min(const F64vec2 &a, const F64vec2 &b) { return _mm_min_pd(a,b); }
  786. friend F64vec2 simd_max(const F64vec2 &a, const F64vec2 &b) { return _mm_max_pd(a,b); }
  787. /* Compare lower DP FP values */
  788. #define F64vec2_COMI(op) \
  789. friend int comi##op (const F64vec2 &a, const F64vec2 &b) { return _mm_comi##op##_sd(a,b); }
  790. F64vec2_COMI(eq) // expanded to comieq(a,b)
  791. F64vec2_COMI(lt) // expanded to comilt(a,b)
  792. F64vec2_COMI(le) // expanded to comile(a,b)
  793. F64vec2_COMI(gt) // expanded to comigt(a,b)
  794. F64vec2_COMI(ge) // expanded to comige(a,b)
  795. F64vec2_COMI(neq) // expanded to comineq(a,b)
  796. #undef F64vec2_COMI
  797. /* Compare lower DP FP values */
  798. #define F64vec2_UCOMI(op) \
  799. friend int ucomi##op (const F64vec2 &a, const F64vec2 &b) { return _mm_ucomi##op##_sd(a,b); }
  800. F64vec2_UCOMI(eq) // expanded to ucomieq(a,b)
  801. F64vec2_UCOMI(lt) // expanded to ucomilt(a,b)
  802. F64vec2_UCOMI(le) // expanded to ucomile(a,b)
  803. F64vec2_UCOMI(gt) // expanded to ucomigt(a,b)
  804. F64vec2_UCOMI(ge) // expanded to ucomige(a,b)
  805. F64vec2_UCOMI(neq) // expanded to ucomineq(a,b)
  806. #undef F64vec2_UCOMI
  807. /* Debug Features */
  808. #if defined(_ENABLE_VEC_DEBUG)
  809. /* Output */
  810. friend std::ostream & operator<<(std::ostream & os, const F64vec2 &a)
  811. {
  812. /* To use: cout << "Elements of F64vec2 fvec are: " << fvec; */
  813. double *dp = (double*)&a;
  814. os << " [1]:" << *(dp+1)
  815. << " [0]:" << *dp;
  816. return os;
  817. }
  818. #endif
  819. /* Element Access Only, no modifications to elements*/
  820. const double& operator[](int i) const
  821. {
  822. /* Assert enabled only during debug /DDEBUG */
  823. assert((0 <= i) && (i <= 1)); /* User should only access elements 0-1 */
  824. double *dp = (double*)&vec;
  825. return *(dp+i);
  826. }
  827. /* Element Access and Modification*/
  828. double& operator[](int i)
  829. {
  830. /* Assert enabled only during debug /DDEBUG */
  831. assert((0 <= i) && (i <= 1)); /* User should only access elements 0-1 */
  832. double *dp = (double*)&vec;
  833. return *(dp+i);
  834. }
  835. };
  836. /* Miscellaneous */
  837. /* Interleave low order data elements of a and b into destination */
  838. inline F64vec2 unpack_low(const F64vec2 &a, const F64vec2 &b)
  839. { return _mm_unpacklo_pd(a, b); }
  840. /* Interleave high order data elements of a and b into target */
  841. inline F64vec2 unpack_high(const F64vec2 &a, const F64vec2 &b)
  842. { return _mm_unpackhi_pd(a, b); }
  843. /* Move Mask to Integer returns 4 bit mask formed of most significant bits of a */
  844. inline int move_mask(const F64vec2 &a)
  845. { return _mm_movemask_pd(a);}
  846. /* Data Motion Functions */
  847. /* Load Unaligned loadu_pd: Unaligned */
  848. inline void loadu(F64vec2 &a, double *p)
  849. { a = _mm_loadu_pd(p); }
  850. /* Store Temporal storeu_pd: Unaligned */
  851. inline void storeu(double *p, const F64vec2 &a)
  852. { _mm_storeu_pd(p, a); }
  853. /* Cacheability Support */
  854. /* Non-Temporal Store */
  855. inline void store_nta(double *p, F64vec2 &a)
  856. { _mm_stream_pd(p,a);}
  857. #define F64vec2_SELECT(op) \
  858. inline F64vec2 select_##op (const F64vec2 &a, const F64vec2 &b, const F64vec2 &c, const F64vec2 &d) \
  859. { \
  860. F64vec2 mask = _mm_cmp##op##_pd(a,b); \
  861. return( (mask & c) | F64vec2((_mm_andnot_pd(mask,d)))); \
  862. }
  863. F64vec2_SELECT(eq) // generates select_eq(a,b)
  864. F64vec2_SELECT(lt) // generates select_lt(a,b)
  865. F64vec2_SELECT(le) // generates select_le(a,b)
  866. F64vec2_SELECT(gt) // generates select_gt(a,b)
  867. F64vec2_SELECT(ge) // generates select_ge(a,b)
  868. F64vec2_SELECT(neq) // generates select_neq(a,b)
  869. F64vec2_SELECT(nlt) // generates select_nlt(a,b)
  870. F64vec2_SELECT(nle) // generates select_nle(a,b)
  871. #undef F64vec2_SELECT
  872. /* Convert the lower DP FP value of a to a 32 bit signed integer using Truncate*/
  873. inline int F64vec2ToInt(const F64vec2 &a)
  874. {
  875. return _mm_cvttsd_si32(a);
  876. }
  877. /* Convert the 4 SP FP values of a to DP FP values */
  878. inline F64vec2 F32vec4ToF64vec2(const F32vec4 &a)
  879. {
  880. return _mm_cvtps_pd(a);
  881. }
  882. /* Convert the 2 DP FP values of a to SP FP values */
  883. inline F32vec4 F64vec2ToF32vec4(const F64vec2 &a)
  884. {
  885. return _mm_cvtpd_ps(a);
  886. }
  887. /* Convert the signed int in b to a DP FP value. Upper DP FP value in a passed through */
  888. inline F64vec2 IntToF64vec2(const F64vec2 &a, int b)
  889. {
  890. return _mm_cvtsi32_sd(a,b);
  891. }
  892. #pragma pack(pop) /* 16-B aligned */
  893. #endif // DVEC_H_INCLUDED