-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libclc] Move logb/ilogb to CLC library; optimize #128028
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __CLC_MATH_CLC_ILOGB_H__ | ||
#define __CLC_MATH_CLC_ILOGB_H__ | ||
|
||
#define __CLC_FUNCTION __clc_ilogb | ||
#define __CLC_BODY <clc/math/unary_decl_with_int_return.inc> | ||
#include <clc/math/gentype.inc> | ||
|
||
#undef __CLC_FUNCTION | ||
|
||
#endif // __CLC_MATH_CLC_ILOGB_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef __CLC_MATH_CLC_LOGB_H__ | ||
#define __CLC_MATH_CLC_LOGB_H__ | ||
|
||
#define __CLC_FUNCTION __clc_logb | ||
#define __CLC_BODY <clc/shared/unary_decl.inc> | ||
#include <clc/math/gentype.inc> | ||
|
||
#undef __CLC_FUNCTION | ||
|
||
#endif // __CLC_MATH_CLC_LOGB_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <clc/utils.h> | ||
|
||
#ifndef __CLC_FUNCTION | ||
#define __CLC_FUNCTION(x) __CLC_CONCAT(__clc_, x) | ||
#endif | ||
|
||
_CLC_OVERLOAD _CLC_DEF __CLC_INTN FUNCTION(__CLC_GENTYPE a) { | ||
return __CLC_FUNCTION(FUNCTION)(a); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <clc/clc_convert.h> | ||
#include <clc/clcmacro.h> | ||
#include <clc/float/definitions.h> | ||
#include <clc/integer/clc_clz.h> | ||
#include <clc/internal/clc.h> | ||
#include <clc/math/math.h> | ||
|
||
#define __CLC_BODY <clc_ilogb.inc> | ||
#include <clc/math/gentype.inc> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if __CLC_FPSIZE == 32 | ||
|
||
_CLC_OVERLOAD _CLC_DEF __CLC_INTN __clc_ilogb(__CLC_GENTYPE x) { | ||
__CLC_UINTN ux = __CLC_AS_UINTN(x); | ||
__CLC_UINTN ax = ux & EXSIGNBIT_SP32; | ||
__CLC_INTN rs = (__CLC_INTN)LOG_MAGIC_NUM_SP32 - | ||
__CLC_AS_INTN(__clc_clz(ux & MANTBITS_SP32)); | ||
__CLC_INTN r = __CLC_AS_INTN(ax >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32; | ||
r = ax < 0x00800000U ? rs : r; | ||
r = ax == 0 ? FP_ILOGB0 : r; | ||
|
||
// We could merge those 2 tests and have: | ||
// | ||
// r = ax >= EXPBITS_SP32 ? 0x7fffffff : r | ||
// | ||
// since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and | ||
// FP_ILOGBNAN can change without requiring changes to __clc_ilogb() code. | ||
r = ax > EXPBITS_SP32 ? FP_ILOGBNAN : r; | ||
r = ax == EXPBITS_SP32 ? 0x7fffffff : r; | ||
return r; | ||
} | ||
|
||
#endif | ||
|
||
#if __CLC_FPSIZE == 64 | ||
|
||
_CLC_OVERLOAD _CLC_DEF __CLC_INTN __clc_ilogb(__CLC_GENTYPE x) { | ||
__CLC_ULONGN ux = __CLC_AS_ULONGN(x); | ||
__CLC_ULONGN ax = ux & ~SIGNBIT_DP64; | ||
__CLC_INTN rs = (__CLC_INTN)LOG_MAGIC_NUM_DP64 - | ||
__CLC_CONVERT_INTN(__clc_clz(ax & MANTBITS_DP64)); | ||
__CLC_INTN r = __CLC_CONVERT_INTN(ax >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64; | ||
r = __CLC_CONVERT_INTN(ax < 0x0010000000000000UL) ? rs : r; | ||
r = __CLC_CONVERT_INTN(ax == 0UL) ? (__CLC_INTN)FP_ILOGB0 : r; | ||
|
||
// We could merge those 2 tests and have: | ||
// | ||
// r = ax >= 0x7ff0000000000000UL ? 0x7fffffff : r | ||
// | ||
// since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and | ||
// FP_ILOGBNAN can change without requiring changes to __clc_ilogb() code. | ||
r = __CLC_CONVERT_INTN(ax > 0x7ff0000000000000UL) ? FP_ILOGBNAN : r; | ||
r = __CLC_CONVERT_INTN(ax == 0x7ff0000000000000UL) ? 0x7fffffff : r; | ||
return r; | ||
} | ||
|
||
#endif | ||
|
||
#if __CLC_FPSIZE == 16 | ||
|
||
_CLC_OVERLOAD _CLC_DEF __CLC_INTN __clc_ilogb(__CLC_GENTYPE x) { | ||
__CLC_USHORTN ux = __CLC_AS_USHORTN(x); | ||
__CLC_USHORTN ax = ux & (__CLC_USHORTN)EXSIGNBIT_FP16; | ||
__CLC_USHORTN mantx = ux & (__CLC_USHORTN)MANTBITS_FP16; | ||
__CLC_INTN rs = | ||
(__CLC_INTN)LOG_MAGIC_NUM_FP16 - __CLC_CONVERT_INTN(__clc_clz(mantx)); | ||
__CLC_INTN r = | ||
__CLC_CONVERT_INTN(ax >> (__CLC_USHORTN)EXPSHIFTBITS_FP16) - EXPBIAS_FP16; | ||
r = __CLC_CONVERT_INTN(ax < (__CLC_USHORTN)0x0400U) ? rs : r; | ||
r = __CLC_CONVERT_INTN(ax == (__CLC_USHORTN)0) ? (__CLC_INTN)FP_ILOGB0 : r; | ||
|
||
// We could merge those 2 tests and have: | ||
// | ||
// r = ax >= EXPBITS_FP16 ? 0x7fffffff : r | ||
// | ||
// since FP_ILOGBNAN is set to INT_MAX, but it's clearer this way and | ||
// FP_ILOGBNAN can change without requiring changes to __clc_ilogb() code. | ||
r = __CLC_CONVERT_INTN(ax > (__CLC_USHORTN)EXPBITS_FP16) ? FP_ILOGBNAN : r; | ||
r = __CLC_CONVERT_INTN(ax == (__CLC_USHORTN)EXPBITS_FP16) ? 0x7fffffff : r; | ||
return r; | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include <clc/clc_convert.h> | ||
#include <clc/clcmacro.h> | ||
#include <clc/float/definitions.h> | ||
#include <clc/integer/clc_clz.h> | ||
#include <clc/internal/clc.h> | ||
#include <clc/math/math.h> | ||
|
||
#define __CLC_BODY <clc_logb.inc> | ||
#include <clc/math/gentype.inc> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if __CLC_FPSIZE == 32 | ||
|
||
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_logb(__CLC_GENTYPE x) { | ||
__CLC_INTN ax = __CLC_AS_INTN(x) & EXSIGNBIT_SP32; | ||
__CLC_GENTYPE s = __CLC_CONVERT_GENTYPE(LOG_MAGIC_NUM_SP32 - __clc_clz(ax)); | ||
__CLC_GENTYPE r = | ||
__CLC_CONVERT_GENTYPE((ax >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32); | ||
r = ax >= PINFBITPATT_SP32 ? __CLC_AS_GENTYPE(ax) : r; | ||
r = ax < 0x00800000 ? s : r; | ||
r = ax == 0 ? __CLC_AS_GENTYPE((__CLC_INTN)NINFBITPATT_SP32) : r; | ||
return r; | ||
} | ||
|
||
#endif | ||
|
||
#if __CLC_FPSIZE == 64 | ||
|
||
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_logb(__CLC_GENTYPE x) { | ||
__CLC_LONGN ax = __CLC_AS_LONGN(x) & EXSIGNBIT_DP64; | ||
__CLC_GENTYPE s = __CLC_CONVERT_GENTYPE(LOG_MAGIC_NUM_DP64 - __clc_clz(ax)); | ||
__CLC_GENTYPE r = | ||
__CLC_CONVERT_GENTYPE((ax >> EXPSHIFTBITS_DP64) - EXPBIAS_DP64); | ||
r = ax >= PINFBITPATT_DP64 ? __CLC_AS_GENTYPE(ax) : r; | ||
r = ax < 0x0010000000000000L ? s : r; | ||
r = ax == 0L ? __CLC_AS_GENTYPE((__CLC_LONGN)NINFBITPATT_DP64) : r; | ||
return r; | ||
} | ||
|
||
#endif | ||
|
||
#if __CLC_FPSIZE == 16 | ||
|
||
_CLC_OVERLOAD _CLC_DEF __CLC_GENTYPE __clc_logb(__CLC_GENTYPE x) { | ||
__CLC_SHORTN ax = __CLC_AS_SHORTN(x) & (__CLC_SHORTN)EXSIGNBIT_FP16; | ||
__CLC_GENTYPE s = __CLC_CONVERT_GENTYPE((__CLC_SHORTN)LOG_MAGIC_NUM_FP16 - | ||
(__CLC_SHORTN)__clc_clz(ax)); | ||
__CLC_GENTYPE r = __CLC_CONVERT_GENTYPE( | ||
(ax >> (__CLC_SHORTN)EXPSHIFTBITS_FP16) - (__CLC_SHORTN)EXPBIAS_FP16); | ||
r = ax >= (__CLC_SHORTN)PINFBITPATT_FP16 ? __CLC_AS_GENTYPE(ax) : r; | ||
r = ax < (__CLC_SHORTN)0x0400 ? s : r; | ||
r = ax == (__CLC_SHORTN)0 ? __CLC_AS_GENTYPE((__CLC_SHORTN)NINFBITPATT_FP16) | ||
: r; | ||
return r; | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be implemented in terms of frexp and avoid relying on the exponent bits.
Something like:
Might be able to avoid the edge case handling too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although I guess this better if you have a dedicated frexp_exp instruction. Could factor out the frexp_exp part into a utility function, which does the bithacking or the intrinsic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm not sure using
frexp
is currently better given that there's no native path for it. We talked about allowing that in the frexp PR.But yeah even in that case, if we use the builtin/intrinsic for
frexp
it's not necessarily the case that it's faster than this implementation forilogb
. So I think the logb/ilogb-specific utility function would be the way to go.I don't think we've got canonical examples of target-specific code paths for utility functions in libclc. We could do it with the
SOURCES
system, or preprocessor defines or some other system. I'm not yet sure.