[libclc] Move asinpi/acospi/atanpi to the CLC library (#132918)

Similar to d46a6999, this commit simultaneously moves these three
functions to the CLC library and optimizes them for vector types by
avoiding scalarization.
This commit is contained in:
Fraser Cormack 2025-03-25 13:31:53 +00:00 committed by GitHub
parent 449e3fad62
commit 3013458a79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 616 additions and 464 deletions

View File

@ -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_ACOSPI_H__
#define __CLC_MATH_CLC_ACOSPI_H__
#define __CLC_BODY <clc/math/unary_decl.inc>
#define __CLC_FUNCTION __clc_acospi
#include <clc/math/gentype.inc>
#undef __CLC_BODY
#undef __CLC_FUNCTION
#endif // __CLC_MATH_CLC_ACOSPI_H__

View File

@ -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_ASINPI_H__
#define __CLC_MATH_CLC_ASINPI_H__
#define __CLC_BODY <clc/math/unary_decl.inc>
#define __CLC_FUNCTION __clc_asinpi
#include <clc/math/gentype.inc>
#undef __CLC_BODY
#undef __CLC_FUNCTION
#endif // __CLC_MATH_CLC_ASINPI_H__

View File

@ -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_ATANPI_H__
#define __CLC_MATH_CLC_ATANPI_H__
#define __CLC_BODY <clc/math/unary_decl.inc>
#define __CLC_FUNCTION __clc_atanpi
#include <clc/math/gentype.inc>
#undef __CLC_BODY
#undef __CLC_FUNCTION
#endif // __CLC_MATH_CLC_ATANPI_H__

View File

@ -18,8 +18,11 @@ integer/clc_rotate.cl
integer/clc_sub_sat.cl
integer/clc_upsample.cl
math/clc_acos.cl
math/clc_acospi.cl
math/clc_asin.cl
math/clc_asinpi.cl
math/clc_atan.cl
math/clc_atanpi.cl
math/clc_ceil.cl
math/clc_copysign.cl
math/clc_fabs.cl

View File

@ -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_acospi.inc>
#include <clc/math/gentype.inc>

View File

@ -0,0 +1,156 @@
//===----------------------------------------------------------------------===//
//
// 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_acospi(__CLC_GENTYPE x) {
// Some constants and split constants.
const __CLC_GENTYPE pi = __CLC_FP_LIT(3.1415926535897933e+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_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(r - s1 * s1, s + s1);
__CLC_GENTYPE rettn =
1.0f - MATH_DIVIDE(2.0f * (s + __clc_mad(y, u, -piby2_tail)), pi);
__CLC_GENTYPE rettp = MATH_DIVIDE(2.0f * (s1 + __clc_mad(y, u, c)), pi);
__CLC_GENTYPE rett = xneg ? rettn : rettp;
__CLC_GENTYPE ret =
MATH_DIVIDE(piby2_head - (x - __clc_mad(x, -u, piby2_tail)), pi);
ret = transform ? rett : ret;
ret = aux > 0x3f800000U ? __CLC_GENTYPE_NAN : ret;
ret = ux == 0x3f800000U ? 0.0f : ret;
ret = ux == 0xbf800000U ? 1.0f : ret;
ret = xexp < -26 ? 0.5f : ret;
return ret;
}
#elif __CLC_FPSIZE == 64
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acospi(__CLC_GENTYPE x) {
const __CLC_GENTYPE pi = __CLC_FP_LIT(0x1.921fb54442d18p+1);
// 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);
// Transform y into the range [0,0.5)
__CLC_GENTYPE r1 = 0.5 * (1.0 - y);
__CLC_GENTYPE s = __clc_sqrt(r1);
__CLC_GENTYPE r = y * y;
r = transform ? r1 : r;
y = transform ? s : y;
// 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 res1 =
__clc_fma(-2.0, MATH_DIVIDE(s + __clc_fma(y, u, -piby2_tail), pi), 1.0);
__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 res2 =
MATH_DIVIDE(__clc_fma(2.0, s1, __clc_fma(2.0, c, 2.0 * y * u)), pi);
res1 = xneg ? res1 : res2;
res2 = 0.5 - __clc_fma(x, u, x) / pi;
res1 = transform ? res1 : res2;
res2 = x == 1.0 ? 0.0 : __CLC_GENTYPE_NAN;
res2 = x == -1.0 ? 1.0 : res2;
res1 = __CLC_CONVERT_LONGN(xexp >= 0) ? res2 : res1;
res1 = __CLC_CONVERT_LONGN(xexp < -56) ? 0.5 : res1;
return res1;
}
#elif __CLC_FPSIZE == 16
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acospi(__CLC_GENTYPE x) {
return __CLC_CONVERT_GENTYPE(__clc_acospi(__CLC_CONVERT_FLOATN(x)));
}
#endif

View File

@ -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_asinpi.inc>
#include <clc/math/gentype.inc>

View File

@ -0,0 +1,156 @@
//===----------------------------------------------------------------------===//
//
// 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_asinpi(__CLC_GENTYPE x) {
const __CLC_GENTYPE pi = __CLC_FP_LIT(3.1415926535897933e+00);
// 0x33a22168
const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(7.5497894159e-08);
// 0x3f490fda
const __CLC_GENTYPE hpiby2_head = __CLC_FP_LIT(7.8539812565e-01);
__CLC_UINTN ux = __CLC_AS_UINTN(x);
__CLC_UINTN aux = ux & EXSIGNBIT_SP32;
__CLC_UINTN xs = ux ^ aux;
__CLC_GENTYPE shalf =
__CLC_AS_GENTYPE(xs | __CLC_AS_UINTN((__CLC_GENTYPE)__CLC_FP_LIT(0.5)));
__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 = 0.5f * (1.0f - 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;
v = MATH_DIVIDE(v, pi);
__CLC_GENTYPE xbypi = MATH_DIVIDE(x, pi);
__CLC_GENTYPE ret = __CLC_AS_GENTYPE(xs | __CLC_AS_UINTN(v));
ret = aux > 0x3f800000U ? __CLC_GENTYPE_NAN : ret;
ret = aux == 0x3f800000U ? shalf : ret;
ret = xexp < -14 ? xbypi : ret;
return ret;
}
#elif __CLC_FPSIZE == 64
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_asinpi(__CLC_GENTYPE x) {
const __CLC_GENTYPE pi = __CLC_FP_LIT(0x1.921fb54442d18p+1);
// 0x3c91a62633145c07
const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(6.1232339957367660e-17);
// 0x3fe921fb54442d18
const __CLC_GENTYPE hpiby2_head = __CLC_FP_LIT(7.8539816339744831e-01);
__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 = 0.5 * (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 = MATH_DIVIDE(v, pi);
v = __CLC_CONVERT_LONGN(xexp >= 0) ? __CLC_GENTYPE_NAN : v;
v = y == 1.0 ? 0.5 : v;
return xneg ? -v : v;
}
#elif __CLC_FPSIZE == 16
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_asinpi(__CLC_GENTYPE x) {
return __CLC_CONVERT_GENTYPE(__clc_asinpi(__CLC_CONVERT_FLOATN(x)));
}
#endif

View File

@ -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_atanpi.inc>
#include <clc/math/gentype.inc>

View File

@ -0,0 +1,172 @@
//===----------------------------------------------------------------------===//
//
// 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_atanpi(__CLC_GENTYPE x) {
const __CLC_GENTYPE pi = __CLC_FP_LIT(3.1415926535897932);
__CLC_UINTN ux = __CLC_AS_UINTN(x);
__CLC_UINTN aux = ux & EXSIGNBIT_SP32;
__CLC_UINTN sx = ux ^ aux;
__CLC_GENTYPE xbypi = MATH_DIVIDE(x, pi);
__CLC_GENTYPE shalf =
__CLC_AS_GENTYPE(sx | __CLC_AS_UINTN((__CLC_GENTYPE)__CLC_FP_LIT(0.5)));
__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 ? shalf : 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);
z = MATH_DIVIDE(z, pi);
__CLC_GENTYPE zs = __CLC_AS_GENTYPE(sx | __CLC_AS_UINTN(z));
ret = aux < 0x4c800000 ? zs : ret;
// |x| < 2^-19
ret = aux < 0x36000000 ? xbypi : ret;
return ret;
}
#elif __CLC_FPSIZE == 64
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atanpi(__CLC_GENTYPE x) {
const __CLC_GENTYPE pi = __CLC_FP_LIT(0x1.921fb54442d18p+1);
__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)) / pi;
__CLC_GENTYPE vp = v / pi;
__CLC_GENTYPE z = __clc_isnan(x) ? x : 0.5;
z = v <= 0x1.0p+56 ? r : z;
z = v < 0x1.0p-26 ? vp : z;
return x == v ? z : -z;
}
#elif __CLC_FPSIZE == 16
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_atanpi(__CLC_GENTYPE x) {
return __CLC_CONVERT_GENTYPE(__clc_atanpi(__CLC_CONVERT_FLOATN(x)));
}
#endif

