Skip to content

Commit 293c78b

Browse files
authored
[libclc] Move ceil/fabs/floor/rint/trunc to CLC library (#114774)
These functions are all mapped to LLVM intrinsics. The clspv and spirv targets don't declare or define any of these CLC functions, and instead map these to their corresponding OpenCL symbols.
1 parent b5dc7b8 commit 293c78b

25 files changed

+145
-45
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __CLC_MATH_CLC_CEIL_H__
2+
#define __CLC_MATH_CLC_CEIL_H__
3+
4+
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
5+
// clspv and spir-v targets provide their own OpenCL-compatible ceil
6+
#define __clc_ceil ceil
7+
#else
8+
9+
// Map the function to an LLVM intrinsic
10+
#define __CLC_FUNCTION __clc_ceil
11+
#define __CLC_INTRINSIC "llvm.ceil"
12+
#include <clc/math/unary_intrin.inc>
13+
14+
#undef __CLC_INTRINSIC
15+
#undef __CLC_FUNCTION
16+
17+
#endif
18+
19+
#endif // __CLC_MATH_CLC_CEIL_H__
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __CLC_MATH_CLC_FABS_H__
2+
#define __CLC_MATH_CLC_FABS_H__
3+
4+
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
5+
// clspv and spir-v targets provide their own OpenCL-compatible fabs
6+
#define __clc_fabs fabs
7+
#else
8+
9+
// Map the function to an LLVM intrinsic
10+
#define __CLC_FUNCTION __clc_fabs
11+
#define __CLC_INTRINSIC "llvm.fabs"
12+
#include <clc/math/unary_intrin.inc>
13+
14+
#undef __CLC_INTRINSIC
15+
#undef __CLC_FUNCTION
16+
17+
#endif
18+
19+
#endif // __CLC_MATH_CLC_FABS_H__
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __CLC_MATH_CLC_FLOOR_H__
2+
#define __CLC_MATH_CLC_FLOOR_H__
3+
4+
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
5+
// clspv and spir-v targets provide their own OpenCL-compatible floor
6+
#define __clc_floor floor
7+
#else
8+
9+
// Map the function to an LLVM intrinsic
10+
#define __CLC_FUNCTION __clc_floor
11+
#define __CLC_INTRINSIC "llvm.floor"
12+
#include <clc/math/unary_intrin.inc>
13+
14+
#undef __CLC_INTRINSIC
15+
#undef __CLC_FUNCTION
16+
17+
#endif
18+
19+
#endif // __CLC_MATH_CLC_FLOOR_H__
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __CLC_MATH_CLC_RINT_H__
2+
#define __CLC_MATH_CLC_RINT_H__
3+
4+
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
5+
// clspv and spir-v targets provide their own OpenCL-compatible rint
6+
#define __clc_rint rint
7+
#else
8+
9+
// Map the function to an LLVM intrinsic
10+
#define __CLC_FUNCTION __clc_rint
11+
#define __CLC_INTRINSIC "llvm.rint"
12+
#include <clc/math/unary_intrin.inc>
13+
14+
#undef __CLC_INTRINSIC
15+
#undef __CLC_FUNCTION
16+
17+
#endif
18+
19+
#endif // __CLC_MATH_CLC_RINT_H__
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef __CLC_MATH_CLC_TRUNC_H__
2+
#define __CLC_MATH_CLC_TRUNC_H__
3+
4+
#if defined(CLC_CLSPV) || defined(CLC_SPIRV)
5+
// clspv and spir-v targets provide their own OpenCL-compatible trunc
6+
#define __clc_trunc trunc
7+
#else
8+
9+
// Map the function to an LLVM intrinsic
10+
#define __CLC_FUNCTION __clc_trunc
11+
#define __CLC_INTRINSIC "llvm.trunc"
12+
#include <clc/math/unary_intrin.inc>
13+
14+
#undef __CLC_INTRINSIC
15+
#undef __CLC_FUNCTION
16+
17+
#endif
18+
19+
#endif // __CLC_MATH_CLC_TRUNC_H__

libclc/generic/include/math/unary_intrin.inc renamed to libclc/clc/include/clc/math/unary_intrin.inc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ _CLC_OVERLOAD float2 __CLC_FUNCTION(float2 f) __asm(__CLC_INTRINSIC ".v2f32");
33
_CLC_OVERLOAD float3 __CLC_FUNCTION(float3 f) __asm(__CLC_INTRINSIC ".v3f32");
44
_CLC_OVERLOAD float4 __CLC_FUNCTION(float4 f) __asm(__CLC_INTRINSIC ".v4f32");
55
_CLC_OVERLOAD float8 __CLC_FUNCTION(float8 f) __asm(__CLC_INTRINSIC ".v8f32");
6-
_CLC_OVERLOAD float16 __CLC_FUNCTION(float16 f) __asm(__CLC_INTRINSIC ".v16f32");
6+
_CLC_OVERLOAD float16 __CLC_FUNCTION(float16 f) __asm(__CLC_INTRINSIC
7+
".v16f32");
78

89
#ifdef cl_khr_fp64
910
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
@@ -12,11 +13,12 @@ _CLC_OVERLOAD double2 __CLC_FUNCTION(double2 d) __asm(__CLC_INTRINSIC ".v2f64");
1213
_CLC_OVERLOAD double3 __CLC_FUNCTION(double3 d) __asm(__CLC_INTRINSIC ".v3f64");
1314
_CLC_OVERLOAD double4 __CLC_FUNCTION(double4 d) __asm(__CLC_INTRINSIC ".v4f64");
1415
_CLC_OVERLOAD double8 __CLC_FUNCTION(double8 d) __asm(__CLC_INTRINSIC ".v8f64");
15-
_CLC_OVERLOAD double16 __CLC_FUNCTION(double16 d) __asm(__CLC_INTRINSIC ".v16f64");
16+
_CLC_OVERLOAD double16 __CLC_FUNCTION(double16 d) __asm(__CLC_INTRINSIC
17+
".v16f64");
1618
#endif
1719

1820
#ifdef cl_khr_fp16
19-
#pragma OPENCL EXTENSION cl_khr_fp16: enable
21+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
2022
_CLC_OVERLOAD half __CLC_FUNCTION(half d) __asm(__CLC_INTRINSIC ".f16");
2123
_CLC_OVERLOAD half2 __CLC_FUNCTION(half2 d) __asm(__CLC_INTRINSIC ".v2f16");
2224
_CLC_OVERLOAD half3 __CLC_FUNCTION(half3 d) __asm(__CLC_INTRINSIC ".v3f16");

libclc/clc/include/clc/utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef __CLC_UTILS_H__
2+
#define __CLC_UTILS_H__
3+
4+
#define __CLC_CONCAT(x, y) x##y
5+
#define __CLC_XCONCAT(x, y) __CLC_CONCAT(x, y)
6+
7+
#define __CLC_STR(x) #x
8+
#define __CLC_XSTR(x) __CLC_STR(x)
9+
10+
#endif // __CLC_UTILS_H__

libclc/generic/lib/clcmacro.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <clc/clc.h>
12
#include <utils.h>
23

34
#define _CLC_UNARY_VECTORIZE(DECLSPEC, RET_TYPE, FUNCTION, ARG1_TYPE) \

libclc/generic/lib/math/ceil.cl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
#include <clc/clc.h>
21
#include "../clcmacro.h"
3-
4-
// Map the llvm intrinsic to an OpenCL function.
5-
#define __CLC_FUNCTION __clc_ceil
6-
#define __CLC_INTRINSIC "llvm.ceil"
7-
#include "math/unary_intrin.inc"
2+
#include <clc/clc.h>
3+
#include <clc/math/clc_ceil.h>
84

95
#undef __CLC_FUNCTION
106
#define __CLC_FUNCTION ceil

libclc/generic/lib/math/clc_fmod.cl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222

2323
#include <clc/clc.h>
24+
#include <clc/math/clc_floor.h>
25+
#include <clc/math/clc_trunc.h>
2426

2527
#include <math/clc_remainder.h>
2628
#include "../clcmacro.h"
@@ -119,7 +121,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y)
119121

