[libclc] [libclc] Move asin/acos/atan to the CLC library (PR #132788)
Fraser Cormack via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 24 10:37:50 PDT 2025
https://github.com/frasercrmck created https://github.com/llvm/llvm-project/pull/132788
This commit simultaneously moves these three functions to the CLC library and optimizing them for vector types by avoiding scalarization.
>From 225400d9dcf9e6bcc08a0b61e062f28f6edfdda6 Mon Sep 17 00:00:00 2001
From: Fraser Cormack <fraser at codeplay.com>
Date: Mon, 24 Mar 2025 17:34:49 +0000
Subject: [PATCH] [libclc] Move asin/acos/atan to the CLC library
This commit simultaneously moves these three functions to the CLC
library and optimizing them for vector types by avoiding scalarization.
---
libclc/clc/include/clc/math/clc_acos.h | 20 +++
libclc/clc/include/clc/math/clc_asin.h | 20 +++
libclc/clc/include/clc/math/clc_atan.h | 20 +++
libclc/clc/lib/generic/SOURCES | 3 +
libclc/clc/lib/generic/math/clc_acos.cl | 20 +++
libclc/clc/lib/generic/math/clc_acos.inc | 158 +++++++++++++++++++++
libclc/clc/lib/generic/math/clc_asin.cl | 19 +++
libclc/clc/lib/generic/math/clc_asin.inc | 154 +++++++++++++++++++++
libclc/clc/lib/generic/math/clc_atan.cl | 19 +++
libclc/clc/lib/generic/math/clc_atan.inc | 168 ++++++++++++++++++++++
libclc/generic/lib/math/acos.cl | 160 +--------------------
libclc/generic/lib/math/asin.cl | 145 +------------------
libclc/generic/lib/math/atan.cl | 169 +----------------------
13 files changed, 613 insertions(+), 462 deletions(-)
create mode 100644 libclc/clc/include/clc/math/clc_acos.h
create mode 100644 libclc/clc/include/clc/math/clc_asin.h
create mode 100644 libclc/clc/include/clc/math/clc_atan.h
create mode 100644 libclc/clc/lib/generic/math/clc_acos.cl
create mode 100644 libclc/clc/lib/generic/math/clc_acos.inc
create mode 100644 libclc/clc/lib/generic/math/clc_asin.cl
create mode 100644 libclc/clc/lib/generic/math/clc_asin.inc
create mode 100644 libclc/clc/lib/generic/math/clc_atan.cl
create mode 100644 libclc/clc/lib/generic/math/clc_atan.inc
diff --git a/libclc/clc/include/clc/math/clc_acos.h b/libclc/clc/include/clc/math/clc_acos.h
new file mode 100644
index 0000000000000..edf519344111d
--- /dev/null
+++ b/libclc/clc/include/clc/math/clc_acos.h
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_ACOS_H__
+#define __CLC_MATH_CLC_ACOS_H__
+
+#define __CLC_BODY <clc/math/unary_decl.inc>
+#define __CLC_FUNCTION __clc_acos
+
+#include <clc/math/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION
+
+#endif // __CLC_MATH_CLC_ACOS_H__
diff --git a/libclc/clc/include/clc/math/clc_asin.h b/libclc/clc/include/clc/math/clc_asin.h
new file mode 100644
index 0000000000000..11227a5e14d50
--- /dev/null
+++ b/libclc/clc/include/clc/math/clc_asin.h
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_ASIN_H__
+#define __CLC_MATH_CLC_ASIN_H__
+
+#define __CLC_BODY <clc/math/unary_decl.inc>
+#define __CLC_FUNCTION __clc_asin
+
+#include <clc/math/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION
+
+#endif // __CLC_MATH_CLC_ASIN_H__
diff --git a/libclc/clc/include/clc/math/clc_atan.h b/libclc/clc/include/clc/math/clc_atan.h
new file mode 100644
index 0000000000000..903b15299d629
--- /dev/null
+++ b/libclc/clc/include/clc/math/clc_atan.h
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_H__
+#define __CLC_MATH_CLC_ATAN_H__
+
+#define __CLC_BODY <clc/math/unary_decl.inc>
+#define __CLC_FUNCTION __clc_atan
+
+#include <clc/math/gentype.inc>
+
+#undef __CLC_BODY
+#undef __CLC_FUNCTION
+
+#endif // __CLC_MATH_CLC_ATAN_H__
diff --git a/libclc/clc/lib/generic/SOURCES b/libclc/clc/lib/generic/SOURCES
index 490ce5c364465..1001287b3f483 100644
--- a/libclc/clc/lib/generic/SOURCES
+++ b/libclc/clc/lib/generic/SOURCES
@@ -17,6 +17,9 @@ integer/clc_rhadd.cl
integer/clc_rotate.cl
integer/clc_sub_sat.cl
integer/clc_upsample.cl
+math/clc_acos.cl
+math/clc_asin.cl
+math/clc_atan.cl
math/clc_ceil.cl
math/clc_copysign.cl
math/clc_fabs.cl
diff --git a/libclc/clc/lib/generic/math/clc_acos.cl b/libclc/clc/lib/generic/math/clc_acos.cl
new file mode 100644
index 0000000000000..822c3f9f13e8e
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_acos.cl
@@ -0,0 +1,20 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_fma.h>
+#include <clc/math/clc_mad.h>
+#include <clc/math/clc_sqrt.h>
+#include <clc/math/math.h>
+#include <clc/relational/clc_isnan.h>
+
+#define __CLC_BODY <clc_acos.inc>
+#include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/math/clc_acos.inc b/libclc/clc/lib/generic/math/clc_acos.inc
new file mode 100644
index 0000000000000..e036a998a65bd
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_acos.inc
@@ -0,0 +1,158 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Computes arccos(x).
+//
+// The incoming argument is first reduced by noting that arccos(x) is invalid
+// for abs(x) > 1.
+//
+// For denormal and small arguments arccos(x) = pi/2 to machine accuracy.
+//
+// Remaining argument ranges are handled as follows:
+// * For abs(x) <= 0.5 use:
+// arccos(x) = pi/2 - arcsin(x) = pi/2 - (x + x^3 * R(x^2))
+// where R(x^2) is a rational minimax approximation to (arcsin(x) - x)/x^3.
+// * For abs(x) > 0.5 exploit the identity:
+// arccos(x) = pi - 2 * arcsin(sqrt(1 - x)/2)
+// together with the above rational approximation, and reconstruct the terms
+// carefully.
+//
+//===----------------------------------------------------------------------===//
+
+#if __CLC_FPSIZE == 32
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acos(__CLC_GENTYPE x) {
+ // Some constants and split constants.
+ const __CLC_GENTYPE piby2 = __CLC_FP_LIT(1.5707963705e+00);
+ const __CLC_GENTYPE pi = __CLC_FP_LIT(3.1415926535897933e+00);
+ const __CLC_GENTYPE piby2_head = __CLC_FP_LIT(1.5707963267948965580e+00);
+ const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(6.12323399573676603587e-17);
+
+ __CLC_UINTN ux = __CLC_AS_UINTN(x);
+ __CLC_UINTN aux = ux & ~SIGNBIT_SP32;
+ __CLC_INTN xneg = ux != aux;
+ __CLC_INTN xexp = __CLC_AS_INTN(aux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
+ __CLC_GENTYPE y = __CLC_AS_GENTYPE(aux);
+
+ // transform if |x| >= 0.5
+ __CLC_INTN transform = xexp >= -1;
+
+ __CLC_GENTYPE y2 = y * y;
+ __CLC_GENTYPE yt = 0.5f * (1.0f - y);
+ __CLC_GENTYPE r = transform ? yt : y2;
+
+ // Use a rational approximation for [0.0, 0.5]
+ __CLC_GENTYPE a =
+ __clc_mad(r,
+ __clc_mad(r,
+ __clc_mad(r, -0.00396137437848476485201154797087F,
+ -0.0133819288943925804214011424456F),
+ -0.0565298683201845211985026327361F),
+ 0.184161606965100694821398249421F);
+
+ __CLC_GENTYPE b = __clc_mad(r, -0.836411276854206731913362287293F,
+ 1.10496961524520294485512696706F);
+ __CLC_GENTYPE u = r * MATH_DIVIDE(a, b);
+
+ __CLC_GENTYPE s = __clc_sqrt(r);
+ y = s;
+ __CLC_GENTYPE s1 = __CLC_AS_GENTYPE(__CLC_AS_UINTN(s) & 0xffff0000);
+ __CLC_GENTYPE c = MATH_DIVIDE(__clc_mad(s1, -s1, r), s + s1);
+ __CLC_GENTYPE rettn = __clc_mad(s + __clc_mad(y, u, -piby2_tail), -2.0f, pi);
+ __CLC_GENTYPE rettp = 2.0F * (s1 + __clc_mad(y, u, c));
+ __CLC_GENTYPE rett = xneg ? rettn : rettp;
+ __CLC_GENTYPE ret = piby2_head - (x - __clc_mad(x, -u, piby2_tail));
+
+ ret = transform ? rett : ret;
+ ret = aux > 0x3f800000U ? __CLC_GENTYPE_NAN : ret;
+ ret = ux == 0x3f800000U ? 0.0f : ret;
+ ret = ux == 0xbf800000U ? pi : ret;
+ ret = xexp < -26 ? piby2 : ret;
+ return ret;
+}
+
+#elif __CLC_FPSIZE == 64
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acos(__CLC_GENTYPE x) {
+ // 0x400921fb54442d18
+ const __CLC_GENTYPE pi = __CLC_FP_LIT(3.1415926535897933e+00);
+ // 0x3ff921fb54442d18
+ const __CLC_GENTYPE piby2 = __CLC_FP_LIT(1.5707963267948965580e+00);
+ // 0x3ff921fb54442d18
+ const __CLC_GENTYPE piby2_head = __CLC_FP_LIT(1.5707963267948965580e+00);
+ // 0x3c91a62633145c07
+ const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(6.12323399573676603587e-17);
+
+ __CLC_GENTYPE y = __clc_fabs(x);
+ __CLC_LONGN xneg = x < __CLC_FP_LIT(0.0);
+ __CLC_INTN xexp = __CLC_CONVERT_INTN(
+ (__CLC_AS_ULONGN(y) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64);
+
+ // abs(x) >= 0.5
+ __CLC_LONGN transform = __CLC_CONVERT_LONGN(xexp >= -1);
+
+ __CLC_GENTYPE rt = __CLC_FP_LIT(0.5) * (__CLC_FP_LIT(1.0) - y);
+ __CLC_GENTYPE y2 = y * y;
+ __CLC_GENTYPE r = transform ? rt : y2;
+
+ // Use a rational approximation for [0.0, 0.5]
+ __CLC_GENTYPE un = __clc_fma(
+ r,
+ __clc_fma(
+ r,
+ __clc_fma(r,
+ __clc_fma(r,
+ __clc_fma(r, 0.0000482901920344786991880522822991,
+ 0.00109242697235074662306043804220),
+ -0.0549989809235685841612020091328),
+ 0.275558175256937652532686256258),
+ -0.445017216867635649900123110649),
+ 0.227485835556935010735943483075);
+
+ __CLC_GENTYPE ud = __clc_fma(
+ r,
+ __clc_fma(r,
+ __clc_fma(r,
+ __clc_fma(r, 0.105869422087204370341222318533,
+ -0.943639137032492685763471240072),
+ 2.76568859157270989520376345954),
+ -3.28431505720958658909889444194),
+ 1.36491501334161032038194214209);
+
+ __CLC_GENTYPE u = r * MATH_DIVIDE(un, ud);
+
+ // Reconstruct acos carefully in transformed region
+ __CLC_GENTYPE s = __clc_sqrt(r);
+ __CLC_GENTYPE ztn = __clc_fma(-2.0, (s + __clc_fma(s, u, -piby2_tail)), pi);
+
+ __CLC_GENTYPE s1 =
+ __CLC_AS_GENTYPE(__CLC_AS_ULONGN(s) & 0xffffffff00000000UL);
+ __CLC_GENTYPE c = MATH_DIVIDE(__clc_fma(-s1, s1, r), s + s1);
+ __CLC_GENTYPE ztp = 2.0 * (s1 + __clc_fma(s, u, c));
+ __CLC_GENTYPE zt = xneg ? ztn : ztp;
+ __CLC_GENTYPE z = piby2_head - (x - __clc_fma(-x, u, piby2_tail));
+
+ z = transform ? zt : z;
+
+ z = __CLC_CONVERT_LONGN(xexp < -56) ? piby2 : z;
+ z = __clc_isnan(x) ? __CLC_AS_GENTYPE((__CLC_AS_ULONGN(x) |
+ (__CLC_ULONGN)QNANBITPATT_DP64))
+ : z;
+ z = x == 1.0 ? 0.0 : z;
+ z = x == -1.0 ? pi : z;
+
+ return z;
+}
+
+#elif __CLC_FPSIZE == 16
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acos(__CLC_GENTYPE x) {
+ return __CLC_CONVERT_GENTYPE(__clc_acos(__CLC_CONVERT_FLOATN(x)));
+}
+
+#endif
diff --git a/libclc/clc/lib/generic/math/clc_asin.cl b/libclc/clc/lib/generic/math/clc_asin.cl
new file mode 100644
index 0000000000000..195ede3907f3c
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_asin.cl
@@ -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
+//
+//===----------------------------------------------------------------------===//
+
+#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_fma.h>
+#include <clc/math/clc_mad.h>
+#include <clc/math/clc_sqrt.h>
+#include <clc/math/math.h>
+
+#define __CLC_BODY <clc_asin.inc>
+#include <clc/math/gentype.inc>
diff --git a/libclc/clc/lib/generic/math/clc_asin.inc b/libclc/clc/lib/generic/math/clc_asin.inc
new file mode 100644
index 0000000000000..a1718b81b4bc4
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_asin.inc
@@ -0,0 +1,154 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Computes arcsin(x).
+//
+// The incoming argument is first reduced by noting that arcsin(x) is invalid
+// for abs(x) > 1 and arcsin(-x) = -arcsin(x).
+//
+// For denormal and small arguments, arcsin(x) = x to machine accuracy.
+//
+// Remaining argument ranges are handled as follows:
+// * For abs(x) <= 0.5 use:
+// arcsin(x) = x + x^3 * R(x^2)
+// where R(x^2) is a rational minimax approximation to (arcsin(x) - x)/x^3.
+// * For abs(x) > 0.5 exploit the identity:
+// arcsin(x) = pi/2 - 2 * arcsin(sqrt(1 - x)/2)
+// together with the above rational approximation, and reconstruct the terms
+// carefully.
+//
+//===----------------------------------------------------------------------===//
+
+#if __CLC_FPSIZE == 32
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_asin(__CLC_GENTYPE x) {
+ // 0x33a22168
+ const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(7.5497894159e-08);
+ // 0x3f490fda
+ const __CLC_GENTYPE hpiby2_head = __CLC_FP_LIT(7.8539812565e-01);
+ // 0x3fc90fdb
+ const __CLC_GENTYPE piby2 = __CLC_FP_LIT(1.5707963705e+00);
+
+ __CLC_UINTN ux = __CLC_AS_UINTN(x);
+ __CLC_UINTN aux = ux & EXSIGNBIT_SP32;
+ __CLC_UINTN xs = ux ^ aux;
+ __CLC_GENTYPE spiby2 = __CLC_AS_GENTYPE(xs | __CLC_AS_UINTN(piby2));
+ __CLC_INTN xexp = __CLC_AS_INTN(aux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
+ __CLC_GENTYPE y = __CLC_AS_GENTYPE(aux);
+
+ // abs(x) >= 0.5
+ __CLC_INTN transform = xexp >= -1;
+
+ __CLC_GENTYPE y2 = y * y;
+ __CLC_GENTYPE rt = __CLC_FP_LIT(0.5) * (__CLC_FP_LIT(1.0) - y);
+ __CLC_GENTYPE r = transform ? rt : y2;
+
+ // Use a rational approximation for [0.0, 0.5]
+ __CLC_GENTYPE a =
+ __clc_mad(r,
+ __clc_mad(r,
+ __clc_mad(r, -0.00396137437848476485201154797087F,
+ -0.0133819288943925804214011424456F),
+ -0.0565298683201845211985026327361F),
+ 0.184161606965100694821398249421F);
+
+ __CLC_GENTYPE b = __clc_mad(r, -0.836411276854206731913362287293F,
+ 1.10496961524520294485512696706F);
+ __CLC_GENTYPE u = r * MATH_DIVIDE(a, b);
+
+ __CLC_GENTYPE s = __clc_sqrt(r);
+ __CLC_GENTYPE s1 = __CLC_AS_GENTYPE(__CLC_AS_UINTN(s) & 0xffff0000);
+ __CLC_GENTYPE c = MATH_DIVIDE(__clc_mad(-s1, s1, r), s + s1);
+ __CLC_GENTYPE p = __clc_mad(2.0f * s, u, -__clc_mad(c, -2.0f, piby2_tail));
+ __CLC_GENTYPE q = __clc_mad(s1, -2.0f, hpiby2_head);
+ __CLC_GENTYPE vt = hpiby2_head - (p - q);
+ __CLC_GENTYPE v = __clc_mad(y, u, y);
+ v = transform ? vt : v;
+
+ __CLC_GENTYPE ret = __CLC_AS_GENTYPE(xs | __CLC_AS_UINTN(v));
+ ret = aux > 0x3f800000U ? __CLC_GENTYPE_NAN : ret;
+ ret = aux == 0x3f800000U ? spiby2 : ret;
+ ret = xexp < -14 ? x : ret;
+
+ return ret;
+}
+
+#elif __CLC_FPSIZE == 64
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_asin(__CLC_GENTYPE x) {
+ // 0x3c91a62633145c07
+ const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(6.1232339957367660e-17);
+ // 0x3fe921fb54442d18
+ const __CLC_GENTYPE hpiby2_head = 7.8539816339744831e-01;
+ // 0x3ff921fb54442d18
+ const __CLC_GENTYPE piby2 = 1.5707963267948965e+00;
+
+ __CLC_GENTYPE y = __clc_fabs(x);
+ __CLC_LONGN xneg = x < __CLC_FP_LIT(0.0);
+ __CLC_INTN xexp = __CLC_CONVERT_INTN(
+ (__CLC_AS_ULONGN(y) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64);
+
+ // abs(x) >= 0.5
+ __CLC_LONGN transform = __CLC_CONVERT_LONGN(xexp >= -1);
+
+ __CLC_GENTYPE rt = __CLC_FP_LIT(0.5) * (__CLC_FP_LIT(1.0) - y);
+ __CLC_GENTYPE y2 = y * y;
+ __CLC_GENTYPE r = transform ? rt : y2;
+
+ // Use a rational approximation for [0.0, 0.5]
+
+ __CLC_GENTYPE un = __clc_fma(
+ r,
+ __clc_fma(
+ r,
+ __clc_fma(r,
+ __clc_fma(r,
+ __clc_fma(r, 0.0000482901920344786991880522822991,
+ 0.00109242697235074662306043804220),
+ -0.0549989809235685841612020091328),
+ 0.275558175256937652532686256258),
+ -0.445017216867635649900123110649),
+ 0.227485835556935010735943483075);
+
+ __CLC_GENTYPE ud = __clc_fma(
+ r,
+ __clc_fma(r,
+ __clc_fma(r,
+ __clc_fma(r, 0.105869422087204370341222318533,
+ -0.943639137032492685763471240072),
+ 2.76568859157270989520376345954),
+ -3.28431505720958658909889444194),
+ 1.36491501334161032038194214209);
+
+ __CLC_GENTYPE u = r * MATH_DIVIDE(un, ud);
+
+ // Reconstruct asin carefully in transformed region
+ __CLC_GENTYPE s = __clc_sqrt(r);
+ __CLC_GENTYPE sh =
+ __CLC_AS_GENTYPE(__CLC_AS_ULONGN(s) & 0xffffffff00000000UL);
+ __CLC_GENTYPE c = MATH_DIVIDE(__clc_fma(-sh, sh, r), s + sh);
+ __CLC_GENTYPE p = __clc_fma(2.0 * s, u, -__clc_fma(-2.0, c, piby2_tail));
+ __CLC_GENTYPE q = __clc_fma(-2.0, sh, hpiby2_head);
+ __CLC_GENTYPE vt = hpiby2_head - (p - q);
+ __CLC_GENTYPE v = __clc_fma(y, u, y);
+ v = transform ? vt : v;
+
+ v = __CLC_CONVERT_LONGN(xexp < -28) ? y : v;
+ v = __CLC_CONVERT_LONGN(xexp >= 0) ? __CLC_GENTYPE_NAN : v;
+ v = y == 1.0 ? piby2 : v;
+
+ return xneg ? -v : v;
+}
+
+#elif __CLC_FPSIZE == 16
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_asin(__CLC_GENTYPE x) {
+ return __CLC_CONVERT_GENTYPE(__clc_asin(__CLC_CONVERT_FLOATN(x)));
+}
+
+#endif
diff --git a/libclc/clc/lib/generic/math/clc_atan.cl b/libclc/clc/lib/generic/math/clc_atan.cl
new file mode 100644
index 0000000000000..d960f75baca2b
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_atan.cl
@@ -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
+//
+//===----------------------------------------------------------------------===//
+
+#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_fma.h>
+#include <clc/math/clc_mad.h>
+#include <clc/math/math.h>
+#include <clc/relational/clc_isnan.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
new file mode 100644
index 0000000000000..23136dbd74e02
--- /dev/null
+++ b/libclc/clc/lib/generic/math/clc_atan.inc
@@ -0,0 +1,168 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atan(__CLC_GENTYPE x) {
+ const __CLC_GENTYPE piby2 = 1.5707963267948966f; // 0x3ff921fb54442d18
+
+ __CLC_UINTN ux = __CLC_AS_UINTN(x);
+ __CLC_UINTN aux = ux & EXSIGNBIT_SP32;
+ __CLC_UINTN sx = ux ^ aux;
+
+ __CLC_GENTYPE spiby2 = __CLC_AS_GENTYPE(sx | __CLC_AS_UINTN(piby2));
+
+ __CLC_GENTYPE v = __CLC_AS_GENTYPE(aux);
+
+ // 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;
+
+ // Core approximation: Remez(2,2) on [-7/16,7/16]
+
+ __CLC_GENTYPE s = x * x;
+ __CLC_GENTYPE a = __clc_mad(s,
+ __clc_mad(s, 0.470677934286149214138357545549e-2f,
+ 0.192324546402108583211697690500f),
+ 0.296528598819239217902158651186f);
+
+ __CLC_GENTYPE b = __clc_mad(s,
+ __clc_mad(s, 0.299309699959659728404442796915f,
+ 0.111072499995399550138837673349e1f),
+ 0.889585796862432286486651434570f);
+
+ __CLC_GENTYPE q = x * s * MATH_DIVIDE(a, b);
+
+ __CLC_GENTYPE z = c - (q - x);
+ __CLC_GENTYPE zs = __CLC_AS_GENTYPE(sx | __CLC_AS_UINTN(z));
+
+ ret = aux < 0x4c800000 ? zs : ret;
+
+ // |x| < 2^-19
+ ret = aux < 0x36000000 ? __CLC_AS_GENTYPE(ux) : ret;
+ return ret;
+}
+
+#elif __CLC_FPSIZE == 64
+
+_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;
+}
+
+#elif __CLC_FPSIZE == 16
+
+_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atan(__CLC_GENTYPE x) {
+ return __CLC_CONVERT_GENTYPE(__clc_atan(__CLC_CONVERT_FLOATN(x)));
+}
+
+#endif
diff --git a/libclc/generic/lib/math/acos.cl b/libclc/generic/lib/math/acos.cl
index 8e75118eab487..1efe5eb438deb 100644
--- a/libclc/generic/lib/math/acos.cl
+++ b/libclc/generic/lib/math/acos.cl
@@ -8,160 +8,8 @@
#include <clc/clc.h>
#include <clc/clcmacro.h>
-#include <clc/math/math.h>
+#include <clc/math/clc_acos.h>
-_CLC_OVERLOAD _CLC_DEF float acos(float x) {
- // Computes arccos(x).
- // The argument is first reduced by noting that arccos(x)
- // is invalid for abs(x) > 1. For denormal and small
- // arguments arccos(x) = pi/2 to machine accuracy.
- // Remaining argument ranges are handled as follows.
- // For abs(x) <= 0.5 use
- // arccos(x) = pi/2 - arcsin(x)
- // = pi/2 - (x + x^3*R(x^2))
- // where R(x^2) is a rational minimax approximation to
- // (arcsin(x) - x)/x^3.
- // For abs(x) > 0.5 exploit the identity:
- // arccos(x) = pi - 2*arcsin(sqrt(1-x)/2)
- // together with the above rational approximation, and
- // reconstruct the terms carefully.
-
-
- // Some constants and split constants.
- const float piby2 = 1.5707963705e+00F;
- const float pi = 3.1415926535897933e+00F;
- const float piby2_head = 1.5707963267948965580e+00F;
- const float piby2_tail = 6.12323399573676603587e-17F;
-
- uint ux = as_uint(x);
- uint aux = ux & ~SIGNBIT_SP32;
- int xneg = ux != aux;
- int xexp = (int)(aux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
- float y = as_float(aux);
-
- // transform if |x| >= 0.5
- int transform = xexp >= -1;
-
- float y2 = y * y;
- float yt = 0.5f * (1.0f - y);
- float r = transform ? yt : y2;
-
- // Use a rational approximation for [0.0, 0.5]
- float a = mad(r,
- mad(r,
- mad(r, -0.00396137437848476485201154797087F, -0.0133819288943925804214011424456F),
- -0.0565298683201845211985026327361F),
- 0.184161606965100694821398249421F);
-
- float b = mad(r, -0.836411276854206731913362287293F, 1.10496961524520294485512696706F);
- float u = r * MATH_DIVIDE(a, b);
-
- float s = MATH_SQRT(r);
- y = s;
- float s1 = as_float(as_uint(s) & 0xffff0000);
- float c = MATH_DIVIDE(mad(s1, -s1, r), s + s1);
- float rettn = mad(s + mad(y, u, -piby2_tail), -2.0f, pi);
- float rettp = 2.0F * (s1 + mad(y, u, c));
- float rett = xneg ? rettn : rettp;
- float ret = piby2_head - (x - mad(x, -u, piby2_tail));
-
- ret = transform ? rett : ret;
- ret = aux > 0x3f800000U ? as_float(QNANBITPATT_SP32) : ret;
- ret = ux == 0x3f800000U ? 0.0f : ret;
- ret = ux == 0xbf800000U ? pi : ret;
- ret = xexp < -26 ? piby2 : ret;
- return ret;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, acos, float);
-
-#ifdef cl_khr_fp64
-
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_OVERLOAD _CLC_DEF double acos(double x) {
- // Computes arccos(x).
- // The argument is first reduced by noting that arccos(x)
- // is invalid for abs(x) > 1. For denormal and small
- // arguments arccos(x) = pi/2 to machine accuracy.
- // Remaining argument ranges are handled as follows.
- // For abs(x) <= 0.5 use
- // arccos(x) = pi/2 - arcsin(x)
- // = pi/2 - (x + x^3*R(x^2))
- // where R(x^2) is a rational minimax approximation to
- // (arcsin(x) - x)/x^3.
- // For abs(x) > 0.5 exploit the identity:
- // arccos(x) = pi - 2*arcsin(sqrt(1-x)/2)
- // together with the above rational approximation, and
- // reconstruct the terms carefully.
-
- const double pi = 3.1415926535897933e+00; /* 0x400921fb54442d18 */
- const double piby2 = 1.5707963267948965580e+00; /* 0x3ff921fb54442d18 */
- const double piby2_head = 1.5707963267948965580e+00; /* 0x3ff921fb54442d18 */
- const double piby2_tail = 6.12323399573676603587e-17; /* 0x3c91a62633145c07 */
-
- double y = fabs(x);
- int xneg = as_int2(x).hi < 0;
- int xexp = (as_int2(y).hi >> 20) - EXPBIAS_DP64;
-
- // abs(x) >= 0.5
- int transform = xexp >= -1;
-
- double rt = 0.5 * (1.0 - y);
- double y2 = y * y;
- double r = transform ? rt : y2;
-
- // Use a rational approximation for [0.0, 0.5]
- double un = fma(r,
- fma(r,
- fma(r,
- fma(r,
- fma(r, 0.0000482901920344786991880522822991,
- 0.00109242697235074662306043804220),
- -0.0549989809235685841612020091328),
- 0.275558175256937652532686256258),
- -0.445017216867635649900123110649),
- 0.227485835556935010735943483075);
-
- double ud = fma(r,
- fma(r,
- fma(r,
- fma(r, 0.105869422087204370341222318533,
- -0.943639137032492685763471240072),
- 2.76568859157270989520376345954),
- -3.28431505720958658909889444194),
- 1.36491501334161032038194214209);
-
- double u = r * MATH_DIVIDE(un, ud);
-
- // Reconstruct acos carefully in transformed region
- double s = sqrt(r);
- double ztn = fma(-2.0, (s + fma(s, u, -piby2_tail)), pi);
-
- double s1 = as_double(as_ulong(s) & 0xffffffff00000000UL);
- double c = MATH_DIVIDE(fma(-s1, s1, r), s + s1);
- double ztp = 2.0 * (s1 + fma(s, u, c));
- double zt = xneg ? ztn : ztp;
- double z = piby2_head - (x - fma(-x, u, piby2_tail));
-
- z = transform ? zt : z;
-
- z = xexp < -56 ? piby2 : z;
- z = isnan(x) ? as_double((as_ulong(x) | QNANBITPATT_DP64)) : z;
- z = x == 1.0 ? 0.0 : z;
- z = x == -1.0 ? pi : z;
-
- return z;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, acos, double);
-
-#endif // cl_khr_fp64
-
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_UNARY_BUILTIN_FP16(acos)
-
-#endif
+#undef __CLC_FUNCTION
+#define __CLC_FUNCTION acos
+#include <clc/math/unary_builtin.inc>
diff --git a/libclc/generic/lib/math/asin.cl b/libclc/generic/lib/math/asin.cl
index 478f1b3c70377..360951c45eda5 100644
--- a/libclc/generic/lib/math/asin.cl
+++ b/libclc/generic/lib/math/asin.cl
@@ -8,145 +8,8 @@
#include <clc/clc.h>
#include <clc/clcmacro.h>
-#include <clc/math/math.h>
+#include <clc/math/clc_asin.h>
-_CLC_OVERLOAD _CLC_DEF float asin(float x) {
- // Computes arcsin(x).
- // The argument is first reduced by noting that arcsin(x)
- // is invalid for abs(x) > 1 and arcsin(-x) = -arcsin(x).
- // For denormal and small arguments arcsin(x) = x to machine
- // accuracy. Remaining argument ranges are handled as follows.
- // For abs(x) <= 0.5 use
- // arcsin(x) = x + x^3*R(x^2)
- // where R(x^2) is a rational minimax approximation to
- // (arcsin(x) - x)/x^3.
- // For abs(x) > 0.5 exploit the identity:
- // arcsin(x) = pi/2 - 2*arcsin(sqrt(1-x)/2)
- // together with the above rational approximation, and
- // reconstruct the terms carefully.
-
- const float piby2_tail = 7.5497894159e-08F; /* 0x33a22168 */
- const float hpiby2_head = 7.8539812565e-01F; /* 0x3f490fda */
- const float piby2 = 1.5707963705e+00F; /* 0x3fc90fdb */
-
- uint ux = as_uint(x);
- uint aux = ux & EXSIGNBIT_SP32;
- uint xs = ux ^ aux;
- float spiby2 = as_float(xs | as_uint(piby2));
- int xexp = (int)(aux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
- float y = as_float(aux);
-
- // abs(x) >= 0.5
- int transform = xexp >= -1;
-
- float y2 = y * y;
- float rt = 0.5f * (1.0f - y);
- float r = transform ? rt : y2;
-
- // Use a rational approximation for [0.0, 0.5]
- float a = mad(r,
- mad(r,
- mad(r, -0.00396137437848476485201154797087F, -0.0133819288943925804214011424456F),
- -0.0565298683201845211985026327361F),
- 0.184161606965100694821398249421F);
-
- float b = mad(r, -0.836411276854206731913362287293F, 1.10496961524520294485512696706F);
- float u = r * MATH_DIVIDE(a, b);
-
- float s = MATH_SQRT(r);
- float s1 = as_float(as_uint(s) & 0xffff0000);
- float c = MATH_DIVIDE(mad(-s1, s1, r), s + s1);
- float p = mad(2.0f*s, u, -mad(c, -2.0f, piby2_tail));
- float q = mad(s1, -2.0f, hpiby2_head);
- float vt = hpiby2_head - (p - q);
- float v = mad(y, u, y);
- v = transform ? vt : v;
-
- float ret = as_float(xs | as_uint(v));
- ret = aux > 0x3f800000U ? as_float(QNANBITPATT_SP32) : ret;
- ret = aux == 0x3f800000U ? spiby2 : ret;
- ret = xexp < -14 ? x : ret;
-
- return ret;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, asin, float);
-
-#ifdef cl_khr_fp64
-
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-_CLC_OVERLOAD _CLC_DEF double asin(double x) {
- // Computes arcsin(x).
- // The argument is first reduced by noting that arcsin(x)
- // is invalid for abs(x) > 1 and arcsin(-x) = -arcsin(x).
- // For denormal and small arguments arcsin(x) = x to machine
- // accuracy. Remaining argument ranges are handled as follows.
- // For abs(x) <= 0.5 use
- // arcsin(x) = x + x^3*R(x^2)
- // where R(x^2) is a rational minimax approximation to
- // (arcsin(x) - x)/x^3.
- // For abs(x) > 0.5 exploit the identity:
- // arcsin(x) = pi/2 - 2*arcsin(sqrt(1-x)/2)
- // together with the above rational approximation, and
- // reconstruct the terms carefully.
-
- const double piby2_tail = 6.1232339957367660e-17; /* 0x3c91a62633145c07 */
- const double hpiby2_head = 7.8539816339744831e-01; /* 0x3fe921fb54442d18 */
- const double piby2 = 1.5707963267948965e+00; /* 0x3ff921fb54442d18 */
-
- double y = fabs(x);
- int xneg = as_int2(x).hi < 0;
- int xexp = (as_int2(y).hi >> 20) - EXPBIAS_DP64;
-
- // abs(x) >= 0.5
- int transform = xexp >= -1;
-
- double rt = 0.5 * (1.0 - y);
- double y2 = y * y;
- double r = transform ? rt : y2;
-
- // Use a rational approximation for [0.0, 0.5]
-
- double un = fma(r,
- fma(r,
- fma(r,
- fma(r,
- fma(r, 0.0000482901920344786991880522822991,
- 0.00109242697235074662306043804220),
- -0.0549989809235685841612020091328),
- 0.275558175256937652532686256258),
- -0.445017216867635649900123110649),
- 0.227485835556935010735943483075);
-
- double ud = fma(r,
- fma(r,
- fma(r,
- fma(r, 0.105869422087204370341222318533,
- -0.943639137032492685763471240072),
- 2.76568859157270989520376345954),
- -3.28431505720958658909889444194),
- 1.36491501334161032038194214209);
-
- double u = r * MATH_DIVIDE(un, ud);
-
- // Reconstruct asin carefully in transformed region
- double s = sqrt(r);
- double sh = as_double(as_ulong(s) & 0xffffffff00000000UL);
- double c = MATH_DIVIDE(fma(-sh, sh, r), s + sh);
- double p = fma(2.0*s, u, -fma(-2.0, c, piby2_tail));
- double q = fma(-2.0, sh, hpiby2_head);
- double vt = hpiby2_head - (p - q);
- double v = fma(y, u, y);
- v = transform ? vt : v;
-
- v = xexp < -28 ? y : v;
- v = xexp >= 0 ? as_double(QNANBITPATT_DP64) : v;
- v = y == 1.0 ? piby2 : v;
-
- return xneg ? -v : v;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, asin, double);
-
-#endif // cl_khr_fp64
+#undef __CLC_FUNCTION
+#define __CLC_FUNCTION asin
+#include <clc/math/unary_builtin.inc>
diff --git a/libclc/generic/lib/math/atan.cl b/libclc/generic/lib/math/atan.cl
index f7f64e97b45ac..b7d1516ae641b 100644
--- a/libclc/generic/lib/math/atan.cl
+++ b/libclc/generic/lib/math/atan.cl
@@ -8,169 +8,8 @@
#include <clc/clc.h>
#include <clc/clcmacro.h>
-#include <clc/math/math.h>
+#include <clc/math/clc_atan.h>
-_CLC_OVERLOAD _CLC_DEF float atan(float x)
-{
- const float piby2 = 1.5707963267948966f; // 0x3ff921fb54442d18
-
- uint ux = as_uint(x);
- uint aux = ux & EXSIGNBIT_SP32;
- uint sx = ux ^ aux;
-
- float spiby2 = as_float(sx | as_uint(piby2));
-
- float v = as_float(aux);
-
- // Return for NaN
- float 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);
- float c = 1.57079632679489655800f; // atan(infinity)
-
- // 19/16 <= x < 39/16
- int l = aux < 0x401c0000;
- float xx = MATH_DIVIDE(v - 1.5f, 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(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;
-
- // Core approximation: Remez(2,2) on [-7/16,7/16]
-
- float s = x * x;
- float a = mad(s,
- mad(s, 0.470677934286149214138357545549e-2f, 0.192324546402108583211697690500f),
- 0.296528598819239217902158651186f);
-
- float b = mad(s,
- mad(s, 0.299309699959659728404442796915f, 0.111072499995399550138837673349e1f),
- 0.889585796862432286486651434570f);
-
- float q = x * s * MATH_DIVIDE(a, b);
-
- float z = c - (q - x);
- float zs = as_float(sx | as_uint(z));
-
- ret = aux < 0x4c800000 ? zs : ret;
-
- // |x| < 2^-19
- ret = aux < 0x36000000 ? as_float(ux) : ret;
- return ret;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, atan, float);
-
-#ifdef cl_khr_fp64
-
-#pragma OPENCL EXTENSION cl_khr_fp64 : enable
-
-
-_CLC_OVERLOAD _CLC_DEF double atan(double x)
-{
- const double piby2 = 1.5707963267948966e+00; // 0x3ff921fb54442d18
-
- double v = fabs(x);
-
- // 2^56 > v > 39/16
- double a = -1.0;
- double b = v;
- // (chi + clo) = arctan(infinity)
- double chi = 1.57079632679489655800e+00;
- double clo = 6.12323399573676480327e-17;
-
- double ta = v - 1.5;
- double tb = 1.0 + 1.5 * v;
- int 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]
- double r = a / b;
- double s = r * r;
- double qn = fma(s,
- fma(s,
- fma(s,
- fma(s, 0.142316903342317766e-3,
- 0.304455919504853031e-1),
- 0.220638780716667420e0),
- 0.447677206805497472e0),
- 0.268297920532545909e0);
-
- double qd = fma(s,
- fma(s,
- fma(s,
- fma(s, 0.389525873944742195e-1,
- 0.424602594203847109e0),
- 0.141254259931958921e1),
- 0.182596787737507063e1),
- 0.804893761597637733e0);
-
- double q = r * s * qn / qd;
- r = chi - ((q - clo) - r);
-
- double z = isnan(x) ? x : piby2;
- z = v <= 0x1.0p+56 ? r : z;
- z = v < 0x1.0p-26 ? v : z;
- return x == v ? z : -z;
-}
-
-_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, atan, double);
-
-#endif // cl_khr_fp64
-
-#ifdef cl_khr_fp16
-
-#pragma OPENCL EXTENSION cl_khr_fp16 : enable
-
-_CLC_DEFINE_UNARY_BUILTIN_FP16(atan)
-
-#endif
+#undef __CLC_FUNCTION
+#define __CLC_FUNCTION atan
+#include <clc/math/unary_builtin.inc>
More information about the cfe-commits
mailing list