### constants

#### Trigonometric functions in constant array initializers in OpenCL

I have a number of vectors I would like to use in my application. In a square grid, these are the vectors of the cardinal and diagonal directions in which I can go from the center of a cell. My OpenCL kernels will use them often so I would like to define them in constant memory. I have written the following piece of code in my kernel file: #define N_RADIAN 2 * M_PI_4_F #define NE_RADIAN 1 * M_PI_4_F #define E_RADIAN 0 * M_PI_4_F #define SE_RADIAN 7 * M_PI_4_F #define S_RADIAN 6 * M_PI_4_F #define SW_RADIAN 5 * M_PI_4_F #define W_RADIAN 4 * M_PI_4_F #define NW_RADIAN 3 * M_PI_4_F constant float2 E[8] = { (float2)(cos( N_RADIAN), sin( N_RADIAN)), // N (float2)(cos(NE_RADIAN), sin(NE_RADIAN)), // NE (float2)(cos( E_RADIAN), sin( E_RADIAN)), // E (float2)(cos(SE_RADIAN), sin(SE_RADIAN)), // SE (float2)(cos( S_RADIAN), sin( S_RADIAN)), // S (float2)(cos(SW_RADIAN), sin(SW_RADIAN)), // SW (float2)(cos( W_RADIAN), sin( W_RADIAN)), // W (float2)(cos(NW_RADIAN), sin(NW_RADIAN)) // NW }; This code refuses to compile for me. The error message I get is error: initializer element is not a compile-time constant. I can understand it if the mathematical functions has to be called on the device for the array to get its values. If that is the case I can make a kernel which computes these values without much fuss. However, this method would be more convenient for me. Is there any way in which I can get these values declared in constant memory? Do you see any other problems with this approach or the code?

I think the only way to do it with constants is to go with something like this: constant float2 E[8] = { (0.0 , 1.0 ) , // N (CL_M_SQRT1_2 , CL_M_SQRT1_2 ) , // NE (1.0 , 0.0 ) , // E (CL_M_SQRT1_2 , -CL_M_SQRT1_2) , // SE (0.0 , -1.0 ) , // S (-CL_M_SQRT1_2 , -CL_M_SQRT1_2) , // SW (-1.0 , 0.0 ) , // W (-CL_M_SQRT1_2 , CL_M_SQRT1_2 ) // NW }; The problem may actually be a blessing in disguise. This alternative code generates values accurate to the limitation of 32-bit IEEE float. The original code is a little off due to the difference between pi/4 and M_PI_4_F. For example, the original code generates cosine (north) = -4.37114e-008 instead of the presumably intended value of zero.

### Related Links

What is the difference between “const” and “val”?

Global constants in Dragon NaturallySpeaking's advanced scripting

What are the “hidden constants”?

Are there a constants in scapy for TCP and UDP?

Where can I find a list of constants for Request Transaction Types for Authorize.Net's API?

When we will consider the constants in asymptotic notations?

Why does Pybrain predict always the same value ?

How can I set a constant in autoconf checking for a program?

Error: <signal> is not a constant

What is the write constant [duplicate]

can a constant pointer be changed during for iterations?

What is the value of ABAP constants with prefix “%_”?

introducing constants in solve function

private type declaration is throwing error?

What is the syntax for declaring a constant string[char] AA?

How to store second (time) as a constant on ios?