https://github.com/frasercrmck created https://github.com/llvm/llvm-project/pull/127828
The "generic" unary_(def|decl)_with_ptr files are intended to be re-used by the sincos and fract builtins in the future as they share an identical type signature. >From 09835c009430464426dced5598a5e8a9cae149ba Mon Sep 17 00:00:00 2001 From: Fraser Cormack <fra...@codeplay.com> Date: Wed, 19 Feb 2025 16:25:22 +0000 Subject: [PATCH] [libclc] Move modf to the CLC library The "generic" unary_(def|decl)_with_ptr files are intended to be re-used by the sincos and fract builtins in the future as they share an identical type signature. --- libclc/clc/include/clc/math/clc_modf.h | 11 +++++ .../include/clc/math/unary_decl_with_ptr.inc | 6 +++ .../include/clc/math/unary_def_with_ptr.inc | 20 ++++++++++ libclc/clc/lib/generic/SOURCES | 1 + .../lib/generic/math/clc_modf.cl} | 13 ++++-- libclc/clc/lib/generic/math/clc_modf.inc | 40 +++++++++++++++++++ libclc/generic/include/clc/math/modf.h | 5 ++- libclc/generic/lib/math/modf.cl | 5 ++- libclc/generic/lib/math/modf.inc | 33 +++++---------- 9 files changed, 103 insertions(+), 31 deletions(-) create mode 100644 libclc/clc/include/clc/math/clc_modf.h create mode 100644 libclc/clc/include/clc/math/unary_decl_with_ptr.inc create mode 100644 libclc/clc/include/clc/math/unary_def_with_ptr.inc rename libclc/{generic/include/clc/math/modf.inc => clc/lib/generic/math/clc_modf.cl} (76%) create mode 100644 libclc/clc/lib/generic/math/clc_modf.inc diff --git a/libclc/clc/include/clc/math/clc_modf.h b/libclc/clc/include/clc/math/clc_modf.h new file mode 100644 index 0000000000000..45484b09628a4 --- /dev/null +++ b/libclc/clc/include/clc/math/clc_modf.h @@ -0,0 +1,11 @@ +#ifndef __CLC_MATH_CLC_MODF_H__ +#define __CLC_MATH_CLC_MODF_H__ + +#define __CLC_FUNCTION __clc_modf +#define __CLC_BODY <clc/math/unary_decl_with_ptr.inc> +#include <clc/math/gentype.inc> + +#undef __CLC_BODY +#undef __CLC_FUNCTION + +#endif // __CLC_MATH_CLC_MODF_H__ diff --git a/libclc/clc/include/clc/math/unary_decl_with_ptr.inc b/libclc/clc/include/clc/math/unary_decl_with_ptr.inc new file mode 100644 index 0000000000000..04122108bc1f7 --- /dev/null +++ b/libclc/clc/include/clc/math/unary_decl_with_ptr.inc @@ -0,0 +1,6 @@ +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x, + global __CLC_GENTYPE *ptr); +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x, + local __CLC_GENTYPE *ptr); +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE +__CLC_FUNCTION(__CLC_GENTYPE x, private __CLC_GENTYPE *ptr); diff --git a/libclc/clc/include/clc/math/unary_def_with_ptr.inc b/libclc/clc/include/clc/math/unary_def_with_ptr.inc new file mode 100644 index 0000000000000..de7c9af756980 --- /dev/null +++ b/libclc/clc/include/clc/math/unary_def_with_ptr.inc @@ -0,0 +1,20 @@ +#include <clc/utils.h> + +#ifndef __CLC_FUNCTION +#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x) +#endif + +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x, + private __CLC_GENTYPE *ptr) { + return __CLC_FUNCTION(FUNCTION)(x, ptr); +} + +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x, + global __CLC_GENTYPE *ptr) { + return __CLC_FUNCTION(FUNCTION)(x, ptr); +} + +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x, + local __CLC_GENTYPE *ptr) { + return __CLC_FUNCTION(FUNCTION)(x, ptr); +} diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES index b0eaf84c41438..ef0ad006307d7 100644 --- a/libclc/clc/lib/generic/SOURCES +++ b/libclc/clc/lib/generic/SOURCES @@ -22,6 +22,7 @@ math/clc_copysign.cl math/clc_fabs.cl math/clc_floor.cl math/clc_mad.cl +math/clc_modf.cl math/clc_nextafter.cl math/clc_rint.cl math/clc_trunc.cl diff --git a/libclc/generic/include/clc/math/modf.inc b/libclc/clc/lib/generic/math/clc_modf.cl similarity index 76% rename from libclc/generic/include/clc/math/modf.inc rename to libclc/clc/lib/generic/math/clc_modf.cl index 42bcf625686d2..27d2a08515257 100644 --- a/libclc/generic/include/clc/math/modf.inc +++ b/libclc/clc/lib/generic/math/clc_modf.cl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2015 Advanced Micro Devices, Inc. + * Copyright (c) 2015 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,11 @@ * THE SOFTWARE. */ -_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE modf(__CLC_GENTYPE x, global __CLC_GENTYPE *iptr); -_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE modf(__CLC_GENTYPE x, local __CLC_GENTYPE *iptr); -_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE modf(__CLC_GENTYPE x, private __CLC_GENTYPE *iptr); +#include <clc/internal/clc.h> +#include <clc/math/clc_copysign.h> +#include <clc/math/clc_trunc.h> +#include <clc/math/math.h> +#include <clc/relational/clc_isinf.h> + +#define __CLC_BODY <clc_modf.inc> +#include <clc/math/gentype.inc> diff --git a/libclc/clc/lib/generic/math/clc_modf.inc b/libclc/clc/lib/generic/math/clc_modf.inc new file mode 100644 index 0000000000000..8242291c98d4e --- /dev/null +++ b/libclc/clc/lib/generic/math/clc_modf.inc @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2015 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_modf(__CLC_GENTYPE x, + private __CLC_GENTYPE *iptr) { + *iptr = __clc_trunc(x); + return __clc_copysign(__clc_isinf(x) ? __CLC_FP_LIT(0.0) : x - *iptr, x); +} + +#define CLC_MODF_DEF(addrspace) \ + _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_modf( \ + __CLC_GENTYPE x, addrspace __CLC_GENTYPE *iptr) { \ + __CLC_GENTYPE private_iptr; \ + __CLC_GENTYPE ret = __clc_modf(x, &private_iptr); \ + *iptr = private_iptr; \ + return ret; \ + } + +CLC_MODF_DEF(local); +CLC_MODF_DEF(global); + +#undef CLC_MODF_DEF diff --git a/libclc/generic/include/clc/math/modf.h b/libclc/generic/include/clc/math/modf.h index f0fb6ca81920a..76eb1284432e4 100644 --- a/libclc/generic/include/clc/math/modf.h +++ b/libclc/generic/include/clc/math/modf.h @@ -20,5 +20,8 @@ * THE SOFTWARE. */ -#define __CLC_BODY <clc/math/modf.inc> +#define __CLC_FUNCTION modf +#define __CLC_BODY <clc/math/unary_decl_with_ptr.inc> #include <clc/math/gentype.inc> + +#undef __CLC_FUNCTION diff --git a/libclc/generic/lib/math/modf.cl b/libclc/generic/lib/math/modf.cl index 5098a41d079c5..5a01a316132e2 100644 --- a/libclc/generic/lib/math/modf.cl +++ b/libclc/generic/lib/math/modf.cl @@ -21,7 +21,8 @@ */ #include <clc/clc.h> -#include <clc/math/math.h> +#include <clc/math/clc_modf.h> -#define __CLC_BODY <modf.inc> +#define FUNCTION modf +#define __CLC_BODY <clc/math/unary_def_with_ptr.inc> #include <clc/math/gentype.inc> diff --git a/libclc/generic/lib/math/modf.inc b/libclc/generic/lib/math/modf.inc index ff7ef30dd42f8..be05f77297c15 100644 --- a/libclc/generic/lib/math/modf.inc +++ b/libclc/generic/lib/math/modf.inc @@ -20,30 +20,15 @@ * THE SOFTWARE. */ -#if __CLC_FPSIZE == 64 -#define ZERO 0.0 -#elif __CLC_FPSIZE == 32 -#define ZERO 0.0f -#elif __CLC_FPSIZE == 16 -#define ZERO 0.0h -#endif - _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE modf(__CLC_GENTYPE x, private __CLC_GENTYPE *iptr) { - *iptr = trunc(x); - return copysign(isinf(x) ? ZERO : x - *iptr, x); + return __clc_modf(x, iptr); +} +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE modf(__CLC_GENTYPE x, + global __CLC_GENTYPE *iptr) { + return __clc_modf(x, iptr); +} +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE modf(__CLC_GENTYPE x, + local __CLC_GENTYPE *iptr) { + return __clc_modf(x, iptr); } - -#define MODF_DEF(addrspace) \ - _CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE modf(__CLC_GENTYPE x, \ - addrspace __CLC_GENTYPE *iptr) { \ - __CLC_GENTYPE private_iptr; \ - __CLC_GENTYPE ret = modf(x, &private_iptr); \ - *iptr = private_iptr; \ - return ret; \ - } - -MODF_DEF(local); -MODF_DEF(global); - -#undef ZERO _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits