|
| 1 | +#include <sycl/ext/oneapi/bfloat16.hpp> |
| 2 | +#include <sycl/sycl.hpp> |
| 3 | + |
| 4 | +#include <gtest/gtest.h> |
| 5 | + |
| 6 | +#include <cmath> |
| 7 | +#include <limits> |
| 8 | +#include <string> |
| 9 | + |
| 10 | +namespace { |
| 11 | +// Helper to convert the expected bits to float value to compare with the result. |
| 12 | +typedef union { |
| 13 | + float Value; |
| 14 | + struct { |
| 15 | + uint32_t Mantissa : 23; |
| 16 | + uint32_t Exponent : 8; |
| 17 | + uint32_t Sign : 1; |
| 18 | + } RawData; |
| 19 | +} floatConvHelper; |
| 20 | + |
| 21 | +float bitsToFloatConv(std::string Bits) { |
| 22 | + floatConvHelper Helper; |
| 23 | + Helper.RawData.Sign = static_cast<uint32_t>(Bits[0] - '0'); |
| 24 | + uint32_t Exponent = 0; |
| 25 | + for (size_t I = 1; I != 9; ++I) |
| 26 | + Exponent = Exponent + static_cast<uint32_t>(Bits[I] - '0') * std::pow(2, 8 - I); |
| 27 | + Helper.RawData.Exponent = Exponent; |
| 28 | + uint32_t Mantissa = 0; |
| 29 | + for (size_t I = 9; I != 32; ++I) |
| 30 | + Mantissa = Mantissa + static_cast<uint32_t>(Bits[I] - '0') * std::pow(2, 31 - I); |
| 31 | + Helper.RawData.Mantissa = Mantissa; |
| 32 | + return Helper.Value; |
| 33 | +} |
| 34 | +} // namespace |
| 35 | + |
| 36 | +TEST(BFloat16, BF16FromFloat) { |
| 37 | + sycl::ext::oneapi::bfloat16 B = 0.0f; |
| 38 | + auto Result = sycl::bit_cast<uint16_t>(B); |
| 39 | + ASSERT_EQ(Result, std::stoi("0000000000000000", nullptr, 2)); |
| 40 | + |
| 41 | + B = 42.0f; |
| 42 | + Result = sycl::bit_cast<uint16_t>(B); |
| 43 | + ASSERT_EQ(Result, std::stoi("100001000101000", nullptr, 2)); |
| 44 | + |
| 45 | + B = std::numeric_limits<float>::min(); |
| 46 | + Result = sycl::bit_cast<uint16_t>(B); |
| 47 | + ASSERT_EQ(Result, std::stoi("0000000010000000", nullptr, 2)); |
| 48 | + |
| 49 | + B = std::numeric_limits<float>::max(); |
| 50 | + Result = sycl::bit_cast<uint16_t>(B); |
| 51 | + ASSERT_EQ(Result, std::stoi("0111111110000000", nullptr, 2)); |
| 52 | + |
| 53 | + B = std::numeric_limits<float>::quiet_NaN(); |
| 54 | + Result = sycl::bit_cast<uint16_t>(B); |
| 55 | + ASSERT_EQ(Result, std::stoi("1111111111000001", nullptr, 2)); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(BFloat16, BF16ToFloat) { |
| 59 | + // See https://float.exposed/b0xffff |
| 60 | + uint16_t V = 0; |
| 61 | + float Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V); |
| 62 | + ASSERT_EQ(Res, |
| 63 | + bitsToFloatConv(std::string("00000000000000000000000000000000"))); |
| 64 | + |
| 65 | + V = 1; |
| 66 | + Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V); |
| 67 | + ASSERT_EQ(Res, |
| 68 | + bitsToFloatConv(std::string("00000000000000010000000000000000"))); |
| 69 | + |
| 70 | + V = 42; |
| 71 | + Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V); |
| 72 | + ASSERT_EQ(Res, |
| 73 | + bitsToFloatConv(std::string("00000000001010100000000000000000"))); |
| 74 | + |
| 75 | + // std::numeric_limits<uint16_t>::max() - 0xffff is bfloat16 -Nan and |
| 76 | + // -Nan == -Nan check in check_bf16_to_float would fail, so use not Nan: |
| 77 | + V = 65407; |
| 78 | + Res = sycl::bit_cast<sycl::ext::oneapi::bfloat16>(V); |
| 79 | + ASSERT_EQ(Res, |
| 80 | + bitsToFloatConv(std::string("11111111011111110000000000000000"))); |
| 81 | +} |
0 commit comments