17
17
#include < limits>
18
18
#include < string>
19
19
20
- using sycl::ext::intel::experimental::bfloat16;
21
-
22
20
// Helper to convert the expected bits to float value to compare with the result
23
21
typedef union {
24
22
float Value;
@@ -29,30 +27,35 @@ typedef union {
29
27
} RawData;
30
28
} floatConvHelper;
31
29
32
- float bitsToFloatConv (std::string & Bits) {
33
- floatConvHelper & Helper;
30
+ float bitsToFloatConv (std::string Bits) {
31
+ floatConvHelper Helper;
34
32
Helper.RawData .Sign = static_cast <uint32_t >(Bits[0 ] - ' 0' );
35
33
uint32_t Exponent = 0 ;
36
34
for (size_t I = 1 ; I != 9 ; ++I)
37
35
Exponent = Exponent + static_cast <uint32_t >(Bits[I] - ' 0' ) * pow (2 , 8 - I);
38
36
Helper.RawData .Exponent = Exponent;
39
37
uint32_t Mantissa = 0 ;
40
38
for (size_t I = 9 ; I != 32 ; ++I)
41
- Mantissa = Mantissa + static_cast <uint32_t >(Bits[I] - ' 0' ) * pow (2 , 8 - I);
39
+ Mantissa = Mantissa + static_cast <uint32_t >(Bits[I] - ' 0' ) * pow (2 , 31 - I);
42
40
Helper.RawData .Mantissa = Mantissa;
41
+ return Helper.Value ;
43
42
}
44
43
45
- inline bool check_bf16_from_float (float &Val, uint16_t &Expected) {
46
- if (from_float (Val) != Expected) {
47
- std::cout << " from_float check for Val = " << Val << " failed!\n " ;
44
+ bool check_bf16_from_float (float Val, uint16_t Expected) {
45
+ uint16_t Result = sycl::ext::intel::experimental::bfloat16::from_float (Val);
46
+ if (Result != Expected) {
47
+ std::cout << " from_float check for Val = " << Val << " failed!\n "
48
+ << " Expected " << Expected << " Got " << Result << " \n " ;
48
49
return false ;
49
50
}
50
51
return true ;
51
52
}
52
53
53
- inline bool check_bf16_to_float (uint16_t &Val, float &Expected) {
54
- if (to_float (Val) != Expected) {
55
- std::cout << " to_float check for Val = " << Val << " failed!\n " ;
54
+ bool check_bf16_to_float (uint16_t Val, float Expected) {
55
+ float Result = sycl::ext::intel::experimental::bfloat16::to_float (Val);
56
+ if (Result != Expected) {
57
+ std::cout << " to_float check for Val = " << Val << " failed!\n "
58
+ << " Expected " << Expected << " Got " << Result << " \n " ;
56
59
return false ;
57
60
}
58
61
return true ;
@@ -71,14 +74,14 @@ int main() {
71
74
std::stoi (" 1111111111000001" , nullptr , 2 ));
72
75
73
76
Success &= check_bf16_to_float (
74
- to_float ( 0 ) , bitsToFloatConv (" 00000000000000000000000000000000" ));
77
+ 0 , bitsToFloatConv (std::string ( " 00000000000000000000000000000000" ) ));
75
78
Success &= check_bf16_to_float (
76
- to_float ( 1 ) , bitsToFloatConv (" 01000111100000000000000000000000" ));
79
+ 1 , bitsToFloatConv (std::string ( " 01000111100000000000000000000000" ) ));
77
80
Success &= check_bf16_to_float (
78
- to_float ( 42 ) , bitsToFloatConv (" 00000000001010100000000000000000 " ));
79
- Success &=
80
- check_bf16_to_float ( to_float ( std::numeric_limits<uint16_t >::max () ),
81
- bitsToFloatConv (" 11111111111111110000000000000000 " ));
81
+ 42 , bitsToFloatConv (std::string ( " 01001010001010000000000000000000 " ) ));
82
+ Success &= check_bf16_to_float (
83
+ std::numeric_limits<uint16_t >::max (),
84
+ bitsToFloatConv (std::string ( " 01001111011111111111111100000000 " ) ));
82
85
if (!Success)
83
86
return -1 ;
84
87
return 0 ;
0 commit comments