Skip to content

Commit e5d5503

Browse files
authored
[libclc] Move hypot to CLC library; optimize (llvm#129551)
This was already nominally in the CLC library; this commit just formally moves it over. It simultaneously optimizes it for vector types by avoiding scalarization.
1 parent f838a5e commit e5d5503

File tree

10 files changed

+161
-118
lines changed

10 files changed

+161
-118
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __CLC_MATH_CLC_HYPOT_H__
2+
#define __CLC_MATH_CLC_HYPOT_H__
3+
4+
#define __CLC_BODY <clc/shared/binary_decl.inc>
5+
#define __CLC_FUNCTION __clc_hypot
6+
7+
#include <clc/math/gentype.inc>
8+
9+
#undef __CLC_BODY
10+
#undef __CLC_FUNCTION
11+
12+
#endif // __CLC_MATH_CLC_HYPOT_H__

libclc/clc/lib/generic/SOURCES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ math/clc_fabs.cl
2323
math/clc_fma.cl
2424
math/clc_floor.cl
2525
math/clc_frexp.cl
26+
math/clc_hypot.cl
2627
math/clc_ldexp.cl
2728
math/clc_log.cl
2829
math/clc_log10.cl
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2014 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/clcmacro.h>
25+
#include <clc/integer/clc_abs.h>
26+
#include <clc/internal/clc.h>
27+
#include <clc/math/clc_fma.h>
28+
#include <clc/math/clc_mad.h>
29+
#include <clc/math/clc_sqrt.h>
30+
#include <clc/math/clc_subnormal_config.h>
31+
#include <clc/math/math.h>
32+
#include <clc/relational/clc_isnan.h>
33+
#include <clc/shared/clc_clamp.h>
34+
35+
#define __CLC_BODY <clc_hypot.inc>
36+
#include <clc/math/gentype.inc>
37+
#undef __CLC_BODY
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2014 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+
// Returns sqrt(x*x + y*y) with no overflow or underflow unless the result
24+
// warrants it
25+
26+
#if __CLC_FPSIZE == 32
27+
_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_hypot(__CLC_GENTYPE x,
28+
__CLC_GENTYPE y) {
29+
__CLC_UINTN ux = __CLC_AS_UINTN(x);
30+
__CLC_UINTN aux = ux & EXSIGNBIT_SP32;
31+
__CLC_UINTN uy = __CLC_AS_UINTN(y);
32+
__CLC_UINTN auy = uy & EXSIGNBIT_SP32;
33+
__CLC_INTN c = aux > auy;
34+
ux = c ? aux : auy;
35+
uy = c ? auy : aux;
36+
37+
__CLC_INTN xexp = __clc_clamp(
38+
__CLC_AS_INTN(ux >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32, -126, 126);
39+
__CLC_GENTYPE fx_exp =
40+
__CLC_AS_GENTYPE((xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
41+
__CLC_GENTYPE fi_exp =
42+
__CLC_AS_GENTYPE((-xexp + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);
43+
__CLC_GENTYPE fx = __CLC_AS_GENTYPE(ux) * fi_exp;
44+
__CLC_GENTYPE fy = __CLC_AS_GENTYPE(uy) * fi_exp;
45+
46+
__CLC_GENTYPE retval = __clc_sqrt(__clc_mad(fx, fx, fy * fy)) * fx_exp;
47+
48+
retval = (ux > PINFBITPATT_SP32 || uy == 0) ? __CLC_AS_GENTYPE(ux) : retval;
49+
retval = (ux == PINFBITPATT_SP32 || uy == PINFBITPATT_SP32)
50+
? __CLC_AS_GENTYPE((__CLC_UINTN)PINFBITPATT_SP32)
51+
: retval;
52+
return retval;
53+
}
54+
55+
#elif __CLC_FPSIZE == 64
56+
57+
_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_hypot(__CLC_GENTYPE x,
58+
__CLC_GENTYPE y) {
59+
__CLC_ULONGN ux = __CLC_AS_ULONGN(x) & ~SIGNBIT_DP64;
60+
__CLC_INTN xexp = __CLC_CONVERT_INTN(ux >> EXPSHIFTBITS_DP64);
61+
x = __CLC_AS_GENTYPE(ux);
62+
63+
__CLC_ULONGN uy = __CLC_AS_ULONGN(y) & ~SIGNBIT_DP64;
64+
__CLC_INTN yexp = __CLC_CONVERT_INTN(uy >> EXPSHIFTBITS_DP64);
65+
y = __CLC_AS_GENTYPE(uy);
66+
67+
__CLC_LONGN c = __CLC_CONVERT_LONGN(xexp > EXPBIAS_DP64 + 500 ||
68+
yexp > EXPBIAS_DP64 + 500);
69+
__CLC_GENTYPE preadjust = c ? 0x1.0p-600 : 1.0;
70+
__CLC_GENTYPE postadjust = c ? 0x1.0p+600 : 1.0;
71+
72+
c = __CLC_CONVERT_LONGN(xexp < EXPBIAS_DP64 - 500 ||
73+
yexp < EXPBIAS_DP64 - 500);
74+
preadjust = c ? 0x1.0p+600 : preadjust;
75+
postadjust = c ? 0x1.0p-600 : postadjust;
76+
77+
__CLC_GENTYPE ax = x * preadjust;
78+
__CLC_GENTYPE ay = y * preadjust;
79+
80+
// The post adjust may overflow, but this can't be avoided in any case
81+
__CLC_GENTYPE r = __clc_sqrt(__clc_fma(ax, ax, ay * ay)) * postadjust;
82+
83+
// If the difference in exponents between x and y is large
84+
__CLC_GENTYPE s = x + y;
85+
c = __CLC_CONVERT_LONGN(__clc_abs(xexp - yexp) > MANTLENGTH_DP64 + 1);
86+
r = c ? s : r;
87+
88+
// Check for NaN
89+
c = __clc_isnan(x) || __clc_isnan(y);
90+
r = c ? __CLC_AS_GENTYPE((__CLC_ULONGN)QNANBITPATT_DP64) : r;
91+
92+
// If either is Inf, we must return Inf
93+
c = x == __CLC_AS_GENTYPE((__CLC_ULONGN)PINFBITPATT_DP64) ||
94+
y == __CLC_AS_GENTYPE((__CLC_ULONGN)PINFBITPATT_DP64);
95+
r = c ? __CLC_AS_GENTYPE((__CLC_ULONGN)PINFBITPATT_DP64) : r;
96+
97+
return r;
98+
}
99+
100+
#elif __CLC_FPSIZE == 16
101+
102+
_CLC_DEF _CLC_OVERLOAD __CLC_GENTYPE __clc_hypot(__CLC_GENTYPE x,
103+
__CLC_GENTYPE y) {
104+
return __CLC_CONVERT_GENTYPE(
105+
__clc_hypot(__CLC_CONVERT_FLOATN(x), __CLC_CONVERT_FLOATN(y)));
106+
}
107+
108+
#endif

libclc/clspv/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ subnormal_config.cl
1818
../../generic/lib/math/cbrt.cl
1919
../../generic/lib/math/clc_exp10.cl
2020
../../generic/lib/math/clc_fmod.cl
21-
../../generic/lib/math/clc_hypot.cl
2221
../../generic/lib/math/clc_pow.cl
2322
../../generic/lib/math/clc_pown.cl
2423
../../generic/lib/math/clc_powr.cl

libclc/generic/include/math/clc_hypot.h

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

libclc/generic/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ math/half_rsqrt.cl
127127
math/half_sin.cl
128128
math/half_sqrt.cl
129129
math/half_tan.cl
130-
math/clc_hypot.cl
131130
math/hypot.cl
132131
math/ilogb.cl
133132
math/ldexp.cl

libclc/generic/lib/math/clc_hypot.cl

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

libclc/generic/lib/math/hypot.cl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <clc/clc.h>
2+
#include <clc/math/clc_hypot.h>
23

3-
#include <math/clc_hypot.h>
4-
5-
#define __CLC_FUNC hypot
6-
#define __CLC_BODY <clc_sw_binary.inc>
4+
#define FUNCTION hypot
5+
#define __CLC_BODY <clc/shared/binary_def.inc>
76
#include <clc/math/gentype.inc>

libclc/spirv/lib/SOURCES

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ math/fma.cl
4848
../../generic/lib/math/frexp.cl
4949
../../generic/lib/math/half_rsqrt.cl
5050
../../generic/lib/math/half_sqrt.cl
51-
../../generic/lib/math/clc_hypot.cl
5251
../../generic/lib/math/hypot.cl
5352
../../generic/lib/math/ilogb.cl
5453
../../generic/lib/math/ldexp.cl

0 commit comments

Comments
 (0)