Author: Matt Arsenault Date: 2026-03-24T08:11:41+01:00 New Revision: cba948b2b93cc5d51f70f0fa0d66bc5cacef9d76
URL: https://github.com/llvm/llvm-project/commit/cba948b2b93cc5d51f70f0fa0d66bc5cacef9d76 DIFF: https://github.com/llvm/llvm-project/commit/cba948b2b93cc5d51f70f0fa0d66bc5cacef9d76.diff LOG: libclc: Update atan (#188095) This was originally ported from rocm device libs in 47882923c7b48c00d6c0ea9960b5457e957093c4. Merge in more recent changes. Added: libclc/clc/include/clc/math/clc_atan_helpers.h libclc/clc/include/clc/math/clc_atan_helpers_decl.inc libclc/clc/lib/generic/math/clc_atan_helpers.cl libclc/clc/lib/generic/math/clc_atan_helpers.inc Modified: libclc/clc/lib/generic/CMakeLists.txt libclc/clc/lib/generic/math/clc_atan.cl libclc/clc/lib/generic/math/clc_atan.inc Removed: ################################################################################ diff --git a/libclc/clc/include/clc/math/clc_atan_helpers.h b/libclc/clc/include/clc/math/clc_atan_helpers.h new file mode 100644 index 0000000000000..ad86cda15f0d7 --- /dev/null +++ b/libclc/clc/include/clc/math/clc_atan_helpers.h @@ -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 +// +//===----------------------------------------------------------------------===// + +#ifndef __CLC_MATH_CLC_ATAN_HELPERS_H__ +#define __CLC_MATH_CLC_ATAN_HELPERS_H__ + +#include "clc/internal/clc.h" + +#define __CLC_BODY "clc/math/clc_atan_helpers_decl.inc" +#include "clc/math/gentype.inc" + +#endif // __CLC_MATH_CLC_ATAN_HELPERS_H__ diff --git a/libclc/clc/include/clc/math/clc_atan_helpers_decl.inc b/libclc/clc/include/clc/math/clc_atan_helpers_decl.inc new file mode 100644 index 0000000000000..6b1aa27a9c60d --- /dev/null +++ b/libclc/clc/include/clc/math/clc_atan_helpers_decl.inc @@ -0,0 +1,10 @@ +//===----------------------------------------------------------------------===// +// +// 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_DECL _CLC_OVERLOAD _CLC_CONST __CLC_GENTYPE +__clc_atan_reduced(__CLC_GENTYPE v); diff --git a/libclc/clc/lib/generic/CMakeLists.txt b/libclc/clc/lib/generic/CMakeLists.txt index 8c5e73c11515d..8ad008a20bce3 100644 --- a/libclc/clc/lib/generic/CMakeLists.txt +++ b/libclc/clc/lib/generic/CMakeLists.txt @@ -62,6 +62,7 @@ libclc_configure_source_list(CLC_GENERIC_SOURCES math/clc_asinh.cl math/clc_asinpi.cl math/clc_atan.cl + math/clc_atan_helpers.cl math/clc_atan2.cl math/clc_atan2pi.cl math/clc_atanh.cl diff --git a/libclc/clc/lib/generic/math/clc_atan.cl b/libclc/clc/lib/generic/math/clc_atan.cl index 3219299dac2a2..768aa94d96333 100644 --- a/libclc/clc/lib/generic/math/clc_atan.cl +++ b/libclc/clc/lib/generic/math/clc_atan.cl @@ -9,11 +9,12 @@ #include "clc/clc_convert.h" #include "clc/float/definitions.h" #include "clc/internal/clc.h" +#include "clc/math/clc_atan_helpers.h" +#include "clc/math/clc_copysign.h" #include "clc/math/clc_fabs.h" #include "clc/math/clc_fma.h" #include "clc/math/clc_mad.h" -#include "clc/math/math.h" -#include "clc/relational/clc_isnan.h" +#include "clc/math/clc_recip_fast.h" #define __CLC_BODY "clc_atan.inc" #include "clc/math/gentype.inc" diff --git a/libclc/clc/lib/generic/math/clc_atan.inc b/libclc/clc/lib/generic/math/clc_atan.inc index 23136dbd74e02..83d849cc54590 100644 --- a/libclc/clc/lib/generic/math/clc_atan.inc +++ b/libclc/clc/lib/generic/math/clc_atan.inc @@ -8,161 +8,51 @@ #if __CLC_FPSIZE == 32 -_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atan(__CLC_GENTYPE x) { - const __CLC_GENTYPE piby2 = 1.5707963267948966f; // 0x3ff921fb54442d18 +_CLC_DEF _CLC_OVERLOAD _CLC_CONST __CLC_FLOATN __clc_atan(__CLC_FLOATN x) { + __CLC_FLOATN v = __clc_fabs(x); + __CLC_INTN g = v > 1.0f; - __CLC_UINTN ux = __CLC_AS_UINTN(x); - __CLC_UINTN aux = ux & EXSIGNBIT_SP32; - __CLC_UINTN sx = ux ^ aux; + __CLC_FLOATN vi = __clc_recip_fast(v); + v = g ? vi : v; - __CLC_GENTYPE spiby2 = __CLC_AS_GENTYPE(sx | __CLC_AS_UINTN(piby2)); + __CLC_FLOATN a = __clc_atan_reduced(v); - __CLC_GENTYPE v = __CLC_AS_GENTYPE(aux); + __CLC_FLOATN y = __clc_mad(0x1.ddcb02p-1f, 0x1.aee9d6p+0f, -a); + a = g ? y : a; - // Return for NaN - __CLC_GENTYPE ret = x; - - // 2^26 <= |x| <= Inf => atan(x) is close to piby2 - ret = aux <= PINFBITPATT_SP32 ? spiby2 : ret; - - // Reduce arguments 2^-19 <= |x| < 2^26 - - // 39/16 <= x < 2^26 - x = -MATH_RECIP(v); - __CLC_GENTYPE c = 1.57079632679489655800f; // atan(infinity) - - // 19/16 <= x < 39/16 - __CLC_INTN l = aux < 0x401c0000; - __CLC_GENTYPE xx = MATH_DIVIDE(v - 1.5f, __clc_mad(v, 1.5f, 1.0f)); - x = l ? xx : x; - c = l ? 9.82793723247329054082e-01f : c; // atan(1.5) - - // 11/16 <= x < 19/16 - l = aux < 0x3f980000U; - xx = MATH_DIVIDE(v - 1.0f, 1.0f + v); - x = l ? xx : x; - c = l ? 7.85398163397448278999e-01f : c; // atan(1) - - // 7/16 <= x < 11/16 - l = aux < 0x3f300000; - xx = MATH_DIVIDE(__clc_mad(v, 2.0f, -1.0f), 2.0f + v); - x = l ? xx : x; - c = l ? 4.63647609000806093515e-01f : c; // atan(0.5) - - // 2^-19 <= x < 7/16 - l = aux < 0x3ee00000; - x = l ? v : x; - c = l ? 0.0f : c; + return __clc_copysign(a, x); +} - // Core approximation: Remez(2,2) on [-7/16,7/16] +#elif __CLC_FPSIZE == 64 - __CLC_GENTYPE s = x * x; - __CLC_GENTYPE a = __clc_mad(s, - __clc_mad(s, 0.470677934286149214138357545549e-2f, - 0.192324546402108583211697690500f), - 0.296528598819239217902158651186f); +_CLC_DEF _CLC_OVERLOAD _CLC_CONST __CLC_DOUBLEN __clc_atan(__CLC_DOUBLEN x) { + __CLC_DOUBLEN v = __clc_fabs(x); + __CLC_LONGN g = v > 1.0; - __CLC_GENTYPE b = __clc_mad(s, - __clc_mad(s, 0.299309699959659728404442796915f, - 0.111072499995399550138837673349e1f), - 0.889585796862432286486651434570f); + v = g ? (1.0 / v) : v; - __CLC_GENTYPE q = x * s * MATH_DIVIDE(a, b); + __CLC_DOUBLEN a = __clc_atan_reduced(v); + __CLC_DOUBLEN y = __clc_fma(0x1.dd9ad336a0500p-1, 0x1.af154eeb562d6p+0, -a); + a = g ? y : a; - __CLC_GENTYPE z = c - (q - x); - __CLC_GENTYPE zs = __CLC_AS_GENTYPE(sx | __CLC_AS_UINTN(z)); + return __clc_copysign(a, x); +} - ret = aux < 0x4c800000 ? zs : ret; +#elif __CLC_FPSIZE == 16 - // |x| < 2^-19 - ret = aux < 0x36000000 ? __CLC_AS_GENTYPE(ux) : ret; - return ret; -} +_CLC_DEF _CLC_OVERLOAD _CLC_CONST __CLC_HALFN __clc_atan(__CLC_HALFN x) { + __CLC_HALFN v = __clc_fabs(x); + __CLC_SHORTN g = v > 1.0h; -#elif __CLC_FPSIZE == 64 + __CLC_HALFN vi = __clc_recip_fast(v); + v = g ? vi : v; -_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atan(__CLC_GENTYPE x) { - const __CLC_GENTYPE piby2 = 1.5707963267948966e+00; // 0x3ff921fb54442d18 - - __CLC_GENTYPE v = __clc_fabs(x); - - // 2^56 > v > 39/16 - __CLC_GENTYPE a = -1.0; - __CLC_GENTYPE b = v; - // (chi + clo) = arctan(infinity) - __CLC_GENTYPE chi = 1.57079632679489655800e+00; - __CLC_GENTYPE clo = 6.12323399573676480327e-17; - - __CLC_GENTYPE ta = v - 1.5; - __CLC_GENTYPE tb = 1.0 + 1.5 * v; - __CLC_LONGN l = v <= 0x1.38p+1; // 39/16 > v > 19/16 - a = l ? ta : a; - b = l ? tb : b; - // (chi + clo) = arctan(1.5) - chi = l ? 9.82793723247329054082e-01 : chi; - clo = l ? 1.39033110312309953701e-17 : clo; - - ta = v - 1.0; - tb = 1.0 + v; - l = v <= 0x1.3p+0; // 19/16 > v > 11/16 - a = l ? ta : a; - b = l ? tb : b; - // (chi + clo) = arctan(1.) - chi = l ? 7.85398163397448278999e-01 : chi; - clo = l ? 3.06161699786838240164e-17 : clo; - - ta = 2.0 * v - 1.0; - tb = 2.0 + v; - l = v <= 0x1.6p-1; // 11/16 > v > 7/16 - a = l ? ta : a; - b = l ? tb : b; - // (chi + clo) = arctan(0.5) - chi = l ? 4.63647609000806093515e-01 : chi; - clo = l ? 2.26987774529616809294e-17 : clo; - - l = v <= 0x1.cp-2; // v < 7/16 - a = l ? v : a; - b = l ? 1.0 : b; - ; - chi = l ? 0.0 : chi; - clo = l ? 0.0 : clo; - - // Core approximation: Remez(4,4) on [-7/16,7/16] - __CLC_GENTYPE r = a / b; - __CLC_GENTYPE s = r * r; - __CLC_GENTYPE qn = - __clc_fma(s, - __clc_fma(s, - __clc_fma(s, - __clc_fma(s, 0.142316903342317766e-3, - 0.304455919504853031e-1), - 0.220638780716667420e0), - 0.447677206805497472e0), - 0.268297920532545909e0); - - __CLC_GENTYPE qd = - __clc_fma(s, - __clc_fma(s, - __clc_fma(s, - __clc_fma(s, 0.389525873944742195e-1, - 0.424602594203847109e0), - 0.141254259931958921e1), - 0.182596787737507063e1), - 0.804893761597637733e0); - - __CLC_GENTYPE q = r * s * qn / qd; - r = chi - ((q - clo) - r); - - __CLC_GENTYPE z = __clc_isnan(x) ? x : piby2; - z = v <= 0x1.0p+56 ? r : z; - z = v < 0x1.0p-26 ? v : z; - return x == v ? z : -z; -} + __CLC_HALFN a = __clc_atan_reduced(v); -#elif __CLC_FPSIZE == 16 + __CLC_HALFN y = __clc_mad(0x1.ea8p-1h, 0x1.a3cp+0h, -a); + a = g ? y : a; -_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atan(__CLC_GENTYPE x) { - return __CLC_CONVERT_GENTYPE(__clc_atan(__CLC_CONVERT_FLOATN(x))); + return __clc_copysign(a, x); } #endif diff --git a/libclc/clc/lib/generic/math/clc_atan_helpers.cl b/libclc/clc/lib/generic/math/clc_atan_helpers.cl new file mode 100644 index 0000000000000..aa623478c258b --- /dev/null +++ b/libclc/clc/lib/generic/math/clc_atan_helpers.cl @@ -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/math/clc_atan_helpers.h" +#include "clc/math/clc_mad.h" + +#define __CLC_BODY "clc_atan_helpers.inc" + +#include "clc/math/gentype.inc" diff --git a/libclc/clc/lib/generic/math/clc_atan_helpers.inc b/libclc/clc/lib/generic/math/clc_atan_helpers.inc new file mode 100644 index 0000000000000..ba3fb6d1904e3 --- /dev/null +++ b/libclc/clc/lib/generic/math/clc_atan_helpers.inc @@ -0,0 +1,53 @@ +//===----------------------------------------------------------------------===// +// +// 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_CONST __CLC_FLOATN +__clc_atan_reduced(__CLC_FLOATN v) { + __CLC_FLOATN t = v * v; + __CLC_FLOATN z = __clc_mad(t, __clc_mad(t, __clc_mad(t, __clc_mad(t, + __clc_mad(t, __clc_mad(t, __clc_mad(t, + 0x1.5a54bp-9f, -0x1.f4b218p-7f), 0x1.53f67ep-5f), -0x1.2fa9aep-4f), + 0x1.b26364p-4f), -0x1.22c1ccp-3f), 0x1.99717ep-3f), -0x1.5554c4p-2f); + + z = __clc_mad(v, t * z, v); + return z; +} + +#elif __CLC_FPSIZE == 64 + +_CLC_DEF _CLC_OVERLOAD _CLC_CONST __CLC_DOUBLEN +__clc_atan_reduced(__CLC_DOUBLEN v) { + __CLC_DOUBLEN t = v * v; + __CLC_DOUBLEN z = __clc_mad(t, __clc_mad(t, __clc_mad(t, __clc_mad(t, + __clc_mad(t, __clc_mad(t, __clc_mad(t, __clc_mad(t, + __clc_mad(t, __clc_mad(t, __clc_mad(t, __clc_mad(t, + __clc_mad(t, __clc_mad(t, __clc_mad(t, __clc_mad(t, + __clc_mad(t, __clc_mad(t, __clc_mad(t, + 0x1.ba404b5e68a13p-17, -0x1.3e260bd3237f4p-13), 0x1.b2bb069efb384p-11), -0x1.7952daf56de9bp-9), + 0x1.d6d43a595c56fp-8), -0x1.c6ea4a57d9582p-7), 0x1.67e295f08b19fp-6), -0x1.e9ae6fc27006ap-6), + 0x1.2c15b5711927ap-5), -0x1.59976e82d3ff0p-5), 0x1.82d5d6ef28734p-5), -0x1.ae5ce6a214619p-5), + 0x1.e1bb48427b883p-5), -0x1.110e48b207f05p-4), 0x1.3b13657b87036p-4), -0x1.745d119378e4fp-4), + 0x1.c71c717e1913cp-4), -0x1.2492492376b7dp-3), 0x1.99999999952ccp-3), -0x1.5555555555523p-2); + z = __clc_mad(v, t*z, v); + return z; +} + +#elif __CLC_FPSIZE == 16 + +_CLC_DEF _CLC_OVERLOAD _CLC_CONST __CLC_HALFN +__clc_atan_reduced(__CLC_HALFN v) { + __CLC_HALFN t = v * v; + __CLC_HALFN z = __clc_mad( + t, __clc_mad(t, __clc_mad(t, 0x1.938p-6h, -0x1.7f4p-4h), 0x1.7dcp-3h), + -0x1.54p-2h); + return __clc_mad(t, v * z, v); +} + +#endif _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
