vec.hpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. #ifndef _SCTL_VEC_WRAPPER_HPP_
  2. #define _SCTL_VEC_WRAPPER_HPP_
  3. #include SCTL_INCLUDE(math_utils.hpp)
  4. #include SCTL_INCLUDE(common.hpp)
  5. #include <cassert>
  6. #include <cstdint>
  7. #include <ostream>
  8. #ifdef __SSE__
  9. #include <xmmintrin.h>
  10. #endif
  11. #ifdef __SSE2__
  12. #include <emmintrin.h>
  13. #endif
  14. #ifdef __SSE3__
  15. #include <pmmintrin.h>
  16. #endif
  17. #ifdef __SSE4_2__
  18. #include <smmintrin.h>
  19. #endif
  20. #ifdef __AVX__
  21. #include <immintrin.h>
  22. #endif
  23. #if defined(__MIC__)
  24. #include <immintrin.h>
  25. #endif
  26. // TODO: Implement AVX versions of floats, int32_t, int64_t
  27. // TODO: Check alignment when SCTL_MEMDEBUG is defined
  28. // TODO: Replace pointers with iterators
  29. namespace SCTL_NAMESPACE {
  30. enum class DataType {
  31. Integer,
  32. Real,
  33. Bool
  34. };
  35. template <class ValueType> class TypeTraits {
  36. public:
  37. static constexpr DataType Type = DataType::Bool;
  38. static constexpr Integer Size = sizeof(ValueType);
  39. static constexpr Integer SigBits = 1;
  40. };
  41. template <> class TypeTraits<int32_t> {
  42. public:
  43. static constexpr DataType Type = DataType::Integer;
  44. static constexpr Integer Size = sizeof(int32_t);
  45. static constexpr Integer SigBits = Size * 8;
  46. };
  47. template <> class TypeTraits<int64_t> {
  48. public:
  49. static constexpr DataType Type = DataType::Integer;
  50. static constexpr Integer Size = sizeof(int64_t);
  51. static constexpr Integer SigBits = Size * 8;
  52. };
  53. template <> class TypeTraits<float> {
  54. public:
  55. static constexpr DataType Type = DataType::Real;
  56. static constexpr Integer Size = sizeof(float);
  57. static constexpr Integer SigBits = 23;
  58. };
  59. template <> class TypeTraits<double> {
  60. public:
  61. static constexpr DataType Type = DataType::Real;
  62. static constexpr Integer Size = sizeof(double);
  63. static constexpr Integer SigBits = 52;
  64. };
  65. template <DataType type, Integer size> class GetType {
  66. public:
  67. typedef bool ValueType;
  68. };
  69. template <> class GetType<DataType::Integer,4> {
  70. public:
  71. typedef int32_t ValueType;
  72. };
  73. template <> class GetType<DataType::Integer,8> {
  74. public:
  75. typedef int64_t ValueType;
  76. };
  77. template <> class GetType<DataType::Real,4> {
  78. public:
  79. typedef float ValueType;
  80. };
  81. template <> class GetType<DataType::Real,8> {
  82. public:
  83. typedef double ValueType;
  84. };
  85. template <class ValueType, Integer N> class alignas(sizeof(ValueType) * N) Vec {
  86. public:
  87. typedef typename GetType<DataType::Integer,TypeTraits<ValueType>::Size>::ValueType IntegerType;
  88. typedef typename GetType<DataType::Real,TypeTraits<ValueType>::Size>::ValueType RealType;
  89. typedef Vec<IntegerType,N> IntegerVec;
  90. typedef Vec<RealType,N> RealVec;
  91. typedef ValueType ScalarType;
  92. static constexpr Integer Size() {
  93. return N;
  94. }
  95. static Vec Zero() {
  96. Vec r;
  97. for (Integer i = 0; i < N; i++) r.v[i] = 0;
  98. return r;
  99. }
  100. static Vec Load1(ValueType const* p) {
  101. Vec r;
  102. for (Integer i = 0; i < N; i++) r.v[i] = p[0];
  103. return r;
  104. }
  105. static Vec Load(ValueType const* p) {
  106. Vec r;
  107. for (Integer i = 0; i < N; i++) r.v[i] = p[i];
  108. return r;
  109. }
  110. static Vec LoadAligned(ValueType const* p) {
  111. Vec r;
  112. for (Integer i = 0; i < N; i++) r.v[i] = p[i];
  113. return r;
  114. }
  115. Vec() = default;
  116. Vec(const ValueType& a) {
  117. for (Integer i = 0; i < N; i++) v[i] = a;
  118. }
  119. void Store(ValueType* p) const {
  120. for (Integer i = 0; i < N; i++) p[i] = v[i];
  121. }
  122. void StoreAligned(ValueType* p) const {
  123. for (Integer i = 0; i < N; i++) p[i] = v[i];
  124. }
  125. // Bitwise NOT
  126. Vec operator~() const {
  127. Vec r;
  128. char* vo = (char*)r.v;
  129. const char* vi = (const char*)this->v;
  130. for (Integer i = 0; i < (Integer)(N*sizeof(ValueType)); i++) vo[i] = ~vi[i];
  131. return r;
  132. }
  133. // Unary plus and minus
  134. Vec operator+() const {
  135. return *this;
  136. }
  137. Vec operator-() const {
  138. Vec r;
  139. for (Integer i = 0; i < N; i++) r.v[i] = -v[i];
  140. return r;
  141. }
  142. // C-style cast
  143. //template <class RetValueType> explicit operator Vec<RetValueType,N>() const {
  144. // Vec<RetValueType,N> r;
  145. // for (Integer i = 0; i < N; i++) r.v[i] = (RetValueType)v[i];
  146. // return r;
  147. //}
  148. // Arithmetic operators
  149. friend Vec operator*(Vec lhs, const Vec& rhs) {
  150. for (Integer i = 0; i < N; i++) lhs.v[i] *= rhs.v[i];
  151. return lhs;
  152. }
  153. friend Vec operator+(Vec lhs, const Vec& rhs) {
  154. for (Integer i = 0; i < N; i++) lhs.v[i] += rhs.v[i];
  155. return lhs;
  156. }
  157. friend Vec operator-(Vec lhs, const Vec& rhs) {
  158. for (Integer i = 0; i < N; i++) lhs.v[i] -= rhs.v[i];
  159. return lhs;
  160. }
  161. friend Vec FMA(Vec a, const Vec& b, const Vec& c) {
  162. for (Integer i = 0; i < N; i++) a.v[i] = a.v[i] * b.v[i] + c.v[i];
  163. return a;
  164. }
  165. // Comparison operators
  166. friend Vec operator< (Vec lhs, const Vec& rhs) {
  167. static const ValueType value_zero = const_zero();
  168. static const ValueType value_one = const_one();
  169. for (Integer i = 0; i < N; i++) lhs.v[i] = (lhs.v[i] < rhs.v[i] ? value_one : value_zero);
  170. return lhs;
  171. }
  172. friend Vec operator<=(Vec lhs, const Vec& rhs) {
  173. static const ValueType value_zero = const_zero();
  174. static const ValueType value_one = const_one();
  175. for (Integer i = 0; i < N; i++) lhs.v[i] = (lhs.v[i] <= rhs.v[i] ? value_one : value_zero);
  176. return lhs;
  177. }
  178. friend Vec operator>=(Vec lhs, const Vec& rhs) {
  179. static const ValueType value_zero = const_zero();
  180. static const ValueType value_one = const_one();
  181. for (Integer i = 0; i < N; i++) lhs.v[i] = (lhs.v[i] >= rhs.v[i] ? value_one : value_zero);
  182. return lhs;
  183. }
  184. friend Vec operator> (Vec lhs, const Vec& rhs) {
  185. static const ValueType value_zero = const_zero();
  186. static const ValueType value_one = const_one();
  187. for (Integer i = 0; i < N; i++) lhs.v[i] = (lhs.v[i] > rhs.v[i] ? value_one : value_zero);
  188. return lhs;
  189. }
  190. friend Vec operator==(Vec lhs, const Vec& rhs) {
  191. static const ValueType value_zero = const_zero();
  192. static const ValueType value_one = const_one();
  193. for (Integer i = 0; i < N; i++) lhs.v[i] = (lhs.v[i] == rhs.v[i] ? value_one : value_zero);
  194. return lhs;
  195. }
  196. friend Vec operator!=(Vec lhs, const Vec& rhs) {
  197. static const ValueType value_zero = const_zero();
  198. static const ValueType value_one = const_one();
  199. for (Integer i = 0; i < N; i++) lhs.v[i] = (lhs.v[i] != rhs.v[i] ? value_one : value_zero);
  200. return lhs;
  201. }
  202. // Bitwise operators
  203. friend Vec operator&(Vec lhs, const Vec& rhs) {
  204. char* vo = (char*)lhs.v;
  205. const char* vi = (const char*)rhs.v;
  206. for (Integer i = 0; i < (Integer)sizeof(ValueType)*N; i++) vo[i] &= vi[i];
  207. return lhs;
  208. }
  209. friend Vec operator^(Vec lhs, const Vec& rhs) {
  210. char* vo = (char*)lhs.v;
  211. const char* vi = (const char*)rhs.v;
  212. for (Integer i = 0; i < (Integer)sizeof(ValueType)*N; i++) vo[i] ^= vi[i];
  213. return lhs;
  214. }
  215. friend Vec operator|(Vec lhs, const Vec& rhs) {
  216. char* vo = (char*)lhs.v;
  217. const char* vi = (const char*)rhs.v;
  218. for (Integer i = 0; i < (Integer)sizeof(ValueType)*N; i++) vo[i] |= vi[i];
  219. return lhs;
  220. }
  221. friend Vec AndNot(Vec lhs, const Vec& rhs) {
  222. return lhs & (~rhs);
  223. }
  224. // Bitshift
  225. friend IntegerVec operator<<(const Vec& lhs, const Integer& rhs) {
  226. IntegerVec r = IntegerVec::LoadAligned(&lhs.v[0]);
  227. for (Integer i = 0; i < N; i++) r.v[i] = r.v[i] << rhs;
  228. return r;
  229. }
  230. // Assignment operators
  231. Vec& operator+=(const Vec& rhs) {
  232. for (Integer i = 0; i < N; i++) v[i] += rhs.v[i];
  233. return *this;
  234. }
  235. Vec& operator-=(const Vec& rhs) {
  236. for (Integer i = 0; i < N; i++) v[i] -= rhs.v[i];
  237. return *this;
  238. }
  239. Vec& operator*=(const Vec& rhs) {
  240. for (Integer i = 0; i < N; i++) v[i] *= rhs.v[i];
  241. return *this;
  242. }
  243. Vec& operator&=(const Vec& rhs) {
  244. char* vo = (char*)this->v;
  245. const char* vi = (const char*)rhs.v;
  246. for (Integer i = 0; i < (Integer)sizeof(ValueType)*N; i++) vo[i] &= vi[i];
  247. return *this;
  248. }
  249. Vec& operator^=(const Vec& rhs) {
  250. char* vo = (char*)this->v;
  251. const char* vi = (const char*)rhs.v;
  252. for (Integer i = 0; i < (Integer)sizeof(ValueType)*N; i++) vo[i] ^= vi[i];
  253. return *this;
  254. }
  255. Vec& operator|=(const Vec& rhs) {
  256. char* vo = (char*)this->v;
  257. const char* vi = (const char*)rhs.v;
  258. for (Integer i = 0; i < (Integer)sizeof(ValueType)*N; i++) vo[i] |= vi[i];
  259. return *this;
  260. }
  261. // Conversion operators
  262. // /
  263. // Other operators
  264. friend Vec max(Vec lhs, const Vec& rhs) {
  265. for (Integer i = 0; i < N; i++) {
  266. if (lhs.v[i] < rhs.v[i]) lhs.v[i] = rhs.v[i];
  267. }
  268. return lhs;
  269. }
  270. friend Vec min(Vec lhs, const Vec& rhs) {
  271. for (Integer i = 0; i < N; i++) {
  272. if (lhs.v[i] > rhs.v[i]) lhs.v[i] = rhs.v[i];
  273. }
  274. return lhs;
  275. }
  276. friend std::ostream& operator<<(std::ostream& os, const Vec& in) {
  277. //for (Integer i = 0; i < (Integer)sizeof(ValueType)*8; i++) os << ((*(uint64_t*)in.v) & (1UL << i) ? '1' : '0');
  278. //os << '\n';
  279. for (Integer i = 0; i < N; i++) os << in.v[i] << ' ';
  280. return os;
  281. }
  282. friend Vec approx_rsqrt(const Vec& x) {
  283. Vec r;
  284. for (int i = 0; i < N; i++) r.v[i] = 1 / sqrt<ValueType>(x.v[i]);
  285. return r;
  286. }
  287. template <class Vec1, class Vec2> friend Vec1 reinterpret(const Vec2& x);
  288. private:
  289. static const ValueType const_zero() {
  290. union {
  291. ValueType value;
  292. unsigned char cvalue[sizeof(ValueType)];
  293. };
  294. for (Integer i = 0; i < (Integer)sizeof(ValueType); i++) cvalue[i] = 0;
  295. return value;
  296. }
  297. static const ValueType const_one() {
  298. union {
  299. ValueType value;
  300. unsigned char cvalue[sizeof(ValueType)];
  301. };
  302. for (Integer i = 0; i < (Integer)sizeof(ValueType); i++) cvalue[i] = ~(unsigned char)0;
  303. return value;
  304. }
  305. ValueType v[N];
  306. };
  307. // Other operators
  308. template <class RetVec, class Vec> RetVec reinterpret(const Vec& v){
  309. static_assert(sizeof(RetVec) == sizeof(Vec));
  310. RetVec& r = *(RetVec*)&v;
  311. return r;
  312. }
  313. template <class RealVec, class IntVec> RealVec ConvertInt2Real(const IntVec& x) {
  314. typedef typename RealVec::ScalarType Real;
  315. typedef typename IntVec::ScalarType Int;
  316. assert(sizeof(RealVec) == sizeof(IntVec));
  317. assert(sizeof(Real) == sizeof(Int));
  318. static constexpr Integer SigBits = TypeTraits<Real>::SigBits;
  319. union {
  320. Int Cint = (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  321. Real Creal;
  322. };
  323. IntVec l(x + IntVec(Cint));
  324. return *(RealVec*)&l - RealVec(Creal);
  325. }
  326. template <class Vec> typename Vec::IntegerVec RoundReal2Int(const Vec& x) {
  327. using IntegerType = typename Vec::IntegerType;
  328. using RealType = typename Vec::RealType;
  329. using IntegerVec = typename Vec::IntegerVec;
  330. using RealVec = typename Vec::RealVec;
  331. static_assert(std::is_same<RealVec,Vec>::value, "RoundReal2Int: expected real input argument!");
  332. static constexpr Integer SigBits = TypeTraits<RealType>::SigBits;
  333. union {
  334. IntegerType Cint = (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(RealType)*8 - SigBits - 2))-1)) << SigBits);
  335. RealType Creal;
  336. };
  337. RealVec d = x + RealVec(Creal);
  338. return reinterpret<IntegerVec>(d) - IntegerVec(Cint);
  339. }
  340. template <class Vec> Vec RoundReal2Real(const Vec& x) {
  341. typedef typename Vec::ScalarType Real;
  342. static constexpr Integer SigBits = TypeTraits<Real>::SigBits;
  343. union {
  344. int64_t Cint = (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  345. Real Creal;
  346. };
  347. Vec Vreal(Creal);
  348. return (x + Vreal) - Vreal;
  349. }
  350. template <class Vec> void sincos_intrin(Vec& sinx, Vec& cosx, const Vec& x) {
  351. constexpr Integer ORDER = 13;
  352. // ORDER ERROR
  353. // 1 8.81e-02
  354. // 3 2.45e-03
  355. // 5 3.63e-05
  356. // 7 3.11e-07
  357. // 9 1.75e-09
  358. // 11 6.93e-12
  359. // 13 2.09e-14
  360. // 15 6.66e-16
  361. // 17 6.66e-16
  362. using Real = typename Vec::ScalarType;
  363. static constexpr Integer SigBits = TypeTraits<Real>::SigBits;
  364. static constexpr Real coeff3 = -1/(((Real)2)*3);
  365. static constexpr Real coeff5 = 1/(((Real)2)*3*4*5);
  366. static constexpr Real coeff7 = -1/(((Real)2)*3*4*5*6*7);
  367. static constexpr Real coeff9 = 1/(((Real)2)*3*4*5*6*7*8*9);
  368. static constexpr Real coeff11 = -1/(((Real)2)*3*4*5*6*7*8*9*10*11);
  369. static constexpr Real coeff13 = 1/(((Real)2)*3*4*5*6*7*8*9*10*11*12*13);
  370. static constexpr Real coeff15 = -1/(((Real)2)*3*4*5*6*7*8*9*10*11*12*13*14*15);
  371. static constexpr Real coeff17 = 1/(((Real)2)*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17);
  372. static constexpr Real coeff19 = -1/(((Real)2)*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19);
  373. static constexpr Real x0 = (Real)1.570796326794896619231321691639l;
  374. static constexpr Real invx0 = 1 / x0;
  375. Vec x_ = RoundReal2Real(x * invx0); // 4.5 - cycles
  376. Vec x1 = x - x_ * x0; // 2 - cycles
  377. Vec x2, x3, x5, x7, x9, x11, x13, x15, x17, x19;
  378. Vec s1 = x1;
  379. if (ORDER >= 3) { // 5 - cycles
  380. x2 = x1 * x1;
  381. x3 = x1 * x2;
  382. s1 += x3 * coeff3;
  383. }
  384. if (ORDER >= 5) { // 3 - cycles
  385. x5 = x3 * x2;
  386. s1 += x5 * coeff5;
  387. }
  388. if (ORDER >= 7) {
  389. x7 = x5 * x2;
  390. s1 += x7 * coeff7;
  391. }
  392. if (ORDER >= 9) {
  393. x9 = x7 * x2;
  394. s1 += x9 * coeff9;
  395. }
  396. if (ORDER >= 11) {
  397. x11 = x9 * x2;
  398. s1 += x11 * coeff11;
  399. }
  400. if (ORDER >= 13) {
  401. x13 = x11 * x2;
  402. s1 += x13 * coeff13;
  403. }
  404. if (ORDER >= 15) {
  405. x15 = x13 * x2;
  406. s1 += x15 * coeff15;
  407. }
  408. if (ORDER >= 17) {
  409. x17 = x15 * x2;
  410. s1 += x17 * coeff17;
  411. }
  412. if (ORDER >= 19) {
  413. x19 = x17 * x2;
  414. s1 += x19 * coeff19;
  415. }
  416. Vec cos_squared = (Real)1.0 - s1 * s1;
  417. Vec inv_cos = approx_rsqrt(cos_squared); // 1.5 - cycles
  418. if (ORDER < 5) {
  419. } else if (ORDER < 9) {
  420. inv_cos *= ((3.0) - cos_squared * inv_cos * inv_cos) * 0.5; // 7 - cycles
  421. } else if (ORDER < 15) {
  422. inv_cos *= ((3.0) - cos_squared * inv_cos * inv_cos); // 7 - cycles
  423. inv_cos *= ((3.0 * pow<pow<0>(3)*3-1>(2.0)) - cos_squared * inv_cos * inv_cos) * (pow<(pow<0>(3)*3-1)*3/2+1>(0.5)); // 8 - cycles
  424. } else {
  425. inv_cos *= ((3.0) - cos_squared * inv_cos * inv_cos); // 7 - cycles
  426. inv_cos *= ((3.0 * pow<pow<0>(3)*3-1>(2.0)) - cos_squared * inv_cos * inv_cos); // 7 - cycles
  427. inv_cos *= ((3.0 * pow<pow<1>(3)*3-1>(2.0)) - cos_squared * inv_cos * inv_cos) * (pow<(pow<1>(3)*3-1)*3/2+1>(0.5)); // 8 - cycles
  428. }
  429. Vec c1 = cos_squared * inv_cos; // 1 - cycle
  430. union {
  431. int64_t int_zero = 0 + (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  432. Real real_zero;
  433. };
  434. union {
  435. int64_t int_one = 1 + (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  436. Real real_one;
  437. };
  438. union {
  439. int64_t int_two = 2 + (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  440. Real real_two;
  441. };
  442. Vec x_offset(real_zero);
  443. auto xAnd1 = (((x_+x_offset) & Vec(real_one)) == x_offset);
  444. auto xAnd2 = (((x_+x_offset) & Vec(real_two)) == x_offset);
  445. Vec s2 = AndNot( c1,xAnd1) | (s1 & xAnd1);
  446. Vec c2 = AndNot(-s1,xAnd1) | (c1 & xAnd1);
  447. Vec s3 = AndNot(-s2,xAnd2) | (s2 & xAnd2);
  448. Vec c3 = AndNot(-c2,xAnd2) | (c2 & xAnd2);
  449. sinx = s3;
  450. cosx = c3;
  451. }
  452. template <class Vec> void exp_intrin(Vec& expx, const Vec& x) {
  453. constexpr Integer ORDER = 10;
  454. using IntegerType = typename Vec::IntegerType;
  455. using RealType = typename Vec::RealType;
  456. using IntegerVec = typename Vec::IntegerVec;
  457. using RealVec = typename Vec::RealVec;
  458. static_assert(std::is_same<Vec,RealVec>::value, "exp_intrin: expected a real argument");
  459. using Real = typename RealVec::ScalarType;
  460. static constexpr Integer SigBits = TypeTraits<Real>::SigBits;
  461. static constexpr Real coeff2 = 1/(((Real)2));
  462. static constexpr Real coeff3 = 1/(((Real)2)*3);
  463. static constexpr Real coeff4 = 1/(((Real)2)*3*4);
  464. static constexpr Real coeff5 = 1/(((Real)2)*3*4*5);
  465. static constexpr Real coeff6 = 1/(((Real)2)*3*4*5*6);
  466. static constexpr Real coeff7 = 1/(((Real)2)*3*4*5*6*7);
  467. static constexpr Real coeff8 = 1/(((Real)2)*3*4*5*6*7*8);
  468. static constexpr Real coeff9 = 1/(((Real)2)*3*4*5*6*7*8*9);
  469. static constexpr Real coeff10 = 1/(((Real)2)*3*4*5*6*7*8*9*10);
  470. static constexpr Real x0 = (Real)0.693147180559945309417232121458l; // ln(2)
  471. static constexpr Real invx0 = 1 / x0;
  472. RealVec x_ = RoundReal2Real(x * invx0); // 4.5 - cycles
  473. IntegerVec int_x_ = RoundReal2Int<RealVec>(x_);
  474. RealVec x1 = x - x_ * x0; // 2 - cycles
  475. RealVec x2, x3, x4, x5, x6, x7, x8, x9, x10;
  476. RealVec e1 = 1.0 + x1;
  477. if (ORDER >= 2) {
  478. x2 = x1 * x1;
  479. e1 += x2 * coeff2;
  480. }
  481. if (ORDER >= 3) {
  482. x3 = x2 * x1;
  483. e1 += x3 * coeff3;
  484. }
  485. if (ORDER >= 4) {
  486. x4 = x2 * x2;
  487. e1 += x4 * coeff4;
  488. }
  489. if (ORDER >= 5) {
  490. x5 = x3 * x2;
  491. e1 += x5 * coeff5;
  492. }
  493. if (ORDER >= 6) {
  494. x6 = x3 * x3;
  495. e1 += x6 * coeff6;
  496. }
  497. if (ORDER >= 7) {
  498. x7 = x4 * x3;
  499. e1 += x7 * coeff7;
  500. }
  501. if (ORDER >= 8) {
  502. x8 = x4 * x4;
  503. e1 += x8 * coeff8;
  504. }
  505. if (ORDER >= 9) {
  506. x9 = x5 * x4;
  507. e1 += x9 * coeff9;
  508. }
  509. if (ORDER >= 10) {
  510. x10 = x5 * x5;
  511. e1 += x10 * coeff10;
  512. }
  513. RealVec e2;
  514. { // set e2 = 2 ^ x_
  515. union {
  516. RealType real_one = 1.0;
  517. IntegerType int_one;
  518. };
  519. //__m256i int_e2 = _mm256_add_epi64(
  520. // _mm256_set1_epi64x(int_one),
  521. // _mm256_slli_epi64(
  522. // _mm256_load_si256((__m256i const*)&int_x_),
  523. // SigBits
  524. // )
  525. // ); // int_e2 = int_one + (int_x_ << SigBits);
  526. IntegerVec int_e2 = IntegerVec(int_one) + (int_x_ << SigBits);
  527. // Handle underflow
  528. static constexpr IntegerType max_exp = -(IntegerType)(1UL<<((sizeof(Real)*8-SigBits-2)));
  529. int_e2 &= (int_x_ > IntegerVec(max_exp));
  530. e2 = RealVec::LoadAligned((RealType*)&int_e2);
  531. }
  532. expx = e1 * e2;
  533. }
  534. #ifdef __AVX__
  535. template <> class alignas(sizeof(double)*4) Vec<double,4> {
  536. typedef __m256d VecType;
  537. typedef double ValueType;
  538. static constexpr Integer N = 4;
  539. public:
  540. typedef typename GetType<DataType::Integer,TypeTraits<ValueType>::Size>::ValueType IntegerType;
  541. typedef typename GetType<DataType::Real,TypeTraits<ValueType>::Size>::ValueType RealType;
  542. typedef Vec<IntegerType,N> IntegerVec;
  543. typedef Vec<RealType,N> RealVec;
  544. typedef ValueType ScalarType;
  545. static constexpr Integer Size() {
  546. return N;
  547. }
  548. static Vec Zero() {
  549. Vec r;
  550. r.v = _mm256_setzero_pd();
  551. return r;
  552. }
  553. static Vec Load1(ValueType const* p) {
  554. Vec r;
  555. r.v = _mm256_broadcast_sd(p);
  556. return r;
  557. }
  558. static Vec Load(ValueType const* p) {
  559. Vec r;
  560. r.v = _mm256_loadu_pd(p);
  561. return r;
  562. }
  563. static Vec LoadAligned(ValueType const* p) {
  564. Vec r;
  565. r.v = _mm256_load_pd(p);
  566. return r;
  567. }
  568. Vec() = default;
  569. Vec(const ValueType& a) {
  570. v = _mm256_set1_pd(a);
  571. }
  572. void Store(ValueType* p) const {
  573. _mm256_storeu_pd(p, v);
  574. }
  575. void StoreAligned(ValueType* p) const {
  576. _mm256_store_pd(p, v);
  577. }
  578. // Bitwise NOT
  579. Vec operator~() const {
  580. Vec r;
  581. static constexpr ScalarType Creal = -1.0;
  582. r.v = _mm256_xor_pd(v, _mm256_set1_pd(Creal));
  583. return r;
  584. }
  585. // Unary plus and minus
  586. Vec operator+() const {
  587. return *this;
  588. }
  589. Vec operator-() const {
  590. return Zero() - (*this);
  591. }
  592. // C-style cast
  593. //template <class RetValueType> explicit operator Vec<RetValueType,N>() const {
  594. //}
  595. // Arithmetic operators
  596. friend Vec operator*(Vec lhs, const Vec& rhs) {
  597. lhs.v = _mm256_mul_pd(lhs.v, rhs.v);
  598. return lhs;
  599. }
  600. friend Vec operator+(Vec lhs, const Vec& rhs) {
  601. lhs.v = _mm256_add_pd(lhs.v, rhs.v);
  602. return lhs;
  603. }
  604. friend Vec operator-(Vec lhs, const Vec& rhs) {
  605. lhs.v = _mm256_sub_pd(lhs.v, rhs.v);
  606. return lhs;
  607. }
  608. friend Vec FMA(Vec a, const Vec& b, const Vec& c) {
  609. #ifdef __FMA__
  610. a.v = _mm256_fmadd_pd(a.v, b.v, c.v);
  611. #else
  612. a.v = _mm256_add_pd(_mm256_mul_pd(a.v, b.v), c.v);
  613. #endif
  614. return a;
  615. }
  616. // Comparison operators
  617. friend Vec operator< (Vec lhs, const Vec& rhs) {
  618. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_LT_OS);
  619. return lhs;
  620. }
  621. friend Vec operator<=(Vec lhs, const Vec& rhs) {
  622. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_LE_OS);
  623. return lhs;
  624. }
  625. friend Vec operator>=(Vec lhs, const Vec& rhs) {
  626. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_GE_OS);
  627. return lhs;
  628. }
  629. friend Vec operator> (Vec lhs, const Vec& rhs) {
  630. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_GT_OS);
  631. return lhs;
  632. }
  633. friend Vec operator==(Vec lhs, const Vec& rhs) {
  634. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_EQ_OS);
  635. return lhs;
  636. }
  637. friend Vec operator!=(Vec lhs, const Vec& rhs) {
  638. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_NEQ_OS);
  639. return lhs;
  640. }
  641. // Bitwise operators
  642. friend Vec operator&(Vec lhs, const Vec& rhs) {
  643. lhs.v = _mm256_and_pd(lhs.v, rhs.v);
  644. return lhs;
  645. }
  646. friend Vec operator^(Vec lhs, const Vec& rhs) {
  647. lhs.v = _mm256_xor_pd(lhs.v, rhs.v);
  648. return lhs;
  649. }
  650. friend Vec operator|(Vec lhs, const Vec& rhs) {
  651. lhs.v = _mm256_or_pd(lhs.v, rhs.v);
  652. return lhs;
  653. }
  654. friend Vec AndNot(Vec lhs, const Vec& rhs) {
  655. lhs.v = _mm256_andnot_pd(rhs.v, lhs.v);
  656. return lhs;
  657. }
  658. // Assignment operators
  659. Vec& operator*=(const Vec& rhs) {
  660. v = _mm256_mul_pd(v, rhs.v);
  661. return *this;
  662. }
  663. Vec& operator+=(const Vec& rhs) {
  664. v = _mm256_add_pd(v, rhs.v);
  665. return *this;
  666. }
  667. Vec& operator-=(const Vec& rhs) {
  668. v = _mm256_sub_pd(v, rhs.v);
  669. return *this;
  670. }
  671. Vec& operator&=(const Vec& rhs) {
  672. v = _mm256_and_pd(v, rhs.v);
  673. return *this;
  674. }
  675. Vec& operator^=(const Vec& rhs) {
  676. v = _mm256_xor_pd(v, rhs.v);
  677. return *this;
  678. }
  679. Vec& operator|=(const Vec& rhs) {
  680. v = _mm256_or_pd(v, rhs.v);
  681. return *this;
  682. }
  683. // Other operators
  684. friend Vec max(Vec lhs, const Vec& rhs) {
  685. lhs.v = _mm256_max_pd(lhs.v, rhs.v);
  686. return lhs;
  687. }
  688. friend Vec min(Vec lhs, const Vec& rhs) {
  689. lhs.v = _mm256_min_pd(lhs.v, rhs.v);
  690. return lhs;
  691. }
  692. friend std::ostream& operator<<(std::ostream& os, const Vec& in) {
  693. union {
  694. VecType vec;
  695. ValueType val[N];
  696. };
  697. vec = in.v;
  698. for (Integer i = 0; i < N; i++) os << val[i] << ' ';
  699. return os;
  700. }
  701. friend Vec approx_rsqrt(const Vec& x) {
  702. Vec r;
  703. r.v = _mm256_cvtps_pd(_mm_rsqrt_ps(_mm256_cvtpd_ps(x.v)));
  704. return r;
  705. }
  706. template <class Vec1, class Vec2> friend Vec1 reinterpret(const Vec2& x);
  707. template <class Vec> friend Vec RoundReal2Real(const Vec& x);
  708. template <class Vec> friend void sincos_intrin(Vec& sinx, Vec& cosx, const Vec& x);
  709. template <class Vec> friend void exp_intrin(Vec& expx, const Vec& x);
  710. private:
  711. VecType v;
  712. };
  713. template <> inline Vec<int64_t,4> reinterpret<Vec<int64_t,4>,Vec<double,4>>(const Vec<double,4>& x){
  714. union {
  715. Vec<int64_t,4> r;
  716. __m256i y;
  717. };
  718. y = _mm256_castpd_si256(x.v);
  719. return r;
  720. }
  721. template <> inline Vec<double,4> RoundReal2Real(const Vec<double,4>& x) {
  722. Vec<double,4> r;
  723. r.v = _mm256_round_pd(x.v,_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC);
  724. return r;
  725. }
  726. #ifdef SCTL_HAVE_SVML
  727. template <> inline void sincos_intrin(Vec<double,4>& sinx, Vec<double,4>& cosx, const Vec<double,4>& x) {
  728. sinx.v = _mm256_sin_pd(x.v);
  729. cosx.v = _mm256_cos_pd(x.v);
  730. }
  731. template <> inline void exp_intrin(Vec<double,4>& expx, const Vec<double,4>& x) {
  732. expx.v = _mm256_exp_pd(x.v);
  733. }
  734. #endif
  735. #endif
  736. #ifdef __AVX512F__
  737. template <> class alignas(sizeof(double)*8) Vec<double,8> {
  738. typedef __m512d VecType;
  739. typedef double ValueType;
  740. static constexpr Integer N = 8;
  741. public:
  742. typedef typename GetType<DataType::Integer,TypeTraits<ValueType>::Size>::ValueType IntegerType;
  743. typedef typename GetType<DataType::Real,TypeTraits<ValueType>::Size>::ValueType RealType;
  744. typedef Vec<IntegerType,N> IntegerVec;
  745. typedef Vec<RealType,N> RealVec;
  746. typedef ValueType ScalarType;
  747. static constexpr Integer Size() {
  748. return N;
  749. }
  750. static Vec Zero() {
  751. Vec r;
  752. r.v = _mm512_setzero_pd();
  753. return r;
  754. }
  755. static Vec Load1(ValueType const* p) {
  756. Vec r;
  757. // TODO: different from _m256d, could make it faster?
  758. // r.v = _mm512_broadcast_f64x4(_mm256_broadcast_sd(p));
  759. r.v = _mm512_set1_pd(*p);
  760. return r;
  761. }
  762. static Vec Load(ValueType const* p) {
  763. Vec r;
  764. r.v = _mm512_loadu_pd(p);
  765. return r;
  766. }
  767. static Vec LoadAligned(ValueType const* p) {
  768. Vec r;
  769. r.v = _mm512_load_pd(p);
  770. return r;
  771. }
  772. Vec() = default;
  773. Vec(const ValueType& a) {
  774. v = _mm512_set1_pd(a);
  775. }
  776. Vec(const __mmask8& a) = delete; // disallow implicit conversions
  777. void Store(ValueType* p) const {
  778. _mm512_storeu_pd(p, v);
  779. }
  780. void StoreAligned(ValueType* p) const {
  781. _mm512_store_pd(p, v);
  782. }
  783. // Bitwise NOT
  784. Vec operator~() const {
  785. Vec r;
  786. static constexpr ScalarType Creal = -1.0;
  787. r.v = _mm512_xor_pd(v, _mm512_set1_pd(Creal));
  788. return r;
  789. }
  790. // Unary plus and minus
  791. Vec operator+() const {
  792. return *this;
  793. }
  794. Vec operator-() const {
  795. return Zero() - (*this);
  796. }
  797. // C-style cast
  798. //template <class RetValueType> explicit operator Vec<RetValueType,N>() const {
  799. //}
  800. // Arithmetic operators
  801. friend Vec operator*(Vec lhs, const Vec& rhs) {
  802. lhs.v = _mm512_mul_pd(lhs.v, rhs.v);
  803. return lhs;
  804. }
  805. friend Vec operator+(Vec lhs, const Vec& rhs) {
  806. lhs.v = _mm512_add_pd(lhs.v, rhs.v);
  807. return lhs;
  808. }
  809. friend Vec operator-(Vec lhs, const Vec& rhs) {
  810. lhs.v = _mm512_sub_pd(lhs.v, rhs.v);
  811. return lhs;
  812. }
  813. friend Vec FMA(Vec a, const Vec& b, const Vec& c) {
  814. a.v = _mm512_fmadd_pd(a.v, b.v, c.v);
  815. //a.v = _mm512_add_pd(_mm512_mul_pd(a.v, b.v), c.v);
  816. return a;
  817. }
  818. // Comparison operators
  819. //friend Vec operator< (Vec lhs, const Vec& rhs) {
  820. // lhs.v = _mm512_castsi512_pd(_mm512_movm_epi64(_mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_LT_OS)));
  821. // return lhs;
  822. //}
  823. //friend Vec operator<=(Vec lhs, const Vec& rhs) {
  824. // lhs.v = _mm512_castsi512_pd(_mm512_movm_epi64(_mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_LE_OS)));
  825. // return lhs;
  826. //}
  827. //friend Vec operator>=(Vec lhs, const Vec& rhs) {
  828. // lhs.v = _mm512_castsi512_pd(_mm512_movm_epi64(_mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_GE_OS)));
  829. // return lhs;
  830. //}
  831. //friend Vec operator> (Vec lhs, const Vec& rhs) {
  832. // lhs.v = _mm512_castsi512_pd(_mm512_movm_epi64(_mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_GT_OS)));
  833. // return lhs;
  834. //}
  835. //friend Vec operator==(Vec lhs, const Vec& rhs) {
  836. // lhs.v = _mm512_castsi512_pd(_mm512_movm_epi64(_mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_EQ_OS)));
  837. // return lhs;
  838. //}
  839. //friend Vec operator!=(Vec lhs, const Vec& rhs) {
  840. // lhs.v = _mm512_castsi512_pd(_mm512_movm_epi64(_mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_NEQ_OS)));
  841. // return lhs;
  842. //}
  843. friend __mmask8 operator< (Vec lhs, const Vec& rhs) {
  844. return _mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_LT_OS);
  845. }
  846. friend __mmask8 operator<=(Vec lhs, const Vec& rhs) {
  847. return _mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_LE_OS);
  848. }
  849. friend __mmask8 operator>=(Vec lhs, const Vec& rhs) {
  850. return _mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_GE_OS);
  851. }
  852. friend __mmask8 operator> (Vec lhs, const Vec& rhs) {
  853. return _mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_GT_OS);
  854. }
  855. friend __mmask8 operator==(Vec lhs, const Vec& rhs) {
  856. return _mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_EQ_OS);
  857. }
  858. friend __mmask8 operator!=(Vec lhs, const Vec& rhs) {
  859. return _mm512_cmp_pd_mask(lhs.v, rhs.v, _CMP_NEQ_OS);
  860. }
  861. // Bitwise operators
  862. friend Vec operator&(Vec lhs, const Vec& rhs) {
  863. lhs.v = _mm512_and_pd(lhs.v, rhs.v);
  864. return lhs;
  865. }
  866. friend Vec operator^(Vec lhs, const Vec& rhs) {
  867. lhs.v = _mm512_xor_pd(lhs.v, rhs.v);
  868. return lhs;
  869. }
  870. friend Vec operator|(Vec lhs, const Vec& rhs) {
  871. lhs.v = _mm512_or_pd(lhs.v, rhs.v);
  872. return lhs;
  873. }
  874. friend Vec AndNot(Vec lhs, const Vec& rhs) {
  875. lhs.v = _mm512_andnot_pd(rhs.v, lhs.v);
  876. return lhs;
  877. }
  878. friend Vec operator&(Vec lhs, const __mmask8& rhs) {
  879. lhs.v = _mm512_maskz_mov_pd(rhs, lhs.v);
  880. return lhs;
  881. }
  882. friend Vec AndNot(Vec lhs, const __mmask8& rhs) {
  883. lhs.v = _mm512_mask_mov_pd(lhs.v, rhs, _mm512_setzero_pd());
  884. return lhs;
  885. }
  886. // Assignment operators
  887. Vec& operator*=(const Vec& rhs) {
  888. v = _mm512_mul_pd(v, rhs.v);
  889. return *this;
  890. }
  891. Vec& operator+=(const Vec& rhs) {
  892. v = _mm512_add_pd(v, rhs.v);
  893. return *this;
  894. }
  895. Vec& operator-=(const Vec& rhs) {
  896. v = _mm512_sub_pd(v, rhs.v);
  897. return *this;
  898. }
  899. Vec& operator&=(const Vec& rhs) {
  900. v = _mm512_and_pd(v, rhs.v);
  901. return *this;
  902. }
  903. Vec& operator^=(const Vec& rhs) {
  904. v = _mm512_xor_pd(v, rhs.v);
  905. return *this;
  906. }
  907. Vec& operator|=(const Vec& rhs) {
  908. v = _mm512_or_pd(v, rhs.v);
  909. return *this;
  910. }
  911. Vec& operator&=(const __mmask8& rhs) {
  912. v = _mm512_maskz_mov_pd(rhs, v);
  913. return *this;
  914. }
  915. // Other operators
  916. friend Vec max(Vec lhs, const Vec& rhs) {
  917. lhs.v = _mm512_max_pd(lhs.v, rhs.v);
  918. return lhs;
  919. }
  920. friend Vec min(Vec lhs, const Vec& rhs) {
  921. lhs.v = _mm512_min_pd(lhs.v, rhs.v);
  922. return lhs;
  923. }
  924. friend std::ostream& operator<<(std::ostream& os, const Vec& in) {
  925. union {
  926. VecType vec;
  927. ValueType val[N];
  928. };
  929. vec = in.v;
  930. for (Integer i = 0; i < N; i++) os << val[i] << ' ';
  931. return os;
  932. }
  933. friend Vec approx_rsqrt(const Vec& x) {
  934. Vec r;
  935. r.v = _mm512_cvtps_pd(_mm256_rsqrt_ps(_mm512_cvtpd_ps(x.v)));
  936. return r;
  937. }
  938. template <class Vec1, class Vec2> friend Vec1 reinterpret(const Vec2& x);
  939. template <class Vec> friend Vec RoundReal2Real(const Vec& x);
  940. template <class Vec> friend void sincos_intrin(Vec& sinx, Vec& cosx, const Vec& x);
  941. template <class Vec> friend void exp_intrin(Vec& expx, const Vec& x);
  942. private:
  943. VecType v;
  944. };
  945. template <> inline Vec<int64_t,8> reinterpret<Vec<int64_t,8>,Vec<double,8>>(const Vec<double,8>& x){
  946. union {
  947. Vec<int64_t,8> r;
  948. __m512i y;
  949. };
  950. y = _mm512_castpd_si512(x.v);
  951. return r;
  952. }
  953. template <> inline Vec<double,8> RoundReal2Real(const Vec<double,8>& x) {
  954. Vec<double,8> r;
  955. // TODO: need double check
  956. r.v = _mm512_roundscale_pd(x.v,_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC);
  957. return r;
  958. }
  959. #ifdef SCTL_HAVE_SVML
  960. template <> inline void sincos_intrin(Vec<double,8>& sinx, Vec<double,8>& cosx, const Vec<double,8>& x) {
  961. sinx.v = _mm512_sin_pd(x.v);
  962. cosx.v = _mm512_cos_pd(x.v);
  963. }
  964. template <> inline void exp_intrin(Vec<double,8>& expx, const Vec<double,8>& x) {
  965. expx.v = _mm512_exp_pd(x.v);
  966. }
  967. #endif
  968. #endif
  969. }
  970. #endif //_SCTL_VEC_WRAPPER_HPP_