Skip to content

Instantly share code, notes, and snippets.

@JarkkoPFC
Last active February 16, 2024 23:26
Show Gist options
  • Save JarkkoPFC/0b160bdc3059c9cdb1113d17a4737471 to your computer and use it in GitHub Desktop.
Save JarkkoPFC/0b160bdc3059c9cdb1113d17a4737471 to your computer and use it in GitHub Desktop.
#include OpenCL files as a C++ source or string
//============================================================================
// Copyright (c) 2024, Jarkko Lempiainen
// All rights reserved.
//============================================================================
// This #include helper file can be used to #include OpenCL source files
// either as regular C++ source files to be compiled with a C++11 compiler
// (as normally done with regular #include) OR embedded as a c-strings to C++
// programs.
//
// The included OpenCL files must be enclosed within OCL_SOURCE_BEGIN and
// OCL_SOURCE_END)" tokens as follows and everything between the tokens
// (including comments) will be part of the c-string:
// my_source.cl:
// OCL_SOURCE_BEGIN
// // some comment
// int foo(int x_, int y_) {return x_+y_;}
// OCL_SOURCE_END)"
//
// Example to #include the OpenCL source file to C++ as a source file:
// #define OCL_CPP_INCLUDE "my_source.cl"
// #include "ocl_include.inc"
//
// Example to #include the OpenCL source file to C++ as a c-string:
// const char *my_source_string=
// #define OCL_CSTR_INCLUDE "my_source.cl"
// #include "ocl_include.inc"
//----------------------------------------------------------------------------
#define OCL_RAW_STRING R
//============================================================================
// C++ include
//============================================================================
#ifdef OCL_CPP_INCLUDE
// CL => C++ source conversion definitions
#ifndef OCL_CPP_CONVERSION_DEFINITIONS
#define OCL_CPP_CONVERSION_DEFINITIONS
typedef uint32_t uint;
typedef uint64_t ulong;
typedef pfc::float32_t float32_t;
typedef pfc::float64_t float64_t;
#define __kernel
#define __constant const
#define __local
#define __global
#define __private
#endif // OCL_CPP_CONVERSION_DEFINITIONS
//----
#define OCL_SOURCE_BEGIN
#define OCL_SOURCE_END static_assert(OCL_RAW_STRING"("
#include OCL_CPP_INCLUDE
,"");
#undef OCL_SOURCE_BEGIN
#undef OCL_SOURCE_END
#undef OCL_CPP_INCLUDE
#endif
//----------------------------------------------------------------------------
//============================================================================
// OpenCL include
//============================================================================
#ifdef OCL_CSTR_INCLUDE
#define OCL_SOURCE_BEGIN OCL_RAW_STRING"("#define OCL_SOURCE_END
#include OCL_CSTR_INCLUDE
"\n"+1;
#undef OCL_SOURCE_BEGIN
#undef OCL_CSTR_INCLUDE
#endif
//----------------------------------------------------------------------------
#undef OCL_RAW_STRING
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment