/***** GCL Generated File *********************/
/* Automatically generated file, do not edit! */
/**********************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <dispatch/dispatch.h>
#include <OpenCL/opencl.h>
#include <OpenCL/gcl_priv.h>
#include "dct_cl_source.h"

static void initBlocks(void);

// Initialize static data structures
static block_kernel_pair pair_map[1] = {
      { NULL, NULL }
};

static block_kernel_map bmap = { 0, 1, initBlocks, pair_map };

// Block function
void (^DCT8x8_kernel)(const cl_ndrange *ndrange, cl_float* src, cl_float* dst, cl_int numTilesW, cl_int numTilesH) =
^(const cl_ndrange *ndrange, cl_float* src, cl_float* dst, cl_int numTilesW, cl_int numTilesH) {
  int err = 0;
  cl_kernel k = bmap.map[0].kernel;
  if (!k) {
    initBlocks();
    k = bmap.map[0].kernel;
  }
  if (!k)
    gcl_log_fatal("kernel DCT8x8 does not exist for device");
  kargs_struct kargs;
  gclCreateArgsAPPLE(k, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 0, src, &kargs);
  err |= gclSetKernelArgMemAPPLE(k, 1, dst, &kargs);
  err |= gclSetKernelArgAPPLE(k, 2, sizeof(numTilesW), &numTilesW, &kargs);
  err |= gclSetKernelArgAPPLE(k, 3, sizeof(numTilesH), &numTilesH, &kargs);
  gcl_log_cl_fatal(err, "setting argument for DCT8x8 failed");
  err = gclExecKernelAPPLE(k, ndrange, &kargs);
  gcl_log_cl_fatal(err, "Executing DCT8x8 failed");
  gclDeleteArgsAPPLE(k, &kargs);
};

// Initialization functions
static void initBlocks(void) {
  const char* build_opts = "";
  static dispatch_once_t once;
  dispatch_once(&once,
    ^{ int err = gclBuildProgramBinaryAPPLE("OpenCL/dct_cl_source", "com.macphun.noiselessPro", &bmap, build_opts);
       if (!err) {
          assert(bmap.map[0].block_ptr == DCT8x8_kernel && "mismatch block");
          bmap.map[0].kernel = clCreateKernel(bmap.program, "DCT8x8", &err);
       }
     });
}

__attribute__((constructor))
static void RegisterMap(void) {
  gclRegisterBlockKernelMap(&bmap);
  bmap.map[0].block_ptr = DCT8x8_kernel;
}

