Skip to content

Commit 684ad25

Browse files
authored
[libclc] Move frexp to CLC library; optimize half vecs (#127836)
This commit moves the frexp builtin to the CLC library. It simultaneously optimizes the code generated for half vectors, which was previously scalarizing and casting up to float. With this commit it still casts up to float, but keeps it in the vector form.
1 parent 079115e commit 684ad25

File tree

9 files changed

+183
-106
lines changed

9 files changed

+183
-106
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __CLC_MATH_CLC_FREXP_H__
2+
#define __CLC_MATH_CLC_FREXP_H__
3+
4+
#define __CLC_FUNCTION __clc_frexp
5+
#define __CLC_BODY <clc/math/unary_decl_with_int_ptr.inc>
6+
#include <clc/math/gentype.inc>
7+
8+
#undef __CLC_BODY
9+
#undef __CLC_FUNCTION
10+
11+
#endif // __CLC_MATH_CLC_FREXP_H__
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
2+
global __CLC_INTN *iptr);
3+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
4+
local __CLC_INTN *iptr);
5+
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
6+
private __CLC_INTN *iptr);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <clc/utils.h>
2+
3+
#ifndef __CLC_FUNCTION
4+
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
5+
#endif
6+
7+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
8+
private __CLC_INTN *iptr) {
9+
return __CLC_FUNCTION(FUNCTION)(x, iptr);
10+
}
11+
12+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
13+
global __CLC_INTN *iptr) {
14+
return __CLC_FUNCTION(FUNCTION)(x, iptr);
15+
}
16+
17+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
18+
local __CLC_INTN *iptr) {
19+
return __CLC_FUNCTION(FUNCTION)(x, iptr);
20+
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#ifndef __CLC_RELATIONAL_CLC_SELECT_H__
22
#define __CLC_RELATIONAL_CLC_SELECT_H__
33

4-
/* Duplciate these so we don't have to distribute utils.h */
5-
#define __CLC_CONCAT(x, y) x##y
6-
#define __CLC_XCONCAT(x, y) __CLC_CONCAT(x, y)
4+
#include <clc/utils.h>
75

86
#define __CLC_SELECT_FN __clc_select
97

@@ -13,7 +11,5 @@
1311
#include <clc/integer/gentype.inc>
1412

1513
#undef __CLC_SELECT_FN
16-
#undef __CLC_CONCAT
17-
#undef __CLC_XCONCAT
1814

1915
#endif // __CLC_RELATIONAL_CLC_SELECT_H__

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ math/clc_ceil.cl
2121
math/clc_copysign.cl
2222
math/clc_fabs.cl
2323
math/clc_floor.cl
24+
math/clc_frexp.cl
2425
math/clc_mad.cl
2526
math/clc_modf.cl
2627
math/clc_nextafter.cl
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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/clc_convert.h>
24+
#include <clc/internal/clc.h>
25+
#include <clc/math/math.h>
26+
#include <clc/relational/clc_select.h>
27+
#include <clc/utils.h>
28+
29+
#define __CLC_BODY <clc_frexp.inc>
30+
#define __CLC_ADDRESS_SPACE private
31+
#include <clc/math/gentype.inc>
32+
#undef __CLC_ADDRESS_SPACE
33+
34+
#define __CLC_BODY <clc_frexp.inc>
35+
#define __CLC_ADDRESS_SPACE global
36+
#include <clc/math/gentype.inc>
37+
#undef __CLC_ADDRESS_SPACE
38+
39+
#define __CLC_BODY <clc_frexp.inc>
40+
#define __CLC_ADDRESS_SPACE local
41+
#include <clc/math/gentype.inc>
42+
#undef __CLC_ADDRESS_SPACE
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2014 Advanced Micro Devices, Inc.
3+
* Copyright (c) 2016 Aaron Watry
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
#include <clc/clcmacro.h>
25+
#include <clc/utils.h>
26+
27+
#define __CLC_AS_GENTYPE __CLC_XCONCAT(__clc_as_, __CLC_GENTYPE)
28+
#define __CLC_AS_INTN __CLC_XCONCAT(__clc_as_, __CLC_INTN)
29+
30+
#if __CLC_FPSIZE == 32
31+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
32+
__clc_frexp(__CLC_GENTYPE x, __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
33+
__CLC_INTN i = __CLC_AS_INTN(x);
34+
__CLC_INTN ai = i & 0x7fffffff;
35+
__CLC_INTN d = ai > 0 & ai < 0x00800000;
36+
/* scale subnormal by 2^26 without multiplying */
37+
__CLC_GENTYPE s = __CLC_AS_GENTYPE(ai | 0x0d800000) - 0x1.0p-100f;
38+
ai = __clc_select(ai, __CLC_AS_INTN(s), d);
39+
__CLC_INTN e =
40+
(ai >> 23) - 126 - __clc_select((__CLC_INTN)0, (__CLC_INTN)26, d);
41+
__CLC_INTN t = ai == (__CLC_INTN)0 | e == (__CLC_INTN)129;
42+
i = (i & (__CLC_INTN)0x80000000) | (__CLC_INTN)0x3f000000 | (ai & 0x007fffff);
43+
*ep = __clc_select(e, (__CLC_INTN)0, t);
44+
return __clc_select(__CLC_AS_GENTYPE(i), x, t);
45+
}
46+
#endif
47+
48+
#if __CLC_FPSIZE == 16
49+
#ifdef __CLC_SCALAR
50+
#define __CLC_CONVERT_HALFN __clc_convert_half
51+
#define __CLC_CONVERT_FLOATN __clc_convert_float
52+
#else
53+
#define __CLC_CONVERT_HALFN __CLC_XCONCAT(__clc_convert_half, __CLC_VECSIZE)
54+
#define __CLC_CONVERT_FLOATN __CLC_XCONCAT(__clc_convert_float, __CLC_VECSIZE)
55+
#endif
56+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
57+
__clc_frexp(__CLC_GENTYPE x, __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
58+
return __CLC_CONVERT_HALFN(__clc_frexp(__CLC_CONVERT_FLOATN(x), ep));
59+
}
60+
#undef __CLC_CONVERT_FLOATN
61+
#undef __CLC_CONVERT_HALFN
62+
#endif
63+
64+
#if __CLC_FPSIZE == 64
65+
#ifdef __CLC_SCALAR
66+
#define __CLC_AS_LONGN __clc_as_long
67+
#define __CLC_LONGN long
68+
#define __CLC_CONVERT_INTN __clc_convert_int
69+
#else
70+
#define __CLC_AS_LONGN __CLC_XCONCAT(__clc_as_long, __CLC_VECSIZE)
71+
#define __CLC_LONGN __CLC_XCONCAT(long, __CLC_VECSIZE)
72+
#define __CLC_CONVERT_INTN __CLC_XCONCAT(__clc_convert_int, __CLC_VECSIZE)
73+
#endif
74+
75+
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
76+
__clc_frexp(__CLC_GENTYPE x, __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
77+
__CLC_LONGN i = __CLC_AS_LONGN(x);
78+
__CLC_LONGN ai = i & 0x7fffffffffffffffL;
79+
__CLC_LONGN d = ai > 0 & ai < 0x0010000000000000L;
80+
// scale subnormal by 2^54 without multiplying
81+
__CLC_GENTYPE s = __CLC_AS_GENTYPE(ai | 0x0370000000000000L) - 0x1.0p-968;
82+
ai = __clc_select(ai, __CLC_AS_LONGN(s), d);
83+
__CLC_LONGN e = (ai >> 52) - (__CLC_LONGN)1022 -
84+
__clc_select((__CLC_LONGN)0, (__CLC_LONGN)54, d);
85+
__CLC_LONGN t = ai == 0 | e == 1025;
86+
i = (i & (__CLC_LONGN)0x8000000000000000L) |
87+
(__CLC_LONGN)0x3fe0000000000000L |
88+
(ai & (__CLC_LONGN)0x000fffffffffffffL);
89+
*ep = __CLC_CONVERT_INTN(__clc_select(e, 0L, t));
90+
return __clc_select(__CLC_AS_GENTYPE(i), x, t);
91+
}
92+
93+
#undef __CLC_AS_LONGN
94+
#undef __CLC_LONGN
95+
#undef __CLC_CONVERT_INTN
96+
#endif
97+
98+
#undef __CLC_AS_GENTYPE
99+
#undef __CLC_AS_INTN

libclc/generic/lib/math/frexp.cl

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
#include <clc/clc.h>
2-
#include <clc/utils.h>
2+
#include <clc/math/clc_frexp.h>
33

4-
#define __CLC_BODY <frexp.inc>
5-
#define __CLC_ADDRESS_SPACE private
4+
#define FUNCTION frexp
5+
#define __CLC_BODY <clc/math/unary_def_with_int_ptr.inc>
66
#include <clc/math/gentype.inc>
7-
#undef __CLC_ADDRESS_SPACE
8-
9-
#define __CLC_BODY <frexp.inc>
10-
#define __CLC_ADDRESS_SPACE global
11-
#include <clc/math/gentype.inc>
12-
#undef __CLC_ADDRESS_SPACE
13-
14-
#define __CLC_BODY <frexp.inc>
15-
#define __CLC_ADDRESS_SPACE local
16-
#include <clc/math/gentype.inc>
17-
#undef __CLC_ADDRESS_SPACE

libclc/generic/lib/math/frexp.inc

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)