From c8690cb4213e1a4d47406484dec260f406b69525 Mon Sep 17 00:00:00 2001 From: Fabian Knorr Date: Tue, 19 Dec 2023 14:44:43 +0100 Subject: [PATCH] Make sycl::half support compiler-dependent --- CMakeLists.txt | 8 ++++++++ cmake/simsycl-config.cmake.in | 3 +++ include/simsycl/config.hh.in | 2 ++ include/simsycl/sycl/forward.hh | 3 +++ include/simsycl/sycl/math.hh | 9 +++++++++ include/simsycl/sycl/type_traits.hh | 13 ++++++++++++- include/simsycl/sycl/vec.hh | 2 ++ 7 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 11c4263..1038c72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,14 @@ endif() find_package(Boost 1.70 COMPONENTS context REQUIRED) +include(CheckTypeSize) +check_type_size(_Float16 FLOAT16 BUILTIN_TYPES_ONLY LANGUAGE CXX) +if (HAVE_FLOAT16) + set(SIMSYCL_FEATURE_HALF_TYPE 1) +else () + set(SIMSYCL_FEATURE_HALF_TYPE 0) +endif() + set(SIMSYCL_CHECK_MODE "SIMSYCL_CHECK_ABORT" CACHE STRING "Check mode to use") set(CONFIG_PATH "${CMAKE_CURRENT_BINARY_DIR}/include/simsycl/config.hh") diff --git a/cmake/simsycl-config.cmake.in b/cmake/simsycl-config.cmake.in index dd005fe..07e4a2b 100644 --- a/cmake/simsycl-config.cmake.in +++ b/cmake/simsycl-config.cmake.in @@ -19,3 +19,6 @@ find_dependency(Boost 1.70 COMPONENTS context REQUIRED) include("${CMAKE_CURRENT_LIST_DIR}/simsycl-targets.cmake") set(CMAKE_MODULE_PATH "${SIMSYCL_ORIGINAL_CMAKE_MODULE_PATH}") + +set(SIMSYCL_FEATURE_HALF_TYPE "@SIMSYCL_FEATURE_HALF_TYPE@") +set(SIMSYCL_CHECK_MODE "@SIMSYCL_CHECK_MODE@") diff --git a/include/simsycl/config.hh.in b/include/simsycl/config.hh.in index 2423424..80af744 100644 --- a/include/simsycl/config.hh.in +++ b/include/simsycl/config.hh.in @@ -5,6 +5,8 @@ #define SIMSYCL_VERSION_MINOR @SIMSYCL_VERSION_MINOR@ #define SIMSYCL_VERSION_PATCH @SIMSYCL_VERSION_PATCH@ +#define SIMSYCL_FEATURE_HALF_TYPE @SIMSYCL_FEATURE_HALF_TYPE@ + #ifndef SIMSYCL_CHECK_MODE #define SIMSYCL_CHECK_MODE @SIMSYCL_CHECK_MODE@ #endif diff --git a/include/simsycl/sycl/forward.hh b/include/simsycl/sycl/forward.hh index f61fc82..0fb9bc1 100644 --- a/include/simsycl/sycl/forward.hh +++ b/include/simsycl/sycl/forward.hh @@ -3,6 +3,7 @@ #include "allocator.hh" #include "enums.hh" +#include #include "../detail/preprocessor.hh" #include @@ -46,7 +47,9 @@ class exception_list; class handler; +#if SIMSYCL_FEATURE_HALF_TYPE using half = _Float16; // currently requires a compiler that supports _Float16 +#endif template ? access_mode::read : access_mode::read_write)> diff --git a/include/simsycl/sycl/math.hh b/include/simsycl/sycl/math.hh index 0b2d7ef..10079af 100644 --- a/include/simsycl/sycl/math.hh +++ b/include/simsycl/sycl/math.hh @@ -21,6 +21,8 @@ return result; \ } +#if SIMSYCL_FEATURE_HALF_TYPE + #define SIMSYCL_DETAIL_MATH_DERIVE_UNARY_FUNCTION_FOR_HALF(name) \ inline half name(const half x) { return static_cast(name(static_cast(x))); } @@ -34,6 +36,13 @@ return static_cast(name(static_cast(x), static_cast(y), static_cast(z))); \ } +#else + +#define SIMSYCL_DETAIL_MATH_DERIVE_UNARY_FUNCTION_FOR_HALF(name) +#define SIMSYCL_DETAIL_MATH_DERIVE_BINARY_FUNCTION_FOR_HALF(name) +#define SIMSYCL_DETAIL_MATH_DERIVE_TERNARY_FUNCTION_FOR_HALF(name) + +#endif namespace simsycl::sycl { diff --git a/include/simsycl/sycl/type_traits.hh b/include/simsycl/sycl/type_traits.hh index 17b43fa..79d9ee7 100644 --- a/include/simsycl/sycl/type_traits.hh +++ b/include/simsycl/sycl/type_traits.hh @@ -36,14 +36,25 @@ struct is_function_object : std::false_type {}; template inline constexpr bool is_function_object_v = is_function_object::value; +#if SIMSYCL_FEATURE_HALF_TYPE template struct is_arithmetic : std::bool_constant || std::is_same_v> {}; +#else +using std::is_arithmetic; +#endif + template inline constexpr bool is_arithmetic_v = is_arithmetic::value; template struct is_floating_point - : std::bool_constant || std::is_same_v || std::is_same_v> {}; +#if SIMSYCL_FEATURE_HALF_TYPE + : std::bool_constant || std::is_same_v || std::is_same_v> { +#else + : std::bool_constant || std::is_same_v> { +#endif +}; + template inline constexpr bool is_floating_point_v = is_floating_point::value; diff --git a/include/simsycl/sycl/vec.hh b/include/simsycl/sycl/vec.hh index c0419ca..b3be697 100644 --- a/include/simsycl/sycl/vec.hh +++ b/include/simsycl/sycl/vec.hh @@ -475,11 +475,13 @@ using ulong4 = vec; using ulong8 = vec; using ulong16 = vec; +#if SIMSYCL_FEATURE_HALF_TYPE using half2 = vec; using half3 = vec; using half4 = vec; using half8 = vec; using half16 = vec; +#endif using float2 = vec; using float3 = vec;