├── autogen.sh ├── Makefile.am ├── include ├── pencil_host_c.h ├── pencil_kernel_cu.h ├── pencil_host_impl.h ├── Makefile.am ├── pencil_cpp.h ├── pencil_ppcg.h ├── pencil_kernel_c.h ├── pencil_kernel_cl.h ├── pencil_mathdecl.h ├── pencil_compat.h ├── pencil_mathimpl.h └── pencil.h ├── .gitignore ├── configure.ac └── README /autogen.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | autoreconf -i 4 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | # @configure_input@ 2 | 3 | ACLOCAL_AMFLAGS = -I m4 4 | 5 | SUBDIRS = include 6 | 7 | EXTRA_DIST = \ 8 | autogen.sh 9 | -------------------------------------------------------------------------------- /include/pencil_host_c.h: -------------------------------------------------------------------------------- 1 | #ifndef _PENCIL_HOST_C_H 2 | #define _PENCIL_HOST_C_H 3 | 4 | #include "pencil_compat.h" 5 | 6 | #define PENCIL_MATHDECL static 7 | 8 | #endif /* _PENCIL_HOST_C_H */ 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # autoreconf -i 2 | Makefile.in 3 | /autom4te.cache/ 4 | /build-aux/ 5 | /m4/ 6 | /aclocal.m4 7 | /configure 8 | 9 | # ./configure 10 | Makefile 11 | /config.log 12 | /config.status 13 | /libtool 14 | -------------------------------------------------------------------------------- /include/pencil_kernel_cu.h: -------------------------------------------------------------------------------- 1 | #ifndef PENCIL_KERNEL_CU_H 2 | #define PENCIL_KERNEL_CU_H 3 | 4 | #include 5 | #include 6 | 7 | #include "pencil_mathimpl.h" 8 | 9 | #endif /* PENCIL_KERNEL_CU_H */ 10 | -------------------------------------------------------------------------------- /include/pencil_host_impl.h: -------------------------------------------------------------------------------- 1 | #ifndef PENCIL_HOST_IMPL_H 2 | #define PENCIL_HOST_IMPL_H 3 | 4 | /* TODO: Might avoid this file by macrofying the math defs and enable only when needed. (And not define that macro in pencil.h or pencil_cpp.h, like PENCIL_MATHDECL) */ 5 | #include "pencil_mathimpl.h" 6 | 7 | #endif /* PENCIL_HOST_IMPL_H */ 8 | -------------------------------------------------------------------------------- /include/Makefile.am: -------------------------------------------------------------------------------- 1 | # @configure_input@ 2 | 3 | ACLOCAL_AMFLAGS = -I m4 4 | 5 | include_HEADERS = \ 6 | pencil.h \ 7 | pencil_compat.h \ 8 | pencil_mathdecl.h \ 9 | pencil_mathimpl.h \ 10 | pencil_cpp.h \ 11 | pencil_ppcg.h \ 12 | pencil_host_c.h \ 13 | pencil_host_impl.h \ 14 | pencil_kernel_c.h \ 15 | pencil_kernel_cu.h \ 16 | pencil_kernel_cl.h 17 | -------------------------------------------------------------------------------- /include/pencil_cpp.h: -------------------------------------------------------------------------------- 1 | #ifndef PENCIL_CPP_H 2 | #define PENCIL_CPP_H 3 | 4 | /* These are mandatory #includes that have to be added before proprocessing (otherwise the #include guard doesn't work). */ 5 | /* In contrast to those in pencil.h, which is optional to the user. */ 6 | 7 | /* int32_t,int64_t must be declared for math builtins */ 8 | #include /* int32_t, int64_t */ 9 | #include /* For pencil_mathimpl.h */ 10 | 11 | #endif /* PENCIL_CPP_H */ 12 | -------------------------------------------------------------------------------- /include/pencil_ppcg.h: -------------------------------------------------------------------------------- 1 | #ifndef _PENCIL_PPCG_H 2 | #define _PENCIL_PPCG_H 3 | 4 | /* TODO: Replace by a PPCG builtin. */ 5 | #define __pencil_def(X) ((X)=0) 6 | #define __pencil_use(X) ((void)(X)) 7 | #define __pencil_maybe() (0.1f != strtod("NaN", NULL)) 8 | 9 | /* TODO: Make ppcg skip summary functions */ 10 | #define PENCIL_SUMMARY_FUNC 11 | #define PENCIL_ACCESS(...) __attribute__((pencil_access(__VA_ARGS__))) 12 | 13 | #define PENCIL_MATHDECL 14 | 15 | #endif /* _PENCIL_PPCG_H */ 16 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ([2.69]) 5 | AC_INIT([pencil-headers], [1.0], [pencil-discussion [AT] inria.fr]) 6 | AC_CONFIG_MACRO_DIR([m4]) 7 | 8 | # Automake 9 | AC_CONFIG_AUX_DIR([build-aux]) 10 | AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects]) 11 | 12 | # Programs 13 | AM_PROG_AR 14 | AC_PROG_CXX 15 | AC_PROG_LIBTOOL 16 | 17 | AC_CONFIG_FILES([Makefile]) 18 | AC_CONFIG_FILES([include/Makefile]) 19 | 20 | AC_OUTPUT 21 | -------------------------------------------------------------------------------- /include/pencil_kernel_c.h: -------------------------------------------------------------------------------- 1 | #ifndef PENCIL_KERNEL_C_H 2 | #define PENCIL_KERNEL_C_H 3 | 4 | /* PENCIL functionality that require an #include in C */ 5 | #include /* bool */ 6 | #include /* int32_t, int64_t */ 7 | #include /* abs, labs, llabs */ 8 | #include /* sqrt, sqrtf, ... */ 9 | #include /* assert */ 10 | 11 | #include "pencil_mathimpl.h" 12 | 13 | #ifndef __has_extension 14 | #define __has_extension(...) 0 15 | #endif 16 | 17 | #if (__GNUC__) > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __has_extension(c_static_assert) 18 | _Static_assert(sizeof(int)==4, "PENCIL: int must be 32 bit to match OpenCL's"); 19 | _Static_assert(sizeof(long)==8, "PENCIL: long must be 64 bit to match OpenCL's"); 20 | #endif 21 | 22 | #endif /* PENCIL_KERNEL_C_H */ 23 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | # PENCIL Header Files 2 | ----------------------- 3 | This repository contains two folders: 4 | - The include/ folder which contains the PENCIL header files. 5 | For instance, it contains the pencil.h file that you should include 6 | in your PENCIL programs. 7 | - The pencil_headers_generator/ folder which contains the PENCIL header files 8 | generator, it is the C++ program that was used to generate some of the PENCIL 9 | header files (Math functions). 10 | Note that the header files generated from the PENCIL header generator 11 | are different from the headers in include/ as the later are modified 12 | by hand after being generated. 13 | 14 | 15 | # Mailing List 16 | ---------------- 17 | For any problem, please send an email to pencil-discussion [AT] inria.fr 18 | 19 | 20 | # Authors 21 | ---------- 22 | - Alexey Kravets. 23 | - Michael Kruse. 24 | - Riyadh Baghdadi. 25 | - Robert David. 26 | 27 | # Toolchain 28 | ----------- -------------------------------------------------------------------------------- /include/pencil_kernel_cl.h: -------------------------------------------------------------------------------- 1 | #ifndef PENCIL_OPENCL_H 2 | #define PENCIL_OPENCL_H 3 | 4 | /* Do not #include other files here; ppcg just pastes this file literally into the target file. */ 5 | 6 | /* These sizes are fixed by the OpenCL specification. */ 7 | typedef int int32_t; 8 | typedef long int64_t; 9 | 10 | int32_t abs32(int32_t x) { return abs(x); } 11 | int64_t abs64(int64_t x) { return abs(x); } 12 | 13 | int32_t min32(int32_t x, int32_t y) { return min(x, y); } 14 | int64_t min64(int64_t x, int64_t y) { return min(x, y); } 15 | 16 | int32_t max32(int32_t x, int32_t y) { return max(x, y); } 17 | int64_t max64(int64_t x, int64_t y) { return max(x, y); } 18 | 19 | int32_t clamp32(int32_t x, int32_t minvalue, int32_t maxvalue) { return clamp(x, minvalue, maxvalue); } 20 | int64_t clamp64(int64_t x, int64_t minvalue, int64_t maxvalue) { return clamp(x, minvalue, maxvalue); } 21 | 22 | 23 | #ifdef cl_khr_fp64 24 | #if __OPENCL_VERSION__ < 120 25 | #pragma OPENCL EXTENSION cl_khr_fp64 : enable 26 | #endif 27 | 28 | /* Here be math functions with double precision. */ 29 | 30 | #if __OPENCL_VERSION__ < 120 31 | #pragma OPENCL EXTENSION cl_khr_fp64 : disable 32 | #endif 33 | #endif 34 | 35 | #endif /* ifndef PENCIL_OPENCL_H */ 36 | -------------------------------------------------------------------------------- /include/pencil_mathdecl.h: -------------------------------------------------------------------------------- 1 | #ifndef PENCIL_MATHDECLS_H 2 | #define PENCIL_MATHDECLS_H 3 | 4 | PENCIL_MATHDECL int32_t abs32(int32_t x); 5 | PENCIL_MATHDECL int64_t abs64(int64_t x); 6 | 7 | PENCIL_MATHDECL int min(int x, int y); 8 | PENCIL_MATHDECL long lmin(long x, long y); 9 | //PENCIL_MATHDECL long long llmin(long long x, long long y); 10 | PENCIL_MATHDECL int32_t min32(int32_t x, int32_t y); 11 | PENCIL_MATHDECL int64_t min64(int64_t x, int64_t y); 12 | 13 | PENCIL_MATHDECL int max(int x, int y); 14 | PENCIL_MATHDECL long lmax(long x, long y); 15 | //PENCIL_MATHDECL long long llmax(long long x, long long y); 16 | PENCIL_MATHDECL int32_t max32(int32_t x, int32_t y); 17 | PENCIL_MATHDECL int64_t max64(int64_t x, int64_t y); 18 | 19 | PENCIL_MATHDECL int clamp(int x, int minval, int maxval); 20 | PENCIL_MATHDECL long lclamp(long x, long minval, long maxval); 21 | //PENCIL_MATHDECL long long llclamp(long long x, long long minval, long long maxval); 22 | PENCIL_MATHDECL int32_t clamp32(int32_t x, int32_t minval, int32_t maxval); 23 | PENCIL_MATHDECL int64_t clamp64(int64_t x, int64_t minval, int64_t maxval); 24 | PENCIL_MATHDECL double fclamp(double x, double minval, double maxval); 25 | PENCIL_MATHDECL float fclampf(float x, float minval, float maxval); 26 | 27 | PENCIL_MATHDECL double mix(double x, double y, double a); 28 | PENCIL_MATHDECL float mixf(float x, float y, float a); 29 | 30 | PENCIL_MATHDECL double atan2pi(double x, double y); 31 | PENCIL_MATHDECL float atan2pif(float x, float y); 32 | 33 | #endif /* PENCIL_MATHDECLS_H */ 34 | -------------------------------------------------------------------------------- /include/pencil_compat.h: -------------------------------------------------------------------------------- 1 | #ifndef PENCIL_COMPAT_H 2 | #define PENCIL_COMPAT_H 3 | 4 | #if 0 5 | /* This file is also included by _host.c which is already preprocessed. That it, 6 | * header guards #defines disappeared such that we cannot #include headers 7 | * anymore that maybe already have been included. */ 8 | #include /* assert */ 9 | #include /* bool */ 10 | #include /* abs, labs, llabs */ 11 | #include /* sqrt, sqrtf, ... */ 12 | #endif 13 | 14 | /* PENCIL builtin functions */ 15 | #define __pencil_kill(...) 16 | #define __pencil_use(...) 17 | #define __pencil_def(...) 18 | #define __pencil_maybe() 1 19 | #define __pencil_assume(...) 20 | #define __pencil_assert(...) assert(__VA_ARGS__) 21 | 22 | /* Avoid gcc warning for __attribute__(((pencil_access(func_summary))) */ 23 | #define pencil_access(X) unused 24 | 25 | 26 | /* Non-standard */ 27 | #define PENCIL_ACCESS(X) 28 | #define PENCIL_BEGIN_SCOP 29 | #define PENCIL_END_SCOP 30 | #define PENCIL_INDEPENDENT(X) 31 | #define pencil_array static const restrict 32 | #define PENCIL_ARRAY static const restrict 33 | #define PENCIL_SUMMARY_FUNC static __attribute__((unused)) 34 | 35 | /* Additional PENCIL types not in C99 */ 36 | /* half */ 37 | #if __ARM_FP16_ARGS 38 | /* use __fp16 only if usable as arguments (some older ARM targets only supports it in structs or arrays) */ 39 | #define half __fp16 40 | #else /* __ARM_FP16_ARGS */ 41 | /* 16-bit floating-point is not supported: fallback to float */ 42 | #define half float 43 | #endif /* __ARM_FP16_ARGS */ 44 | 45 | #endif /* PENCIL_COMPAT_H */ 46 | -------------------------------------------------------------------------------- /include/pencil_mathimpl.h: -------------------------------------------------------------------------------- 1 | #ifndef PENCIL_MATHDEFS_H 2 | #define PENCIL_MATHDEFS_H 3 | 4 | #ifdef __CUDACC__ 5 | #define PENCIL_MATHDEF __host__ __device__ static 6 | /* TODO: Prefer CUDA intrinisics over own definitions (min, max). */ 7 | #else 8 | #define PENCIL_MATHDEF static inline 9 | #endif 10 | 11 | /* PENCIL standard library functions NOT in math.h or stdlib.h */ 12 | PENCIL_MATHDEF int32_t abs32(int32_t x) { return (x >= 0) ? x : -x; } 13 | PENCIL_MATHDEF int64_t abs64(int64_t x) { return (x >= 0) ? x : -x; } 14 | 15 | #ifndef __CUDACC__ 16 | PENCIL_MATHDEF int min(int x, int y) { return (x<=y) ? x : y; } 17 | #endif 18 | PENCIL_MATHDEF long lmin(long x, long y) { return (x<=y) ? x : y; } 19 | //PENCIL_MATHDEF long long llmin(long long x, long long y) { return (x<=y) ? x : y; } 20 | PENCIL_MATHDEF int32_t min32(int32_t x, int32_t y) { return (x<=y) ? x : y; } 21 | PENCIL_MATHDEF int64_t min64(int64_t x, int64_t y) { return (x<=y) ? x : y; } 22 | 23 | #ifndef __CUDACC__ 24 | PENCIL_MATHDEF int max(int x, int y) { return (x>=y) ? x : y; } 25 | #endif 26 | PENCIL_MATHDEF long lmax(long x, long y) { return (x>=y) ? x : y; } 27 | //PENCIL_MATHDEF long long llmax(long long x, long long y) { return (x>=y) ? x : y; } 28 | PENCIL_MATHDEF int32_t max32(int32_t x, int32_t y) { return (x>=y) ? x : y; } 29 | PENCIL_MATHDEF int64_t max64(int64_t x, int64_t y) { return (x>=y) ? x : y; } 30 | 31 | 32 | PENCIL_MATHDEF int clamp(int x, int minval, int maxval) { if (x < minval) return minval; if (x > maxval) return maxval; return x; } 33 | PENCIL_MATHDEF long lclamp(long x, long minval, long maxval) { if (x < minval) return minval; if (x > maxval) return maxval; return x; } 34 | //static inline long long llclamp(long long x, long long minval, long long maxval) { if (x < minval) return minval; if (x > maxval) return maxval; return x; } 35 | PENCIL_MATHDEF int32_t clamp32(int32_t x, int32_t minval, int32_t maxval) { if (x < minval) return minval; if (x > maxval) return maxval; return x; } 36 | PENCIL_MATHDEF int64_t clamp64(int64_t x, int64_t minval, int64_t maxval) { if (x < minval) return minval; if (x > maxval) return maxval; return x; } 37 | PENCIL_MATHDEF double fclamp(double x, double minval, double maxval) { if (x < minval) return minval; if (x > maxval) return maxval; return x; } 38 | PENCIL_MATHDEF float fclampf(float x, float minval, float maxval) { if (x < minval) return minval; if (x > maxval) return maxval; return x; } 39 | 40 | PENCIL_MATHDEF double mix(double x, double y, double a) { return x + (x-y) * a; } 41 | PENCIL_MATHDEF float mixf(float x, float y, float a) { return x + (x-y) * a; } 42 | 43 | PENCIL_MATHDEF double atan2pi(double x, double y) { return atan2(x, y) / 3.14159265358979323846; } 44 | PENCIL_MATHDEF float atan2pif(float x, float y) { return atan2f(x, y) / 3.14159265358979323846f; } 45 | 46 | #endif /* PENCIL_MATHDEFS_H */ 47 | -------------------------------------------------------------------------------- /include/pencil.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, ARM Limited 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | */ 22 | 23 | /* 24 | * Header file to be included by programmer into pencil files. 25 | */ 26 | 27 | #ifndef _PENCIL_H 28 | #define _PENCIL_H 29 | 30 | /* The user is free to include these before pencil.h; We will need them anyway so include them in all cases and avoid different behaviour depending on the #include order. */ 31 | /* TODO: Except in pure mode */ 32 | #include /* bool */ 33 | #include /* int32_t, int64_t */ 34 | #include /* strtod, abs, labs, llabs */ 35 | #include /* sin, sinf, ... */ 36 | 37 | /* Preprocessor aliases for PENCIL builtins. 38 | * They can be expanded here because the same magic happens using the __pencil_* 39 | * definitions. 40 | */ 41 | #define PENCIL_USE __pencil_use 42 | #define PENCIL_DEF __pencil_def 43 | #define PENCIL_MAYBE (__pencil_maybe()) 44 | 45 | /* For compatibility to the PENCIL spec. */ 46 | #define USE PENCIL_USE 47 | #define DEF PENCIL_DEF 48 | #define MAYBE PENCIL_MAYBE 49 | 50 | #ifndef PRL_PENCIL_H 51 | /* This is for outer C-code not itself PENCIL-code calling PENCIL functions. */ 52 | /* TODO: We maybe should put it into a separate header file because it is not 53 | * compatible to the PENCIL grammar. It is not sufficient to put it into the 54 | * "The file is processed as a C file" branch it would not be available to host 55 | * code when compiling embedded PENCIL functions using ppcg. 56 | */ 57 | enum npr_mem_tags { 58 | PENCIL_NPR_MEM_NOWRITE = 1, 59 | PENCIL_NPR_MEM_NOREAD = 2, 60 | PENCIL_NPR_MEM_NOACCESS = PENCIL_NPR_MEM_NOWRITE | PENCIL_NPR_MEM_NOREAD, 61 | PENCIL_NPR_MEM_READ = 4, 62 | PENCIL_NPR_MEM_WRITE = 8, 63 | PENCIL_NPR_MEM_READWRITE = PENCIL_NPR_MEM_READ | PENCIL_NPR_MEM_WRITE 64 | }; 65 | void prl_npr_mem_tag(void *location, enum npr_mem_tags mode) __attribute__((weak)); 66 | static void __pencil_npr_mem_tag(void *location, enum npr_mem_tags mode) { 67 | if (&prl_npr_mem_tag) 68 | prl_npr_mem_tag(location, mode); 69 | } 70 | #endif /* PRL_PENCIL_H */ 71 | 72 | #ifdef __PENCIL__ 73 | /* The file is processed by the PENCIL-to-OpenCL code generator. */ 74 | 75 | /* Custom stdbool.h 76 | * We cannot directly include stdbool because its content might notconfiorm the 77 | * PENCIL grammar. 78 | */ 79 | #define bool _Bool 80 | #define true 1 81 | #define false 0 82 | #define __bool_true_false_are_defined 1 83 | 84 | /* PENCIL-specific macros */ 85 | #define ACCESS(...) PENCIL_ACCESS(__VA_ARGS__) 86 | 87 | #define pencil_array static const restrict 88 | #define PENCIL_ARRAY static const restrict 89 | 90 | /* must define PENCIL_MATHDECL (either static or not) */ 91 | #include "pencil_mathdecl.h" 92 | 93 | #else /* __PENCIL__ */ 94 | /* The file is processed as a C file. */ 95 | 96 | /* PENCIL functionality that require an #include in C */ 97 | #include /* assert */ 98 | #include /* bool */ 99 | #include /* abs, labs, llabs */ 100 | #include /* sqrt, sqrtf, ... */ 101 | 102 | #include "pencil_compat.h" 103 | #include "pencil_mathimpl.h" 104 | 105 | #endif /* __PENCIL__ */ 106 | 107 | #endif /* _PENCIL_H */ 108 | --------------------------------------------------------------------------------