Skip to content

Commit f8948d3

Browse files
authored
[libclc] Move log/log2/log10 to CLC library (#128540)
This commit also enables fp16 log, which was previously missing. Other than that, no changes to codegen for AMDGPU/Nvidia targets. Note that for simplicity this commit doesn't try to refactor or optimize the implementations. Notably, each log is only implementated for scalar types; vector types are scalarized. It doesn't look too difficult to make the implementations suitable for vector codegen, so I'll try that in a future commit. There's also an unused implementation of log in clc_log_base.h, whereas the implementation currently used by libclc targets re-uses log2 with an additional multiplication. That should also be cleaned up as on first inspection it looks a more optimal implementation, though it would have to be checked against the OpenCL CTS for good measure.
1 parent 6aeec5e commit f8948d3

File tree

14 files changed

+523
-429
lines changed

14 files changed

+523
-429
lines changed

libclc/clc/include/clc/float/definitions.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@
6969

7070
#ifdef cl_khr_fp16
7171

72-
#if __OPENCL_VERSION__ >= 120
73-
7472
#define HALF_DIG 3
7573
#define HALF_MANT_DIG 11
7674
#define HALF_MAX_10_EXP +4
@@ -83,6 +81,6 @@
8381
#define HALF_MIN 0x1.0p-14h
8482
#define HALF_EPSILON 0x1.0p-10h
8583

86-
#endif
84+
#define M_LOG2E_H 0x1.714p+0h
8785

8886
#endif

libclc/clc/include/clc/math/clc_log.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __CLC_MATH_CLC_LOG_H__
2+
#define __CLC_MATH_CLC_LOG_H__
3+
4+
#define __CLC_FUNCTION __clc_log
5+
#define __CLC_BODY <clc/shared/unary_decl.inc>
6+
7+
#include <clc/math/gentype.inc>
8+
9+
#undef __CLC_BODY
10+
#undef __CLC_FUNCTION
11+
12+
#endif // __CLC_MATH_CLC_LOG_H__
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __CLC_MATH_CLC_LOG10_H__
2+
#define __CLC_MATH_CLC_LOG10_H__
3+
4+
#define __CLC_FUNCTION __clc_log10
5+
#define __CLC_BODY <clc/shared/unary_decl.inc>
6+
7+
#include <clc/math/gentype.inc>
8+
9+
#undef __CLC_BODY
10+
#undef __CLC_FUNCTION
11+
12+
#endif // __CLC_MATH_CLC_LOG10_H__
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __CLC_MATH_CLC_LOG2_H__
2+
#define __CLC_MATH_CLC_LOG2_H__
3+
4+
#define __CLC_FUNCTION __clc_log2
5+
#define __CLC_BODY <clc/shared/unary_decl.inc>
6+
7+
#include <clc/math/gentype.inc>
8+
9+
#undef __CLC_BODY
10+
#undef __CLC_FUNCTION
11+
12+
#endif // __CLC_MATH_CLC_LOG2_H__

libclc/clc/lib/generic/SOURCES

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ math/clc_fabs.cl
2323
math/clc_fma.cl
2424
math/clc_floor.cl
2525
math/clc_frexp.cl
26+
math/clc_log.cl
27+
math/clc_log10.cl
28+
math/clc_log2.cl
2629
math/clc_mad.cl
2730
math/clc_modf.cl
2831
math/clc_nan.cl
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <clc/clcmacro.h>
2+
#include <clc/float/definitions.h>
3+
#include <clc/internal/clc.h>
4+
#include <clc/math/clc_log2.h>
5+
6+
/*
7+
*log(x) = log2(x) * (1/log2(e))
8+
*/
9+
10+
_CLC_OVERLOAD _CLC_DEF float __clc_log(float x) {
11+
return __clc_log2(x) * (1.0f / M_LOG2E_F);
12+
}
13+
14+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_log, float);
15+
16+
#ifdef cl_khr_fp64
17+
18+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
19+
20+
_CLC_OVERLOAD _CLC_DEF double __clc_log(double x) {
21+
return __clc_log2(x) * (1.0 / M_LOG2E);
22+
}
23+
24+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_log, double);
25+
26+
#endif // cl_khr_fp64
27+
28+
#ifdef cl_khr_fp16
29+
30+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
31+
32+
_CLC_OVERLOAD _CLC_DEF half __clc_log(half x) {
33+
return (half)__clc_log2((float)x) * (1.0h / M_LOG2E_H);
34+
}
35+
36+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_log, half);
37+
38+
#endif // cl_khr_fp16
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 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+
#include <clc/math/tables.h>
26+
27+
#ifdef cl_khr_fp64
28+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
29+
#endif // cl_khr_fp64
30+
31+
#ifdef cl_khr_fp16
32+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
33+
#endif // cl_khr_fp16
34+
35+
#define COMPILING_LOG10
36+
#include "clc_log_base.h"
37+
#undef COMPILING_LOG10
38+
39+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_log10, float);
40+
41+
#ifdef cl_khr_fp64
42+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_log10, double);
43+
#endif // cl_khr_fp64
44+
45+
#ifdef cl_khr_fp16
46+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_log10, half);
47+
#endif // cl_khr_fp16
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 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+
#include <clc/math/tables.h>
26+
27+
#ifdef cl_khr_fp64
28+
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
29+
#endif // cl_khr_fp64
30+
31+
#ifdef cl_khr_fp16
32+
#pragma OPENCL EXTENSION cl_khr_fp16 : enable
33+
#endif // cl_khr_fp16
34+
35+
#define COMPILING_LOG2
36+
#include "clc_log_base.h"
37+
#undef COMPILING_LOG2
38+
39+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, float, __clc_log2, float);
40+
41+
#ifdef cl_khr_fp64
42+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, double, __clc_log2, double);
43+
#endif // cl_khr_fp64
44+
45+
#ifdef cl_khr_fp16
46+
_CLC_UNARY_VECTORIZE(_CLC_OVERLOAD _CLC_DEF, half, __clc_log2, half);
47+
#endif // cl_khr_fp16

0 commit comments

Comments
 (0)