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