Skip to content

Commit

Permalink
Make sycl::half support compiler-dependent
Browse files Browse the repository at this point in the history
  • Loading branch information
fknorr committed Dec 19, 2023
1 parent 810088d commit c8690cb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions cmake/simsycl-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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@")
2 changes: 2 additions & 0 deletions include/simsycl/config.hh.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions include/simsycl/sycl/forward.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "allocator.hh"
#include "enums.hh"

#include <simsycl/config.hh>
#include "../detail/preprocessor.hh"

#include <functional>
Expand Down Expand Up @@ -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<typename DataT, int Dimensions = 1,
access_mode AccessMode = (std::is_const_v<DataT> ? access_mode::read : access_mode::read_write)>
Expand Down
9 changes: 9 additions & 0 deletions include/simsycl/sycl/math.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<half>(name(static_cast<float>(x))); }

Expand All @@ -34,6 +36,13 @@
return static_cast<half>(name(static_cast<float>(x), static_cast<float>(y), static_cast<float>(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 {

Expand Down
13 changes: 12 additions & 1 deletion include/simsycl/sycl/type_traits.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,25 @@ struct is_function_object : std::false_type {};
template<class Fn>
inline constexpr bool is_function_object_v = is_function_object<Fn>::value;

#if SIMSYCL_FEATURE_HALF_TYPE
template<typename T>
struct is_arithmetic : std::bool_constant<std::is_arithmetic_v<T> || std::is_same_v<T, sycl::half>> {};
#else
using std::is_arithmetic;
#endif

template<class T>
inline constexpr bool is_arithmetic_v = is_arithmetic<T>::value;

template<typename T>
struct is_floating_point
: std::bool_constant<std::is_same_v<T, sycl::half> || std::is_same_v<T, float> || std::is_same_v<T, double>> {};
#if SIMSYCL_FEATURE_HALF_TYPE
: std::bool_constant<std::is_same_v<T, sycl::half> || std::is_same_v<T, float> || std::is_same_v<T, double>> {
#else
: std::bool_constant<std::is_same_v<T, float> || std::is_same_v<T, double>> {
#endif
};

template<class T>
inline constexpr bool is_floating_point_v = is_floating_point<T>::value;

Expand Down
2 changes: 2 additions & 0 deletions include/simsycl/sycl/vec.hh
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,13 @@ using ulong4 = vec<uint64_t, 4>;
using ulong8 = vec<uint64_t, 8>;
using ulong16 = vec<uint64_t, 16>;

#if SIMSYCL_FEATURE_HALF_TYPE
using half2 = vec<half, 2>;
using half3 = vec<half, 3>;
using half4 = vec<half, 4>;
using half8 = vec<half, 8>;
using half16 = vec<half, 16>;
#endif

using float2 = vec<float, 2>;
using float3 = vec<float, 3>;
Expand Down

0 comments on commit c8690cb

Please sign in to comment.