View File

@ -7,159 +7,9 @@
//===----------------------------------------------------------------------===//
#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/math/math.h>
#include <clc/math/clc_acospi.h>
_CLC_OVERLOAD _CLC_DEF float acospi(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.
#undef __CLC_FUNCTION
#define __CLC_FUNCTION acospi
#include <clc/math/unary_builtin.inc>
// Some constants and split constants.
const float pi = 3.1415926535897933e+00f;
const float piby2_head = 1.5707963267948965580e+00f; /* 0x3ff921fb54442d18 */
const float piby2_tail = 6.12323399573676603587e-17f; /* 0x3c91a62633145c07 */
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(r - s1 * s1, s + s1);
// float rettn = 1.0f - MATH_DIVIDE(2.0f * (s + (y * u - piby2_tail)), pi);
float rettn = 1.0f - MATH_DIVIDE(2.0f * (s + mad(y, u, -piby2_tail)), pi);
// float rettp = MATH_DIVIDE(2.0F * s1 + (2.0F * c + 2.0F * y * u), pi);
float rettp = MATH_DIVIDE(2.0f*(s1 + mad(y, u, c)), pi);
float rett = xneg ? rettn : rettp;
// float ret = MATH_DIVIDE(piby2_head - (x - (piby2_tail - x * u)), pi);
float ret = MATH_DIVIDE(piby2_head - (x - mad(x, -u, piby2_tail)), pi);
ret = transform ? rett : ret;
ret = aux > 0x3f800000U ? as_float(QNANBITPATT_SP32) : ret;
ret = ux == 0x3f800000U ? 0.0f : ret;
ret = ux == 0xbf800000U ? 1.0f : ret;
ret = xexp < -26 ? 0.5f : ret;
return ret;
}
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, acospi, float)
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
_CLC_OVERLOAD _CLC_DEF double acospi(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 = 0x1.921fb54442d18p+1;
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;
// Transform y into the range [0,0.5)
double r1 = 0.5 * (1.0 - y);
double s = sqrt(r1);
double r = y * y;
r = transform ? r1 : r;
y = transform ? s : y;
// 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 res1 = fma(-2.0, MATH_DIVIDE(s + fma(y, u, -piby2_tail), pi), 1.0);
double s1 = as_double(as_ulong(s) & 0xffffffff00000000UL);
double c = MATH_DIVIDE(fma(-s1, s1, r), s + s1);
double res2 = MATH_DIVIDE(fma(2.0, s1, fma(2.0, c, 2.0 * y * u)), pi);
res1 = xneg ? res1 : res2;
res2 = 0.5 - fma(x, u, x) / pi;
res1 = transform ? res1 : res2;
const double qnan = as_double(QNANBITPATT_DP64);
res2 = x == 1.0 ? 0.0 : qnan;
res2 = x == -1.0 ? 1.0 : res2;
res1 = xexp >= 0 ? res2 : res1;
res1 = xexp < -56 ? 0.5 : res1;
return res1;
}
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, acospi, double)
#endif
#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
_CLC_DEFINE_UNARY_BUILTIN_FP16(acospi)
#endif

