Scalar Data Types
The C99 scalar data types supported by OpenCL C are described in Table 4.1. Unlike C, OpenCL C describes the sizes, that is, the exact number of bits for the integer and floating-point data types.
Table 4.1. Built-In Scalar Data Types
Type |
Description |
bool |
A conditional data type that is either true or false. The value true expands to the integer constant 1, and the value false expands to the integer constant 0. |
char |
A signed two's complement 8-bit integer. |
unsigned char, uchar |
An unsigned 8-bit integer. |
short |
A signed two's complement 16-bit integer. |
unsigned short, ushort |
An unsigned 16-bit integer. |
int |
A signed two's complement 32-bit integer. |
unsigned int, uint |
An unsigned 32-bit integer. |
long |
A signed two's complement 64-bit integer. |
unsigned long, ulong |
An unsigned 64-bit integer. |
float |
A 32-bit floating-point. The float data type must conform to the IEEE 754 single-precision storage format. |
double |
A 64-bit floating-point. The double data type must conform to the IEEE 754 double-precision storage format. This is an optional format and is available only if the double-precision extension (cl_khr_fp64) is supported by the device. |
half |
A 16-bit floating-point. The half data type must conform to the IEEE 754-2008 half-precision storage format. |
size_t |
The unsigned integer type of the result of the sizeof operator. This is a 32-bit unsigned integer if the address space of the device is 32 bits and is a 64-bit unsigned integer if the address space of the device is 64 bits. |
ptrdiff_t |
A signed integer type that is the result of subtracting two pointers. This is a 32-bit signed integer if the address space of the device is 32 bits and is a 64-bit signed integer if the address space of the device is 64 bits. |
intptr_t |
A signed integer type with the property that any valid pointer to void can be converted to this type, then converted back to a pointer to void, and the result will compare equal to the original pointer. |
uintptr_t |
An unsigned integer type with the property that any valid pointer to void can be converted to this type, then converted back to a pointer to void, and the result will compare equal to the original pointer. |
void |
The void type constitutes an empty set of values; it is an incomplete type that cannot be completed. |
The half Data Type
The half data type must be IEEE 754-2008-compliant. half numbers have 1 sign bit, 5 exponent bits, and 10 mantissa bits. The interpretation of the sign, exponent, and mantissa is analogous to that of IEEE 754 floating-point numbers. The exponent bias is 15. The half data type must represent finite and normal numbers, denormalized numbers, infinities, and NaN. Denormalized numbers for the half data type, which may be generated when converting a float to a half using the built-in function vstore_half and converting a half to a float using the built-in function vload_half, cannot be flushed to zero.
Conversions from float to half correctly round the mantissa to 11 bits of precision. Conversions from half to float are lossless; all half numbers are exactly representable as float values.
The half data type can be used only to declare a pointer to a buffer that contains half values. A few valid examples are given here:
void bar(global half *p) { ... } void foo(global half *pg, local half *pl) { global half *ptr; int offset; ptr = pg + offset; bar(ptr); }
Following is an example that is not a valid usage of the half type:
half a; half a[100]; half *p; a = *p; // not allowed. must use vload_half function
Loads from a pointer to a half and stores to a pointer to a half can be performed using the vload_half, vload_half n , vloada_half n and vstore_half, vstore_half n , and vstorea_half n functions, respectively. The load functions read scalar or vector half values from memory and convert them to a scalar or vector float value. The store functions take a scalar or vector float value as input, convert it to a half scalar or vector value (with appropriate rounding mode), and write the half scalar or vector value to memory.