Skip to content

Commit 754b12d

Browse files
wenju-heaokblast
authored andcommitted
[NFC][libclc] Simplify degrees, radians and smoothstep macros (llvm#164203)
Implementation doesn't change.
1 parent cbdfb86 commit 754b12d

File tree

9 files changed

+97
-145
lines changed

9 files changed

+97
-145
lines changed

libclc/clc/lib/generic/common/clc_degrees.cl

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/common/clc_degrees.h>
910
#include <clc/internal/clc.h>
1011

11-
#define DEGREES_SINGLE_DEF(TYPE, LITERAL) \
12-
_CLC_OVERLOAD _CLC_DEF TYPE __clc_degrees(TYPE radians) { \
13-
return (TYPE)LITERAL * radians; \
14-
}
15-
16-
#define DEGREES_DEF(TYPE, LITERAL) \
17-
DEGREES_SINGLE_DEF(TYPE, LITERAL) \
18-
DEGREES_SINGLE_DEF(TYPE##2, LITERAL) \
19-
DEGREES_SINGLE_DEF(TYPE##3, LITERAL) \
20-
DEGREES_SINGLE_DEF(TYPE##4, LITERAL) \
21-
DEGREES_SINGLE_DEF(TYPE##8, LITERAL) \
22-
DEGREES_SINGLE_DEF(TYPE##16, LITERAL)
23-
24-
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
25-
DEGREES_DEF(float, 0x1.ca5dc2p+5F)
26-
27-
#ifdef cl_khr_fp64
28-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
29-
30-
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
31-
DEGREES_DEF(double, 0x1.ca5dc1a63c1f8p+5)
32-
33-
#endif
34-
35-
#ifdef cl_khr_fp16
36-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
37-
38-
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
39-
DEGREES_DEF(half, (half)0x1.ca5dc1a63c1f8p+5)
40-
41-
#endif
12+
#define __CLC_BODY <clc_degrees.inc>
13+
#include <clc/math/gentype.inc>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
10+
#if __CLC_FPSIZE == 32
11+
#define DEGREE_LITERAL 0x1.ca5dc2p+5F
12+
#elif __CLC_FPSIZE == 64
13+
#define DEGREE_LITERAL 0x1.ca5dc1a63c1f8p+5
14+
#elif __CLC_FPSIZE == 16
15+
#define DEGREE_LITERAL (half)0x1.ca5dc1a63c1f8p+5
16+
#endif
17+
18+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_degrees(__CLC_GENTYPE radians) {
19+
return DEGREE_LITERAL * radians;
20+
}
21+
22+
#undef DEGREE_LITERAL

libclc/clc/lib/generic/common/clc_radians.cl

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/common/clc_radians.h>
910
#include <clc/internal/clc.h>
1011

11-
#define __CLC_RADIANS_SINGLE_DEF(TYPE, LITERAL) \
12-
_CLC_OVERLOAD _CLC_DEF TYPE __clc_radians(TYPE radians) { \
13-
return (TYPE)LITERAL * radians; \
14-
}
15-
16-
#define __CLC_RADIANS_DEF(TYPE, LITERAL) \
17-
__CLC_RADIANS_SINGLE_DEF(TYPE, LITERAL) \
18-
__CLC_RADIANS_SINGLE_DEF(TYPE##2, LITERAL) \
19-
__CLC_RADIANS_SINGLE_DEF(TYPE##3, LITERAL) \
20-
__CLC_RADIANS_SINGLE_DEF(TYPE##4, LITERAL) \
21-
__CLC_RADIANS_SINGLE_DEF(TYPE##8, LITERAL) \
22-
__CLC_RADIANS_SINGLE_DEF(TYPE##16, LITERAL)
23-
24-
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
25-
__CLC_RADIANS_DEF(float, 0x1.1df46ap-6F)
26-
27-
#ifdef cl_khr_fp64
28-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
29-
30-
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
31-
__CLC_RADIANS_DEF(double, 0x1.1df46a2529d39p-6)
32-
33-
#endif
34-
35-
#ifdef cl_khr_fp16
36-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
37-
38-
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
39-
__CLC_RADIANS_DEF(half, (half)0x1.1df46a2529d39p-6)
40-
41-
#endif
12+
#define __CLC_BODY <clc_radians.inc>
13+
#include <clc/math/gentype.inc>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
10+
#if __CLC_FPSIZE == 32
11+
#define RADIAN_LITERAL 0x1.1df46ap-6F
12+
#elif __CLC_FPSIZE == 64
13+
#define RADIAN_LITERAL 0x1.1df46a2529d39p-6
14+
#elif __CLC_FPSIZE == 16
15+
#define RADIAN_LITERAL (half)0x1.1df46a2529d39p-6
16+
#endif
17+
18+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_radians(__CLC_GENTYPE degrees) {
19+
return RADIAN_LITERAL * degrees;
20+
}
21+
22+
#undef RADIAN_LITERAL

libclc/clc/lib/generic/common/clc_smoothstep.cl

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,10 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
9+
#include <clc/common/clc_smoothstep.h>
810
#include <clc/internal/clc.h>
911
#include <clc/shared/clc_clamp.h>
1012

11-
#define SMOOTHSTEP_SINGLE_DEF(edge_type, x_type, lit_suff) \
12-
_CLC_OVERLOAD _CLC_DEF x_type __clc_smoothstep(edge_type edge0, \
13-
edge_type edge1, x_type x) { \
14-
x_type t = __clc_clamp((x - edge0) / (edge1 - edge0), 0.0##lit_suff, \
15-
1.0##lit_suff); \
16-
return t * t * (3.0##lit_suff - 2.0##lit_suff * t); \
17-
}
18-
19-
#define SMOOTHSTEP_DEF(type, lit_suffix) \
20-
SMOOTHSTEP_SINGLE_DEF(type, type, lit_suffix) \
21-
SMOOTHSTEP_SINGLE_DEF(type##2, type##2, lit_suffix) \
22-
SMOOTHSTEP_SINGLE_DEF(type##3, type##3, lit_suffix) \
23-
SMOOTHSTEP_SINGLE_DEF(type##4, type##4, lit_suffix) \
24-
SMOOTHSTEP_SINGLE_DEF(type##8, type##8, lit_suffix) \
25-
SMOOTHSTEP_SINGLE_DEF(type##16, type##16, lit_suffix)
26-
27-
SMOOTHSTEP_DEF(float, F)
28-
29-
#ifdef cl_khr_fp64
30-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
31-
SMOOTHSTEP_DEF(double, );
32-
#endif
33-
34-
#ifdef cl_khr_fp16
35-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
36-
SMOOTHSTEP_DEF(half, H);
37-
#endif
13+
#define __CLC_BODY <clc_smoothstep.inc>
14+
#include <clc/math/gentype.inc>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_smoothstep(__CLC_GENTYPE edge0,
10+
__CLC_GENTYPE edge1,
11+
__CLC_GENTYPE x) {
12+
__CLC_GENTYPE t = __clc_clamp((x - edge0) / (edge1 - edge0),
13+
__CLC_FP_LIT(0.0), __CLC_FP_LIT(1.0));
14+
return t * t * (__CLC_FP_LIT(3.0) - __CLC_FP_LIT(2.0) * t);
15+
}

libclc/clc/lib/generic/common/clc_step.cl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <clc/common/clc_step.h>
10+
#include <clc/internal/clc.h>
11+
912
#define __CLC_BODY <clc_step.inc>
1013
#include <clc/math/gentype.inc>

libclc/opencl/lib/generic/common/smoothstep.cl

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,59 +9,5 @@
99
#include <clc/common/clc_smoothstep.h>
1010
#include <clc/opencl/common/smoothstep.h>
1111

12-
#define SMOOTHSTEP_SINGLE_DEF(X_TYPE) \
13-
_CLC_OVERLOAD _CLC_DEF X_TYPE smoothstep(X_TYPE edge0, X_TYPE edge1, \
14-
X_TYPE x) { \
15-
return __clc_smoothstep(edge0, edge1, x); \
16-
}
17-
18-
#define SMOOTHSTEP_S_S_V_DEFS(X_TYPE) \
19-
_CLC_OVERLOAD _CLC_DEF X_TYPE##2 smoothstep(X_TYPE x, X_TYPE y, \
20-
X_TYPE##2 z) { \
21-
return __clc_smoothstep((X_TYPE##2)x, (X_TYPE##2)y, z); \
22-
} \
23-
\
24-
_CLC_OVERLOAD _CLC_DEF X_TYPE##3 smoothstep(X_TYPE x, X_TYPE y, \
25-
X_TYPE##3 z) { \
26-
return __clc_smoothstep((X_TYPE##3)x, (X_TYPE##3)y, z); \
27-
} \
28-
\
29-
_CLC_OVERLOAD _CLC_DEF X_TYPE##4 smoothstep(X_TYPE x, X_TYPE y, \
30-
X_TYPE##4 z) { \
31-
return __clc_smoothstep((X_TYPE##4)x, (X_TYPE##4)y, z); \
32-
} \
33-
\
34-
_CLC_OVERLOAD _CLC_DEF X_TYPE##8 smoothstep(X_TYPE x, X_TYPE y, \
35-
X_TYPE##8 z) { \
36-
return __clc_smoothstep((X_TYPE##8)x, (X_TYPE##8)y, z); \
37-
} \
38-
\
39-
_CLC_OVERLOAD _CLC_DEF X_TYPE##16 smoothstep(X_TYPE x, X_TYPE y, \
40-
X_TYPE##16 z) { \
41-
return __clc_smoothstep((X_TYPE##16)x, (X_TYPE##16)y, z); \
42-
}
43-
44-
#define SMOOTHSTEP_DEF(type) \
45-
SMOOTHSTEP_SINGLE_DEF(type) \
46-
SMOOTHSTEP_SINGLE_DEF(type##2) \
47-
SMOOTHSTEP_SINGLE_DEF(type##3) \
48-
SMOOTHSTEP_SINGLE_DEF(type##4) \
49-
SMOOTHSTEP_SINGLE_DEF(type##8) \
50-
SMOOTHSTEP_SINGLE_DEF(type##16) \
51-
SMOOTHSTEP_S_S_V_DEFS(type)
52-
53-
SMOOTHSTEP_DEF(float)
54-
55-
#ifdef cl_khr_fp64
56-
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
57-
58-
SMOOTHSTEP_DEF(double);
59-
60-
#endif
61-
62-
#ifdef cl_khr_fp16
63-
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
64-
65-
SMOOTHSTEP_DEF(half);
66-
67-
#endif
12+
#define __CLC_BODY <smoothstep.inc>
13+
#include <clc/math/gentype.inc>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE smoothstep(__CLC_GENTYPE edge0,
10+
__CLC_GENTYPE edge1,
11+
__CLC_GENTYPE x) {
12+
return __clc_smoothstep(edge0, edge1, x);
13+
}
14+
15+
#if !defined(__CLC_SCALAR)
16+
17+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE smoothstep(__CLC_SCALAR_GENTYPE edge0,
18+
__CLC_SCALAR_GENTYPE edge1,
19+
__CLC_GENTYPE x) {
20+
return __clc_smoothstep((__CLC_GENTYPE)edge0, (__CLC_GENTYPE)edge1, x);
21+
}
22+
23+
#endif

0 commit comments

Comments
 (0)