// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com> // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
template<typename Real> void MandelbrotThread::render(int img_width, int img_height)
{ enum { packetSize = Eigen::internal::packet_traits<Real>::size }; // number of reals in a Packet typedef Eigen::Array<Real, packetSize, 1> Packet; // wrap a Packet as a vector
for(int y = id; y < img_height; y += threadcount)
{ int pix = y * img_width;
// for each pixel, we're going to do the iteration z := z^2 + c where z and c are complex numbers, // starting with z = c = complex coord of the pixel. pzi and pzr denote the real and imaginary parts of z. // pci and pcr denote the real and imaginary parts of c.
Packet pzi_start, pci_start; for(int i = 0; i < packetSize; i++) pzi_start[i] = pci_start[i] = start.y() + y * step.y();
for(int x = 0; x < alignedWidth; x += packetSize, pix += packetSize)
{
Packet pcr, pci = pci_start, pzr, pzi = pzi_start, pzr_buf; for(int i = 0; i < packetSize; i++) pzr[i] = pcr[i] = start.x() + (x+i) * step.x();
// do the iterations. Every iters_before_test iterations we check for divergence, // in which case we can stop iterating. int j = 0; typedef Eigen::Matrix<int, packetSize, 1> Packeti;
Packeti pix_iter = Packeti::Zero(), // number of iteration per pixel in the packet
pix_dont_diverge; // whether or not each pixel has already diverged do
{ for(int i = 0; i < iters_before_test/4; i++) // peel the inner loop by 4
{ # define ITERATE \
pzr_buf = pzr; \
pzr = pzr.square(); \
pzr -= pzi.square(); \
pzr += pcr; \
pzi = (2*pzr_buf)*pzi; \
pzi += pci;
ITERATE ITERATE ITERATE ITERATE
}
pix_dont_diverge = ((pzr.square() + pzi.square())
.eval() // temporary fix as what follows is not yet vectorized by Eigen
<= Packet::Constant(4)) // the 4 here is not a magic value, it's a math fact that if // the square modulus is >4 then divergence is inevitable.
.template cast<int>();
pix_iter += iters_before_test * pix_dont_diverge;
j++;
total_iter += iters_before_test * packetSize;
} while(j < max_iter/iters_before_test && pix_dont_diverge.any()); // any() is not yet vectorized by Eigen
// if the width is not a multiple of packetSize, fill the remainder in black for(int x = alignedWidth; x < img_width; x++, pix++)
buffer[4*pix] = buffer[4*pix+1] = buffer[4*pix+2] = 0;
} return;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.