|
| 1 | +//==-------------- bfloat16_type_traits.hpp - DPC++ Explicit SIMD API ------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// Implementation of SIMD element type traits for the bfloat16 type. |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <sycl/ext/intel/esimd/detail/elem_type_traits.hpp> |
| 14 | +#include <sycl/ext/intel/esimd/detail/intrin.hpp> |
| 15 | + |
| 16 | +#include <sycl/ext/oneapi/experimental/bfloat16.hpp> |
| 17 | + |
| 18 | +/// @cond ESIMD_DETAIL |
| 19 | + |
| 20 | +namespace sycl { |
| 21 | +__SYCL_INLINE_VER_NAMESPACE(_V1) { |
| 22 | +namespace ext::intel::esimd::detail { |
| 23 | + |
| 24 | +using bfloat16 = sycl::ext::oneapi::experimental::bfloat16; |
| 25 | + |
| 26 | +template <> struct element_type_traits<bfloat16> { |
| 27 | + // TODO map the raw type to __bf16 once SPIRV target supports it: |
| 28 | + using RawT = |
| 29 | + typename std::invoke_result_t<decltype(&bfloat16::raw), bfloat16>; |
| 30 | + // Nearest standard enclosing C++ type to delegate natively unsupported |
| 31 | + // operations to: |
| 32 | + using EnclosingCppT = float; |
| 33 | + // Can't map bfloat16 operations to opertations on RawT: |
| 34 | + static inline constexpr bool use_native_cpp_ops = false; |
| 35 | + static inline constexpr bool is_floating_point = true; |
| 36 | +}; |
| 37 | + |
| 38 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 39 | +// VC BE-specific glitch |
| 40 | +// @llvm.genx.bf.cvt uses half (_Float16) as bit representation for bfloat16 |
| 41 | +using vc_be_bfloat16_raw_t = _Float16; |
| 42 | +#endif // __SYCL_DEVICE_ONLY__ |
| 43 | + |
| 44 | +// ------------------- Type conversion traits |
| 45 | + |
| 46 | +template <int N> struct vector_conversion_traits<bfloat16, N> { |
| 47 | + using StdT = __cpp_t<bfloat16>; |
| 48 | + using StdVecT = vector_type_t<StdT, N>; |
| 49 | + using RawT = __raw_t<bfloat16>; |
| 50 | + |
| 51 | + static ESIMD_INLINE vector_type_t<RawT, N> |
| 52 | + convert_to_raw(vector_type_t<StdT, N> Val) { |
| 53 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 54 | + using RawVecT = vector_type_t<vc_be_bfloat16_raw_t, N>; |
| 55 | + RawVecT ConvVal = __esimd_bf_cvt<vc_be_bfloat16_raw_t, StdT, N>(Val); |
| 56 | + // cast from _Float16 to int16_t: |
| 57 | + return __esimd_bitcast<vector_type_t<RawT, N>>(ConvVal); |
| 58 | +#else |
| 59 | + vector_type_t<RawT, N> Output = 0; |
| 60 | + |
| 61 | + for (int i = 0; i < N; i++) { |
| 62 | + Output[i] = bfloat16(Val[i]).raw(); |
| 63 | + } |
| 64 | + return Output; |
| 65 | +#endif // __SYCL_DEVICE_ONLY__ |
| 66 | + } |
| 67 | + |
| 68 | + static ESIMD_INLINE vector_type_t<StdT, N> |
| 69 | + convert_to_cpp(vector_type_t<RawT, N> Val) { |
| 70 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 71 | + using RawVecT = vector_type_t<vc_be_bfloat16_raw_t, N>; |
| 72 | + RawVecT Bits = __esimd_bitcast<RawVecT>(Val); |
| 73 | + return __esimd_bf_cvt<StdT, vc_be_bfloat16_raw_t, N>(Bits); |
| 74 | +#else |
| 75 | + vector_type_t<StdT, N> Output; |
| 76 | + |
| 77 | + for (int i = 0; i < N; i++) { |
| 78 | + Output[i] = bfloat16::from_bits(Val[i]); |
| 79 | + } |
| 80 | + return Output; |
| 81 | +#endif // __SYCL_DEVICE_ONLY__ |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +template <> struct scalar_conversion_traits<bfloat16> { |
| 86 | + using RawT = __raw_t<bfloat16>; |
| 87 | + |
| 88 | + static ESIMD_INLINE RawT bitcast_to_raw(bfloat16 Val) { return Val.raw(); } |
| 89 | + |
| 90 | + static ESIMD_INLINE bfloat16 bitcast_to_wrapper(RawT Val) { |
| 91 | + return bfloat16::from_bits(Val); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +// bfloat16 uses default inefficient implementations of std C++ operations, |
| 96 | +// hence no specializations of other traits. |
| 97 | + |
| 98 | +// Misc |
| 99 | +inline std::ostream &operator<<(std::ostream &O, bfloat16 const &rhs) { |
| 100 | + O << static_cast<float>(rhs); |
| 101 | + return O; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace ext::intel::esimd::detail |
| 105 | +} // __SYCL_INLINE_VER_NAMESPACE(_V1) |
| 106 | +} // namespace sycl |
| 107 | + |
| 108 | +/// @endcond ESIMD_DETAIL |
0 commit comments