View File

@ -7,149 +7,8 @@
//===----------------------------------------------------------------------===//
#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/math/math.h>
#include <clc/math/clc_asinpi.h>
_CLC_OVERLOAD _CLC_DEF float asinpi(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 pi = 3.1415926535897933e+00f;
const float piby2_tail = 7.5497894159e-08F; /* 0x33a22168 */
const float hpiby2_head = 7.8539812565e-01F; /* 0x3f490fda */
uint ux = as_uint(x);
uint aux = ux & EXSIGNBIT_SP32;
uint xs = ux ^ aux;
float shalf = as_float(xs | as_uint(0.5f));
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;
v = MATH_DIVIDE(v, pi);
float xbypi = MATH_DIVIDE(x, pi);
float ret = as_float(xs | as_uint(v));
ret = aux > 0x3f800000U ? as_float(QNANBITPATT_SP32) : ret;
ret = aux == 0x3f800000U ? shalf : ret;
ret = xexp < -14 ? xbypi : ret;
return ret;
}
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, asinpi, float)
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
_CLC_OVERLOAD _CLC_DEF double asinpi(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 pi = 0x1.921fb54442d18p+1;
const double piby2_tail = 6.1232339957367660e-17; /* 0x3c91a62633145c07 */
const double hpiby2_head = 7.8539816339744831e-01; /* 0x3fe921fb54442d18 */
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 = MATH_DIVIDE(v, pi);
v = xexp >= 0 ? as_double(QNANBITPATT_DP64) : v;
v = y == 1.0 ? 0.5 : v;
return xneg ? -v : v;
}
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, asinpi, double)
#endif
#undef __CLC_FUNCTION
#define __CLC_FUNCTION asinpi
#include <clc/math/unary_builtin.inc>

