Use Built-In Functions

OpenCL™ offers a library of built-in functions, including vector variants. For details, see the OpenCL specification.

Using built-in functions is typically more efficient than using their manual implementation in OpenCL code. Consider the following code example:

__kernel void Foo(const __global float* a, 
                          const __global float* b, 
                          __global float* c)
{
  int tid = get_global_id(0);
  c[tid] = 1/sqrt(a[tid] + b[tid]);
}

The following code uses the rsqrt built-in to implement the same example:

__kernel void Foo(const __global float* a, 
                          const __global float* b, 
                          __global float* c)
{
  int tid = get_global_id(0);
  c[tid] = rsqrt(a[tid] + b[tid]);
}

Consider simple expressions and built-ins based equivalents below:

dx * fCos + dy * fSin == dot( (float2)(dx, dy),(float2)(fCos, fSin))
x * a - b  == mad(x, a, -b)
sqrt(dot(x, y)) == distance(x,y)

Use specialized built-in versions like math, integer, and geometric built-ins, where possible, as the specialized built-ins work faster than their manually-computed counterparts. For example, when the x value for xy is ≥0, use powr instead of pow.

See Also

The OpenCL™ 1.2 Specification at https://www.khronos.org/registry/OpenCL/specs/opencl-1.2.pdf