-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libclc] Move hypot to CLC library; optimize #129551
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,12 @@ | ||
#ifndef __CLC_MATH_CLC_HYPOT_H__ | ||
#define __CLC_MATH_CLC_HYPOT_H__ | ||
|
||
#define __CLC_BODY <clc/shared/binary_decl.inc> | ||
#define __CLC_FUNCTION __clc_hypot | ||
|
||
#include <clc/math/gentype.inc> | ||
|
||
#undef __CLC_BODY | ||
#undef __CLC_FUNCTION | ||
|
||
#endif // __CLC_MATH_CLC_HYPOT_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2014 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/clcmacro.h> | ||
#include <clc/integer/clc_abs.h> | ||
#include <clc/internal/clc.h> | ||
#include <clc/math/clc_fma.h> | ||
#include <clc/math/clc_mad.h> | ||
#include <clc/math/clc_sqrt.h> | ||
#include <clc/math/clc_subnormal_config.h> | ||
#include <clc/math/math.h> | ||
#include <clc/relational/clc_isnan.h> | ||
#include <clc/shared/clc_clamp.h> | ||
|
||
#define __CLC_BODY <clc_hypot.inc> | ||
#include <clc/math/gentype.inc> | ||
#undef __CLC_BODY |
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,108 @@ | ||
/* | ||
* Copyright (c) 2014 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. | ||
*/ | ||
|
||
// Returns sqrt(x*x + y*y) with no overflow or underflow unless the result | ||
// warrants it | ||
|
||
#if __CLC_FPSIZE == 32 | ||
_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_hypot(__CLC_GENTYPE x, | ||
__CLC_GENTYPE y) { | ||
__CLC_UINTN ux = __CLC_AS_UINTN(x); | ||
__CLC_UINTN aux = ux & EXSIGNBIT_SP32; | ||
__CLC_UINTN uy = __CLC_AS_UINTN(y); | ||
__CLC_UINTN auy = uy & EXSIGNBIT_SP32; | ||
__CLC_INTN c = aux > auy; | ||
ux = c ? aux : auy; | ||
uy = c ? auy : aux; | ||
|
||
__CLC_INTN xexp = __clc_clamp( | ||
__CLC_AS_INTN(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126); | ||
__CLC_GENTYPE fx_exp = | ||
__CLC_AS_GENTYPE((xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32); | ||
__CLC_GENTYPE fi_exp = | ||
__CLC_AS_GENTYPE((-xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32); | ||
__CLC_GENTYPE fx = __CLC_AS_GENTYPE(ux) * fi_exp; | ||
__CLC_GENTYPE fy = __CLC_AS_GENTYPE(uy) * fi_exp; | ||
|
||
__CLC_GENTYPE retval = __clc_sqrt(__clc_mad(fx, fx, fy * fy)) * fx_exp; | ||
|
||
retval = (ux > PINFBITPATT_SP32 || uy == 0) ? __CLC_AS_GENTYPE(ux) : retval; | ||
retval = (ux == PINFBITPATT_SP32 || uy == PINFBITPATT_SP32) | ||
? __CLC_AS_GENTYPE((__CLC_UINTN)PINFBITPATT_SP32) | ||
: retval; | ||
frasercrmck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return retval; | ||
} | ||
|
||
#elif __CLC_FPSIZE == 64 | ||
|
||
_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_hypot(__CLC_GENTYPE x, | ||
__CLC_GENTYPE y) { | ||
__CLC_ULONGN ux = __CLC_AS_ULONGN(x) & ~SIGNBIT_DP64; | ||
__CLC_INTN xexp = __CLC_CONVERT_INTN(ux >> EXPSHIFTBITS_DP64); | ||
x = __CLC_AS_GENTYPE(ux); | ||
|
||
__CLC_ULONGN uy = __CLC_AS_ULONGN(y) & ~SIGNBIT_DP64; | ||
__CLC_INTN yexp = __CLC_CONVERT_INTN(uy >> EXPSHIFTBITS_DP64); | ||
y = __CLC_AS_GENTYPE(uy); | ||
|
||
__CLC_LONGN c = __CLC_CONVERT_LONGN(xexp > EXPBIAS_DP64 + 500 || | ||
yexp > EXPBIAS_DP64 + 500); | ||
__CLC_GENTYPE preadjust = c ? 0x1.0p-600 : 1.0; | ||
__CLC_GENTYPE postadjust = c ? 0x1.0p+600 : 1.0; | ||
|
||
c = __CLC_CONVERT_LONGN(xexp < EXPBIAS_DP64 - 500 || | ||
yexp < EXPBIAS_DP64 - 500); | ||
preadjust = c ? 0x1.0p+600 : preadjust; | ||
postadjust = c ? 0x1.0p-600 : postadjust; | ||
|
||
__CLC_GENTYPE ax = x * preadjust; | ||
__CLC_GENTYPE ay = y * preadjust; | ||
|
||
// The post adjust may overflow, but this can't be avoided in any case | ||
__CLC_GENTYPE r = __clc_sqrt(__clc_fma(ax, ax, ay * ay)) * postadjust; | ||
|
||
// If the difference in exponents between x and y is large | ||
__CLC_GENTYPE s = x + y; | ||
c = __CLC_CONVERT_LONGN(__clc_abs(xexp - yexp) > MANTLENGTH_DP64 + 1); | ||
r = c ? s : r; | ||
|
||
// Check for NaN | ||
c = __clc_isnan(x) || __clc_isnan(y); | ||
r = c ? __CLC_AS_GENTYPE((__CLC_ULONGN)QNANBITPATT_DP64) : r; | ||
frasercrmck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// If either is Inf, we must return Inf | ||
c = x == __CLC_AS_GENTYPE((__CLC_ULONGN)PINFBITPATT_DP64) || | ||
y == __CLC_AS_GENTYPE((__CLC_ULONGN)PINFBITPATT_DP64); | ||
r = c ? __CLC_AS_GENTYPE((__CLC_ULONGN)PINFBITPATT_DP64) : r; | ||
|
||
return r; | ||
} | ||
|
||
#elif __CLC_FPSIZE == 16 | ||
|
||
_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_hypot(__CLC_GENTYPE x, | ||
__CLC_GENTYPE y) { | ||
return __CLC_CONVERT_GENTYPE( | ||
__clc_hypot(__CLC_CONVERT_FLOATN(x), __CLC_CONVERT_FLOATN(y))); | ||
} | ||
|
||
#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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#include <clc/clc.h> | ||
#include <clc/math/clc_hypot.h> | ||
|
||
#include <math/clc_hypot.h> | ||
|
||
#define __CLC_FUNC hypot | ||
#define __CLC_BODY <clc_sw_binary.inc> | ||
#define FUNCTION hypot | ||
#define __CLC_BODY <clc/shared/binary_def.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
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.
Uh oh!
There was an error while loading. Please reload this page.