Description

The following table describes the list of built-in common functions. These all operate component-wise. The description is per-component.

The generic type name gentype indicates that the function can take any of

  • float, float2, float3, float4, float8, or float16

  • double [1], double2, double3, double4, double8 or double16

  • half [2], half2, half3, half4, half8 or half16

as the type for the arguments.

The generic type name gentypef indicates that the function can take any of

  • float, float2, float3, float4, float8, or float16

as the type for the arguments.

The generic type name gentyped [3] indicates that the function can take any of

  • double, double2, double3, double4, double8 or double16

as the type for the arguments.

The generic type name gentypeh [4] indicates that the function can take any of

  • half, half2, half3, half4, half8 or half16

as the type for the arguments.

All functions taking or returning half types are supported only when the cl_khr_fp16 extension macro is supported.
Table 1. Built-in Scalar and Vector Argument Common Functions
Function Description

gentype clamp(gentype x, gentype minval, gentype maxval)
gentypef clamp(gentypef x, float minval, float maxval)
gentyped clamp(gentyped x, double minval, double maxval)

gentypeh clamp(gentypeh x, half minval, half maxval)

Returns fmin(fmax(x, minval), maxval). Results are undefined if minval > maxval.

gentype degrees(gentype radians)

Converts radians to degrees, i.e. (180 / π) * radians.

gentype max(gentype x, gentype y)
gentypef max(gentypef x, float y)
gentyped max(gentyped x, double y)

gentypeh max(gentypeh x, half y)

Returns y if x < y, otherwise it returns x. If x or y are infinite or NaN, the return values are undefined.

gentype min(gentype x, gentype y)
gentypef min(gentypef x, float y)
gentyped min(gentyped x, double y)

gentypeh min(gentypeh x, half y)

Returns y if y < x, otherwise it returns x. If x or y are infinite or NaN, the return values are undefined.

gentype mix(gentype x, gentype y, gentype a)
gentypef mix(gentypef x, gentypef y, float a)
gentyped mix(gentyped x, gentyped y, double a)

gentypeh mix(gentypeh x, gentypeh y, half a)

Returns the linear blend of x and y implemented as:

x + (y - x) * a

a must be a value in the range [0.0, 1.0]. If a is not in the range [0.0, 1.0], the return values are undefined.

The half-precision mix function can be implemented using contractions such as mad or fma.

gentype radians(gentype degrees)

Converts degrees to radians, i.e. (π / 180) * degrees.

gentype step(gentype edge, gentype x)
gentypef step(float edge, gentypef x)
gentyped step(double edge, gentyped x)

gentypeh step(half edge, gentypeh x)

Returns 0.0 if x < edge, otherwise it returns 1.0.

gentype smoothstep(gentype edge0, gentype edge1, gentype x)
gentypef smoothstep(float edge0, float edge1, gentypef x)
gentyped smoothstep(double edge0, double edge1, gentyped x)

gentypeh smoothstep(half edge0, half edge1, gentypeh x)

Returns 0.0 if x <= edge0 and 1.0 if x >= edge1 and performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1. This is useful in cases where you would want a threshold function with a smooth transition.

This is equivalent to:

gentype t;
t = clamp ((x - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);

Results are undefined if edge0 >= edge1 or if x, edge0 or edge1 is a NaN.

The half-precision mix function can be implemented using contractions such as mad or fma.

gentype sign(gentype x)

Returns 1.0 if x > 0, -0.0 if x = -0.0, +0.0 if x = +0.0, or -1.0 if x < 0. Returns 0.0 if x is a NaN.

See Also

Document Notes

For more information, see the OpenCL C Specification

This page is extracted from the OpenCL C Specification. Fixes and changes should be made to the Specification, not directly.

Copyright 2014-2025 The Khronos Group Inc.

SPDX-License-Identifier: CC-BY-4.0


1. Only if double precision is supported. In OpenCL C 3.0 this will be indicated by the presence of the __opencl_c_fp64 feature macro.
2. Only if the cl_khr_fp16 extension is supported and has been enabled.
3. Only if double precision is supported. In OpenCL C 3.0 this will be indicated by the presence of the __opencl_c_fp64 feature macro.
4. Only if the cl_khr_fp16 extension is supported and has been enabled.