Skip to content

[libclc] Move frexp to CLC library; optimize half vecs #127836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libclc/clc/include/clc/math/clc_frexp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef __CLC_MATH_CLC_FREXP_H__
#define __CLC_MATH_CLC_FREXP_H__

#define __CLC_FUNCTION __clc_frexp
#define __CLC_BODY <clc/math/unary_decl_with_int_ptr.inc>
#include <clc/math/gentype.inc>

#undef __CLC_BODY
#undef __CLC_FUNCTION

#endif // __CLC_MATH_CLC_FREXP_H__
6 changes: 6 additions & 0 deletions libclc/clc/include/clc/math/unary_decl_with_int_ptr.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
global __CLC_INTN *iptr);
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
local __CLC_INTN *iptr);
_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE __CLC_FUNCTION(__CLC_GENTYPE x,
private __CLC_INTN *iptr);
20 changes: 20 additions & 0 deletions libclc/clc/include/clc/math/unary_def_with_int_ptr.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <clc/utils.h>

#ifndef __CLC_FUNCTION
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x)
#endif

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
private __CLC_INTN *iptr) {
return __CLC_FUNCTION(FUNCTION)(x, iptr);
}

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
global __CLC_INTN *iptr) {
return __CLC_FUNCTION(FUNCTION)(x, iptr);
}

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE FUNCTION(__CLC_GENTYPE x,
local __CLC_INTN *iptr) {
return __CLC_FUNCTION(FUNCTION)(x, iptr);
}
6 changes: 1 addition & 5 deletions libclc/clc/include/clc/relational/clc_select.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#ifndef __CLC_RELATIONAL_CLC_SELECT_H__
#define __CLC_RELATIONAL_CLC_SELECT_H__

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

#define __CLC_SELECT_FN __clc_select

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

#undef __CLC_SELECT_FN
#undef __CLC_CONCAT
#undef __CLC_XCONCAT

#endif // __CLC_RELATIONAL_CLC_SELECT_H__
1 change: 1 addition & 0 deletions libclc/clc/lib/generic/SOURCES
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ math/clc_ceil.cl
math/clc_copysign.cl
math/clc_fabs.cl
math/clc_floor.cl
math/clc_frexp.cl
math/clc_mad.cl
math/clc_nextafter.cl
math/clc_rint.cl
Expand Down
42 changes: 42 additions & 0 deletions libclc/clc/lib/generic/math/clc_frexp.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2015 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <clc/clc_convert.h>
#include <clc/internal/clc.h>
#include <clc/math/math.h>
#include <clc/relational/clc_select.h>
#include <clc/utils.h>

#define __CLC_BODY <clc_frexp.inc>
#define __CLC_ADDRESS_SPACE private
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE

#define __CLC_BODY <clc_frexp.inc>
#define __CLC_ADDRESS_SPACE global
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE

#define __CLC_BODY <clc_frexp.inc>
#define __CLC_ADDRESS_SPACE local
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE
99 changes: 99 additions & 0 deletions libclc/clc/lib/generic/math/clc_frexp.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2014 Advanced Micro Devices, Inc.
* Copyright (c) 2016 Aaron Watry
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <clc/clcmacro.h>
#include <clc/utils.h>

#define __CLC_AS_GENTYPE __CLC_XCONCAT(__clc_as_, __CLC_GENTYPE)
#define __CLC_AS_INTN __CLC_XCONCAT(__clc_as_, __CLC_INTN)

#if __CLC_FPSIZE == 32
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
__clc_frexp(__CLC_GENTYPE x, __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
__CLC_INTN i = __CLC_AS_INTN(x);
__CLC_INTN ai = i & 0x7fffffff;
__CLC_INTN d = ai > 0 & ai < 0x00800000;
/* scale subnormal by 2^26 without multiplying */
__CLC_GENTYPE s = __CLC_AS_GENTYPE(ai | 0x0d800000) - 0x1.0p-100f;
ai = __clc_select(ai, __CLC_AS_INTN(s), d);
__CLC_INTN e =
(ai >> 23) - 126 - __clc_select((__CLC_INTN)0, (__CLC_INTN)26, d);
__CLC_INTN t = ai == (__CLC_INTN)0 | e == (__CLC_INTN)129;
i = (i & (__CLC_INTN)0x80000000) | (__CLC_INTN)0x3f000000 | (ai & 0x007fffff);
*ep = __clc_select(e, (__CLC_INTN)0, t);
return __clc_select(__CLC_AS_GENTYPE(i), x, t);
}
#endif

