CpuGeneratorsAVXFMA.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. #include "CpuGenerators.h"
  2. #include <immintrin.h>
  3. #include <omp.h>
  4. #include <cmath>
  5. #include <utility>
  6. #include <memory>
  7. using mnd::CpuGenerator;
  8. namespace mnd
  9. {
  10. template class CpuGenerator<float, mnd::X86_AVX_FMA, false>;
  11. template class CpuGenerator<float, mnd::X86_AVX_FMA, true>;
  12. template class CpuGenerator<double, mnd::X86_AVX_FMA, false>;
  13. template class CpuGenerator<double, mnd::X86_AVX_FMA, true>;
  14. template class CpuGenerator<DoubleDouble, mnd::X86_AVX_FMA, false>;
  15. template class CpuGenerator<DoubleDouble, mnd::X86_AVX_FMA, true>;
  16. template class CpuGenerator<QuadDouble, mnd::X86_AVX_FMA, false>;
  17. template class CpuGenerator<QuadDouble, mnd::X86_AVX_FMA, true>;
  18. }
  19. template<bool parallel>
  20. void CpuGenerator<float, mnd::X86_AVX_FMA, parallel>::generate(const mnd::MandelInfo& info, float* data)
  21. {
  22. using T = float;
  23. const MandelViewport& view = info.view;
  24. const float dppf = float(view.width / info.bWidth);
  25. const float viewxf = float(view.x);
  26. __m256 viewx = { viewxf, viewxf, viewxf, viewxf, viewxf, viewxf, viewxf, viewxf };
  27. __m256 dpp = { dppf, dppf, dppf, dppf, dppf, dppf, dppf, dppf };
  28. T jX = mnd::convert<T>(info.juliaX);
  29. T jY = mnd::convert<T>(info.juliaY);
  30. __m256 juliaX = { jX, jX, jX, jX, jX, jX, jX, jX };
  31. __m256 juliaY = { jY, jY, jY, jY, jY, jY, jY, jY };
  32. #if defined(_OPENMP)
  33. if constexpr(parallel)
  34. omp_set_num_threads(omp_get_num_procs());
  35. # pragma omp parallel for schedule(static, 1) if (parallel)
  36. #endif
  37. for (long j = 0; j < info.bHeight; j++) {
  38. T y = T(view.y) + T(j) * T(view.height / info.bHeight);
  39. __m256 ys = {y, y, y, y, y, y, y, y};
  40. for (long i = 0; i < info.bWidth; i += 24) {
  41. __m256 pixc = { float(i), float(i + 1), float(i + 2), float(i + 3), float(i + 4), float(i + 5), float(i + 6), float(i + 7) };
  42. __m256 pixc2 = { float(i + 8), float(i + 9), float(i + 10), float(i + 11), float(i + 12), float(i + 13), float(i + 14), float(i + 15) };
  43. __m256 pixc3 = { float(i + 16), float(i + 17), float(i + 18), float(i + 19), float(i + 20), float(i + 21), float(i + 22), float(i + 23) };
  44. __m256 xs = _mm256_add_ps(_mm256_mul_ps(dpp, pixc), viewx);
  45. __m256 xs2 = _mm256_add_ps(_mm256_mul_ps(dpp, pixc2), viewx);
  46. __m256 xs3 = _mm256_add_ps(_mm256_mul_ps(dpp, pixc3), viewx);
  47. __m256 counter = { 0, 0, 0, 0, 0, 0, 0, 0 };
  48. __m256 adder = { 1, 1, 1, 1, 1, 1, 1, 1 };
  49. __m256 resultsa = { 0, 0, 0, 0, 0, 0, 0, 0 };
  50. __m256 resultsb = { 0, 0, 0, 0, 0, 0, 0, 0 };
  51. __m256 counter2 = { 0, 0, 0, 0, 0, 0, 0, 0 };
  52. __m256 adder2 = { 1, 1, 1, 1, 1, 1, 1, 1 };
  53. __m256 resultsa2 = { 0, 0, 0, 0, 0, 0, 0, 0 };
  54. __m256 resultsb2 = { 0, 0, 0, 0, 0, 0, 0, 0 };
  55. __m256 counter3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
  56. __m256 adder3 = { 1, 1, 1, 1, 1, 1, 1, 1 };
  57. __m256 resultsa3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
  58. __m256 resultsb3 = { 0, 0, 0, 0, 0, 0, 0, 0 };
  59. __m256 threshold = { 16.0f, 16.0f, 16.0f, 16.0f, 16.0f, 16.0f, 16.0f, 16.0f };
  60. __m256 two = { 2, 2, 2, 2, 2, 2, 2, 2 };
  61. __m256 a = xs;
  62. __m256 a2 = xs2;
  63. __m256 a3 = xs3;
  64. __m256 b = ys;
  65. __m256 b2 = ys;
  66. __m256 b3 = ys;
  67. __m256 cx = info.julia ? juliaX : xs;
  68. __m256 cx2 = info.julia ? juliaX : xs2;
  69. __m256 cx3 = info.julia ? juliaX : xs3;
  70. __m256 cy = info.julia ? juliaY : ys;
  71. if (info.smooth) {
  72. __m256 cmp = _mm256_cmp_ps(threshold, threshold, _CMP_LE_OQ);
  73. __m256 cmp2 = _mm256_cmp_ps(threshold, threshold, _CMP_LE_OQ);
  74. __m256 cmp3 = _mm256_cmp_ps(threshold, threshold, _CMP_LE_OQ);
  75. for (int k = 0; k < info.maxIter; k++) {
  76. __m256 bb = _mm256_mul_ps(b, b);
  77. __m256 bb2 = _mm256_mul_ps(b2, b2);
  78. __m256 bb3 = _mm256_mul_ps(b3, b3);
  79. __m256 ab = _mm256_mul_ps(a, b);
  80. __m256 ab2 = _mm256_mul_ps(a2, b2);
  81. __m256 ab3 = _mm256_mul_ps(a3, b3);
  82. a = _mm256_add_ps(_mm256_fmsub_ps(a, a, bb), cx);
  83. a2 = _mm256_add_ps(_mm256_fmsub_ps(a2, a2, bb2), cx2);
  84. a3 = _mm256_add_ps(_mm256_fmsub_ps(a3, a3, bb3), cx3);
  85. b = _mm256_fmadd_ps(two, ab, cy);
  86. b2 = _mm256_fmadd_ps(two, ab2, cy);
  87. b3 = _mm256_fmadd_ps(two, ab3, cy);
  88. /*resultsa = _mm256_or_ps(_mm256_andnot_ps(cmp, resultsa), _mm256_and_ps(cmp, a));
  89. resultsb = _mm256_or_ps(_mm256_andnot_ps(cmp, resultsb), _mm256_and_ps(cmp, b));
  90. resultsa2 = _mm256_or_ps(_mm256_andnot_ps(cmp2, resultsa2), _mm256_and_ps(cmp2, a2));
  91. resultsb2 = _mm256_or_ps(_mm256_andnot_ps(cmp2, resultsb2), _mm256_and_ps(cmp2, b2));
  92. resultsa3 = _mm256_or_ps(_mm256_andnot_ps(cmp3, resultsa3), _mm256_and_ps(cmp3, a3));
  93. resultsb3 = _mm256_or_ps(_mm256_andnot_ps(cmp3, resultsb3), _mm256_and_ps(cmp3, b3));*/
  94. resultsa = _mm256_blendv_ps(resultsa, a, cmp);
  95. resultsb = _mm256_blendv_ps(resultsb, b, cmp);
  96. resultsa2 = _mm256_blendv_ps(resultsa2, a2, cmp2);
  97. resultsb2 = _mm256_blendv_ps(resultsb2, b2, cmp2);
  98. resultsa3 = _mm256_blendv_ps(resultsa3, a3, cmp3);
  99. resultsb3 = _mm256_blendv_ps(resultsb3, b3, cmp3);
  100. cmp = _mm256_cmp_ps(_mm256_fmadd_ps(a, a, bb), threshold, _CMP_LE_OQ);
  101. cmp2 = _mm256_cmp_ps(_mm256_fmadd_ps(a2, a2, bb2), threshold, _CMP_LE_OQ);
  102. cmp3 = _mm256_cmp_ps(_mm256_fmadd_ps(a3, a3, bb3), threshold, _CMP_LE_OQ);
  103. adder = _mm256_and_ps(adder, cmp);
  104. counter = _mm256_add_ps(counter, adder);
  105. adder2 = _mm256_and_ps(adder2, cmp2);
  106. counter2 = _mm256_add_ps(counter2, adder2);
  107. adder3 = _mm256_and_ps(adder3, cmp3);
  108. counter3 = _mm256_add_ps(counter3, adder3);
  109. if ((k & 0x7) == 0 && _mm256_testz_ps(cmp, cmp) != 0 && _mm256_testz_ps(cmp2, cmp2) != 0 && _mm256_testz_ps(cmp3, cmp3) != 0) {
  110. break;
  111. }
  112. }
  113. }
  114. else {
  115. for (int k = 0; k < info.maxIter; k++) {
  116. __m256 bb = _mm256_mul_ps(b, b);
  117. __m256 bb2 = _mm256_mul_ps(b2, b2);
  118. __m256 bb3 = _mm256_mul_ps(b3, b3);
  119. __m256 ab = _mm256_mul_ps(a, b);
  120. __m256 ab2 = _mm256_mul_ps(a2, b2);
  121. __m256 ab3 = _mm256_mul_ps(a3, b3);
  122. __m256 cmp = _mm256_cmp_ps(_mm256_fmadd_ps(a, a, bb), threshold, _CMP_LE_OQ);
  123. __m256 cmp2 = _mm256_cmp_ps(_mm256_fmadd_ps(a2, a2, bb2), threshold, _CMP_LE_OQ);
  124. __m256 cmp3 = _mm256_cmp_ps(_mm256_fmadd_ps(a3, a3, bb3), threshold, _CMP_LE_OQ);
  125. a = _mm256_add_ps(_mm256_fmsub_ps(a, a, bb), cx);
  126. a2 = _mm256_add_ps(_mm256_fmsub_ps(a2, a2, bb2), cx2);
  127. a3 = _mm256_add_ps(_mm256_fmsub_ps(a3, a3, bb3), cx3);
  128. b = _mm256_fmadd_ps(two, ab, cy);
  129. b2 = _mm256_fmadd_ps(two, ab2, cy);
  130. b3 = _mm256_fmadd_ps(two, ab3, cy);
  131. adder = _mm256_and_ps(adder, cmp);
  132. counter = _mm256_add_ps(counter, adder);
  133. adder2 = _mm256_and_ps(adder2, cmp2);
  134. counter2 = _mm256_add_ps(counter2, adder2);
  135. adder3 = _mm256_and_ps(adder3, cmp3);
  136. counter3 = _mm256_add_ps(counter3, adder3);
  137. if ((k & 0x7) == 0 && _mm256_testz_ps(cmp, cmp) != 0 && _mm256_testz_ps(cmp2, cmp2) != 0 && _mm256_testz_ps(cmp3, cmp3) != 0) {
  138. break;
  139. }
  140. }
  141. }
  142. auto alignVec = [](float* data) -> float* {
  143. void* aligned = data;
  144. ::size_t length = 64;
  145. std::align(32, 8 * sizeof(float), aligned, length);
  146. return static_cast<float*>(aligned);
  147. };
  148. float resData[96];
  149. float* ftRes = alignVec(resData);
  150. float* resa = ftRes + 24;
  151. float* resb = resa + 24;
  152. _mm256_store_ps(ftRes, counter);
  153. _mm256_store_ps(ftRes + 8, counter2);
  154. _mm256_store_ps(ftRes + 16, counter3);
  155. _mm256_store_ps(resa, resultsa);
  156. _mm256_store_ps(resa + 8, resultsa2);
  157. _mm256_store_ps(resa + 16, resultsa3);
  158. _mm256_store_ps(resb, resultsb);
  159. _mm256_store_ps(resb + 8, resultsb2);
  160. _mm256_store_ps(resb + 16, resultsb3);
  161. for (int k = 0; k < 24 && i + k < info.bWidth; k++) {
  162. if (info.smooth) {
  163. data[i + k + j * info.bWidth] = ftRes[k] < 0 ? info.maxIter :
  164. ftRes[k] >= info.maxIter ? info.maxIter :
  165. ((float)ftRes[k]) + 1 - ::log(::log(resa[k] * resa[k] + resb[k] * resb[k]) / 2) / ::log(2.0f);
  166. }
  167. else {
  168. data[i + k + j * info.bWidth] = ftRes[k] < 0 ? info.maxIter : ftRes[k];
  169. }
  170. }
  171. }
  172. }
  173. }
  174. template<bool parallel>
  175. void CpuGenerator<double, mnd::X86_AVX_FMA, parallel>::generate(const mnd::MandelInfo& info, float* data)
  176. {
  177. using T = double;
  178. const MandelViewport& view = info.view;
  179. const double dppf = double(view.width / info.bWidth);
  180. const double viewxf = double(view.x);
  181. __m256d viewx = { viewxf, viewxf, viewxf, viewxf };
  182. __m256d dpp = { dppf, dppf, dppf, dppf };
  183. T jX = mnd::convert<T>(info.juliaX);
  184. T jY = mnd::convert<T>(info.juliaY);
  185. __m256d juliaX = { jX, jX, jX, jX };
  186. __m256d juliaY = { jY, jY, jY, jY };
  187. #if defined(_OPENMP)
  188. if constexpr(parallel)
  189. omp_set_num_threads(omp_get_num_procs());
  190. # pragma omp parallel for schedule(static, 1) if (parallel)
  191. #endif
  192. for (long j = 0; j < info.bHeight; j++) {
  193. T y = T(view.y + T(j) * view.height / info.bHeight);
  194. __m256d ys = { y, y, y, y };
  195. for (long i = 0; i < info.bWidth; i += 8) {
  196. __m256d pixc = { double(i), double(i + 1), double(i + 2), double(i + 3) };
  197. __m256d pixc2 = { double(i + 4), double(i + 5), double(i + 6), double(i + 7) };
  198. __m256d xs = _mm256_fmadd_pd(dpp, pixc, viewx);
  199. __m256d xs2 = _mm256_fmadd_pd(dpp, pixc2, viewx);
  200. int itRes[4] = { 0, 0, 0, 0 };
  201. __m256d threshold = { 16.0, 16.0, 16.0, 16.0 };
  202. __m256d counter = { 0, 0, 0, 0 };
  203. __m256d adder = { 1, 1, 1, 1 };
  204. __m256d counter2 = { 0, 0, 0, 0 };
  205. __m256d adder2 = { 1, 1, 1, 1 };
  206. __m256d two = { 2, 2, 2, 2 };
  207. __m256d resultsa = { 0, 0, 0, 0 };
  208. __m256d resultsb = { 0, 0, 0, 0 };
  209. __m256d resultsa2 = { 0, 0, 0, 0 };
  210. __m256d resultsb2 = { 0, 0, 0, 0 };
  211. __m256d a = xs;
  212. __m256d b = ys;
  213. __m256d a2 = xs2;
  214. __m256d b2 = ys;
  215. __m256d cx = info.julia ? juliaX : xs;
  216. __m256d cy = info.julia ? juliaY : ys;
  217. __m256d cx2 = info.julia ? juliaX : xs2;
  218. //__m256d cy2 = info.julia ? juliaY : ys;
  219. __m256d cmp = _mm256_cmp_pd(threshold, threshold, _CMP_LE_OQ);
  220. __m256d cmp2 = _mm256_cmp_pd(threshold, threshold, _CMP_LE_OQ);
  221. for (int k = 0; k < info.maxIter; k++) {
  222. __m256d ab = _mm256_mul_pd(a, b);
  223. __m256d bb = _mm256_mul_pd(b, b);
  224. __m256d ab2 = _mm256_mul_pd(a2, b2);
  225. __m256d bb2 = _mm256_mul_pd(b2, b2);
  226. a = _mm256_fmsub_pd(a, a, bb);
  227. a = _mm256_add_pd(a, cx);
  228. a2 = _mm256_fmsub_pd(a2, a2, bb2);
  229. a2 = _mm256_add_pd(a2, cx2);
  230. b = _mm256_fmadd_pd(two, ab, cy);
  231. b2 = _mm256_fmadd_pd(two, ab2, cy);
  232. if (info.smooth) {
  233. resultsa = _mm256_blendv_pd(resultsa, a, cmp);
  234. resultsb = _mm256_blendv_pd(resultsb, b, cmp);
  235. resultsa2 = _mm256_blendv_pd(resultsa2, a2, cmp2);
  236. resultsb2 = _mm256_blendv_pd(resultsb2, b2, cmp2);
  237. }
  238. cmp = _mm256_cmp_pd(_mm256_fmadd_pd(a, a, bb), threshold, _CMP_LE_OQ);
  239. cmp2 = _mm256_cmp_pd(_mm256_fmadd_pd(a2, a2, bb2), threshold, _CMP_LE_OQ);
  240. adder = _mm256_and_pd(adder, cmp);
  241. adder2 = _mm256_and_pd(adder2, cmp2);
  242. counter = _mm256_add_pd(counter, adder);
  243. counter2 = _mm256_add_pd(counter2, adder2);
  244. if ((k & 0x7) == 0 && _mm256_testz_si256(_mm256_castpd_si256(cmp), _mm256_castpd_si256(cmp)) != 0 &&
  245. _mm256_testz_si256(_mm256_castpd_si256(cmp2), _mm256_castpd_si256(cmp2)) != 0) {
  246. break;
  247. }
  248. }
  249. auto alignVec = [](double* data) -> double* {
  250. void* aligned = data;
  251. ::size_t length = 64;
  252. std::align(32, 4 * sizeof(double), aligned, length);
  253. return static_cast<double*>(aligned);
  254. };
  255. double resData[8];
  256. double* ftRes = alignVec(resData);
  257. double* resa = (double*) &resultsa;
  258. double* resb = (double*) &resultsb;
  259. _mm256_store_pd(ftRes, counter);
  260. for (int k = 0; k < 4 && i + k < info.bWidth; k++) {
  261. if (info.smooth)
  262. data[i + k + j * info.bWidth] = ftRes[k] < 0 ? info.maxIter :
  263. ftRes[k] >= info.maxIter ? info.maxIter :
  264. ((float)ftRes[k]) + 1 - ::log(::log(resa[k] * resa[k] + resb[k] * resb[k]) / 2) / ::log(2.0f);
  265. else
  266. data[i + k + j * info.bWidth] = ftRes[k] < 0 ? info.maxIter : float(ftRes[k]);
  267. }
  268. resa = (double*) &resultsa2;
  269. resb = (double*) &resultsb2;
  270. _mm256_store_pd(ftRes, counter2);
  271. i += 4;
  272. for (int k = 0; k < 4 && i + k < info.bWidth; k++) {
  273. if (info.smooth)
  274. data[i + k + j * info.bWidth] = ftRes[k] < 0 ? info.maxIter :
  275. ftRes[k] >= info.maxIter ? info.maxIter :
  276. ((float)ftRes[k]) + 1 - ::log(::log(resa[k] * resa[k] + resb[k] * resb[k]) / 2) / ::log(2.0f);
  277. else
  278. data[i + k + j * info.bWidth] = ftRes[k] < 0 ? info.maxIter : float(ftRes[k]);
  279. }
  280. i -= 4;
  281. }
  282. }
  283. }
  284. struct VecPair
  285. {
  286. __m256d a;
  287. __m256d b;
  288. };
  289. struct VecTriple
  290. {
  291. __m256d a;
  292. __m256d b;
  293. __m256d c;
  294. };
  295. struct VecQuadruple
  296. {
  297. __m256d a;
  298. __m256d b;
  299. __m256d c;
  300. __m256d d;
  301. };
  302. static inline VecPair quickTwoSum(__m256d a, __m256d b)
  303. {
  304. __m256d s = _mm256_add_pd(a, b);
  305. __m256d e = _mm256_sub_pd(b, _mm256_sub_pd(s, a));
  306. return { s, e };
  307. }
  308. static inline VecPair quickTwoDiff(__m256d a, __m256d b)
  309. {
  310. __m256d s = _mm256_sub_pd(a, b);
  311. __m256d e = _mm256_sub_pd(_mm256_sub_pd(a, s), b);
  312. return { s, e };
  313. }
  314. static inline VecPair twoSum(__m256d a, __m256d b)
  315. {
  316. __m256d s = _mm256_add_pd(a, b);
  317. __m256d bb = _mm256_sub_pd(s, a);
  318. __m256d e = _mm256_add_pd(_mm256_sub_pd(a, _mm256_sub_pd(s, bb)), _mm256_sub_pd(b, bb));
  319. return { s, e };
  320. }
  321. static inline VecPair twoDiff(__m256d a, __m256d b)
  322. {
  323. __m256d s = _mm256_sub_pd(a, b);
  324. __m256d bb = _mm256_sub_pd(s, a);
  325. __m256d e = _mm256_sub_pd(_mm256_sub_pd(a, _mm256_sub_pd(s, bb)), _mm256_add_pd(b, bb));
  326. return { s, e };
  327. }
  328. static inline VecTriple threeSum(__m256d a, __m256d b, __m256d c)
  329. {
  330. auto [s, e] = twoSum(a, b);
  331. auto [r0, e2] = twoSum(s, c);
  332. auto [r1, r2] = twoSum(e, e2);
  333. return { r0, r1, r2 };
  334. }
  335. static inline VecPair threeTwoSum(__m256d a, __m256d b, __m256d c)
  336. {
  337. auto[t, e1] = twoSum(a, b);
  338. auto[s, e2] = twoSum(t, c);
  339. return { s, _mm256_add_pd(e1, e2) };
  340. }
  341. static inline __m256d threeOneSum(__m256d a, __m256d b, __m256d c)
  342. {
  343. return _mm256_add_pd(a, _mm256_add_pd(b, c));
  344. }
  345. static inline VecTriple sixThreeSum(__m256d a, __m256d b, __m256d c,
  346. __m256d d, __m256d e, __m256d f)
  347. {
  348. auto[x0, x1, x2] = threeSum(a, b, c);
  349. auto[y0, y1, y2] = threeSum(d, e, f);
  350. auto[r0, t0] = twoSum(x0, y0);
  351. auto[t1, t2] = twoSum(x1, y1);
  352. auto[r1, t3] = twoSum(t0, t1);
  353. auto t4 = _mm256_add_pd(x2, y2);
  354. auto r2 = threeOneSum(t2, t3, t4);
  355. return { r0, r1, r2 };
  356. }
  357. static inline VecPair addDD(const VecPair& a, const VecPair& b)
  358. {
  359. auto[s, e] = twoSum(a.a, b.a);
  360. e = _mm256_add_pd(e, _mm256_add_pd(a.b, b.b));
  361. auto[r1, r2] = quickTwoSum(s, e);
  362. return { r1, r2 };
  363. }
  364. static inline VecPair nineTwoSum(__m256d a, __m256d b, __m256d c,
  365. __m256d d, __m256d e, __m256d f,
  366. __m256d g, __m256d h, __m256d i)
  367. {
  368. auto[x1, x2] = twoSum(a, d);
  369. auto[y1, y2] = twoSum(b, c);
  370. auto[z1, z2] = twoSum(e, h);
  371. auto[u1, u2] = twoSum(f, g);
  372. auto[t1, t2] = addDD({ x1, x2 }, { y1, y2 });
  373. auto[t3, t4] = addDD({ z1, z2 }, { u1, u2 });
  374. auto[t5, t6] = addDD({ t1, t2 }, { t3, t4 });
  375. return threeTwoSum(t5, t6, i);
  376. }
  377. static inline VecQuadruple renormalize(__m256d x0, __m256d x1, __m256d x2, __m256d x3, __m256d x4)
  378. {
  379. auto [st1, t4] = quickTwoSum(x3, x4);
  380. auto [st2, t3] = quickTwoSum(x2, st1);
  381. auto [st3, t2] = quickTwoSum(x1, st2);
  382. auto [t0, t1] = quickTwoSum(x0, st3);
  383. __m256d s = t0;
  384. __m256d e;
  385. __m256d t[] = { t1, t2, t3, t4 };
  386. __m256d b[4] = { 0, 0, 0, 0 };
  387. int k = 0;
  388. for (int i = 0; i < 4; i++) {
  389. auto[st, et] = quickTwoSum(s, t[i]);
  390. s = st; e = et;
  391. b[k] = s;
  392. //if (e != 0) {
  393. b[k] = s;
  394. s = e;
  395. k = k + 1;
  396. //}
  397. }
  398. return { b[0], b[1], b[2], b[3] };
  399. }
  400. static inline VecPair twoProd(__m256d a, __m256d b)
  401. {
  402. __m256d p = _mm256_mul_pd(a, b);
  403. __m256d e = _mm256_fmsub_pd(a, b, p);
  404. return { p, e };
  405. }
  406. struct AvxDoubleDouble
  407. {
  408. __m256d x[2];
  409. inline AvxDoubleDouble(__m256d a, __m256d b) :
  410. x{ a, b }
  411. {}
  412. inline AvxDoubleDouble(double a, double b) :
  413. x{ _mm256_set1_pd(a), _mm256_set1_pd(b) }
  414. {}
  415. inline AvxDoubleDouble operator + (const AvxDoubleDouble& sm) const
  416. {
  417. auto[s, e] = twoSum(x[0], sm.x[0]);
  418. e = _mm256_add_pd(e, _mm256_add_pd(x[1], sm.x[1]));
  419. auto[r1, r2] = quickTwoSum(s, e);
  420. return AvxDoubleDouble{ r1, r2 };
  421. }
  422. inline AvxDoubleDouble operator - (const AvxDoubleDouble& sm) const
  423. {
  424. auto[s, e] = twoDiff(x[0], sm.x[0]);
  425. e = _mm256_add_pd(e, x[1]);
  426. e = _mm256_sub_pd(e, sm.x[1]);
  427. auto[r1, r2] = quickTwoSum(s, e);
  428. return AvxDoubleDouble{ r1, r2 };
  429. }
  430. inline AvxDoubleDouble operator * (const AvxDoubleDouble& sm) const
  431. {
  432. auto[p1, p2] = twoProd(this->x[0], sm.x[0]);
  433. p2 = _mm256_add_pd(p2,
  434. _mm256_add_pd(_mm256_mul_pd(sm.x[1], x[0]), _mm256_mul_pd(sm.x[0], x[1])) );
  435. auto[r1, r2] = quickTwoSum(p1, p2);
  436. return AvxDoubleDouble{ r1, r2 };
  437. }
  438. };
  439. template<bool parallel>
  440. void CpuGenerator<mnd::DoubleDouble, mnd::X86_AVX_FMA, parallel>::generate(const mnd::MandelInfo& info, float* data)
  441. {
  442. const MandelViewport& view = info.view;
  443. using T = LightDoubleDouble;
  444. T viewx = mnd::convert<T>(view.x);
  445. T viewy = mnd::convert<T>(view.y);
  446. T wpp = mnd::convert<T>(view.width / info.bWidth);
  447. T hpp = mnd::convert<T>(view.height / info.bHeight);
  448. T jX = mnd::convert<T>(info.juliaX);
  449. T jY = mnd::convert<T>(info.juliaY);
  450. AvxDoubleDouble juliaX = { jX[0], jX[1] };
  451. AvxDoubleDouble juliaY = { jY[0], jY[1] };
  452. #if defined(_OPENMP)
  453. if constexpr(parallel)
  454. omp_set_num_threads(omp_get_num_procs());
  455. # pragma omp parallel for schedule(static, 1) if (parallel)
  456. #endif
  457. for (long j = 0; j < info.bHeight; j++) {
  458. T y = viewy + T(double(j)) * hpp;
  459. __m256d y0s = { y.x[0], y.x[0], y.x[0], y.x[0] };
  460. __m256d y1s = { y.x[1], y.x[1], y.x[1], y.x[1] };
  461. AvxDoubleDouble ys{ y0s, y1s };
  462. for (long i = 0; i < info.bWidth; i += 4) {
  463. T x1 = viewx + T(double(i)) * wpp;
  464. T x2 = x1 + wpp;
  465. T x3 = x2 + wpp;
  466. T x4 = x3 + wpp;
  467. __m256d x0s = {
  468. x1[0], x2[0], x3[0], x4[0],
  469. };
  470. __m256d x1s = {
  471. x1[1], x2[1], x3[1], x4[1],
  472. };
  473. AvxDoubleDouble xs{ x0s, x1s };
  474. AvxDoubleDouble cx = info.julia ? juliaX : xs;
  475. AvxDoubleDouble cy = info.julia ? juliaY : ys;
  476. int itRes[4] = { 0, 0, 0, 0 };
  477. __m256d threshold = { 16.0, 16.0, 16.0, 16.0 };
  478. __m256d counter = { 0, 0, 0, 0 };
  479. __m256d adder = { 1, 1, 1, 1 };
  480. AvxDoubleDouble a = xs;
  481. AvxDoubleDouble b = ys;
  482. __m256d resultsa;
  483. __m256d resultsb;
  484. __m256d cmp = _mm256_cmp_pd(threshold, threshold, _CMP_LE_OQ);
  485. for (int k = 0; k < info.maxIter; k++) {
  486. AvxDoubleDouble aa = a * a;
  487. AvxDoubleDouble bb = b * b;
  488. AvxDoubleDouble abab = a * b; abab = abab + abab;
  489. a = aa - bb + cx;
  490. b = abab + cy;
  491. if (info.smooth) {
  492. resultsa = _mm256_blendv_pd(resultsa, a.x[0], cmp);
  493. resultsb = _mm256_blendv_pd(resultsb, b.x[0], cmp);
  494. }
  495. cmp = _mm256_cmp_pd(_mm256_add_pd(aa.x[0], bb.x[0]), threshold, _CMP_LE_OQ);
  496. adder = _mm256_and_pd(adder, cmp);
  497. counter = _mm256_add_pd(counter, adder);
  498. if (_mm256_testz_si256(_mm256_castpd_si256(cmp), _mm256_castpd_si256(cmp)) != 0) {
  499. break;
  500. }
  501. }
  502. auto alignVec = [](double* data) -> double* {
  503. void* aligned = data;
  504. ::size_t length = 64;
  505. std::align(32, 4 * sizeof(double), aligned, length);
  506. return static_cast<double*>(aligned);
  507. };
  508. double resData[8];
  509. double* ftRes = alignVec(resData);
  510. double* resa = (double*) &resultsa;
  511. double* resb = (double*) &resultsb;
  512. _mm256_store_pd(ftRes, counter);
  513. for (int k = 0; k < 4 && i + k < info.bWidth; k++) {
  514. if (info.smooth)
  515. data[i + k + j * info.bWidth] = ftRes[k] < 0 ? info.maxIter :
  516. ftRes[k] >= info.maxIter ? info.maxIter :
  517. ((float)ftRes[k]) + 1 - ::log(::log(resa[k] * resa[k] + resb[k] * resb[k]) / 2) / ::log(2.0f);
  518. else
  519. data[i + k + j * info.bWidth] = ftRes[k] >= 0 ? float(ftRes[k]) : info.maxIter;
  520. }
  521. }
  522. }
  523. }
  524. struct AvxQuadDouble
  525. {
  526. __m256d x[4];
  527. inline AvxQuadDouble(__m256d a, __m256d b, __m256d c, __m256d d) :
  528. x{ a, b, c, d}
  529. {}
  530. inline AvxQuadDouble(double a, double b, double c, double d) :
  531. x{ _mm256_set1_pd(a), _mm256_set1_pd(b), _mm256_set1_pd(c), _mm256_set1_pd(d) }
  532. {}
  533. inline AvxQuadDouble operator + (const AvxQuadDouble& sm) const
  534. {
  535. auto[s0, e0] = twoSum(x[0], sm.x[0]);
  536. auto[s1, e1] = twoSum(x[1], sm.x[1]);
  537. auto[s2, e2] = twoSum(x[2], sm.x[2]);
  538. auto[s3, e3] = twoSum(x[3], sm.x[3]);
  539. __m256d r0 = s0;
  540. auto [r1, t0] = twoSum(s1, e0);
  541. auto [r2, t1, t2] = threeSum(s2, e1, t0);
  542. auto [r3, t3, _t4] = threeSum(s3, e2, t1);
  543. auto [r4, _t5, _t6] = threeSum(e3, t3, t2);
  544. auto [re0, re1, re2, re3] = renormalize(r0, r1, r2, r3, r4);
  545. return { re0, re1, re2, re3 };
  546. }
  547. inline AvxQuadDouble operator - (const AvxQuadDouble& sm) const
  548. {
  549. auto[s0, e0] = twoDiff(x[0], sm.x[0]);
  550. auto[s1, e1] = twoDiff(x[1], sm.x[1]);
  551. auto[s2, e2] = twoDiff(x[2], sm.x[2]);
  552. auto[s3, e3] = twoDiff(x[3], sm.x[3]);
  553. __m256d r0 = s0;
  554. auto [r1, t0] = twoSum(s1, e0);
  555. auto [r2, t1, t2] = threeSum(s2, e1, t0);
  556. auto [r3, t3, _t4] = threeSum(s3, e2, t1);
  557. auto [r4, _t5, _t6] = threeSum(e3, t3, t2);
  558. auto [re0, re1, re2, re3] = renormalize(r0, r1, r2, r3, r4);
  559. return { re0, re1, re2, re3 };
  560. }
  561. inline AvxQuadDouble operator * (const AvxQuadDouble& sm) const
  562. {
  563. auto[a0, b0] = twoProd(x[0], sm.x[0]);
  564. auto[b1, c0] = twoProd(x[0], sm.x[1]);
  565. auto[b2, c1] = twoProd(x[1], sm.x[0]);
  566. auto[c2, d0] = twoProd(x[0], sm.x[2]);
  567. auto[c3, d1] = twoProd(x[1], sm.x[1]);
  568. auto[c4, d2] = twoProd(x[2], sm.x[0]);
  569. auto d5 = _mm256_mul_pd(x[3], sm.x[0]);
  570. auto d6 = _mm256_mul_pd(x[2], sm.x[1]);
  571. auto d7 = _mm256_mul_pd(x[1], sm.x[2]);
  572. auto d8 = _mm256_mul_pd(x[0], sm.x[3]);
  573. auto r0 = a0;
  574. auto[r1, c5, d3] = threeSum(b0, b1, b2);
  575. auto[r2, d4, e0] = sixThreeSum(c0, c1, c2, c3, c4, c5);
  576. auto[r3, e1] = nineTwoSum(d0, d1, d2, d3, d4, d5, d6, d7, d8);
  577. auto r4 = _mm256_add_pd(e0, e1);
  578. auto [n0, n1, n2, n3] = renormalize(r0, r1, r2, r3, r4);
  579. return { n0, n1, n2, n3 };
  580. }
  581. };
  582. template<bool parallel>
  583. void CpuGenerator<mnd::QuadDouble, mnd::X86_AVX_FMA, parallel>::generate(const mnd::MandelInfo& info, float* data)
  584. {
  585. const MandelViewport& view = info.view;
  586. using T = mnd::Float256;
  587. T viewx = mnd::convert<T>(view.x);
  588. T viewy = mnd::convert<T>(view.y);
  589. T wpp = mnd::convert<T>(view.width / info.bWidth);
  590. T hpp = mnd::convert<T>(view.height / info.bHeight);
  591. T jX = mnd::convert<T>(info.juliaX);
  592. T jY = mnd::convert<T>(info.juliaY);
  593. auto toQd = [] (const mnd::Float256& x) -> std::tuple<double, double, double, double> {
  594. double a = double(x);
  595. mnd::Float256 rem = x - a;
  596. double b = double(rem);
  597. rem = rem - b;
  598. double c = double(rem);
  599. rem = rem - c;
  600. double d = double(rem);
  601. return { a, b, c, d };
  602. };
  603. auto toAvxQuadDouble = [&toQd] (const mnd::Float256& x) -> AvxQuadDouble {
  604. auto [a, b, c, d] = toQd(x);
  605. return AvxQuadDouble{ a, b, c, d };
  606. };
  607. auto toAvxQuadDouble4 = [&toQd] (const mnd::Float256& a, const mnd::Float256& b,
  608. const mnd::Float256& c, const mnd::Float256& d) -> AvxQuadDouble {
  609. auto [x0, y0, z0, u0] = toQd(a);
  610. auto [x1, y1, z1, u1] = toQd(b);
  611. auto [x2, y2, z2, u2] = toQd(c);
  612. auto [x3, y3, z3, u3] = toQd(d);
  613. __m256d xs = { x0, x1, x2, x3 };
  614. __m256d ys = { y0, y1, y2, y3 };
  615. __m256d zs = { z0, z1, z2, z3 };
  616. __m256d us = { u0, u1, u2, u3 };
  617. return AvxQuadDouble{ xs, ys, zs, us };
  618. };
  619. AvxQuadDouble juliaX = toAvxQuadDouble(jX);
  620. AvxQuadDouble juliaY = toAvxQuadDouble(jY);
  621. #if defined(_OPENMP)
  622. if constexpr(parallel)
  623. omp_set_num_threads(omp_get_num_procs());
  624. # pragma omp parallel for schedule(static, 1) if (parallel)
  625. #endif
  626. for (long j = 0; j < info.bHeight; j++) {
  627. T y = viewy + T(double(j)) * hpp;
  628. AvxQuadDouble ys = toAvxQuadDouble(y);
  629. for (long i = 0; i < info.bWidth; i += 4) {
  630. T x1 = viewx + T(double(i)) * wpp;
  631. T x2 = x1 + wpp;
  632. T x3 = x2 + wpp;
  633. T x4 = x3 + wpp;
  634. AvxQuadDouble xs = toAvxQuadDouble4(x1, x2, x3, x4);
  635. AvxQuadDouble cx = info.julia ? juliaX : xs;
  636. AvxQuadDouble cy = info.julia ? juliaY : ys;
  637. int itRes[4] = { 0, 0, 0, 0 };
  638. __m256d threshold = { 16.0, 16.0, 16.0, 16.0 };
  639. __m256d counter = { 0, 0, 0, 0 };
  640. __m256d adder = { 1, 1, 1, 1 };
  641. AvxQuadDouble a = xs;
  642. AvxQuadDouble b = ys;
  643. __m256d resultsa;
  644. __m256d resultsb;
  645. __m256d cmp = _mm256_cmp_pd(threshold, threshold, _CMP_LE_OQ);
  646. for (int k = 0; k < info.maxIter; k++) {
  647. AvxQuadDouble aa = a * a;
  648. AvxQuadDouble bb = b * b;
  649. AvxQuadDouble abab = a * b; abab = abab + abab;
  650. a = aa - bb + cx;
  651. b = abab + cy;
  652. if (info.smooth) {
  653. resultsa = _mm256_blendv_pd(resultsa, a.x[0], cmp);
  654. resultsb = _mm256_blendv_pd(resultsb, b.x[0], cmp);
  655. }
  656. cmp = _mm256_cmp_pd(_mm256_add_pd(aa.x[0], bb.x[0]), threshold, _CMP_LE_OQ);
  657. adder = _mm256_and_pd(adder, cmp);
  658. counter = _mm256_add_pd(counter, adder);
  659. if (_mm256_testz_si256(_mm256_castpd_si256(cmp), _mm256_castpd_si256(cmp)) != 0) {
  660. break;
  661. }
  662. }
  663. auto alignVec = [](double* data) -> double* {
  664. void* aligned = data;
  665. ::size_t length = 64;
  666. std::align(32, 4 * sizeof(double), aligned, length);
  667. return static_cast<double*>(aligned);
  668. };
  669. double resData[8];
  670. double* ftRes = alignVec(resData);
  671. double* resa = (double*) &resultsa;
  672. double* resb = (double*) &resultsb;
  673. _mm256_store_pd(ftRes, counter);
  674. for (int k = 0; k < 4 && i + k < info.bWidth; k++) {
  675. if (info.smooth)
  676. data[i + k + j * info.bWidth] = ftRes[k] < 0 ? info.maxIter :
  677. ftRes[k] >= info.maxIter ? info.maxIter :
  678. ((float)ftRes[k]) + 1 - ::log(::log(resa[k] * resa[k] + resb[k] * resb[k]) / 2) / ::log(2.0f);
  679. else
  680. data[i + k + j * info.bWidth] = ftRes[k] >= 0 ? float(ftRes[k]) : info.maxIter;
  681. }
  682. }
  683. }
  684. }