-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.c
270 lines (256 loc) · 6.87 KB
/
mandelbrot.c
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
mandelbrot - visualize Mandelbrot set using SIMD
Written in 2013 by <Ahmet Inan> <xdsopl@googlemail.com>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <math.h>
#include <complex.h>
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <immintrin.h>
#ifdef __AVX__
typedef float vnsf __attribute__ ((__vector_size__ (32)));
typedef unsigned vnsu __attribute__ ((__vector_size__ (32)));
#define vnsf_set1(a) _mm256_set1_ps(a)
#define vnsf_cmplt(a, b) ((vnsu)_mm256_cmp_ps(a, b, _CMP_LT_OQ))
#define vnsf_len (8)
#else
typedef float vnsf __attribute__ ((__vector_size__ (16)));
typedef unsigned vnsu __attribute__ ((__vector_size__ (16)));
#define vnsf_set1(a) _mm_set1_ps(a)
#define vnsf_cmplt(a, b) ((vnsu)_mm_cmplt_ps(a, b))
#define vnsf_len (4)
#endif
SDL_Surface *screen;
const int NUM = 32;
#define MAX_ITER (100)
float zoom = 5.0;
float xoff = -0.75;
float yoff = 0.0;
void resize_screen(int w, int h)
{
screen = SDL_SetVideoMode(w, h, 32, SDL_DOUBLEBUF|SDL_RESIZABLE);
if (NULL == screen)
exit(1);
if (screen->format->BytesPerPixel != 4)
exit(1);
}
void handle_events()
{
static int button_left = 0;
static int button_right = 0;
static int button_middle = 0;
int width = 0, height = 0;
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
switch (event.button.button) {
case SDL_BUTTON_LEFT:
button_left = 1;
break;
case SDL_BUTTON_MIDDLE:
button_middle = 1;
break;
case SDL_BUTTON_RIGHT:
button_right = 1;
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONUP:
switch (event.button.button) {
case SDL_BUTTON_LEFT:
button_left = 0;
break;
case SDL_BUTTON_MIDDLE:
button_middle = 0;
break;
case SDL_BUTTON_RIGHT:
button_right = 0;
break;
default:
break;
}
break;
case SDL_MOUSEMOTION:
if (button_left) {
yoff -= zoom * (float)event.motion.yrel / (float)screen->h;
xoff -= zoom * (float)event.motion.xrel / (float)screen->w;
}
if (button_middle) {
}
if (button_right) {
zoom -= zoom * (float)event.motion.yrel / (float)screen->h;
}
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_q:
exit(0);
break;
case SDLK_ESCAPE:
exit(0);
break;
case SDLK_r:
zoom = 5.0;
xoff = -0.75;
yoff = 0.0;
break;
default:
break;
}
break;
case SDL_VIDEORESIZE:
width = event.resize.w;
height = event.resize.h;
break;
case SDL_QUIT:
exit(1);
break;
default:
break;
}
}
if (width && height)
resize_screen(width, height);
}
void handle_stats(double elapsed, uint64_t stat_pixel, uint64_t pixels)
{
double pps; /* pixel per second */
double mpps; /* mega pixel per second */
double fps; /* frames per second */
pps = (double)stat_pixel / elapsed;
mpps = 0.000001 * pps;
fps = pps / (double)pixels;
char outs[128];
if (fps < 1.0) {
snprintf(outs, sizeof(outs), "%.1f s/f %.1f p/s", 1.0/fps, pps);
} else {
snprintf(outs, sizeof(outs), "%.1f f/s %.1f mp/s", fps, mpps);
}
SDL_WM_SetCaption(outs, "mandelbrot");
// fprintf(stderr, "%s\n", outs);
}
#if 0
void calc(int t[NUM], complex float c[NUM])
{
for (int i = 0; i < NUM; i++) {
complex float z = 0;
for (t[i] = 0; t[i] < MAX_ITER; t[i]++) {
float abs2 = crealf(z) * crealf(z) + cimagf(z) * cimagf(z);
if (abs2 >= 4.0f)
break;
z = z * z + c[i];
}
if (t[i] >= MAX_ITER)
t[i] = 0;
}
}
#else
void calc(int T[NUM], complex float C[NUM])
{
const vnsf _1 = vnsf_set1(1.0f);
const vnsf _2 = vnsf_set1(2.0f);
const vnsf _4 = vnsf_set1(4.0f);
const vnsf _MAX_ITER = vnsf_set1((float)MAX_ITER);
vnsf c_real[NUM / vnsf_len];
vnsf c_imag[NUM / vnsf_len];
for (int i = 0; i < NUM; i++) {
((float *)c_real)[i] = crealf(C[i]);
((float *)c_imag)[i] = cimagf(C[i]);
}
vnsf z_real[NUM / vnsf_len];
vnsf z_imag[NUM / vnsf_len];
vnsf t[NUM / vnsf_len];
memset(z_real, 0, sizeof(z_real));
memset(z_imag, 0, sizeof(z_imag));
memset(t, 0, sizeof(t));
for (int n = 0; n < MAX_ITER; n++) {
for (int i = 0; i < NUM / vnsf_len; i++) {
vnsf abs2 = z_real[i] * z_real[i] + z_imag[i] * z_imag[i];
t[i] += (vnsf)((vnsu)_1 & vnsf_cmplt(abs2, _4));
vnsf tmp_real = z_real[i] * z_real[i] - z_imag[i] * z_imag[i] + c_real[i];
vnsf tmp_imag = _2 * z_imag[i] * z_real[i] + c_imag[i];
z_real[i] = tmp_real;
z_imag[i] = tmp_imag;
}
}
for (int i = 0; i < NUM / vnsf_len; i++)
t[i] = (vnsf)(vnsf_cmplt(t[i], _MAX_ITER) & (vnsu)t[i]);
for (int i = 0; i < NUM; i++)
T[i] = ((float *)t)[i];
}
#endif
uint32_t srgb(float r, float g, float b)
{
r = fminf(fmaxf(r, 0.0f), 1.0f);
g = fminf(fmaxf(g, 0.0f), 1.0f);
b = fminf(fmaxf(b, 0.0f), 1.0f);
#if 1
float K0 = 0.04045f;
float a = 0.055f;
float phi = 12.92f;
float gamma = 2.4f;
r = r <= K0 / phi ? r * phi : (1.0f + a) * powf(r, 1.0f / gamma) - a;
g = g <= K0 / phi ? g * phi : (1.0f + a) * powf(g, 1.0f / gamma) - a;
b = b <= K0 / phi ? b * phi : (1.0f + a) * powf(b, 1.0f / gamma) - a;
#endif
return (int)(255.0f * r) << 16 |
(int)(255.0f * g) << 8 |
(int)(255.0f * b);
}
uint32_t color(float v)
{
float r = 6.0f * fabsf(v - 3.0f / 6.0f) - 1.0f;
float g = 2.0f - 6.0f * fabsf(v - 2.0f / 6.0f);
float b = 2.0f - 6.0f * fabsf(v - 4.0f / 6.0f);
return srgb(r, g, b);
}
int main()
{
SDL_Init(SDL_INIT_VIDEO);
resize_screen(640, 480);
SDL_WM_SetCaption("Mandelbrot", "mandelbrot");
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
uint32_t stat_ticks = SDL_GetTicks();
uint64_t stat_pixels = 0;
uint32_t palette[MAX_ITER];
for (int i = 0; i < MAX_ITER; i++)
palette[i] = i ? color(i / (float)MAX_ITER) : 0;
while (1) {
handle_events();
uint32_t *fbp = (uint32_t *)screen->pixels;
int w = screen->w;
int h = screen->h;
float dx = 1.0f / (float)w;
float dy = 1.0f / (float)h;
for (int j = 0; j < (h * w - NUM); j += NUM) {
complex float c[NUM];
for (int k = 0; k < NUM; k++)
c[k] = (zoom * (dx * (float)((j+k)%w) - 0.5f) + xoff)
+ I * (zoom * (dy * (float)((j+k)/w) - 0.5f) + yoff);
int t[NUM];
calc(t, c);
for (int k = 0; k < NUM; k++)
fbp[j + k] = palette[t[k]];
}
#if 0
for (int j = 0; j < h; j++)
for (int i = 0; i < MAX_ITER; i++)
fbp[j * w + i] = palette[i];
#endif
stat_pixels += w*h;
uint32_t cur_ticks = SDL_GetTicks();
if ((cur_ticks - stat_ticks) > 1000) {
handle_stats(0.001 * (double)(cur_ticks - stat_ticks), stat_pixels, w*h);
stat_pixels = 0;
stat_ticks = cur_ticks;
}
SDL_Flip(screen);
}
return 0;
}