Skip to content

Commit 9f8e480

Browse files
authored
[SYCL][NVPTX][AMDGCN] Move MSVC math functions to headers (#19718)
MSVC's `<complex>` header uses a number of non-standard functions defined in `<cmath>`. Move definitions of these functions to headers to fix compiltion on Windows platform with MSVC.
1 parent 6810583 commit 9f8e480

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

libdevice/msvc_math.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#if defined(__SPIR__) || defined(__SPIRV__) || defined(__NVPTX__)
9+
#if defined(__SPIR__) || defined(__SPIRV__)
1010

1111
#include "device.h"
1212
#include <math.h>
@@ -281,4 +281,4 @@ float _FSinh(float x, float y) { // compute y * sinh(x), |y| <= 1
281281
return neg ? -x : x;
282282
}
283283
}
284-
#endif // __SPIR__ || __SPIRV__ || __NVPTX__
284+
#endif // __SPIR__ || __SPIRV__

sycl/include/sycl/stl_wrappers/__sycl_cmath_wrapper_impl.hpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
// The 'sycl_device_only' attribute enables device-side overloading.
1818
#define __SYCL_DEVICE __attribute__((sycl_device_only, always_inline))
19-
#define __SYCL_DEVICE_C \
20-
extern "C" __attribute__((sycl_device_only, always_inline))
19+
#define __SYCL_DEVICE_C extern "C" __SYCL_DEVICE
2120

2221
#include <type_traits>
2322

@@ -259,9 +258,16 @@ __SYCL_SPIRV_MAP_BINARY_C(nextafter);
259258
__SYCL_SPIRV_MAP_BINARY_C(copysign);
260259

261260
/// Classification and comparison
262-
//
263261

264-
// unsupported: fpclassify
262+
__SYCL_DEVICE_C int fpclassify(float x) {
263+
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
264+
FP_ZERO, x);
265+
}
266+
__SYCL_DEVICE_C int fpclassify(double x) {
267+
return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL,
268+
FP_ZERO, x);
269+
}
270+
265271
// unsupported: isfinite
266272
// unsupported: isinf
267273
// unsupported: isnan
@@ -460,7 +466,7 @@ template <typename T> __SYCL_DEVICE __sycl_promote_t<T> scalbn(T x, int exp) {
460466
__SYCL_SPIRV_MAP_BINARY_CXX(copysign);
461467

462468
// Classification and comparison
463-
// using ::fpclassify;
469+
using ::fpclassify;
464470
// using ::isfinite;
465471
// using ::isgreater;
466472
// using ::isgreaterequal;
@@ -482,11 +488,29 @@ _GLIBCXX_END_NAMESPACE_VERSION
482488
} // namespace std
483489
#endif
484490

491+
#if defined(_WIN32)
492+
__SYCL_DEVICE_C double _Cosh(double x, double y) { return cosh(x) * y; }
493+
__SYCL_DEVICE_C float _FCosh(float x, float y) { return coshf(x) * y; }
494+
__SYCL_DEVICE_C short _Dtest(double *p) { return fpclassify(*p); }
495+
__SYCL_DEVICE_C short _FDtest(float *p) { return fpclassify(*p); }
496+
__SYCL_DEVICE_C double _Sinh(double x, double y) { return sinh(x) * y; }
497+
__SYCL_DEVICE_C float _FSinh(float x, float y) { return sinhf(x) * y; }
498+
__SYCL_DEVICE_C short _Exp(double *px, double y, short eoff) {
499+
return exp(*px) * ldexp(y, eoff);
500+
}
501+
__SYCL_DEVICE_C short _FExp(float *px, float y, short eoff) {
502+
return exp(*px) * ldexp(y, eoff);
503+
}
504+
__SYCL_DEVICE_C float _hypotf(float x, float y) { return hypotf(x, y); }
505+
__SYCL_DEVICE_C int _fdsign(float x) { return __builtin_signbit(x); }
506+
__SYCL_DEVICE_C int _dsign(double x) { return __builtin_signbit(x); }
507+
#endif // defined(_WIN32)
508+
485509
#undef __SYCL_SPIRV_MAP_BINARY_C
486510
#undef __SYCL_SPIRV_MAP_BINARY_CXX
487511
#undef __SYCL_SPIRV_MAP_UNARY_C
488512
#undef __SYCL_SPIRV_MAP_UNARY_CXX
489513
#undef __SYCL_DEVICE_C
490514
#undef __SYCL_DEVICE
491-
#endif
492-
#endif
515+
#endif // __SYCL_DEVICE_ONLY__
516+
#endif // __SYCL_CMATH_WRAPPER_IMPL_HPP__

sycl/include/sycl/stl_wrappers/cmath

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88

99
#pragma once
1010

11+
#if defined(__SYCL_DEVICE_ONLY__) && defined(_WIN32)
12+
#if defined(__NVPTX__) || defined(__AMDGCN__)
13+
// Forward declare device _*dsign functions before including cmath to enable
14+
// compilation of math functions defined in UCRT headers.
15+
extern "C" __attribute__((sycl_device_only, always_inline)) int
16+
_fdsign(float x);
17+
extern "C" __attribute__((sycl_device_only, always_inline)) int
18+
_dsign(double x);
19+
#endif // defined(__NVPTX__) || defined(__AMDGCN__)
20+
#endif // defined(__SYCL_DEVICE_ONLY__) && defined(_WIN32)
21+
1122
// Include real STL <cmath> header - the next one from the include search
1223
// directories.
1324
#if defined(__has_include_next)

sycl/test/self-contained-headers/lit.local.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ config.test_format = SYCLHeadersTest()
77
# standalone. `os.path.join` is required here so the filtering works
88
# cross-platform
99
config.sycl_headers_xfail = [
10+
# FIXME: remove this rule when the header is moved to the clang project
11+
os.path.join(
12+
"sycl", "stl_wrappers", "__sycl_cmath_wrapper_impl.hpp"
13+
),
1014
]

0 commit comments

Comments
 (0)