CpuGenerators.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "CpuGenerators.h"
  2. #include "Types.h"
  3. #ifdef WITH_MPFR
  4. #include "MpfrWrapper.h"
  5. #endif // WITH_MPFR
  6. #include <omp.h>
  7. #include <cmath>
  8. #include <memory>
  9. using mnd::CpuGenerator;
  10. namespace mnd
  11. {
  12. template class CpuGenerator<float, mnd::NONE, false>;
  13. template class CpuGenerator<float, mnd::NONE, true>;
  14. template class CpuGenerator<double, mnd::NONE, false>;
  15. template class CpuGenerator<double, mnd::NONE, true>;
  16. template class CpuGenerator<Fixed64, mnd::NONE, false>;
  17. template class CpuGenerator<Fixed64, mnd::NONE, true>;
  18. template class CpuGenerator<Fixed128, mnd::NONE, false>;
  19. template class CpuGenerator<Fixed128, mnd::NONE, true>;
  20. //template class CpuGenerator<Fixed128, mnd::NONE, false>;
  21. //template class CpuGenerator<Fixed128, mnd::NONE, false, true>;
  22. //template class CpuGenerator<Fixed128, mnd::NONE, true>;
  23. //template class CpuGenerator<Fixed128, mnd::NONE, true, true>;
  24. #ifdef WITH_BOOST
  25. #include <boost/multiprecision/cpp_bin_float.hpp>
  26. template class CpuGenerator<mnd::Float128, mnd::NONE, false>;
  27. template class CpuGenerator<mnd::Float128, mnd::NONE, true>;
  28. template class CpuGenerator<mnd::Float256, mnd::NONE, false>;
  29. template class CpuGenerator<mnd::Float256, mnd::NONE, true>;
  30. template class CpuGenerator<Fixed512, mnd::NONE, false>;
  31. template class CpuGenerator<Fixed512, mnd::NONE, true>;
  32. #endif
  33. #ifdef WITH_QD
  34. template class CpuGenerator<mnd::DoubleDouble, mnd::NONE, false>;
  35. template class CpuGenerator<mnd::DoubleDouble, mnd::NONE, true>;
  36. template class CpuGenerator<mnd::QuadDouble, mnd::NONE, false>;
  37. template class CpuGenerator<mnd::QuadDouble, mnd::NONE, true>;
  38. #endif
  39. }
  40. template<typename T, bool parallel>
  41. void CpuGenerator<T, mnd::NONE, parallel>::generate(const mnd::MandelInfo& info, float* data)
  42. {
  43. const MandelViewport& view = info.view;
  44. T viewx = mnd::convert<T>(view.x);
  45. T viewy = mnd::convert<T>(view.y);
  46. T wpp = mnd::convert<T>(view.width / info.bWidth);
  47. T hpp = mnd::convert<T>(view.height / info.bHeight);
  48. T juliaX = mnd::convert<T>(info.juliaX);
  49. T juliaY = mnd::convert<T>(info.juliaY);
  50. #if defined(_OPENMP)
  51. if constexpr (parallel)
  52. omp_set_num_threads(omp_get_num_procs());
  53. # pragma omp parallel for schedule(static, 1) if (parallel)
  54. #endif
  55. for (long j = 0; j < info.bHeight; j++) {
  56. T y = viewy + T(double(j)) * hpp;
  57. for (long i = 0; i < info.bWidth; i++) {
  58. T x = viewx + T(double(i)) * wpp;
  59. T a = x;
  60. T b = y;
  61. T cx = info.julia ? juliaX : x;
  62. T cy = info.julia ? juliaY : y;
  63. int k = 0;
  64. for (k = 0; k < info.maxIter; k++) {
  65. T aa = a * a;
  66. T bb = b * b;
  67. T ab = a * b;
  68. a = aa - bb + cx;
  69. b = ab + ab + cy;
  70. if (aa + bb > T(16.0)) {
  71. break;
  72. }
  73. }
  74. if (info.smooth) {
  75. if (k >= info.maxIter)
  76. data[i + j * info.bWidth] = float(info.maxIter);
  77. else {
  78. float aapp = mnd::convert<float>(a);
  79. float bapp = mnd::convert<float>(b);
  80. data[i + j * info.bWidth] = ((float) k) + 1 - ::logf(::logf(aapp * aapp + bapp * bapp) / 2) / ::logf(2.0f);
  81. }
  82. }
  83. else
  84. data[i + j * info.bWidth] = float(k);
  85. }
  86. }
  87. }
  88. /*
  89. template<bool parallel>
  90. void CpuGenerator<double, mnd::NONE, parallel>::generate(const mnd::MandelInfo& info, float* data)
  91. {
  92. const MandelViewport& view = info.view;
  93. T viewx = mnd::convert<T>(view.x);
  94. T viewy = mnd::convert<T>(view.y);
  95. T wpp = mnd::convert<T>(view.width / info.bWidth);
  96. T hpp = mnd::convert<T>(view.height / info.bHeight);
  97. if constexpr (parallel)
  98. omp_set_num_threads(omp_get_num_procs());
  99. #pragma omp parallel for schedule(static, 1) if (parallel)
  100. for (long j = 0; j < info.bHeight; j++) {
  101. T y = viewy + T(double(j)) * hpp;
  102. long i = 0;
  103. for (i; i < info.bWidth; i++) {
  104. T x = viewx + T(double(i)) * wpp;
  105. T a = x;
  106. T b = y;
  107. int k = 0;
  108. for (k = 0; k < info.maxIter; k++) {
  109. T aa = a * a;
  110. T bb = b * b;
  111. T ab = a * b;
  112. a = aa - bb + x;
  113. b = ab + ab + y;
  114. if (aa + bb > T(16.0)) {
  115. break;
  116. }
  117. }
  118. if (info.smooth) {
  119. if (k >= info.maxIter)
  120. data[i + j * info.bWidth] = float(info.maxIter);
  121. else
  122. data[i + j * info.bWidth] = ((float) k) + 1 - ::logf(::logf(mnd::convert<float>(a * a + b * b)) / 2) / ::logf(2.0f);
  123. }
  124. else
  125. data[i + j * info.bWidth] = k;
  126. }
  127. }
  128. }*/
  129. /*
  130. #if defined(WITH_BOOST) || 1
  131. template<bool parallel>
  132. void CpuGenerator<Fixed128, mnd::NONE, parallel>::generate(const mnd::MandelInfo& info, float* data)
  133. {
  134. using T = Fixed128;
  135. const MandelViewport& view = info.view;
  136. const auto fixedFromFloat = [] (const mnd::Float128& f) {
  137. boost::multiprecision::int128_t frac = boost::multiprecision::int128_t(f * 4294967296.0 * 4294967296.0 * 4294967296.0);
  138. std::vector<uint32_t> bits;
  139. export_bits(frac, std::back_inserter(bits), 32);
  140. bits.clear();
  141. while (bits.size() < 4) bits.push_back(0);
  142. return Fixed128{ bits[0], bits[1], bits[2], bits[3] };
  143. };
  144. if constexpr (parallel)
  145. omp_set_num_threads(2 * omp_get_num_procs());
  146. #pragma omp parallel for if (parallel)
  147. for (long j = 0; j < info.bHeight; j++) {
  148. T y = fixedFromFloat(view.y + mnd::Real(j) * view.height / info.bHeight);
  149. long i = 0;
  150. for (i; i < info.bWidth; i++) {
  151. T x = fixedFromFloat(view.x + mnd::Real(i) * view.width / info.bWidth);
  152. T a = x;
  153. T b = y;
  154. int k = 0;
  155. for (k = 0; k < info.maxIter; k++) {
  156. T aa = a * a;
  157. T bb = b * b;
  158. T ab = a * b;
  159. a = aa - bb + x;
  160. b = ab + ab + y;
  161. if (aa + bb > T(16)) {
  162. break;
  163. }
  164. }
  165. if constexpr (smooth) {
  166. if (k >= info.maxIter)
  167. data[i + j * info.bWidth] = info.maxIter;
  168. else
  169. data[i + j * info.bWidth] = ((float) k) + 1 - ::logf(::logf(float(a * a + b * b)) / 2) / ::logf(2.0f);
  170. }
  171. else
  172. data[i + j * info.bWidth] = k;
  173. }
  174. }
  175. }
  176. #endif // WITH_BOOST
  177. */
  178. #ifdef WITH_MPFR
  179. template<unsigned int bits, bool parallel>
  180. void CpuGenerator<mnd::MpfrFloat<bits>, mnd::NONE, parallel>::generate(const mnd::MandelInfo& info, float* data)
  181. {
  182. const MandelViewport& view = info.view;
  183. using T = mnd::MpfrFloat<bits>;
  184. #if defined(_OPENMP)
  185. if constexpr (parallel)
  186. omp_set_num_threads(2 * omp_get_num_procs());
  187. # pragma omp parallel for if (parallel)
  188. #endif
  189. for (long j = 0; j < info.bHeight; j++) {
  190. T y = T(view.y) + T(j) * T(view.height / info.bHeight);
  191. long i = 0;
  192. for (i; i < info.bWidth; i++) {
  193. T x = T(view.x + T(i) * T(view.width / info.bWidth));
  194. T a = x;
  195. T b = y;
  196. int k = 0;
  197. for (k = 0; k < info.maxIter; k++) {
  198. T aa = a * a;
  199. T bb = b * b;
  200. T ab = a * b;
  201. a = aa - bb + x;
  202. b = ab + ab + y;
  203. if (aa + bb > T(16)) {
  204. break;
  205. }
  206. }
  207. if (info.smooth) {
  208. if (k >= info.maxIter)
  209. data[i + j * info.bWidth] = info.maxIter;
  210. else
  211. data[i + j * info.bWidth] = ((float) k) + 1 - ::log(::log(a * a + b * b) / 2) / ::log(2.0f);
  212. }
  213. else
  214. data[i + j * info.bWidth] = k;
  215. }
  216. }
  217. }
  218. #endif // WITH_MPFR
  219. /*
  220. void CpuGeneratorDouble::generate(const mnd::MandelInfo& info, float* data)
  221. {
  222. const MandelViewport& view = info.view;
  223. omp_set_num_threads(2 * omp_get_num_procs());
  224. #pragma omp parallel for
  225. for (long j = 0; j < info.bHeight; j++) {
  226. double y = double(view.y) + double(j) * double(view.height / info.bHeight);
  227. long i = 0;
  228. for (i; i < info.bWidth; i++) {
  229. double x = view.x + double(i) * view.width / info.bWidth;
  230. double a = x;
  231. double b = y;
  232. int k = 0;
  233. for (k = 0; k < info.maxIter; k++) {
  234. double aa = a * a;
  235. double bb = b * b;
  236. double ab = a * b;
  237. a = aa - bb + x;
  238. b = ab + ab + y;
  239. if (aa + bb > 16) {
  240. break;
  241. }
  242. }
  243. data[i + j * info.bWidth] = k;
  244. }
  245. }
  246. }
  247. void CpuGenerator128::generate(const mnd::MandelInfo& info, float* data)
  248. {
  249. const MandelViewport& view = info.view;
  250. omp_set_num_threads(2 * omp_get_num_procs());
  251. #pragma omp parallel for
  252. for (long j = 0; j < info.bHeight; j++) {
  253. Fixed128 y = Fixed128(view.y) + Fixed128(j) * Fixed128(view.height / info.bHeight);
  254. long i = 0;
  255. for (i; i < info.bWidth; i++) {
  256. Fixed128 x = view.x + Fixed128(i) * Fixed128(view.width / info.bWidth);
  257. Fixed128 a = x;
  258. Fixed128 b = y;
  259. int k = 0;
  260. for (k = 0; k < info.maxIter; k++) {
  261. Fixed128 aa = a * a;
  262. Fixed128 bb = b * b;
  263. Fixed128 ab = a * b;
  264. a = aa - bb + x;
  265. b = ab + ab + y;
  266. if (aa + bb > Fixed128(16)) {
  267. break;
  268. }
  269. }
  270. data[i + j * info.bWidth] = k;
  271. }
  272. }
  273. }
  274. */