[libclc] Move (fast) length & distance to CLC library (#139701)

This commit also refactors how geometric builtins are defined and
declared, by sharing more helpers. It also removes an unnecessary
gentype-like helper in favour of the more complete math/gentype.inc.

There are no changes to the IR for any of these four builtins.

The 'normalize' builtin will follow in a subsequent commit because it
would involve the addition of missing halfn-type overloads for
completeness.
This commit is contained in:
Fraser Cormack
2025-05-13 11:45:55 +01:00
committed by GitHub
parent 49ee674e5d
commit 655151a7e0
35 changed files with 365 additions and 284 deletions

View File

@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DECL __CLC_SCALAR_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE a,
__CLC_GENTYPE b);
#endif

View File

@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/utils.h>
#ifndef __CLC_FUNCTION
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
#endif
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE FUNCTION(__CLC_GENTYPE a,
__CLC_GENTYPE b) {
return __CLC_FUNCTION(FUNCTION)(a, b);
}
#endif

View File

@@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_GEOMETRIC_CLC_DISTANCE_H__
#define __CLC_GEOMETRIC_CLC_DISTANCE_H__
#define __CLC_FUNCTION __clc_distance
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_DISTANCE_H__

View File

@@ -9,7 +9,11 @@
#ifndef __CLC_GEOMETRIC_CLC_DOT_H__
#define __CLC_GEOMETRIC_CLC_DOT_H__
#define __CLC_BODY <clc/geometric/clc_dot.inc>
#include <clc/geometric/floatn.inc>
#define __CLC_FUNCTION __clc_dot
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_DOT_H__

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__
#define __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__
#define __FLOAT_ONLY
#define __CLC_FUNCTION __clc_fast_distance
#define __CLC_BODY <clc/geometric/binary_decl.inc>
#include <clc/math/gentype.inc>
#undef __FLOAT_ONLY
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_FAST_DISTANCE_H__

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__
#define __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__
#define __FLOAT_ONLY
#define __CLC_FUNCTION __clc_fast_length
#define __CLC_BODY <clc/geometric/unary_decl.inc>
#include <clc/math/gentype.inc>
#undef __FLOAT_ONLY
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_FAST_LENGTH_H__

View File

@@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef __CLC_GEOMETRIC_CLC_LENGTH_H__
#define __CLC_GEOMETRIC_CLC_LENGTH_H__
#define __CLC_FUNCTION __clc_length
#define __CLC_BODY <clc/geometric/unary_decl.inc>
#include <clc/math/gentype.inc>
#undef __CLC_FUNCTION
#endif // __CLC_GEOMETRIC_CLC_LENGTH_H__

View File

@@ -1,98 +0,0 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/clcfunc.h>
#include <clc/clctypes.h>
#define __CLC_FLOAT float
#define __CLC_FPSIZE 32
#define __CLC_FLOATN float
#define __CLC_SCALAR
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_SCALAR
#define __CLC_FLOATN float2
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN float3
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN float4
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_FLOAT
#undef __CLC_FPSIZE
#ifndef __FLOAT_ONLY
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#define __CLC_FLOAT double
#define __CLC_FPSIZE 64
#define __CLC_FLOATN double
#define __CLC_SCALAR
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_SCALAR
#define __CLC_FLOATN double2
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN double3
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN double4
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_FLOAT
#undef __CLC_FPSIZE
#endif
#endif
#ifndef __FLOAT_ONLY
#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
#define __CLC_FLOAT half
#define __CLC_FPSIZE 16
#define __CLC_FLOATN half
#define __CLC_SCALAR
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_SCALAR
#define __CLC_FLOATN half2
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN half3
#include __CLC_BODY
#undef __CLC_FLOATN
#define __CLC_FLOATN half4
#include __CLC_BODY
#undef __CLC_FLOATN
#undef __CLC_FLOAT
#undef __CLC_FPSIZE
#endif
#endif
#undef __CLC_BODY

View File

@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DECL
#ifdef __CLC_GEOMETRIC_RET_GENTYPE
__CLC_GENTYPE
#else
__CLC_SCALAR_GENTYPE
#endif
__CLC_FUNCTION(__CLC_GENTYPE a);
#endif

View File

@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/utils.h>
#ifndef __CLC_FUNCTION
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
#endif
// Geometric functions are only defined for scalar, vec2, vec3 and vec4
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF
#ifdef __CLC_GEOMETRIC_RET_GENTYPE
__CLC_GENTYPE
#else
__CLC_SCALAR_GENTYPE
#endif
FUNCTION(__CLC_GENTYPE a) {
return __CLC_FUNCTION(FUNCTION)(a);
}
#endif

View File

@@ -2,7 +2,11 @@ common/clc_degrees.cl
common/clc_radians.cl
common/clc_sign.cl
common/clc_smoothstep.cl
geometric/clc_distance.cl
geometric/clc_dot.cl
geometric/clc_fast_distance.cl
geometric/clc_fast_length.cl
geometric/clc_length.cl
integer/clc_abs.cl
integer/clc_abs_diff.cl
integer/clc_add_sat.cl