120122
for (i = 0; i < ntimes; i++) {
121123
// Compute integral multiplier
122-
t = trunc(dx / w);
124+
t = __clc_trunc(dx / w);
123125

124126
// Compute w * t in quad precision
125127
p = w * t;
@@ -138,7 +140,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y)
138140

139141
// One more time
140142
// Variable todd says whether the integer t is odd or not
141-
t = floor(dx / w);
143+
t = __clc_floor(dx / w);
142144
long lt = (long)t;
143145
int todd = lt & 1;
144146

libclc/generic/lib/math/clc_pow.cl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
#include <clc/clc.h>
24+
#include <clc/math/clc_fabs.h>
2425

2526
#include "config.h"
2627
#include "math.h"
@@ -80,7 +81,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_pow(float x, float y)
8081
* First handle case that x is close to 1
8182
*/
8283
float r = 1.0f - as_float(ax);
83-
int near1 = fabs(r) < 0x1.0p-4f;
84+
int near1 = __clc_fabs(r) < 0x1.0p-4f;
8485
float r2 = r*r;
8586

8687
/* Coefficients are just 1/3, 1/4, 1/5 and 1/6 */

libclc/generic/lib/math/clc_pown.cl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
#include <clc/clc.h>
24+
#include <clc/math/clc_fabs.h>
2425

2526
#include "config.h"
2627
#include "math.h"
@@ -78,7 +79,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_pown(float x, int ny)
7879
// Extra precise log calculation
7980
// First handle case that x is close to 1
8081
float r = 1.0f - as_float(ax);
81-
int near1 = fabs(r) < 0x1.0p-4f;
82+
int near1 = __clc_fabs(r) < 0x1.0p-4f;
8283
float r2 = r*r;
8384

8485
// Coefficients are just 1/3, 1/4, 1/5 and 1/6

libclc/generic/lib/math/clc_powr.cl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
#include <clc/clc.h>
24+
#include <clc/math/clc_fabs.h>
2425

2526
#include "config.h"
2627
#include "math.h"
@@ -76,7 +77,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_powr(float x, float y)
7677
// Extra precise log calculation
7778
// First handle case that x is close to 1
7879
float r = 1.0f - as_float(ax);
79-
int near1 = fabs(r) < 0x1.0p-4f;
80+
int near1 = __clc_fabs(r) < 0x1.0p-4f;
8081
float r2 = r*r;
8182

8283
// Coefficients are just 1/3, 1/4, 1/5 and 1/6

libclc/generic/lib/math/clc_remainder.cl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222

2323
#include <clc/clc.h>
24+
#include <clc/math/clc_floor.h>
25+
#include <clc/math/clc_trunc.h>
2426

2527
#include <math/clc_remainder.h>
2628
#include "../clcmacro.h"
@@ -129,7 +131,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y)
129131

130132
for (i = 0; i < ntimes; i++) {
131133
// Compute integral multiplier
132-
t = trunc(dx / w);
134+
t = __clc_trunc(dx / w);
133135

134136
// Compute w * t in quad precision
135137
p = w * t;
@@ -148,7 +150,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y)
148150

149151
// One more time
150152
// Variable todd says whether the integer t is odd or not
151-
t = floor(dx / w);
153+
t = __clc_floor(dx / w);
152154
long lt = (long)t;
153155
int todd = lt & 1;
154156

libclc/generic/lib/math/clc_remquo.cl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222

2323
#include <clc/clc.h>
24+
#include <clc/math/clc_floor.h>
25+
#include <clc/math/clc_trunc.h>
2426

2527
#include <math/clc_remainder.h>
2628
#include "../clcmacro.h"
@@ -154,7 +156,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y, __private int *pq
154156

155157
for (i = 0; i < ntimes; i++) {
156158
// Compute integral multiplier
157-
t = trunc(dx / w);
159+
t = __clc_trunc(dx / w);
158160

159161
// Compute w * t in quad precision
160162
p = w * t;
@@ -173,7 +175,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y, __private int *pq
173175

174176
// One more time
175177
// Variable todd says whether the integer t is odd or not
176-
t = floor(dx / w);
178+
t = __clc_floor(dx / w);
177179
long lt = (long)t;
178180
int todd = lt & 1;
179181

libclc/generic/lib/math/clc_rootn.cl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
#include <clc/clc.h>
24+
#include <clc/math/clc_fabs.h>
2425

2526
#include "config.h"
2627
#include "math.h"
@@ -78,7 +79,7 @@ _CLC_DEF _CLC_OVERLOAD float __clc_rootn(float x, int ny)
7879
// Extra precise log calculation
7980
// First handle case that x is close to 1
8081
float r = 1.0f - as_float(ax);
81-
int near1 = fabs(r) < 0x1.0p-4f;
82+
int near1 = __clc_fabs(r) < 0x1.0p-4f;
8283
float r2 = r*r;
8384

8485
// Coefficients are just 1/3, 1/4, 1/5 and 1/6

libclc/generic/lib/math/clc_sqrt.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// Map the llvm sqrt intrinsic to an OpenCL function.
2626
#define __CLC_FUNCTION __clc_llvm_intr_sqrt
2727
#define __CLC_INTRINSIC "llvm.sqrt"
28-
#include <math/unary_intrin.inc>
28+
#include <clc/math/unary_intrin.inc>
2929
#undef __CLC_FUNCTION
3030
#undef __CLC_INTRINSIC
3131

libclc/generic/lib/math/clc_tan.cl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* THE SOFTWARE.
2121
*/
2222
#include <clc/clc.h>
23+
#include <clc/math/clc_fabs.h>
2324

2425
#include "math.h"
2526
#include "sincos_helpers.h"
@@ -48,7 +49,7 @@ _CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_tan, float);
4849
#include "sincosD_piby4.h"
4950

5051
_CLC_DEF _CLC_OVERLOAD double __clc_tan(double x) {
51-
double y = fabs(x);
52+
double y = __clc_fabs(x);
5253

5354
double r, rr;
5455
int regn;
@@ -66,4 +67,5 @@ _CLC_DEF _CLC_OVERLOAD double __clc_tan(double x) {
6667
return isnan(x) || isinf(x) ? as_double(QNANBITPATT_DP64) : as_double(t);
6768
}
6869
_CLC_UNARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_tan, double);
70+
6971
#endif

libclc/generic/lib/math/fabs.cl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
#include <clc/clc.h>
21
#include "../clcmacro.h"
3-
4-
// Map the llvm intrinsic to an OpenCL function.
5-
#define __CLC_FUNCTION __clc_fabs
6-
#define __CLC_INTRINSIC "llvm.fabs"
7-
#include "math/unary_intrin.inc"
2+
#include <clc/clc.h>
3+
#include <clc/math/clc_fabs.h>
84

95
#undef __CLC_FUNCTION
106
#define __CLC_FUNCTION fabs

libclc/generic/lib/math/floor.cl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
#include <clc/clc.h>
21
#include "../clcmacro.h"
3-
4-
// Map the llvm intrinsic to an OpenCL function.
5-
#define __CLC_FUNCTION __clc_floor
6-
#define __CLC_INTRINSIC "llvm.floor"
7-
#include "math/unary_intrin.inc"
2+
#include <clc/clc.h>
3+
#include <clc/math/clc_floor.h>
84

95
#undef __CLC_FUNCTION
106
#define __CLC_FUNCTION floor

libclc/generic/lib/math/native_unary_intrinsic.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#define __CLC_INTRINSIC "llvm." __CLC_XSTR(__CLC_NATIVE_INTRINSIC)
2828

2929
#undef cl_khr_fp64
30-
#include <math/unary_intrin.inc>
30+
#include <clc/math/unary_intrin.inc>
3131

3232
#endif
3333

libclc/generic/lib/math/rint.cl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include <clc/clc.h>
2-
3-
// Map the llvm intrinsic to an OpenCL function.
4-
#define __CLC_FUNCTION __clc_rint
5-
#define __CLC_INTRINSIC "llvm.rint"
6-
#include "math/unary_intrin.inc"
2+
#include <clc/math/clc_rint.h>
73

84
#undef __CLC_FUNCTION
95
#define __CLC_FUNCTION rint

libclc/generic/lib/math/round.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Map the llvm intrinsic to an OpenCL function.
44
#define __CLC_FUNCTION __clc_round
55
#define __CLC_INTRINSIC "llvm.round"
6-
#include "math/unary_intrin.inc"
6+
#include <clc/math/unary_intrin.inc>
77

88
#undef __CLC_FUNCTION
99
#define __CLC_FUNCTION round

0 commit comments

Comments
 (0)