|
| 1 | +//==--------- bfloat16.hpp ------- SYCL bfloat16 conversion ----------------==// |
| 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 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <CL/__spirv/spirv_ops.hpp> |
| 12 | + |
| 13 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 14 | +namespace sycl { |
| 15 | +namespace ext { |
| 16 | +namespace intel { |
| 17 | +namespace experimental { |
| 18 | + |
| 19 | +class [[sycl_detail::uses_aspects(ext_intel_bf16_conversion)]] bfloat16 { |
| 20 | + using storage_t = uint16_t; |
| 21 | + storage_t value; |
| 22 | + |
| 23 | +public: |
| 24 | + bfloat16() = default; |
| 25 | + bfloat16(const bfloat16 &) = default; |
| 26 | + ~bfloat16() = default; |
| 27 | + |
| 28 | + // Explicit conversion functions |
| 29 | + static storage_t from_float(const float &a) { |
| 30 | +#if defined(__SYCL_DEVICE_ONLY__) |
| 31 | + return __spirv_ConvertFToBF16INTEL(a); |
| 32 | +#else |
| 33 | + throw exception{errc::feature_not_supported, |
| 34 | + "Bfloat16 conversion is not supported on host device"}; |
| 35 | +#endif |
| 36 | + } |
| 37 | + static float to_float(const storage_t &a) { |
| 38 | +#if defined(__SYCL_DEVICE_ONLY__) |
| 39 | + return __spirv_ConvertBF16ToFINTEL(a); |
| 40 | +#else |
| 41 | + throw exception{errc::feature_not_supported, |
| 42 | + "Bfloat16 conversion is not supported on host device"}; |
| 43 | +#endif |
| 44 | + } |
| 45 | + |
| 46 | + // Direct initialization |
| 47 | + bfloat16(const storage_t &a) : value(a) {} |
| 48 | + |
| 49 | + // Implicit conversion from float to bfloat16 |
| 50 | + bfloat16(const float &a) { value = from_float(a); } |
| 51 | + |
| 52 | + bfloat16 &operator=(const float &rhs) { |
| 53 | + value = from_float(rhs); |
| 54 | + return *this; |
| 55 | + } |
| 56 | + |
| 57 | + // Implicit conversion from bfloat16 to float |
| 58 | + operator float() const { return to_float(value); } |
| 59 | + |
| 60 | + // Get raw bits representation of bfloat16 |
| 61 | + operator storage_t() const { return value; } |
| 62 | + |
| 63 | + // Logical operators (!,||,&&) are covered if we can cast to bool |
| 64 | + explicit operator bool() { return to_float(value) != 0.0f; } |
| 65 | + |
| 66 | + // Unary minus operator overloading |
| 67 | + friend bfloat16 operator-(bfloat16 &lhs) { |
| 68 | + return bfloat16{-to_float(lhs.value)}; |
| 69 | + } |
| 70 | + |
| 71 | +// Increment and decrement operators overloading |
| 72 | +#define OP(op) \ |
| 73 | + friend bfloat16 &operator op(bfloat16 &lhs) { \ |
| 74 | + float f = to_float(lhs.value); \ |
| 75 | + lhs.value = from_float(op f); \ |
| 76 | + return lhs; \ |
| 77 | + } \ |
| 78 | + friend bfloat16 operator op(bfloat16 &lhs, int) { \ |
| 79 | + bfloat16 old = lhs; \ |
| 80 | + operator op(lhs); \ |
| 81 | + return old; \ |
| 82 | + } |
| 83 | + OP(++) |
| 84 | + OP(--) |
| 85 | +#undef OP |
| 86 | + |
| 87 | + // Assignment operators overloading |
| 88 | +#define OP(op) \ |
| 89 | + friend bfloat16 &operator op(bfloat16 &lhs, const bfloat16 &rhs) { \ |
| 90 | + float f = static_cast<float>(lhs); \ |
| 91 | + f op static_cast<float>(rhs); \ |
| 92 | + return lhs = f; \ |
| 93 | + } \ |
| 94 | + template <typename T> \ |
| 95 | + friend bfloat16 &operator op(bfloat16 &lhs, const T &rhs) { \ |
| 96 | + float f = static_cast<float>(lhs); \ |
| 97 | + f op static_cast<float>(rhs); \ |
| 98 | + return lhs = f; \ |
| 99 | + } \ |
| 100 | + template <typename T> friend T &operator op(T &lhs, const bfloat16 &rhs) { \ |
| 101 | + float f = static_cast<float>(lhs); \ |
| 102 | + f op static_cast<float>(rhs); \ |
| 103 | + return lhs = f; \ |
| 104 | + } |
| 105 | + OP(+=) |
| 106 | + OP(-=) |
| 107 | + OP(*=) |
| 108 | + OP(/=) |
| 109 | +#undef OP |
| 110 | + |
| 111 | +// Binary operators overloading |
| 112 | +#define OP(type, op) \ |
| 113 | + friend type operator op(const bfloat16 &lhs, const bfloat16 &rhs) { \ |
| 114 | + return type{static_cast<float>(lhs) op static_cast<float>(rhs)}; \ |
| 115 | + } \ |
| 116 | + template <typename T> \ |
| 117 | + friend type operator op(const bfloat16 &lhs, const T &rhs) { \ |
| 118 | + return type{static_cast<float>(lhs) op static_cast<float>(rhs)}; \ |
| 119 | + } \ |
| 120 | + template <typename T> \ |
| 121 | + friend type operator op(const T &lhs, const bfloat16 &rhs) { \ |
| 122 | + return type{static_cast<float>(lhs) op static_cast<float>(rhs)}; \ |
| 123 | + } |
| 124 | + OP(bfloat16, +) |
| 125 | + OP(bfloat16, -) |
| 126 | + OP(bfloat16, *) |
| 127 | + OP(bfloat16, /) |
| 128 | + OP(bool, ==) |
| 129 | + OP(bool, !=) |
| 130 | + OP(bool, <) |
| 131 | + OP(bool, >) |
| 132 | + OP(bool, <=) |
| 133 | + OP(bool, >=) |
| 134 | +#undef OP |
| 135 | + |
| 136 | + // Bitwise(|,&,~,^), modulo(%) and shift(<<,>>) operations are not supported |
| 137 | + // for floating-point types. |
| 138 | +}; |
| 139 | + |
| 140 | +} // namespace experimental |
| 141 | +} // namespace intel |
| 142 | +} // namespace ext |
| 143 | + |
| 144 | +namespace __SYCL2020_DEPRECATED("use 'ext::intel' instead") INTEL { |
| 145 | + using namespace ext::intel; |
| 146 | +} |
| 147 | +} // namespace sycl |
| 148 | +} // __SYCL_INLINE_NAMESPACE(cl) |
0 commit comments