View File

@@ -6,4 +6,8 @@
//
//===----------------------------------------------------------------------===//
_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT __clc_dot(__CLC_FLOATN p0, __CLC_FLOATN p1);
#include <clc/geometric/clc_length.h>
#include <clc/internal/clc.h>
#define __CLC_BODY <clc_distance.inc>
#include <clc/math/gentype.inc>

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE __clc_distance(__CLC_GENTYPE p0,
__CLC_GENTYPE p1) {
return __clc_length(p0 - p1);
}
#endif

View File

@@ -0,0 +1,14 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/geometric/clc_fast_length.h>
#include <clc/internal/clc.h>
#define __FLOAT_ONLY
#define __CLC_BODY <clc_fast_distance.inc>
#include <clc/math/gentype.inc>

View File

@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#if (__CLC_VECSIZE_OR_1 == 1 || __CLC_VECSIZE_OR_1 == 2 || \
__CLC_VECSIZE_OR_1 == 3 || __CLC_VECSIZE_OR_1 == 4)
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE
__clc_fast_distance(__CLC_GENTYPE p0, __CLC_GENTYPE p1) {
return __clc_fast_length(p0 - p1);
}
#endif

View File

@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/geometric/clc_dot.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_fabs.h>
#include <clc/math/clc_half_sqrt.h>
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float p) {
return __clc_fabs(p);
}
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float2 p) {
return __clc_half_sqrt(__clc_dot(p, p));
}
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float3 p) {
return __clc_half_sqrt(__clc_dot(p, p));
}
_CLC_OVERLOAD _CLC_DEF float __clc_fast_length(float4 p) {
return __clc_half_sqrt(__clc_dot(p, p));
}

View File

@@ -0,0 +1,16 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <clc/float/definitions.h>
#include <clc/geometric/clc_dot.h>
#include <clc/internal/clc.h>
#include <clc/math/clc_fabs.h>
#include <clc/math/clc_sqrt.h>
#define __CLC_BODY <clc_length.inc>
#include <clc/math/gentype.inc>

View File

@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#if __CLC_VECSIZE_OR_1 == 1
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_length(__CLC_GENTYPE p) {
return __clc_fabs(p);
}
#elif (__CLC_VECSIZE_OR_1 == 2 || __CLC_VECSIZE_OR_1 == 3 || \
__CLC_VECSIZE_OR_1 == 4)
#if __CLC_FPSIZE == 32
#define __CLC_GENTYPE_MIN FLT_MIN
#define __CLC_GENTYPE_MIN_SCALE_UP 0x1.0p+86F
#define __CLC_GENTYPE_MIN_SCALE_DOWN 0x1.0p-86F
#define __CLC_GENTYPE_INF_SCALE_UP 0x1.0p+65F
#define __CLC_GENTYPE_INF_SCALE_DOWN 0x1.0p-65F
#elif __CLC_FPSIZE == 64
#define __CLC_GENTYPE_MIN DBL_MIN
#define __CLC_GENTYPE_MIN_SCALE_UP 0x1.0p+563
#define __CLC_GENTYPE_MIN_SCALE_DOWN 0x1.0p-563
#define __CLC_GENTYPE_INF_SCALE_UP 0x1.0p+513
#define __CLC_GENTYPE_INF_SCALE_DOWN 0x1.0p-513
#elif __CLC_FPSIZE == 16
#define __CLC_GENTYPE_MIN HALF_MIN
#define __CLC_GENTYPE_MIN_SCALE_UP 0x1.0p+12H
#define __CLC_GENTYPE_MIN_SCALE_DOWN 0x1.0p-12H
#define __CLC_GENTYPE_INF_SCALE_UP 0x1.0p+7H
#define __CLC_GENTYPE_INF_SCALE_DOWN 0x1.0p-7H
#else
#error "Unsupported FP size"
#endif
_CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE __clc_length(__CLC_GENTYPE p) {
__CLC_SCALAR_GENTYPE l2 = __clc_dot(p, p);
if (l2 < __CLC_GENTYPE_MIN) {
p *= __CLC_GENTYPE_MIN_SCALE_UP;
return __clc_sqrt(__clc_dot(p, p)) * __CLC_GENTYPE_MIN_SCALE_DOWN;
} else if (l2 == INFINITY) {
p *= __CLC_GENTYPE_INF_SCALE_DOWN;
return __clc_sqrt(__clc_dot(p, p)) * __CLC_GENTYPE_INF_SCALE_UP;
}
return __clc_sqrt(l2);
}
#undef __CLC_GENTYPE_INF_SCALE_DOWN
#undef __CLC_GENTYPE_INF_SCALE_UP
#undef __CLC_GENTYPE_MIN_SCALE_DOWN
#undef __CLC_GENTYPE_MIN_SCALE_UP
#undef __CLC_GENTYPE_MIN
#endif