Maths Functions¶
The following maths functions are defined on both scalar and vector float types, i.e. float, float1, float2, float3
and float4
.
On vectors the math function is applied to each component element-wise. For example: sin(float2(1.0f, 2.0f))
is equivalent to float2(sin(1.0f), sin(2.0f))
.
When a function takes multiple arguments the same logic is applied to each argument element-wise. See the SpecificationMathsKernel for an example of the following maths functions used in a single kernel.
Trigonometric functions¶
// floatn sin(floatn a);
pixel = sin(pixel);
// floatn cos(floatn a);
pixel = cos(pixel);
// floatn tan(floatn a);
pixel = tan(pixel);
// floatn asin(floatn a);
pixel = asin(pixel);
// floatn acos(floatn a);
pixel = acos(pixel);
// floatn atan(floatn a);
pixel = atan(pixel);
// floatn atan2(floatn a, floatn b);
pixel = atan2(pixel, pixel);
Logarithmic, exponential and power functions¶
// floatn exp(floatn x);
// Returns the natural exponent, e^x.
pixel = exp(pixel);
// floatn log(floatn x);
// Returns the natural log, log x of base e.
pixel = log(pixel);
// floatn log2(floatn x);
// Returns the log x of base 2.
pixel = log2(pixel);
// floatn log10(floatn x);
// Returns the log x of base 10.
pixel = log10(pixel);
// floatn pow(floatn x, floatn a);
// Returns x^a.
pixel = pow(pixel, pixel);
// floatn sqrt(floatn x);
// Returns the positive square root of x.
pixel = sqrt(pixel);
// floatn rsqrt(floatn x);
// Returns the inverse square root of x, i.e. 1.0f/sqrt(x).
pixel = rsqrt(pixel + 1.0f);
Rounding functions¶
// floatn floor(floatn x);
// Returns x rounded to an integer towards negative infinity.
pixel = floor(pixel);
// floatn ceil(floatn x);
// Returns x rounded to an integer towards positive infinity.
pixel = ceil(pixel);
Absolute and sign functions¶
// floatn fabs(floatn x);
// Returns the absolute value of x.
pixel = fabs(pixel);
// floatn sign(floatn x);
// Returns the value of the sign bit of x i.e. 1.0f for positive values,
// -1.0f for negative values and 0.0f for +/-0.0f.
pixel = sign(pixel);
Remainder functions¶
// floatn fmod(floatn x, floatn y);
// Returns the floating point remainder of the division of x / y.
pixel = fmod(pixel, SampleType(dst)(0.5f));
Clamping functions¶
// floatn min(floatn x, floatn y);
pixel = min(pixel, SampleType(dst)(0.5f));
// floatn max(floatn x, floatn y);
pixel = max(pixel, SampleType(dst)(0.5f));
// floatn clamp(floatn x, floatn y, floatn z);
pixel = clamp(pixel, SampleType(dst)(0.0f), SampleType(dst)(1.0f));
Special maths functions¶
These functions don’t follow the standard pattern on vectors like the above functions, but instead compute something bespoke.
// float dot(floatn x, floatn y);
// Returns the dot product of x dot y.
float dotProduct = dot(pixel, pixel);
// float3 cross(float3 x, float3 y);
// Returns the cross product of x cross y.
float3 crossProduct = cross(float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f));
// int length(floatn x);
// Returns the number of elements in a vector i.e. length(float2(1.0f, 2.0f)) -> 2
int len = length(pixel);
// floatn normalize(floatn x);
// Returns the L2-Norm of x.
pixel = normalize(pixel);
// float median(float data[], int size);
// Returns the median value in an array of data of length size.
float arr[6] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
float med = median(arr, 6);