Skip to content

Commit 3013458

Browse files
authored
[libclc] Move asinpi/acospi/atanpi to the CLC library (#132918)
Similar to d46a699, this commit simultaneously moves these three functions to the CLC library and optimizes them for vector types by avoiding scalarization.
1 parent 449e3fa commit 3013458

File tree

13 files changed

+616
-464
lines changed

13 files changed

+616
-464
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_MATH_CLC_ACOSPI_H__
10+
#define __CLC_MATH_CLC_ACOSPI_H__
11+
12+
#define __CLC_BODY <clc/math/unary_decl.inc>
13+
#define __CLC_FUNCTION __clc_acospi
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
18+
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_ACOSPI_H__
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_MATH_CLC_ASINPI_H__
10+
#define __CLC_MATH_CLC_ASINPI_H__
11+
12+
#define __CLC_BODY <clc/math/unary_decl.inc>
13+
#define __CLC_FUNCTION __clc_asinpi
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
18+
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_ASINPI_H__
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __CLC_MATH_CLC_ATANPI_H__
10+
#define __CLC_MATH_CLC_ATANPI_H__
11+
12+
#define __CLC_BODY <clc/math/unary_decl.inc>
13+
#define __CLC_FUNCTION __clc_atanpi
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
18+
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_ATANPI_H__

libclc/clc/lib/generic/SOURCES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ integer/clc_rotate.cl
1818
integer/clc_sub_sat.cl
1919
integer/clc_upsample.cl
2020
math/clc_acos.cl
21+
math/clc_acospi.cl
2122
math/clc_asin.cl
23+
math/clc_asinpi.cl
2224
math/clc_atan.cl
25+
math/clc_atanpi.cl
2326
math/clc_ceil.cl
2427
math/clc_copysign.cl
2528
math/clc_fabs.cl
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/clc_convert.h>
10+
#include <clc/float/definitions.h>
11+
#include <clc/internal/clc.h>
12+
#include <clc/math/clc_fabs.h>
13+
#include <clc/math/clc_fma.h>
14+
#include <clc/math/clc_mad.h>
15+
#include <clc/math/clc_sqrt.h>
16+
#include <clc/math/math.h>
17+
18+
#define __CLC_BODY <clc_acospi.inc>
19+
#include <clc/math/gentype.inc>
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Computes arccos(x).
10+
//
11+
// The incoming argument is first reduced by noting that arccos(x) is invalid
12+
// for abs(x) > 1.
13+
//
14+
// For denormal and small arguments arccos(x) = pi/2 to machine accuracy.
15+
//
16+
// Remaining argument ranges are handled as follows:
17+
// * For abs(x) <= 0.5 use:
18+
// arccos(x) = pi/2 - arcsin(x) = pi/2 - (x + x^3 * R(x^2))
19+
// where R(x^2) is a rational minimax approximation to (arcsin(x) - x)/x^3.
20+
// * For abs(x) > 0.5 exploit the identity:
21+
// arccos(x) = pi - 2 * arcsin(sqrt(1 - x)/2)
22+
// together with the above rational approximation, and reconstruct the terms
23+
// carefully.
24+
//
25+
//===----------------------------------------------------------------------===//
26+
27+
#if __CLC_FPSIZE == 32
28+
29+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acospi(__CLC_GENTYPE x) {
30+
// Some constants and split constants.
31+
const __CLC_GENTYPE pi = __CLC_FP_LIT(3.1415926535897933e+00);
32+
// 0x3ff921fb54442d18
33+
const __CLC_GENTYPE piby2_head = __CLC_FP_LIT(1.5707963267948965580e+00);
34+
// 0x3c91a62633145c07
35+
const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(6.12323399573676603587e-17);
36+
37+
__CLC_UINTN ux = __CLC_AS_UINTN(x);
38+
__CLC_UINTN aux = ux & ~SIGNBIT_SP32;
39+
__CLC_INTN xneg = ux != aux;
40+
__CLC_INTN xexp = __CLC_AS_INTN(aux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
41+
42+
__CLC_GENTYPE y = __CLC_AS_GENTYPE(aux);
43+
44+
// transform if |x| >= 0.5
45+
__CLC_INTN transform = xexp >= -1;
46+
47+
__CLC_GENTYPE y2 = y * y;
48+
__CLC_GENTYPE yt = 0.5f * (1.0f - y);
49+
__CLC_GENTYPE r = transform ? yt : y2;
50+
51+
// Use a rational approximation for [0.0, 0.5]
52+
__CLC_GENTYPE a =
53+
__clc_mad(r,
54+
__clc_mad(r,
55+
__clc_mad(r, -0.00396137437848476485201154797087F,
56+
-0.0133819288943925804214011424456F),
57+
-0.0565298683201845211985026327361F),
58+
0.184161606965100694821398249421F);
59+
__CLC_GENTYPE b = __clc_mad(r, -0.836411276854206731913362287293F,
60+
1.10496961524520294485512696706F);
61+
__CLC_GENTYPE u = r * MATH_DIVIDE(a, b);
62+
63+
__CLC_GENTYPE s = __clc_sqrt(r);
64+
y = s;
65+
__CLC_GENTYPE s1 = __CLC_AS_GENTYPE(__CLC_AS_UINTN(s) & 0xffff0000);
66+
__CLC_GENTYPE c = MATH_DIVIDE(r - s1 * s1, s + s1);
67+
__CLC_GENTYPE rettn =
68+
1.0f - MATH_DIVIDE(2.0f * (s + __clc_mad(y, u, -piby2_tail)), pi);
69+
__CLC_GENTYPE rettp = MATH_DIVIDE(2.0f * (s1 + __clc_mad(y, u, c)), pi);
70+
__CLC_GENTYPE rett = xneg ? rettn : rettp;
71+
__CLC_GENTYPE ret =
72+
MATH_DIVIDE(piby2_head - (x - __clc_mad(x, -u, piby2_tail)), pi);
73+
74+
ret = transform ? rett : ret;
75+
ret = aux > 0x3f800000U ? __CLC_GENTYPE_NAN : ret;
76+
ret = ux == 0x3f800000U ? 0.0f : ret;
77+
ret = ux == 0xbf800000U ? 1.0f : ret;
78+
ret = xexp < -26 ? 0.5f : ret;
79+
return ret;
80+
}
81+
82+
#elif __CLC_FPSIZE == 64
83+
84+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acospi(__CLC_GENTYPE x) {
85+
const __CLC_GENTYPE pi = __CLC_FP_LIT(0x1.921fb54442d18p+1);
86+
// 0x3c91a62633145c07
87+
const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(6.12323399573676603587e-17);
88+
89+
__CLC_GENTYPE y = __clc_fabs(x);
90+
__CLC_LONGN xneg = x < __CLC_FP_LIT(0.0);
91+
__CLC_INTN xexp = __CLC_CONVERT_INTN(
92+
(__CLC_AS_ULONGN(y) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64);
93+
94+
// abs(x) >= 0.5
95+
__CLC_LONGN transform = __CLC_CONVERT_LONGN(xexp >= -1);
96+
97+
// Transform y into the range [0,0.5)
98+
__CLC_GENTYPE r1 = 0.5 * (1.0 - y);
99+
__CLC_GENTYPE s = __clc_sqrt(r1);
100+
__CLC_GENTYPE r = y * y;
101+
r = transform ? r1 : r;
102+
y = transform ? s : y;
103+
104+
// Use a rational approximation for [0.0, 0.5]
105+
__CLC_GENTYPE un = __clc_fma(
106+
r,
107+
__clc_fma(
108+
r,
109+
__clc_fma(r,
110+
__clc_fma(r,
111+
__clc_fma(r, 0.0000482901920344786991880522822991,
112+
0.00109242697235074662306043804220),
113+
-0.0549989809235685841612020091328),
114+
0.275558175256937652532686256258),
115+
-0.445017216867635649900123110649),
116+
0.227485835556935010735943483075);
117+
118+
__CLC_GENTYPE ud = __clc_fma(
119+
r,
120+
__clc_fma(r,
121+
__clc_fma(r,
122+
__clc_fma(r, 0.105869422087204370341222318533,
123+
-0.943639137032492685763471240072),
124+
2.76568859157270989520376345954),
125+
-3.28431505720958658909889444194),
126+
1.36491501334161032038194214209);
127+
128+
__CLC_GENTYPE u = r * MATH_DIVIDE(un, ud);
129+
130+
// Reconstruct acos carefully in transformed region
131+
__CLC_GENTYPE res1 =
132+
__clc_fma(-2.0, MATH_DIVIDE(s + __clc_fma(y, u, -piby2_tail), pi), 1.0);
133+
__CLC_GENTYPE s1 =
134+
__CLC_AS_GENTYPE(__CLC_AS_ULONGN(s) & 0xffffffff00000000UL);
135+
__CLC_GENTYPE c = MATH_DIVIDE(__clc_fma(-s1, s1, r), s + s1);
136+
__CLC_GENTYPE res2 =
137+
MATH_DIVIDE(__clc_fma(2.0, s1, __clc_fma(2.0, c, 2.0 * y * u)), pi);
138+
res1 = xneg ? res1 : res2;
139+
res2 = 0.5 - __clc_fma(x, u, x) / pi;
140+
res1 = transform ? res1 : res2;
141+
142+
res2 = x == 1.0 ? 0.0 : __CLC_GENTYPE_NAN;
143+
res2 = x == -1.0 ? 1.0 : res2;
144+
res1 = __CLC_CONVERT_LONGN(xexp >= 0) ? res2 : res1;
145+
res1 = __CLC_CONVERT_LONGN(xexp < -56) ? 0.5 : res1;
146+
147+
return res1;
148+
}
149+
150+
#elif __CLC_FPSIZE == 16
151+
152+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acospi(__CLC_GENTYPE x) {
153+
return __CLC_CONVERT_GENTYPE(__clc_acospi(__CLC_CONVERT_FLOATN(x)));
154+
}
155+
156+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/clc_convert.h>
10+
#include <clc/float/definitions.h>
11+
#include <clc/internal/clc.h>
12+
#include <clc/math/clc_fabs.h>
13+
#include <clc/math/clc_fma.h>
14+
#include <clc/math/clc_mad.h>
15+
#include <clc/math/clc_sqrt.h>
16+
#include <clc/math/math.h>
17+
18+
#define __CLC_BODY <clc_asinpi.inc>
19+
#include <clc/math/gentype.inc>

0 commit comments

Comments
 (0)