Skip to content

Commit 46f81d6

Browse files
committed
Improvements for bfloat16 class
Add conversion operator for sycl::half Conversion operator for storage_t is replaced by 'raw()' method Signed-off-by: Alexey Sotkin <[email protected]>
1 parent 83da9f2 commit 46f81d6

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

sycl/include/sycl/ext/intel/experimental/bfloat16.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#pragma once
1010

1111
#include <CL/__spirv/spirv_ops.hpp>
12+
#include <CL/sycl/half_type.hpp>
1213

1314
__SYCL_INLINE_NAMESPACE(cl) {
1415
namespace sycl {
@@ -59,9 +60,10 @@ class [[sycl_detail::uses_aspects(ext_intel_bf16_conversion)]] bfloat16 {
5960

6061
// Implicit conversion from bfloat16 to float
6162
operator float() const { return to_float(value); }
63+
operator sycl::half() const { return to_float(value); }
6264

6365
// Get raw bits representation of bfloat16
64-
operator storage_t() const { return value; }
66+
storage_t raw() const { return value; }
6567

6668
// Logical operators (!,||,&&) are covered if we can cast to bool
6769
explicit operator bool() { return to_float(value) != 0.0f; }

sycl/test/extensions/bfloat16.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using sycl::ext::intel::experimental::bfloat16;
99

1010
SYCL_EXTERNAL uint16_t some_bf16_intrinsic(uint16_t x, uint16_t y);
11+
SYCL_EXTERNAL void foo(long x, sycl::half y);
1112

1213
__attribute__((noinline)) float op(float a, float b) {
1314
// CHECK: define {{.*}} spir_func float @_Z2opff(float [[a:%.*]], float [[b:%.*]])
@@ -27,11 +28,22 @@ __attribute__((noinline)) float op(float a, float b) {
2728
// CHECK-NOT: uitofp
2829
// CHECK-NOT: fptoui
2930

30-
bfloat16 D = bfloat16::from_bits(some_bf16_intrinsic(A, C));
31+
bfloat16 D = bfloat16::from_bits(some_bf16_intrinsic(A.raw(), C.raw()));
3132
// CHECK: [[D:%.*]] = tail call spir_func zeroext i16 @_Z19some_bf16_intrinsictt(i16 zeroext [[A]], i16 zeroext [[C]])
3233
// CHECK-NOT: uitofp
3334
// CHECK-NOT: fptoui
3435

36+
long L = bfloat16(3.14f);
37+
// CHECK: [[L_bfloat16:%.*]] = tail call spir_func zeroext i16 @_Z27__spirv_ConvertFToBF16INTELf(float 0x40091EB860000000)
38+
// CHECK: [[L_float:%.*]] = tail call spir_func float @_Z27__spirv_ConvertBF16ToFINTELt(i16 zeroext [[L_bfloat16]])
39+
// CHECK: [[L:%.*]] = fptosi float [[L_float]] to i{{32|64}}
40+
41+
sycl::half H = bfloat16(2.71f);
42+
// CHECK: [[H_bfloat16:%.*]] = tail call spir_func zeroext i16 @_Z27__spirv_ConvertFToBF16INTELf(float 0x4005AE1480000000)
43+
// CHECK: [[H_float:%.*]] = tail call spir_func float @_Z27__spirv_ConvertBF16ToFINTELt(i16 zeroext [[H_bfloat16]])
44+
// CHECK: [[H:%.*]] = fptrunc float [[H_float]] to half
45+
foo(L, H);
46+
3547
return D;
3648
// CHECK: [[RetVal:%.*]] = tail call spir_func float @_Z27__spirv_ConvertBF16ToFINTELt(i16 zeroext [[D]])
3749
// CHECK: ret float [[RetVal]]

0 commit comments

Comments
 (0)