#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

//#define YSIZE 
//#define XSIZE 
//#define ZSIZE 8
#define CONCURRENT_THREADS 15498
#define LOOKUP_GAP 2
//#define Coord(x,y,z) ((ulong)z*(y ## SIZE)*(x ## SIZE)+y*(x ## SIZE)+x)
//#define CO Coord(z,x,y)


/*
get_global_id(0)%xSIZE

global_id(d) = global_offset(d) + local_id(d) + group_id(d) * local_size(d)

global_offset(d) + local_id(d) + group_id(d) * local_size(d)

 ((global_offset(d) + group_id(d) * local_size(d) + local_id(d))%xSIZE)*zSIZE


x*(z ## SIZE) +

y*(x ## SIZE)*(z ## SIZE)

+ z


123976
63355824
8

63479808
1015676928


240172506

63355824

8


303528338
16
4856453408

4.8GB ?

size_t bufsize = 128 * ipt * cgpu->thread_concurrency;
1GB

#define COORDINDEX(x,y,z) (x+idx[y]+z)
#define COORDINDEX(x,y,z) (x+idx[y+z])
#define IDX COORDINDEX(x,y,z)
//#define Coord(x,y,z) (ulong)x*(z ## SIZE)+y*(x ## SIZE)*(z ## SIZE)+z
//#define CO Coord(x,y,z)
uint x = (get_global_id(0)%xSIZE)*zSIZE;
const uint loop_ceiling = ySIZE*zSIZE-zSIZE;
uint x = get_global_id(0)%xSIZE;

*/



#define Coord(x,y,z) ((ulong)y*(x ## SIZE)*(z ## SIZE)+z)
#define CO Coord(x,y,z)

int main(int argc, char **argv){
	const uint32_t nfact = 1024;
	const uint32_t zSIZE = 8;
	const uint32_t bufsize = (nfact/LOOKUP_GAP);
    const uint32_t ySIZE = (nfact/LOOKUP_GAP+(nfact%LOOKUP_GAP>0));
//    const uint32_t xSIZE = CONCURRENT_THREADS;
	//uint32_t buf[bufsize];
	uint32_t num1 = 0;
	uint32_t num2 = 0;
	uint32_t x, y, z;
    uint32_t idx[bufsize];

	x = 0;

	char *filename = "test.txt";
	FILE* fp;
    //for(uint y=0; y<(nfact/LOOKUP_GAP); ++y){
    //    for(uint z=0; z<zSIZE; ++z)   y*(nfactdivlg+(nfactormodlg>0))*threads
    //        idx[] = z + x*threads + y*(nfactdivlg+(nfactormodlg>0))*threads;
    //}

	if( (fp = fopen(filename, "w")) ){
		fprintf(fp, "idx[512] = {\n"); // bufsize);
	    for(y=0; y<(nfact/LOOKUP_GAP); ++y){
//		       for(z=0; z<zSIZE; ++z){
					num1++;

					if(num1%32 == 0 && y+z != 0){
						fprintf(fp, "\n\t\t");
			//idx[((y*zSIZE)+z)] = z;
            //idx[((y<<z)+z)] = z+x*CONCURRENT_THREADS+y*(nfact/LOOKUP_GAP+(nfact%LOOKUP_GAP>0))*CONCURRENT_THREADS;
            //buf[] = X[z];((ulong)y*(x ## SIZE)*(z ## SIZE)+z)
						//fprintf(fp, "\n\t\t%u*CONCURRENT_THREADS*8+%u, ", y, z);
					}

						//fprintf(fp, "%u*8, ", y);
						fprintf(fp, "%u, ", y*8);

//					if(y == 0 && z == 0){
//							fprintf(fp, "0, ");
//					}else if(y==0){
//							fprintf(fp, "%u, ", z);
//					}else if(z==0){
//							fprintf(fp, "%u*CONCURRENT_THREADS*8, ", y);
//					}else{

//							fprintf(fp, "%u*CONCURRENT_THREADS*8+%u, ", y, z);
//					}
  //  		    }
		}
	}else{
            printf("Error opening file!\n");
    }
        //g_free(filename);
        fclose(fp);


		//fprintf(fp, "idx[%u] = {\n", bufsize);

/*
        if( (fp = fopen(filename, "w")) ){
 
			fprintf(fp, "idx[%u] = {\n", bufsize);
           for(num1=0; num1<(bufsize); num1++){

			//num2 = num1*buffsize*CONCURRENT_THREADS;
				num2 = num1*8;

//((ulong)y*(x ## SIZE)*(z ## SIZE)+x*(z ## SIZE)+z)   y*(x ## SIZE)*(z ## SIZE)
				if(num1%20 == 0){
					fprintf(fp, "\t\t");
					//fprintf(fp, "\n");
					fprintf(fp, "%u, ", (unsigned long long)idx[num1]);
				}else
					fprintf(fp, "%u, ", (unsigned long long)idx[num1]);
                //fprintf(fp, "%d\n", PLevels->aLevels[perf_id].iMemoryClock);
                //fprintf(fp, "%d\n", PLevels->aLevels[perf_id].iVddc);
            }
			fprintf(fp, "%u\n", (unsigned long long)idx[num1]);
			fprintf(fp, "};\n", bufsize);



				//if(num1%16 == 0)
				//	fprintf(fp, "\n\t\t");
				//fprintf(fp, "%u*CONCURRENT_THREADS, ", num2);

			}



        else{
            printf("Error opening file!\n");
        }
        //g_free(filename);
        fclose(fp);
*/

	return 0;
}