-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot_paralela_sse.cpp
175 lines (151 loc) · 3.79 KB
/
mandelbrot_paralela_sse.cpp
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
/*
Modified from: http://rosettacode.org/wiki/Mandelbrot_set#PPM_non_interactive
c program:
--------------------------------
1. draws Mandelbrot set for Fc(z)=z*z +c
using Mandelbrot algorithm ( boolean escape time )
-------------------------------
2. technique of creating ppm file is based on the code of Claudio Rocchini
http://en.wikipedia.org/wiki/Image:Color_complex_plot.jpg
create 24 bit color graphic file , portable pixmap file = PPM
see http://en.wikipedia.org/wiki/Portable_pixmap
to see the file use external application ( graphic viewer)
*/
#include <iostream>
#include <stdio.h>
#include <time.h>
int main()
{
clock_t start, end;
double cpu_time_used;
// Screen (integer) coordinates
const int iXmax = 16384;
const int iYmax = 16384;
// World (double) coordinate = parameter plane
double Cx_, Cy;
const double CxMin = -2.5;
const double CxMax = 1.5;
const double CyMin = -2.0;
const double CyMax = 2.0;
const double Two = 2.0;
double PixelWidth = (CxMax - CxMin) / iXmax;
double PixelHeight = (CyMax - CyMin) / iYmax;
// Color component ( R or G or B) is coded from 0 to 255
// It is 24 bit color RGB file
const int MaxColorComponentValue = 255;
// File
FILE * fp;
char *filename = "mandelbrot.ppm";
static unsigned char color[3];
// Z = Zx + Zy * i
// Z0 = 0
double Zx, Zy;
// Zx2 = Zx * Zx
// Zy2= Zy * Zy
double Zx2, Zy2;
int Iteration;
const int IterationMax = 256;
// Bail-out value , radius of circle
const double EscapeRadius = 2;
double ER2 = EscapeRadius*EscapeRadius;
// Create a new file, give it a name and open it in binary mode
fp = fopen(filename, "wb");
// Write ASCII header to the file
fprintf(fp, "P6\n %d\n %d\n %d\n", iXmax, iYmax, MaxColorComponentValue);
// Compute and write image data bytes to the file
start = clock();
for (double iY = 0; iY < iYmax; iY++)
{
__asm
{
// Cy = CyMin + iY*PixelHeight;
movupd xmm0, [CyMin]
movupd xmm1, [iY]
movupd xmm2, [PixelHeight]
mulpd xmm1, xmm2
addpd xmm0, xmm1
movupd[Cy], xmm0
}
if (std::fabs(Cy) < PixelHeight / 2)
{
// Main antenna
Cy = 0.0;
}
for (double iX = 0; iX < iXmax; iX++)
{
// initial value of orbit = critical point Z= 0
Zx = 0.0;
Zy = 0.0;
__asm
{
//Cx = CxMin + iX*PixelWidth;
movupd xmm0, [CxMin]
movupd xmm1, [iX]
movupd xmm2, [PixelWidth]
mulpd xmm1, xmm2
addpd xmm0, xmm1
movupd[Cx_], xmm0
// Zx2 = Zx*Zx;
movupd xmm0, [Zx]
mulpd xmm0, xmm0
movupd[Zx2], xmm0
//Zy2 = Zy*Zy;
movupd xmm0, [Zy]
mulpd xmm0, xmm0
movupd[Zy2], xmm0
}
for (Iteration = 0; Iteration < IterationMax && ((Zx2 + Zy2) < ER2); Iteration++)
{
__asm
{
//Zy = 2 * Zx*Zy + Cy;
movupd xmm0, [Two]
movupd xmm1, [Zx]
movupd xmm2, [Zy]
movupd xmm3, [Cy]
mulpd xmm0, xmm1
mulpd xmm0, xmm2
addpd xmm0, xmm3
movupd[Zy], xmm0
//Zx = Zx2 - Zy2 + Cx_;
movupd xmm0, [Zx2]
movupd xmm1, [Zy2]
movupd xmm2, [Cx_]
subpd xmm0, xmm1
addpd xmm0, xmm2
movupd[Zx], xmm0
//Zx2 = Zx*Zx;
movupd xmm0, [Zx]
mulpd xmm0, xmm0
movupd[Zx2], xmm0
//Zy2 = Zy*Zy;
movupd xmm0, [Zy]
mulpd xmm0, xmm0
movupd[Zy2], xmm0
}
}
if (Iteration == IterationMax)
{
// Interior of Mandelbrot set = black
color[0] = 0;
color[1] = 0;
color[2] = 0;
}
else
{
// Exterior of Mandelbrot set = white
color[0] = ((IterationMax - Iteration) % 8) * 63;
color[1] = ((IterationMax - Iteration) % 4) * 127;
color[2] = ((IterationMax - Iteration) % 2) * 255;
};
fwrite(color, 1, 3, fp);
}
}
end = clock();
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
std::cout << "time = " << cpu_time_used << " seconds\n";
fclose(fp);
int TESTE;
std::cin >> TESTE;
return 0;
}