vec.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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 <cstdint>
  6. #include <ostream>
  7. #ifdef __SSE__
  8. #include <xmmintrin.h>
  9. #endif
  10. #ifdef __SSE2__
  11. #include <emmintrin.h>
  12. #endif
  13. #ifdef __SSE3__
  14. #include <pmmintrin.h>
  15. #endif
  16. #ifdef __SSE4_2__
  17. #include <smmintrin.h>
  18. #endif
  19. #ifdef __AVX__
  20. #include <immintrin.h>
  21. #endif
  22. #if defined(__MIC__)
  23. #include <immintrin.h>
  24. #endif
  25. // TODO: Implement AVX versions of floats, int32_t, int64_t
  26. // TODO: Add operators to reinterpret types
  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() {}
  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. private:
  288. static const ValueType const_zero() {
  289. union {
  290. ValueType value;
  291. unsigned char cvalue[sizeof(ValueType)];
  292. };
  293. for (Integer i = 0; i < (Integer)sizeof(ValueType); i++) cvalue[i] = 0;
  294. return value;
  295. }
  296. static const ValueType const_one() {
  297. union {
  298. ValueType value;
  299. unsigned char cvalue[sizeof(ValueType)];
  300. };
  301. for (Integer i = 0; i < (Integer)sizeof(ValueType); i++) cvalue[i] = ~(unsigned char)0;
  302. return value;
  303. }
  304. ValueType v[N];
  305. };
  306. // Other operators
  307. template <class RealVec, class IntVec> RealVec ConvertInt2Real(const IntVec& x) {
  308. typedef typename RealVec::ScalarType Real;
  309. typedef typename IntVec::ScalarType Int;
  310. assert(sizeof(RealVec) == sizeof(IntVec));
  311. assert(sizeof(Real) == sizeof(Int));
  312. static constexpr Integer SigBits = TypeTraits<Real>::SigBits;
  313. union {
  314. Int Cint = (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  315. Real Creal;
  316. };
  317. IntVec l(x + IntVec(Cint));
  318. return *(RealVec*)&l - RealVec(Creal);
  319. }
  320. // union {
  321. // Int Cint = (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  322. // Real Creal;
  323. // };
  324. // RealVec d(x + RealVec(Creal));
  325. // return *(IntVec*)&d - IntVec(Cint);
  326. //}
  327. template <class Vec> typename Vec::IntegerVec RoundReal2Int(const Vec& x) {
  328. using IntegerType = typename Vec::IntegerType;
  329. using RealType = typename Vec::RealType;
  330. using IntegerVec = typename Vec::IntegerVec;
  331. using RealVec = typename Vec::RealVec;
  332. if (std::is_same<IntegerVec,Vec>::value) {
  333. IntegerVec v = IntegerVec::LoadAligned((const IntegerType*)&x); // TODO: simplify: return x;
  334. return v;
  335. } else if (std::is_same<RealVec,Vec>::value) {
  336. static constexpr Integer SigBits = TypeTraits<RealType>::SigBits;
  337. union {
  338. IntegerType Cint = (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(RealType)*8 - SigBits - 2))-1)) << SigBits);
  339. RealType Creal;
  340. };
  341. RealVec d(x + RealVec(Creal));
  342. return IntegerVec::LoadAligned((const IntegerType*)&d) - IntegerVec(Cint);
  343. } else {
  344. IntegerVec v;
  345. return v;
  346. }
  347. }
  348. template <class Vec> Vec RoundReal2Real(const Vec& x) {
  349. typedef typename Vec::ScalarType Real;
  350. static constexpr Integer SigBits = TypeTraits<Real>::SigBits;
  351. union {
  352. int64_t Cint = (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  353. Real Creal;
  354. };
  355. Vec Vreal(Creal);
  356. return (x + Vreal) - Vreal;
  357. }
  358. template <class Vec> void sincos_intrin(Vec& sinx, Vec& cosx, const Vec& x) {
  359. constexpr Integer ORDER = 13;
  360. // ORDER ERROR
  361. // 1 8.81e-02
  362. // 3 2.45e-03
  363. // 5 3.63e-05
  364. // 7 3.11e-07
  365. // 9 1.75e-09
  366. // 11 6.93e-12
  367. // 13 2.09e-14
  368. // 15 6.66e-16
  369. // 17 6.66e-16
  370. using Real = typename Vec::ScalarType;
  371. static constexpr Integer SigBits = TypeTraits<Real>::SigBits;
  372. static constexpr Real coeff3 = -1/(((Real)2)*3);
  373. static constexpr Real coeff5 = 1/(((Real)2)*3*4*5);
  374. static constexpr Real coeff7 = -1/(((Real)2)*3*4*5*6*7);
  375. static constexpr Real coeff9 = 1/(((Real)2)*3*4*5*6*7*8*9);
  376. static constexpr Real coeff11 = -1/(((Real)2)*3*4*5*6*7*8*9*10*11);
  377. static constexpr Real coeff13 = 1/(((Real)2)*3*4*5*6*7*8*9*10*11*12*13);
  378. static constexpr Real coeff15 = -1/(((Real)2)*3*4*5*6*7*8*9*10*11*12*13*14*15);
  379. static constexpr Real coeff17 = 1/(((Real)2)*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17);
  380. static constexpr Real coeff19 = -1/(((Real)2)*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19);
  381. static constexpr Real x0 = (Real)1.570796326794896619231321691639l;
  382. static constexpr Real invx0 = 1 / x0;
  383. Vec x_ = RoundReal2Real(x * invx0); // 4.5 - cycles
  384. Vec x1 = x - x_ * x0; // 2 - cycles
  385. Vec x2, x3, x5, x7, x9, x11, x13, x15, x17, x19;
  386. Vec s1 = x1;
  387. if (ORDER >= 3) { // 5 - cycles
  388. x2 = x1 * x1;
  389. x3 = x1 * x2;
  390. s1 += x3 * coeff3;
  391. }
  392. if (ORDER >= 5) { // 3 - cycles
  393. x5 = x3 * x2;
  394. s1 += x5 * coeff5;
  395. }
  396. if (ORDER >= 7) {
  397. x7 = x5 * x2;
  398. s1 += x7 * coeff7;
  399. }
  400. if (ORDER >= 9) {
  401. x9 = x7 * x2;
  402. s1 += x9 * coeff9;
  403. }
  404. if (ORDER >= 11) {
  405. x11 = x9 * x2;
  406. s1 += x11 * coeff11;
  407. }
  408. if (ORDER >= 13) {
  409. x13 = x11 * x2;
  410. s1 += x13 * coeff13;
  411. }
  412. if (ORDER >= 15) {
  413. x15 = x13 * x2;
  414. s1 += x15 * coeff15;
  415. }
  416. if (ORDER >= 17) {
  417. x17 = x15 * x2;
  418. s1 += x17 * coeff17;
  419. }
  420. if (ORDER >= 19) {
  421. x19 = x17 * x2;
  422. s1 += x19 * coeff19;
  423. }
  424. Vec cos_squared = (Real)1.0 - s1 * s1;
  425. Vec inv_cos = approx_rsqrt(cos_squared); // 1.5 - cycles
  426. if (ORDER < 5) {
  427. } else if (ORDER < 9) {
  428. inv_cos *= ((3.0) - cos_squared * inv_cos * inv_cos) * 0.5; // 7 - cycles
  429. } else if (ORDER < 15) {
  430. inv_cos *= ((3.0) - cos_squared * inv_cos * inv_cos); // 7 - cycles
  431. 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
  432. } else {
  433. inv_cos *= ((3.0) - cos_squared * inv_cos * inv_cos); // 7 - cycles
  434. inv_cos *= ((3.0 * pow<pow<0>(3)*3-1>(2.0)) - cos_squared * inv_cos * inv_cos); // 7 - cycles
  435. 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
  436. }
  437. Vec c1 = cos_squared * inv_cos; // 1 - cycle
  438. union {
  439. int64_t int_zero = 0 + (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  440. Real real_zero;
  441. };
  442. union {
  443. int64_t int_one = 1 + (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  444. Real real_one;
  445. };
  446. union {
  447. int64_t int_two = 2 + (1UL << (SigBits - 1)) + ((SigBits + ((1UL<<(sizeof(Real)*8 - SigBits - 2))-1)) << SigBits);
  448. Real real_two;
  449. };
  450. Vec x_offset(real_zero);
  451. Vec xAnd1 = (((x_+x_offset) & Vec(real_one)) == x_offset);
  452. Vec xAnd2 = (((x_+x_offset) & Vec(real_two)) == x_offset);
  453. Vec s2 = AndNot( c1,xAnd1) | (s1 & xAnd1);
  454. Vec c2 = AndNot(-s1,xAnd1) | (c1 & xAnd1);
  455. Vec s3 = AndNot(-s2,xAnd2) | (s2 & xAnd2);
  456. Vec c3 = AndNot(-c2,xAnd2) | (c2 & xAnd2);
  457. sinx = s3;
  458. cosx = c3;
  459. }
  460. template <class Vec> void exp_intrin(Vec& expx, const Vec& x) {
  461. constexpr Integer ORDER = 10;
  462. using IntegerType = typename Vec::IntegerType;
  463. using RealType = typename Vec::RealType;
  464. using IntegerVec = typename Vec::IntegerVec;
  465. using RealVec = typename Vec::RealVec;
  466. static_assert(std::is_same<Vec,RealVec>::value, "exp_intrin: expected a real argument");
  467. using Real = typename RealVec::ScalarType;
  468. static constexpr Integer SigBits = TypeTraits<Real>::SigBits;
  469. static constexpr Real coeff2 = 1/(((Real)2));
  470. static constexpr Real coeff3 = 1/(((Real)2)*3);
  471. static constexpr Real coeff4 = 1/(((Real)2)*3*4);
  472. static constexpr Real coeff5 = 1/(((Real)2)*3*4*5);
  473. static constexpr Real coeff6 = 1/(((Real)2)*3*4*5*6);
  474. static constexpr Real coeff7 = 1/(((Real)2)*3*4*5*6*7);
  475. static constexpr Real coeff8 = 1/(((Real)2)*3*4*5*6*7*8);
  476. static constexpr Real coeff9 = 1/(((Real)2)*3*4*5*6*7*8*9);
  477. static constexpr Real coeff10 = 1/(((Real)2)*3*4*5*6*7*8*9*10);
  478. static constexpr Real x0 = (Real)0.693147180559945309417232121458l; // ln(2)
  479. static constexpr Real invx0 = 1 / x0;
  480. RealVec x_ = RoundReal2Real(x * invx0); // 4.5 - cycles
  481. IntegerVec int_x_ = RoundReal2Int<RealVec>(x_);
  482. RealVec x1 = x - x_ * x0; // 2 - cycles
  483. RealVec x2, x3, x4, x5, x6, x7, x8, x9, x10;
  484. RealVec e1 = 1 + x1;
  485. if (ORDER >= 2) {
  486. x2 = x1 * x1;
  487. e1 += x2 * coeff2;
  488. }
  489. if (ORDER >= 3) {
  490. x3 = x2 * x1;
  491. e1 += x3 * coeff3;
  492. }
  493. if (ORDER >= 4) {
  494. x4 = x2 * x2;
  495. e1 += x4 * coeff4;
  496. }
  497. if (ORDER >= 5) {
  498. x5 = x3 * x2;
  499. e1 += x5 * coeff5;
  500. }
  501. if (ORDER >= 6) {
  502. x6 = x3 * x3;
  503. e1 += x6 * coeff6;
  504. }
  505. if (ORDER >= 7) {
  506. x7 = x4 * x3;
  507. e1 += x7 * coeff7;
  508. }
  509. if (ORDER >= 8) {
  510. x8 = x4 * x4;
  511. e1 += x8 * coeff8;
  512. }
  513. if (ORDER >= 9) {
  514. x9 = x5 * x4;
  515. e1 += x9 * coeff9;
  516. }
  517. if (ORDER >= 10) {
  518. x10 = x5 * x5;
  519. e1 += x10 * coeff10;
  520. }
  521. RealVec e2;
  522. { // set e2 = 2 ^ x_
  523. union {
  524. RealType real_one = 1.0;
  525. IntegerType int_one;
  526. };
  527. //__m256i int_e2 = _mm256_add_epi64(
  528. // _mm256_set1_epi64x(int_one),
  529. // _mm256_slli_epi64(
  530. // _mm256_load_si256((__m256i const*)&int_x_),
  531. // SigBits
  532. // )
  533. // ); // int_e2 = int_one + (int_x_ << SigBits);
  534. IntegerVec int_e2 = IntegerVec(int_one) + (int_x_ << SigBits);
  535. // Handle underflow
  536. static constexpr IntegerType max_exp = -(IntegerType)(1UL<<((sizeof(Real)*8-SigBits-2)));
  537. int_e2 &= (int_x_ > IntegerVec(max_exp));
  538. e2 = RealVec::LoadAligned((RealType*)&int_e2);
  539. }
  540. expx = e1 * e2;
  541. }
  542. #ifdef __AVX__
  543. template <> class alignas(sizeof(double)*4) Vec<double,4> {
  544. typedef __m256d VecType;
  545. typedef double ValueType;
  546. static constexpr Integer N = 4;
  547. public:
  548. typedef typename GetType<DataType::Integer,TypeTraits<ValueType>::Size>::ValueType IntegerType;
  549. typedef typename GetType<DataType::Real,TypeTraits<ValueType>::Size>::ValueType RealType;
  550. typedef Vec<IntegerType,N> IntegerVec;
  551. typedef Vec<RealType,N> RealVec;
  552. typedef ValueType ScalarType;
  553. static constexpr Integer Size() {
  554. return N;
  555. }
  556. static Vec Zero() {
  557. Vec r;
  558. r.v = _mm256_setzero_pd();
  559. return r;
  560. }
  561. static Vec Load1(ValueType const* p) {
  562. Vec r;
  563. r.v = _mm256_broadcast_sd(p);
  564. return r;
  565. }
  566. static Vec Load(ValueType const* p) {
  567. Vec r;
  568. r.v = _mm256_loadu_pd(p);
  569. return r;
  570. }
  571. static Vec LoadAligned(ValueType const* p) {
  572. Vec r;
  573. r.v = _mm256_load_pd(p);
  574. return r;
  575. }
  576. Vec() {}
  577. Vec(const ValueType& a) {
  578. v = _mm256_set1_pd(a);
  579. }
  580. void Store(ValueType* p) const {
  581. _mm256_storeu_pd(p, v);
  582. }
  583. void StoreAligned(ValueType* p) const {
  584. _mm256_store_pd(p, v);
  585. }
  586. // Bitwise NOT
  587. Vec operator~() const {
  588. Vec r;
  589. static constexpr ScalarType Creal = -1.0;
  590. r.v = _mm256_xor_pd(v, _mm256_set1_pd(Creal));
  591. return r;
  592. }
  593. // Unary plus and minus
  594. Vec operator+() const {
  595. return *this;
  596. }
  597. Vec operator-() const {
  598. return Zero() - (*this);
  599. }
  600. // C-style cast
  601. template <class RetValueType> explicit operator Vec<RetValueType,N>() const {
  602. Vec<RetValueType,N> r;
  603. VecType& ret_v = *(VecType*)&r.v;
  604. ret_v = v;
  605. return r;
  606. }
  607. // Arithmetic operators
  608. friend Vec operator*(Vec lhs, const Vec& rhs) {
  609. lhs.v = _mm256_mul_pd(lhs.v, rhs.v);
  610. return lhs;
  611. }
  612. friend Vec operator+(Vec lhs, const Vec& rhs) {
  613. lhs.v = _mm256_add_pd(lhs.v, rhs.v);
  614. return lhs;
  615. }
  616. friend Vec operator-(Vec lhs, const Vec& rhs) {
  617. lhs.v = _mm256_sub_pd(lhs.v, rhs.v);
  618. return lhs;
  619. }
  620. friend Vec FMA(Vec a, const Vec& b, const Vec& c) {
  621. #ifdef __FMA__
  622. a.v = _mm256_fmadd_pd(a.v, b.v, c.v);
  623. #else
  624. a.v = _mm256_add_pd(_mm256_mul_pd(a.v, b.v), c.v);
  625. #endif
  626. return a;
  627. }
  628. // Comparison operators
  629. friend Vec operator< (Vec lhs, const Vec& rhs) {
  630. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_LT_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_LE_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_GE_OS);
  639. return lhs;
  640. }
  641. friend Vec operator> (Vec lhs, const Vec& rhs) {
  642. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_GT_OS);
  643. return lhs;
  644. }
  645. friend Vec operator==(Vec lhs, const Vec& rhs) {
  646. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_EQ_OS);
  647. return lhs;
  648. return lhs;
  649. }
  650. friend Vec operator!=(Vec lhs, const Vec& rhs) {
  651. lhs.v = _mm256_cmp_pd(lhs.v, rhs.v, _CMP_NEQ_OS);
  652. return lhs;
  653. }
  654. // Bitwise operators
  655. friend Vec operator&(Vec lhs, const Vec& rhs) {
  656. lhs.v = _mm256_and_pd(lhs.v, rhs.v);
  657. return lhs;
  658. }
  659. friend Vec operator^(Vec lhs, const Vec& rhs) {
  660. lhs.v = _mm256_xor_pd(lhs.v, rhs.v);
  661. return lhs;
  662. }
  663. friend Vec operator|(Vec lhs, const Vec& rhs) {
  664. lhs.v = _mm256_or_pd(lhs.v, rhs.v);
  665. return lhs;
  666. }
  667. friend Vec AndNot(Vec lhs, const Vec& rhs) {
  668. lhs.v = _mm256_andnot_pd(rhs.v, lhs.v);
  669. return lhs;
  670. }
  671. // Assignment operators
  672. Vec& operator*=(const Vec& rhs) {
  673. v = _mm256_mul_pd(v, rhs.v);
  674. return *this;
  675. }
  676. Vec& operator+=(const Vec& rhs) {
  677. v = _mm256_add_pd(v, rhs.v);
  678. return *this;
  679. }
  680. Vec& operator-=(const Vec& rhs) {
  681. v = _mm256_sub_pd(v, rhs.v);
  682. return *this;
  683. }
  684. Vec& operator&=(const Vec& rhs) {
  685. v = _mm256_and_pd(v, rhs.v);
  686. return *this;
  687. }
  688. Vec& operator^=(const Vec& rhs) {
  689. v = _mm256_xor_pd(v, rhs.v);
  690. return *this;
  691. }
  692. Vec& operator|=(const Vec& rhs) {
  693. v = _mm256_or_pd(v, rhs.v);
  694. return *this;
  695. }
  696. // Other operators
  697. friend Vec max(Vec lhs, const Vec& rhs) {
  698. lhs.v = _mm256_max_pd(lhs.v, rhs.v);
  699. return lhs;
  700. }
  701. friend Vec min(Vec lhs, const Vec& rhs) {
  702. lhs.v = _mm256_min_pd(lhs.v, rhs.v);
  703. return lhs;
  704. }
  705. friend std::ostream& operator<<(std::ostream& os, const Vec& in) {
  706. union {
  707. VecType vec;
  708. ValueType val[N];
  709. };
  710. vec = in.v;
  711. for (Integer i = 0; i < N; i++) os << val[i] << ' ';
  712. return os;
  713. }
  714. friend Vec approx_rsqrt(const Vec& x) {
  715. Vec r;
  716. r.v = _mm256_cvtps_pd(_mm_rsqrt_ps(_mm256_cvtpd_ps(x.v)));
  717. return r;
  718. }
  719. template <class Vec> friend Vec RoundReal2Real(const Vec& x);
  720. template <class Vec> friend void sincos_intrin(Vec& sinx, Vec& cosx, const Vec& x);
  721. template <class Vec> friend void exp_intrin(Vec& expx, const Vec& x);
  722. private:
  723. VecType v;
  724. };
  725. template <> inline Vec<double,4> RoundReal2Real(const Vec<double,4>& x) {
  726. Vec<double,4> r;
  727. r.v = _mm256_round_pd(x.v,_MM_FROUND_TO_NEAREST_INT |_MM_FROUND_NO_EXC);
  728. return r;
  729. }
  730. #ifdef SCTL_HAVE_SVML
  731. template <> inline void sincos_intrin(Vec<double,4>& sinx, Vec<double,4>& cosx, const Vec<double,4>& x) {
  732. sinx.v = _mm256_sin_pd(x.v);
  733. cosx.v = _mm256_cos_pd(x.v);
  734. }
  735. template <> inline void exp_intrin(Vec<double,4>& expx, const Vec<double,4>& x) {
  736. expx.v = _mm256_exp_pd(x.v);
  737. }
  738. #endif
  739. #endif
  740. }
  741. #endif //_SCTL_VEC_WRAPPER_HPP_