#if __CLC_FPSIZE == 16
#ifdef __CLC_SCALAR
#define __CLC_CONVERT_HALFN __clc_convert_half
#define __CLC_CONVERT_FLOATN __clc_convert_float
#else
#define __CLC_CONVERT_HALFN __CLC_XCONCAT(__clc_convert_half, __CLC_VECSIZE)
#define __CLC_CONVERT_FLOATN __CLC_XCONCAT(__clc_convert_float, __CLC_VECSIZE)
#endif
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
__clc_frexp(__CLC_GENTYPE x, __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
return __CLC_CONVERT_HALFN(__clc_frexp(__CLC_CONVERT_FLOATN(x), ep));
}
#undef __CLC_CONVERT_FLOATN
#undef __CLC_CONVERT_HALFN
#endif

#if __CLC_FPSIZE == 64
#ifdef __CLC_SCALAR
#define __CLC_AS_LONGN __clc_as_long
#define __CLC_LONGN long
#define __CLC_CONVERT_INTN __clc_convert_int
#else
#define __CLC_AS_LONGN __CLC_XCONCAT(__clc_as_long, __CLC_VECSIZE)
#define __CLC_LONGN __CLC_XCONCAT(long, __CLC_VECSIZE)
#define __CLC_CONVERT_INTN __CLC_XCONCAT(__clc_convert_int, __CLC_VECSIZE)
#endif

_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE
__clc_frexp(__CLC_GENTYPE x, __CLC_ADDRESS_SPACE __CLC_INTN *ep) {
__CLC_LONGN i = __CLC_AS_LONGN(x);
__CLC_LONGN ai = i & 0x7fffffffffffffffL;
__CLC_LONGN d = ai > 0 & ai < 0x0010000000000000L;
// scale subnormal by 2^54 without multiplying
__CLC_GENTYPE s = __CLC_AS_GENTYPE(ai | 0x0370000000000000L) - 0x1.0p-968;
ai = __clc_select(ai, __CLC_AS_LONGN(s), d);
__CLC_LONGN e = (ai >> 52) - (__CLC_LONGN)1022 -
__clc_select((__CLC_LONGN)0, (__CLC_LONGN)54, d);
__CLC_LONGN t = ai == 0 | e == 1025;
i = (i & (__CLC_LONGN)0x8000000000000000L) |
(__CLC_LONGN)0x3fe0000000000000L |
(ai & (__CLC_LONGN)0x000fffffffffffffL);
*ep = __CLC_CONVERT_INTN(__clc_select(e, 0L, t));
return __clc_select(__CLC_AS_GENTYPE(i), x, t);
}

#undef __CLC_AS_LONGN
#undef __CLC_LONGN
#undef __CLC_CONVERT_INTN
#endif

#undef __CLC_AS_GENTYPE
#undef __CLC_AS_INTN
17 changes: 3 additions & 14 deletions libclc/generic/lib/math/frexp.cl
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
#include <clc/clc.h>
#include <clc/utils.h>
#include <clc/math/clc_frexp.h>

#define __CLC_BODY <frexp.inc>
#define __CLC_ADDRESS_SPACE private
#define FUNCTION frexp
#define __CLC_BODY <clc/math/unary_def_with_int_ptr.inc>
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE

#define __CLC_BODY <frexp.inc>
#define __CLC_ADDRESS_SPACE global
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE

#define __CLC_BODY <frexp.inc>
#define __CLC_ADDRESS_SPACE local
#include <clc/math/gentype.inc>
#undef __CLC_ADDRESS_SPACE
87 changes: 0 additions & 87 deletions libclc/generic/lib/math/frexp.inc

This file was deleted.