Author: Fraser Cormack Date: 2025-05-12T14:55:27+01:00 New Revision: 87978ea2723d39d3045bfe7624e198389ad35749
URL: https://github.com/llvm/llvm-project/commit/87978ea2723d39d3045bfe7624e198389ad35749 DIFF: https://github.com/llvm/llvm-project/commit/87978ea2723d39d3045bfe7624e198389ad35749.diff LOG: [libclc] Move tan to the CLC library (#139547) There was already a __clc_tan in the OpenCL layer. This commit moves the function over whilst vectorizing it. The function __clc_tan is no longer a public symbol, which should have never been the case. Added: libclc/clc/include/clc/math/clc_tan.h libclc/clc/lib/generic/math/clc_tan.cl libclc/clc/lib/generic/math/clc_tan.inc Modified: libclc/clc/lib/generic/SOURCES libclc/clc/lib/generic/math/clc_sincos_helpers.inc libclc/clspv/lib/SOURCES libclc/generic/lib/SOURCES libclc/generic/lib/math/tan.cl libclc/spirv/lib/SOURCES Removed: libclc/generic/include/math/clc_tan.h libclc/generic/lib/math/clc_sw_unary.inc libclc/generic/lib/math/clc_tan.cl ################################################################################ diff --git a/libclc/generic/include/math/clc_tan.h b/libclc/clc/include/clc/math/clc_tan.h similarity index 83% rename from libclc/generic/include/math/clc_tan.h rename to libclc/clc/include/clc/math/clc_tan.h index 52c0a3440baca..028ff28ecd69d 100644 --- a/libclc/generic/include/math/clc_tan.h +++ b/libclc/clc/include/clc/math/clc_tan.h @@ -6,7 +6,14 @@ // //===----------------------------------------------------------------------===// -#define __CLC_FUNCTION __clc_tan +#ifndef __CLC_MATH_CLC_TAN_H__ +#define __CLC_MATH_CLC_TAN_H__ + #define __CLC_BODY <clc/math/unary_decl.inc> +#define __CLC_FUNCTION __clc_tan + #include <clc/math/gentype.inc> + #undef __CLC_FUNCTION + +#endif // __CLC_MATH_CLC_TAN_H__ diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES index 4d66c749fc53e..0a839e885e391 100644 --- a/libclc/clc/lib/generic/SOURCES +++ b/libclc/clc/lib/generic/SOURCES @@ -95,6 +95,7 @@ math/clc_sinpi.cl math/clc_sqrt.cl math/clc_sw_fma.cl math/clc_tables.cl +math/clc_tan.cl math/clc_tanh.cl math/clc_tanpi.cl math/clc_tgamma.cl diff --git a/libclc/clc/lib/generic/math/clc_sincos_helpers.inc b/libclc/clc/lib/generic/math/clc_sincos_helpers.inc index 516a40c4672a9..29c742136b7e9 100644 --- a/libclc/clc/lib/generic/math/clc_sincos_helpers.inc +++ b/libclc/clc/lib/generic/math/clc_sincos_helpers.inc @@ -90,7 +90,7 @@ _CLC_DECL _CLC_OVERLOAD __CLC_FLOATN __clc_tanf_piby4(__CLC_FLOATN x, __CLC_FLOATN t = __clc_mad(x * r, __clc_native_divide(a, b), x); __CLC_FLOATN tr = -MATH_RECIP(t); - return regn & 1 ? tr : t; + return (regn & 1) != 0 ? tr : t; } _CLC_DEF _CLC_OVERLOAD void __clc_fullMulS(private __CLC_FLOATN *hi, diff --git a/libclc/clc/lib/generic/math/clc_tan.cl b/libclc/clc/lib/generic/math/clc_tan.cl new file mode 100644 index 0000000000000..adf42c43d0484 --- /dev/null +++ b/libclc/clc/lib/generic/math/clc_tan.cl @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// +// 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/clc_convert.h> +#include <clc/float/definitions.h> +#include <clc/internal/clc.h> +#include <clc/math/clc_fabs.h> +#include <clc/math/clc_sincos_helpers.h> +#include <clc/math/clc_sincos_piby4.h> +#include <clc/math/math.h> +#include <clc/math/tables.h> +#include <clc/relational/clc_isinf.h> +#include <clc/relational/clc_isnan.h> +#include <clc/relational/clc_select.h> + +#define __CLC_BODY <clc_tan.inc> +#include <clc/math/gentype.inc> diff --git a/libclc/clc/lib/generic/math/clc_tan.inc b/libclc/clc/lib/generic/math/clc_tan.inc new file mode 100644 index 0000000000000..8a318a53a34ba --- /dev/null +++ b/libclc/clc/lib/generic/math/clc_tan.inc @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// 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_FPSIZE == 32 + +_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_tan(__CLC_GENTYPE x) { + __CLC_GENTYPE absx = __clc_fabs(x); + __CLC_UINTN x_signbit = __CLC_AS_UINTN(x) & SIGNBIT_SP32; + + __CLC_GENTYPE r0, r1; + __CLC_INTN regn = __clc_argReductionS(&r0, &r1, absx); + + __CLC_GENTYPE t = __clc_tanf_piby4(r0 + r1, regn); + t = __CLC_AS_GENTYPE(__CLC_AS_UINTN(t) ^ x_signbit); + + t = __clc_select(t, __CLC_GENTYPE_NAN, __clc_isnan(x) || __clc_isinf(x)); + // Take care of subnormals + t = (x == 0.0f) ? x : t; + return t; +} + +#elif __CLC_FPSIZE == 64 + +_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_tan(__CLC_GENTYPE x) { + __CLC_GENTYPE y = __clc_fabs(x); + + __CLC_BIT_INTN is_medium = y < 0x1.0p+30; + + __CLC_INTN regn_m, regn_l; + __CLC_GENTYPE r_m, r_l, rr_m, rr_l; + + __clc_remainder_piby2_medium(y, &r_m, &rr_m, ®n_m); + __clc_remainder_piby2_large(y, &r_l, &rr_l, ®n_l); + + __CLC_GENTYPE r = is_medium ? r_m : r_l; + __CLC_GENTYPE rr = is_medium ? rr_m : rr_l; + __CLC_INTN regn = __CLC_CONVERT_INTN(is_medium) ? regn_m : regn_l; + + __CLC_GENTYPE lead, tail; + __clc_tan_piby4(r, rr, &lead, &tail); + + __CLC_LONGN t = + __CLC_AS_LONGN(__CLC_CONVERT_BIT_INTN((regn & 1) != 0) ? tail : lead); + t ^= __CLC_CONVERT_BIT_INTN(x < 0.0) << 63; + + return __clc_isnan(x) || __clc_isinf(x) ? __CLC_GENTYPE_NAN + : __CLC_AS_GENTYPE(t); +} + +#elif __CLC_FPSIZE == 16 + +_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_tan(__CLC_GENTYPE x) { + return __CLC_CONVERT_GENTYPE(__clc_tan(__CLC_CONVERT_FLOATN(x))); +} + +#endif diff --git a/libclc/clspv/lib/SOURCES b/libclc/clspv/lib/SOURCES index 437210d1922da..d7cd36d09349b 100644 --- a/libclc/clspv/lib/SOURCES +++ b/libclc/clspv/lib/SOURCES @@ -16,7 +16,6 @@ subnormal_config.cl ../../generic/lib/math/atanh.cl ../../generic/lib/math/atanpi.cl ../../generic/lib/math/cbrt.cl -../../generic/lib/math/clc_tan.cl ../../generic/lib/math/cos.cl ../../generic/lib/math/cosh.cl ../../generic/lib/math/cospi.cl diff --git a/libclc/generic/lib/SOURCES b/libclc/generic/lib/SOURCES index 8c7565e3dd231..46ce6d6e36c24 100644 --- a/libclc/generic/lib/SOURCES +++ b/libclc/generic/lib/SOURCES @@ -146,7 +146,6 @@ math/sincos.cl math/sinh.cl math/sinpi.cl math/sqrt.cl -math/clc_tan.cl math/tan.cl math/tanh.cl math/tanpi.cl diff --git a/libclc/generic/lib/math/clc_sw_unary.inc b/libclc/generic/lib/math/clc_sw_unary.inc deleted file mode 100644 index 6fa051d9adfcc..0000000000000 --- a/libclc/generic/lib/math/clc_sw_unary.inc +++ /dev/null @@ -1,30 +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/utils.h> - -#define __CLC_SW_FUNC(x) __CLC_CONCAT(__clc_, x) - -#if __CLC_FPSIZE > 16 -_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNC(__CLC_GENTYPE x) { - return __CLC_SW_FUNC(__CLC_FUNC)(x); -} -#elif __CLC_FPSIZE == 16 -#ifdef __CLC_SCALAR -_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNC(__CLC_GENTYPE x) { - return convert_half(__CLC_SW_FUNC(__CLC_FUNC)(convert_float(x))); -} -#else -_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __CLC_FUNC(__CLC_GENTYPE x) { - return __CLC_XCONCAT(convert_half, __CLC_VECSIZE)(__CLC_SW_FUNC(__CLC_FUNC)( - __CLC_XCONCAT(convert_float, __CLC_VECSIZE)(x))); -} -#endif -#endif - -#undef __CLC_SW_FUNC diff --git a/libclc/generic/lib/math/clc_tan.cl b/libclc/generic/lib/math/clc_tan.cl deleted file mode 100644 index ce51c1031fa45..0000000000000 --- a/libclc/generic/lib/math/clc_tan.cl +++ /dev/null @@ -1,61 +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/clc.h> -#include <clc/clcmacro.h> -#include <clc/math/clc_fabs.h> -#include <clc/math/clc_sincos_helpers.h> -#include <clc/math/math.h> -#include <clc/math/tables.h> -#include <clc/relational/clc_isinf.h> -#include <clc/relational/clc_isnan.h> - -_CLC_DEF _CLC_OVERLOAD float __clc_tan(float x) { - int ix = as_int(x); - int ax = ix & 0x7fffffff; - float dx = as_float(ax); - - float r0, r1; - int regn = __clc_argReductionS(&r0, &r1, dx); - - float t = __clc_tanf_piby4(r0 + r1, regn); - t = as_float(as_int(t) ^ (ix ^ ax)); - - t = ax >= PINFBITPATT_SP32 ? as_float(QNANBITPATT_SP32) : t; - // Take care of subnormals - t = (x == 0.0f) ? x : t; - return t; -} -_CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_tan, float); - -#ifdef cl_khr_fp64 -#include <clc/math/clc_sincos_piby4.h> - -_CLC_DEF _CLC_OVERLOAD double __clc_tan(double x) { - double y = __clc_fabs(x); - - double r, rr; - int regn; - - if (y < 0x1.0p+30) - __clc_remainder_piby2_medium(y, &r, &rr, ®n); - else - __clc_remainder_piby2_large(y, &r, &rr, ®n); - - double lead, tail; - __clc_tan_piby4(r, rr, &lead, &tail); - - int2 t = as_int2(regn & 1 ? tail : lead); - t.hi ^= (x < 0.0) << 31; - - return __clc_isnan(x) || __clc_isinf(x) ? as_double(QNANBITPATT_DP64) - : as_double(t); -} -_CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_tan, double); - -#endif diff --git a/libclc/generic/lib/math/tan.cl b/libclc/generic/lib/math/tan.cl index ebbaa3a241a70..883e331395616 100644 --- a/libclc/generic/lib/math/tan.cl +++ b/libclc/generic/lib/math/tan.cl @@ -7,9 +7,8 @@ //===----------------------------------------------------------------------===// #include <clc/clc.h> +#include <clc/math/clc_tan.h> -#include <math/clc_tan.h> - -#define __CLC_FUNC tan -#define __CLC_BODY <clc_sw_unary.inc> +#define FUNCTION tan +#define __CLC_BODY <clc/shared/unary_def.inc> #include <clc/math/gentype.inc> diff --git a/libclc/spirv/lib/SOURCES b/libclc/spirv/lib/SOURCES index 20e052253d977..f3852ebe3e3e6 100644 --- a/libclc/spirv/lib/SOURCES +++ b/libclc/spirv/lib/SOURCES @@ -65,7 +65,6 @@ math/fma.cl ../../generic/lib/math/sincos.cl ../../generic/lib/math/sinh.cl ../../generic/lib/math/sinpi.cl -../../generic/lib/math/clc_tan.cl ../../generic/lib/math/tan.cl ../../generic/lib/math/tanh.cl ../../generic/lib/math/tanpi.cl _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits