Skip to content

Commit d32e71d

Browse files
authored
[libclc] Move fmod, remainder & remquo to the CLC library (#132054)
These functions were already nominally in the CLC namespace; this commit just formally moves them over. Note that 'half' versions of these CLC functions are now provided. Previously the corresponding OpenCL builtins would forward directly to the 'float' versions of the CLC builtins. Now the OpenCL builtins call the 'half' CLC builtins, which themselves call the 'float' CLC versions. This keeps the interface between the OpenCL and CLC libraries neater and keeps the CLC library self-contained. No changes to the generated code for non-SPIR-V targets is observed.
1 parent 7cc17fb commit d32e71d

File tree

18 files changed

+104
-38
lines changed

18 files changed

+104
-38
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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_FUNCTION(__CLC_GENTYPE x,
10+
__CLC_GENTYPE y) {
11+
return __CLC_CONVERT_GENTYPE(
12+
__CLC_FUNCTION(__CLC_CONVERT_FLOATN(x), __CLC_CONVERT_FLOATN(y)));
13+
}

libclc/generic/include/math/clc_fmod.h renamed to libclc/clc/include/clc/math/clc_fmod.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef __CLC_MATH_CLC_FMOD_H__
10+
#define __CLC_MATH_CLC_FMOD_H__
11+
912
#define __CLC_FUNCTION __clc_fmod
10-
#define __CLC_BODY <clc/math/binary_decl_tt.inc>
13+
#define __CLC_BODY <clc/shared/binary_decl.inc>
14+
1115
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
1218
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_FMOD_H__

libclc/generic/include/math/clc_remainder.h renamed to libclc/clc/include/clc/math/clc_remainder.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef __CLC_MATH_CLC_REMAINDER_H__
10+
#define __CLC_MATH_CLC_REMAINDER_H__
11+
912
#define __CLC_FUNCTION __clc_remainder
10-
#define __CLC_BODY <clc/math/binary_decl_tt.inc>
13+
#define __CLC_BODY <clc/shared/binary_decl.inc>
14+
1115
#include <clc/math/gentype.inc>
16+
17+
#undef __CLC_BODY
1218
#undef __CLC_FUNCTION
19+
20+
#endif // __CLC_MATH_CLC_REMAINDER_H__

libclc/generic/include/math/clc_remquo.h renamed to libclc/clc/include/clc/math/clc_remquo.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef __CLC_MATH_CLC_REMQUO_H__
10+
#define __CLC_MATH_CLC_REMQUO_H__
11+
912
#define __CLC_FUNCTION __clc_remquo
1013

11-
#define __CLC_BODY <clc/math/remquo.inc>
14+
#define __CLC_BODY <clc/math/remquo_decl.inc>
1215
#define __CLC_ADDRESS_SPACE private
1316
#include <clc/math/gentype.inc>
14-
#undef __CLC_ADDRESS_SPACE
1517

18+
#undef __CLC_ADDRESS_SPACE
19+
#undef __CLC_BODY
1620
#undef __CLC_FUNCTION
21+
22+
#endif // __CLC_MATH_CLC_REMQUO_H__

libclc/clc/include/clc/math/gentype.inc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#define __CLC_CONVERT_S_GENTYPE __CLC_XCONCAT(__clc_convert_, __CLC_S_GENTYPE)
6969
#define __CLC_CONVERT_U_GENTYPE __CLC_XCONCAT(__clc_convert_, __CLC_U_GENTYPE)
7070

71+
#if (!defined(__HALF_ONLY) && !defined(__DOUBLE_ONLY))
7172
#define __CLC_SCALAR_GENTYPE float
7273
#define __CLC_FPSIZE 32
7374
#define __CLC_FP_LIT(x) x##F
@@ -133,7 +134,9 @@
133134
#undef __CLC_FPSIZE
134135
#undef __CLC_SCALAR_GENTYPE
135136

136-
#ifndef __FLOAT_ONLY
137+
#endif
138+
139+
#if (!defined(__HALF_ONLY) && !defined(__FLOAT_ONLY))
137140
#ifdef cl_khr_fp64
138141
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
139142

@@ -204,7 +207,7 @@
204207
#endif
205208
#endif
206209

207-
#ifndef __FLOAT_ONLY
210+
#if (!defined(__FLOAT_ONLY) && !defined(__DOUBLE_ONLY))
208211
#ifdef cl_khr_fp16
209212
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
210213

libclc/generic/include/clc/math/remquo.inc renamed to libclc/clc/include/clc/math/remquo_decl.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q);
9+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(
10+
__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q);

libclc/clc/lib/generic/SOURCES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ math/clc_copysign.cl
3333
math/clc_ep_log.cl
3434
math/clc_fabs.cl
3535
math/clc_fma.cl
36+
math/clc_fmod.cl
3637
math/clc_floor.cl
3738
math/clc_frexp.cl
3839
math/clc_hypot.cl
@@ -45,6 +46,8 @@ math/clc_mad.cl
4546
math/clc_modf.cl
4647
math/clc_nan.cl
4748
math/clc_nextafter.cl
49+
math/clc_remainder.cl
50+
math/clc_remquo.cl
4851
math/clc_rint.cl
4952
math/clc_round.cl
5053
math/clc_rsqrt.cl

libclc/generic/lib/math/clc_fmod.cl renamed to libclc/clc/lib/generic/math/clc_fmod.cl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <clc/clc.h>
109
#include <clc/clc_convert.h>
1110
#include <clc/clcmacro.h>
1211
#include <clc/integer/clc_clz.h>
12+
#include <clc/internal/clc.h>
1313
#include <clc/math/clc_floor.h>
1414
#include <clc/math/clc_fma.h>
15-
#include <clc/math/clc_subnormal_config.h>
15+
#include <clc/math/clc_ldexp.h>
1616
#include <clc/math/clc_trunc.h>
1717
#include <clc/math/math.h>
1818
#include <clc/shared/clc_max.h>
19-
#include <math/clc_remainder.h>
2019

