-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.c
61 lines (52 loc) · 1.76 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
/*
* Copyright 2019 IBM Corp. and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#include <time.h>
typedef double FP_t;
__attribute__((noinline))
void mandelbrot(int x_pixels, int y_pixels, int* out_table, int max_iterations) {
FP_t x_scale_factor = 3.5 / (FP_t) x_pixels;
FP_t y_scale_factor = 2.0 / (FP_t) y_pixels;
for (int py = 0; py < y_pixels; ++py) {
for (int px = 0; px < x_pixels; ++px) {
FP_t x0 = px * x_scale_factor - 2.5;
FP_t y0 = py * y_scale_factor - 1.0;
FP_t x = 0.0;
FP_t y = 0.0;
int iteration = 0;
while (x*x + y*y <= 2*2 && iteration < max_iterations) {
FP_t x_temp = x*x - y*y + x0;
y = 2*x*y + y0;
x = x_temp;
iteration += 1;
}
out_table[py*x_pixels + px] = iteration;
}
}
}
int main(void) {
// force JIT compilation
int small_table [3][4] = {0};
mandelbrot(4, 3, (int*)small_table, 10);
// run and time the benchmark
int table[34][80] = {0};
time_t time_diff = time(NULL);
mandelbrot(80, 34, (int*)table, 300000);
time_diff = time(NULL) - time_diff;
return time_diff;
}