Vc  0.7.5-dev
SIMD Vector Classes for C++
Mandelbrot

This example draws a colorized Mandelbrot image on screen using Qt4 widgets.

The example uses a simple class to abstract complex numbers. In principle, one could just use std::complex, if it would perform well enough. But especially the norm function is very slow for scalar float/double. Also, complex multiplication is correctly implemented to handle NaN and infinity. This is not required for Mandelbrot as these special cases will not occur. Additionally, the provided complex abstraction stores the square of the real and imaginary parts to help the compiler in optimizing the code as good as possible.

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;
};

Mandelbrot uses the function z = z² + c for iteration.

template<typename T> inline MyComplex<T> P(MyComplex<T> z, T c_real, T c_imag)
{
return z.squaredPlus(c_real, c_imag);
}