File tree Expand file tree Collapse file tree 2 files changed +13
-10
lines changed
include/sycl/ext/oneapi/experimental Expand file tree Collapse file tree 2 files changed +13
-10
lines changed Original file line number Diff line number Diff line change 13
13
14
14
#if !defined(__SYCL_DEVICE_ONLY__)
15
15
#include < cmath>
16
- #include < cstring> // for std::memcpy
17
16
#endif
18
17
19
18
namespace sycl {
@@ -64,11 +63,13 @@ class bfloat16 {
64
63
return __spirv_ConvertBF16ToFINTEL (a);
65
64
#endif
66
65
#else
67
- uint32_t bits = a;
68
- bits <<= 16 ;
69
- float res;
70
- std::memcpy (&res, &bits, sizeof (res));
71
- return res;
66
+ union {
67
+ uint32_t bits;
68
+ float res;
69
+ } val;
70
+ val.bits = a;
71
+ val.bits <<= 16 ;
72
+ return val.res ;
72
73
#endif
73
74
}
74
75
Original file line number Diff line number Diff line change @@ -73,15 +73,17 @@ int main() {
73
73
Success &= check_bf16_from_float (std::numeric_limits<float >::quiet_NaN (),
74
74
std::stoi (" 1111111111000001" , nullptr , 2 ));
75
75
76
+ // see https://float.exposed/b0xffff
76
77
Success &= check_bf16_to_float (
77
78
0 , bitsToFloatConv (std::string (" 00000000000000000000000000000000" )));
78
79
Success &= check_bf16_to_float (
79
- 1 , bitsToFloatConv (std::string (" 01000111100000000000000000000000 " )));
80
+ 1 , bitsToFloatConv (std::string (" 00000000000000010000000000000000 " )));
80
81
Success &= check_bf16_to_float (
81
- 42 , bitsToFloatConv (std::string (" 01001010001010000000000000000000 " )));
82
+ 42 , bitsToFloatConv (std::string (" 00000000001010100000000000000000 " )));
82
83
Success &= check_bf16_to_float (
83
- std::numeric_limits<uint16_t >::max (),
84
- bitsToFloatConv (std::string (" 01001111011111111111111100000000" )));
84
+ // std::numeric_limits<uint16_t>::max() - 0xffff is bfloat16 -Nan and
85
+ // -Nan == -Nan check in check_bf16_to_float would fail, so use not Nan:
86
+ 65407 , bitsToFloatConv (std::string (" 11111111011111110000000000000000" )));
85
87
if (!Success)
86
88
return -1 ;
87
89
return 0 ;
You can’t perform that action at this time.
0 commit comments