Description

The following table describes the list of built-in geometric functions.

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

  • float, float2, float3, or float4

as the type for the arguments.

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

  • double, double2, double3, or double4

as the type for the arguments.

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

  • half, half2, half3, or half4

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.

For any specific use of a function with gentype* arguments the actual type has to be the same for all arguments and the return type, unless they are explicitly specified as an actual type.

Table 1. Built-in Scalar and Vector Argument Geometric Functions
Function Description

float4 cross(float4 p0, float4 p1)
float3 cross(float3 p0, float3 p1)
double4 cross(double4 p0, double4 p1)
double3 cross(double3 p0, double3 p1)

half4 cross(half4 p0, half4 p1)
half3 cross(half3 p0, half3 p1)

Returns the cross product of p0.xyz and p1.xyz. The w component of float4 result returned will be 0.0.

float dot(gentypef p0, gentypef p1)
double dot(gentyped p0, gentyped p1)

half dot(gentypeh p0, gentypeh p1)

Compute the dot product of p0 and p1.

float distance(gentypef p0, gentypef p1)
double distance(gentyped p0, gentyped p1)

half distance(gentypeh p0, gentypeh p1)

Returns the distance between p0 and p1. This is calculated as length(p0 - p1).

float length(gentypef p)
double length(gentyped p)

half length(gentypeh p)

Return the length of vector p, i.e., √ p.x2 + p.y 2 + …​

gentypef normalize(gentypef p)
gentyped normalize(gentyped p)

gentypeh normalize(gentypeh p)

Returns a vector in the same direction as p but with a length of 1.

float fast_distance(floatn p0, floatn p1)

Returns fast_length(p0 - p1).

float fast_length(floatn p)

Returns the length of vector p computed as:

half_sqrt(p.x2 + p.y2 + …​)

floatn fast_normalize(floatn p)

Returns a vector in the same direction as p but with a length of 1. fast_normalize is computed as:

p * half_rsqrt(p.x2 + p.y2 + …​)

The result shall be within 8192 ulps error from the infinitely precise result of

if (all(p == 0.0f))
  result = p;
else
  result = p /
    sqrt(p.x*p.x + p.y*p.y + ...);

with the following exceptions:

  1. If the sum of squares is greater than FLT_MAX then the value of the floating-point values in the result vector are undefined.

  2. If the sum of squares is less than FLT_MIN then the implementation may return back p.

  3. If the device is in “denorms are flushed to zero” mode, individual operand elements with magnitude less than sqrt(FLT_MIN) may be flushed to zero before proceeding with the calculation.

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.