/*
 *
 */
#pragma OPENCL EXTENSION cl_khr_fp64 : enable

__kernel void mandelbrot(int width, int height, double ox, double oy, double zoom, int maxcount, __global int* c, int iNumElements)
{
    // get index into global data array
    int iGID = get_global_id(0);

    // bound check (equivalent to the limit on a 'for' loop for standard/serial C code
    if (iGID >= iNumElements) return; 

    int n ;
    int x = (iGID % width) ;
    int y = (iGID / width) ;
    double C_re  = 4.0*((double)x - (double)width /2.0)/((double)width*zoom)+ox ;
    double C_im  = 4.0*((double)y - (double)height/2.0)/((double)width*zoom)+oy ;
    double ZO_re = 0.0 ;
    double ZO_im = 0.0 ;
    int   count =   0 ;

    for( n=0 ; n<maxcount ; n++ ){
      double Z1_re = ZO_re*ZO_re - ZO_im*ZO_im + C_re ;
      double Z1_im = ZO_re*ZO_im + ZO_im*ZO_re + C_im ;

      if( sqrt(Z1_re*Z1_re + Z1_im*Z1_im)>1000000.0 ) break ;
      ZO_re = Z1_re ;
      ZO_im = Z1_im ;
      count = count + 1 ;
    }
    if( count<maxcount ){
      c[iGID] = count ;
    }else{
      c[iGID] = -1 ;
    }
}
