1
0

CpuGenerators.cpp 9.8 KB

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