aboutsummaryrefslogtreecommitdiffstats
path: root/opencl/mandelbrot_calc.c
blob: 19abc8f32b22ae6c355666a44653e5e32b212aef (plain)
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
__kernel void mandelbrot_calc(const __global double2 *c_pt,
                            __global unsigned int *out_it,
                            const unsigned int max_iterations)
{
    const int id = get_global_id(0);
    const double2 opt = c_pt[id];

    double2 pt = opt;
    double tmp = pt.x * pt.y;
    unsigned int iterations = 0;

    double q = (pt.x - 0.25) * (pt.x - 0.25) + pt.y * pt.y;
    if (q * (q + (pt.x - 0.25)) <= 0.25 * pt.y * pt.y)
        iterations = max_iterations;

    while (iterations < max_iterations) {
        pt *= pt;

        if (pt.x + pt.y > 4.0)
            break;

        pt.x = pt.x - pt.y;
        pt.y = 2 * tmp;
        pt += opt;
        tmp = pt.x * pt.y;

        ++iterations;
    }

    if (iterations == max_iterations)
        out_it[id] = 0;
    else
        out_it[id] = ((iterations & 0xFF) << 16) | ((iterations & 0x07) << 6);
}