View File

@ -7,169 +7,8 @@
//===----------------------------------------------------------------------===//
#include <clc/clc.h>
#include <clc/clcmacro.h>
#include <clc/math/math.h>
#include <clc/math/clc_atanpi.h>
_CLC_OVERLOAD _CLC_DEF float atanpi(float x) {
const float pi = 3.1415926535897932f;
uint ux = as_uint(x);
uint aux = ux & EXSIGNBIT_SP32;
uint sx = ux ^ aux;
float xbypi = MATH_DIVIDE(x, pi);
float shalf = as_float(sx | as_uint(0.5f));
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 ? shalf : 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);
z = MATH_DIVIDE(z, pi);
float zs = as_float(sx | as_uint(z));
ret = aux < 0x4c800000 ? zs : ret;
// |x| < 2^-19
ret = aux < 0x36000000 ? xbypi : ret;
return ret;
}
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, atanpi, float)
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
_CLC_OVERLOAD _CLC_DEF double atanpi(double x) {
const double pi = 0x1.921fb54442d18p+1;
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)) / pi;
double vp = v / pi;
double z = isnan(x) ? x : 0.5;
z = v <= 0x1.0p+56 ? r : z;
z = v < 0x1.0p-26 ? vp : z;
return x == v ? z : -z;
}
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, atanpi, double)
#endif
#ifdef cl_khr_fp16
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
_CLC_DEFINE_UNARY_BUILTIN_FP16(atanpi)
#endif
#undef __CLC_FUNCTION
#define __CLC_FUNCTION atanpi
#include <clc/math/unary_builtin.inc>