Skip to content

Commit d46a699

Browse files
authored
[libclc] Move asin/acos/atan to the CLC library (#132788)
This commit simultaneously moves these three functions to the CLC library and optimizing them for vector types by avoiding scalarization.
1 parent 6477945 commit d46a699

File tree

13 files changed

+613
-462
lines changed

13 files changed

+613
-462
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_ACOS_H__
10+
#define __CLC_MATH_CLC_ACOS_H__
11+
12+
#define __CLC_BODY <clc/math/unary_decl.inc>
13+
#define __CLC_FUNCTION __clc_acos
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
18+
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_ACOS_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_ASIN_H__
10+
#define __CLC_MATH_CLC_ASIN_H__
11+
12+
#define __CLC_BODY <clc/math/unary_decl.inc>
13+
#define __CLC_FUNCTION __clc_asin
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
18+
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_ASIN_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_ATAN_H__
10+
#define __CLC_MATH_CLC_ATAN_H__
11+
12+
#define __CLC_BODY <clc/math/unary_decl.inc>
13+
#define __CLC_FUNCTION __clc_atan
14+
15+
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
18+
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_ATAN_H__

libclc/clc/lib/generic/SOURCES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ integer/clc_rhadd.cl
1717
integer/clc_rotate.cl
1818
integer/clc_sub_sat.cl
1919
integer/clc_upsample.cl
20+
math/clc_acos.cl
21+
math/clc_asin.cl
22+
math/clc_atan.cl
2023
math/clc_ceil.cl
2124
math/clc_copysign.cl
2225
math/clc_fabs.cl
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+
#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+
#include <clc/relational/clc_isnan.h>
18+
19+
#define __CLC_BODY <clc_acos.inc>
20+
#include <clc/math/gentype.inc>
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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_acos(__CLC_GENTYPE x) {
30+
// Some constants and split constants.
31+
const __CLC_GENTYPE piby2 = __CLC_FP_LIT(1.5707963705e+00);
32+
const __CLC_GENTYPE pi = __CLC_FP_LIT(3.1415926535897933e+00);
33+
const __CLC_GENTYPE piby2_head = __CLC_FP_LIT(1.5707963267948965580e+00);
34+
const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(6.12323399573676603587e-17);
35+
36+
__CLC_UINTN ux = __CLC_AS_UINTN(x);
37+
__CLC_UINTN aux = ux & ~SIGNBIT_SP32;
38+
__CLC_INTN xneg = ux != aux;
39+
__CLC_INTN xexp = __CLC_AS_INTN(aux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;
40+
__CLC_GENTYPE y = __CLC_AS_GENTYPE(aux);
41+
42+
// transform if |x| >= 0.5
43+
__CLC_INTN transform = xexp >= -1;
44+
45+
__CLC_GENTYPE y2 = y * y;
46+
__CLC_GENTYPE yt = 0.5f * (1.0f - y);
47+
__CLC_GENTYPE r = transform ? yt : y2;
48+
49+
// Use a rational approximation for [0.0, 0.5]
50+
__CLC_GENTYPE a =
51+
__clc_mad(r,
52+
__clc_mad(r,
53+
__clc_mad(r, -0.00396137437848476485201154797087F,
54+
-0.0133819288943925804214011424456F),
55+
-0.0565298683201845211985026327361F),
56+
0.184161606965100694821398249421F);
57+
58+
__CLC_GENTYPE b = __clc_mad(r, -0.836411276854206731913362287293F,
59+
1.10496961524520294485512696706F);
60+
__CLC_GENTYPE u = r * MATH_DIVIDE(a, b);
61+
62+
__CLC_GENTYPE s = __clc_sqrt(r);
63+
y = s;
64+
__CLC_GENTYPE s1 = __CLC_AS_GENTYPE(__CLC_AS_UINTN(s) & 0xffff0000);
65+
__CLC_GENTYPE c = MATH_DIVIDE(__clc_mad(s1, -s1, r), s + s1);
66+
__CLC_GENTYPE rettn = __clc_mad(s + __clc_mad(y, u, -piby2_tail), -2.0f, pi);
67+
__CLC_GENTYPE rettp = 2.0F * (s1 + __clc_mad(y, u, c));
68+
__CLC_GENTYPE rett = xneg ? rettn : rettp;
69+
__CLC_GENTYPE ret = piby2_head - (x - __clc_mad(x, -u, piby2_tail));
70+
71+
ret = transform ? rett : ret;
72+
ret = aux > 0x3f800000U ? __CLC_GENTYPE_NAN : ret;
73+
ret = ux == 0x3f800000U ? 0.0f : ret;
74+
ret = ux == 0xbf800000U ? pi : ret;
75+
ret = xexp < -26 ? piby2 : ret;
76+
return ret;
77+
}
78+
79+
#elif __CLC_FPSIZE == 64
80+
81+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acos(__CLC_GENTYPE x) {
82+
// 0x400921fb54442d18
83+
const __CLC_GENTYPE pi = __CLC_FP_LIT(3.1415926535897933e+00);
84+
// 0x3ff921fb54442d18
85+
const __CLC_GENTYPE piby2 = __CLC_FP_LIT(1.5707963267948965580e+00);
86+
// 0x3ff921fb54442d18
87+
const __CLC_GENTYPE piby2_head = __CLC_FP_LIT(1.5707963267948965580e+00);
88+
// 0x3c91a62633145c07
89+
const __CLC_GENTYPE piby2_tail = __CLC_FP_LIT(6.12323399573676603587e-17);
90+
91+
__CLC_GENTYPE y = __clc_fabs(x);
92+
__CLC_LONGN xneg = x < __CLC_FP_LIT(0.0);
93+
__CLC_INTN xexp = __CLC_CONVERT_INTN(
94+
(__CLC_AS_ULONGN(y) >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64);
95+
96+
// abs(x) >= 0.5
97+
__CLC_LONGN transform = __CLC_CONVERT_LONGN(xexp >= -1);
98+
99+
__CLC_GENTYPE rt = __CLC_FP_LIT(0.5) * (__CLC_FP_LIT(1.0) - y);
100+
__CLC_GENTYPE y2 = y * y;
101+
__CLC_GENTYPE r = transform ? rt : y2;
102+
103+
// Use a rational approximation for [0.0, 0.5]
104+
__CLC_GENTYPE un = __clc_fma(
105+
r,
106+
__clc_fma(
107+
r,
108+
__clc_fma(r,
109+
__clc_fma(r,
110+
__clc_fma(r, 0.0000482901920344786991880522822991,
111+
0.00109242697235074662306043804220),
112+
-0.0549989809235685841612020091328),
113+
0.275558175256937652532686256258),
114+
-0.445017216867635649900123110649),
115+
0.227485835556935010735943483075);
116+
117+
__CLC_GENTYPE ud = __clc_fma(
118+
r,
119+
__clc_fma(r,
120+
__clc_fma(r,
121+
__clc_fma(r, 0.105869422087204370341222318533,
122+
-0.943639137032492685763471240072),
123+
2.76568859157270989520376345954),
124+
-3.28431505720958658909889444194),
125+
1.36491501334161032038194214209);
126+
127+
__CLC_GENTYPE u = r * MATH_DIVIDE(un, ud);
128+
129+
// Reconstruct acos carefully in transformed region
130+
__CLC_GENTYPE s = __clc_sqrt(r);
131+
__CLC_GENTYPE ztn = __clc_fma(-2.0, (s + __clc_fma(s, u, -piby2_tail)), pi);
132+
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 ztp = 2.0 * (s1 + __clc_fma(s, u, c));
137+
__CLC_GENTYPE zt = xneg ? ztn : ztp;
138+
__CLC_GENTYPE z = piby2_head - (x - __clc_fma(-x, u, piby2_tail));
139+
140+
z = transform ? zt : z;
141+
142+
z = __CLC_CONVERT_LONGN(xexp < -56) ? piby2 : z;
143+
z = __clc_isnan(x) ? __CLC_AS_GENTYPE((__CLC_AS_ULONGN(x) |
144+
(__CLC_ULONGN)QNANBITPATT_DP64))
145+
: z;
146+
z = x == 1.0 ? 0.0 : z;
147+
z = x == -1.0 ? pi : z;
148+
149+
return z;
150+
}
151+
152+
#elif __CLC_FPSIZE == 16
153+
154+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_acos(__CLC_GENTYPE x) {
155+
return __CLC_CONVERT_GENTYPE(__clc_acos(__CLC_CONVERT_FLOATN(x)));
156+
}
157+
158+
#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_asin.inc>
19+
#include <clc/math/gentype.inc>

0 commit comments

Comments
 (0)