2120
_CLC_DEF _CLC_OVERLOAD float __clc_fmod(float x, float y) {
2221
int ux = __clc_as_int(x);
@@ -67,6 +66,9 @@ _CLC_DEF _CLC_OVERLOAD float __clc_fmod(float x, float y) {
6766
_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_fmod, float, float);
6867

6968
#ifdef cl_khr_fp64
69+
70+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
71+
7072
_CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) {
7173
ulong ux = __clc_as_ulong(x);
7274
ulong ax = ux & ~SIGNBIT_DP64;
@@ -92,7 +94,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) {
9294
// but it doesn't matter - it just means that we'll go round
9395
// the loop below one extra time.
9496
int ntimes = __clc_max(0, (xexp1 - yexp1) / 53);
95-
double w = ldexp(dy, ntimes * 53);
97+
double w = __clc_ldexp(dy, ntimes * 53);
9698
w = ntimes == 0 ? dy : w;
9799
double scale = ntimes == 0 ? 1.0 : 0x1.0p-53;
98100

@@ -171,3 +173,15 @@ _CLC_DEF _CLC_OVERLOAD double __clc_fmod(double x, double y) {
171173
_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_fmod, double,
172174
double);
173175
#endif
176+
177+
#ifdef cl_khr_fp16
178+
179+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
180+
181+
// Forward the half version of this builtin onto the float one
182+
#define __HALF_ONLY
183+
#define __CLC_FUNCTION __clc_fmod
184+
#define __CLC_BODY <clc/math/binary_def_via_fp32.inc>
185+
#include <clc/math/gentype.inc>
186+
187+
#endif

libclc/generic/lib/math/clc_remainder.cl renamed to libclc/clc/lib/generic/math/clc_remainder.cl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include <clc/clc.h>
109
#include <clc/clc_convert.h>
1110
#include <clc/clcmacro.h>
1211
#include <clc/integer/clc_clz.h>
12+
#include <clc/internal/clc.h>
1313
#include <clc/math/clc_floor.h>
1414
#include <clc/math/clc_fma.h>
15-
#include <clc/math/clc_subnormal_config.h>
15+
#include <clc/math/clc_ldexp.h>
16+
#include <clc/math/clc_remainder.h>
1617
#include <clc/math/clc_trunc.h>
1718
#include <clc/math/math.h>
1819
#include <clc/shared/clc_max.h>
19-
#include <math/clc_remainder.h>
2020

2121
_CLC_DEF _CLC_OVERLOAD float __clc_remainder(float x, float y) {
2222
int ux = __clc_as_int(x);
@@ -77,6 +77,9 @@ _CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, float, __clc_remainder, float,
7777
float);
7878

7979
#ifdef cl_khr_fp64
80+
81+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
82+
8083
_CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) {
8184
ulong ux = __clc_as_ulong(x);
8285
ulong ax = ux & ~SIGNBIT_DP64;
@@ -104,7 +107,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) {
104107
// but it doesn't matter - it just means that we'll go round
105108
// the loop below one extra time.
106109
int ntimes = __clc_max(0, (xexp1 - yexp1) / 53);
107-
double w = ldexp(dy, ntimes * 53);
110+
double w = __clc_ldexp(dy, ntimes * 53);
108111
w = ntimes == 0 ? dy : w;
109112
double scale = ntimes == 0 ? 1.0 : 0x1.0p-53;
110113

@@ -207,3 +210,15 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remainder(double x, double y) {
207210
_CLC_BINARY_VECTORIZE(_CLC_DEF _CLC_OVERLOAD, double, __clc_remainder, double,
208211
double);
209212
#endif
213+
214+
#ifdef cl_khr_fp16
215+
216+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
217+
218+
// Forward the half version of this builtin onto the float one
219+
#define __HALF_ONLY
220+
#define __CLC_FUNCTION __clc_remainder
221+
#define __CLC_BODY <clc/math/binary_def_via_fp32.inc>
222+
#include <clc/math/gentype.inc>
223+
224+
#endif

libclc/generic/lib/math/clc_remquo.cl renamed to libclc/clc/lib/generic/math/clc_remquo.cl

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

9-
#include <clc/clc.h>
109
#include <clc/clc_convert.h>
1110
#include <clc/clcmacro.h>
1211
#include <clc/integer/clc_clz.h>
12+
#include <clc/internal/clc.h>
1313
#include <clc/math/clc_floor.h>
1414
#include <clc/math/clc_fma.h>
15+
#include <clc/math/clc_ldexp.h>
1516
#include <clc/math/clc_subnormal_config.h>
1617
#include <clc/math/clc_trunc.h>
1718
#include <clc/math/math.h>
1819
#include <clc/shared/clc_max.h>
19-
#include <math/clc_remainder.h>
2020

2121
_CLC_DEF _CLC_OVERLOAD float __clc_remquo(float x, float y,
2222
__private int *quo) {
@@ -116,6 +116,9 @@ __VEC_REMQUO(float, 8, 4)
116116
__VEC_REMQUO(float, 16, 8)
117117

118118
#ifdef cl_khr_fp64
119+
120+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
121+
119122
_CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y,
120123
__private int *pquo) {
121124
ulong ux = __clc_as_ulong(x);
@@ -144,7 +147,7 @@ _CLC_DEF _CLC_OVERLOAD double __clc_remquo(double x, double y,
144147
// but it doesn't matter - it just means that we'll go round
145148
// the loop below one extra time.
146149
int ntimes = __clc_max(0, (xexp1 - yexp1) / 53);
147-
double w = ldexp(dy, ntimes * 53);
150+
double w = __clc_ldexp(dy, ntimes * 53);
148151
w = ntimes == 0 ? dy : w;
149152
double scale = ntimes == 0 ? 1.0 : 0x1.0p-53;
150153

libclc/clspv/lib/SOURCES

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ subnormal_config.cl
1717
../../generic/lib/math/atanpi.cl
1818
../../generic/lib/math/cbrt.cl
1919
../../generic/lib/math/clc_exp10.cl
20-
../../generic/lib/math/clc_fmod.cl
2120
../../generic/lib/math/clc_pow.cl
2221
../../generic/lib/math/clc_pown.cl
2322
../../generic/lib/math/clc_powr.cl
24-
../../generic/lib/math/clc_remainder.cl
25-
../../generic/lib/math/clc_remquo.cl
2623
../../generic/lib/math/clc_rootn.cl
2724
../../generic/lib/math/clc_tan.cl
2825
../../generic/lib/math/clc_tanpi.cl

libclc/generic/include/clc/math/remquo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
#define __CLC_FUNCTION remquo
1010

11-
#define __CLC_BODY <clc/math/remquo.inc>
11+
#define __CLC_BODY <clc/math/remquo_decl.inc>
1212
#define __CLC_ADDRESS_SPACE global
1313
#include <clc/math/gentype.inc>
1414
#undef __CLC_ADDRESS_SPACE
1515

16-
#define __CLC_BODY <clc/math/remquo.inc>
16+
#define __CLC_BODY <clc/math/remquo_decl.inc>
1717
#define __CLC_ADDRESS_SPACE local
1818
#include <clc/math/gentype.inc>
1919
#undef __CLC_ADDRESS_SPACE
2020

21-
#define __CLC_BODY <clc/math/remquo.inc>
21+
#define __CLC_BODY <clc/math/remquo_decl.inc>
2222
#define __CLC_ADDRESS_SPACE private
2323
#include <clc/math/gentype.inc>
2424
#undef __CLC_ADDRESS_SPACE

libclc/generic/lib/SOURCES

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ math/floor.cl
108108
math/fma.cl
109109
math/fmax.cl
110110
math/fmin.cl
111-
math/clc_fmod.cl
112111
math/fmod.cl
113112
math/fract.cl
114113
math/frexp.cl
@@ -163,9 +162,7 @@ math/clc_pown.cl
163162
math/pown.cl
164163
math/clc_powr.cl
165164
math/powr.cl
166-
math/clc_remainder.cl
167165
math/remainder.cl
168-
math/clc_remquo.cl
169166
math/remquo.cl
170167
math/rint.cl
171168
math/clc_rootn.cl

libclc/generic/lib/math/fmod.cl

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

99
#include <clc/clc.h>
10-
#include <math/clc_fmod.h>
10+
#include <clc/math/clc_fmod.h>
1111

12-
#define __CLC_FUNC fmod
13-
#define __CLC_BODY <clc_sw_binary.inc>
12+
#define FUNCTION fmod
13+
#define __CLC_BODY <clc/shared/binary_def.inc>
1414
#include <clc/math/gentype.inc>

libclc/generic/lib/math/remainder.cl

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

99
#include <clc/clc.h>
10-
#include <math/clc_remainder.h>
10+
#include <clc/math/clc_remainder.h>
1111

12-
#define __CLC_FUNC remainder
13-
#define __CLC_BODY <clc_sw_binary.inc>
12+
#define FUNCTION remainder
13+
#define __CLC_BODY <clc/shared/binary_def.inc>
1414
#include <clc/math/gentype.inc>

libclc/generic/lib/math/remquo.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include <clc/clc.h>
10-
#include <math/clc_remquo.h>
10+
#include <clc/math/clc_remquo.h>
1111

1212
#define __CLC_BODY <remquo.inc>
1313
#define __CLC_ADDRESS_SPACE global

libclc/generic/lib/math/remquo.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE remquo(__CLC_GENTYPE x, __CLC_GENTYPE y, __CLC_ADDRESS_SPACE __CLC_INTN *q) {
9+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE remquo(__CLC_GENTYPE x, __CLC_GENTYPE y,
10+
__CLC_ADDRESS_SPACE __CLC_INTN *q) {
1011
__CLC_INTN local_q;
1112
__CLC_GENTYPE ret = __clc_remquo(x, y, &local_q);
1213
*q = local_q;

libclc/spirv/lib/SOURCES

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ subnormal_config.cl
4141
../../generic/lib/math/clc_exp10.cl
4242
../../generic/lib/math/exp10.cl
4343
math/fma.cl
44-
../../generic/lib/math/clc_fmod.cl
4544
../../generic/lib/math/fmod.cl
4645
../../generic/lib/math/fract.cl
4746
../../generic/lib/math/frexp.cl
@@ -65,9 +64,7 @@ math/fma.cl
6564
../../generic/lib/math/pown.cl
6665
../../generic/lib/math/clc_powr.cl
6766
../../generic/lib/math/powr.cl
68-
../../generic/lib/math/clc_remainder.cl
6967
../../generic/lib/math/remainder.cl
70-
../../generic/lib/math/clc_remquo.cl
7168
../../generic/lib/math/remquo.cl
7269
../../generic/lib/math/clc_rootn.cl
7370
../../generic/lib/math/rootn.cl

0 commit comments

Comments
 (0)