Skip to content

Commit a90b5b1

Browse files
authored
[libclc] Move degrees/radians to CLC library & optimize (#123222)
Missing half variants were also added. The builtins are now consistently emitted in vector form (i.e., with a splat of the literal to the appropriate vector size).
1 parent 58fc802 commit a90b5b1

File tree

9 files changed

+160
-24
lines changed

9 files changed

+160
-24
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __CLC_MATH_CLC_DEGREES_H__
2+
#define __CLC_MATH_CLC_DEGREES_H__
3+
4+
#define __CLC_BODY <clc/math/unary_decl.inc>
5+
#define __CLC_FUNCTION __clc_degrees
6+
7+
#include <clc/math/gentype.inc>
8+
9+
#undef __CLC_BODY
10+
#undef __CLC_FUNCTION
11+
12+
#endif // __CLC_MATH_CLC_DEGREES_H__
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __CLC_MATH_CLC_RADIANS_H__
2+
#define __CLC_MATH_CLC_RADIANS_H__
3+
4+
#define __CLC_BODY <clc/math/unary_decl.inc>
5+
#define __CLC_FUNCTION __clc_radians
6+
7+
#include <clc/math/gentype.inc>
8+
9+
#undef __CLC_BODY
10+
#undef __CLC_FUNCTION
11+
12+
#endif // __CLC_MATH_CLC_RADIANS_H__

libclc/clc/lib/generic/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
common/clc_degrees.cl
2+
common/clc_radians.cl
13
common/clc_smoothstep.cl
24
geometric/clc_dot.cl
35
integer/clc_abs.cl
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
#include <clc/clcmacro.h>
24+
#include <clc/internal/clc.h>
25+
26+
#define DEGREES_SINGLE_DEF(TYPE, LITERAL) \
27+
_CLC_OVERLOAD _CLC_DEF TYPE __clc_degrees(TYPE radians) { \
28+
return (TYPE)LITERAL * radians; \
29+
}
30+
31+
#define DEGREES_DEF(TYPE, LITERAL) \
32+
DEGREES_SINGLE_DEF(TYPE, LITERAL) \
33+
DEGREES_SINGLE_DEF(TYPE##2, LITERAL) \
34+
DEGREES_SINGLE_DEF(TYPE##3, LITERAL) \
35+
DEGREES_SINGLE_DEF(TYPE##4, LITERAL) \
36+
DEGREES_SINGLE_DEF(TYPE##8, LITERAL) \
37+
DEGREES_SINGLE_DEF(TYPE##16, LITERAL)
38+
39+
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
40+
DEGREES_DEF(float, 0x1.ca5dc2p+5F)
41+
42+
#ifdef cl_khr_fp64
43+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
44+
45+
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
46+
DEGREES_DEF(double, 0x1.ca5dc1a63c1f8p+5)
47+
48+
#endif
49+
50+
#ifdef cl_khr_fp16
51+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
52+
53+
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
54+
DEGREES_DEF(half, (half)0x1.ca5dc1a63c1f8p+5)
55+
56+
#endif
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2014,2015 Advanced Micro Devices, Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
#include <clc/clcmacro.h>
24+
#include <clc/internal/clc.h>
25+
26+
#define RADIANS_SINGLE_DEF(TYPE, LITERAL) \
27+
_CLC_OVERLOAD _CLC_DEF TYPE __clc_radians(TYPE radians) { \
28+
return (TYPE)LITERAL * radians; \
29+
}
30+
31+
#define RADIANS_DEF(TYPE, LITERAL) \
32+
RADIANS_SINGLE_DEF(TYPE, LITERAL) \
33+
RADIANS_SINGLE_DEF(TYPE##2, LITERAL) \
34+
RADIANS_SINGLE_DEF(TYPE##3, LITERAL) \
35+
RADIANS_SINGLE_DEF(TYPE##4, LITERAL) \
36+
RADIANS_SINGLE_DEF(TYPE##8, LITERAL) \
37+
RADIANS_SINGLE_DEF(TYPE##16, LITERAL)
38+
39+
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
40+
RADIANS_DEF(float, 0x1.1df46ap-6F)
41+
42+
#ifdef cl_khr_fp64
43+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
44+
45+
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
46+
RADIANS_DEF(double, 0x1.1df46a2529d39p-6)
47+
48+
#endif
49+
50+
#ifdef cl_khr_fp16
51+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
52+
53+
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
54+
RADIANS_DEF(half, (half)0x1.1df46a2529d39p-6)
55+
56+
#endif

libclc/clc/lib/spirv/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
../generic/common/clc_degrees.cl
2+
../generic/common/clc_radians.cl
13
../generic/common/clc_smoothstep.cl
24
../generic/geometric/clc_dot.cl
35
../generic/math/clc_ceil.cl

libclc/clc/lib/spirv64/SOURCES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
../generic/common/clc_degrees.cl
2+
../generic/common/clc_radians.cl
13
../generic/common/clc_smoothstep.cl
24
../generic/geometric/clc_dot.cl
35
../generic/math/clc_ceil.cl

libclc/generic/lib/common/degrees.cl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,20 @@
2222

2323
#include <clc/clc.h>
2424
#include <clc/clcmacro.h>
25+
#include <clc/common/clc_degrees.h>
2526

26-
_CLC_OVERLOAD _CLC_DEF float degrees(float radians) {
27-
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
28-
return 0x1.ca5dc2p+5F * radians;
29-
}
30-
31-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, degrees, float);
32-
27+
_CLC_DEFINE_UNARY_BUILTIN(float, degrees, __clc_degrees, float)
3328

3429
#ifdef cl_khr_fp64
3530
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
3631

37-
_CLC_OVERLOAD _CLC_DEF double degrees(double radians) {
38-
// 180/pi = ~57.29577951308232087685 or 0x1.ca5dc1a63c1f8p+5 or 0x1.ca5dc2p+5F
39-
return 0x1.ca5dc1a63c1f8p+5 * radians;
40-
}
32+
_CLC_DEFINE_UNARY_BUILTIN(double, degrees, __clc_degrees, double)
33+
34+
#endif
35+
36+
#ifdef cl_khr_fp16
37+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
4138

42-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, degrees, double);
39+
_CLC_DEFINE_UNARY_BUILTIN(half, degrees, __clc_degrees, half)
4340

4441
#endif

libclc/generic/lib/common/radians.cl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,20 @@
2222

2323
#include <clc/clc.h>
2424
#include <clc/clcmacro.h>
25+
#include <clc/common/clc_radians.h>
2526

26-
_CLC_OVERLOAD _CLC_DEF float radians(float degrees) {
27-
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
28-
return 0x1.1df46ap-6F * degrees;
29-
}
30-
31-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, radians, float);
32-
27+
_CLC_DEFINE_UNARY_BUILTIN(float, radians, __clc_radians, float)
3328

3429
#ifdef cl_khr_fp64
3530
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
3631

37-
_CLC_OVERLOAD _CLC_DEF double radians(double degrees) {
38-
// pi/180 = ~0.01745329251994329577 or 0x1.1df46a2529d39p-6 or 0x1.1df46ap-6F
39-
return 0x1.1df46a2529d39p-6 * degrees;
40-
}
32+
_CLC_DEFINE_UNARY_BUILTIN(double, radians, __clc_radians, double)
33+
34+
#endif
35+
36+
#ifdef cl_khr_fp16
37+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
4138

42-
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, radians, double);
39+
_CLC_DEFINE_UNARY_BUILTIN(half, radians, __clc_radians, half)
4340

4441
#endif

0 commit comments

Comments
 (0)