https://github.com/frasercrmck updated https://github.com/llvm/llvm-project/pull/139701
>From 0b08b0e3fe0befc2c10b66bd4c8efdab0f805e1a Mon Sep 17 00:00:00 2001 From: Fraser Cormack <fra...@codeplay.com> Date: Mon, 14 Apr 2025 10:06:08 +0100 Subject: [PATCH 1/2] [libclc] Move (fast) length & distance to CLC library 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. --- .../clc/include/clc/geometric/binary_decl.inc | 16 +++ .../clc/include/clc/geometric/binary_def.inc | 24 ++++ .../clc/include/clc/geometric/clc_distance.h | 19 ++++ libclc/clc/include/clc/geometric/clc_dot.h | 8 +- .../include/clc/geometric/clc_fast_distance.h | 21 ++++ .../include/clc/geometric/clc_fast_length.h | 21 ++++ .../include/clc/geometric/clc_length.h} | 12 +- libclc/clc/include/clc/geometric/floatn.inc | 98 ---------------- .../clc/include/clc/geometric/unary_decl.inc | 21 ++++ .../clc/include/clc/geometric/unary_def.inc | 29 +++++ libclc/clc/lib/generic/SOURCES | 4 + .../generic/geometric/clc_distance.cl} | 6 +- .../lib/generic/geometric/clc_distance.inc} | 10 +- .../generic/geometric/clc_fast_distance.cl} | 7 +- .../generic/geometric/clc_fast_distance.inc} | 10 +- .../lib/generic/geometric/clc_fast_length.cl | 28 +++++ .../lib/generic/geometric/clc_length.cl} | 9 +- .../clc/lib/generic/geometric/clc_length.inc | 60 ++++++++++ .../generic/include/clc/geometric/distance.h | 8 +- libclc/generic/include/clc/geometric/dot.h | 8 +- .../include/clc/geometric/fast_distance.h | 8 +- .../include/clc/geometric/fast_length.h | 8 +- .../include/clc/geometric/fast_length.inc | 9 -- .../include/clc/geometric/fast_normalize.h | 10 +- .../include/clc/geometric/fast_normalize.inc | 9 -- libclc/generic/include/clc/geometric/length.h | 8 +- .../generic/include/clc/geometric/length.inc | 9 -- .../generic/include/clc/geometric/normalize.h | 10 +- .../include/clc/geometric/normalize.inc | 9 -- libclc/generic/lib/geometric/distance.cl | 6 +- libclc/generic/lib/geometric/fast_distance.cl | 7 +- libclc/generic/lib/geometric/fast_length.cl | 20 +--- .../generic/lib/geometric/fast_normalize.cl | 2 +- .../generic/lib/geometric/fast_normalize.inc | 10 +- libclc/generic/lib/geometric/length.cl | 105 +----------------- 35 files changed, 365 insertions(+), 284 deletions(-) create mode 100644 libclc/clc/include/clc/geometric/binary_decl.inc create mode 100644 libclc/clc/include/clc/geometric/binary_def.inc create mode 100644 libclc/clc/include/clc/geometric/clc_distance.h create mode 100644 libclc/clc/include/clc/geometric/clc_fast_distance.h create mode 100644 libclc/clc/include/clc/geometric/clc_fast_length.h rename libclc/{generic/include/clc/geometric/dot.inc => clc/include/clc/geometric/clc_length.h} (58%) delete mode 100644 libclc/clc/include/clc/geometric/floatn.inc create mode 100644 libclc/clc/include/clc/geometric/unary_decl.inc create mode 100644 libclc/clc/include/clc/geometric/unary_def.inc rename libclc/clc/{include/clc/geometric/clc_dot.inc => lib/generic/geometric/clc_distance.cl} (72%) rename libclc/{generic/lib/geometric/fast_distance.inc => clc/lib/generic/geometric/clc_distance.inc} (52%) rename libclc/{generic/include/clc/geometric/fast_distance.inc => clc/lib/generic/geometric/clc_fast_distance.cl} (68%) rename libclc/{generic/lib/geometric/distance.inc => clc/lib/generic/geometric/clc_fast_distance.inc} (56%) create mode 100644 libclc/clc/lib/generic/geometric/clc_fast_length.cl rename libclc/{generic/include/clc/geometric/distance.inc => clc/lib/generic/geometric/clc_length.cl} (61%) create mode 100644 libclc/clc/lib/generic/geometric/clc_length.inc delete mode 100644 libclc/generic/include/clc/geometric/fast_length.inc delete mode 100644 libclc/generic/include/clc/geometric/fast_normalize.inc delete mode 100644 libclc/generic/include/clc/geometric/length.inc delete mode 100644 libclc/generic/include/clc/geometric/normalize.inc diff --git a/libclc/clc/include/clc/geometric/binary_decl.inc b/libclc/clc/include/clc/geometric/binary_decl.inc new file mode 100644 index 0000000000000..3f3aafdeae6c3 --- /dev/null +++ b/libclc/clc/include/clc/geometric/binary_decl.inc @@ -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 diff --git a/libclc/clc/include/clc/geometric/binary_def.inc b/libclc/clc/include/clc/geometric/binary_def.inc new file mode 100644 index 0000000000000..8fc875a052210 --- /dev/null +++ b/libclc/clc/include/clc/geometric/binary_def.inc @@ -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 diff --git a/libclc/clc/include/clc/geometric/clc_distance.h b/libclc/clc/include/clc/geometric/clc_distance.h new file mode 100644 index 0000000000000..1acea411a08b6 --- /dev/null +++ b/libclc/clc/include/clc/geometric/clc_distance.h @@ -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__ diff --git a/libclc/clc/include/clc/geometric/clc_dot.h b/libclc/clc/include/clc/geometric/clc_dot.h index 7ef9ccab3771e..a5a97d1bdd2bc 100644 --- a/libclc/clc/include/clc/geometric/clc_dot.h +++ b/libclc/clc/include/clc/geometric/clc_dot.h @@ -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__ diff --git a/libclc/clc/include/clc/geometric/clc_fast_distance.h b/libclc/clc/include/clc/geometric/clc_fast_distance.h new file mode 100644 index 0000000000000..489bea4299516 --- /dev/null +++ b/libclc/clc/include/clc/geometric/clc_fast_distance.h @@ -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__ diff --git a/libclc/clc/include/clc/geometric/clc_fast_length.h b/libclc/clc/include/clc/geometric/clc_fast_length.h new file mode 100644 index 0000000000000..40552634fff94 --- /dev/null +++ b/libclc/clc/include/clc/geometric/clc_fast_length.h @@ -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__ diff --git a/libclc/generic/include/clc/geometric/dot.inc b/libclc/clc/include/clc/geometric/clc_length.h similarity index 58% rename from libclc/generic/include/clc/geometric/dot.inc rename to libclc/clc/include/clc/geometric/clc_length.h index de3882b2f50db..f8ce12db07871 100644 --- a/libclc/generic/include/clc/geometric/dot.inc +++ b/libclc/clc/include/clc/geometric/clc_length.h @@ -6,4 +6,14 @@ // //===----------------------------------------------------------------------===// -_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT dot(__CLC_FLOATN p0, __CLC_FLOATN p1); +#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__ diff --git a/libclc/clc/include/clc/geometric/floatn.inc b/libclc/clc/include/clc/geometric/floatn.inc deleted file mode 100644 index cc7ee248efd4d..0000000000000 --- a/libclc/clc/include/clc/geometric/floatn.inc +++ /dev/null @@ -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 diff --git a/libclc/clc/include/clc/geometric/unary_decl.inc b/libclc/clc/include/clc/geometric/unary_decl.inc new file mode 100644 index 0000000000000..6da16d2e8abf8 --- /dev/null +++ b/libclc/clc/include/clc/geometric/unary_decl.inc @@ -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 diff --git a/libclc/clc/include/clc/geometric/unary_def.inc b/libclc/clc/include/clc/geometric/unary_def.inc new file mode 100644 index 0000000000000..5bedefb662765 --- /dev/null +++ b/libclc/clc/include/clc/geometric/unary_def.inc @@ -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 diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES index 2fffc5bbe7afd..16f1c0fc6998e 100644 --- a/libclc/clc/lib/generic/SOURCES +++ b/libclc/clc/lib/generic/SOURCES @@ -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 diff --git a/libclc/clc/include/clc/geometric/clc_dot.inc b/libclc/clc/lib/generic/geometric/clc_distance.cl similarity index 72% rename from libclc/clc/include/clc/geometric/clc_dot.inc rename to libclc/clc/lib/generic/geometric/clc_distance.cl index 8636f687ec7cb..54bd437fe2268 100644 --- a/libclc/clc/include/clc/geometric/clc_dot.inc +++ b/libclc/clc/lib/generic/geometric/clc_distance.cl @@ -6,4 +6,8 @@ // //===----------------------------------------------------------------------===// -_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT __clc_dot(__CLC_FLOATN p0, __CLC_FLOATN p1); +#include <clc/internal/clc.h> +#include <clc/geometric/clc_length.h> + +#define __CLC_BODY <clc_distance.inc> +#include <clc/math/gentype.inc> diff --git a/libclc/generic/lib/geometric/fast_distance.inc b/libclc/clc/lib/generic/geometric/clc_distance.inc similarity index 52% rename from libclc/generic/lib/geometric/fast_distance.inc rename to libclc/clc/lib/generic/geometric/clc_distance.inc index c7c4f0d442263..89f74a6233afd 100644 --- a/libclc/generic/lib/geometric/fast_distance.inc +++ b/libclc/clc/lib/generic/geometric/clc_distance.inc @@ -6,6 +6,12 @@ // //===----------------------------------------------------------------------===// -_CLC_OVERLOAD _CLC_DEF __CLC_FLOAT fast_distance(__CLC_FLOATN p0, __CLC_FLOATN p1) { - return fast_length(p0 - p1); +#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 diff --git a/libclc/generic/include/clc/geometric/fast_distance.inc b/libclc/clc/lib/generic/geometric/clc_fast_distance.cl similarity index 68% rename from libclc/generic/include/clc/geometric/fast_distance.inc rename to libclc/clc/lib/generic/geometric/clc_fast_distance.cl index 118fb5c1e65a3..bf74f93c535bc 100644 --- a/libclc/generic/include/clc/geometric/fast_distance.inc +++ b/libclc/clc/lib/generic/geometric/clc_fast_distance.cl @@ -6,4 +6,9 @@ // //===----------------------------------------------------------------------===// -_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT fast_distance(__CLC_FLOATN p0, __CLC_FLOATN p1); +#include <clc/internal/clc.h> +#include <clc/geometric/clc_fast_length.h> + +#define __FLOAT_ONLY +#define __CLC_BODY <clc_fast_distance.inc> +#include <clc/math/gentype.inc> diff --git a/libclc/generic/lib/geometric/distance.inc b/libclc/clc/lib/generic/geometric/clc_fast_distance.inc similarity index 56% rename from libclc/generic/lib/geometric/distance.inc rename to libclc/clc/lib/generic/geometric/clc_fast_distance.inc index a65ad896fdd08..68f71d8bb3a7c 100644 --- a/libclc/generic/lib/geometric/distance.inc +++ b/libclc/clc/lib/generic/geometric/clc_fast_distance.inc @@ -6,6 +6,12 @@ // //===----------------------------------------------------------------------===// -_CLC_OVERLOAD _CLC_DEF __CLC_FLOAT distance(__CLC_FLOATN p0, __CLC_FLOATN p1) { - return length(p0 - p1); +#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 diff --git a/libclc/clc/lib/generic/geometric/clc_fast_length.cl b/libclc/clc/lib/generic/geometric/clc_fast_length.cl new file mode 100644 index 0000000000000..ce6ec5283e524 --- /dev/null +++ b/libclc/clc/lib/generic/geometric/clc_fast_length.cl @@ -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)); +} diff --git a/libclc/generic/include/clc/geometric/distance.inc b/libclc/clc/lib/generic/geometric/clc_length.cl similarity index 61% rename from libclc/generic/include/clc/geometric/distance.inc rename to libclc/clc/lib/generic/geometric/clc_length.cl index 713586079796e..d9a9166b92ae7 100644 --- a/libclc/generic/include/clc/geometric/distance.inc +++ b/libclc/clc/lib/generic/geometric/clc_length.cl @@ -6,4 +6,11 @@ // //===----------------------------------------------------------------------===// -_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT distance(__CLC_FLOATN p0, __CLC_FLOATN p1); +#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> diff --git a/libclc/clc/lib/generic/geometric/clc_length.inc b/libclc/clc/lib/generic/geometric/clc_length.inc new file mode 100644 index 0000000000000..f8d611c778aae --- /dev/null +++ b/libclc/clc/lib/generic/geometric/clc_length.inc @@ -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 diff --git a/libclc/generic/include/clc/geometric/distance.h b/libclc/generic/include/clc/geometric/distance.h index 5101ee76ee878..28ec8274302ef 100644 --- a/libclc/generic/include/clc/geometric/distance.h +++ b/libclc/generic/include/clc/geometric/distance.h @@ -6,5 +6,9 @@ // //===----------------------------------------------------------------------===// -#define __CLC_BODY <clc/geometric/distance.inc> -#include <clc/geometric/floatn.inc> +#define __CLC_FUNCTION distance +#define __CLC_BODY <clc/geometric/binary_decl.inc> + +#include <clc/math/gentype.inc> + +#undef __CLC_FUNCTION diff --git a/libclc/generic/include/clc/geometric/dot.h b/libclc/generic/include/clc/geometric/dot.h index 95141d0d97dab..92d5b56d2e067 100644 --- a/libclc/generic/include/clc/geometric/dot.h +++ b/libclc/generic/include/clc/geometric/dot.h @@ -6,5 +6,9 @@ // //===----------------------------------------------------------------------===// -#define __CLC_BODY <clc/geometric/dot.inc> -#include <clc/geometric/floatn.inc> +#define __CLC_FUNCTION dot +#define __CLC_BODY <clc/geometric/binary_decl.inc> + +#include <clc/math/gentype.inc> + +#undef __CLC_FUNCTION diff --git a/libclc/generic/include/clc/geometric/fast_distance.h b/libclc/generic/include/clc/geometric/fast_distance.h index c9ac8eb47a14c..ef401fdf7289f 100644 --- a/libclc/generic/include/clc/geometric/fast_distance.h +++ b/libclc/generic/include/clc/geometric/fast_distance.h @@ -6,7 +6,11 @@ // //===----------------------------------------------------------------------===// -#define __CLC_BODY <clc/geometric/fast_distance.inc> #define __FLOAT_ONLY -#include <clc/geometric/floatn.inc> +#define __CLC_FUNCTION fast_distance +#define __CLC_BODY <clc/geometric/binary_decl.inc> + +#include <clc/math/gentype.inc> + #undef __FLOAT_ONLY +#undef __CLC_FUNCTION diff --git a/libclc/generic/include/clc/geometric/fast_length.h b/libclc/generic/include/clc/geometric/fast_length.h index df658929999a8..b6fba6a8ca30c 100644 --- a/libclc/generic/include/clc/geometric/fast_length.h +++ b/libclc/generic/include/clc/geometric/fast_length.h @@ -6,7 +6,11 @@ // //===----------------------------------------------------------------------===// -#define __CLC_BODY <clc/geometric/fast_length.inc> #define __FLOAT_ONLY -#include <clc/geometric/floatn.inc> +#define __CLC_FUNCTION fast_length +#define __CLC_BODY <clc/geometric/unary_decl.inc> + +#include <clc/math/gentype.inc> + #undef __FLOAT_ONLY +#undef __CLC_FUNCTION diff --git a/libclc/generic/include/clc/geometric/fast_length.inc b/libclc/generic/include/clc/geometric/fast_length.inc deleted file mode 100644 index beb4b2fe75b86..0000000000000 --- a/libclc/generic/include/clc/geometric/fast_length.inc +++ /dev/null @@ -1,9 +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 -// -//===----------------------------------------------------------------------===// - -_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT fast_length(__CLC_FLOATN p0); diff --git a/libclc/generic/include/clc/geometric/fast_normalize.h b/libclc/generic/include/clc/geometric/fast_normalize.h index a249ca3d01b8c..1af81c66973b7 100644 --- a/libclc/generic/include/clc/geometric/fast_normalize.h +++ b/libclc/generic/include/clc/geometric/fast_normalize.h @@ -6,7 +6,13 @@ // //===----------------------------------------------------------------------===// -#define __CLC_BODY <clc/geometric/fast_normalize.inc> #define __FLOAT_ONLY -#include <clc/geometric/floatn.inc> +#define __CLC_FUNCTION fast_normalize +#define __CLC_GEOMETRIC_RET_GENTYPE +#define __CLC_BODY <clc/geometric/unary_decl.inc> + +#include <clc/math/gentype.inc> + +#undef __CLC_FUNCTION +#undef __CLC_GEOMETRIC_RET_GENTYPE #undef __FLOAT_ONLY diff --git a/libclc/generic/include/clc/geometric/fast_normalize.inc b/libclc/generic/include/clc/geometric/fast_normalize.inc deleted file mode 100644 index b0f4111937ad0..0000000000000 --- a/libclc/generic/include/clc/geometric/fast_normalize.inc +++ /dev/null @@ -1,9 +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 -// -//===----------------------------------------------------------------------===// - -_CLC_OVERLOAD _CLC_DECL __CLC_FLOATN fast_normalize(__CLC_FLOATN p); diff --git a/libclc/generic/include/clc/geometric/length.h b/libclc/generic/include/clc/geometric/length.h index 2ee860fd2aa64..2105a8f8cece0 100644 --- a/libclc/generic/include/clc/geometric/length.h +++ b/libclc/generic/include/clc/geometric/length.h @@ -6,5 +6,9 @@ // //===----------------------------------------------------------------------===// -#define __CLC_BODY <clc/geometric/length.inc> -#include <clc/geometric/floatn.inc> +#define __CLC_FUNCTION length +#define __CLC_BODY <clc/geometric/unary_decl.inc> + +#include <clc/math/gentype.inc> + +#undef __CLC_FUNCTION diff --git a/libclc/generic/include/clc/geometric/length.inc b/libclc/generic/include/clc/geometric/length.inc deleted file mode 100644 index 5b563fd343d98..0000000000000 --- a/libclc/generic/include/clc/geometric/length.inc +++ /dev/null @@ -1,9 +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 -// -//===----------------------------------------------------------------------===// - -_CLC_OVERLOAD _CLC_DECL __CLC_FLOAT length(__CLC_FLOATN p0); diff --git a/libclc/generic/include/clc/geometric/normalize.h b/libclc/generic/include/clc/geometric/normalize.h index c9a499bb31e7e..a15b7003329b5 100644 --- a/libclc/generic/include/clc/geometric/normalize.h +++ b/libclc/generic/include/clc/geometric/normalize.h @@ -6,5 +6,11 @@ // //===----------------------------------------------------------------------===// -#define __CLC_BODY <clc/geometric/normalize.inc> -#include <clc/geometric/floatn.inc> +#define __CLC_FUNCTION normalize +#define __CLC_GEOMETRIC_RET_GENTYPE +#define __CLC_BODY <clc/geometric/unary_decl.inc> + +#include <clc/math/gentype.inc> + +#undef __CLC_GEOMETRIC_RET_GENTYPE +#undef __CLC_FUNCTION diff --git a/libclc/generic/include/clc/geometric/normalize.inc b/libclc/generic/include/clc/geometric/normalize.inc deleted file mode 100644 index a592d8a721cd4..0000000000000 --- a/libclc/generic/include/clc/geometric/normalize.inc +++ /dev/null @@ -1,9 +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 -// -//===----------------------------------------------------------------------===// - -_CLC_OVERLOAD _CLC_DECL __CLC_FLOATN normalize(__CLC_FLOATN p); diff --git a/libclc/generic/lib/geometric/distance.cl b/libclc/generic/lib/geometric/distance.cl index 2101e5071fbad..2416ad1352aaf 100644 --- a/libclc/generic/lib/geometric/distance.cl +++ b/libclc/generic/lib/geometric/distance.cl @@ -7,6 +7,8 @@ //===----------------------------------------------------------------------===// #include <clc/clc.h> +#include <clc/geometric/clc_distance.h> -#define __CLC_BODY <distance.inc> -#include <clc/geometric/floatn.inc> +#define FUNCTION distance +#define __CLC_BODY <clc/geometric/binary_def.inc> +#include <clc/math/gentype.inc> diff --git a/libclc/generic/lib/geometric/fast_distance.cl b/libclc/generic/lib/geometric/fast_distance.cl index 5c1cedf95f1d4..589c79496506e 100644 --- a/libclc/generic/lib/geometric/fast_distance.cl +++ b/libclc/generic/lib/geometric/fast_distance.cl @@ -7,8 +7,9 @@ //===----------------------------------------------------------------------===// #include <clc/clc.h> +#include <clc/geometric/clc_fast_distance.h> -#define __CLC_BODY <fast_distance.inc> #define __FLOAT_ONLY -#include <clc/geometric/floatn.inc> -#undef __FLOAT_ONLY +#define FUNCTION fast_distance +#define __CLC_BODY <clc/geometric/binary_def.inc> +#include <clc/math/gentype.inc> diff --git a/libclc/generic/lib/geometric/fast_length.cl b/libclc/generic/lib/geometric/fast_length.cl index 53a1a1e84a8d1..11e8172998053 100644 --- a/libclc/generic/lib/geometric/fast_length.cl +++ b/libclc/generic/lib/geometric/fast_length.cl @@ -7,19 +7,9 @@ //===----------------------------------------------------------------------===// #include <clc/clc.h> +#include <clc/geometric/clc_fast_length.h> -_CLC_OVERLOAD _CLC_DEF float fast_length(float p) { - return fabs(p); -} - -_CLC_OVERLOAD _CLC_DEF float fast_length(float2 p) { - return half_sqrt(dot(p, p)); -} - -_CLC_OVERLOAD _CLC_DEF float fast_length(float3 p) { - return half_sqrt(dot(p, p)); -} - -_CLC_OVERLOAD _CLC_DEF float fast_length(float4 p) { - return half_sqrt(dot(p, p)); -} +#define __FLOAT_ONLY +#define FUNCTION fast_length +#define __CLC_BODY <clc/geometric/unary_def.inc> +#include <clc/math/gentype.inc> diff --git a/libclc/generic/lib/geometric/fast_normalize.cl b/libclc/generic/lib/geometric/fast_normalize.cl index fddcd3f8f5e42..585400d2200fd 100644 --- a/libclc/generic/lib/geometric/fast_normalize.cl +++ b/libclc/generic/lib/geometric/fast_normalize.cl @@ -14,5 +14,5 @@ _CLC_OVERLOAD _CLC_DEF float fast_normalize(float p) { #define __CLC_BODY <fast_normalize.inc> #define __FLOAT_ONLY -#include <clc/geometric/floatn.inc> +#include <clc/math/gentype.inc> #undef __FLOAT_ONLY diff --git a/libclc/generic/lib/geometric/fast_normalize.inc b/libclc/generic/lib/geometric/fast_normalize.inc index f4f8c9f1f5c92..ec1be94efc279 100644 --- a/libclc/generic/lib/geometric/fast_normalize.inc +++ b/libclc/generic/lib/geometric/fast_normalize.inc @@ -6,11 +6,13 @@ // //===----------------------------------------------------------------------===// -#ifndef __CLC_SCALAR - +// Geometric functions are only defined for scalar, vec2, vec3 and vec4 // Only handle vector implementations -_CLC_OVERLOAD _CLC_DEF __CLC_FLOATN fast_normalize(__CLC_FLOATN p) { - __CLC_FLOAT l2 = dot(p, p); +#if (__CLC_VECSIZE_OR_1 == 2 || __CLC_VECSIZE_OR_1 == 3 || \ + __CLC_VECSIZE_OR_1 == 4) + +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE fast_normalize(__CLC_GENTYPE p) { + __CLC_SCALAR_GENTYPE l2 = dot(p, p); return l2 == 0.0f ? p : p * half_rsqrt(l2); } diff --git a/libclc/generic/lib/geometric/length.cl b/libclc/generic/lib/geometric/length.cl index 93db505b740cd..3a4e286e9a0fb 100644 --- a/libclc/generic/lib/geometric/length.cl +++ b/libclc/generic/lib/geometric/length.cl @@ -7,105 +7,8 @@ //===----------------------------------------------------------------------===// #include <clc/clc.h> +#include <clc/geometric/clc_length.h> -_CLC_OVERLOAD _CLC_DEF float length(float p) { - return fabs(p); -} - -#define V_FLENGTH(p) \ - float l2 = dot(p, p); \ - \ - if (l2 < FLT_MIN) { \ - p *= 0x1.0p+86F; \ - return sqrt(dot(p, p)) * 0x1.0p-86F; \ - } else if (l2 == INFINITY) { \ - p *= 0x1.0p-65F; \ - return sqrt(dot(p, p)) * 0x1.0p+65F; \ - } \ - \ - return sqrt(l2); - -_CLC_OVERLOAD _CLC_DEF float length(float2 p) { - V_FLENGTH(p); -} - -_CLC_OVERLOAD _CLC_DEF float length(float3 p) { - V_FLENGTH(p); -} - -_CLC_OVERLOAD _CLC_DEF float length(float4 p) { - V_FLENGTH(p); -} - -#ifdef cl_khr_fp64 -#pragma OPENCL EXTENSION cl_khr_fp64 : enable - -_CLC_OVERLOAD _CLC_DEF double length(double p){ - return fabs(p); -} - -#define V_DLENGTH(p) \ - double l2 = dot(p, p); \ - \ - if (l2 < DBL_MIN) { \ - p *= 0x1.0p+563; \ - return sqrt(dot(p, p)) * 0x1.0p-563; \ - } else if (l2 == INFINITY) { \ - p *= 0x1.0p-513; \ - return sqrt(dot(p, p)) * 0x1.0p+513; \ - } \ - \ - return sqrt(l2); - -_CLC_OVERLOAD _CLC_DEF double length(double2 p) { - V_DLENGTH(p); -} - -_CLC_OVERLOAD _CLC_DEF double length(double3 p) { - V_DLENGTH(p); -} - -_CLC_OVERLOAD _CLC_DEF double length(double4 p) { - V_DLENGTH(p); -} - -#endif - -#ifdef cl_khr_fp16 -#pragma OPENCL EXTENSION cl_khr_fp16 : enable - -_CLC_OVERLOAD _CLC_DEF half length(half p){ - return fabs(p); -} - -// Only available in CLC1.2 -#ifndef HALF_MIN -#define HALF_MIN 0x1.0p-14h -#endif - -#define V_HLENGTH(p) \ - half l2 = dot(p, p); \ - \ - if (l2 < HALF_MIN) { \ - p *= 0x1.0p+12h; \ - return sqrt(dot(p, p)) * 0x1.0p-12h; \ - } else if (l2 == INFINITY) { \ - p *= 0x1.0p-7h; \ - return sqrt(dot(p, p)) * 0x1.0p+7h; \ - } \ - \ - return sqrt(l2); - -_CLC_OVERLOAD _CLC_DEF half length(half2 p) { - V_HLENGTH(p); -} - -_CLC_OVERLOAD _CLC_DEF half length(half3 p) { - V_HLENGTH(p); -} - -_CLC_OVERLOAD _CLC_DEF half length(half4 p) { - V_HLENGTH(p); -} - -#endif +#define FUNCTION length +#define __CLC_BODY <clc/geometric/unary_def.inc> +#include <clc/math/gentype.inc> >From a862debf68fa2735d853d9f48fc31c0b659fd83e Mon Sep 17 00:00:00 2001 From: Fraser Cormack <fra...@codeplay.com> Date: Tue, 13 May 2025 11:27:16 +0100 Subject: [PATCH 2/2] fix formatting; no else after return --- libclc/clc/lib/generic/geometric/clc_distance.cl | 2 +- libclc/clc/lib/generic/geometric/clc_fast_distance.cl | 2 +- libclc/clc/lib/generic/geometric/clc_length.inc | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/libclc/clc/lib/generic/geometric/clc_distance.cl b/libclc/clc/lib/generic/geometric/clc_distance.cl index 54bd437fe2268..ba22c9e17330a 100644 --- a/libclc/clc/lib/generic/geometric/clc_distance.cl +++ b/libclc/clc/lib/generic/geometric/clc_distance.cl @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#include <clc/internal/clc.h> #include <clc/geometric/clc_length.h> +#include <clc/internal/clc.h> #define __CLC_BODY <clc_distance.inc> #include <clc/math/gentype.inc> diff --git a/libclc/clc/lib/generic/geometric/clc_fast_distance.cl b/libclc/clc/lib/generic/geometric/clc_fast_distance.cl index bf74f93c535bc..996039898480c 100644 --- a/libclc/clc/lib/generic/geometric/clc_fast_distance.cl +++ b/libclc/clc/lib/generic/geometric/clc_fast_distance.cl @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#include <clc/internal/clc.h> #include <clc/geometric/clc_fast_length.h> +#include <clc/internal/clc.h> #define __FLOAT_ONLY #define __CLC_BODY <clc_fast_distance.inc> diff --git a/libclc/clc/lib/generic/geometric/clc_length.inc b/libclc/clc/lib/generic/geometric/clc_length.inc index f8d611c778aae..760373547886f 100644 --- a/libclc/clc/lib/generic/geometric/clc_length.inc +++ b/libclc/clc/lib/generic/geometric/clc_length.inc @@ -43,7 +43,9 @@ _CLC_OVERLOAD _CLC_DEF __CLC_SCALAR_GENTYPE __clc_length(__CLC_GENTYPE 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) { + } + + if (l2 == INFINITY) { p *= __CLC_GENTYPE_INF_SCALE_DOWN; return __clc_sqrt(__clc_dot(p, p)) * __CLC_GENTYPE_INF_SCALE_UP; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits