mandel.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Copyright (C) 2010-2015 Matthias Kretz <kretz@kde.org>
  3. Permission to use, copy, modify, and distribute this software
  4. and its documentation for any purpose and without fee is hereby
  5. granted, provided that the above copyright notice appear in all
  6. copies and that both that the copyright notice and this
  7. permission notice and warranty disclaimer appear in supporting
  8. documentation, and that the name of the author not be used in
  9. advertising or publicity pertaining to distribution of the
  10. software without specific, written prior permission.
  11. The author disclaim all warranties with regard to this
  12. software, including all implied warranties of merchantability
  13. and fitness. In no event shall the author be liable for any
  14. special, indirect or consequential damages or any damages
  15. whatsoever resulting from loss of use, data or profits, whether
  16. in an action of contract, negligence or other tortious action,
  17. arising out of or in connection with the use or performance of
  18. this software.
  19. */
  20. #include "mandel.h"
  21. #include <QMutexLocker>
  22. #include <QtCore/QtDebug>
  23. #include "../tsc.h"
  24. #include <Vc/vector.h>
  25. using Vc::float_v;
  26. using Vc::float_m;
  27. using uint_v = Vc::SimdArray<unsigned int, float_v::size()>;
  28. using uint_m = uint_v::mask_type;
  29. template<MandelImpl Impl>
  30. Mandel<Impl>::Mandel(QObject *_parent)
  31. : MandelBase(_parent)
  32. {
  33. }
  34. MandelBase::MandelBase(QObject *_parent)
  35. : QThread(_parent),
  36. m_restart(false), m_abort(false)
  37. {
  38. }
  39. MandelBase::~MandelBase()
  40. {
  41. m_mutex.lock();
  42. m_abort = true;
  43. m_wait.wakeOne();
  44. m_mutex.unlock();
  45. wait();
  46. }
  47. void MandelBase::brot(const QSize &size, float x, float y, float scale)
  48. {
  49. QMutexLocker lock(&m_mutex);
  50. m_size = size;
  51. m_x = x;
  52. m_y = y;
  53. m_scale = scale;
  54. if (!isRunning()) {
  55. start(LowPriority);
  56. } else {
  57. m_restart = true;
  58. m_wait.wakeOne();
  59. }
  60. }
  61. void MandelBase::run()
  62. {
  63. while (!m_abort) {
  64. // first we copy the parameters to our local data so that the main main thread can give a
  65. // new task while we're working
  66. m_mutex.lock();
  67. // destination image, RGB is good - no need for alpha
  68. QImage image(m_size, QImage::Format_RGB32);
  69. float x = m_x;
  70. float y = m_y;
  71. float scale = m_scale;
  72. m_mutex.unlock();
  73. // benchmark the number of cycles it takes
  74. TimeStampCounter timer;
  75. timer.start();
  76. // calculate the mandelbrot set/image
  77. mandelMe(image, x, y, scale, 255);
  78. timer.stop();
  79. // if no new set was requested in the meantime - return the finished image
  80. if (!m_restart) {
  81. emit ready(image, timer.cycles());
  82. }
  83. // wait for more work
  84. m_mutex.lock();
  85. if (!m_restart) {
  86. m_wait.wait(&m_mutex);
  87. }
  88. m_restart = false;
  89. m_mutex.unlock();
  90. }
  91. }
  92. static const float S = 4.f;
  93. /**
  94. * std::complex is way too slow for our limited purposes:
  95. *
  96. * norm is implemented as std::abs(z) * std::abs(z) for float
  97. * z * z is implemented as multiplication & lots of branches looking for NaN and inf
  98. *
  99. * since we know that we require the square of r and i for norm and multiplication we can
  100. * explicitely cache it in the object
  101. */
  102. //! [MyComplex]
  103. template<typename T>
  104. class MyComplex
  105. {
  106. public:
  107. MyComplex(T r, T i)
  108. : m_real(r), m_imag(i),
  109. m_real2(r * r), m_imag2(i * i)
  110. {
  111. }
  112. MyComplex squaredPlus(T r, T i) const
  113. {
  114. return MyComplex(
  115. m_real2 + r - m_imag2,
  116. (m_real + m_real) * m_imag + i
  117. );
  118. }
  119. T norm() const
  120. {
  121. return m_real2 + m_imag2;
  122. }
  123. private:
  124. T m_real, m_imag;
  125. T m_real2, m_imag2;
  126. };
  127. //! [MyComplex]
  128. //! [P function]
  129. template<typename T> inline MyComplex<T> P(MyComplex<T> z, T c_real, T c_imag)
  130. {
  131. return z.squaredPlus(c_real, c_imag);
  132. }
  133. //! [P function]
  134. template<> void Mandel<VcImpl>::mandelMe(QImage &image, float x0,
  135. float y0, float scale, int maxIt)
  136. {
  137. typedef MyComplex<float_v> Z;
  138. const unsigned int height = image.height();
  139. const unsigned int width = image.width();
  140. const float_v colorScale = 0xff / static_cast<float>(maxIt);
  141. for (unsigned int y = 0; y < height; ++y) {
  142. unsigned int *Vc_RESTRICT line = reinterpret_cast<unsigned int *>(image.scanLine(y));
  143. const float_v c_imag = y0 + y * scale;
  144. uint_m toStore;
  145. for (uint_v x = uint_v::IndexesFromZero(); !(toStore = x < width).isEmpty();
  146. x += float_v::Size) {
  147. const float_v c_real = x0 + simd_cast<float_v>(x) * scale;
  148. Z z(c_real, c_imag);
  149. float_v n = float_v::Zero();
  150. float_m inside = z.norm() < S;
  151. while (!(inside && n < maxIt).isEmpty()) {
  152. z = P(z, c_real, c_imag);
  153. ++n(inside);
  154. inside = z.norm() < S;
  155. }
  156. uint_v colorValue = simd_cast<uint_v>((maxIt - n) * colorScale) * 0x10101;
  157. if (toStore.isFull()) {
  158. colorValue.store(line, Vc::Unaligned);
  159. line += uint_v::Size;
  160. } else {
  161. colorValue.store(line, toStore, Vc::Unaligned);
  162. break; // we don't need to check again wether x[0] + float_v::Size < width to break out of the loop
  163. }
  164. }
  165. if (restart()) {
  166. break;
  167. }
  168. }
  169. }
  170. template<> void Mandel<ScalarImpl>::mandelMe(QImage &image, float x0,
  171. float y0, float scale, int maxIt)
  172. {
  173. typedef MyComplex<float> Z;
  174. const int height = image.height();
  175. const int width = image.width();
  176. const float colorScale = 0xff / static_cast<float>(maxIt);
  177. for (int y = 0; y < height; ++y) {
  178. unsigned int *Vc_RESTRICT line = reinterpret_cast<unsigned int *>(image.scanLine(y));
  179. const float c_imag = y0 + y * scale;
  180. for (int x = 0; x < width; ++x) {
  181. const float c_real = x0 + x * scale;
  182. Z z(c_real, c_imag);
  183. int n = 0;
  184. for (; z.norm() < S && n < maxIt; ++n) {
  185. z = P(z, c_real, c_imag);
  186. }
  187. *line++ = static_cast<unsigned int>((maxIt - n) * colorScale) * 0x10101;
  188. }
  189. if (restart()) {
  190. break;
  191. }
  192. }
  193. }
  194. template class Mandel<VcImpl>;
  195. template class Mandel<ScalarImpl>;
  196. // vim: sw=4 sts=4 et tw=100